Skip to content

Commit

Permalink
[SDK, Jaeger, Zipkin, & Otlp] Support loading envvars for BatchExport…
Browse files Browse the repository at this point in the history
…ActivityProcessorOptions from IConfiguration (#3776)

* Support loading envvars for ExportActivityProcessorOptions & BatchExportActivityProcessorOptions from IConfiguration.

* Update src/OpenTelemetry/CHANGELOG.md

Co-authored-by: Piotr Kiełkowicz <[email protected]>

* Patch CHANGELOG.

* Unit test.

Co-authored-by: Cijo Thomas <[email protected]>
Co-authored-by: Piotr Kiełkowicz <[email protected]>
  • Loading branch information
3 people committed Oct 18, 2022
1 parent 3f53be6 commit 4239992
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ internal JaegerExporterOptions(IConfiguration configuration)
{
this.Endpoint = endpoint;
}

this.BatchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration);
}

/// <summary>
Expand Down Expand Up @@ -114,7 +116,7 @@ internal JaegerExporterOptions(IConfiguration configuration)
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }

/// <summary>
/// Gets or sets the factory function called to create the <see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ internal OtlpExporterOptions(IConfiguration configuration)
Timeout = TimeSpan.FromMilliseconds(this.TimeoutMilliseconds),
};
};

this.BatchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration);
}

/// <summary>
Expand Down Expand Up @@ -142,7 +144,7 @@ public Uri Endpoint
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is Batch.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }

/// <summary>
/// Gets or sets the factory function called to create the <see
Expand Down
4 changes: 3 additions & 1 deletion src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal ZipkinExporterOptions(IConfiguration configuration)
{
this.Endpoint = endpoint;
}

this.BatchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration);
}

/// <summary>
Expand All @@ -81,7 +83,7 @@ internal ZipkinExporterOptions(IConfiguration configuration)
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportActivityProcessorOptions();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }

/// <summary>
/// Gets or sets the factory function called to create the <see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Metrics.MeterProviderBuilder!>! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation<T>(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Metrics.MeterProviderBuilder!>! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation<T>(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Metrics.MeterProviderBuilder!>! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation<T>(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Metrics.MeterProviderBuilder!>! configure) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddInstrumentation<T>(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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;
}
}
23 changes: 18 additions & 5 deletions src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
// limitations under the License.
// </copyright>

#nullable enable

using System;
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace
Expand All @@ -39,26 +42,36 @@ public class BatchExportActivityProcessorOptions : BatchExportProcessorOptions<A

internal const string ScheduledDelayEnvVarKey = "OTEL_BSP_SCHEDULE_DELAY";

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

if (EnvironmentVariableHelper.LoadNumeric(ExporterTimeoutEnvVarKey, out value))
/// <summary>
/// Initializes a new instance of the <see cref="BatchExportActivityProcessorOptions"/> class.
/// </summary>
/// <param name="configuration"><see cref="IConfiguration"/>.</param>
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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal TracerProviderBuilderBase(IServiceCollection services)
{
Guard.ThrowIfNull(services);

services.AddOpenTelemetryProviderBuilderServices();
services.AddOpenTelemetryTracerProviderBuilderServices();
services.TryAddSingleton<TracerProvider>(sp => new TracerProviderSdk(sp, ownsServiceProvider: false));

this.services = services;
Expand All @@ -64,7 +64,7 @@ protected TracerProviderBuilderBase()
{
var services = new ServiceCollection();

services.AddOpenTelemetryProviderBuilderServices();
services.AddOpenTelemetryTracerProviderBuilderServices();

this.services = services;
this.ownsServices = true;
Expand Down
21 changes: 19 additions & 2 deletions src/OpenTelemetry/Trace/ExportActivityProcessorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,30 @@

#nullable enable

using Microsoft.Extensions.Configuration;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace;

/// <summary>
/// Options for configuring either a <see cref="SimpleActivityExportProcessor"/> or <see cref="BatchActivityExportProcessor"/>.
/// </summary>
public class ExportActivityProcessorOptions
{
private BatchExportActivityProcessorOptions? batchExportProcessorOptions;
private BatchExportActivityProcessorOptions batchExportProcessorOptions;

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

internal ExportActivityProcessorOptions(IConfiguration configuration)
{
this.batchExportProcessorOptions = new BatchExportActivityProcessorOptions(configuration);
}

/// <summary>
/// Gets or sets the export processor type to be used. The default value is <see cref="ExportProcessorType.Batch"/>.
Expand All @@ -34,7 +51,7 @@ public class ExportActivityProcessorOptions
/// </summary>
public BatchExportActivityProcessorOptions BatchExportProcessorOptions
{
get => this.batchExportProcessorOptions ??= new();
get => this.batchExportProcessorOptions;
set
{
Guard.ThrowIfNull(value);
Expand Down
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.Trace.Tests
Expand Down Expand Up @@ -59,6 +61,36 @@ public void BatchExportProcessorOptions_EnvironmentVariableOverride()
Assert.Equal(4, options.ScheduledDelayMilliseconds);
}

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

0 comments on commit 4239992

Please sign in to comment.