Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Add otel ignore_context_propagation as opt in

jyerosbb628e974a12
diff --git a/litellm/integrations/opentelemetry.py b/litellm/integrations/opentelemetry.py
index 7689a6cc7e4..559ed05d30a 100644
--- a/litellm/integrations/opentelemetry.py
+++ b/litellm/integrations/opentelemetry.py
@@ -11,7 +11,7 @@ from litellm.integrations._types.open_inference import (
)
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps
-from litellm.secret_managers.main import get_secret_bool
+from litellm.secret_managers.main import get_secret_bool, str_to_bool
from litellm.types.services import ServiceLoggerPayload
from litellm.types.utils import (
ChatCompletionMessageToolCall,
@@ -68,6 +68,7 @@ class OpenTelemetryConfig:
service_name: Optional[str] = None
deployment_environment: Optional[str] = None
model_id: Optional[str] = None
+ ignore_context_propagation: Optional[bool] = None
def __post_init__(self) -> None:
# If endpoint is specified but exporter is still the default "console",
@@ -89,6 +90,10 @@ class OpenTelemetryConfig:
)
if not self.model_id:
self.model_id = os.getenv("OTEL_MODEL_ID", self.service_name)
+ if self.ignore_context_propagation is None:
+ self.ignore_context_propagation = str_to_bool(
+ os.getenv("OTEL_IGNORE_CONTEXT_PROPAGATION")
+ )
@classmethod
def from_env(cls):
@@ -710,12 +715,7 @@ class OpenTelemetry(CustomLogger):
)
ctx, parent_span = self._get_span_context(kwargs)
- # CRITICAL FIX: For langfuse_otel, ALWAYS create primary spans
- # Don't use parent spans from other providers as they cause trace corruption
- is_langfuse_otel = (
- hasattr(self, "callback_name") and self.callback_name == "langfuse_otel"
- )
- if is_langfuse_otel:
+ if self.config.ignore_context_propagation:
parent_span = None # Ignore parent spans from other providers
ctx = None
@@ -1256,12 +1256,7 @@ class OpenTelemetry(CustomLogger):
)
_parent_context, parent_otel_span = self._get_span_context(kwargs)
- # CRITICAL FIX: For langfuse_otel, ALWAYS create primary spans
- # Don't use parent spans from other providers as they cause trace corruption
- is_langfuse_otel = (
- hasattr(self, "callback_name") and self.callback_name == "langfuse_otel"
- )
- if is_langfuse_otel:
+ if self.config.ignore_context_propagation:
parent_otel_span = None # Ignore parent spans from other providers
_parent_context = None
diff --git a/tests/test_litellm/integrations/test_opentelemetry.py b/tests/test_litellm/integrations/test_opentelemetry.py
index 7f3b44e1420..3d6fc146a08 100644
--- a/tests/test_litellm/integrations/test_opentelemetry.py
+++ b/tests/test_litellm/integrations/test_opentelemetry.py
@@ -2175,7 +2175,7 @@ class TestOpenTelemetrySemanticConventions138(unittest.TestCase):
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
otel = OpenTelemetry(
- callback_name="langfuse_otel",
+ config=OpenTelemetryConfig(ignore_context_propagation=True),
tracer_provider=tracer_provider,
)
otel.tracer = tracer_provider.get_tracer("litellm")