diff --git a/src/OpenTelemetry.Exporter.Console/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Console/.publicApi/net462/PublicAPI.Unshipped.txt index 91e9fc8830a..0f6bd0c9e9a 100644 --- a/src/OpenTelemetry.Exporter.Console/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Console/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 91e9fc8830a..0f6bd0c9e9a 100644 --- a/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md index f28b918564d..1b584dd454a 100644 --- a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md @@ -6,6 +6,11 @@ the data if it is disposed. ([#3578](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3578)) +* Added overloads which accept a name to the `MeterProviderBuilder` + `AddConsoleExporter` extension to allow for more fine-grained options + management + ([#3648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3648)) + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs index ed22e66e31c..e8250fadf13 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs @@ -16,6 +16,8 @@ using System; using System.Threading; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Exporter; using OpenTelemetry.Internal; @@ -35,70 +37,94 @@ public static class ConsoleExporterMetricsExtensions /// builder to use. /// The instance of to chain the calls. public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder) - { - return AddConsoleExporter(builder, options => { }); - } + => AddConsoleExporter(builder, name: null, configureExporter: null); /// /// Adds to the . /// /// builder to use. - /// Exporter configuration options. + /// Callback action for configuring . /// The instance of to chain the calls. public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action configureExporter) + => AddConsoleExporter(builder, name: null, configureExporter); + + /// + /// Adds to the . + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddConsoleExporter( + this MeterProviderBuilder builder, + string name, + Action configureExporter) { Guard.ThrowIfNull(builder); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configureExporter != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddConsoleExporter(builder, sp.GetOptions(), sp.GetOptions(), configureExporter, null); - }); + builder.ConfigureServices(services => services.Configure(name, configureExporter)); } - return AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), configureExporter, null); + return builder.ConfigureBuilder((sp, builder) => + { + AddConsoleExporter( + builder, + sp.GetRequiredService>().Get(name), + sp.GetRequiredService>().Get(name)); + }); } /// /// Adds to the . /// /// builder to use. - /// Exporter and configuration options. + /// Callback action for + /// configuring and . /// The instance of to chain the calls. public static MeterProviderBuilder AddConsoleExporter( this MeterProviderBuilder builder, Action configureExporterAndMetricReader) + => AddConsoleExporter(builder, name: null, configureExporterAndMetricReader); + + /// + /// Adds to the . + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for + /// configuring and . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddConsoleExporter( + this MeterProviderBuilder builder, + string name, + Action configureExporterAndMetricReader) { - Guard.ThrowIfNull(builder, nameof(builder)); + Guard.ThrowIfNull(builder); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + return builder.ConfigureBuilder((sp, builder) => { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddConsoleExporter(builder, sp.GetOptions(), sp.GetOptions(), null, configureExporterAndMetricReader); - }); - } + var exporterOptions = sp.GetRequiredService>().Get(name); + var metricReaderOptions = sp.GetRequiredService>().Get(name); - 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 configureExporter, - Action configureExporterAndMetricReader) + MetricReaderOptions metricReaderOptions) { - if (configureExporterAndMetricReader != null) - { - configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions); - } - else - { - configureExporter?.Invoke(exporterOptions); - } - var metricExporter = new ConsoleMetricExporter(exporterOptions); var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader( diff --git a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj index d0f69330b15..e91623caf6b 100644 --- a/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj +++ b/src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj @@ -24,7 +24,6 @@ - diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index fb80e7c3a78..a57d90fc54f 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ override OpenTelemetry.Exporter.InMemoryExporter.Dispose(bool disposing) -> void +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index fb80e7c3a78..a57d90fc54f 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ override OpenTelemetry.Exporter.InMemoryExporter.Dispose(bool disposing) -> void +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index e701088e4d2..744052f14ec 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -7,6 +7,11 @@ disposed. ([#3607](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3607)) +* Added overloads which accept a name to the `MeterProviderBuilder` + `AddInMemoryExporter` extension to allow for more fine-grained options + management + ([#3648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3648)) + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 613067d5e8a..b72e16f0459 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -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; @@ -40,9 +42,23 @@ public static class InMemoryExporterMetricsExtensions /// Collection which will be populated with the exported . /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection exportedItems) - { - return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null); - } + => AddInMemoryExporter(builder, name: null, exportedItems, configureMetricReader: null); + + /// + /// Adds InMemory metric exporter to the . + /// + /// + /// Be aware that may continue to be updated after export. + /// + /// builder to use. + /// Collection which will be populated with the exported . + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddInMemoryExporter( + this MeterProviderBuilder builder, + ICollection exportedItems, + Action configureMetricReader) + => AddInMemoryExporter(builder, name: null, exportedItems, configureMetricReader); /// /// Adds InMemory metric exporter to the . @@ -51,23 +67,32 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder /// Be aware that may continue to be updated after export. /// /// builder to use. + /// Name which is used when retrieving options. /// Collection which will be populated with the exported . - /// configuration options. + /// Callback action for configuring . /// The instance of to chain the calls. - public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection exportedItems, Action configureMetricReader) + public static MeterProviderBuilder AddInMemoryExporter( + this MeterProviderBuilder builder, + string name, + ICollection exportedItems, + Action configureMetricReader) { Guard.ThrowIfNull(builder); Guard.ThrowIfNull(exportedItems); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configureMetricReader != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddInMemoryExporter(builder, exportedItems, sp.GetOptions(), configureMetricReader); - }); + builder.ConfigureServices(services => services.Configure(name, configureMetricReader)); } - return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader); + return builder.ConfigureBuilder((sp, builder) => + { + var options = sp.GetRequiredService>().Get(name); + + AddInMemoryExporter(builder, exportedItems, options); + }); } /// @@ -83,9 +108,7 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, ICollection exportedItems) - { - return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null); - } + => AddInMemoryExporter(builder, name: null, exportedItems, configureMetricReader: null); /// /// Adds InMemory metric exporter to the . @@ -96,35 +119,55 @@ public static MeterProviderBuilder AddInMemoryExporter( /// /// builder to use. /// Collection which will be populated with the exported represented as . - /// configuration options. + /// Callback action for configuring . /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, ICollection exportedItems, Action configureMetricReader) + => AddInMemoryExporter(builder, name: null, exportedItems, configureMetricReader); + + /// + /// Adds InMemory metric exporter to the . + /// The exporter will be setup to export . + /// + /// + /// Use this if you need a copy of that will not be updated after export. + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Collection which will be populated with the exported represented as . + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddInMemoryExporter( + this MeterProviderBuilder builder, + string name, + ICollection exportedItems, + Action configureMetricReader) { Guard.ThrowIfNull(builder); Guard.ThrowIfNull(exportedItems); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configureMetricReader != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddInMemoryExporter(builder, exportedItems, sp.GetOptions(), configureMetricReader); - }); + builder.ConfigureServices(services => services.Configure(name, configureMetricReader)); } - return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader); + return builder.ConfigureBuilder((sp, builder) => + { + var options = sp.GetRequiredService>().Get(name); + + AddInMemoryExporter(builder, exportedItems, options); + }); } private static MeterProviderBuilder AddInMemoryExporter( MeterProviderBuilder builder, ICollection exportedItems, - MetricReaderOptions metricReaderOptions, - Action configureMetricReader) + MetricReaderOptions metricReaderOptions) { - configureMetricReader?.Invoke(metricReaderOptions); - var metricExporter = new InMemoryExporter(exportedItems); var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader( @@ -139,11 +182,8 @@ private static MeterProviderBuilder AddInMemoryExporter( private static MeterProviderBuilder AddInMemoryExporter( MeterProviderBuilder builder, ICollection exportedItems, - MetricReaderOptions metricReaderOptions, - Action configureMetricReader) + MetricReaderOptions metricReaderOptions) { - configureMetricReader?.Invoke(metricReaderOptions); - var metricExporter = new InMemoryExporter( exportFunc: (in Batch metricBatch) => ExportMetricSnapshot(in metricBatch, exportedItems)); diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net462/PublicAPI.Unshipped.txt index e69de29bb2d..6ee083d2bf9 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net462/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net6.0/PublicAPI.Unshipped.txt index e69de29bb2d..6ee083d2bf9 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index e69de29bb2d..6ee083d2bf9 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index e69de29bb2d..6ee083d2bf9 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.OtlpMetricExporterExtensions.AddOtlpExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md index bab3a72cbf3..9af0873f3c1 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Added overloads which accept a name to the `MeterProviderBuilder` + `AddOtlpExporter` extension to allow for more fine-grained options management + ([#3648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3648)) + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj index 4c584cbbe99..529e72b586f 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj @@ -39,7 +39,6 @@ - diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs index f3c1547f181..ee7f306ca0f 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs @@ -15,6 +15,8 @@ // using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Exporter; using OpenTelemetry.Internal; @@ -31,72 +33,97 @@ public static class OtlpMetricExporterExtensions /// builder to use. /// The instance of to chain the calls. public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder) - { - return AddOtlpExporter(builder, options => { }); - } + => AddOtlpExporter(builder, name: null, configureExporter: null); /// /// Adds to the . /// /// builder to use. - /// Exporter configuration options. + /// Callback action for configuring . /// The instance of to chain the calls. public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action configureExporter) + => AddOtlpExporter(builder, name: null, configureExporter); + + /// + /// Adds to the . + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddOtlpExporter( + this MeterProviderBuilder builder, + string name, + Action configureExporter) { Guard.ThrowIfNull(builder); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configureExporter != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddOtlpExporter(builder, sp.GetOptions(), sp.GetOptions(), configureExporter, null, sp); - }); + builder.ConfigureServices(services => services.Configure(name, configureExporter)); } - return AddOtlpExporter(builder, new OtlpExporterOptions(), new MetricReaderOptions(), configureExporter, null, serviceProvider: null); + return builder.ConfigureBuilder((sp, builder) => + { + AddOtlpExporter( + builder, + sp.GetRequiredService>().Get(name), + sp.GetRequiredService>().Get(name), + sp); + }); } /// /// Adds to the . /// /// builder to use. - /// Exporter and configuration options. + /// Callback action for + /// configuring and . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddOtlpExporter( + this MeterProviderBuilder builder, + Action configureExporterAndMetricReader) + => AddOtlpExporter(builder, name: null, configureExporterAndMetricReader); + + /// + /// Adds to the . + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for + /// configuring and . /// The instance of to chain the calls. public static MeterProviderBuilder AddOtlpExporter( this MeterProviderBuilder builder, + string name, Action configureExporterAndMetricReader) { - Guard.ThrowIfNull(builder, nameof(builder)); + Guard.ThrowIfNull(builder); + + name ??= Options.DefaultName; - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + return builder.ConfigureBuilder((sp, builder) => { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddOtlpExporter(builder, sp.GetOptions(), sp.GetOptions(), null, configureExporterAndMetricReader, sp); - }); - } + var exporterOptions = sp.GetRequiredService>().Get(name); + var metricReaderOptions = sp.GetRequiredService>().Get(name); + + configureExporterAndMetricReader?.Invoke(exporterOptions, metricReaderOptions); - return AddOtlpExporter(builder, new OtlpExporterOptions(), new MetricReaderOptions(), null, configureExporterAndMetricReader, serviceProvider: null); + AddOtlpExporter(builder, exporterOptions, metricReaderOptions, sp); + }); } internal static MeterProviderBuilder AddOtlpExporter( MeterProviderBuilder builder, OtlpExporterOptions exporterOptions, MetricReaderOptions metricReaderOptions, - Action configureExporter, - Action configureExporterAndMetricReader, IServiceProvider serviceProvider, Func, BaseExporter> configureExporterInstance = null) { - if (configureExporterAndMetricReader != null) - { - configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions); - } - else - { - configureExporter?.Invoke(exporterOptions); - } - exporterOptions.TryEnableIHttpClientFactoryIntegration(serviceProvider, "OtlpMetricExporter"); BaseExporter metricExporter = new OtlpMetricExporter(exporterOptions); diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt index 46dbf03b1ed..2fc54a64cb8 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -14,4 +14,6 @@ static Microsoft.AspNetCore.Builder.PrometheusExporterApplicationBuilderExtensio static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path = null, OpenTelemetry.Metrics.MeterProvider meterProvider = null, System.Action configureBranchedPipeline = null) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder static Microsoft.AspNetCore.Builder.PrometheusExporterEndpointRouteBuilderExtensions.MapPrometheusScrapingEndpoint(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string path) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder -static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusExporterMeterProviderBuilderExtensions.AddPrometheusExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md index acd5d5ba488..151ee398408 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md @@ -6,6 +6,11 @@ instead of 200, when no metrics are collected ([#3643](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3643)) +* Added overloads which accept a name to the `MeterProviderBuilder` + `AddPrometheusExporter` extension to allow for more fine-grained options + management + ([#3648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3648)) + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs index 5d8ccce7149..408a1b59b61 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterApplicationBuilderExtensions.cs @@ -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; @@ -119,7 +120,7 @@ public static IApplicationBuilder UseOpenTelemetryPrometheusScrapingEndpoint( { if (path == null) { - var options = app.ApplicationServices.GetOptions(); + var options = app.ApplicationServices.GetRequiredService>().Value; path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath; } diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs index 77a31b87e6a..732492e0cbf 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterEndpointRouteBuilderExtensions.cs @@ -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; @@ -89,7 +90,8 @@ public static IEndpointConventionBuilder MapPrometheusScrapingEndpoint( if (path == null) { - var options = endpoints.ServiceProvider.GetOptions(); + var options = endpoints.ServiceProvider.GetRequiredService>().Value; + path = options.ScrapeEndpointPath ?? PrometheusExporterOptions.DefaultScrapeEndpointPath; } diff --git a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs index 1876433b946..daacd2c66b8 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -15,6 +15,8 @@ // using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Exporter; using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; @@ -30,28 +32,54 @@ public static class PrometheusExporterMeterProviderBuilderExtensions /// Adds to the . /// /// builder to use. - /// Exporter configuration options. /// The instance of to chain the calls. - public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder, Action configure = null) + public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder) + => AddPrometheusExporter(builder, name: null, configure: null); + + /// + /// Adds to the . + /// + /// builder to use. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddPrometheusExporter( + this MeterProviderBuilder builder, + Action configure) + => AddPrometheusExporter(builder, name: null, configure); + + /// + /// Adds to the . + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddPrometheusExporter( + this MeterProviderBuilder builder, + string name, + Action configure) { Guard.ThrowIfNull(builder); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configure != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddPrometheusExporter(builder, sp.GetOptions(), configure); - }); + builder.ConfigureServices(services => services.Configure(name, configure)); } - return AddPrometheusExporter(builder, new PrometheusExporterOptions(), configure); + return builder.ConfigureBuilder((sp, builder) => + { + var options = sp.GetRequiredService>().Get(name); + + AddPrometheusExporter(builder, options); + }); } - private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options, Action 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, diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt index 01bea3492fa..9bc2e72461d 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/net462/PublicAPI.Unshipped.txt @@ -5,4 +5,6 @@ OpenTelemetry.Exporter.PrometheusHttpListenerOptions.PrometheusHttpListenerOptio OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.get -> string OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> void OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions -static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 48dbc8b011e..cc20c38b13e 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -5,4 +5,6 @@ OpenTelemetry.Exporter.PrometheusHttpListenerOptions.ScrapeEndpointPath.set -> v OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.get -> System.Collections.Generic.IReadOnlyCollection OpenTelemetry.Exporter.PrometheusHttpListenerOptions.UriPrefixes.set -> void OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions -static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.PrometheusHttpListenerMeterProviderBuilderExtensions.AddPrometheusHttpListener(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md index eb1859a39d0..f46954288a1 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md @@ -6,6 +6,11 @@ instead of 200, when no metrics are collected ([#3643](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3643)) +* Added overloads which accept a name to the `MeterProviderBuilder` + `AddPrometheusHttpListener` extension to allow for more fine-grained options + management + ([#3648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3648)) + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs index 41011f95ad2..605aaff6df4 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs @@ -15,6 +15,8 @@ // using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Exporter; using OpenTelemetry.Exporter.Prometheus; using OpenTelemetry.Internal; @@ -30,32 +32,54 @@ public static class PrometheusHttpListenerMeterProviderBuilderExtensions /// Adds PrometheusHttpListener to MeterProviderBuilder. /// /// builder to use. - /// PrometheusHttpListenerOptions options. + /// The instance of to chain calls. + public static MeterProviderBuilder AddPrometheusHttpListener(this MeterProviderBuilder builder) + => AddPrometheusHttpListener(builder, name: null, configure: null); + + /// + /// Adds PrometheusHttpListener to MeterProviderBuilder. + /// + /// builder to use. + /// Callback action for configuring . /// The instance of to chain calls. public static MeterProviderBuilder AddPrometheusHttpListener( this MeterProviderBuilder builder, - Action configure = null) + Action configure) + => AddPrometheusHttpListener(builder, name: null, configure); + + /// + /// Adds PrometheusHttpListener to MeterProviderBuilder. + /// + /// builder to use. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain calls. + public static MeterProviderBuilder AddPrometheusHttpListener( + this MeterProviderBuilder builder, + string name, + Action configure) { Guard.ThrowIfNull(builder); - if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + name ??= Options.DefaultName; + + if (configure != null) { - return deferredMeterProviderBuilder.Configure((sp, builder) => - { - AddPrometheusHttpListener(builder, sp.GetOptions(), configure); - }); + builder.ConfigureServices(services => services.Configure(name, configure)); } - return AddPrometheusHttpListener(builder, new PrometheusHttpListenerOptions(), configure); + return builder.ConfigureBuilder((sp, builder) => + { + var options = sp.GetRequiredService>().Get(name); + + AddPrometheusHttpListener(builder, options); + }); } private static MeterProviderBuilder AddPrometheusHttpListener( MeterProviderBuilder builder, - PrometheusHttpListenerOptions options, - Action configure = null) + PrometheusHttpListenerOptions options) { - configure?.Invoke(options); - var exporter = new PrometheusExporter(); var reader = new BaseExportingMetricReader(exporter) diff --git a/src/OpenTelemetry/Internal/ServiceProviderExtensions.cs b/src/OpenTelemetry/Internal/ServiceProviderExtensions.cs deleted file mode 100644 index e594d5034a1..00000000000 --- a/src/OpenTelemetry/Internal/ServiceProviderExtensions.cs +++ /dev/null @@ -1,47 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER -using Microsoft.Extensions.Options; -#endif - -namespace System -{ - /// - /// Extension methods for OpenTelemetry dependency injection support. - /// - internal static class ServiceProviderExtensions - { - /// - /// Get options from the supplied . - /// - /// Options type. - /// . - /// Options instance. - public static T GetOptions(this IServiceProvider serviceProvider) - where T : class, new() - { -#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER - IOptions options = (IOptions)serviceProvider.GetService(typeof(IOptions)); - - // Note: options could be null if user never invoked services.AddOptions(). - return options?.Value ?? new T(); -#else - return new T(); -#endif - } - } -} diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs index b3eca6b5d0c..85bae563da6 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs @@ -162,8 +162,6 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp builder, exporterOptions, readerOptions, - configureExporter: null, - configureExporterAndMetricReader: null, serviceProvider: null, configureExporterInstance: otlpExporter => { diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs index 8553b419dfa..e501446e595 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMetricsExporterTests.cs @@ -79,6 +79,33 @@ void CheckMetricReaderDefaults() } } + [Fact] + public void TestAddOtlpExporter_NamedOptions() + { + int defaultExporterOptionsConfigureOptionsInvocations = 0; + int namedExporterOptionsConfigureOptionsInvocations = 0; + + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .ConfigureServices(services => + { + services.Configure(o => defaultExporterOptionsConfigureOptionsInvocations++); + services.Configure(o => defaultExporterOptionsConfigureOptionsInvocations++); + + services.Configure("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++); + services.Configure("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++); + + services.Configure("Exporter3", o => namedExporterOptionsConfigureOptionsInvocations++); + services.Configure("Exporter3", o => namedExporterOptionsConfigureOptionsInvocations++); + }) + .AddOtlpExporter() + .AddOtlpExporter("Exporter2", o => { }) + .AddOtlpExporter("Exporter3", (eo, ro) => { }) + .Build(); + + Assert.Equal(2, defaultExporterOptionsConfigureOptionsInvocations); + Assert.Equal(4, namedExporterOptionsConfigureOptionsInvocations); + } + [Fact] public void UserHttpFactoryCalled() { diff --git a/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMeterProviderBuilderExtensionsTests.cs b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMeterProviderBuilderExtensionsTests.cs new file mode 100644 index 00000000000..f4212ad5d5b --- /dev/null +++ b/test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMeterProviderBuilderExtensionsTests.cs @@ -0,0 +1,45 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Microsoft.Extensions.DependencyInjection; +using OpenTelemetry.Metrics; +using Xunit; + +namespace OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests; + +public sealed class PrometheusExporterMeterProviderBuilderExtensionsTests +{ + [Fact] + public void TestAddPrometheusExporter_NamedOptions() + { + int defaultExporterOptionsConfigureOptionsInvocations = 0; + int namedExporterOptionsConfigureOptionsInvocations = 0; + + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .ConfigureServices(services => + { + services.Configure(o => defaultExporterOptionsConfigureOptionsInvocations++); + + services.Configure("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++); + }) + .AddPrometheusExporter() + .AddPrometheusExporter("Exporter2", o => { }) + .Build(); + + Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations); + Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations); + } +} diff --git a/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerMeterProviderBuilderExtensionsTests.cs b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerMeterProviderBuilderExtensionsTests.cs new file mode 100644 index 00000000000..eb7832b4856 --- /dev/null +++ b/test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerMeterProviderBuilderExtensionsTests.cs @@ -0,0 +1,45 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Microsoft.Extensions.DependencyInjection; +using OpenTelemetry.Metrics; +using Xunit; + +namespace OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests; + +public sealed class PrometheusHttpListenerMeterProviderBuilderExtensionsTests +{ + [Fact] + public void TestAddPrometheusHttpListener_NamedOptions() + { + int defaultExporterOptionsConfigureOptionsInvocations = 0; + int namedExporterOptionsConfigureOptionsInvocations = 0; + + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .ConfigureServices(services => + { + services.Configure(o => defaultExporterOptionsConfigureOptionsInvocations++); + + services.Configure("Exporter2", o => namedExporterOptionsConfigureOptionsInvocations++); + }) + .AddPrometheusHttpListener() + .AddPrometheusHttpListener("Exporter2", o => { }) + .Build(); + + Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations); + Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations); + } +} diff --git a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs index 142ed9371f9..f6604a44902 100644 --- a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Diagnostics.Metrics; +using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Tests; using Xunit; @@ -47,13 +48,27 @@ public void SdkSupportsMultipleReaders(MetricReaderTemporalityPreference aggrega long GetSum() => valuesSum[indexSum++]; var observableCounter = meter.CreateObservableCounter("obs-counter", () => GetSum()); + bool defaultNamedOptionsConfigureCalled = false; + bool namedOptionsConfigureCalled = false; + var meterProviderBuilder = Sdk.CreateMeterProviderBuilder() + .ConfigureServices(services => + { + services.Configure(o => + { + defaultNamedOptionsConfigureCalled = true; + }); + services.Configure("Exporter2", o => + { + namedOptionsConfigureCalled = true; + }); + }) .AddMeter(meter.Name) .AddInMemoryExporter(exportedItems1, metricReaderOptions => { metricReaderOptions.TemporalityPreference = MetricReaderTemporalityPreference.Delta; }) - .AddInMemoryExporter(exportedItems2, metricReaderOptions => + .AddInMemoryExporter("Exporter2", exportedItems2, metricReaderOptions => { metricReaderOptions.TemporalityPreference = aggregationTemporality; }); @@ -67,6 +82,9 @@ public void SdkSupportsMultipleReaders(MetricReaderTemporalityPreference aggrega using var meterProvider = meterProviderBuilder.Build(); + Assert.True(defaultNamedOptionsConfigureCalled); + Assert.True(namedOptionsConfigureCalled); + counter.Add(10, new KeyValuePair("key", "value")); meterProvider.ForceFlush();