Skip to content

Commit

Permalink
Baggage part 1/3: change propagator signatures (#6157)
Browse files Browse the repository at this point in the history
## Summary of changes

First of multiple PRs to add OpenTelemetry baggage support.

This PR prepares propagators to support baggage. Since all propagators are currently _trace_ propagators and work on `SpanContext`, this PR changes all propagator signatures to accept or return a new `PropationContext` instead. `PropationContext` is a light container for both the existing `SpanContext` and the new `Baggage` type.

Baggage progpation across services is _not_ implemented in this PR.

## Reason for change

Adding support for OpenTelemetry baggage.

## Implementation details

- Add `Baggage` class
- Add `PropagationContext`, which contains `SpanContext` and `Baggage`
- Modify all propagator signatures (and usages) to replace `SpanContext` with `PropagationContext`

## Test coverage

- added unit tests for `Baggage` (get, set, remove, etc)
- existing propagators are already covered by extensive tests

## Other details
<!-- Fixes #{issue} -->

<!--  ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
  • Loading branch information
lucaspimentel authored Nov 1, 2024
1 parent 2eb04ff commit b433407
Show file tree
Hide file tree
Showing 81 changed files with 2,089 additions and 1,057 deletions.
12 changes: 7 additions & 5 deletions tracer/src/Datadog.Trace.Coverage.collector/CoverageCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,15 @@ private void ProcessFolder(string folder, SearchOption searchOption)
_logger?.Warning($"Processed {numAssemblies} assemblies in folder: {folder}");

// The following is just a best effort approach to indicate in the test session that
// we sucessfully instrumented all assemblies to collect code coverage.
// we successfully instrumented all assemblies to collect code coverage.
// Is not part of the spec but useful for support tickets.
// We try to extract session variables (from out of process sessions)
// and try to send a message to the IPC server for setting the test.code_coverage.injected tag.
if (SpanContextPropagator.Instance.Extract(
EnvironmentHelpers.GetEnvironmentVariables(),
new DictionaryGetterAndSetter(DictionaryGetterAndSetter.EnvironmentVariableKeyProcessor)) is { } sessionContext)
var extractedContext = SpanContextPropagator.Instance.Extract(
EnvironmentHelpers.GetEnvironmentVariables(),
new DictionaryGetterAndSetter(DictionaryGetterAndSetter.EnvironmentVariableKeyProcessor));

if (extractedContext.SpanContext is { } sessionContext)
{
try
{
Expand All @@ -323,7 +325,7 @@ private void ProcessFolder(string folder, SearchOption searchOption)
}
else
{
_logger?.Debug($"CoverageCollector.Test session context cannot be found, skipping IPC client and sending injection tags");
_logger?.Debug("CoverageCollector.Test session context cannot be found, skipping IPC client and sending injection tags");
}
}

Expand Down
10 changes: 6 additions & 4 deletions tracer/src/Datadog.Trace/AspNet/TracingHttpModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ private void OnBeginRequest(object sender, EventArgs eventArgs)
HttpRequest httpRequest = httpContext.Request;
var requestHeaders = RequestDataHelper.GetHeaders(httpRequest) ?? new System.Collections.Specialized.NameValueCollection();
NameValueHeadersCollection? headers = null;
SpanContext propagatedContext = null;
PropagationContext extractedContext = default;

if (tracer.InternalActiveScope == null)
{
try
{
// extract propagated http headers
headers = requestHeaders.Wrap();
propagatedContext = SpanContextPropagator.Instance.Extract(headers.Value);
extractedContext = SpanContextPropagator.Instance.Extract(headers.Value).MergeBaggageInto(Baggage.Current);
}
catch (Exception ex)
{
Expand All @@ -152,7 +153,7 @@ private void OnBeginRequest(object sender, EventArgs eventArgs)
string httpMethod = httpRequest.HttpMethod.ToUpperInvariant();
string url = httpContext.Request.GetUrlForSpan(tracer.TracerManager.QueryStringManager);
var tags = new WebTags();
scope = tracer.StartActiveInternal(_requestOperationName, propagatedContext, tags: tags);
scope = tracer.StartActiveInternal(_requestOperationName, extractedContext.SpanContext, tags: tags);
// Leave resourceName blank for now - we'll update it in OnEndRequest
scope.Span.DecorateWebServerSpan(resourceName: null, httpMethod, host, url, userAgent, tags);
if (headers is not null)
Expand All @@ -172,7 +173,8 @@ private void OnBeginRequest(object sender, EventArgs eventArgs)
// (e.g. WCF being hosted in IIS)
if (HttpRuntime.UsingIntegratedPipeline)
{
SpanContextPropagator.Instance.Inject(scope.Span.Context, requestHeaders.Wrap());
var injectedContext = new PropagationContext(scope.Span.Context, Baggage.Current);
SpanContextPropagator.Instance.Inject(injectedContext, requestHeaders.Wrap());
}

httpContext.Items[_httpContextScopeKey] = scope;
Expand Down
Loading

0 comments on commit b433407

Please sign in to comment.