diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index edaf04e992c..b6b3afd07d8 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -30,9 +30,7 @@ public static void Main() builder.AddOpenTelemetry(options => { options.IncludeScopes = true; - options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( - serviceName: "MyService", - serviceVersion: "1.0.0")); + options.ConfigureResource(r => r.AddService(serviceName: "MyService", serviceVersion: "1.0.0")); options.AddConsoleExporter(); }); }); diff --git a/docs/logs/customizing-the-sdk/README.md b/docs/logs/customizing-the-sdk/README.md index e9cc9e020ef..8b1d9bcb944 100644 --- a/docs/logs/customizing-the-sdk/README.md +++ b/docs/logs/customizing-the-sdk/README.md @@ -52,26 +52,25 @@ var loggerFactory = LoggerFactory.Create(builder => For more information on Processors, please review [Extending the SDK](../extending-the-sdk/README.md#processor) -### SetResourceBuilder +### ConfigureResource [Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) is the immutable representation of the entity producing the telemetry. If no `Resource` is explicitly configured, the default is to use a resource indicating this [Telemetry SDK](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#telemetry-sdk). -The `SetResourceBuilder` method on `OpenTelemetryLoggerOptions` can be used to -set a single `ResourceBuilder`. If `SetResourceBuilder` is called multiple -times, only the last is kept. It is not possible to change the resource builder +The `ConfigureResource` method on `OpenTelemetryLoggerOptions` can be used to +configure the `ResourceBuilder`. It is not possible to change the resources *after* creating the `LoggerFactory`. -The snippet below shows configuring a custom `ResourceBuilder` to the provider. +The snippet below shows configuring the `ResourceBuilder` of the provider. ```csharp var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(options => { - options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( + options.ConfigureResource(r => r.AddService( serviceName: "MyService", serviceVersion: "1.0.0" )); diff --git a/examples/AspNetCore/Program.cs b/examples/AspNetCore/Program.cs index c507e7df44e..0ee7a24c22a 100644 --- a/examples/AspNetCore/Program.cs +++ b/examples/AspNetCore/Program.cs @@ -24,27 +24,28 @@ var builder = WebApplication.CreateBuilder(args); -var serviceName = "AspNetCoreExampleService"; - // OpenTelemetry var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; // Switch between Zipkin/Jaeger/OTLP by setting UseExporter in appsettings.json. var tracingExporter = builder.Configuration.GetValue("UseTracingExporter").ToLowerInvariant(); -var resourceBuilder = tracingExporter switch +var serviceName = tracingExporter switch { - "jaeger" => ResourceBuilder.CreateDefault().AddService(builder.Configuration.GetValue("Jaeger:ServiceName"), serviceVersion: assemblyVersion, serviceInstanceId: Environment.MachineName), - "zipkin" => ResourceBuilder.CreateDefault().AddService(builder.Configuration.GetValue("Zipkin:ServiceName"), serviceVersion: assemblyVersion, serviceInstanceId: Environment.MachineName), - "otlp" => ResourceBuilder.CreateDefault().AddService(builder.Configuration.GetValue("Otlp:ServiceName"), serviceVersion: assemblyVersion, serviceInstanceId: Environment.MachineName), - _ => ResourceBuilder.CreateDefault().AddService(serviceName, serviceVersion: assemblyVersion, serviceInstanceId: Environment.MachineName), + "jaeger" => builder.Configuration.GetValue("Jaeger:ServiceName"), + "zipkin" => builder.Configuration.GetValue("Zipkin:ServiceName"), + "otlp" => builder.Configuration.GetValue("Otlp:ServiceName"), + _ => "AspNetCoreExampleService", }; +Action configureResource = r => r.AddService( + serviceName, serviceVersion: assemblyVersion, serviceInstanceId: Environment.MachineName); + // Traces builder.Services.AddOpenTelemetryTracing(options => { options - .SetResourceBuilder(resourceBuilder) + .ConfigureResource(configureResource) .SetSampler(new AlwaysOnSampler()) .AddHttpClientInstrumentation() .AddAspNetCoreInstrumentation(); @@ -88,7 +89,7 @@ builder.Logging.AddOpenTelemetry(options => { - options.SetResourceBuilder(resourceBuilder); + options.ConfigureResource(configureResource); var logExporter = builder.Configuration.GetValue("UseLogExporter").ToLowerInvariant(); switch (logExporter) { @@ -114,7 +115,7 @@ // Metrics builder.Services.AddOpenTelemetryMetrics(options => { - options.SetResourceBuilder(resourceBuilder) + options.ConfigureResource(configureResource) .AddHttpClientInstrumentation() .AddAspNetCoreInstrumentation(); diff --git a/examples/Console/TestConsoleExporter.cs b/examples/Console/TestConsoleExporter.cs index 9929beab33b..031e6fc4e09 100644 --- a/examples/Console/TestConsoleExporter.cs +++ b/examples/Console/TestConsoleExporter.cs @@ -39,7 +39,7 @@ private static object RunWithActivitySource() // and use Console exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("Samples.SampleClient", "Samples.SampleServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("console-test")) + .ConfigureResource(res => res.AddService("console-test")) .AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestHttpClient.cs b/examples/Console/TestHttpClient.cs index 734cae06b58..ba122c5f09a 100644 --- a/examples/Console/TestHttpClient.cs +++ b/examples/Console/TestHttpClient.cs @@ -34,7 +34,7 @@ internal static object Run() using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation() - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("http-service-example")) + .ConfigureResource(r => r.AddService("http-service-example")) .AddSource("http-client-test") .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestInMemoryExporter.cs b/examples/Console/TestInMemoryExporter.cs index f46ed608bdf..5cfbe327417 100644 --- a/examples/Console/TestInMemoryExporter.cs +++ b/examples/Console/TestInMemoryExporter.cs @@ -51,7 +51,7 @@ private static void RunWithActivitySource(ICollection exportedItems) // and use InMemory exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("Samples.SampleClient", "Samples.SampleServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("inmemory-test")) + .ConfigureResource(r => r.AddService("inmemory-test")) .AddInMemoryExporter(exportedItems) .Build(); diff --git a/examples/Console/TestJaegerExporter.cs b/examples/Console/TestJaegerExporter.cs index 2c67e9bff66..aca3909886e 100644 --- a/examples/Console/TestJaegerExporter.cs +++ b/examples/Console/TestJaegerExporter.cs @@ -56,7 +56,7 @@ internal static object RunWithActivity(string host, int port) // Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient" // and use the Jaeger exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("jaeger-test")) + .ConfigureResource(r => r.AddService("jaeger-test")) .AddSource("Samples.SampleClient", "Samples.SampleServer") .AddJaegerExporter(o => { diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 35fc3dd1324..78ee9cbed97 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -33,7 +33,7 @@ internal static object Run(MetricsOptions options) using var meter = new Meter("TestMeter"); var providerBuilder = Sdk.CreateMeterProviderBuilder() - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("myservice")) + .ConfigureResource(r => r.AddService("myservice")) .AddMeter(meter.Name); // All instruments from this meter are enabled. if (options.UseExporter.Equals("otlp", StringComparison.OrdinalIgnoreCase)) diff --git a/examples/Console/TestOTelShimWithConsoleExporter.cs b/examples/Console/TestOTelShimWithConsoleExporter.cs index 984ac7ad380..31bb3c85f6b 100644 --- a/examples/Console/TestOTelShimWithConsoleExporter.cs +++ b/examples/Console/TestOTelShimWithConsoleExporter.cs @@ -28,7 +28,7 @@ internal static object Run(OpenTelemetryShimOptions options) // and use a single pipeline with a custom MyProcessor, and Console exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyWebServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) + .ConfigureResource(r => r.AddService("MyServiceName")) .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestOpenTracingShim.cs b/examples/Console/TestOpenTracingShim.cs index f0e967992a2..7361c3aa4d5 100644 --- a/examples/Console/TestOpenTracingShim.cs +++ b/examples/Console/TestOpenTracingShim.cs @@ -31,7 +31,7 @@ internal static object Run(OpenTracingShimOptions options) // and use Console exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyWebServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) + .ConfigureResource(r => r.AddService("MyServiceName")) .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestOtlpExporter.cs b/examples/Console/TestOtlpExporter.cs index 3936f1b0ef8..f5417e77c66 100644 --- a/examples/Console/TestOtlpExporter.cs +++ b/examples/Console/TestOtlpExporter.cs @@ -71,7 +71,7 @@ private static object RunWithActivitySource(string endpoint, string protocol) // and use OTLP exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("Samples.SampleClient", "Samples.SampleServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("otlp-test")) + .ConfigureResource(r => r.AddService("otlp-test")) .AddOtlpExporter(opt => { // If endpoint was not specified, the proper one will be selected according to the protocol. diff --git a/examples/Console/TestZipkinExporter.cs b/examples/Console/TestZipkinExporter.cs index 40c44551b84..0f6bbce2272 100644 --- a/examples/Console/TestZipkinExporter.cs +++ b/examples/Console/TestZipkinExporter.cs @@ -39,7 +39,7 @@ internal static object Run(string zipkinUri) // and use the Zipkin exporter. using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("Samples.SampleClient", "Samples.SampleServer") - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("zipkin-test")) + .ConfigureResource(r => r.AddService("zipkin-test")) .AddZipkinExporter(o => { o.Endpoint = new Uri(zipkinUri); diff --git a/examples/GrpcService/Startup.cs b/examples/GrpcService/Startup.cs index a2d1adf7e0d..c7034011b02 100644 --- a/examples/GrpcService/Startup.cs +++ b/examples/GrpcService/Startup.cs @@ -45,7 +45,7 @@ public void ConfigureServices(IServiceCollection services) { case "jaeger": services.AddOpenTelemetryTracing((builder) => builder - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(this.Configuration.GetValue("Jaeger:ServiceName"))) + .ConfigureResource(r => r.AddService(this.Configuration.GetValue("Jaeger:ServiceName"))) .AddAspNetCoreInstrumentation() .AddJaegerExporter(jaegerOptions => { diff --git a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt index 3f5fd985f93..ecaf006aa47 100644 --- a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt @@ -9,4 +9,7 @@ OpenTelemetry.Logs.LogRecord.TraceId.set -> void OpenTelemetry.Logs.LogRecord.TraceState.set -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider() -> void -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void \ No newline at end of file +OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! +~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 diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index 3f5fd985f93..43156ee3c3a 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -7,6 +7,9 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void OpenTelemetry.Logs.LogRecord.TraceId.set -> void OpenTelemetry.Logs.LogRecord.TraceState.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! OpenTelemetry.Logs.OpenTelemetryLoggerProvider.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider() -> void -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void \ No newline at end of file +OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void +~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 \ No newline at end of file diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 3f5fd985f93..ecaf006aa47 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -9,4 +9,7 @@ OpenTelemetry.Logs.LogRecord.TraceId.set -> void OpenTelemetry.Logs.LogRecord.TraceState.set -> void OpenTelemetry.Logs.OpenTelemetryLoggerProvider.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider() -> void -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void \ No newline at end of file +OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! +~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 diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index 3f5fd985f93..43156ee3c3a 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -7,6 +7,9 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void OpenTelemetry.Logs.LogRecord.TraceId.set -> void OpenTelemetry.Logs.LogRecord.TraceState.set -> void +OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! OpenTelemetry.Logs.OpenTelemetryLoggerProvider.ForceFlush(int timeoutMilliseconds = -1) -> bool OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider() -> void -OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void \ No newline at end of file +OpenTelemetry.Logs.OpenTelemetryLoggerProvider.OpenTelemetryLoggerProvider(System.Action! configure) -> void +~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 \ No newline at end of file diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index bad0e536281..05ee2bcfda2 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -38,6 +38,10 @@ Released 2022-June-1 * Swallow `ObjectDisposedException` in `BatchExportProcessor` and `PeriodicExportingMetricReader`. ([#3291](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3291)) +* Add `ConfigureResource` which can replace SetResourceBuilder more succinctly + in most cases and has greater flexibility (applies to + TracerProvicerBuilder, MeterProviderBuilder, OpenTelemetryLoggingOptions). + ([#3307](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3307)) ## 1.3.0-beta.2 diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs index 1a779ac63ef..d20fcce2b47 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs @@ -16,6 +16,7 @@ #nullable enable +using System; using System.Collections.Generic; using OpenTelemetry.Internal; using OpenTelemetry.Resources; @@ -73,6 +74,8 @@ public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor processo /// /// Sets the from which the Resource associated with /// this provider is built from. Overwrites currently set ResourceBuilder. + /// You should usually use instead + /// (call if desired). /// /// from which Resource will be built. /// Returns for chaining. @@ -83,5 +86,18 @@ public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBui this.ResourceBuilder = resourceBuilder; return this; } + + /// + /// 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 OpenTelemetryLoggerOptions ConfigureResource(Action configure) + { + Guard.ThrowIfNull(configure, nameof(configure)); + configure(this.ResourceBuilder); + return this; + } } } diff --git a/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs b/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs index e3614abb495..aa5a5ace8e5 100644 --- a/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs +++ b/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs @@ -45,6 +45,16 @@ protected MeterProviderBuilderBase() internal List MetricReaders { get; } = new List(); + internal ResourceBuilder ResourceBuilder + { + get => this.resourceBuilder; + set + { + Debug.Assert(value != null, $"{nameof(this.ResourceBuilder)} must not be set to null"); + this.resourceBuilder = value; + } + } + /// public override MeterProviderBuilder AddInstrumentation(Func instrumentationFactory) { @@ -126,14 +136,6 @@ internal MeterProviderBuilder SetMaxMetricPointsPerMetricStream(int maxMetricPoi return this; } - internal MeterProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) - { - Debug.Assert(resourceBuilder != null, $"{nameof(resourceBuilder)} must not be null"); - - this.resourceBuilder = resourceBuilder; - return this; - } - /// /// Run the configured actions to initialize the . /// diff --git a/src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs index bda417b078d..c9ab4726712 100644 --- a/src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs @@ -183,6 +183,8 @@ public static MeterProviderBuilder SetMaxMetricPointsPerMetricStream(this MeterP /// /// Sets the from which the Resource associated with /// this provider is built from. Overwrites currently set ResourceBuilder. + /// You should usually use instead + /// (call if desired). /// /// MeterProviderBuilder instance. /// from which Resource will be built. @@ -191,7 +193,27 @@ public static MeterProviderBuilder SetResourceBuilder(this MeterProviderBuilder { if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase) { - meterProviderBuilderBase.SetResourceBuilder(resourceBuilder); + meterProviderBuilderBase.ResourceBuilder = resourceBuilder; + } + + return meterProviderBuilder; + } + + /// + /// Modify the from which the Resource associated with + /// this provider is built from in-place. + /// + /// MeterProviderBuilder instance. + /// An action which modifies the provided in-place. + /// Returns for chaining. + public static MeterProviderBuilder ConfigureResource(this MeterProviderBuilder meterProviderBuilder, Action configure) + { + Guard.ThrowIfNull(meterProviderBuilder, nameof(meterProviderBuilder)); + Guard.ThrowIfNull(configure, nameof(configure)); + + if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase) + { + configure(meterProviderBuilderBase.ResourceBuilder); } return meterProviderBuilder; diff --git a/src/OpenTelemetry/README.md b/src/OpenTelemetry/README.md index 27638d3b9a6..e51346cd85e 100644 --- a/src/OpenTelemetry/README.md +++ b/src/OpenTelemetry/README.md @@ -238,15 +238,14 @@ is the immutable representation of the entity producing the telemetry. If no `Resource` is explicitly configured, the default is to use a resource indicating this [Telemetry SDK](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#telemetry-sdk). -`SetResourceBuilder` method on `TracerProviderBuilder` can be used to set a -`ResourceBuilder` on the provider. When the provider is built, it automatically +The `ConfigureResource` method on `TracerProviderBuilder` can be used to set a +configure the resource on the provider. When the provider is built, it automatically builds the final `Resource` from the configured `ResourceBuilder`. As with -samplers, there can only be a single `Resource` associated with a provider. If -multiple `SetResourceBuilder` is called, the last one wins. Also, it is not -possible to change the resource builder *after* the provider is built, by -calling the `Build()` method on the `TracerProviderBuilder`. `ResourceBuilder` -offers various methods to construct resource comprising of multiple attributes -from various sources. +samplers, there can only be a single `Resource` associated with a provider. +It is not possible to change the resource builder *after* the provider is +built, by calling the `Build()` method on the `TracerProviderBuilder`. +`ResourceBuilder` offers various methods to construct resource comprising +of multiple attributes from various sources. The snippet below shows configuring a custom `ResourceBuilder` to the provider. @@ -256,7 +255,7 @@ using OpenTelemetry.Resources; using OpenTelemetry.Trace; using var tracerProvider = Sdk.CreateTracerProviderBuilder() - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) + .ConfigureResource(r => r.AddService("MyServiceName")) .Build(); ``` diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index 7ab0f53a70f..708c7bc45b1 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -38,6 +38,20 @@ protected TracerProviderBuilderBase() { } + /// + /// Gets or sets the from which the Resource associated with + /// this provider is built from. Setting this overwrites currently set ResourceBuilder. + /// + internal ResourceBuilder ResourceBuilder + { + get => this.resourceBuilder; + set + { + Guard.ThrowIfNull(value); + this.resourceBuilder = value; + } + } + /// public override TracerProviderBuilder AddInstrumentation( Func instrumentationFactory) @@ -135,20 +149,6 @@ internal TracerProviderBuilder SetSampler(Sampler sampler) return this; } - /// - /// Sets the from which the Resource associated with - /// this provider is built from. Overwrites currently set ResourceBuilder. - /// - /// from which Resource will be built. - /// Returns for chaining. - internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) - { - Guard.ThrowIfNull(resourceBuilder); - - this.resourceBuilder = resourceBuilder; - return this; - } - /// /// Adds processor to the provider. /// diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index 2c1bef0ef5e..24f26e98e01 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -16,6 +16,7 @@ using System; using System.Diagnostics; +using OpenTelemetry.Internal; using OpenTelemetry.Resources; namespace OpenTelemetry.Trace @@ -61,6 +62,8 @@ public static TracerProviderBuilder SetSampler(this TracerProviderBuilder tracer /// /// Sets the from which the Resource associated with /// this provider is built from. Overwrites currently set ResourceBuilder. + /// You should usually use instead + /// (call if desired). /// /// TracerProviderBuilder instance. /// from which Resource will be built. @@ -69,7 +72,27 @@ public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilde { if (tracerProviderBuilder is TracerProviderBuilderBase tracerProviderBuilderBase) { - tracerProviderBuilderBase.SetResourceBuilder(resourceBuilder); + tracerProviderBuilderBase.ResourceBuilder = resourceBuilder; + } + + return tracerProviderBuilder; + } + + /// + /// Modify the from which the Resource associated with + /// this provider is built from in-place. + /// + /// TracerProviderBuilder instance. + /// An action which modifies the provided in-place. + /// Returns for chaining. + public static TracerProviderBuilder ConfigureResource(this TracerProviderBuilder tracerProviderBuilder, Action configure) + { + Guard.ThrowIfNull(tracerProviderBuilder, nameof(tracerProviderBuilder)); + Guard.ThrowIfNull(configure, nameof(configure)); + + if (tracerProviderBuilder is TracerProviderBuilderBase tracerProviderBuilderBase) + { + configure(tracerProviderBuilderBase.ResourceBuilder); } return tracerProviderBuilder; diff --git a/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs b/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs index 7f719db01ab..ff2555eaeb3 100644 --- a/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs +++ b/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs @@ -52,7 +52,7 @@ public void GlobalSetup() this.tracerProvider = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation() - .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(ServiceName)) + .ConfigureResource(r => r.AddService(ServiceName)) .AddSource(SourceName) .Build(); diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index 59c1335bee4..7463df95843 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -1068,11 +1068,15 @@ public void AddLegacyOperationNameAddsActivityListenerForEmptyActivitySource() Assert.True(emptyActivitySource.HasListeners()); } - [Fact] - public void TracerProviderSdkBuildsWithSDKResource() + [Theory] + [InlineData(true)] + [InlineData(false)] + public void TracerProviderSdkBuildsWithSDKResource(bool useConfigure) { - var tracerProvider = Sdk.CreateTracerProviderBuilder().SetResourceBuilder( - ResourceBuilder.CreateDefault().AddTelemetrySdk()).Build(); + var tracerProvider = useConfigure ? + Sdk.CreateTracerProviderBuilder().SetResourceBuilder( + ResourceBuilder.CreateDefault().AddTelemetrySdk()).Build() : + Sdk.CreateTracerProviderBuilder().ConfigureResource(r => r.AddTelemetrySdk()).Build(); var resource = tracerProvider.GetResource(); var attributes = resource.Attributes;