From 6060936b67235f9be488ddc364df9a0a3bcd7912 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Tue, 10 May 2022 12:21:38 -0700 Subject: [PATCH 01/13] initial commit. Metric Copy. --- .../InMemoryExporter.cs | 14 +++- .../InMemoryExporterMetricsExtensions.cs | 80 +++++++++++++++++-- .../MetricCopy.cs | 58 ++++++++++++++ .../OpenTelemetry.Exporter.InMemory.csproj | 2 - src/OpenTelemetry/AssemblyInfo.cs | 1 + .../Metrics/InMemoryExporterTests.cs | 12 +-- 6 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index d76995f8fb6..27e6f4d9644 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -14,6 +14,7 @@ // limitations under the License. // +using System; using System.Collections.Generic; namespace OpenTelemetry.Exporter @@ -22,13 +23,24 @@ public class InMemoryExporter : BaseExporter where T : class { private readonly ICollection exportedItems; + private readonly ExportDelegate onExport; public InMemoryExporter(ICollection exportedItems) { this.exportedItems = exportedItems; + this.onExport = this.DefaultExport; } - public override ExportResult Export(in Batch batch) + public InMemoryExporter(Func, ExportResult> exportFunc) + { + this.onExport = (in Batch batch) => exportFunc(batch); + } + + private delegate ExportResult ExportDelegate(in Batch batch); + + public override ExportResult Export(in Batch batch) => this.onExport(batch); + + private ExportResult DefaultExport(in Batch batch) { if (this.exportedItems == null) { diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 9809b09552b..03b62fb30b7 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -33,10 +33,28 @@ public static class InMemoryExporterMetricsExtensions /// /// Adds InMemory metric exporter to the using default options. /// + /// + /// Be aware that may continue to be updated after export. + /// /// builder to use. - /// Collection which will be populated with the exported MetricItem. + /// 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); + } + + /// + /// 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 . + /// configuration options. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection exportedItems, Action configureMetricReader) { Guard.ThrowIfNull(builder); Guard.ThrowIfNull(exportedItems); @@ -45,21 +63,45 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder { return deferredMeterProviderBuilder.Configure((sp, builder) => { - AddInMemoryExporter(builder, exportedItems, sp.GetOptions(), null); + AddInMemoryExporter(builder, exportedItems, sp.GetOptions(), configureMetricReader); }); } - return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), null); + return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader); } /// /// Adds InMemory metric exporter to the using default options. + /// 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. + /// Collection which will be populated with the exported represented as . + /// The instance of to chain the calls. + public static MeterProviderBuilder AddInMemoryExporter( + this MeterProviderBuilder builder, + ICollection exportedItems) + { + return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null); + } + + /// + /// 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. - /// Collection which will be populated with the exported MetricItem. + /// Collection which will be populated with the exported represented as . /// configuration options. /// 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, + ICollection exportedItems, + Action configureMetricReader) { Guard.ThrowIfNull(builder); Guard.ThrowIfNull(exportedItems); @@ -93,5 +135,33 @@ private static MeterProviderBuilder AddInMemoryExporter( return builder.AddReader(metricReader); } + + private static MeterProviderBuilder AddInMemoryExporter( + MeterProviderBuilder builder, + ICollection exportedItems, + MetricReaderOptions metricReaderOptions, + Action configureMetricReader) + { + configureMetricReader?.Invoke(metricReaderOptions); + + var metricExporter = new InMemoryExporter( + exportFunc: metricBatch => + { + foreach (var metric in metricBatch) + { + exportedItems.Add(new MetricCopy(metric)); + } + + return ExportResult.Success; + }); + + var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader( + metricExporter, + metricReaderOptions, + DefaultExportIntervalMilliseconds, + DefaultExportTimeoutMilliseconds); + + return builder.AddReader(metricReader); + } } } diff --git a/src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs b/src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs new file mode 100644 index 00000000000..378af2a4788 --- /dev/null +++ b/src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs @@ -0,0 +1,58 @@ +// +// 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 System.Collections.Generic; + +namespace OpenTelemetry.Metrics +{ + /// + /// This class represents a selective copy of . + /// This contains the minimum fields and properties needed for most + /// unit testing scenarios. + /// + public class MetricCopy + { + private readonly MetricStreamIdentity instrumentIdentity; + + public MetricCopy(Metric metric) + { + this.instrumentIdentity = metric.InstrumentIdentity; + this.MetricType = metric.MetricType; + + List metricPoints = new(); + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) + { + metricPoints.Add(metricPoint.Copy()); + } + + this.MetricPoints = metricPoints; + } + + public string Name => this.instrumentIdentity.InstrumentName; + + public string Description => this.instrumentIdentity.Description; + + public string Unit => this.instrumentIdentity.Unit; + + public string MeterName => this.instrumentIdentity.MeterName; + + public MetricType MetricType { get; } + + public string MeterVersion => this.instrumentIdentity.MeterVersion; + + public IReadOnlyList MetricPoints { get; } + } +} diff --git a/src/OpenTelemetry.Exporter.InMemory/OpenTelemetry.Exporter.InMemory.csproj b/src/OpenTelemetry.Exporter.InMemory/OpenTelemetry.Exporter.InMemory.csproj index bdb3c634cd8..546d54e153f 100644 --- a/src/OpenTelemetry.Exporter.InMemory/OpenTelemetry.Exporter.InMemory.csproj +++ b/src/OpenTelemetry.Exporter.InMemory/OpenTelemetry.Exporter.InMemory.csproj @@ -27,8 +27,6 @@ - - diff --git a/src/OpenTelemetry/AssemblyInfo.cs b/src/OpenTelemetry/AssemblyInfo.cs index 5b17a4357a6..545b35a13e8 100644 --- a/src/OpenTelemetry/AssemblyInfo.cs +++ b/src/OpenTelemetry/AssemblyInfo.cs @@ -17,6 +17,7 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)] +[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.InMemory" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Exporter.Prometheus.Tests" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)] diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index c5337ef49a6..c2e84858371 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -16,17 +16,19 @@ using System.Collections.Generic; using System.Diagnostics.Metrics; + using OpenTelemetry.Tests; + using Xunit; namespace OpenTelemetry.Metrics.Tests { public class InMemoryExporterTests { - [Fact(Skip = "To be run after https://github.com/open-telemetry/opentelemetry-dotnet/issues/2361 is fixed")] + [Fact] public void InMemoryExporterShouldDeepCopyMetricPoints() { - var exportedItems = new List(); + var exportedItems = new List(); using var meter = new Meter(Utils.GetCurrentMethodName()); using var meterProvider = Sdk.CreateMeterProviderBuilder() @@ -45,9 +47,9 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() meterProvider.ForceFlush(); var metric = exportedItems[0]; // Only one Metric object is added to the collection at this point - var metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator(); + var metricPointsEnumerator = metric.MetricPoints.GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric - ref readonly var metricPointForFirstExport = ref metricPointsEnumerator.Current; + var metricPointForFirstExport = metricPointsEnumerator.Current; Assert.Equal(10, metricPointForFirstExport.GetSumLong()); // Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") @@ -56,7 +58,7 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() meterProvider.ForceFlush(); metric = exportedItems[1]; // Second Metric object is added to the collection at this point - metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator(); + metricPointsEnumerator = metric.MetricPoints.GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric var metricPointForSecondExport = metricPointsEnumerator.Current; Assert.Equal(25, metricPointForSecondExport.GetSumLong()); From 8b011a3a1c176e046878977eb7847178e39c25b8 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Tue, 10 May 2022 12:36:03 -0700 Subject: [PATCH 02/13] test fix --- .../Trace/BatchExportActivityProcessorTest.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs index 33c6579d11c..67e24fa18fc 100644 --- a/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs @@ -190,9 +190,8 @@ public void CheckExportForRecordingButNotSampledActivity() [Fact] public void CheckExportDrainsBatchOnFailure() { - using var exporter = new InMemoryExporter(null); using var processor = new BatchActivityExportProcessor( - exporter, + exporter: new FailureExporter(), maxQueueSize: 3, maxExportBatchSize: 3); @@ -208,5 +207,11 @@ public void CheckExportDrainsBatchOnFailure() Assert.Equal(3, processor.ProcessedCount); // Verify batch was drained even though nothing was exported. } + + private class FailureExporter : BaseExporter + where T : class + { + public override ExportResult Export(in Batch batch) => ExportResult.Failure; + } } } From 375f910a9ae864a72e53f84b50a3ac0119c2f3ed Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Tue, 10 May 2022 12:39:05 -0700 Subject: [PATCH 03/13] public api --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 14 +++++++++++++- .../netstandard2.0/PublicAPI.Unshipped.txt | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index ca21920895c..d6bfeccb593 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1,3 +1,15 @@ +OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions +OpenTelemetry.Metrics.MetricCopy +OpenTelemetry.Metrics.MetricCopy.Description.get -> string +OpenTelemetry.Metrics.MetricCopy.MeterName.get -> string +OpenTelemetry.Metrics.MetricCopy.MeterVersion.get -> string +OpenTelemetry.Metrics.MetricCopy.MetricCopy(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.MetricCopy.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.MetricCopy.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.MetricCopy.Name.get -> string +OpenTelemetry.Metrics.MetricCopy.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file 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 ca21920895c..d6bfeccb593 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,3 +1,15 @@ +OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions +OpenTelemetry.Metrics.MetricCopy +OpenTelemetry.Metrics.MetricCopy.Description.get -> string +OpenTelemetry.Metrics.MetricCopy.MeterName.get -> string +OpenTelemetry.Metrics.MetricCopy.MeterVersion.get -> string +OpenTelemetry.Metrics.MetricCopy.MetricCopy(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.MetricCopy.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.MetricCopy.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.MetricCopy.Name.get -> string +OpenTelemetry.Metrics.MetricCopy.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file From 868d8def6b84802e89c11add20a92c48ab6b733e Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Wed, 11 May 2022 11:15:25 -0700 Subject: [PATCH 04/13] rename MetricCopy to ReadOnlyMetric --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 22 +++++++++---------- .../netstandard2.0/PublicAPI.Unshipped.txt | 22 +++++++++---------- .../InMemoryExporterMetricsExtensions.cs | 16 +++++++------- .../{MetricCopy.cs => ReadOnlyMetric.cs} | 6 ++--- .../Metrics/InMemoryExporterTests.cs | 2 +- 5 files changed, 34 insertions(+), 34 deletions(-) rename src/OpenTelemetry.Exporter.InMemory/{MetricCopy.cs => ReadOnlyMetric.cs} (92%) diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index d6bfeccb593..878adeade8a 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1,15 +1,15 @@ OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions -OpenTelemetry.Metrics.MetricCopy -OpenTelemetry.Metrics.MetricCopy.Description.get -> string -OpenTelemetry.Metrics.MetricCopy.MeterName.get -> string -OpenTelemetry.Metrics.MetricCopy.MeterVersion.get -> string -OpenTelemetry.Metrics.MetricCopy.MetricCopy(OpenTelemetry.Metrics.Metric metric) -> void -OpenTelemetry.Metrics.MetricCopy.MetricPoints.get -> System.Collections.Generic.IReadOnlyList -OpenTelemetry.Metrics.MetricCopy.MetricType.get -> OpenTelemetry.Metrics.MetricType -OpenTelemetry.Metrics.MetricCopy.Name.get -> string -OpenTelemetry.Metrics.MetricCopy.Unit.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric +OpenTelemetry.Metrics.ReadOnlyMetric.Description.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MeterName.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MeterVersion.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.ReadOnlyMetric.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.ReadOnlyMetric.Name.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.ReadOnlyMetric(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.ReadOnlyMetric.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file 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 d6bfeccb593..878adeade8a 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,15 +1,15 @@ OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions -OpenTelemetry.Metrics.MetricCopy -OpenTelemetry.Metrics.MetricCopy.Description.get -> string -OpenTelemetry.Metrics.MetricCopy.MeterName.get -> string -OpenTelemetry.Metrics.MetricCopy.MeterVersion.get -> string -OpenTelemetry.Metrics.MetricCopy.MetricCopy(OpenTelemetry.Metrics.Metric metric) -> void -OpenTelemetry.Metrics.MetricCopy.MetricPoints.get -> System.Collections.Generic.IReadOnlyList -OpenTelemetry.Metrics.MetricCopy.MetricType.get -> OpenTelemetry.Metrics.MetricType -OpenTelemetry.Metrics.MetricCopy.Name.get -> string -OpenTelemetry.Metrics.MetricCopy.Unit.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric +OpenTelemetry.Metrics.ReadOnlyMetric.Description.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MeterName.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MeterVersion.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.ReadOnlyMetric.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.ReadOnlyMetric.Name.get -> string +OpenTelemetry.Metrics.ReadOnlyMetric.ReadOnlyMetric(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.ReadOnlyMetric.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 03b62fb30b7..2055047e8f1 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -72,35 +72,35 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder /// /// Adds InMemory metric exporter to the using default options. - /// The exporter will be setup to export . + /// 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. - /// Collection which will be populated with the exported represented as . + /// Collection which will be populated with the exported represented as . /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, - ICollection exportedItems) + ICollection exportedItems) { return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null); } /// /// Adds InMemory metric exporter to the . - /// The exporter will be setup to export . + /// 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. - /// Collection which will be populated with the exported represented as . + /// Collection which will be populated with the exported represented as . /// configuration options. /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, - ICollection exportedItems, + ICollection exportedItems, Action configureMetricReader) { Guard.ThrowIfNull(builder); @@ -138,7 +138,7 @@ private static MeterProviderBuilder AddInMemoryExporter( private static MeterProviderBuilder AddInMemoryExporter( MeterProviderBuilder builder, - ICollection exportedItems, + ICollection exportedItems, MetricReaderOptions metricReaderOptions, Action configureMetricReader) { @@ -149,7 +149,7 @@ private static MeterProviderBuilder AddInMemoryExporter( { foreach (var metric in metricBatch) { - exportedItems.Add(new MetricCopy(metric)); + exportedItems.Add(new ReadOnlyMetric(metric)); } return ExportResult.Success; diff --git a/src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs b/src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs similarity index 92% rename from src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs rename to src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs index 378af2a4788..b2282ed0e69 100644 --- a/src/OpenTelemetry.Exporter.InMemory/MetricCopy.cs +++ b/src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,11 +23,11 @@ namespace OpenTelemetry.Metrics /// This contains the minimum fields and properties needed for most /// unit testing scenarios. /// - public class MetricCopy + public class ReadOnlyMetric { private readonly MetricStreamIdentity instrumentIdentity; - public MetricCopy(Metric metric) + public ReadOnlyMetric(Metric metric) { this.instrumentIdentity = metric.InstrumentIdentity; this.MetricType = metric.MetricType; diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index c2e84858371..19c700d7391 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -28,7 +28,7 @@ public class InMemoryExporterTests [Fact] public void InMemoryExporterShouldDeepCopyMetricPoints() { - var exportedItems = new List(); + var exportedItems = new List(); using var meter = new Meter(Utils.GetCurrentMethodName()); using var meterProvider = Sdk.CreateMeterProviderBuilder() From f7649221ac7fd3a46482ba5982cb74bab2176e31 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Fri, 13 May 2022 15:35:45 -0700 Subject: [PATCH 05/13] rename ReadOnlyMetric to MetricSnapshot --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 22 +++++++++---------- .../netstandard2.0/PublicAPI.Unshipped.txt | 22 +++++++++---------- .../InMemoryExporterMetricsExtensions.cs | 16 +++++++------- .../{ReadOnlyMetric.cs => MetricSnapshot.cs} | 6 ++--- .../Metrics/InMemoryExporterTests.cs | 2 +- 5 files changed, 34 insertions(+), 34 deletions(-) rename src/OpenTelemetry.Exporter.InMemory/{ReadOnlyMetric.cs => MetricSnapshot.cs} (92%) diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index 878adeade8a..1f9d58b8036 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1,15 +1,15 @@ OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions -OpenTelemetry.Metrics.ReadOnlyMetric -OpenTelemetry.Metrics.ReadOnlyMetric.Description.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MeterName.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MeterVersion.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MetricPoints.get -> System.Collections.Generic.IReadOnlyList -OpenTelemetry.Metrics.ReadOnlyMetric.MetricType.get -> OpenTelemetry.Metrics.MetricType -OpenTelemetry.Metrics.ReadOnlyMetric.Name.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.ReadOnlyMetric(OpenTelemetry.Metrics.Metric metric) -> void -OpenTelemetry.Metrics.ReadOnlyMetric.Unit.get -> string +OpenTelemetry.Metrics.MetricSnapshot +OpenTelemetry.Metrics.MetricSnapshot.Description.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MeterName.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MeterVersion.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.MetricSnapshot.MetricSnapshot(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.MetricSnapshot.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.MetricSnapshot.Name.get -> string +OpenTelemetry.Metrics.MetricSnapshot.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file 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 878adeade8a..1f9d58b8036 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,15 +1,15 @@ OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions -OpenTelemetry.Metrics.ReadOnlyMetric -OpenTelemetry.Metrics.ReadOnlyMetric.Description.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MeterName.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MeterVersion.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.MetricPoints.get -> System.Collections.Generic.IReadOnlyList -OpenTelemetry.Metrics.ReadOnlyMetric.MetricType.get -> OpenTelemetry.Metrics.MetricType -OpenTelemetry.Metrics.ReadOnlyMetric.Name.get -> string -OpenTelemetry.Metrics.ReadOnlyMetric.ReadOnlyMetric(OpenTelemetry.Metrics.Metric metric) -> void -OpenTelemetry.Metrics.ReadOnlyMetric.Unit.get -> string +OpenTelemetry.Metrics.MetricSnapshot +OpenTelemetry.Metrics.MetricSnapshot.Description.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MeterName.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MeterVersion.get -> string +OpenTelemetry.Metrics.MetricSnapshot.MetricPoints.get -> System.Collections.Generic.IReadOnlyList +OpenTelemetry.Metrics.MetricSnapshot.MetricSnapshot(OpenTelemetry.Metrics.Metric metric) -> void +OpenTelemetry.Metrics.MetricSnapshot.MetricType.get -> OpenTelemetry.Metrics.MetricType +OpenTelemetry.Metrics.MetricSnapshot.Name.get -> string +OpenTelemetry.Metrics.MetricSnapshot.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 2055047e8f1..603229776f5 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -72,35 +72,35 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder /// /// Adds InMemory metric exporter to the using default options. - /// The exporter will be setup to export . + /// 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. - /// Collection which will be populated with the exported represented as . + /// Collection which will be populated with the exported represented as . /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, - ICollection exportedItems) + ICollection exportedItems) { return builder.AddInMemoryExporter(exportedItems: exportedItems, configureMetricReader: null); } /// /// Adds InMemory metric exporter to the . - /// The exporter will be setup to export . + /// 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. - /// Collection which will be populated with the exported represented as . + /// Collection which will be populated with the exported represented as . /// configuration options. /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter( this MeterProviderBuilder builder, - ICollection exportedItems, + ICollection exportedItems, Action configureMetricReader) { Guard.ThrowIfNull(builder); @@ -138,7 +138,7 @@ private static MeterProviderBuilder AddInMemoryExporter( private static MeterProviderBuilder AddInMemoryExporter( MeterProviderBuilder builder, - ICollection exportedItems, + ICollection exportedItems, MetricReaderOptions metricReaderOptions, Action configureMetricReader) { @@ -149,7 +149,7 @@ private static MeterProviderBuilder AddInMemoryExporter( { foreach (var metric in metricBatch) { - exportedItems.Add(new ReadOnlyMetric(metric)); + exportedItems.Add(new MetricSnapshot(metric)); } return ExportResult.Success; diff --git a/src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs b/src/OpenTelemetry.Exporter.InMemory/MetricSnapshot.cs similarity index 92% rename from src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs rename to src/OpenTelemetry.Exporter.InMemory/MetricSnapshot.cs index b2282ed0e69..635e3549310 100644 --- a/src/OpenTelemetry.Exporter.InMemory/ReadOnlyMetric.cs +++ b/src/OpenTelemetry.Exporter.InMemory/MetricSnapshot.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,11 +23,11 @@ namespace OpenTelemetry.Metrics /// This contains the minimum fields and properties needed for most /// unit testing scenarios. /// - public class ReadOnlyMetric + public class MetricSnapshot { private readonly MetricStreamIdentity instrumentIdentity; - public ReadOnlyMetric(Metric metric) + public MetricSnapshot(Metric metric) { this.instrumentIdentity = metric.InstrumentIdentity; this.MetricType = metric.MetricType; diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index 19c700d7391..389745638b9 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -28,7 +28,7 @@ public class InMemoryExporterTests [Fact] public void InMemoryExporterShouldDeepCopyMetricPoints() { - var exportedItems = new List(); + var exportedItems = new List(); using var meter = new Meter(Utils.GetCurrentMethodName()); using var meterProvider = Sdk.CreateMeterProviderBuilder() From e79f5672a9db84e37480c19e34d6c71c5900eaee Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Wed, 18 May 2022 12:30:23 -0700 Subject: [PATCH 06/13] addressing code review comments --- src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs | 8 +++----- .../InMemoryExporterMetricsExtensions.cs | 5 +++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 27e6f4d9644..34e1574732b 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -23,21 +23,19 @@ public class InMemoryExporter : BaseExporter where T : class { private readonly ICollection exportedItems; - private readonly ExportDelegate onExport; + private readonly Func, ExportResult> onExport; public InMemoryExporter(ICollection exportedItems) { this.exportedItems = exportedItems; - this.onExport = this.DefaultExport; + this.onExport = (Batch batch) => this.DefaultExport(batch); } public InMemoryExporter(Func, ExportResult> exportFunc) { - this.onExport = (in Batch batch) => exportFunc(batch); + this.onExport = exportFunc; } - private delegate ExportResult ExportDelegate(in Batch batch); - public override ExportResult Export(in Batch batch) => this.onExport(batch); private ExportResult DefaultExport(in Batch batch) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 603229776f5..1ac862a7f25 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -147,6 +147,11 @@ private static MeterProviderBuilder AddInMemoryExporter( var metricExporter = new InMemoryExporter( exportFunc: metricBatch => { + if (exportedItems == null) + { + return ExportResult.Failure; + } + foreach (var metric in metricBatch) { exportedItems.Add(new MetricSnapshot(metric)); From 04b894e1fd1d6580ff4adf575bc1eb7fe5254446 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Fri, 20 May 2022 12:30:37 -0700 Subject: [PATCH 07/13] update unit test --- .../Metrics/InMemoryExporterTests.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index 389745638b9..ded928b77bd 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -41,30 +41,31 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() var counter = meter.CreateCounter("meter"); - // Emit 10 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") + // TEST 1: Emit 10 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") counter.Add(10, new KeyValuePair("tag1", "value1")); meterProvider.ForceFlush(); - var metric = exportedItems[0]; // Only one Metric object is added to the collection at this point - var metricPointsEnumerator = metric.MetricPoints.GetEnumerator(); - Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric - var metricPointForFirstExport = metricPointsEnumerator.Current; - Assert.Equal(10, metricPointForFirstExport.GetSumLong()); + Assert.Single(exportedItems); + var metric1 = exportedItems[0]; // Only one Metric object is added to the collection at this point + Assert.Single(metric1.MetricPoints); + var metricPoint1 = metric1.MetricPoints[0]; + Assert.Equal(10, metricPoint1.GetSumLong()); - // Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") + // TEST 2: Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") counter.Add(25, new KeyValuePair("tag1", "value1")); meterProvider.ForceFlush(); - metric = exportedItems[1]; // Second Metric object is added to the collection at this point - metricPointsEnumerator = metric.MetricPoints.GetEnumerator(); - Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric - var metricPointForSecondExport = metricPointsEnumerator.Current; - Assert.Equal(25, metricPointForSecondExport.GetSumLong()); + Assert.Equal(2, exportedItems.Count); + var metric2 = exportedItems[1]; // Second Metric object is added to the collection at this point + Assert.Single(metric2.MetricPoints); + var metricPoint2 = metric2.MetricPoints[0]; + Assert.Equal(25, metricPoint2.GetSumLong()); - // MetricPoint.LongValue for the first exporter metric should still be 10 - Assert.Equal(10, metricPointForFirstExport.GetSumLong()); + // TEST 3: Verify first exported metric is unchanged + // MetricPoint.LongValue for the first exported metric should still be 10 + Assert.Equal(10, metricPoint1.GetSumLong()); } } } From c8261de43f82c54bfd37ae9e19bf53791f21cfbf Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Fri, 20 May 2022 14:12:18 -0700 Subject: [PATCH 08/13] fix test --- test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index ded928b77bd..841d4f293ad 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -49,8 +49,7 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() Assert.Single(exportedItems); var metric1 = exportedItems[0]; // Only one Metric object is added to the collection at this point Assert.Single(metric1.MetricPoints); - var metricPoint1 = metric1.MetricPoints[0]; - Assert.Equal(10, metricPoint1.GetSumLong()); + Assert.Equal(10, metric1.MetricPoints[0].GetSumLong()); // TEST 2: Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") counter.Add(25, new KeyValuePair("tag1", "value1")); @@ -60,12 +59,11 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() Assert.Equal(2, exportedItems.Count); var metric2 = exportedItems[1]; // Second Metric object is added to the collection at this point Assert.Single(metric2.MetricPoints); - var metricPoint2 = metric2.MetricPoints[0]; - Assert.Equal(25, metricPoint2.GetSumLong()); + Assert.Equal(25, metric2.MetricPoints[0].GetSumLong()); // TEST 3: Verify first exported metric is unchanged // MetricPoint.LongValue for the first exported metric should still be 10 - Assert.Equal(10, metricPoint1.GetSumLong()); + Assert.Equal(10, metric1.MetricPoints[0].GetSumLong()); } } } From 4d7541932286130252de304cc5462971b7fea6af Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Fri, 20 May 2022 15:57:06 -0700 Subject: [PATCH 09/13] ExportMetricSnapshot as method --- .../InMemoryExporterMetricsExtensions.cs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 1ac862a7f25..f817f5744fa 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -145,20 +145,7 @@ private static MeterProviderBuilder AddInMemoryExporter( configureMetricReader?.Invoke(metricReaderOptions); var metricExporter = new InMemoryExporter( - exportFunc: metricBatch => - { - if (exportedItems == null) - { - return ExportResult.Failure; - } - - foreach (var metric in metricBatch) - { - exportedItems.Add(new MetricSnapshot(metric)); - } - - return ExportResult.Success; - }); + exportFunc: metricBatch => ExportMetricSnapshot(metricBatch, exportedItems)); var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader( metricExporter, @@ -168,5 +155,20 @@ private static MeterProviderBuilder AddInMemoryExporter( return builder.AddReader(metricReader); } + + private static ExportResult ExportMetricSnapshot(in Batch batch, ICollection exportedItems) + { + if (exportedItems == null) + { + return ExportResult.Failure; + } + + foreach (var metric in batch) + { + exportedItems.Add(new MetricSnapshot(metric)); + } + + return ExportResult.Success; + } } } From 436fd4b0a1b887a74b50decdccea057199839292 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Mon, 23 May 2022 10:56:33 -0700 Subject: [PATCH 10/13] addressing comments --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 3 +-- .../.publicApi/netstandard2.0/PublicAPI.Unshipped.txt | 3 +-- src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs | 2 +- .../InMemoryExporterMetricsExtensions.cs | 5 ----- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index 1f9d58b8036..17941b6fcf3 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -1,4 +1,3 @@ -OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions OpenTelemetry.Metrics.MetricSnapshot OpenTelemetry.Metrics.MetricSnapshot.Description.get -> string @@ -12,4 +11,4 @@ OpenTelemetry.Metrics.MetricSnapshot.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, 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 1f9d58b8036..17941b6fcf3 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,4 +1,3 @@ -OpenTelemetry.Exporter.InMemoryExporter.InMemoryExporter(System.Func, OpenTelemetry.ExportResult> exportFunc) -> void OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions OpenTelemetry.Metrics.MetricSnapshot OpenTelemetry.Metrics.MetricSnapshot.Description.get -> string @@ -12,4 +11,4 @@ OpenTelemetry.Metrics.MetricSnapshot.Unit.get -> string static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder \ No newline at end of file +static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection exportedItems, System.Action configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 34e1574732b..8dab7b51d96 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -31,7 +31,7 @@ public InMemoryExporter(ICollection exportedItems) this.onExport = (Batch batch) => this.DefaultExport(batch); } - public InMemoryExporter(Func, ExportResult> exportFunc) + internal InMemoryExporter(Func, ExportResult> exportFunc) { this.onExport = exportFunc; } diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index f817f5744fa..1042069fddf 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -158,11 +158,6 @@ private static MeterProviderBuilder AddInMemoryExporter( private static ExportResult ExportMetricSnapshot(in Batch batch, ICollection exportedItems) { - if (exportedItems == null) - { - return ExportResult.Failure; - } - foreach (var metric in batch) { exportedItems.Add(new MetricSnapshot(metric)); From f2673c7cf42f70cd5812321710358cf5c6cf2b54 Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Mon, 23 May 2022 10:59:31 -0700 Subject: [PATCH 11/13] changelog --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index 4b7ad94f5c3..b53b635bc94 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Adds new `AddInMemoryExporter` extension method to export `Metric` as new + type `MetricSnapshot`. + ([#2361](https://github.com/open-telemetry/opentelemetry-dotnet/issues/2361)) + ## 1.3.0-beta.2 Released 2022-May-16 From 2ce413b122b666139acb55851320956c1d4101cd Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Mon, 23 May 2022 11:01:50 -0700 Subject: [PATCH 12/13] markdownlint --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index b53b635bc94..e00489807a5 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,8 +2,8 @@ ## Unreleased -* Adds new `AddInMemoryExporter` extension method to export `Metric` as new - type `MetricSnapshot`. +* Adds new `AddInMemoryExporter` extension method to export `Metric` as new + type `MetricSnapshot`. ([#2361](https://github.com/open-telemetry/opentelemetry-dotnet/issues/2361)) ## 1.3.0-beta.2 From da22b5caaefe3cb9a260a4faa109bae8ca70ed4e Mon Sep 17 00:00:00 2001 From: TimothyMothra Date: Mon, 23 May 2022 13:25:08 -0700 Subject: [PATCH 13/13] code review: restore if --- .../InMemoryExporterMetricsExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 1042069fddf..f817f5744fa 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -158,6 +158,11 @@ private static MeterProviderBuilder AddInMemoryExporter( private static ExportResult ExportMetricSnapshot(in Batch batch, ICollection exportedItems) { + if (exportedItems == null) + { + return ExportResult.Failure; + } + foreach (var metric in batch) { exportedItems.Add(new MetricSnapshot(metric));