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

[Extensions.AWS] Add net6.0 target framework, optimize AWSXRayIdGenerator #1096

Merged
merged 26 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af0621e
[Extensions.AWSXRay] Add net6.0 TFM
wjrogers Mar 18, 2023
6837b42
[Extensions.AWSXray] Fix nullable warnings
wjrogers Mar 18, 2023
4a21120
[Extensions.AWSXray] Fix CA1846 (prefer 'AsSpan' over 'Substring')
wjrogers Mar 18, 2023
5f8e191
[Extensions.AWSXray] Remove reference to System.Net.Http
wjrogers Mar 18, 2023
0018518
[Extensions.AWSXRay] Use Activity.TraceIdGenerator on net6.0+
wjrogers Mar 21, 2023
20a7b99
[Extensions.AWSXRay] Update CHANGELOG.md
wjrogers Mar 21, 2023
3e81739
[Extensions.AWSXRay] Restore methods that take a Sampler parameter
wjrogers Mar 21, 2023
dee73ce
Merge branch 'main' into extensions-awsxray-target-net6.0
cijothomas Mar 21, 2023
8560e90
Merge branch 'main' into extensions-awsxray-target-net6.0
cijothomas Mar 28, 2023
124dcee
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers Apr 7, 2023
cd86af8
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers Apr 20, 2023
21f62ae
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers May 11, 2023
f3323d0
Revert changes to files moved to the new resource detectors project
wjrogers May 11, 2023
867a30b
[Extensions.AWSXRay] remove moved PublicAPI declarations
wjrogers May 11, 2023
6c5d621
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers May 23, 2023
38c89fe
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers Jun 6, 2023
2a6a238
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers Jun 14, 2023
75484d1
[Extensions.AWSXRay] Use cryptographic RNG to satisfy CA5394
wjrogers Jun 14, 2023
c5cce85
Merge branch 'main' into extensions-awsxray-target-net6.0
Kielek Jun 15, 2023
575da3f
Merge branch 'main' into extensions-awsxray-target-net6.0
wjrogers Jun 29, 2023
a7bc334
Merge branch 'main' into extensions-awsxray-target-net6.0
Kielek Jul 19, 2023
945d7d8
Merge branch 'main' into extensions-awsxray-target-net6.0
Kielek Jul 20, 2023
d141338
[Extensions.AWSXRay] Remove test target netcoreapp3.1
wjrogers Jul 20, 2023
1ec49e1
Merge branch 'main' into extensions-awsxray-target-net6.0
Kielek Jul 28, 2023
a203df7
Merge branch 'main' into extensions-awsxray-target-net6.0
Kielek Jul 28, 2023
55373f0
fix public api
Kielek Jul 28, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator
OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator
OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.AWSXRayPropagator() -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>!>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
static OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator.ReplaceTraceId() -> void
static OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator.ReplaceTraceId(OpenTelemetry.Trace.Sampler! sampler) -> void
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder! builder, OpenTelemetry.Trace.Sampler! sampler) -> OpenTelemetry.Trace.TracerProviderBuilder!
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Extensions.AWS/AWSXRayIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

#if !NET6_0_OR_GREATER
using System;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -50,7 +51,6 @@ internal static void ReplaceTraceId(Sampler? sampler = null)
{
var awsXRayTraceId = GenerateAWSXRayCompatibleTraceId();

// TODO: Apply API to directly modify trace id once .NET runtime publicizes it.
activity.SetParentId(awsXRayTraceId, default, activity.ActivityTraceFlags);

// When not using instrumented library and creating root activity using ActivitySource.StartActivity(),
Expand Down Expand Up @@ -197,3 +197,4 @@ private static ActivitySamplingResult ComputeRootActivitySamplingResult(
return ActivitySamplingResult.PropagationData;
}
}
#endif
71 changes: 71 additions & 0 deletions src/OpenTelemetry.Extensions.AWS/AWSXRayIdGenerator.net6.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// <copyright file="AWSXRayIdGenerator.net6.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NET6_0_OR_GREATER
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Security.Cryptography;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Extensions.AWS;

