Skip to content

Commit

Permalink
MetricReaderOptions for AddInMemoryExporter extension methods (#2931)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Feb 23, 2022
1 parent 644ba5b commit cbc5172
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems, System.Action<OpenTelemetry.Metrics.MetricReaderOptions> configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.InMemoryExporterMetricsExtensions.AddInMemoryExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Collections.Generic.ICollection<OpenTelemetry.Metrics.Metric> exportedItems, System.Action<OpenTelemetry.Metrics.MetricReaderOptions> configureMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Adds the ability to configure `MetricReaderOptions` via the
`AddInMemoryExporter` extension method.
([#2931](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2931))

## 1.2.0-rc2

Released 2022-Feb-02
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Metrics
{
/// <summary>
/// Extension methods to simplify registering of the InMemory exporter.
/// </summary>
public static class InMemoryExporterMetricsExtensions
{
/// <summary>
/// Adds InMemory exporter to the TracerProvider.
/// Adds InMemory metric exporter to the <see cref="MeterProviderBuilder"/> using default options.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="exportedItems">Collection which will be populated with the exported MetricItem.</param>
Expand All @@ -33,7 +37,61 @@ public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(exportedItems);

return builder.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems)));
if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
{
return deferredMeterProviderBuilder.Configure((sp, builder) =>
{
AddInMemoryExporter(builder, exportedItems, sp.GetOptions<MetricReaderOptions>(), null);
});
}

return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), null);
}

/// <summary>
/// Adds InMemory metric exporter to the <see cref="MeterProviderBuilder"/> using default options.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="exportedItems">Collection which will be populated with the exported MetricItem.</param>
/// <param name="configureMetricReader"><see cref="MetricReader"/> configuration options.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection<Metric> exportedItems, Action<MetricReaderOptions> configureMetricReader)
{
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(exportedItems);

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

return AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader);
}

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

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

if (metricReaderOptions.MetricReaderType == (MetricReaderType)(-1))
{
metricReaderOptions.MetricReaderType = MetricReaderType.Manual;
}

var metricReader = metricReaderOptions.MetricReaderType == MetricReaderType.Manual
? new BaseExportingMetricReader(metricExporter)
: new PeriodicExportingMetricReader(metricExporter, metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds);

metricReader.Temporality = metricReaderOptions.Temporality;
return builder.AddReader(metricReader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>

Expand Down
8 changes: 5 additions & 3 deletions test/Benchmarks/Metrics/HistogramBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System.Threading;
using BenchmarkDotNet.Attributes;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Tests;

Expand Down Expand Up @@ -76,11 +75,14 @@ public void Setup()
}

var exportedItems = new List<Metric>();
var reader = new PeriodicExportingMetricReader(new InMemoryExporter<Metric>(exportedItems), 1000);

this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name)
.AddReader(reader)
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.AddView(this.histogramLong.Name, new ExplicitBucketHistogramConfiguration() { Boundaries = this.bounds })
.AddView(this.histogramDouble.Name, new ExplicitBucketHistogramConfiguration() { Boundaries = this.bounds })
.Build();
Expand Down
11 changes: 6 additions & 5 deletions test/Benchmarks/Metrics/MetricsBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ public void Setup()
this.meter = new Meter(Utils.GetCurrentMethodName());

var exportedItems = new List<Metric>();
var reader = new PeriodicExportingMetricReader(new InMemoryExporter<Metric>(exportedItems), 1000)
{
Temporality = this.AggregationTemporality,
};
this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name) // All instruments from this meter are enabled.
.AddReader(reader)
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
metricReaderOptions.Temporality = this.AggregationTemporality;
})
.Build();

