diff --git a/src/langtrace_python_sdk/instrumentation/anthropic/patch.py b/src/langtrace_python_sdk/instrumentation/anthropic/patch.py index 69618667..b992dc9a 100644 --- a/src/langtrace_python_sdk/instrumentation/anthropic/patch.py +++ b/src/langtrace_python_sdk/instrumentation/anthropic/patch.py @@ -23,6 +23,7 @@ get_langtrace_attributes, get_llm_request_attributes, get_llm_url, + get_span_name, is_streaming, set_event_completion, set_event_completion_chunk, @@ -64,7 +65,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - APIS["MESSAGES_CREATE"]["METHOD"], kind=SpanKind.CLIENT + name=get_span_name(APIS["MESSAGES_CREATE"]["METHOD"]), kind=SpanKind.CLIENT ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) diff --git a/src/langtrace_python_sdk/instrumentation/chroma/patch.py b/src/langtrace_python_sdk/instrumentation/chroma/patch.py index 3142c0dd..3159c5c5 100644 --- a/src/langtrace_python_sdk/instrumentation/chroma/patch.py +++ b/src/langtrace_python_sdk/instrumentation/chroma/patch.py @@ -16,6 +16,7 @@ from langtrace.trace_attributes import DatabaseSpanAttributes from langtrace_python_sdk.utils import set_span_attribute +from langtrace_python_sdk.utils.llm import get_span_name from langtrace_python_sdk.utils.silently_fail import silently_fail from opentelemetry import baggage, trace from opentelemetry.trace import SpanKind @@ -60,7 +61,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = DatabaseSpanAttributes(**span_attributes) with tracer.start_as_current_span( - api["METHOD"], + name=get_span_name(api["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/cohere/patch.py b/src/langtrace_python_sdk/instrumentation/cohere/patch.py index 560ff631..8930f297 100644 --- a/src/langtrace_python_sdk/instrumentation/cohere/patch.py +++ b/src/langtrace_python_sdk/instrumentation/cohere/patch.py @@ -21,6 +21,7 @@ get_llm_request_attributes, get_extra_attributes, get_llm_url, + get_span_name, set_event_completion, set_event_completion_chunk, set_usage_attributes, @@ -57,7 +58,9 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) - span = tracer.start_span(APIS["RERANK"]["METHOD"], kind=SpanKind.CLIENT) + span = tracer.start_span( + name=get_span_name(APIS["RERANK"]["METHOD"]), kind=SpanKind.CLIENT + ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) try: @@ -137,7 +140,10 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) - span = tracer.start_span(APIS["EMBED"]["METHOD"], kind=SpanKind.CLIENT) + span = tracer.start_span( + name=get_span_name(APIS["EMBED"]["METHOD"]), + kind=SpanKind.CLIENT, + ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) try: @@ -225,7 +231,9 @@ def traced_method(wrapped, instance, args, kwargs): # stringify the list of objects attributes.llm_tool_results = json.dumps(kwargs.get("tool_results")) - span = tracer.start_span(APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT) + span = tracer.start_span( + name=get_span_name(APIS["CHAT_CREATE"]["METHOD"]), kind=SpanKind.CLIENT + ) # Set the attributes on the span for field, value in attributes.model_dump(by_alias=True).items(): @@ -391,7 +399,9 @@ def traced_method(wrapped, instance, args, kwargs): # stringify the list of objects attributes.llm_tool_results = json.dumps(kwargs.get("tool_results")) - span = tracer.start_span(APIS["CHAT_STREAM"]["METHOD"], kind=SpanKind.CLIENT) + span = tracer.start_span( + name=get_span_name(APIS["CHAT_STREAM"]["METHOD"]), kind=SpanKind.CLIENT + ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) try: diff --git a/src/langtrace_python_sdk/instrumentation/crewai/patch.py b/src/langtrace_python_sdk/instrumentation/crewai/patch.py index 0846a19b..5bc07b81 100644 --- a/src/langtrace_python_sdk/instrumentation/crewai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/crewai/patch.py @@ -2,6 +2,7 @@ from importlib_metadata import version as v from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME from langtrace_python_sdk.utils import set_span_attribute +from langtrace_python_sdk.utils.llm import get_span_name from langtrace_python_sdk.utils.silently_fail import silently_fail from langtrace_python_sdk.constants.instrumentation.common import ( LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, @@ -143,7 +144,9 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) - with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span: + with tracer.start_as_current_span( + get_span_name(operation_name), kind=SpanKind.CLIENT + ) as span: _set_input_attributes(span, kwargs, attributes) try: diff --git a/src/langtrace_python_sdk/instrumentation/gemini/patch.py b/src/langtrace_python_sdk/instrumentation/gemini/patch.py index ceedb379..7e7cc68d 100644 --- a/src/langtrace_python_sdk/instrumentation/gemini/patch.py +++ b/src/langtrace_python_sdk/instrumentation/gemini/patch.py @@ -10,6 +10,7 @@ get_langtrace_attributes, get_llm_request_attributes, get_llm_url, + get_span_name, is_streaming, set_event_completion, set_event_completion_chunk, @@ -35,7 +36,7 @@ def traced_method(wrapped, instance, args, kwargs): } attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - name=name, + name=get_span_name(name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) @@ -76,7 +77,7 @@ async def traced_method(wrapped, instance, args, kwargs): } attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - name=name, + name=get_span_name(name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) diff --git a/src/langtrace_python_sdk/instrumentation/groq/patch.py b/src/langtrace_python_sdk/instrumentation/groq/patch.py index 11c89a88..eb64eb1a 100644 --- a/src/langtrace_python_sdk/instrumentation/groq/patch.py +++ b/src/langtrace_python_sdk/instrumentation/groq/patch.py @@ -29,6 +29,7 @@ get_llm_request_attributes, get_llm_url, get_langtrace_attributes, + get_span_name, set_event_completion, set_event_completion_chunk, set_usage_attributes, @@ -107,7 +108,7 @@ 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"], + name=get_span_name(APIS["CHAT_COMPLETION"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) @@ -335,7 +336,7 @@ async 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 + name=get_span_name(APIS["CHAT_COMPLETION"]["METHOD"]), kind=SpanKind.CLIENT ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) diff --git a/src/langtrace_python_sdk/instrumentation/langchain/patch.py b/src/langtrace_python_sdk/instrumentation/langchain/patch.py index 4d761ada..511ee137 100644 --- a/src/langtrace_python_sdk/instrumentation/langchain/patch.py +++ b/src/langtrace_python_sdk/instrumentation/langchain/patch.py @@ -18,6 +18,7 @@ from langtrace.trace_attributes import FrameworkSpanAttributes from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace.propagation import set_span_in_context from opentelemetry.trace import SpanKind, StatusCode @@ -57,7 +58,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method_name, + name=get_span_name(method_name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py b/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py index e7c15b8b..a1afe26c 100644 --- a/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py +++ b/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py @@ -17,6 +17,7 @@ import json from langtrace.trace_attributes import FrameworkSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace.propagation import set_span_in_context @@ -55,7 +56,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method_name, + name=get_span_name(method_name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py b/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py index 6d51504e..128eab0f 100644 --- a/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py +++ b/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py @@ -17,6 +17,7 @@ import json from langtrace.trace_attributes import FrameworkSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace import SpanKind, StatusCode from opentelemetry.trace.status import Status @@ -75,7 +76,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method_name, + name=get_span_name(method_name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/langgraph/patch.py b/src/langtrace_python_sdk/instrumentation/langgraph/patch.py index 3b09732a..8d736914 100644 --- a/src/langtrace_python_sdk/instrumentation/langgraph/patch.py +++ b/src/langtrace_python_sdk/instrumentation/langgraph/patch.py @@ -15,6 +15,7 @@ """ import json +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry.trace.propagation import set_span_in_context from langtrace.trace_attributes import FrameworkSpanAttributes @@ -52,7 +53,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method_name, + name=get_span_name(method_name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py b/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py index eccd4f64..fe03adac 100644 --- a/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py +++ b/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py @@ -15,6 +15,7 @@ """ from langtrace.trace_attributes import FrameworkSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace import SpanKind from opentelemetry.trace.status import Status, StatusCode @@ -49,7 +50,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = FrameworkSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method, + name=get_span_name(method), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -94,7 +95,9 @@ async 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( + name=get_span_name(method), kind=SpanKind.CLIENT + ) as span: async for field, value in attributes.model_dump(by_alias=True).items(): if value is not None: span.set_attribute(field, value) diff --git a/src/langtrace_python_sdk/instrumentation/ollama/patch.py b/src/langtrace_python_sdk/instrumentation/ollama/patch.py index fd2ce922..a3704697 100644 --- a/src/langtrace_python_sdk/instrumentation/ollama/patch.py +++ b/src/langtrace_python_sdk/instrumentation/ollama/patch.py @@ -5,6 +5,7 @@ get_langtrace_attributes, get_llm_request_attributes, get_llm_url, + get_span_name, set_event_completion, set_event_completion_chunk, ) @@ -35,7 +36,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) with tracer.start_as_current_span( - f'ollama.{api["METHOD"]}', kind=SpanKind.CLIENT + name=get_span_name(f'ollama.{api["METHOD"]}'), kind=SpanKind.CLIENT ) as span: _set_input_attributes(span, kwargs, attributes) diff --git a/src/langtrace_python_sdk/instrumentation/openai/patch.py b/src/langtrace_python_sdk/instrumentation/openai/patch.py index 8a08707d..6541801e 100644 --- a/src/langtrace_python_sdk/instrumentation/openai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/openai/patch.py @@ -37,6 +37,7 @@ get_langtrace_attributes, get_llm_request_attributes, get_llm_url, + get_span_name, get_tool_calls, is_streaming, set_event_completion, @@ -64,7 +65,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) with tracer.start_as_current_span( - APIS["IMAGES_GENERATION"]["METHOD"], + name=get_span_name(APIS["IMAGES_GENERATION"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -127,7 +128,7 @@ async def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) with tracer.start_as_current_span( - APIS["IMAGES_GENERATION"]["METHOD"], + name=get_span_name(APIS["IMAGES_GENERATION"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -192,7 +193,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) with tracer.start_as_current_span( - APIS["IMAGES_EDIT"]["METHOD"], + name=APIS["IMAGES_EDIT"]["METHOD"], kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -282,7 +283,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - APIS["CHAT_COMPLETION"]["METHOD"], + name=get_span_name(APIS["CHAT_COMPLETION"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) @@ -376,7 +377,7 @@ async def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - APIS["CHAT_COMPLETION"]["METHOD"], + name=get_span_name(APIS["CHAT_COMPLETION"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) @@ -455,7 +456,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = LLMSpanAttributes(**span_attributes) with tracer.start_as_current_span( - APIS["EMBEDDINGS_CREATE"]["METHOD"], + name=get_span_name(APIS["EMBEDDINGS_CREATE"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -512,7 +513,7 @@ async def traced_method(wrapped, instance, args, kwargs): ) with tracer.start_as_current_span( - APIS["EMBEDDINGS_CREATE"]["METHOD"], + name=get_span_name(APIS["EMBEDDINGS_CREATE"]["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/pinecone/patch.py b/src/langtrace_python_sdk/instrumentation/pinecone/patch.py index e3b40f3c..91c4f9d4 100644 --- a/src/langtrace_python_sdk/instrumentation/pinecone/patch.py +++ b/src/langtrace_python_sdk/instrumentation/pinecone/patch.py @@ -17,6 +17,7 @@ import json from langtrace.trace_attributes import DatabaseSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace import SpanKind from opentelemetry.trace.status import Status, StatusCode @@ -57,7 +58,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = DatabaseSpanAttributes(**span_attributes) with tracer.start_as_current_span( - api["METHOD"], + name=get_span_name(api["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/qdrant/patch.py b/src/langtrace_python_sdk/instrumentation/qdrant/patch.py index b097616d..4f93cbc4 100644 --- a/src/langtrace_python_sdk/instrumentation/qdrant/patch.py +++ b/src/langtrace_python_sdk/instrumentation/qdrant/patch.py @@ -16,6 +16,7 @@ import json from langtrace.trace_attributes import DatabaseSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from langtrace_python_sdk.utils.silently_fail import silently_fail from langtrace_python_sdk.utils import set_span_attribute from opentelemetry import baggage, trace @@ -58,7 +59,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = DatabaseSpanAttributes(**span_attributes) with tracer.start_as_current_span( - api["METHOD"], + name=get_span_name(api["METHOD"]), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/instrumentation/vertexai/patch.py b/src/langtrace_python_sdk/instrumentation/vertexai/patch.py index 26673c47..d6430d69 100644 --- a/src/langtrace_python_sdk/instrumentation/vertexai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/vertexai/patch.py @@ -8,6 +8,7 @@ get_langtrace_attributes, get_llm_request_attributes, get_llm_url, + get_span_name, set_event_completion, set_span_attributes, set_usage_attributes, @@ -39,7 +40,7 @@ def traced_method(wrapped, instance, args, kwargs): } attributes = LLMSpanAttributes(**span_attributes) span = tracer.start_span( - name=name, + name=get_span_name(name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) diff --git a/src/langtrace_python_sdk/instrumentation/weaviate/patch.py b/src/langtrace_python_sdk/instrumentation/weaviate/patch.py index e35ad498..46ce5ac3 100644 --- a/src/langtrace_python_sdk/instrumentation/weaviate/patch.py +++ b/src/langtrace_python_sdk/instrumentation/weaviate/patch.py @@ -17,6 +17,7 @@ import json from langtrace.trace_attributes import DatabaseSpanAttributes +from langtrace_python_sdk.utils.llm import get_span_name from opentelemetry import baggage, trace from opentelemetry.trace import SpanKind from opentelemetry.trace.status import Status, StatusCode @@ -132,7 +133,7 @@ def traced_method(wrapped, instance, args, kwargs): attributes = DatabaseSpanAttributes(**span_attributes) with tracer.start_as_current_span( - method_name, + name=get_span_name(method_name), kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/utils/llm.py b/src/langtrace_python_sdk/utils/llm.py index 748d415f..42ad53cc 100644 --- a/src/langtrace_python_sdk/utils/llm.py +++ b/src/langtrace_python_sdk/utils/llm.py @@ -33,6 +33,13 @@ import os +def get_span_name(operation_name): + extra_attributes = get_extra_attributes() + if extra_attributes is not None and "langtrace.span.name" in extra_attributes: + return f'{operation_name}-{extra_attributes["langtrace.span.name"]}' + return operation_name + + def estimate_tokens(prompt): """ Estimate the number of tokens in a prompt.""" diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index 0212ffee..00875564 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.2.17" +__version__ = "2.2.18"