/// <summary>
/// Generate AWS X-Ray compatible trace IDs.
/// See https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids.
/// </summary>
public static class AWSXRayIdGenerator
{
/// <summary>
/// Sets <see cref="Activity.TraceIdGenerator"/> to <see cref="GenerateAWSXRayCompatibleTraceId"/>.
/// </summary>
public static void ReplaceTraceId()
{
Activity.TraceIdGenerator = GenerateAWSXRayCompatibleTraceId;
}

/// <summary>
/// Sets <see cref="Activity.TraceIdGenerator"/> to <see cref="GenerateAWSXRayCompatibleTraceId"/>.
/// </summary>
/// <param name="sampler">Unused. (See deprecation message.)</param>
[Obsolete($"When targeting .NET 6.0 or later, the X-Ray ID generator does not need to update the sampling decision. Use ${nameof(ReplaceTraceId)}() instead.")]
public static void ReplaceTraceId(Sampler sampler)
{
ReplaceTraceId();
}

/// <summary>
/// Generates an AWS X-Ray compatible trace ID.
/// </summary>
/// <returns>
/// An <see cref="ActivityTraceId"/> whose first 4 bytes are the big-endian unix timestamp (in seconds) and whose
/// remaining 12 bytes are randomly generated.
/// </returns>
internal static ActivityTraceId GenerateAWSXRayCompatibleTraceId()
{
Span<byte> buffer = stackalloc byte[16];

// intentionally truncating to 4 bytes because AWS X-Ray requires 8 hex characters
var seconds = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
_ = BinaryPrimitives.TryWriteUInt32BigEndian(buffer, seconds);

// fill the rest of the buffer with random bytes
RandomNumberGenerator.Fill(buffer.Slice(4, 12));

return ActivityTraceId.CreateFromBytes(buffer);
}
}
#endif
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Extensions.AWS/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
supporting ResourceBuilderExtensions extension, and migrate all detectors to
implement OpenTelemetry.Resources.IResourceDetector
([#875](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/875))
* Add a `net6.0` build with optimized trace ID generation using the new
`Activity.TraceIdGenerator` API.
([#1096](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1096))
* Drop support for `AWSLambdaResourceDetector`.
AWS Lambda Resources are detected by `OpenTelemetry.Instrumentation.AWSLambda`
package
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<Description>OpenTelemetry extensions for AWS.</Description>
<MinVerTagPrefix>Extensions.AWSXRay-</MinVerTagPrefix>
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ internal static string ToXRayTraceIdFormat(string traceId)

sb.Append(Version);
sb.Append(TraceIdDelimiter);
sb.Append(traceId.Substring(0, EpochHexDigits));
sb.Append(traceId, 0, EpochHexDigits);
sb.Append(TraceIdDelimiter);
sb.Append(traceId.Substring(EpochHexDigits));
sb.Append(traceId, EpochHexDigits, traceId.Length - EpochHexDigits);

return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ public static TracerProviderBuilder AddXRayTraceId(this TracerProviderBuilder bu
return builder;
}

#if NET6_0_OR_GREATER
/// <summary>
/// Replace the trace id of root activity.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="sampler">Unused. (See deprecation message.)</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/>.</returns>
[System.Obsolete($"When targeting .NET 6.0 or later, the X-Ray ID generator does not need to update the sampling decision. Use ${nameof(AddXRayTraceId)} instead.")]
#else
/// <summary>
/// 1. Replace the trace id of root activity.
/// 2. Update the sampling decision for root activity when it's created through ActivitySource.StartActivity().
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="sampler"><see cref="Sampler"/> being used.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/>.</returns>
#endif
public static TracerProviderBuilder AddXRayTraceIdWithSampler(this TracerProviderBuilder builder, Sampler sampler)
{
Guard.ThrowIfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public void TestGenerateTraceIdForRootNode()
activity.Start();

Assert.NotEqual(originalTraceId, activity.TraceId);
#if NET6_0_OR_GREATER
// the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which does not change the parent ID
Assert.Equal(originalParentSpanId, activity.ParentSpanId);
#else
Assert.NotEqual(originalParentSpanId, activity.ParentSpanId);
#endif
Assert.Equal("0000000000000000", activity.ParentSpanId.ToHexString());
Assert.Equal(originalTraceFlag, activity.ActivityTraceFlags);
}
Expand Down Expand Up @@ -81,11 +86,13 @@ public void TestGenerateTraceIdForNonRootNodeNotSampled()
[Fact]
public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOn()
{
#pragma warning disable CS0618 // Type or member is obsolete
using (Sdk.CreateTracerProviderBuilder()
.AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(1.0))
.AddSource("TestTraceIdBasedSamplerOn")
.SetSampler(new TraceIdRatioBasedSampler(1.0))
.Build())
#pragma warning restore CS0618 // Type or member is obsolete
{
using (var activitySource = new ActivitySource("TestTraceIdBasedSamplerOn"))
{
Expand All @@ -100,11 +107,13 @@ public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSam
[Fact]
public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOff()
{
#pragma warning disable CS0618 // Type or member is obsolete
using (Sdk.CreateTracerProviderBuilder()
.AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(0.0))
.AddSource("TestTraceIdBasedSamplerOff")
.SetSampler(new TraceIdRatioBasedSampler(0.0))
.Build())
#pragma warning restore CS0618 // Type or member is obsolete
{
using (var activitySource = new ActivitySource("TestTraceIdBasedSamplerOff"))
{
Expand Down