Skip to content

Commit fff8fd3

Browse files
committed
fix: skip empty claude stream session updates
1 parent aed5bb8 commit fff8fd3

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

  • instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk

instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk/src/opentelemetry/instrumentation/claude_agent_sdk/patch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk/tests/test_session_capture.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
from opentelemetry import baggage
2424
from opentelemetry import context as otel_context
25+
from opentelemetry.instrumentation.claude_agent_sdk import (
26+
patch as claude_patch,
27+
)
2528
from 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
261281
async def test_client_query_session_id_is_used_before_result_message(
262282
tracer_provider, span_exporter

0 commit comments

Comments
 (0)