Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(dify-agent): handle non-recording application tracing parents (#44803)
zyssyz123d3e897cfb702
diff --git a/dify-agent/src/dify_agent/runtime/observability.py b/dify-agent/src/dify_agent/runtime/observability.pyindex 93d7523b799..1c1f7224490 100644--- a/dify-agent/src/dify_agent/runtime/observability.py+++ b/dify-agent/src/dify_agent/runtime/observability.py@@ -307,7 +307,9 @@ class _ApplicationRunTracer(_RunScopedTracer):# Preserve only this invocation's descendants. Platform spans, a previous# invocation, or caller-supplied ambient context must not choose the root.if isinstance(current, ReadableSpan):- attributes = current.attributes or {}+ # Logfire also wraps non-recording spans as ReadableSpan. Without+ # attributes they cannot prove invocation ownership; use the API parent.+ attributes = getattr(current, "attributes", None) or {}current_context = current.get_span_context()if (attributes.get(DIFY_TRACING_RUN_ID_ATTRIBUTE) == str(self.tracing.run_id)diff --git a/dify-agent/tests/local/dify_agent/runtime/test_application_tracing.py b/dify-agent/tests/local/dify_agent/runtime/test_application_tracing.pyindex e9d37cbaa00..56ab68f454e 100644--- a/dify-agent/tests/local/dify_agent/runtime/test_application_tracing.py+++ b/dify-agent/tests/local/dify_agent/runtime/test_application_tracing.py@@ -13,11 +13,11 @@ from uuid import uuid4import httpximport logfireimport pytest-from opentelemetry.sdk.trace import TracerProvider+from opentelemetry.sdk.trace import ReadableSpan, TracerProviderfrom opentelemetry.sdk.trace.export import SimpleSpanProcessorfrom opentelemetry.sdk.trace.export import SpanExportResultfrom opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter-from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags, use_span+from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags, set_span_in_context, use_spanfrom pydantic import ValidationErrorfrom pydantic_ai import Toolfrom pydantic_ai.models.test import TestModel@@ -138,7 +138,8 @@ def _tool() -> str:@pytest.mark.parametrize("capture_content", [False, True])-def test_actual_agent_tree_uses_api_parent_and_per_run_content_policy(inbox, capture_content):[email protected]("ambient_parent", ["otel", "logfire-unsampled", "logfire-recording"])+def test_actual_agent_tree_uses_api_parent_and_per_run_content_policy(inbox, capture_content, ambient_parent):collector = configured_collector(inbox)tracing = tracing_context(capture_content=capture_content)agent = create_agent(@@ -147,9 +148,38 @@ def test_actual_agent_tree_uses_api_parent_and_per_run_content_policy(inbox, capcapabilities=[],)collector.instrument(agent, execution_context=execution_context(tracing))- unrelated = NonRecordingSpan(SpanContext(trace_id=1, span_id=2, is_remote=True, trace_flags=TraceFlags(1)))+ platform_client = None+ unrelated = NonRecordingSpan(+ SpanContext(+ trace_id=1,+ span_id=2,+ is_remote=True,+ trace_flags=TraceFlags(0 if ambient_parent == "logfire-unsampled" else 1),+ )+ )+ if ambient_parent != "otel":+ platform_client = logfire.configure(+ local=True,+ send_to_logfire=False,+ console=False,+ metrics=False,+ inspect_arguments=False,+ )+ unrelated = (+ platform_client.config.get_tracer_provider()+ .get_tracer("platform")+ .start_span("platform-parent", context=set_span_in_context(unrelated))+ )+ # The real Logfire wrapper advertises ReadableSpan even though the+ # underlying non-recording OTel span has no attributes to inspect.+ assert isinstance(unrelated, ReadableSpan)+ if ambient_parent == "logfire-unsampled":+ assert isinstance(unrelated.span, NonRecordingSpan)+ else:+ assert unrelated.is_recording()+ assert unrelated.get_span_context().is_validtry:- with use_span(unrelated):+ with use_span(unrelated, end_on_exit=True):assert agent.run_sync("prompt-private-content").output == "answer-private-content"assert asyncio.run(collector.finish_invocation(tracing))spans = [span for batch in inbox.batches for span in batch["spans"]]@@ -173,6 +203,8 @@ def test_actual_agent_tree_uses_api_parent_and_per_run_content_policy(inbox, capassert set(inbox.headers) == {"test-inner-key"}finally:asyncio.run(collector.aclose())+ if platform_client is not None:+ platform_client.shutdown()def test_retry_reuses_exact_batch_and_completion_waits_for_ack(inbox):