Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Jaeger to load environment variables through IConfiguration.
  • Loading branch information
CodeBlanch committed Oct 3, 2022
commit d4ccabf63f1cdfddb51d811fbe61fa29a3188c6d
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/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 `AddJaegerExporter` extension
([#XXXX](https://github.com/open-telemetry/opentelemetry-dotnet/pull/XXXX))

## 1.4.0-beta.1

Released 2022-Sep-29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public static TracerProviderBuilder AddJaegerExporter(

name ??= Options.DefaultName;

if (configure != null)
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services => services.Configure(name, configure));
}
if (configure != null)
{
services.Configure(name, configure);
}

services.AddSingleton<IOptionsFactory<JaegerExporterOptions>, JaegerExporterOptions.JaegerExporterOptionsFactory>();
});

return builder.ConfigureBuilder((sp, builder) =>
{
Expand Down
65 changes: 54 additions & 11 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

Expand All @@ -43,37 +45,63 @@ public class JaegerExporterOptions

internal static readonly Func<HttpClient> DefaultHttpClientFactory = () => new HttpClient();

/// <summary>
/// Initializes a new instance of the <see cref="JaegerExporterOptions"/> class.
/// </summary>
public JaegerExporterOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

internal JaegerExporterOptions(IConfiguration configuration)
{
if (EnvironmentVariableHelper.LoadString(OTelProtocolEnvVarKey, out string protocolEnvVar))
var protocol = configuration.GetValue<string>(OTelProtocolEnvVarKey, null);
if (!string.IsNullOrEmpty(protocol))
{
if (JaegerExporterProtocolParser.TryParse(protocolEnvVar, out var protocol))
if (JaegerExporterProtocolParser.TryParse(protocol, out var parsedProtocol))
{
this.Protocol = protocol;
this.Protocol = parsedProtocol;
}
else
{
throw new FormatException($"{OTelProtocolEnvVarKey} environment variable has an invalid value: '{protocolEnvVar}'");
throw new FormatException($"{OTelProtocolEnvVarKey} environment variable has an invalid value: '{protocol}'");
}
}

if (EnvironmentVariableHelper.LoadString(OTelAgentHostEnvVarKey, out string agentHostEnvVar))
var agentHost = configuration.GetValue<string>(OTelAgentHostEnvVarKey, null);
if (!string.IsNullOrEmpty(agentHost))
{
this.AgentHost = agentHostEnvVar;
this.AgentHost = agentHost;
}

if (EnvironmentVariableHelper.LoadNumeric(OTelAgentPortEnvVarKey, out int agentPortEnvVar))
var agentPort = configuration.GetValue<string>(OTelAgentPortEnvVarKey, null);
if (!string.IsNullOrEmpty(agentPort))
{
this.AgentPort = agentPortEnvVar;
if (EnvironmentVariableHelper.LoadNumeric(OTelAgentPortEnvVarKey, agentPort, out int parsedAgentPort))
{
this.AgentPort = parsedAgentPort;
}
}

if (EnvironmentVariableHelper.LoadString(OTelEndpointEnvVarKey, out string endpointEnvVar)
&& Uri.TryCreate(endpointEnvVar, UriKind.Absolute, out Uri endpoint))
var endpoint = configuration.GetValue<string>(OTelEndpointEnvVarKey, null);
if (!string.IsNullOrEmpty(endpoint))
{
this.Endpoint = endpoint;
if (Uri.TryCreate(endpoint, UriKind.Absolute, out Uri parsedEndpoint))
{
this.Endpoint = parsedEndpoint;
}
else
{
throw new FormatException($"{OTelEndpointEnvVarKey} environment variable has an invalid value: '{endpoint}'");
}
}
}

/// <summary>
/// Gets or sets the <see cref="JaegerExportProtocol"/> to use when
/// communicating to Jaeger. Default value: <see
/// cref="JaegerExportProtocol.UdpCompactThrift"/>.
/// </summary>
public JaegerExportProtocol Protocol { get; set; } = JaegerExportProtocol.UdpCompactThrift;

/// <summary>
Expand Down Expand Up @@ -129,5 +157,20 @@ public JaegerExporterOptions()
/// </list>
/// </remarks>
public Func<HttpClient> HttpClientFactory { get; set; } = DefaultHttpClientFactory;

internal sealed class JaegerExporterOptionsFactory : IOptionsFactory<JaegerExporterOptions>
{
private readonly IConfiguration configuration;

public JaegerExporterOptionsFactory(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

this.configuration = configuration;
}

public JaegerExporterOptions Create(string name)
=> new(this.configuration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace OpenTelemetry.Exporter.Jaeger.Tests
Expand Down Expand Up @@ -93,6 +95,29 @@ public void JaegerExporterOptions_EnvironmentVariableNames()
Assert.Equal("OTEL_EXPORTER_JAEGER_ENDPOINT", JaegerExporterOptions.OTelEndpointEnvVarKey);
}

[Fact]
public void JaegerExporterOptions_FromConfigurationTest()
{
var values = new Dictionary<string, string>()
{
[JaegerExporterOptions.OTelProtocolEnvVarKey] = "http/thrift.binary",
[JaegerExporterOptions.OTelAgentHostEnvVarKey] = "jaeger-host",
[JaegerExporterOptions.OTelAgentPortEnvVarKey] = "123",
[JaegerExporterOptions.OTelEndpointEnvVarKey] = "http://custom-endpoint:12345",
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(values)
.Build();

var options = new JaegerExporterOptions(configuration);

Assert.Equal("jaeger-host", options.AgentHost);
Assert.Equal(123, options.AgentPort);
Assert.Equal(JaegerExportProtocol.HttpBinaryThrift, options.Protocol);
Assert.Equal(new Uri("http://custom-endpoint:12345"), options.Endpoint);
}

private static void ClearEnvVars()
{
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelProtocolEnvVarKey, null);
Expand Down