this.counter = this.meter.CreateCounter<long>("counter");
Expand Down
6 changes: 3 additions & 3 deletions test/Benchmarks/Metrics/MetricsViewBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ public void Setup()
{
this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name)
.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(this.metrics)))
.AddInMemoryExporter(this.metrics)
.Build();
}
else if (this.ViewConfig == ViewConfiguration.ViewNoInstrSelect)
{
this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name)
.AddView("nomatch", new MetricStreamConfiguration() { TagKeys = new string[] { "DimName1", "DimName2", "DimName3" } })
.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(this.metrics)))
.AddInMemoryExporter(this.metrics)
.Build();
}
else if (this.ViewConfig == ViewConfiguration.ViewSelectsInstr)
{
this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name)
.AddView(this.counter.Name, new MetricStreamConfiguration() { TagKeys = new string[] { "DimName1", "DimName2", "DimName3" } })
.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(this.metrics)))
.AddInMemoryExporter(this.metrics)
.Build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ public void TestCounterToOltpMetric(string name, string description, string unit
using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddReader(
new BaseExportingMetricReader(new InMemoryExporter<Metric>(metrics))
{
Temporality = aggregationTemporality,
})
.AddInMemoryExporter(metrics, metricReaderOptions =>
{
metricReaderOptions.Temporality = aggregationTemporality;
})
.Build();

var attributes = ToAttributes(keysValues).ToArray();
Expand Down Expand Up @@ -334,13 +333,13 @@ public void TestHistogramToOltpMetric(string name, string description, string un
{
var metrics = new List<Metric>();

var metricReader = new BaseExportingMetricReader(new InMemoryExporter<Metric>(metrics));
metricReader.Temporality = aggregationTemporality;

using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddReader(metricReader)
.AddInMemoryExporter(metrics, metricReaderOptions =>
{
metricReaderOptions.Temporality = aggregationTemporality;
})
.Build();

var attributes = ToAttributes(keysValues).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
#if NETCOREAPP3_1
Expand Down Expand Up @@ -57,15 +56,10 @@ public void AddAspNetCoreInstrumentation_BadArgs()
public async Task RequestMetricIsCaptured()
{
var metricItems = new List<Metric>();
var metricExporter = new InMemoryExporter<Metric>(metricItems);

var metricReader = new BaseExportingMetricReader(metricExporter)
{
Temporality = AggregationTemporality.Cumulative,
};
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddReader(metricReader)
.AddInMemoryExporter(metricItems)
.Build();

using (var client = this.factory.CreateClient())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -55,16 +54,11 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
var processor = new Mock<BaseProcessor<Activity>>();
tc.Url = HttpTestData.NormalizeValues(tc.Url, host, port);

var metricItems = new List<Metric>();
var metricExporter = new InMemoryExporter<Metric>(metricItems);
var metrics = new List<Metric>();

var metricReader = new BaseExportingMetricReader(metricExporter)
{
Temporality = AggregationTemporality.Cumulative,
};
var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.AddReader(metricReader)
.AddInMemoryExporter(metrics)
.Build();

using (serverLifeTime)
Expand Down Expand Up @@ -107,7 +101,7 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut

meterProvider.Dispose();

var requestMetrics = metricItems
var requestMetrics = metrics
.Where(metric => metric.Name == "http.client.duration")
.ToArray();

Expand Down
5 changes: 2 additions & 3 deletions test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Collections.Generic;
using System.Diagnostics.Metrics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Tests;
using Xunit;

Expand All @@ -32,9 +31,9 @@ public void InMemoryExporterShouldDeepCopyMetricPoints()
using var meter = new Meter(Utils.GetCurrentMethodName());
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems))
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
Temporality = AggregationTemporality.Delta,
metricReaderOptions.Temporality = AggregationTemporality.Delta;
})
.Build();

Expand Down
10 changes: 4 additions & 6 deletions test/OpenTelemetry.Tests/Metrics/MemoryEfficiencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Collections.Generic;
using System.Diagnostics.Metrics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Tests;
using Xunit;

Expand All @@ -35,11 +34,10 @@ public void ExportOnlyWhenPointChanged(AggregationTemporality temporality)

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddReader(
new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems))
{
Temporality = temporality,
})
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.Temporality = temporality;
})
.Build();

var counter = meter.CreateCounter<long>("meter");
Expand Down
Loading

0 comments on commit cbc5172

Please sign in to comment.