Clean up metric reader options (#3038)

This commit is contained in:
Alan West 2022-03-15 07:20:09 -07:00 committed by GitHub
parent bab645fe3d
commit a709cfd2a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 96 additions and 80 deletions

View File

@ -93,7 +93,6 @@ namespace Examples.AspNet
// The ConsoleMetricExporter defaults to a manual collect cycle.
// This configuration causes metrics to be exported to stdout on a 10s interval.
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 10000;
});
break;

View File

@ -135,7 +135,6 @@ builder.Services.AddOpenTelemetryMetrics(options =>
{
// The ConsoleMetricExporter defaults to a manual collect cycle.
// This configuration causes metrics to be exported to stdout on a 10s interval.
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 10000;
});
break;

View File

@ -70,7 +70,6 @@ namespace Examples.Console
{
exporterOptions.Protocol = options.UseGrpc ? OtlpExportProtocol.Grpc : OtlpExportProtocol.HttpProtobuf;
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds;
metricReaderOptions.Temporality = options.IsDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative;
});
@ -82,7 +81,6 @@ namespace Examples.Console
{
exporterOptions.Targets = ConsoleExporterOutputTargets.Console;
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds;
metricReaderOptions.Temporality = options.IsDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative;
});

View File

@ -15,6 +15,7 @@
// </copyright>
using System;
using System.Threading;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
@ -25,6 +26,9 @@ namespace OpenTelemetry.Metrics
/// </summary>
public static class ConsoleExporterMetricsExtensions
{
private const int DefaultExportIntervalMilliseconds = Timeout.Infinite;
private const int DefaultExportTimeoutMilliseconds = Timeout.Infinite;
/// <summary>
/// Adds <see cref="ConsoleMetricExporter"/> to the <see cref="MeterProviderBuilder"/> using default options.
/// </summary>
@ -97,13 +101,12 @@ namespace OpenTelemetry.Metrics
var metricExporter = new ConsoleMetricExporter(exporterOptions);
var metricReader = metricReaderOptions.MetricReaderType == MetricReaderType.Manual
? new BaseExportingMetricReader(metricExporter)
: new PeriodicExportingMetricReader(
metricExporter,
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds ?? -1);
var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
metricExporter,
metricReaderOptions,
DefaultExportIntervalMilliseconds,
DefaultExportTimeoutMilliseconds);
metricReader.Temporality = metricReaderOptions.Temporality;
return builder.AddReader(metricReader);
}
}

View File

@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>

View File

@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
@ -26,6 +27,9 @@ namespace OpenTelemetry.Metrics
/// </summary>
public static class InMemoryExporterMetricsExtensions
{
private const int DefaultExportIntervalMilliseconds = Timeout.Infinite;
private const int DefaultExportTimeoutMilliseconds = Timeout.Infinite;
/// <summary>
/// Adds InMemory metric exporter to the <see cref="MeterProviderBuilder"/> using default options.
/// </summary>
@ -81,13 +85,12 @@ namespace OpenTelemetry.Metrics
var metricExporter = new InMemoryExporter<Metric>(exportedItems);
var metricReader = metricReaderOptions.MetricReaderType == MetricReaderType.Manual
? new BaseExportingMetricReader(metricExporter)
: new PeriodicExportingMetricReader(
metricExporter,
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds ?? -1);
var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
metricExporter,
metricReaderOptions,
DefaultExportIntervalMilliseconds,
DefaultExportTimeoutMilliseconds);
metricReader.Temporality = metricReaderOptions.Temporality;
return builder.AddReader(metricReader);
}
}

View File

@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>

View File

@ -46,6 +46,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />

View File

@ -26,6 +26,7 @@ namespace OpenTelemetry.Metrics
public static class OtlpMetricExporterExtensions
{
private const int DefaultExportIntervalMilliseconds = 60000;
private const int DefaultExportTimeoutMilliseconds = 30000;
/// <summary>
/// Adds <see cref="OtlpMetricExporter"/> to the <see cref="MeterProviderBuilder"/> using default options.
@ -104,13 +105,12 @@ namespace OpenTelemetry.Metrics
var metricExporter = new OtlpMetricExporter(exporterOptions);
var metricReader = metricReaderOptions.MetricReaderType == MetricReaderType.Manual
? new BaseExportingMetricReader(metricExporter)
: new PeriodicExportingMetricReader(
metricExporter,
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds ?? DefaultExportIntervalMilliseconds);
var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
metricExporter,
metricReaderOptions,
DefaultExportIntervalMilliseconds,
DefaultExportTimeoutMilliseconds);
metricReader.Temporality = metricReaderOptions.Temporality;
return builder.AddReader(metricReader);
}
}

