File tree Expand file tree Collapse file tree
instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk
src/opentelemetry/instrumentation/claude_agent_sdk Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -608,6 +608,10 @@ def _process_stream_event_message(
608608 if isinstance (event , dict ):
609609 session_id = event .get ("session_id" )
610610
611+ if not session_id :
612+ # Entry baggage is already applied when the agent invocation starts.
613+ return
614+
611615 _set_session_id (agent_invocation , session_id )
612616
613617
Original file line number Diff line number Diff line change 2222
2323from opentelemetry import baggage
2424from opentelemetry import context as otel_context
25+ from opentelemetry .instrumentation .claude_agent_sdk import (
26+ patch as claude_patch ,
27+ )
2528from opentelemetry .instrumentation .claude_agent_sdk .patch import (
2629 _process_agent_invocation_stream ,
2730 wrap_claude_client_query ,
@@ -257,6 +260,23 @@ async def test_stream_event_dict_session_fallback(
257260 assert llm_span .attributes [GEN_AI_SESSION_ID ] == "sess-event-dict"
258261
259262
263+ def test_stream_event_without_session_skips_baggage_lookup (monkeypatch ):
264+ def fail_baggage_lookup ():
265+ raise AssertionError ("unexpected per-event baggage lookup" )
266+
267+ monkeypatch .setattr (
268+ claude_patch ,
269+ "_entry_baggage_identity_attributes" ,
270+ fail_baggage_lookup ,
271+ )
272+ agent_invocation = SimpleNamespace (conversation_id = None , attributes = {})
273+
274+ claude_patch ._process_stream_event_message (StreamEvent (), agent_invocation )
275+
276+ assert agent_invocation .conversation_id is None
277+ assert GEN_AI_SESSION_ID not in agent_invocation .attributes
278+
279+
260280@pytest .mark .asyncio
261281async def test_client_query_session_id_is_used_before_result_message (
262282 tracer_provider , span_exporter
You can’t perform that action at this time.
0 commit comments