From 3ca478b57774c11a155df36156d428b2fd012efc Mon Sep 17 00:00:00 2001 From: Tim Langhorst Date: Fri, 24 Feb 2023 13:52:19 +0100 Subject: [PATCH 1/3] Added ServiceProvider option to ConfigureResource --- .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + .../OpenTelemetryBuilder.cs | 23 +++++++++++++++++++ .../.publicApi/net462/PublicAPI.Unshipped.txt | 2 ++ .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.0/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.1/PublicAPI.Unshipped.txt | 2 ++ .../Builder/MeterProviderBuilderExtensions.cs | 20 ++++++++++++++++ .../TracerProviderBuilderExtensions.cs | 22 ++++++++++++++++++ 8 files changed, 74 insertions(+) diff --git a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 3812863f17c..f8195743aaa 100644 --- a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,6 +1,7 @@ Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions OpenTelemetry.OpenTelemetryBuilder OpenTelemetry.OpenTelemetryBuilder.ConfigureResource(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! +OpenTelemetry.OpenTelemetryBuilder.ConfigureResource(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! OpenTelemetry.OpenTelemetryBuilder.Services.get -> Microsoft.Extensions.DependencyInjection.IServiceCollection! OpenTelemetry.OpenTelemetryBuilder.WithMetrics() -> OpenTelemetry.OpenTelemetryBuilder! OpenTelemetry.OpenTelemetryBuilder.WithMetrics(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! diff --git a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs index 110d2b89915..4648d330fee 100644 --- a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs +++ b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs @@ -68,6 +68,29 @@ public OpenTelemetryBuilder ConfigureResource( return this; } + /// + /// Registers an action to configure the s used + /// by tracing and metrics. + /// + /// + /// configuration + /// action. + /// The supplied for chaining + /// calls. + public OpenTelemetryBuilder ConfigureResource( + Action configure) + { + Guard.ThrowIfNull(configure); + + this.Services.ConfigureOpenTelemetryMeterProvider( + (sp, builder) => builder.ConfigureResource(configure)); + + this.Services.ConfigureOpenTelemetryTracerProvider( + (sp, builder) => builder.ConfigureResource(configure)); + + return this; + } + /// /// Adds metric services into the builder. /// diff --git a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt index c7e1bde089f..f4ee6e508a6 100644 --- a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt @@ -25,6 +25,8 @@ static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Func! implementationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! +static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! OpenTelemetry.Metrics.MetricType.LongSumNonMonotonic = 138 -> OpenTelemetry.Metrics.MetricType OpenTelemetry.Metrics.MetricType.DoubleSumNonMonotonic = 141 -> OpenTelemetry.Metrics.MetricType diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index c7e1bde089f..f4ee6e508a6 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -25,6 +25,8 @@ static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Func! implementationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! +static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! OpenTelemetry.Metrics.MetricType.LongSumNonMonotonic = 138 -> OpenTelemetry.Metrics.MetricType OpenTelemetry.Metrics.MetricType.DoubleSumNonMonotonic = 141 -> OpenTelemetry.Metrics.MetricType diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index c7e1bde089f..f4ee6e508a6 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -25,6 +25,8 @@ static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Func! implementationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! +static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! OpenTelemetry.Metrics.MetricType.LongSumNonMonotonic = 138 -> OpenTelemetry.Metrics.MetricType OpenTelemetry.Metrics.MetricType.DoubleSumNonMonotonic = 141 -> OpenTelemetry.Metrics.MetricType diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index c7e1bde089f..f4ee6e508a6 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -25,6 +25,8 @@ static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Func! implementationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! +static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, System.Action! configure) -> OpenTelemetry.Metrics.MeterProviderBuilder! static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureResource(this OpenTelemetry.Trace.TracerProviderBuilder! tracerProviderBuilder, System.Action! configure) -> OpenTelemetry.Trace.TracerProviderBuilder! OpenTelemetry.Metrics.MetricType.LongSumNonMonotonic = 138 -> OpenTelemetry.Metrics.MetricType OpenTelemetry.Metrics.MetricType.DoubleSumNonMonotonic = 141 -> OpenTelemetry.Metrics.MetricType diff --git a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs index 12f46b18208..dbccb30016b 100644 --- a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs @@ -303,6 +303,26 @@ public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder m return meterProviderBuilder; } + /// + /// Modify the from which the Resource associated with + /// this provider is built from in-place. + /// + /// . + /// An action which modifies the provided in-place. + /// The supplied for chaining. + public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder meterProviderBuilder, Action configure) + { + meterProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is MeterProviderBuilderSdk meterProviderBuilderSdk) + { + meterProviderBuilderSdk.ConfigureResource(resourceBuilder => configure(resourceBuilder, sp)); + } + }); + + return meterProviderBuilder; + } + /// /// Run the given actions to initialize the . /// diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs index a47bf9eb09e..2d1671ae5ac 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs @@ -165,6 +165,28 @@ public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder return tracerProviderBuilder; } + /// + /// Modify the from which the Resource associated with + /// this provider is built from in-place. + /// + /// . + /// An action which modifies the provided in-place. + /// Returns for chaining. + public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder tracerProviderBuilder, Action configure) + { + Guard.ThrowIfNull(configure); + + tracerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is TracerProviderBuilderSdk tracerProviderBuilderSdk) + { + tracerProviderBuilderSdk.ConfigureResource(traceBuilder => configure(traceBuilder, sp)); + } + }); + + return tracerProviderBuilder; + } + /// /// Adds a processor to the provider. /// From 4652db265b5ffa039e50c9088c86b0de781eaf77 Mon Sep 17 00:00:00 2001 From: Tim Langhorst Date: Fri, 24 Feb 2023 14:24:33 +0100 Subject: [PATCH 2/3] Added ServiceProvider option to WithTracing and WithMetrics --- .../netstandard2.0/PublicAPI.Unshipped.txt | 2 + .../OpenTelemetryBuilder.cs | 42 ++++++++++++++++++- .../Builder/MeterProviderBuilderBase.cs | 10 +++++ .../Builder/TracerProviderBuilderBase.cs | 22 ++++++++-- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index f8195743aaa..2c10767d8b5 100644 --- a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -5,7 +5,9 @@ OpenTelemetry.OpenTelemetryBuilder.ConfigureResource(System.Action Microsoft.Extensions.DependencyInjection.IServiceCollection! OpenTelemetry.OpenTelemetryBuilder.WithMetrics() -> OpenTelemetry.OpenTelemetryBuilder! OpenTelemetry.OpenTelemetryBuilder.WithMetrics(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! +OpenTelemetry.OpenTelemetryBuilder.WithMetrics(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! OpenTelemetry.OpenTelemetryBuilder.WithTracing() -> OpenTelemetry.OpenTelemetryBuilder! OpenTelemetry.OpenTelemetryBuilder.WithTracing(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! +OpenTelemetry.OpenTelemetryBuilder.WithTracing(System.Action! configure) -> OpenTelemetry.OpenTelemetryBuilder! static Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetry(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> OpenTelemetry.OpenTelemetryBuilder! diff --git a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs index 4648d330fee..ad8c7e57e37 100644 --- a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs +++ b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs @@ -118,7 +118,26 @@ public OpenTelemetryBuilder WithMetrics(Action configure) var builder = new MeterProviderBuilderBase(this.Services); - configure(builder); + builder.Configure(configure); + + return this; + } + + /// + /// Adds metric services into the builder. + /// + /// + /// + /// configuration callback. + /// The supplied for chaining + /// calls. + public OpenTelemetryBuilder WithMetrics(Action configure) + { + Guard.ThrowIfNull(configure); + + var builder = new MeterProviderBuilderBase(this.Services); + + builder.Configure(configure); return this; } @@ -150,7 +169,26 @@ public OpenTelemetryBuilder WithTracing(Action configure) var builder = new TracerProviderBuilderBase(this.Services); - configure(builder); + builder.Configure(configure); + + return this; + } + + /// + /// Adds tracing services into the builder. + /// + /// + /// + /// configuration callback. + /// The supplied for chaining + /// calls. + public OpenTelemetryBuilder WithTracing(Action configure) + { + Guard.ThrowIfNull(configure); + + var builder = new TracerProviderBuilderBase(this.Services); + + builder.Configure(configure); return this; } diff --git a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs index d77953fc4b1..d68db4a8f9a 100644 --- a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs +++ b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs @@ -102,6 +102,16 @@ MeterProviderBuilder IDeferredMeterProviderBuilder.Configure(Action this.Build(); + internal void Configure(Action configure) + { + this.ConfigureBuilderInternal((_, builder) => configure(builder)); + } + + internal void Configure(Action configure) + { + this.ConfigureBuilderInternal((sp, builder) => configure(builder, sp)); + } + /// /// Run the configured actions to initialize the . /// diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs index a1edc48d960..103511d01e6 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs @@ -41,7 +41,8 @@ public TracerProviderBuilderBase() .AddOpenTelemetrySharedProviderBuilderServices() .AddOpenTelemetryTracerProviderBuilderServices() .TryAddSingleton( - sp => throw new NotSupportedException("Self-contained TracerProvider cannot be accessed using the application IServiceProvider call Build instead.")); + sp => throw new NotSupportedException( + "Self-contained TracerProvider cannot be accessed using the application IServiceProvider call Build instead.")); services.ConfigureOpenTelemetryTracerProvider((sp, builder) => this.services = null); @@ -69,7 +70,8 @@ internal TracerProviderBuilderBase(IServiceCollection services) TracerProvider? ITracerProviderBuilder.Provider => null; /// - public override TracerProviderBuilder AddInstrumentation(Func instrumentationFactory) + public override TracerProviderBuilder AddInstrumentation( + Func instrumentationFactory) { Guard.ThrowIfNull(instrumentationFactory); @@ -112,12 +114,23 @@ TracerProviderBuilder ITracerProviderBuilder.ConfigureServices(Action this.ConfigureServicesInternal(configure); /// - TracerProviderBuilder IDeferredTracerProviderBuilder.Configure(Action configure) + TracerProviderBuilder IDeferredTracerProviderBuilder.Configure( + Action configure) => this.ConfigureBuilderInternal(configure); internal TracerProvider InvokeBuild() => this.Build(); + internal void Configure(Action configure) + { + this.ConfigureBuilderInternal((_, builder) => configure(builder)); + } + + internal void Configure(Action configure) + { + this.ConfigureBuilderInternal((sp, builder) => configure(builder, sp)); + } + /// /// Adds instrumentation to the provider. /// d @@ -154,7 +167,8 @@ protected TracerProvider Build() { if (!this.allowBuild) { - throw new NotSupportedException("A TracerProviderBuilder bound to external service cannot be built directly. Access the TracerProvider using the application IServiceProvider instead."); + throw new NotSupportedException( + "A TracerProviderBuilder bound to external service cannot be built directly. Access the TracerProvider using the application IServiceProvider instead."); } var services = this.services; From 89df3ddc54803062c6e76aa78638034764728f35 Mon Sep 17 00:00:00 2001 From: Tim Langhorst Date: Fri, 24 Feb 2023 14:41:07 +0100 Subject: [PATCH 3/3] Added changelog entry --- src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md b/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md index 480d06e5918..8a1bc585b7a 100644 --- a/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md +++ b/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md @@ -5,6 +5,9 @@ * Removed deprecated extensions: `AddOpenTelemetryTracing`, `AddOpenTelemetryMetrics`, `Configure`, & `GetServices`. ([#4071](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4071)) +* Added optional IServiceProvider arguments to the callbacks + of `ConfigureResource`, `WithTracing`, & `WithMetrics`. + ([#4229](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4229)) ## 1.4.0-rc.4