View File

@ -3,10 +3,7 @@ OpenTelemetry.Batch<T>.Batch(T[] items, int count) -> void
OpenTelemetry.Batch<T>.Count.get -> long
OpenTelemetry.Metrics.MetricReaderOptions
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderOptions() -> void
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderType.get -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderType.set -> void
OpenTelemetry.Metrics.MetricReaderOptions.PeriodicExportingMetricReaderOptions.get -> OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions
OpenTelemetry.Metrics.MetricReaderOptions.PeriodicExportingMetricReaderOptions.set -> void
OpenTelemetry.Metrics.MetricReaderOptions.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.MetricReaderOptions.Temporality.set -> void
OpenTelemetry.Metrics.AggregationTemporality
@ -81,9 +78,6 @@ OpenTelemetry.Metrics.MetricReader.MetricReader() -> void
OpenTelemetry.Metrics.MetricReader.Shutdown(int timeoutMilliseconds = -1) -> bool
OpenTelemetry.Metrics.MetricReader.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.MetricReader.Temporality.set -> void
OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderType.Manual = 0 -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderType.Periodic = 1 -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricStreamConfiguration
OpenTelemetry.Metrics.MetricStreamConfiguration.Description.get -> string
OpenTelemetry.Metrics.MetricStreamConfiguration.Description.set -> void
@ -105,6 +99,8 @@ OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.PeriodicExportingMetricReaderOptions() -> void
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds.get -> int?
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds.set -> void
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds.get -> int?
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds.set -> void
OpenTelemetry.ReadOnlyTagCollection
OpenTelemetry.ReadOnlyTagCollection.Count.get -> int
OpenTelemetry.ReadOnlyTagCollection.Enumerator

View File

@ -3,10 +3,7 @@ OpenTelemetry.Batch<T>.Batch(T[] items, int count) -> void
OpenTelemetry.Batch<T>.Count.get -> long
OpenTelemetry.Metrics.MetricReaderOptions
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderOptions() -> void
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderType.get -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderOptions.MetricReaderType.set -> void
OpenTelemetry.Metrics.MetricReaderOptions.PeriodicExportingMetricReaderOptions.get -> OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions
OpenTelemetry.Metrics.MetricReaderOptions.PeriodicExportingMetricReaderOptions.set -> void
OpenTelemetry.Metrics.MetricReaderOptions.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.MetricReaderOptions.Temporality.set -> void
OpenTelemetry.Metrics.AggregationTemporality
@ -81,9 +78,6 @@ OpenTelemetry.Metrics.MetricReader.MetricReader() -> void
OpenTelemetry.Metrics.MetricReader.Shutdown(int timeoutMilliseconds = -1) -> bool
OpenTelemetry.Metrics.MetricReader.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.MetricReader.Temporality.set -> void
OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderType.Manual = 0 -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricReaderType.Periodic = 1 -> OpenTelemetry.Metrics.MetricReaderType
OpenTelemetry.Metrics.MetricStreamConfiguration
OpenTelemetry.Metrics.MetricStreamConfiguration.Description.get -> string
OpenTelemetry.Metrics.MetricStreamConfiguration.Description.set -> void
@ -105,6 +99,8 @@ OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.PeriodicExportingMetricReaderOptions() -> void
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds.get -> int?
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds.set -> void
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds.get -> int?
OpenTelemetry.Metrics.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds.set -> void
OpenTelemetry.ReadOnlyTagCollection
OpenTelemetry.ReadOnlyTagCollection.Count.get -> int
OpenTelemetry.ReadOnlyTagCollection.Enumerator

View File

