Skip to content
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

Added changes to AWSXRayPropagator #2359

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NETFRAMEWORK
using System.Net;
#endif
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
Expand Down Expand Up @@ -99,6 +102,34 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
return;
}

#if !NETFRAMEWORK
if (carrier.GetType() == typeof(HttpRequestMessage))
{
var httpRequestMessage = (HttpRequestMessage)(object)carrier;

// If X-Amzn-Trace-Id already exists in the headers and the carrier is of HttpRequestMessage,
// This means that the request is coming from the AWS SDK Instrumentation library and in this
// case, we don't want to overwrite the propagation context from the AWS SDK Span with the
// context from the outgoing HttpRequest
if (httpRequestMessage.Headers.Contains(AWSXRayTraceHeaderKey))
{
return;
}
}
#endif

#if NETFRAMEWORK
if (carrier.GetType() == typeof(HttpWebRequest))
{
var httpWebRequest = (HttpWebRequest)(object)carrier;

if (httpWebRequest.Headers.Get(AWSXRayTraceHeaderKey) != null)
{
return;
}
}
#endif

var sb = new StringBuilder();
sb.Append(RootKey);
sb.Append(KeyValueDelimiter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NETFRAMEWORK
using System.Net;
#endif
using System.Diagnostics;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Extensions.AWS.Trace;
Expand All @@ -26,8 +29,20 @@ public class AWSXRayPropagatorTests
carrier[name] = value;
};

#if NETFRAMEWORK
private static readonly Action<HttpWebRequest, string, string> HeaderValueSetter = (request, name, value) => request.Headers.Add(name, value);
#endif

private readonly AWSXRayPropagator awsXRayPropagator = new();

#if !NETFRAMEWORK
private static Action<HttpRequestMessage, string, string> HeaderValueSetter => (request, name, value) =>
{
request.Headers.Remove(name);
request.Headers.Add(name, value);
};
#endif

[Fact]
public void TestInjectTraceHeader()
{
Expand Down Expand Up @@ -56,6 +71,49 @@ public void TestInjectTraceHeaderNotSampled()
Assert.Equal("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0", carrier[AWSXRayTraceHeaderKey]);
}

[Fact]
public void TestInjectTraceHeaderAlreadyExists()
{
var traceIdHeader = "Root=1-00000-00000000000000000;Parent=123456789;Sampled=0";

#if !NETFRAMEWORK
var carrier = new HttpRequestMessage();
#else
var carrier = (HttpWebRequest)WebRequest.Create(new Uri("http://www.google.com/"));
#endif
carrier.Headers.Add(AWSXRayTraceHeaderKey, traceIdHeader);
var traceId = ActivityTraceId.CreateFromString(TraceId.AsSpan());
var parentId = ActivitySpanId.CreateFromString(ParentId.AsSpan());
var traceFlags = ActivityTraceFlags.None;
var activityContext = new ActivityContext(traceId, parentId, traceFlags);
this.awsXRayPropagator.Inject(new PropagationContext(activityContext, default), carrier, HeaderValueSetter);

#if !NETFRAMEWORK
Assert.True(carrier.Headers.Contains(AWSXRayTraceHeaderKey));
Assert.Equal(traceIdHeader, carrier.Headers.GetValues(AWSXRayTraceHeaderKey).FirstOrDefault());
#else
Assert.Equal(traceIdHeader, carrier.Headers.Get(AWSXRayTraceHeaderKey));
#endif
}

[Fact]
public void TestInjectTraceHeaderAlreadyExistsButNotHttpRequestMessage()
{
var traceIdHeader = "Root=1-00000-00000000000000000;Parent=123456789;Sampled=0";
var carrier = new Dictionary<string, string>()
{
{ AWSXRayTraceHeaderKey, traceIdHeader },
};
var traceId = ActivityTraceId.CreateFromString(TraceId.AsSpan());
var parentId = ActivitySpanId.CreateFromString(ParentId.AsSpan());
var traceFlags = ActivityTraceFlags.None;
var activityContext = new ActivityContext(traceId, parentId, traceFlags);
this.awsXRayPropagator.Inject(new PropagationContext(activityContext, default), carrier, Setter);

Assert.True(carrier.ContainsKey(AWSXRayTraceHeaderKey));
Assert.Equal("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0", carrier[AWSXRayTraceHeaderKey]);
}

[Fact]
public void TestExtractTraceHeader()
{
Expand Down
Loading