From 145ff7f9c5f5e5c7d2447d3a0aa2306802585358 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 16:56:53 +0100 Subject: [PATCH 01/18] Added IServiceProvider option to callbacks for hosting extensions --- .../netstandard2.0/PublicAPI.Unshipped.txt | 3 + .../OpenTelemetryBuilder.cs | 65 ++++++++++++++++++- .../.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/MeterProviderBuilderBase.cs | 10 +++ .../Builder/MeterProviderBuilderExtensions.cs | 20 ++++++ .../Builder/TracerProviderBuilderBase.cs | 22 +++++-- .../TracerProviderBuilderExtensions.cs | 22 +++++++ 10 files changed, 144 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 3812863f17c..2c10767d8b5 100644 --- a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,10 +1,13 @@ 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! +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 110d2b89915..ad8c7e57e37 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. /// @@ -95,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; } @@ -127,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/.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/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/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/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; 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 5c86fc17fbb1250d9a5d6f1836bcd02475ee5276 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 16:59:17 +0100 Subject: [PATCH 02/18] 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..af29f085add 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`. + ([#4230](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4230)) ## 1.4.0-rc.4 From c6dc65d72301003e27f02e221fa63f78f7f4c1c8 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 20:04:35 +0100 Subject: [PATCH 03/18] Reverted changes to metrics and tracing --- .../OpenTelemetryBuilder.cs | 42 +------------------ .../Builder/MeterProviderBuilderBase.cs | 10 ----- .../Builder/TracerProviderBuilderBase.cs | 10 ----- 3 files changed, 2 insertions(+), 60 deletions(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs index ad8c7e57e37..4648d330fee 100644 --- a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs +++ b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs @@ -118,26 +118,7 @@ public OpenTelemetryBuilder WithMetrics(Action configure) var builder = new MeterProviderBuilderBase(this.Services); - 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); + configure(builder); return this; } @@ -169,26 +150,7 @@ public OpenTelemetryBuilder WithTracing(Action configure) var builder = new TracerProviderBuilderBase(this.Services); - 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); + configure(builder); return this; } diff --git a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs index d68db4a8f9a..d77953fc4b1 100644 --- a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs +++ b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs @@ -102,16 +102,6 @@ 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 103511d01e6..0ea194e105c 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs @@ -121,16 +121,6 @@ TracerProviderBuilder IDeferredTracerProviderBuilder.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 From b7f61560dd938a5b0e3e325017f5c14ecfdc4f2c Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 20:05:47 +0100 Subject: [PATCH 04/18] Removed unneeded stuff from the public API --- .../.publicApi/netstandard2.0/PublicAPI.Unshipped.txt | 2 -- 1 file changed, 2 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 2c10767d8b5..f8195743aaa 100644 --- a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -5,9 +5,7 @@ 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! From 12ef20cc2e0999c0ec04812ee6e6f30c5797c773 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 20:08:30 +0100 Subject: [PATCH 05/18] Swapped params --- src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs | 2 +- .../Metrics/Builder/MeterProviderBuilderExtensions.cs | 4 ++-- .../Trace/Builder/TracerProviderBuilderExtensions.cs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs index 4648d330fee..aa82ded9269 100644 --- a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs +++ b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs @@ -78,7 +78,7 @@ public OpenTelemetryBuilder ConfigureResource( /// The supplied for chaining /// calls. public OpenTelemetryBuilder ConfigureResource( - Action configure) + Action configure) { Guard.ThrowIfNull(configure); diff --git a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs index dbccb30016b..bae540aefb3 100644 --- a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs @@ -310,13 +310,13 @@ public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder m /// . /// An action which modifies the provided in-place. /// The supplied for chaining. - public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder meterProviderBuilder, Action configure) + public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder meterProviderBuilder, Action configure) { meterProviderBuilder.ConfigureBuilder((sp, builder) => { if (builder is MeterProviderBuilderSdk meterProviderBuilderSdk) { - meterProviderBuilderSdk.ConfigureResource(resourceBuilder => configure(resourceBuilder, sp)); + meterProviderBuilderSdk.ConfigureResource(resourceBuilder => configure(sp, resourceBuilder)); } }); diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs index 2d1671ae5ac..931d1dee30e 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs @@ -172,7 +172,7 @@ public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder /// . /// An action which modifies the provided in-place. /// Returns for chaining. - public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder tracerProviderBuilder, Action configure) + public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder tracerProviderBuilder, Action configure) { Guard.ThrowIfNull(configure); @@ -180,7 +180,7 @@ public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder { if (builder is TracerProviderBuilderSdk tracerProviderBuilderSdk) { - tracerProviderBuilderSdk.ConfigureResource(traceBuilder => configure(traceBuilder, sp)); + tracerProviderBuilderSdk.ConfigureResource(traceBuilder => configure(sp, traceBuilder)); } }); From 5da158c1b3657901f81bbe133337805e145dcb57 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 20:57:47 +0100 Subject: [PATCH 06/18] Added more documentation on the option configuration --- .../README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md index 8b8f60c6d62..96808c03fa3 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md @@ -67,7 +67,14 @@ and environment variables. The `OtlpExporterOptions` type setters take precedence over the environment variables. This can be achieved by providing an `Action` delegate to the -`AddOtlpExporter()` method. +`AddOtlpExporter()` method or using `AddOptions()`. + +If additional services from the dependency injection are required, they can be configured like this: +```csharp +services.AddOptions().Configure((opts, svc) => { + // ... +}); +``` TODO: Show metrics specific configuration (i.e MetricReaderOptions). From 796cad8573b288848af29185bea4416037dc4fc0 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:15:33 +0100 Subject: [PATCH 07/18] Made the AddDetector variant for the ResolvingResourceDetector available publicly --- src/OpenTelemetry/Resources/ResourceBuilder.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index 44657001c9d..25f408bf675 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -142,8 +142,7 @@ public ResourceBuilder AddDetector(IResourceDetector resourceDetector) /// /// Resource detector factory. /// Supplied for call chaining. - // Note: This API may be made public if there is a need for it. - internal ResourceBuilder AddDetector(Func resourceDetectorFactory) + public ResourceBuilder AddDetector(Func resourceDetectorFactory) { Guard.ThrowIfNull(resourceDetectorFactory); From dbbe47f3dfc8db1bd6252a022f40f4e3ae2d2c96 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:26:19 +0100 Subject: [PATCH 08/18] Added more documentation to the IResourceDetector --- .../README.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/OpenTelemetry.Extensions.Hosting/README.md b/src/OpenTelemetry.Extensions.Hosting/README.md index d7e9cedce72..503a84baea9 100644 --- a/src/OpenTelemetry.Extensions.Hosting/README.md +++ b/src/OpenTelemetry.Extensions.Hosting/README.md @@ -83,6 +83,28 @@ app.Run(); A fully functional example can be found [here](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/examples/AspNetCore). +### Resources +To dynamically add resources at startup from the dependency injection you can provide an `IResourceDetector`. +To make use of it add it to the dependency injection and they you can use the `ISerivceProvider` add it to OpenTelemetry: +```csharp +public class YourResourceDetector : IResourceDetector +{ + public Resource Detect() + { + return ResourceBuilder.CreateEmpty() + .AddService("your service name") + .Build(); + } +} + +services.AddSingleton(); + +services.AddOpenTelemetry() + .ConfigureResource(builder => + builder.AddDetector(sp => sp!.GetRequiredService())) + WithTracing(builder => builder.AddConsoleExporter()); +``` + ## Migrating from pre-release versions of OpenTelemetry.Extensions.Hosting Pre-release versions (all versions prior to 1.4.0) of From 7e5ef072629d49e7ecb396d3d3f2a02b56d45662 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:31:18 +0100 Subject: [PATCH 09/18] Fixed APIs --- .../netstandard2.0/PublicAPI.Unshipped.txt | 1 - .../OpenTelemetryBuilder.cs | 23 ------------------- .../README.md | 6 ++--- .../.publicApi/net462/PublicAPI.Unshipped.txt | 3 +-- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 3 +-- .../netstandard2.0/PublicAPI.Unshipped.txt | 3 +-- .../netstandard2.1/PublicAPI.Unshipped.txt | 3 +-- .../Builder/MeterProviderBuilderExtensions.cs | 20 ---------------- 8 files changed, 7 insertions(+), 55 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..3812863f17c 100644 --- a/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.Hosting/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,7 +1,6 @@ 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 aa82ded9269..110d2b89915 100644 --- a/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs +++ b/src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs @@ -68,29 +68,6 @@ 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.Extensions.Hosting/README.md b/src/OpenTelemetry.Extensions.Hosting/README.md index 503a84baea9..7376d2e0a40 100644 --- a/src/OpenTelemetry.Extensions.Hosting/README.md +++ b/src/OpenTelemetry.Extensions.Hosting/README.md @@ -87,7 +87,7 @@ A fully functional example can be found To dynamically add resources at startup from the dependency injection you can provide an `IResourceDetector`. To make use of it add it to the dependency injection and they you can use the `ISerivceProvider` add it to OpenTelemetry: ```csharp -public class YourResourceDetector : IResourceDetector +public class MyResourceDetector : IResourceDetector { public Resource Detect() { @@ -97,11 +97,11 @@ public class YourResourceDetector : IResourceDetector } } -services.AddSingleton(); +services.AddSingleton(); services.AddOpenTelemetry() .ConfigureResource(builder => - builder.AddDetector(sp => sp!.GetRequiredService())) + builder.AddDetector(sp => sp!.GetRequiredService())) WithTracing(builder => builder.AddConsoleExporter()); ``` diff --git a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt index f4ee6e508a6..8238c4c8ff1 100644 --- a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt @@ -25,8 +25,7 @@ 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 +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index f4ee6e508a6..8238c4c8ff1 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -25,8 +25,7 @@ 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 +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index f4ee6e508a6..8238c4c8ff1 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -25,8 +25,7 @@ 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 +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index f4ee6e508a6..8238c4c8ff1 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -25,8 +25,7 @@ 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 +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs index bae540aefb3..12f46b18208 100644 --- a/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderExtensions.cs @@ -303,26 +303,6 @@ 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(sp, resourceBuilder)); - } - }); - - return meterProviderBuilder; - } - /// /// Run the given actions to initialize the . /// From b53a42c66a7b2f79f6fa86344f87e49d2966cb81 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:34:19 +0100 Subject: [PATCH 10/18] Fixed Changelog --- src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md | 3 --- src/OpenTelemetry/CHANGELOG.md | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md b/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md index af29f085add..480d06e5918 100644 --- a/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md +++ b/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md @@ -5,9 +5,6 @@ * 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`. - ([#4230](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4230)) ## 1.4.0-rc.4 diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index e4645324677..e1c8339cc91 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -14,6 +14,9 @@ Released 2023-Feb-10 could be removed. ([#4174](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4174)) +* Made the `IServiceProvider` variant of `AddDetector` publicly available. + ([#4230](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4230)) + ## 1.4.0-rc.3 Released 2023-Feb-01 From 2c242c50cf8be90d09c0fc3561f63dad50bc3302 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:35:43 +0100 Subject: [PATCH 11/18] Removed unneeded version of ConfigureResource --- .../TracerProviderBuilderExtensions.cs | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs index 931d1dee30e..a47bf9eb09e 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs @@ -165,28 +165,6 @@ 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(sp, traceBuilder)); - } - }); - - return tracerProviderBuilder; - } - /// /// Adds a processor to the provider. /// From 74155133edb60cbeae9d868f549f17816d8dd419 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Fri, 24 Feb 2023 21:40:50 +0100 Subject: [PATCH 12/18] Removed extra formatting --- .../Trace/Builder/TracerProviderBuilderBase.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs index 0ea194e105c..a1edc48d960 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs @@ -41,8 +41,7 @@ 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); @@ -70,8 +69,7 @@ internal TracerProviderBuilderBase(IServiceCollection services) TracerProvider? ITracerProviderBuilder.Provider => null; /// - public override TracerProviderBuilder AddInstrumentation( - Func instrumentationFactory) + public override TracerProviderBuilder AddInstrumentation(Func instrumentationFactory) { Guard.ThrowIfNull(instrumentationFactory); @@ -114,8 +112,7 @@ TracerProviderBuilder ITracerProviderBuilder.ConfigureServices(Action this.ConfigureServicesInternal(configure); /// - TracerProviderBuilder IDeferredTracerProviderBuilder.Configure( - Action configure) + TracerProviderBuilder IDeferredTracerProviderBuilder.Configure(Action configure) => this.ConfigureBuilderInternal(configure); internal TracerProvider InvokeBuild() @@ -157,8 +154,7 @@ 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 2847aa6417350f87d34eb9c2e1df767329451433 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Mon, 6 Mar 2023 18:35:42 +0100 Subject: [PATCH 13/18] Reverted public API change --- src/OpenTelemetry/Resources/ResourceBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index 25f408bf675..928d6c0c9fb 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -142,7 +142,7 @@ public ResourceBuilder AddDetector(IResourceDetector resourceDetector) /// /// Resource detector factory. /// Supplied for call chaining. - public ResourceBuilder AddDetector(Func resourceDetectorFactory) + internal ResourceBuilder AddDetector(Func resourceDetectorFactory) { Guard.ThrowIfNull(resourceDetectorFactory); From 2b4831b60b6d2676a818aa276ea4822c9045c957 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Mon, 6 Mar 2023 18:36:34 +0100 Subject: [PATCH 14/18] Reverted note --- src/OpenTelemetry/Resources/ResourceBuilder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index 928d6c0c9fb..44657001c9d 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -142,6 +142,7 @@ public ResourceBuilder AddDetector(IResourceDetector resourceDetector) /// /// Resource detector factory. /// Supplied for call chaining. + // Note: This API may be made public if there is a need for it. internal ResourceBuilder AddDetector(Func resourceDetectorFactory) { Guard.ThrowIfNull(resourceDetectorFactory); From 261ea06f295cfd5c4be8b34e8bea320f435f5819 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Mon, 6 Mar 2023 18:38:00 +0100 Subject: [PATCH 15/18] Reverted API doc changes --- src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt | 1 - src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt | 1 - .../.publicApi/netstandard2.0/PublicAPI.Unshipped.txt | 1 - .../.publicApi/netstandard2.1/PublicAPI.Unshipped.txt | 1 - src/OpenTelemetry/CHANGELOG.md | 3 --- 5 files changed, 7 deletions(-) diff --git a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt index 8005aa12402..e69de29bb2d 100644 --- a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1 +0,0 @@ -OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index 8005aa12402..e69de29bb2d 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -1 +0,0 @@ -OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 8005aa12402..e69de29bb2d 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +0,0 @@ -OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index 8005aa12402..e69de29bb2d 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -1 +0,0 @@ -OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 7bc935354b0..a213f19fa68 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -18,9 +18,6 @@ Released 2023-Feb-10 could be removed. ([#4174](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4174)) -* Made the `IServiceProvider` variant of `AddDetector` publicly available. - ([#4230](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4230)) - ## 1.4.0-rc.3 Released 2023-Feb-01 From 290d486bda086a9ccc6c5c6a82a5606b331e4920 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Mon, 6 Mar 2023 21:34:28 +0100 Subject: [PATCH 16/18] Improved resource detector docs --- src/OpenTelemetry.Extensions.Hosting/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/README.md b/src/OpenTelemetry.Extensions.Hosting/README.md index 5b4479ed102..886fcd1df0d 100644 --- a/src/OpenTelemetry.Extensions.Hosting/README.md +++ b/src/OpenTelemetry.Extensions.Hosting/README.md @@ -101,8 +101,9 @@ services.AddSingleton(); services.AddOpenTelemetry() .ConfigureResource(builder => - builder.AddDetector(sp => sp!.GetRequiredService())) - WithTracing(builder => builder.AddConsoleExporter()); + builder.AddDetector(sp => sp.GetRequiredService())) + .WithTracing(builder => builder.AddConsoleExporter()) + .WithMetrics(builder => builder.AddConsoleExporter()); ``` ## Migrating from pre-release versions of OpenTelemetry.Extensions.Hosting From ba2edeb16f7fdcabc0690da3e7fb4d5aa6a4baf6 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Mon, 6 Mar 2023 21:39:24 +0100 Subject: [PATCH 17/18] Satisfied markdownlint --- .../README.md | 4 +++- src/OpenTelemetry.Extensions.Hosting/README.md | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md index 96808c03fa3..ccdc6d93d1c 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md @@ -69,7 +69,9 @@ The `OtlpExporterOptions` type setters take precedence over the environment vari This can be achieved by providing an `Action` delegate to the `AddOtlpExporter()` method or using `AddOptions()`. -If additional services from the dependency injection are required, they can be configured like this: +If additional services from the dependency injection are required, they can be +configured like this: + ```csharp services.AddOptions().Configure((opts, svc) => { // ... diff --git a/src/OpenTelemetry.Extensions.Hosting/README.md b/src/OpenTelemetry.Extensions.Hosting/README.md index 886fcd1df0d..2f8757b616c 100644 --- a/src/OpenTelemetry.Extensions.Hosting/README.md +++ b/src/OpenTelemetry.Extensions.Hosting/README.md @@ -84,8 +84,12 @@ A fully functional example can be found [here](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/examples/AspNetCore). ### Resources -To dynamically add resources at startup from the dependency injection you can provide an `IResourceDetector`. -To make use of it add it to the dependency injection and they you can use the `ISerivceProvider` add it to OpenTelemetry: + +To dynamically add resources at startup from the dependency injection you can +provide an `IResourceDetector`. +To make use of it add it to the dependency injection and they you can use the +`ISerivceProvider` add it to OpenTelemetry: + ```csharp public class MyResourceDetector : IResourceDetector { From 6d81fe4b59b0e79308a6eecea20a0116f9876af2 Mon Sep 17 00:00:00 2001 From: Cromefire_ Date: Thu, 30 Mar 2023 19:36:09 +0200 Subject: [PATCH 18/18] Added dependency injection to sample --- src/OpenTelemetry.Extensions.Hosting/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Extensions.Hosting/README.md b/src/OpenTelemetry.Extensions.Hosting/README.md index 2f8757b616c..9dd0ee96f08 100644 --- a/src/OpenTelemetry.Extensions.Hosting/README.md +++ b/src/OpenTelemetry.Extensions.Hosting/README.md @@ -93,10 +93,18 @@ To make use of it add it to the dependency injection and they you can use the ```csharp public class MyResourceDetector : IResourceDetector { + private readonly IWebHostEnvironment webHostEnvironment; + + public MyResourceDetector(IWebHostEnvironment webHostEnvironment) + { + this.webHostEnvironment = webHostEnvironment; + } + public Resource Detect() { return ResourceBuilder.CreateEmpty() - .AddService("your service name") + .AddService(serviceName: this.webHostEnvironment.ApplicationName) + .AddAttributes(new Dictionary { ["host.environment"] = this.webHostEnvironment.EnvironmentName }) .Build(); } }