Skip to content

Commit

Permalink
Merge pull request #205 from Scale3-Labs/ali/sampling
Browse files Browse the repository at this point in the history
Ali/sampling
  • Loading branch information
alizenhom committed Jun 13, 2024
2 parents cbde43b + 7411eab commit b9fd119
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 52 deletions.
31 changes: 23 additions & 8 deletions src/examples/langchain_example/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

from langtrace_python_sdk import langtrace
from langtrace_python_sdk.utils.with_root_span import (
with_langtrace_root_span,
with_additional_attributes,
)
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
from openai import OpenAI

_ = load_dotenv(find_dotenv())

langtrace.init()
langtrace.init(
write_spans_to_console=False,
disable_tracing_for_functions={"langchain": ["RunnableSequence.invoke"]},
)


def api_call_1():
Expand All @@ -29,7 +30,8 @@ def api_call_1():
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
res = chain.invoke({"input": "how can langsmith help with testing?"})
print(res)
# print(res)
return res


def api_call_2():
Expand All @@ -43,13 +45,26 @@ def api_call_2():
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
res = chain.invoke({"input": "how can langsmith help with testing?"})
print(res)
# print(res)
return res


@with_langtrace_root_span()
def basic_app():
api_call_1()
api_call_2()
# api_call_2()
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Talk like a pirate"},
{"role": "user", "content": "Tell me a story in 3 sentences or less."},
],
# stream=True,
stream=False,
)

return response


@with_langtrace_root_span()
Expand Down
2 changes: 1 addition & 1 deletion src/examples/llamaindex_example/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

nest_asyncio.apply()

langtrace.init(write_spans_to_console=False)
langtrace.init()


def multiply(a: int, b: int) -> int:
Expand Down
6 changes: 5 additions & 1 deletion src/examples/llamaindex_example/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
_ = load_dotenv(find_dotenv())


langtrace.init(write_spans_to_console=False)
langtrace.init(
disable_tracing_for_functions={
"open_ai": ["openai.chat.completions.create"],
}
)


@with_langtrace_root_span()
Expand Down
2 changes: 1 addition & 1 deletion src/examples/openai_example/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ def chat_completion():
]
result.append(content[0] if len(content) > 0 else "")

print("".join(result))
# print("".join(result))
return response
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"OPERATION": "query",
},
"DELETE": {
"METHOD": PineconeMethods.DELETE,
"METHOD": PineconeMethods.DELETE.value,
"ENDPOINT": "/vectors/delete",
"OPERATION": "delete",
},
Expand Down
10 changes: 7 additions & 3 deletions src/langtrace_python_sdk/instrumentation/chroma/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from langtrace.trace_attributes import DatabaseSpanAttributes
from langtrace_python_sdk.utils.llm import set_span_attributes
from langtrace_python_sdk.utils.silently_fail import silently_fail
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

from opentelemetry.trace.propagation import set_span_in_context
from langtrace_python_sdk.constants.instrumentation.chroma import APIS
from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
Expand Down Expand Up @@ -59,7 +59,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = DatabaseSpanAttributes(**span_attributes)

