Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metrics] Clean up repo exporters to use new DI patterns #3648

Merged
merged 8 commits into from
Sep 13, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

using System;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;

Expand All @@ -36,7 +38,7 @@ public static class ConsoleExporterMetricsExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder)
{
return AddConsoleExporter(builder, options => { });
return AddConsoleExporter(builder, configureExporter: null);
}

/// <summary>
Expand All @@ -49,15 +51,18 @@ public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder
{
Guard.ThrowIfNull(builder);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
if (configureExporter != null)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddConsoleExporter(builder, sp.GetOptions<ConsoleExporterOptions>(), sp.GetOptions<MetricReaderOptions>(), configureExporter, null);
});
builder.ConfigureServices(services => services.Configure(configureExporter));
}

return AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), configureExporter, null);
return builder.ConfigureBuilder((sp, builder) =>
{
AddConsoleExporter(
builder,
sp.GetRequiredService<IOptions<ConsoleExporterOptions>>().Value,
sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value);
});
}

/// <summary>
Expand All @@ -70,35 +75,24 @@ public static MeterProviderBuilder AddConsoleExporter(
this MeterProviderBuilder builder,
Action<ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
{
Guard.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(builder);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
return builder.ConfigureBuilder((sp, builder) =>
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddConsoleExporter(builder, sp.GetOptions<ConsoleExporterOptions>(), sp.GetOptions<MetricReaderOptions>(), null, configureExporterAndMetricReader);
});
}
var exporterOptions = sp.GetRequiredService<IOptions<ConsoleExporterOptions>>().Value;
var metricReaderOptions = sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value;

return AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), null, configureExporterAndMetricReader);
configureExporterAndMetricReader?.Invoke(exporterOptions, metricReaderOptions);

AddConsoleExporter(builder, exporterOptions, metricReaderOptions);
});
}

private static MeterProviderBuilder AddConsoleExporter(
MeterProviderBuilder builder,
ConsoleExporterOptions exporterOptions,
MetricReaderOptions metricReaderOptions,
Action<ConsoleExporterOptions> configureExporter,
Action<ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
MetricReaderOptions metricReaderOptions)
{
if (configureExporterAndMetricReader != null)
{
configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions);
}
else
{
configureExporter?.Invoke(exporterOptions);
}

var metricExporter = new ConsoleMetricExporter(exporterOptions);

var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;

Expand All @@ -41,7 +43,7 @@ public static class InMemoryExporterMetricsExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection<Metric> exportedItems)
{
return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null);
return AddInMemoryExporter(builder, exportedItems, configureMetricReader: null);
}

/// <summary>
Expand All @@ -59,15 +61,17 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(exportedItems);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
if (configureMetricReader != null)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddInMemoryExporter(builder, exportedItems, sp.GetOptions<MetricReaderOptions>(), configureMetricReader);
});
builder.ConfigureServices(services => services.Configure(configureMetricReader));
}

return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader);
return builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value;

AddInMemoryExporter(builder, exportedItems, options);
});
}

/// <summary>
Expand All @@ -84,7 +88,7 @@ public static MeterProviderBuilder AddInMemoryExporter(
this MeterProviderBuilder builder,
ICollection<MetricSnapshot> exportedItems)
{
return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null);
return AddInMemoryExporter(builder, exportedItems, configureMetricReader: null);
}

/// <summary>
Expand All @@ -106,25 +110,24 @@ public static MeterProviderBuilder AddInMemoryExporter(
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(exportedItems);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
if (configureMetricReader != null)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddInMemoryExporter(builder, exportedItems, sp.GetOptions<MetricReaderOptions>(), configureMetricReader);
});
builder.ConfigureServices(services => services.Configure(configureMetricReader));
}

return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader);
return builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value;

AddInMemoryExporter(builder, exportedItems, options);
});
}

