-
Notifications
You must be signed in to change notification settings - Fork 319
Extract trace context from Kafka producer record headers #10020
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
Extract trace context from Kafka producer record headers #10020
Conversation
Allow Kafka producers to continue existing traces by extracting trace context from record headers and using it as parent for the produce span. This enables distributed tracing when messages are forwarded between services with pre-existing 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.
Hi @PlugaruT 👋 Thanks for taking time to contribute.
Quick feedback about the change but I think this might break the context tracking later as KafkaProducerCallback won't use the extracted context.
What I would do is:
- Declare an AgentSpanContext
- Try to extract from record headers
- If null, set it with active span
- Reuse this span context for both
startSpanandKafkaProducerCallback.
I will send the current PR into CI to get more feedback (see #10022) and ping @DataDog/apm-idm-java to get a proper review
Hi @PerfectSlayer, are you referring to how |
Yes
The active span content is implicitly used for |
|
Hey @DataDog/apm-idm-java, can I get a review on this one 🙏 |
|
I pinged the team in their internal Slack channel |
| final AgentSpan span; | ||
| final AgentSpan callbackParentSpan; | ||
|
|
||
| if (extractedContext != null) { |
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.
This could be a breaking change if users expect activeSpan to take precedence. Adding a configuration option, would allow users to configure this behavior.
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.
I actually was expecting the other way around, especially coming from opentelemetry, I expected to have the context reuse the one from headers and attach new spans to the same trace and not break it.
I can do it, no problem there, but I'd say this is a bug fix rather than a feature.
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.
It's done. Let me know if the name works
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.
Makes sense. I'll exclude the commit that adds the setting, then. Let's see if all the CI checks pass in the mirroring PR: #10022
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.
I'm looking into the failing test. Just found how to run the system tests locally.
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.
@ygree I force pushed without the commit that added the config and also fixed the test, at least it passes on my computer 😅
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.
Thanks! I'll pull your changes to the mirroring PR and run the CI checks.
ygree
left a comment
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 same change should be made in datadog.trace.instrumentation.kafka_clients38.ProducerAdvice to cover Kafka 3.8+ clients.
79f8032 to
973db4e
Compare
What Does This Do
Enables Kafka producers to extract and continue existing traces from record headers.
Motivation
Fix for #5092
Currently, when a Kafka producer sends a record that already has trace context in its headers (e.g., when forwarding messages between services), the tracer creates a new root span instead of continuing the existing trace. This breaks distributed tracing continuity
This change extracts the trace context from record headers using
extractContextAndGetSpanContext()and uses it as the parent for the produce span, maintaining trace continuity across service boundaries. It's also making dd-trace behave the same way opentelemetry does, see here.Additional Notes
Contributor Checklist
type:and (comp:orinst:) labels in addition to any useful labelsclose,fixor any linking keywords when referencing an issue.Use
solvesinstead, and assign the PR milestone to the issueJira ticket: [PROJ-IDENT]