diff --git a/src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md b/src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md index 352288c42a8..eb922b12f2b 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Added support for loading environment variables from `IConfiguration` when + using the `AddZipkinExporter` extension + ([#3759](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3759)) + ## 1.4.0-beta.1 Released 2022-Sep-29 diff --git a/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj b/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj index 0aee4bd6334..c5e14a100de 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj +++ b/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs index 261cecd1a64..93a43014874 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs @@ -62,10 +62,15 @@ public static TracerProviderBuilder AddZipkinExporter( name ??= Options.DefaultName; - if (configure != null) + builder.ConfigureServices(services => { - builder.ConfigureServices(services => services.Configure(name, configure)); - } + if (configure != null) + { + services.Configure(name, configure); + } + + services.RegisterOptionsFactory(configuration => new ZipkinExporterOptions(configuration)); + }); return builder.ConfigureBuilder((sp, builder) => { diff --git a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs index 9c354574a72..d51eb007b81 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs @@ -17,6 +17,7 @@ using System; using System.Diagnostics; using System.Net.Http; +using Microsoft.Extensions.Configuration; using OpenTelemetry.Internal; using OpenTelemetry.Trace; @@ -44,8 +45,13 @@ public sealed class ZipkinExporterOptions /// Initializes zipkin endpoint. /// public ZipkinExporterOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) { - if (EnvironmentVariableHelper.LoadUri(ZipkinEndpointEnvVar, out Uri endpoint)) + } + + internal ZipkinExporterOptions(IConfiguration configuration) + { + if (configuration.TryGetUriValue(ZipkinEndpointEnvVar, out var endpoint)) { this.Endpoint = endpoint; } diff --git a/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs b/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs index 4cd2d97d2e5..3c19c2c106c 100644 --- a/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs @@ -23,6 +23,7 @@ using System.Net; using System.Net.Http; using System.Text; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Exporter.Zipkin.Implementation; using OpenTelemetry.Resources; @@ -207,6 +208,23 @@ public void ErrorGettingUriFromEnvVarSetsDefaultEndpointValue() } } + [Fact] + public void EndpointConfigurationUsingIConfiguration() + { + var values = new Dictionary() + { + [ZipkinExporterOptions.ZipkinEndpointEnvVar] = "http://custom-endpoint:12345", + }; + + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + var options = new ZipkinExporterOptions(configuration); + + Assert.Equal(new Uri("http://custom-endpoint:12345"), options.Endpoint); + } + [Fact] public void UserHttpFactoryCalled() {