Skip to content

Commit

Permalink
Merge branch 'main' into instrumentationServiceFabricRemoting
Browse files Browse the repository at this point in the history
  • Loading branch information
sablancoleis authored Dec 9, 2024
2 parents f8b95b3 + 92b48e1 commit 7f42d60
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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

0 comments on commit 7f42d60

Please sign in to comment.