Skip to content

sync span based baggage APIs with global baggage APIs#7453

Open
ida613 wants to merge 10 commits intomasterfrom
ida613/otel-baggage-api-fix
Open

sync span based baggage APIs with global baggage APIs#7453
ida613 wants to merge 10 commits intomasterfrom
ida613/otel-baggage-api-fix

Conversation

@ida613
Copy link
Collaborator

@ida613 ida613 commented Feb 6, 2026

What does this PR do?

Make sure baggage added using the global APIs are in sync with the span APIs. Since span APIs are legacy, they take precedence. It is still possible to add baggage using the global APIs and to not have them also added to the legacy baggages.

Motivation

Additional Notes

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

Overall package size

Self size: 4.64 MB
Deduped: 5.48 MB
No deduping: 5.48 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 2.0.6 | 81.92 kB | 813.08 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@codecov
Copy link

codecov bot commented Feb 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.21%. Comparing base (76e93d7) to head (2fa1be7).
⚠️ Report is 73 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7453      +/-   ##
==========================================
- Coverage   80.40%   80.21%   -0.20%     
==========================================
  Files         732      731       -1     
  Lines       31050    31364     +314     
==========================================
+ Hits        24966    25158     +192     
- Misses       6084     6206     +122     
Flag Coverage Δ
aiguard-macos 39.00% <25.00%> (-0.17%) ⬇️
aiguard-ubuntu 39.12% <25.00%> (-0.09%) ⬇️
aiguard-windows 38.86% <25.00%> (-0.20%) ⬇️
apm-capabilities-tracing-macos 48.38% <100.00%> (-0.51%) ⬇️
apm-capabilities-tracing-ubuntu 48.41% <100.00%> (-0.48%) ⬇️
apm-capabilities-tracing-windows 48.11% <100.00%> (-0.51%) ⬇️
apm-integrations-child-process 38.57% <25.00%> (-0.10%) ⬇️
apm-integrations-couchbase-18 37.47% <25.00%> (+0.05%) ⬆️
apm-integrations-couchbase-eol 37.96% <25.00%> (+0.05%) ⬆️
apm-integrations-oracledb 37.80% <25.00%> (-0.21%) ⬇️
appsec-express 55.66% <25.00%> (+0.21%) ⬆️
appsec-fastify 51.97% <25.00%> (-0.10%) ⬇️
appsec-graphql 52.13% <25.00%> (-0.25%) ⬇️
appsec-kafka 44.57% <25.00%> (-0.15%) ⬇️
appsec-ldapjs 44.17% <25.00%> (-0.22%) ⬇️
appsec-lodash 43.86% <25.00%> (-0.21%) ⬇️
appsec-macos 58.74% <25.00%> (+0.19%) ⬆️
appsec-mongodb-core 48.96% <25.00%> (-0.30%) ⬇️
appsec-mongoose 49.76% <25.00%> (-0.19%) ⬇️
appsec-mysql 51.14% <25.00%> (-0.20%) ⬇️
appsec-node-serialize 43.37% <25.00%> (-0.21%) ⬇️
appsec-passport 47.89% <25.00%> (-0.30%) ⬇️
appsec-postgres 50.89% <25.00%> (-0.34%) ⬇️
appsec-sourcing 42.72% <25.00%> (-0.21%) ⬇️
appsec-template 43.54% <25.00%> (-0.21%) ⬇️
appsec-ubuntu 58.82% <25.00%> (+0.24%) ⬆️
appsec-windows 58.59% <25.00%> (+0.15%) ⬆️
instrumentations-instrumentation-bluebird 32.28% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-body-parser 40.59% <25.00%> (-0.23%) ⬇️
instrumentations-instrumentation-child_process 37.88% <25.00%> (-0.10%) ⬇️
instrumentations-instrumentation-cookie-parser 34.32% <25.00%> (-0.27%) ⬇️
instrumentations-instrumentation-express 34.66% <25.00%> (-0.28%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.46% <25.00%> (-0.28%) ⬇️
instrumentations-instrumentation-express-session 40.21% <25.00%> (-0.30%) ⬇️
instrumentations-instrumentation-fs 31.88% <25.00%> (-0.06%) ⬇️
instrumentations-instrumentation-generic-pool 29.76% <ø> (-0.04%) ⬇️
instrumentations-instrumentation-http 39.93% <25.00%> (+0.21%) ⬆️
instrumentations-instrumentation-knex 32.28% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-mongoose 33.46% <25.00%> (-0.25%) ⬇️
instrumentations-instrumentation-multer 40.33% <25.00%> (-0.23%) ⬇️
instrumentations-instrumentation-mysql2 38.37% <25.00%> (+0.01%) ⬆️
instrumentations-instrumentation-passport 44.18% <25.00%> (+3.32%) ⬆️
instrumentations-instrumentation-passport-http 43.84% <25.00%> (+3.01%) ⬆️
instrumentations-instrumentation-passport-local 44.39% <25.00%> (+3.56%) ⬆️
instrumentations-instrumentation-pg 37.79% <25.00%> (-0.09%) ⬇️
instrumentations-instrumentation-promise 32.20% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-promise-js 32.21% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-q 32.26% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-url 32.17% <25.00%> (-0.07%) ⬇️
instrumentations-instrumentation-when 32.22% <25.00%> (-0.07%) ⬇️
llmobs-ai 41.41% <25.00%> (-0.23%) ⬇️
llmobs-anthropic 40.39% <25.00%> (-0.32%) ⬇️
llmobs-bedrock 39.31% <25.00%> (-0.28%) ⬇️
llmobs-google-genai 39.91% <25.00%> (-0.29%) ⬇️
llmobs-langchain 39.48% <25.00%> (-0.25%) ⬇️
llmobs-openai 44.23% <25.00%> (-0.32%) ⬇️
llmobs-vertex-ai 40.18% <25.00%> (-0.24%) ⬇️
platform-core 29.71% <ø> (+1.62%) ⬆️
platform-esbuild 32.89% <ø> (+1.40%) ⬆️
platform-instrumentations-misc 40.53% <ø> (+0.91%) ⬆️
platform-shimmer 36.14% <ø> (+1.31%) ⬆️
platform-unit-guardrails 31.27% <ø> (+0.89%) ⬆️
plugins-azure-event-hubs 24.02% <ø> (+1.38%) ⬆️
plugins-azure-service-bus 23.42% <ø> (+1.34%) ⬆️
plugins-bullmq 43.70% <25.00%> (-0.10%) ⬇️
plugins-cassandra 37.84% <25.00%> (-0.29%) ⬇️
plugins-cookie 25.08% <ø> (+1.38%) ⬆️
plugins-cookie-parser 24.87% <ø> (+1.37%) ⬆️
plugins-crypto 24.72% <ø> (+1.83%) ⬆️
plugins-dd-trace-api 38.43% <25.00%> (-0.06%) ⬇️
plugins-express-mongo-sanitize 25.04% <ø> (+1.36%) ⬆️
plugins-express-session 24.83% <ø> (+1.37%) ⬆️
plugins-fastify 42.35% <25.00%> (-0.27%) ⬇️
plugins-fetch 38.38% <25.00%> (-0.31%) ⬇️
plugins-fs 38.68% <25.00%> (-0.09%) ⬇️
plugins-generic-pool 24.06% <ø> (+1.38%) ⬆️
plugins-google-cloud-pubsub 45.51% <25.00%> (-0.31%) ⬇️
plugins-grpc 41.04% <25.00%> (-0.34%) ⬇️
plugins-handlebars 25.08% <ø> (+1.36%) ⬆️
plugins-hapi 40.22% <25.00%> (-0.30%) ⬇️
plugins-hono 40.48% <25.00%> (-0.24%) ⬇️
plugins-ioredis 38.48% <25.00%> (-0.09%) ⬇️
plugins-knex 24.80% <ø> (+1.29%) ⬆️
plugins-ldapjs 22.61% <ø> (+1.33%) ⬆️
plugins-light-my-request 24.48% <ø> (?)
plugins-limitd-client 32.57% <25.00%> (-0.07%) ⬇️
plugins-lodash 24.13% <ø> (+1.39%) ⬆️
plugins-mariadb ?
plugins-memcached 38.21% <25.00%> (-0.09%) ⬇️
plugins-microgateway-core 39.24% <25.00%> (-0.30%) ⬇️
plugins-moleculer 40.60% <25.00%> (-0.33%) ⬇️
plugins-mongodb 39.25% <25.00%> (-0.39%) ⬇️
plugins-mongodb-core 39.09% <25.00%> (-0.13%) ⬇️
plugins-mongoose 38.93% <25.00%> (-0.34%) ⬇️
plugins-multer 24.83% <ø> (+1.37%) ⬆️
plugins-mysql 39.23% <25.00%> (-0.13%) ⬇️
plugins-mysql2 39.33% <25.00%> (-0.07%) ⬇️
plugins-node-serialize 25.12% <ø> (+1.38%) ⬆️
plugins-opensearch 37.67% <25.00%> (-0.44%) ⬇️
plugins-passport-http 24.91% <ø> (?)
plugins-postgres 35.72% <25.00%> (-0.07%) ⬇️
plugins-process 24.72% <ø> (+1.83%) ⬆️
plugins-pug 25.08% <ø> (+1.38%) ⬆️
plugins-redis 38.96% <25.00%> (+0.07%) ⬆️
plugins-router 43.12% <25.00%> (-0.36%) ⬇️
plugins-sequelize 23.66% <ø> (+1.38%) ⬆️
plugins-test-and-upstream-amqp10 38.55% <25.00%> (-0.09%) ⬇️
plugins-test-and-upstream-amqplib 43.74% <25.00%> (-0.26%) ⬇️
plugins-test-and-upstream-apollo 39.09% <25.00%> (-0.32%) ⬇️
plugins-test-and-upstream-avsc 38.82% <25.00%> (-0.09%) ⬇️
plugins-test-and-upstream-bunyan 33.88% <25.00%> (-0.23%) ⬇️
plugins-test-and-upstream-connect 40.89% <25.00%> (-0.25%) ⬇️
plugins-test-and-upstream-graphql 40.23% <25.00%> (-0.09%) ⬇️
plugins-test-and-upstream-koa 40.47% <25.00%> (-0.32%) ⬇️
plugins-test-and-upstream-protobufjs 39.05% <25.00%> (-0.09%) ⬇️
plugins-test-and-upstream-rhea 44.14% <25.00%> (-0.11%) ⬇️
plugins-undici 39.18% <25.00%> (-0.31%) ⬇️
plugins-url 24.72% <ø> (+1.83%) ⬆️
plugins-valkey 38.13% <25.00%> (-0.09%) ⬇️
plugins-vm 24.72% <ø> (+1.83%) ⬆️
plugins-winston 34.08% <25.00%> (-0.28%) ⬇️
plugins-ws 42.00% <25.00%> (+1.15%) ⬆️
profiling-macos 39.98% <25.00%> (-0.18%) ⬇️
profiling-ubuntu 40.10% <25.00%> (-0.10%) ⬇️
profiling-windows 41.70% <25.00%> (+0.14%) ⬆️
serverless-azure-functions-client 23.75% <ø> (+0.86%) ⬆️
serverless-azure-functions-eventhubs 23.75% <ø> (+0.86%) ⬆️
serverless-azure-functions-servicebus 23.75% <ø> (+0.86%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pr-commenter
Copy link

pr-commenter bot commented Feb 6, 2026

Benchmarks

Benchmark execution time: 2026-02-17 15:34:46

Comparing candidate commit 2fa1be7 in PR branch ida613/otel-baggage-api-fix with baseline commit 76e93d7 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 230 metrics, 30 unstable metrics.

@datadog-official
Copy link

datadog-official bot commented Feb 6, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2fa1be7 | Docs | Datadog PR Page | Was this helpful? Give us feedback!

@ida613 ida613 changed the title sync span based baggage apis with global baggage apis sync span based baggage APIs with global baggage APIs Feb 6, 2026
@ida613 ida613 marked this pull request as ready for review February 6, 2026 19:08
@ida613 ida613 requested a review from a team as a code owner February 6, 2026 19:08
@ida613 ida613 requested review from a team and BridgeAR and removed request for a team February 6, 2026 19:08
removeAllBaggageItems()
for (const baggage of baggageItems) {
setBaggageItem(baggage[0], baggage[1].value)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate about this? I am unsure what this does.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is calling the global baggage APIs to make sure the currently active baggage is in sync with baggage on the context. It essentially does the same thing as these operations but for global baggage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Otel baggage overrule DD baggage? Should it be merged? Do we have any documentation or RFCs around that or was there any discussion around that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The code lets whichever comes later override, but I'm not sure if this is what's intended. @rachelyangdog @zacharycmontoya do you think we need to discuss this before proceeding?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so turns out dd baggage should always override otel baggage, updated the code

Copy link
Collaborator Author

@ida613 ida613 Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I support this approach. Legacy baggage won't be included in this support. We will only keep the Current OTel API Baggage in sync with the global Datadog baggage that's tracked independently of spans.

Thank you Zach! I can do this no problem, but currently in node, dd and otel baggages only get synced when we call active or with. In order to do what dotnet does (syncing on each individual operation), we would have to replace the otel baggage operations within dd-trace, which will involve a ton of work. Is it acceptable to only sync when calling with or active?

Did we already establish a precedence for this scenario or are we only now adding that? My preference would be to prefer global baggage, aka from the baggage HTTP header. How feasible would that be and do you see any issues with that preference?

Sounds good with me, it's not hard to do. I'll work on it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it should be fine to only update when calling active or with. Let me describe the interactions I expect, but if my understanding is wrong please point it out so we're operating off the same understanding.

DD API

Each time the user wants to read/write to the active global baggage, they call one of the Baggage API's here. Notably, when clearing or setting baggage, these API's will automatically update the current baggage before returning to the caller. As long as we instrument each time active or with is called in the OpenTelemetry context manager, we don't need to update any code here.

OTel API

Each time the user wants to read/write to the active global baggage, they will need to get the current baggage by calling getActiveBagage which gets the current context by calling active. Then, once the user has updated the baggage and wants to set it as current, the user must attach the baggage to a Context using the OTel API setBaggage and call the api.context.with API with the Context and a callback, so the baggage will become active.

So with this flow, we just need our active operation to copy over the Datadog global baggage to overwrite all the OpenTelemetry baggage in the context and our with operation to copy over the OpenTelemetry baggage to override all the Datadog baggage.

And actually, this is exactly what my .NET implementation does! 😄 It just turns out that the .NET OTel Baggage API's directly set the "current" OTel baggage API within each API call (Set/Remove) so we just have to do the active/with implementation in more places

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Zach! that's my understanding too. I've updated the code accordingly

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zacharycmontoya turns out legacy baggage and the new baggage are extracted/injected and propagated independently, so there will be never be a key conflict. But the legacy baggage never interact with the otel drop in code, is this okay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's okay 👍🏼

const SpanContext = require('./span_context')

function mergeGlobalBaggageWith (baggages) {
const combinedBaggages = structuredClone(baggages) || {}
Copy link
Member

@BridgeAR BridgeAR Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const combinedBaggages = structuredClone(baggages) || {}
const combinedBaggages = baggages ? rfdc(baggages) : {}

Please use rfdc. structuredClone is relatively slow. This must be required at the top and I believe we used a vendored rfdc. Check other usages across the repo.

In addition: we do not need to clone anything if we called JSON.parse() before. We could just do the cloning before we pass it to the method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thank you Ruben! this is really insightful :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move the cloning to only happen where it is needed?

Copy link
Collaborator Author

@ida613 ida613 Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this good practice when we could also pass an object into it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand. I meant we should just copy that object before passing it through. That way we do not have to copy in all cases but only in one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this helper function in the latest commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants