Skip to content

Commit

Permalink
Ensure baggage gets copied with baggage prefix
Browse files Browse the repository at this point in the history
This brings our agent in line with the wider agent specification on how
to implement baggage propagation

https://github.com/elastic/apm/blob/main/specs/agents/tracing-distributed-tracing.md#baggage-related-configuration
  • Loading branch information
Mpdreamz committed Nov 15, 2023
1 parent f7f52ae commit 9cb2439
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Elastic.Apm/Api/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Elastic.Apm.Api
{
public class Context
{
private Lazy<Dictionary<string, string>> _custom = new Lazy<Dictionary<string, string>>();
private Lazy<Dictionary<string, string>> _custom = new();

[JsonConverter(typeof(CustomJsonConverter))]
public Dictionary<string, string> Custom => _custom.Value;

internal Lazy<LabelsDictionary> InternalLabels = new Lazy<LabelsDictionary>();
internal Lazy<LabelsDictionary> InternalLabels = new();

/// <summary>
/// <seealso cref="ShouldSerializeLabels" />
Expand Down
4 changes: 3 additions & 1 deletion src/Elastic.Apm/Model/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ private void CheckAndCaptureBaggage()
if (!WildcardMatcher.IsAnyMatch(Configuration.BaggageToAttach, baggage.Key))
continue;

Context.InternalLabels.Value.Add(baggage.Key, baggage.Value);
var newKey = $"baggage.{baggage.Key}";
var labels = Context.InternalLabels.Value;
labels[newKey] = baggage.Value;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Elastic.Apm/Model/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ private void CheckAndCaptureBaggage()
if (!WildcardMatcher.IsAnyMatch(Configuration.BaggageToAttach, baggage.Key))
continue;

Otel ??= new OTel() { Attributes = new Dictionary<string, object>() };
Otel.Attributes.Add(baggage.Key, baggage.Value);
Otel ??= new OTel { Attributes = new Dictionary<string, object>() };

var newKey = $"baggage.{baggage.Key}";
Otel.Attributes[newKey] = baggage.Value;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Elastic.Apm/Model/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,10 @@ private void CheckAndCaptureBaggage()
if (!WildcardMatcher.IsAnyMatch(Configuration.BaggageToAttach, baggage.Key))
continue;

Otel ??= new OTel() { Attributes = new Dictionary<string, object>() };
Otel.Attributes.Add(baggage.Key, baggage.Value);
Otel ??= new OTel { Attributes = new Dictionary<string, object>() };

var newKey = $"baggage.{baggage.Key}";
Otel.Attributes[newKey] = baggage.Value;
}
}

Expand Down

0 comments on commit 9cb2439

Please sign in to comment.