-
Notifications
You must be signed in to change notification settings - Fork 223
internal: showcase context propagation using baggage #1349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, no comments
| for key, value in dict_to_propagate.items(): | ||
| current_ctx = baggage.set_baggage(key, str(value), context=current_ctx) | ||
| return current_ctx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The context is immutable, i.e. every set_baggage operation produces a new, updated context with the relevant properties. We update whatever context we get and return the new one.
|
|
||
| return self | ||
|
|
||
| @contextmanager |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're turning this into a context manager as we need to detach the context at the end of the execution to avoid memory leaks and issues in async/multi-thread processing.
Every call like
span.update_trace(name="foo")
[...]
# ongoing executionmust become
with span.update_trace(name="foo"):
[...]
# ongoing executionto ensure that all child spans will be started with the attached context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this is a breaking change!
| else None, | ||
| ) | ||
|
|
||
| def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're injecting the baggage span processor in the on_start here to make use of it and call its own implementation: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/processor/opentelemetry-processor-baggage/src/opentelemetry/processor/baggage/processor.py.
The other overwrites are taken as-is from the SpanProcessor, i.e. there is no need to overwrite the on_end, etc.
Important
Introduces context propagation using OpenTelemetry baggage in Langfuse, updating span processing and tests accordingly.
propagate_attributes()inattributes.pyto propagate context attributes using OpenTelemetry baggage.LangfuseSpanProcessorinspan_processor.pyto useBaggageSpanProcessorfor attaching baggage to spans.update_trace()inspan.pyto usepropagate_attributes()for context management.opentelemetry-processor-baggagetopyproject.toml.test_core_sdk.pyto test context propagation and baggage handling in spans and traces.This description was created by
for 02d3dea. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
Updated On: 2025-09-17 13:59:45 UTC
This PR implements OpenTelemetry baggage-based context propagation to showcase how trace-level attributes can be automatically propagated to child spans. The implementation consists of several interconnected changes:
Core Architecture Changes:
update_tracemethod inspan.pyis converted from a regular method to a context manager using@contextmanager. This method now creates an OpenTelemetry context with trace attributes stored in baggage, attaches the context during execution, and properly detaches it when the context manager exits.propagate_attributesfunction is added toattributes.pythat converts dictionary attributes into OpenTelemetry baggage entries, enabling cross-boundary context propagation.LangfuseSpanProcessorinspan_processor.pyis enhanced with aBaggageSpanProcessorintegration that automatically attaches baggage keys to spans during their initialization via theon_startlifecycle method.Integration with OpenTelemetry Stack:
The changes build upon the existing OpenTelemetry infrastructure already present in the codebase (api, sdk, exporter) by adding the
opentelemetry-processor-baggagedependency. This creates a complete context propagation pipeline where trace-level attributes set viaupdate_trace()are automatically available to child spans without manual propagation.Usage Pattern Changes:
The
update_tracemethod now requires usage as a context manager (with span.update_trace(...):) rather than a direct method call. Child spans created within this context automatically inherit the trace-level attributes through the baggage mechanism, eliminating the need for manual attribute management across span hierarchies.The test modifications in
test_core_sdk.pydemonstrate this new pattern, showing how trace metadata is automatically propagated to child spans when operations are performed within theupdate_tracecontext manager scope.Confidence score: 3/5
update_tracemethod signature