private static MeterProviderBuilder AddInMemoryExporter(
MeterProviderBuilder builder,
ICollection<Metric> exportedItems,
MetricReaderOptions metricReaderOptions,
Action<MetricReaderOptions> configureMetricReader)
MetricReaderOptions metricReaderOptions)
{
configureMetricReader?.Invoke(metricReaderOptions);

var metricExporter = new InMemoryExporter<Metric>(exportedItems);

var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
Expand All @@ -139,11 +142,8 @@ private static MeterProviderBuilder AddInMemoryExporter(
private static MeterProviderBuilder AddInMemoryExporter(
MeterProviderBuilder builder,
ICollection<MetricSnapshot> exportedItems,
MetricReaderOptions metricReaderOptions,
Action<MetricReaderOptions> configureMetricReader)
MetricReaderOptions metricReaderOptions)
{
configureMetricReader?.Invoke(metricReaderOptions);

var metricExporter = new InMemoryExporter<Metric>(
exportFunc: (in Batch<Metric> metricBatch) => ExportMetricSnapshot(in metricBatch, exportedItems));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagAndValueTransformer.cs" Link="Includes\TagAndValueTransformer.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;

Expand All @@ -32,7 +34,7 @@ public static class OtlpMetricExporterExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder)
{
return AddOtlpExporter(builder, options => { });
return AddOtlpExporter(builder, configureExporter: null);
}

/// <summary>
Expand All @@ -45,15 +47,19 @@ public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder bui
{
Guard.ThrowIfNull(builder);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
if (configureExporter != null)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddOtlpExporter(builder, sp.GetOptions<OtlpExporterOptions>(), sp.GetOptions<MetricReaderOptions>(), configureExporter, null, sp);
});
builder.ConfigureServices(services => services.Configure(configureExporter));
}

return AddOtlpExporter(builder, new OtlpExporterOptions(), new MetricReaderOptions(), configureExporter, null, serviceProvider: null);
return builder.ConfigureBuilder((sp, builder) =>
{
AddOtlpExporter(
builder,
sp.GetRequiredService<IOptions<OtlpExporterOptions>>().Value,
sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value,
sp);
});
}

/// <summary>
Expand All @@ -66,37 +72,26 @@ public static MeterProviderBuilder AddOtlpExporter(
this MeterProviderBuilder builder,
Action<OtlpExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
{
Guard.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(builder);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
return builder.ConfigureBuilder((sp, builder) =>
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddOtlpExporter(builder, sp.GetOptions<OtlpExporterOptions>(), sp.GetOptions<MetricReaderOptions>(), null, configureExporterAndMetricReader, sp);
});
}
var exporterOptions = sp.GetRequiredService<IOptions<OtlpExporterOptions>>().Value;
var metricReaderOptions = sp.GetRequiredService<IOptions<MetricReaderOptions>>().Value;

return AddOtlpExporter(builder, new OtlpExporterOptions(), new MetricReaderOptions(), null, configureExporterAndMetricReader, serviceProvider: null);
configureExporterAndMetricReader?.Invoke(exporterOptions, metricReaderOptions);

AddOtlpExporter(builder, exporterOptions, metricReaderOptions, sp);
});
}

internal static MeterProviderBuilder AddOtlpExporter(
MeterProviderBuilder builder,
OtlpExporterOptions exporterOptions,
MetricReaderOptions metricReaderOptions,
Action<OtlpExporterOptions> configureExporter,
Action<OtlpExporterOptions, MetricReaderOptions> configureExporterAndMetricReader,
IServiceProvider serviceProvider,
Func<BaseExporter<Metric>, BaseExporter<Metric>> configureExporterInstance = null)
{
if (configureExporterAndMetricReader != null)
{
configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions);
}
else
{
configureExporter?.Invoke(exporterOptions);
}

exporterOptions.TryEnableIHttpClientFactoryIntegration(serviceProvider, "OtlpMetricExporter");

BaseExporter<Metric> metricExporter = new OtlpMetricExporter(exporterOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
using OpenTelemetry.Metrics;
Expand Down Expand Up @@ -119,7 +120,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint(
{
if (path == null)
{
var options = app.ApplicationServices.GetOptions<PrometheusExporterOptions>();
var options = app.ApplicationServices.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;

path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
using OpenTelemetry.Metrics;
Expand Down Expand Up @@ -89,7 +90,8 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint(

if (path == null)
{
var options = endpoints.ServiceProvider.GetOptions<PrometheusExporterOptions>();
var options = endpoints.ServiceProvider.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;

path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Exporter.Prometheus;
using OpenTelemetry.Internal;
Expand All @@ -36,22 +38,23 @@ public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuild
{
Guard.ThrowIfNull(builder);

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
if (configure != null)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddPrometheusExporter(builder, sp.GetOptions<PrometheusExporterOptions>(), configure);
});
builder.ConfigureServices(services => services.Configure(configure));
}

return AddPrometheusExporter(builder, new PrometheusExporterOptions(), configure);
return builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptions<PrometheusExporterOptions>>().Value;

AddPrometheusExporter(builder, options);
});
}

private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options, Action<PrometheusExporterOptions> configure = null)
private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options)
{
configure?.Invoke(options);

var exporter = new PrometheusExporter(scrapeResponseCacheDurationMilliseconds: options.ScrapeResponseCacheDurationMilliseconds);

var reader = new BaseExportingMetricReader(exporter)
{
TemporalityPreference = MetricReaderTemporalityPreference.Cumulative,
Expand Down
Loading