From fb5342c5301eefa98ccad80f27d8504d59bc4fd6 Mon Sep 17 00:00:00 2001 From: Mike Goldsmith Date: Fri, 26 Apr 2024 15:28:07 +0100 Subject: [PATCH] [OpenTelemetry.Extensions] Add baggage Activity processor (#1659) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cijo Thomas Co-authored-by: Piotr Kiełkowicz --- .github/component_owners.yml | 2 ++ .../.publicApi/net462/PublicAPI.Unshipped.txt | 1 + .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + src/OpenTelemetry.Extensions/CHANGELOG.md | 3 +++ src/OpenTelemetry.Extensions/README.md | 15 +++++++++++++ .../Trace/BaggageActivityProcessor.cs | 21 +++++++++++++++++++ .../TracerProviderBuilderExtensions.cs | 16 ++++++++++++++ 8 files changed, 60 insertions(+) create mode 100644 src/OpenTelemetry.Extensions/Trace/BaggageActivityProcessor.cs diff --git a/.github/component_owners.yml b/.github/component_owners.yml index 7a978f4db3..73aba2c284 100644 --- a/.github/component_owners.yml +++ b/.github/component_owners.yml @@ -20,6 +20,7 @@ components: - SergeyKanzhelev src/OpenTelemetry.Extensions/: - codeblanch + - mikegoldsmith src/OpenTelemetry.Extensions.AWS/: - srprash - atshaw43 @@ -114,6 +115,7 @@ components: - SergeyKanzhelev test/OpenTelemetry.Extensions.Tests/: - codeblanch + - mikegoldsmith test/OpenTelemetry.Extensions.AWS.Tests/: - srprash - atshaw43 diff --git a/src/OpenTelemetry.Extensions/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions/.publicApi/net462/PublicAPI.Unshipped.txt index 02a765cd0b..4b388d9972 100644 --- a/src/OpenTelemetry.Extensions/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions/.publicApi/net462/PublicAPI.Unshipped.txt @@ -11,3 +11,4 @@ OpenTelemetry.Logs.LogToActivityEventConversionOptions.StateConverter.set -> voi OpenTelemetry.Trace.TracerProviderBuilderExtensions static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AttachLogsToActivityEvent(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAutoFlushActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Func! predicate, int timeoutMilliseconds = 10000) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddBaggageActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Extensions/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions/.publicApi/net6.0/PublicAPI.Unshipped.txt index 02a765cd0b..4b388d9972 100644 --- a/src/OpenTelemetry.Extensions/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -11,3 +11,4 @@ OpenTelemetry.Logs.LogToActivityEventConversionOptions.StateConverter.set -> voi OpenTelemetry.Trace.TracerProviderBuilderExtensions static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AttachLogsToActivityEvent(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAutoFlushActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Func! predicate, int timeoutMilliseconds = 10000) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddBaggageActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 02a765cd0b..4b388d9972 100644 --- a/src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -11,3 +11,4 @@ OpenTelemetry.Logs.LogToActivityEventConversionOptions.StateConverter.set -> voi OpenTelemetry.Trace.TracerProviderBuilderExtensions static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AttachLogsToActivityEvent(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAutoFlushActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Func! predicate, int timeoutMilliseconds = 10000) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddBaggageActivityProcessor(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Extensions/CHANGELOG.md b/src/OpenTelemetry.Extensions/CHANGELOG.md index 83e698517d..4c4d011360 100644 --- a/src/OpenTelemetry.Extensions/CHANGELOG.md +++ b/src/OpenTelemetry.Extensions/CHANGELOG.md @@ -8,6 +8,9 @@ * Update OpenTelemetry SDK version to `1.8.1`. ([#1668](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1668)) +* Add Baggage Activity Processor. + ([#1659](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1659)) + ## 1.0.0-beta.4 Released 2023-Feb-27 diff --git a/src/OpenTelemetry.Extensions/README.md b/src/OpenTelemetry.Extensions/README.md index 2111fe4f43..8e4ca16c64 100644 --- a/src/OpenTelemetry.Extensions/README.md +++ b/src/OpenTelemetry.Extensions/README.md @@ -37,3 +37,18 @@ public static TracerProviderBuilder AddMyExporter(this TracerProviderBuilder bui .AddAutoFlushActivityProcessor(a => a.Parent == null && (a.Kind == ActivityKind.Server || a.Kind == ActivityKind.Consumer), 5000); } ``` + +### BaggageActivityProcessor + +The BaggageActivityProcessor reads entries stored in Baggage from the current `Baggage` +and adds the baggage keys and values to the `Activity` as tags (attributes) on start. + +Add this activity processor to a tracer provider. + +Example of adding BaggageActivityProcessor to `TracerProvider`: + +```cs +var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddBaggageActivityProcessor() + .Build(); +``` diff --git a/src/OpenTelemetry.Extensions/Trace/BaggageActivityProcessor.cs b/src/OpenTelemetry.Extensions/Trace/BaggageActivityProcessor.cs new file mode 100644 index 0000000000..23d08dec76 --- /dev/null +++ b/src/OpenTelemetry.Extensions/Trace/BaggageActivityProcessor.cs @@ -0,0 +1,21 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using System.Diagnostics; + +namespace OpenTelemetry.Trace; + +/// +/// Activity processor that adds fields to every new span. +/// +internal sealed class BaggageActivityProcessor : BaseProcessor +{ + /// + public override void OnStart(Activity activity) + { + foreach (var entry in Baggage.Current) + { + activity.SetTag(entry.Key, entry.Value); + } + } +} diff --git a/src/OpenTelemetry.Extensions/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Extensions/TracerProviderBuilderExtensions.cs index d64fb7c5cd..5034cb11e7 100644 --- a/src/OpenTelemetry.Extensions/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Extensions/TracerProviderBuilderExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; using System.Threading; +using OpenTelemetry.Internal; namespace OpenTelemetry.Trace; @@ -45,6 +46,21 @@ public static TracerProviderBuilder AddAutoFlushActivityProcessor( #pragma warning disable CA2000 // Dispose objects before losing scope return builder.AddProcessor(new AutoFlushActivityProcessor(predicate, timeoutMilliseconds)); +#pragma warning restore CA2000 // Dispose objects before losing scope + } + + /// + /// Adds the to the . + /// + /// to add the to. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddBaggageActivityProcessor( + this TracerProviderBuilder builder) + { + Guard.ThrowIfNull(builder); + +#pragma warning disable CA2000 // Dispose objects before losing scope + return builder.AddProcessor(new BaggageActivityProcessor()); #pragma warning restore CA2000 // Dispose objects before losing scope } }