fix(telemetry): propagate invoke_agent identity via OTEL Baggage (#55)#69
Closed
tiagoek wants to merge 2 commits into
Closed
fix(telemetry): propagate invoke_agent identity via OTEL Baggage (#55)#69tiagoek wants to merge 2 commits into
tiagoek wants to merge 2 commits into
Conversation
…e for third-party spans
When propagate=True on invoke_agent_span, mirror gen_ai.agent.{name,id,description}
into W3C Baggage so BaggageSpanProcessor applies them to spans created by external
instrumentation (LangGraph/Traceloop), fixing inconsistent agent attrs (GH #55).
Includes subprocess regression test mirroring LangGraph tracer.
Comment on lines
+51
to
+74
| @contextmanager | ||
| def _otel_baggage_for_invoke_agent(span_attrs: Dict[str, Any]): | ||
| """Mirror gen_ai.agent.* identity into W3C Baggage for the duration of the context. | ||
|
|
||
| Third-party instrumentations (e.g. LangGraph / Traceloop) create spans without merging | ||
| :func:`get_propagated_attributes`. :class:`~opentelemetry.processor.baggage.BaggageSpanProcessor` | ||
| (registered by :func:`sap_cloud_sdk.core.telemetry.auto_instrument.auto_instrument`) copies | ||
| baggage onto every started span, so agent id/name stay consistent on those spans. | ||
| """ | ||
| keys = ( | ||
| _ATTR_GEN_AI_AGENT_NAME, | ||
| _ATTR_GEN_AI_AGENT_ID, | ||
| _ATTR_GEN_AI_AGENT_DESCRIPTION, | ||
| ) | ||
| ctx = otel_context.get_current() | ||
| for key in keys: | ||
| val = span_attrs.get(key) | ||
| if val is not None: | ||
| ctx = otel_baggage.set_baggage(key, str(val), ctx) | ||
| token = otel_context.attach(ctx) | ||
| try: | ||
| yield | ||
| finally: | ||
| otel_context.detach(token) |
Contributor
There was a problem hiding this comment.
I don't think baggage is the use case for this because it can easily overflow for other traces. A suggestion would be using context variables to store the values and then creating a SpanProcessor that injects context variables into traces could be the way to go. Then when the trace is closed, you clean context vars automatically.
…TLP integration script - Extend verify_invoke_agent_baggage subprocess helper with --propagate-false - Fix empty-span attribute dict falsy check - Add Jaeger end-to-end script (subprocess workers per OTEL provider limitation)
Contributor
Author
|
Superseded by a new draft PR (ContextVar + InvokeAgentIdentitySpanProcessor branch fix/gh-55-invoke-agent-identity-span-processor). |
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When
invoke_agent_span(..., propagate=True)is used, span attributes are merged into nested spans only for code paths that callget_propagated_attributes()(SDK helpers). Third-party instrumentations (e.g. LangGraph / Traceloop) create spans that never receivegen_ai.agent.name/gen_ai.agent.id, which breaks consistent GenAI semantic attributes (reported in #55).This change mirrors
gen_ai.agent.{name,id,description}into W3C OpenTelemetry Baggage for the duration of theinvoke_agent_spancontext whenpropagate=True. The existingBaggageSpanProcessorregistered byauto_instrument()then applies those entries to every started span in scope—including spans from external instrumentations.Related Issue
Closes #55
Type of Change
How to Test
uv sync --group dev(or equivalent venv)pytest tests/core/unit/telemetry/test_tracer.py -qPYTHONPATH=src python tests/core/unit/telemetry/verify_invoke_agent_baggage.py(expectsOK:)Checklist
tests/core/unit/telemetry/test_tracer.py)propagateoninvoke_agent_spanBreaking Changes
None.
Additional Notes
SAP internal / process: Link related Jira ticket here when applicable (not available in this draft).
Requires
auto_instrument()soBaggageSpanProcessoris registered; matches Kyma/agent runtime guidance.