@@ -3074,31 +3074,44 @@ def test_start_workflow_span_does_not_attach_as_current(span_exporter: InMemoryS
30743074 assert spans [0 ].attributes ["key" ] == "value" # type: ignore[index] # pyrefly: ignore[unsupported-operation] # ty: ignore[not-subscriptable]
30753075
30763076
3077- def test_create_workflow_span_uses_scoped_conversation_id (span_exporter : InMemorySpanExporter ) -> None :
3078- """An ambient conversation id is applied only within its workflow execution scope."""
3077+ @pytest .mark .parametrize ("workflow_name" , [None , "" , "customer_support_pipeline" ])
3078+ @pytest .mark .parametrize ("unattached" , [False , True ])
3079+ def test_workflow_span_uses_scoped_conversation_id (
3080+ span_exporter : InMemorySpanExporter , workflow_name : str | None , unattached : bool
3081+ ) -> None :
3082+ """Named and unnamed workflow spans retain scoped conversation ids with either span factory."""
30793083 from agent_framework .observability import (
30803084 OtelAttr ,
30813085 _use_telemetry_conversation_id , # pyright: ignore[reportPrivateUsage]
30823086 create_workflow_span ,
3087+ start_workflow_span ,
30833088 )
30843089
3090+ span_factory = start_workflow_span if unattached else create_workflow_span
3091+ attributes : dict [str , str | int ] = {OtelAttr .WORKFLOW_NAME : workflow_name } if workflow_name is not None else {}
30853092 span_exporter .clear () # type: ignore[attr-defined]
30863093 with _use_telemetry_conversation_id ("application-thread" ):
3087- with create_workflow_span (OtelAttr .WORKFLOW_RUN_SPAN ):
3094+ with span_factory (OtelAttr .WORKFLOW_RUN_SPAN , attributes = attributes ):
30883095 pass
3089- with create_workflow_span (
3096+ with span_factory (
30903097 OtelAttr .WORKFLOW_RUN_SPAN ,
3091- attributes = {OtelAttr .CONVERSATION_ID : "explicit-thread" },
3098+ attributes = {** attributes , OtelAttr .CONVERSATION_ID : "explicit-thread" },
30923099 ):
30933100 pass
3094- with create_workflow_span (OtelAttr .MESSAGE_SEND_SPAN ):
3101+ with span_factory (OtelAttr .MESSAGE_SEND_SPAN , attributes = attributes ):
30953102 pass
3096- with create_workflow_span (OtelAttr .WORKFLOW_RUN_SPAN ):
3103+ with span_factory (OtelAttr .WORKFLOW_RUN_SPAN , attributes = attributes ):
30973104 pass
30983105
30993106 spans = span_exporter .get_finished_spans () # type: ignore[attr-defined]
3100- workflow_spans = [span for span in spans if span .name == OtelAttr .WORKFLOW_RUN_SPAN ]
3107+ workflow_spans = [
3108+ span
3109+ for span in spans
3110+ if span .attributes is not None and span .attributes .get (OtelAttr .OPERATION ) == "invoke_workflow"
3111+ ]
31013112 assert len (workflow_spans ) == 3
3113+ expected_name = f"invoke_workflow { workflow_name } " if workflow_name else "invoke_workflow"
3114+ assert all (span .name == expected_name for span in workflow_spans )
31023115 ambient_attributes = workflow_spans [0 ].attributes
31033116 explicit_attributes = workflow_spans [1 ].attributes
31043117 unscoped_attributes = workflow_spans [2 ].attributes
@@ -3112,6 +3125,9 @@ def test_create_workflow_span_uses_scoped_conversation_id(span_exporter: InMemor
31123125 message_send_attributes = message_send_span .attributes
31133126 assert message_send_attributes is not None
31143127 assert OtelAttr .CONVERSATION_ID not in message_send_attributes
3128+ assert OtelAttr .OPERATION not in message_send_attributes
3129+ assert OtelAttr .CONVERSATION_ID not in attributes
3130+ assert OtelAttr .OPERATION not in attributes
31153131
31163132
31173133def test_create_processing_span (span_exporter ):
0 commit comments