From 423999223a9c9e69be081d03d52147caf8e7157a Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 18 Oct 2022 10:27:06 -0700 Subject: [PATCH] [SDK, Jaeger, Zipkin, & Otlp] Support loading envvars for BatchExportActivityProcessorOptions from IConfiguration (#3776) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support loading envvars for ExportActivityProcessorOptions & BatchExportActivityProcessorOptions from IConfiguration. * Update src/OpenTelemetry/CHANGELOG.md Co-authored-by: Piotr Kiełkowicz * Patch CHANGELOG. * Unit test. Co-authored-by: Cijo Thomas Co-authored-by: Piotr Kiełkowicz --- .../JaegerExporterOptions.cs | 4 ++- .../OtlpExporterOptions.cs | 4 ++- .../ZipkinExporterOptions.cs | 4 ++- .../.publicApi/net462/PublicAPI.Unshipped.txt | 1 + .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + .../netstandard2.1/PublicAPI.Unshipped.txt | 1 + src/OpenTelemetry/CHANGELOG.md | 6 ++++ ...viderBuilderServiceCollectionExtensions.cs | 30 +++++++++++------ .../BatchExportActivityProcessorOptions.cs | 23 ++++++++++--- .../Builder/TracerProviderBuilderBase.cs | 4 +-- .../Trace/ExportActivityProcessorOptions.cs | 21 ++++++++++-- ...BatchExportActivityProcessorOptionsTest.cs | 32 +++++++++++++++++++ 13 files changed, 110 insertions(+), 22 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs index f2946388b90..592fd554330 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs @@ -76,6 +76,8 @@ internal JaegerExporterOptions(IConfiguration configuration) { this.Endpoint = endpoint; } + + this.BatchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration); } /// @@ -114,7 +116,7 @@ internal JaegerExporterOptions(IConfiguration configuration) /// /// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter. /// - public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions(); + public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } /// /// Gets or sets the factory function called to create the @@ -142,7 +144,7 @@ public Uri Endpoint /// /// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is Batch. /// - public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions(); + public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } /// /// Gets or sets the factory function called to create the @@ -81,7 +83,7 @@ internal ZipkinExporterOptions(IConfiguration configuration) /// /// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter. /// - public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions(); + public BatchExportProcessorOptions BatchExportProcessorOptions { get; set; } /// /// Gets or sets the factory function called to create the void OpenTelemetry.Metrics.MetricPoint.GetHistogramMax() -> double OpenTelemetry.Metrics.MetricPoint.GetHistogramMin() -> double OpenTelemetry.Metrics.MetricPoint.HasMinMax() -> bool +OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions(Microsoft.Extensions.Configuration.IConfiguration! configuration) -> void static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder! diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index eedd81fc086..4bd71b5f99c 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -17,6 +17,7 @@ OpenTelemetry.Metrics.HistogramConfiguration.RecordMinMax.set -> void OpenTelemetry.Metrics.MetricPoint.GetHistogramMax() -> double OpenTelemetry.Metrics.MetricPoint.GetHistogramMin() -> double OpenTelemetry.Metrics.MetricPoint.HasMinMax() -> bool +OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions(Microsoft.Extensions.Configuration.IConfiguration! configuration) -> void static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index eedd81fc086..4bd71b5f99c 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -17,6 +17,7 @@ OpenTelemetry.Metrics.HistogramConfiguration.RecordMinMax.set -> void OpenTelemetry.Metrics.MetricPoint.GetHistogramMax() -> double OpenTelemetry.Metrics.MetricPoint.GetHistogramMin() -> double OpenTelemetry.Metrics.MetricPoint.HasMinMax() -> bool +OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions(Microsoft.Extensions.Configuration.IConfiguration! configuration) -> void static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index eedd81fc086..4bd71b5f99c 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -17,6 +17,7 @@ OpenTelemetry.Metrics.HistogramConfiguration.RecordMinMax.set -> void OpenTelemetry.Metrics.MetricPoint.GetHistogramMax() -> double OpenTelemetry.Metrics.MetricPoint.GetHistogramMin() -> double OpenTelemetry.Metrics.MetricPoint.HasMinMax() -> bool +OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions(Microsoft.Extensions.Configuration.IConfiguration! configuration) -> void static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static Microsoft.Extensions.DependencyInjection.MeterProviderBuilderServiceCollectionExtensions.ConfigureOpenTelemetryMetrics(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection! static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder! diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index c0f909e2129..563550db2a1 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +* Added support for loading environment variables from `IConfiguration` when + using the `MetricReaderOptions` & `BatchExportActivityProcessorOptions` + classes. + ([#3760](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3760), + [#3776](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3776)) + ## 1.4.0-beta.2 Released 2022-Oct-17 diff --git a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs index f08c48c9395..f10e072f8b7 100644 --- a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs +++ b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs @@ -21,12 +21,31 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using OpenTelemetry.Internal; using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; namespace Microsoft.Extensions.DependencyInjection; internal static class ProviderBuilderServiceCollectionExtensions { - public static IServiceCollection AddOpenTelemetryProviderBuilderServices(this IServiceCollection services) + public static IServiceCollection AddOpenTelemetryMeterProviderBuilderServices(this IServiceCollection services) + { + services.AddOpenTelemetryProviderBuilderServices(); + + services.RegisterOptionsFactory(configuration => new MetricReaderOptions(configuration)); + + return services; + } + + public static IServiceCollection AddOpenTelemetryTracerProviderBuilderServices(this IServiceCollection services) + { + services.AddOpenTelemetryProviderBuilderServices(); + + services.RegisterOptionsFactory(configuration => new ExportActivityProcessorOptions(configuration)); + + return services; + } + + private static IServiceCollection AddOpenTelemetryProviderBuilderServices(this IServiceCollection services) { Debug.Assert(services != null, "services was null"); @@ -41,13 +60,4 @@ public static IServiceCollection AddOpenTelemetryProviderBuilderServices(this IS return services!; } - - public static IServiceCollection AddOpenTelemetryMeterProviderBuilderServices(this IServiceCollection services) - { - services.AddOpenTelemetryProviderBuilderServices(); - - services.RegisterOptionsFactory(configuration => new MetricReaderOptions(configuration)); - - return services; - } } diff --git a/src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs b/src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs index 5b78df251cb..8ea8890e8ba 100644 --- a/src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs +++ b/src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs @@ -14,8 +14,11 @@ // limitations under the License. // +#nullable enable + using System; using System.Diagnostics; +using Microsoft.Extensions.Configuration; using OpenTelemetry.Internal; namespace OpenTelemetry.Trace @@ -39,26 +42,36 @@ public class BatchExportActivityProcessorOptions : BatchExportProcessorOptions + /// Initializes a new instance of the class. + /// public BatchExportActivityProcessorOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) { - int value; + } - if (EnvironmentVariableHelper.LoadNumeric(ExporterTimeoutEnvVarKey, out value)) + /// + /// Initializes a new instance of the class. + /// + /// . + public BatchExportActivityProcessorOptions(IConfiguration configuration) + { + if (configuration.TryGetIntValue(ExporterTimeoutEnvVarKey, out int value)) { this.ExporterTimeoutMilliseconds = value; } - if (EnvironmentVariableHelper.LoadNumeric(MaxExportBatchSizeEnvVarKey, out value)) + if (configuration.TryGetIntValue(MaxExportBatchSizeEnvVarKey, out value)) { this.MaxExportBatchSize = value; } - if (EnvironmentVariableHelper.LoadNumeric(MaxQueueSizeEnvVarKey, out value)) + if (configuration.TryGetIntValue(MaxQueueSizeEnvVarKey, out value)) { this.MaxQueueSize = value; } - if (EnvironmentVariableHelper.LoadNumeric(ScheduledDelayEnvVarKey, out value)) + if (configuration.TryGetIntValue(ScheduledDelayEnvVarKey, out value)) { this.ScheduledDelayMilliseconds = value; } diff --git a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs index 223f07e798c..6692c72edae 100644 --- a/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs @@ -51,7 +51,7 @@ internal TracerProviderBuilderBase(IServiceCollection services) { Guard.ThrowIfNull(services); - services.AddOpenTelemetryProviderBuilderServices(); + services.AddOpenTelemetryTracerProviderBuilderServices(); services.TryAddSingleton(sp => new TracerProviderSdk(sp, ownsServiceProvider: false)); this.services = services; @@ -64,7 +64,7 @@ protected TracerProviderBuilderBase() { var services = new ServiceCollection(); - services.AddOpenTelemetryProviderBuilderServices(); + services.AddOpenTelemetryTracerProviderBuilderServices(); this.services = services; this.ownsServices = true; diff --git a/src/OpenTelemetry/Trace/ExportActivityProcessorOptions.cs b/src/OpenTelemetry/Trace/ExportActivityProcessorOptions.cs index 450e893e83d..173dc960a26 100644 --- a/src/OpenTelemetry/Trace/ExportActivityProcessorOptions.cs +++ b/src/OpenTelemetry/Trace/ExportActivityProcessorOptions.cs @@ -16,13 +16,30 @@ #nullable enable +using Microsoft.Extensions.Configuration; using OpenTelemetry.Internal; namespace OpenTelemetry.Trace; +/// +/// Options for configuring either a or . +/// public class ExportActivityProcessorOptions { - private BatchExportActivityProcessorOptions? batchExportProcessorOptions; + private BatchExportActivityProcessorOptions batchExportProcessorOptions; + + /// + /// Initializes a new instance of the class. + /// + public ExportActivityProcessorOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) + { + } + + internal ExportActivityProcessorOptions(IConfiguration configuration) + { + this.batchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration); + } /// /// Gets or sets the export processor type to be used. The default value is . @@ -34,7 +51,7 @@ public class ExportActivityProcessorOptions /// public BatchExportActivityProcessorOptions BatchExportProcessorOptions { - get => this.batchExportProcessorOptions ??= new(); + get => this.batchExportProcessorOptions; set { Guard.ThrowIfNull(value); diff --git a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorOptionsTest.cs b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorOptionsTest.cs index 712f854aa88..15ecbd6babd 100644 --- a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorOptionsTest.cs +++ b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorOptionsTest.cs @@ -15,6 +15,8 @@ // using System; +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; using Xunit; namespace OpenTelemetry.Trace.Tests @@ -59,6 +61,36 @@ public void BatchExportProcessorOptions_EnvironmentVariableOverride() Assert.Equal(4, options.ScheduledDelayMilliseconds); } + [Fact] + public void ExportActivityProcessorOptions_UsingIConfiguration() + { + var values = new Dictionary() + { + [BatchExportActivityProcessorOptions.MaxQueueSizeEnvVarKey] = "1", + [BatchExportActivityProcessorOptions.MaxExportBatchSizeEnvVarKey] = "2", + [BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey] = "3", + [BatchExportActivityProcessorOptions.ScheduledDelayEnvVarKey] = "4", + }; + + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + var parentOptions = new ExportActivityProcessorOptions(configuration); + + Assert.Equal(1, parentOptions.BatchExportProcessorOptions.MaxQueueSize); + Assert.Equal(2, parentOptions.BatchExportProcessorOptions.MaxExportBatchSize); + Assert.Equal(3, parentOptions.BatchExportProcessorOptions.ExporterTimeoutMilliseconds); + Assert.Equal(4, parentOptions.BatchExportProcessorOptions.ScheduledDelayMilliseconds); + + var options = new BatchExportActivityProcessorOptions(configuration); + + Assert.Equal(1, options.MaxQueueSize); + Assert.Equal(2, options.MaxExportBatchSize); + Assert.Equal(3, options.ExporterTimeoutMilliseconds); + Assert.Equal(4, options.ScheduledDelayMilliseconds); + } + [Fact] public void BatchExportProcessorOptions_InvalidPortEnvironmentVariableOverride() {