Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics exporter modes #2353

Merged
merged 6 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace OpenTelemetry.Exporter
/// Exporter of OpenTelemetry metrics to Prometheus.
/// </summary>
[AggregationTemporality(AggregationTemporality.Cumulative)]
[ExportModes(ExportModes.Pull)]
public class PrometheusExporter : BaseExporter<Metric>
{
internal readonly PrometheusExporterOptions Options;
Expand Down
20 changes: 16 additions & 4 deletions src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@ public class BaseExportingMetricReader : MetricReader
protected readonly BaseExporter<Metric> exporter;
protected bool disposed;

private readonly ExportModes supportedExportModes = ExportModes.Push | ExportModes.Pull;

public BaseExportingMetricReader(BaseExporter<Metric> exporter)
{
this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));

var attributes = exporter.GetType().GetCustomAttributes(typeof(AggregationTemporalityAttribute), true);
var exportorType = exporter.GetType();
var attributes = exportorType.GetCustomAttributes(typeof(AggregationTemporalityAttribute), true);
if (attributes.Length > 0)
{
var attr = (AggregationTemporalityAttribute)attributes[attributes.Length - 1];
this.PreferredAggregationTemporality = attr.Preferred;
this.SupportedAggregationTemporality = attr.Supported;
}

attributes = exportorType.GetCustomAttributes(typeof(ExportModesAttribute), true);
if (attributes.Length > 0)
{
AggregationTemporalityAttribute aggregationTemporality = (AggregationTemporalityAttribute)attributes[attributes.Length - 1];
this.PreferredAggregationTemporality = aggregationTemporality.Preferred;
this.SupportedAggregationTemporality = aggregationTemporality.Supported;
var attr = (ExportModesAttribute)attributes[attributes.Length - 1];
this.supportedExportModes = attr.Supported;
}
}

protected ExportModes SupportedExportModes => this.supportedExportModes;

public override void OnCollect(Batch<Metric> metrics)
{
this.exporter.Export(metrics);
Expand Down
47 changes: 47 additions & 0 deletions src/OpenTelemetry/Metrics/ExportModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <copyright file="ExportModes.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>

using System;

namespace OpenTelemetry.Metrics
{
[Flags]
public enum ExportModes : byte
{
/*
0 0 0 0 0 0 0 0
| | | | | | | |
| | | | | | | +--- Push
| | | | | | +----- Pull
| | | | | +------- (reserved)
| | | | +--------- (reserved)
| | | +----------- (reserved)
| | +------------- (reserved)
| +--------------- (reserved)
+----------------- (reserved)
*/

/// <summary>
/// Push.
/// </summary>
Push = 0b1,

/// <summary>
/// Pull.
/// </summary>
Pull = 0b10,
}
}
33 changes: 33 additions & 0 deletions src/OpenTelemetry/Metrics/ExportModesAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <copyright file="ExportModesAttribute.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>

using System;

namespace OpenTelemetry.Metrics
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class ExportModesAttribute : Attribute
{
private ExportModes supportedExportModes;

public ExportModesAttribute(ExportModes supported)
{
this.supportedExportModes = supported;
}

public ExportModes Supported => this.supportedExportModes;
}
}
15 changes: 13 additions & 2 deletions src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,31 @@ namespace OpenTelemetry.Metrics
{
public class PeriodicExportingMetricReader : BaseExportingMetricReader
{
internal const int DefaultExportIntervalMilliseconds = 60000;
internal const int DefaultExportTimeoutMilliseconds = 30000;

private readonly Task exportTask;
private readonly CancellationTokenSource token;

public PeriodicExportingMetricReader(BaseExporter<Metric> exporter, int exportIntervalMs)
public PeriodicExportingMetricReader(
BaseExporter<Metric> exporter,
int exportIntervalMilliseconds = DefaultExportIntervalMilliseconds,
int exportTimeoutMilliseconds = DefaultExportTimeoutMilliseconds)
reyang marked this conversation as resolved.
Show resolved Hide resolved
: base(exporter)
{
if ((this.SupportedExportModes & ExportModes.Push) != ExportModes.Push)
{
throw new InvalidOperationException("The exporter does not support push mode.");
}

this.token = new CancellationTokenSource();

// TODO: Use dedicated thread.
this.exportTask = new Task(() =>
{
while (!this.token.IsCancellationRequested)
{
Task.Delay(exportIntervalMs).Wait();
Task.Delay(exportIntervalMilliseconds).Wait();
this.Collect();
}
});
Expand Down