Skip to content

Commit

Permalink
[Zipkin] Support loading envvars from IConfiguration (#3759)
Browse files Browse the repository at this point in the history
* Support loading envvars from IConfiguration in Zipkin exporter.

* CHANGELOG patch.
  • Loading branch information
CodeBlanch committed Oct 12, 2022
1 parent f191e84 commit e8af2a0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ConfigurationExtensions.cs" Link="Includes\ConfigurationExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public static TracerProviderBuilder AddZipkinExporter(this TracerProviderBuilder

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) =>
{
Expand Down
8 changes: 7 additions & 1 deletion src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

Expand Down Expand Up @@ -44,8 +45,13 @@ public sealed class ZipkinExporterOptions
/// Initializes zipkin endpoint.
/// </summary>
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;
}
Expand Down
18 changes: 18 additions & 0 deletions test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -207,6 +208,23 @@ public void ErrorGettingUriFromEnvVarSetsDefaultEndpointValue()
}
}

[Fact]
public void EndpointConfigurationUsingIConfiguration()
{
var values = new Dictionary<string, string>()
{
[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()
{
Expand Down

0 comments on commit e8af2a0

Please sign in to comment.