From 644ba5bc14eac8919032cb27a958d1fa919a8b37 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Wed, 23 Feb 2022 09:19:45 -0800 Subject: [PATCH] Remove metric related options from ConsoleExporterOptions (#2929) --- examples/AspNetCore/Startup.cs | 8 +- examples/Console/TestMetrics.cs | 10 ++- .../.publicApi/net461/PublicAPI.Unshipped.txt | 10 +-- .../netstandard2.0/PublicAPI.Unshipped.txt | 10 +-- .../CHANGELOG.md | 5 ++ .../ConsoleExporterMetricsExtensions.cs | 87 ++++++++++++++++--- .../ConsoleExporterOptions.cs | 18 ---- .../CHANGELOG.md | 5 ++ 8 files changed, 102 insertions(+), 51 deletions(-) diff --git a/examples/AspNetCore/Startup.cs b/examples/AspNetCore/Startup.cs index 51c485b1303..88199d052d7 100644 --- a/examples/AspNetCore/Startup.cs +++ b/examples/AspNetCore/Startup.cs @@ -134,12 +134,14 @@ public void ConfigureServices(IServiceCollection services) builder.AddOtlpExporter(); break; default: - builder.AddConsoleExporter(options => + builder.AddConsoleExporter((exporterOptions, metricReaderOptions) => { + exporterOptions.Targets = ConsoleExporterOutputTargets.Console; + // The ConsoleMetricExporter defaults to a manual collect cycle. // This configuration causes metrics to be exported to stdout on a 10s interval. - options.MetricReaderType = MetricReaderType.Periodic; - options.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 10000; + metricReaderOptions.MetricReaderType = MetricReaderType.Periodic; + metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 10000; }); break; } diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 3e20efcc541..15abffbded4 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -79,11 +79,13 @@ internal static object Run(MetricsOptions options) else { providerBuilder - .AddConsoleExporter(o => + .AddConsoleExporter((exporterOptions, metricReaderOptions) => { - o.MetricReaderType = MetricReaderType.Periodic; - o.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds; - o.AggregationTemporality = options.IsDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative; + exporterOptions.Targets = ConsoleExporterOutputTargets.Console; + + metricReaderOptions.MetricReaderType = MetricReaderType.Periodic; + metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds; + metricReaderOptions.Temporality = options.IsDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative; }); } diff --git a/src/OpenTelemetry.Exporter.Console/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Console/.publicApi/net461/PublicAPI.Unshipped.txt index 1e1b5bf2eb4..157653feeb4 100644 --- a/src/OpenTelemetry.Exporter.Console/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Console/.publicApi/net461/PublicAPI.Unshipped.txt @@ -1,11 +1,7 @@ -OpenTelemetry.Exporter.ConsoleExporterOptions.AggregationTemporality.get -> OpenTelemetry.Metrics.AggregationTemporality -OpenTelemetry.Exporter.ConsoleExporterOptions.AggregationTemporality.set -> void -OpenTelemetry.Exporter.ConsoleExporterOptions.MetricReaderType.get -> OpenTelemetry.Metrics.MetricReaderType -OpenTelemetry.Exporter.ConsoleExporterOptions.MetricReaderType.set -> void -OpenTelemetry.Exporter.ConsoleExporterOptions.PeriodicExportingMetricReaderOptions.get -> OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions -OpenTelemetry.Exporter.ConsoleExporterOptions.PeriodicExportingMetricReaderOptions.set -> void OpenTelemetry.Exporter.ConsoleMetricExporter OpenTelemetry.Exporter.ConsoleMetricExporter.ConsoleMetricExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions override OpenTelemetry.Exporter.ConsoleMetricExporter.Export(in OpenTelemetry.Batch batch) -> OpenTelemetry.ExportResult -static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, 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 1e1b5bf2eb4..157653feeb4 100644 --- a/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Console/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,11 +1,7 @@ -OpenTelemetry.Exporter.ConsoleExporterOptions.AggregationTemporality.get -> OpenTelemetry.Metrics.AggregationTemporality -OpenTelemetry.Exporter.ConsoleExporterOptions.AggregationTemporality.set -> void -OpenTelemetry.Exporter.ConsoleExporterOptions.MetricReaderType.get -> OpenTelemetry.Metrics.MetricReaderType -OpenTelemetry.Exporter.ConsoleExporterOptions.MetricReaderType.set -> void -OpenTelemetry.Exporter.ConsoleExporterOptions.PeriodicExportingMetricReaderOptions.get -> OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions -OpenTelemetry.Exporter.ConsoleExporterOptions.PeriodicExportingMetricReaderOptions.set -> void OpenTelemetry.Exporter.ConsoleMetricExporter OpenTelemetry.Exporter.ConsoleMetricExporter.ConsoleMetricExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions override OpenTelemetry.Exporter.ConsoleMetricExporter.Export(in OpenTelemetry.Batch batch) -> OpenTelemetry.ExportResult -static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md index 69e9e9aa40f..cffe9d5dfd6 100644 --- a/src/OpenTelemetry.Exporter.Console/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Console/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* Removes metric related configuration options from `ConsoleExporterOptions`. + `MetricReaderType`, `PeriodicExporterMetricReaderOptions`, and `Temporality` + are now configurable via the `MetricReaderOptions`. + ([#2929](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2929)) + ## 1.2.0-rc2 Released 2022-Feb-02 diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs index e2cfba27fe0..0c35b81bdea 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs @@ -20,31 +20,94 @@ namespace OpenTelemetry.Metrics { + /// + /// Extension methods to simplify registering of the Console exporter. + /// public static class ConsoleExporterMetricsExtensions { /// - /// Adds Console exporter to the TracerProvider. + /// Adds to the using default options. /// /// builder to use. - /// Exporter configuration options. /// The instance of to chain the calls. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")] - public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action configure = null) + public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder) + { + return AddConsoleExporter(builder, options => { }); + } + + /// + /// Adds to the . + /// + /// builder to use. + /// Exporter configuration options. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action configureExporter) { Guard.ThrowIfNull(builder); - var options = new ConsoleExporterOptions(); - configure?.Invoke(options); + if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + { + return deferredMeterProviderBuilder.Configure((sp, builder) => + { + AddConsoleExporter(builder, sp.GetOptions(), sp.GetOptions(), configureExporter, null); + }); + } + + return AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), configureExporter, null); + } + + /// + /// Adds to the . + /// + /// builder to use. + /// Exporter and configuration options. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddConsoleExporter( + this MeterProviderBuilder builder, + Action configureExporterAndMetricReader) + { + Guard.ThrowIfNull(builder, nameof(builder)); + + if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) + { + return deferredMeterProviderBuilder.Configure((sp, builder) => + { + AddConsoleExporter(builder, sp.GetOptions(), sp.GetOptions(), null, configureExporterAndMetricReader); + }); + } + + return AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), null, configureExporterAndMetricReader); + } + + private static MeterProviderBuilder AddConsoleExporter( + MeterProviderBuilder builder, + ConsoleExporterOptions exporterOptions, + MetricReaderOptions metricReaderOptions, + Action configureExporter, + Action configureExporterAndMetricReader) + { + if (configureExporterAndMetricReader != null) + { + configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions); + } + else + { + configureExporter?.Invoke(exporterOptions); + } - var exporter = new ConsoleMetricExporter(options); + var metricExporter = new ConsoleMetricExporter(exporterOptions); - var reader = options.MetricReaderType == MetricReaderType.Manual - ? new BaseExportingMetricReader(exporter) - : new PeriodicExportingMetricReader(exporter, options.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds); + if (metricReaderOptions.MetricReaderType == (MetricReaderType)(-1)) + { + metricReaderOptions.MetricReaderType = MetricReaderType.Manual; + } - reader.Temporality = options.AggregationTemporality; + var metricReader = metricReaderOptions.MetricReaderType == MetricReaderType.Manual + ? new BaseExportingMetricReader(metricExporter) + : new PeriodicExportingMetricReader(metricExporter, metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds); - return builder.AddReader(reader); + metricReader.Temporality = metricReaderOptions.Temporality; + return builder.AddReader(metricReader); } } } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterOptions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterOptions.cs index 93c7e3422c5..a7183ea489f 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterOptions.cs @@ -14,8 +14,6 @@ // limitations under the License. // -using OpenTelemetry.Metrics; - namespace OpenTelemetry.Exporter { public class ConsoleExporterOptions @@ -24,21 +22,5 @@ public class ConsoleExporterOptions /// Gets or sets the output targets for the console exporter. /// public ConsoleExporterOutputTargets Targets { get; set; } = ConsoleExporterOutputTargets.Console; - - /// - /// Gets or sets the to use. Defaults to MetricReaderType.Manual. - /// - public MetricReaderType MetricReaderType { get; set; } = MetricReaderType.Manual; - - /// - /// Gets or sets the options. Ignored unless MetricReaderType is Periodic. - /// - public PeriodicExportingMetricReaderOptions PeriodicExportingMetricReaderOptions { get; set; } = new PeriodicExportingMetricReaderOptions(); - - /// - /// Gets or sets the AggregationTemporality used for Histogram - /// and Sum metrics. - /// - public AggregationTemporality AggregationTemporality { get; set; } = AggregationTemporality.Delta; } } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md index da8dedcd2ad..e8ddd8a3c72 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md @@ -9,6 +9,11 @@ The default value is `http://localhost:4318`. ([#2868](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2868)) +* Removes metric related configuration options from `OtlpExporterOptions`. + `MetricReaderType`, `PeriodicExporterMetricReaderOptions`, and `Temporality` + are now configurable via the `MetricReaderOptions`. + ([#2717](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2717)) + ## 1.2.0-rc2 Released 2022-Feb-02