-
Notifications
You must be signed in to change notification settings - Fork 155
[Tracing] Add interoperability between the Datadog Baggage API and the OpenTelemetry Baggage API #7921
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
Merged
zacharycmontoya
merged 18 commits into
master
from
zach.montoya/otel-baggage-sync-APMAPI-1736
Mar 19, 2026
Merged
[Tracing] Add interoperability between the Datadog Baggage API and the OpenTelemetry Baggage API #7921
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ac471d9
Implement automatic instrumentation for OpenTelemetry Baggage, so tha…
zacharycmontoya d496e9f
Fix OpenTelemetry baggage interop. When the user calls the static Ope…
zacharycmontoya b52ef06
Add support for remaining static API methods:
zacharycmontoya 3cda30c
Add snapshot tests for new AspNetCoreMinimalApisTests.OtelBaggageApiI…
zacharycmontoya 6dc4e28
Optimization 1: In static OpenTelemetry.Baggage API calls, only updat…
zacharycmontoya 61edb29
Update SetCurrent comments
zacharycmontoya 7d6fce8
Merge branch 'master' into zach.montoya/otel-baggage-sync-APMAPI-1736
zacharycmontoya 7993bf7
PR Feedback: Remove unnecessary code comments on OnMethodBegin / OnMe…
zacharycmontoya acfc98d
PR Feedback: Inline the IntegrationName into the InstrumentMethod att…
zacharycmontoya ae367aa
PR Feedback: Add 'exception is null' conditional to all of the OnMeth…
zacharycmontoya e9ef733
PR Feedback: Add links to the baggage implementation types
zacharycmontoya 7a603e1
PR Feedback: Refactor OnMethodBegin methods to all use the 'TBaggage …
zacharycmontoya 5d067d8
PR Feedback: Simplify duck typing for OpenTelemetry.Baggage.get_Curre…
zacharycmontoya 4cf6920
Update OTel Baggage API integration to test to omit the 'otel-baggage…
zacharycmontoya 6513124
Make the OTelBaggage_SetCurrentIntegration, more resilient by only up…
zacharycmontoya af30626
Add Datadog.Trace.Baggage.AsDictionary() method for getting the exist…
zacharycmontoya 64ea7e4
Fix issue in TypeNameTests
zacharycmontoya f8b5443
Merge branch 'master' into zach.montoya/otel-baggage-sync-APMAPI-1736
zacharycmontoya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
40 changes: 40 additions & 0 deletions
40
tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/OpenTelemetry/IApiBaggage.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // <copyright file="IApiBaggage.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.Collections.Generic; | ||
| using Datadog.Trace.DuckTyping; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.OpenTelemetry | ||
| { | ||
| /// <summary> | ||
| /// OpenTelemetry.Baggage interface for duck-typing | ||
| /// https://github.com/open-telemetry/opentelemetry-dotnet/blob/db429bf642c1a2c2f71b49f88d63e0a661018298/src/OpenTelemetry.Api/Baggage.cs#L16 | ||
| /// </summary> | ||
| internal interface IApiBaggage | ||
| { | ||
| [DuckField(Name = "baggage")] | ||
| Dictionary<string, string> Baggage { get; } | ||
|
|
||
| IApiBaggage Create(Dictionary<string, string?>? baggageItems); | ||
|
|
||
| IReadOnlyDictionary<string, string?> GetBaggage(); | ||
|
|
||
| string? GetBaggage(string name); | ||
|
|
||
| IBaggageHolder EnsureBaggageHolder(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Baggage holder interface for duck-typing | ||
| /// https://github.com/open-telemetry/opentelemetry-dotnet/blob/db429bf642c1a2c2f71b49f88d63e0a661018298/src/OpenTelemetry.Api/Baggage.cs#L371 | ||
| /// </summary> | ||
| internal interface IBaggageHolder | ||
| { | ||
| [DuckField(Name = "Baggage")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OMG the casing inconsistency in this code base 😅 |
||
| IApiBaggage Baggage { get; set; } | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
...race/ClrProfiler/AutoInstrumentation/OpenTelemetry/OTelBaggage_ClearBaggageIntegration.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // <copyright file="OTelBaggage_ClearBaggageIntegration.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using Datadog.Trace; | ||
| using Datadog.Trace.ClrProfiler.CallTarget; | ||
| using Datadog.Trace.Configuration; | ||
| using Datadog.Trace.DuckTyping; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.OpenTelemetry | ||
| { | ||
| /// <summary> | ||
| /// OpenTelemetry.Api Baggage.ClearBaggage calltarget instrumentation | ||
| /// </summary> | ||
| [InstrumentMethod( | ||
| AssemblyName = "OpenTelemetry.Api", | ||
| TypeName = "OpenTelemetry.Baggage", | ||
| MethodName = "ClearBaggage", | ||
| ReturnTypeName = "OpenTelemetry.Baggage", | ||
| ParameterTypeNames = new[] { "OpenTelemetry.Baggage" }, | ||
| MinimumVersion = "1.0.0", | ||
| MaximumVersion = "1.0.0", | ||
andrewlock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| IntegrationName = nameof(Configuration.IntegrationId.OpenTelemetry))] | ||
| [Browsable(false)] | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public sealed class OTelBaggage_ClearBaggageIntegration | ||
| { | ||
| internal const IntegrationId IntegrationId = Configuration.IntegrationId.OpenTelemetry; | ||
|
|
||
| internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, in CallTargetState state) | ||
| { | ||
| if (Tracer.Instance.CurrentTraceSettings.Settings.IsIntegrationEnabled(IntegrationId) | ||
| && exception is null) | ||
| { | ||
| // Important: Before returning to the caller, the static OpenTelemetry.Baggage APIs set the to-be-returned baggage object as OpenTelemetry.Baggage.Current. | ||
| // However, this is not done through the public setter (which we instrument), but through the backing field, so we must manually update Datadog.Trace.Baggage.Current | ||
| // so it remains in-sync. | ||
| Baggage.Current.Clear(); | ||
andrewlock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return new CallTargetReturn<TReturn>(returnValue); | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
....Trace/ClrProfiler/AutoInstrumentation/OpenTelemetry/OTelBaggage_GetCurrentIntegration.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // <copyright file="OTelBaggage_GetCurrentIntegration.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using Datadog.Trace; | ||
| using Datadog.Trace.ClrProfiler.CallTarget; | ||
| using Datadog.Trace.Configuration; | ||
| using Datadog.Trace.DuckTyping; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.OpenTelemetry | ||
| { | ||
| /// <summary> | ||
| /// OpenTelemetry.Api Baggage.get_Current calltarget instrumentation | ||
| /// </summary> | ||
| [InstrumentMethod( | ||
| AssemblyName = "OpenTelemetry.Api", | ||
| TypeName = "OpenTelemetry.Baggage", | ||
| MethodName = "get_Current", | ||
| ReturnTypeName = "OpenTelemetry.Baggage", | ||
| ParameterTypeNames = new string[0], | ||
| MinimumVersion = "1.0.0", | ||
| MaximumVersion = "1.0.0", | ||
| IntegrationName = nameof(Configuration.IntegrationId.OpenTelemetry))] | ||
| [Browsable(false)] | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public sealed class OTelBaggage_GetCurrentIntegration | ||
| { | ||
| internal const IntegrationId IntegrationId = Configuration.IntegrationId.OpenTelemetry; | ||
|
|
||
| internal static CallTargetState OnMethodBegin<TInstance>(TInstance apiBaggage) | ||
| where TInstance : IApiBaggage | ||
| { | ||
| if (Tracer.Instance.CurrentTraceSettings.Settings.IsIntegrationEnabled(IntegrationId)) | ||
| { | ||
| // Since Datadog.Trace.Baggage.Current may have been updated since the last time OpenTelemetry.Baggage.Current was accessed, | ||
| // we must update the underlying OpenTelemetry.Baggage.Current store with the latest Datadog.Trace.Baggage.Current items. | ||
| // Note: When the user sets OpenTelemetry.Baggage.Current, those changes will override the contents of Datadog.Trace.Baggage.Current, | ||
| // so we can always consider Datadog.Trace.Baggage.Current as being up-to-date. | ||
| var baggageHolder = apiBaggage.EnsureBaggageHolder(); | ||
| baggageHolder.Baggage = apiBaggage.Create(Baggage.Current.AsDictionary()); | ||
| } | ||
|
|
||
| return CallTargetState.GetDefault(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.