with tracer.start_as_current_span(api["METHOD"], kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
api["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
7 changes: 5 additions & 2 deletions src/langtrace_python_sdk/instrumentation/groq/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import json

from langtrace.trace_attributes import Event, LLMSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace.propagation import set_span_in_context
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

Expand Down Expand Up @@ -112,7 +113,9 @@ def traced_method(wrapped, instance, args, kwargs):
# with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"],
# kind=SpanKind.CLIENT) as span:
span = tracer.start_span(
APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT
APIS["CHAT_COMPLETION"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
)
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
Expand Down
10 changes: 8 additions & 2 deletions src/langtrace_python_sdk/instrumentation/langchain/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import json

from langtrace.trace_attributes import FrameworkSpanAttributes
from opentelemetry import baggage
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME
from opentelemetry import baggage, trace
from opentelemetry.trace.propagation import set_span_in_context
from opentelemetry.trace import SpanKind, StatusCode
from opentelemetry.trace.status import Status

Expand Down Expand Up @@ -54,7 +56,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method_name, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import json

from langtrace.trace_attributes import FrameworkSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace.propagation import set_span_in_context

from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

Expand Down Expand Up @@ -52,7 +54,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method_name, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
16 changes: 13 additions & 3 deletions src/langtrace_python_sdk/instrumentation/langchain_core/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import json

from langtrace.trace_attributes import FrameworkSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind, StatusCode
from opentelemetry.trace.status import Status
from opentelemetry.trace.propagation import set_span_in_context

from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
Expand Down Expand Up @@ -73,7 +74,12 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method_name, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:

for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down Expand Up @@ -147,7 +153,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method_name, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
9 changes: 7 additions & 2 deletions src/langtrace_python_sdk/instrumentation/langgraph/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"""

import json
from opentelemetry.trace.propagation import set_span_in_context

from langtrace.trace_attributes import FrameworkSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

Expand Down Expand Up @@ -50,7 +51,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method_name, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
10 changes: 7 additions & 3 deletions src/langtrace_python_sdk/instrumentation/llamaindex/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"""

from langtrace.trace_attributes import FrameworkSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

from opentelemetry.trace.propagation import set_span_in_context
from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
SERVICE_PROVIDERS,
Expand Down Expand Up @@ -48,7 +48,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = FrameworkSpanAttributes(**span_attributes)

with tracer.start_as_current_span(method, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
method,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
span.set_attribute(field, value)
Expand Down
28 changes: 20 additions & 8 deletions src/langtrace_python_sdk/instrumentation/openai/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

from importlib_metadata import version as v
from langtrace.trace_attributes import Event, LLMSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

from opentelemetry.trace.propagation import set_span_in_context
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME
from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
Expand Down Expand Up @@ -64,7 +64,9 @@ def traced_method(wrapped, instance, args, kwargs):
attributes = LLMSpanAttributes(**span_attributes)

with tracer.start_as_current_span(
APIS["IMAGES_GENERATION"]["METHOD"], kind=SpanKind.CLIENT
APIS["IMAGES_GENERATION"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
Expand Down Expand Up @@ -141,7 +143,9 @@ async def traced_method(wrapped, instance, args, kwargs):
attributes = LLMSpanAttributes(**span_attributes)

with tracer.start_as_current_span(
APIS["IMAGES_GENERATION"]["METHOD"], kind=SpanKind.CLIENT
APIS["IMAGES_GENERATION"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
items = attributes.model_dump(by_alias=True).items()
for field, value in items:
Expand Down Expand Up @@ -226,7 +230,9 @@ def traced_method(wrapped, instance, args, kwargs):
attributes = LLMSpanAttributes(**span_attributes)

with tracer.start_as_current_span(
APIS["IMAGES_EDIT"]["METHOD"], kind=SpanKind.CLIENT
APIS["IMAGES_EDIT"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
Expand Down Expand Up @@ -349,7 +355,9 @@ def traced_method(wrapped, instance, args, kwargs):
# with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"],
# kind=SpanKind.CLIENT) as span:
span = tracer.start_span(
APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT
APIS["CHAT_COMPLETION"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
)
for field, value in attributes.model_dump(by_alias=True).items():
if value is not None:
Expand Down Expand Up @@ -833,7 +841,9 @@ def traced_method(wrapped, instance, args, kwargs):
attributes["llm.user"] = kwargs.get("user")

with tracer.start_as_current_span(
APIS["EMBEDDINGS_CREATE"]["METHOD"], kind=SpanKind.CLIENT
APIS["EMBEDDINGS_CREATE"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:

for field, value in attributes.model_dump(by_alias=True).items():
Expand Down Expand Up @@ -898,7 +908,9 @@ async def traced_method(wrapped, instance, args, kwargs):
attributes["llm.user"] = kwargs.get("user")

with tracer.start_as_current_span(
APIS["EMBEDDINGS_CREATE"]["METHOD"], kind=SpanKind.CLIENT
APIS["EMBEDDINGS_CREATE"]["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:

async for field, value in attributes.model_dump(by_alias=True).items():
Expand Down
10 changes: 7 additions & 3 deletions src/langtrace_python_sdk/instrumentation/pinecone/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import json

from langtrace.trace_attributes import DatabaseSpanAttributes
from opentelemetry import baggage
from opentelemetry import baggage, trace
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode

from opentelemetry.trace.propagation import set_span_in_context
from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
SERVICE_PROVIDERS,
Expand Down Expand Up @@ -56,7 +56,11 @@ def traced_method(wrapped, instance, args, kwargs):

attributes = DatabaseSpanAttributes(**span_attributes)

with tracer.start_as_current_span(api["METHOD"], kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(
api["METHOD"],
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
) as span:

if span.is_recording():
set_span_attribute(span, "server.address", instance._config.host)
Expand Down
Loading

0 comments on commit b9fd119

Please sign in to comment.