@ -7,6 +7,19 @@
period.
([#2982](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2982))
* Added the `PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds`
option.
([#3038](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3038))
* Removed `MetricReaderType`. This enumeration was previously used when
configuing a metric reader with an exporter to configure whether the export
cycle would be periodic or manual (i.e., requiring a explicit call to flush
metrics). This change affects the push-based metric exporters: OTLP, Console,
and InMemory. For these exporters, a manual export cycle can now be achieved
by setting `PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds`
to `-1`.
([#3038](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3038))
## 1.2.0-rc3
Released 2022-Mar-04

View File

@ -0,0 +1,40 @@
// <copyright file="PeriodicExportingMetricReaderHelper.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
namespace OpenTelemetry.Metrics;
internal static class PeriodicExportingMetricReaderHelper
{
internal static PeriodicExportingMetricReader CreatePeriodicExportingMetricReader(
BaseExporter<Metric> exporter,
MetricReaderOptions options,
int defaultExportIntervalMilliseconds,
int defaultExportTimeoutMilliseconds)
{
var exportInterval =
options.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds
?? defaultExportIntervalMilliseconds;
var exportTimeout =
options.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds
?? defaultExportTimeoutMilliseconds;
var metricReader = new PeriodicExportingMetricReader(exporter, exportInterval, exportTimeout);
metricReader.Temporality = options.Temporality;
return metricReader;
}
}

View File

@ -28,12 +28,7 @@ public class MetricReaderOptions
public AggregationTemporality Temporality { get; set; } = AggregationTemporality.Cumulative;
/// <summary>
/// Gets or sets the <see cref="MetricReaderType" /> to use. Defaults to <c>MetricReaderType.Periodic</c>.
/// Gets the <see cref="PeriodicExportingMetricReaderOptions" /> options.
/// </summary>
public MetricReaderType MetricReaderType { get; set; } = MetricReaderType.Periodic;
/// <summary>
/// Gets or sets the <see cref="PeriodicExportingMetricReaderOptions" /> options. Ignored unless <c>MetricReaderType</c> is <c>Periodic</c>.
/// </summary>
public PeriodicExportingMetricReaderOptions PeriodicExportingMetricReaderOptions { get; set; } = new PeriodicExportingMetricReaderOptions();
public PeriodicExportingMetricReaderOptions PeriodicExportingMetricReaderOptions { get; private set; } = new PeriodicExportingMetricReaderOptions();
}

View File

@ -1,36 +0,0 @@
// <copyright file="MetricReaderType.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
namespace OpenTelemetry.Metrics
{
/// <summary>
/// Type of <see cref="MetricReader" /> to be used.
/// </summary>
public enum MetricReaderType
{
/// <summary>
/// Use the <see cref="BaseExportingMetricReader" />.
/// This requires manually invoking <c>MetricReader.Collect()</c> to export metrics.
/// </summary>
Manual,
/// <summary>
/// Uses the <see cref="PeriodicExportingMetricReader" />.
/// <c>MetricReader.Collect()</c> will be invoked on a defined interval.
/// </summary>
Periodic,
}
}

View File

@ -52,7 +52,7 @@ namespace OpenTelemetry.Metrics
{
Guard.ThrowIfInvalidTimeout(exportIntervalMilliseconds);
Guard.ThrowIfZero(exportIntervalMilliseconds);
Guard.ThrowIfOutOfRange(exportTimeoutMilliseconds, min: 0);
Guard.ThrowIfInvalidTimeout(exportTimeoutMilliseconds);
if ((this.SupportedExportModes & ExportModes.Push) != ExportModes.Push)
{

View File

@ -20,7 +20,16 @@ namespace OpenTelemetry.Metrics
{
/// <summary>
/// Gets or sets the metric export interval in milliseconds.
/// If not set, the default value depends on the type of metric exporter
/// associated with the metric reader.
/// </summary>
public int? ExportIntervalMilliseconds { get; set; }
/// <summary>
/// Gets or sets the metric export timeout in milliseconds.
/// If not set, the default value depends on the type of metric exporter
/// associated with the metric reader.
/// </summary>
public int? ExportTimeoutMilliseconds { get; set; }
}
}

View File

@ -90,7 +90,6 @@ namespace Benchmarks.Metrics
.AddMeter(this.meter.Name)
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.AddView(this.histogram.Name, new ExplicitBucketHistogramConfiguration() { Boundaries = this.bounds })

View File

@ -74,7 +74,6 @@ namespace Benchmarks.Metrics
.AddMeter(this.meter.Name) // All instruments from this meter are enabled.
.AddInMemoryExporter(exportedItems, metricReaderOptions =>
{
metricReaderOptions.MetricReaderType = MetricReaderType.Periodic;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
metricReaderOptions.Temporality = this.AggregationTemporality;
})