Add test for MeterFactory (#691)
* Add test for MeterFactory * Try remove file header diff Co-authored-by: Mike Goldsmith <goldsmith.mike@gmail.com> Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
parent
021736f7e9
commit
ace469d42b
|
|
@ -28,7 +28,6 @@ namespace OpenTelemetry.Metrics.Configuration
|
|||
private readonly Dictionary<MeterRegistryKey, MeterSdk> meterRegistry = new Dictionary<MeterRegistryKey, MeterSdk>();
|
||||
private readonly MetricProcessor metricProcessor;
|
||||
private readonly MetricExporter metricExporter;
|
||||
private readonly PushMetricController pushMetricController;
|
||||
private readonly TimeSpan defaultPushInterval = TimeSpan.FromSeconds(60);
|
||||
private MeterSdk defaultMeter;
|
||||
|
||||
|
|
@ -38,7 +37,7 @@ namespace OpenTelemetry.Metrics.Configuration
|
|||
this.metricExporter = meterBuilder.MetricExporter ?? new NoOpMetricExporter();
|
||||
|
||||
// We only have PushMetricController now with only configurable thing being the push interval
|
||||
this.pushMetricController = new PushMetricController(
|
||||
this.PushMetricController = new PushMetricController(
|
||||
this.meterRegistry,
|
||||
this.metricProcessor,
|
||||
this.metricExporter,
|
||||
|
|
@ -49,6 +48,8 @@ namespace OpenTelemetry.Metrics.Configuration
|
|||
this.meterRegistry.Add(new MeterRegistryKey(string.Empty, null), this.defaultMeter);
|
||||
}
|
||||
|
||||
internal PushMetricController PushMetricController { get; }
|
||||
|
||||
public static MeterFactory Create(Action<MeterBuilder> configure)
|
||||
{
|
||||
if (configure == null)
|
||||
|
|
|
|||
|
|
@ -47,39 +47,47 @@ namespace OpenTelemetry.Metrics.Export
|
|||
s => this.Worker((CancellationToken)s), cts.Token);
|
||||
}
|
||||
|
||||
internal IEnumerable<Metric> Collect(Stopwatch sw)
|
||||
{
|
||||
foreach (var meter in this.meters.Values)
|
||||
{
|
||||
meter.Collect();
|
||||
}
|
||||
|
||||
OpenTelemetrySdkEventSource.Log.CollectionCompleted(sw.ElapsedMilliseconds);
|
||||
|
||||
// Collection is over at this point. All metrics are given
|
||||
// to the MetricProcesor(Batcher).
|
||||
// Let MetricProcessor know that this cycle is ending,
|
||||
// and send the metrics from MetricProcessor
|
||||
// to the MetricExporter.
|
||||
this.metricProcessor.FinishCollectionCycle(out var metricToExport);
|
||||
return metricToExport;
|
||||
}
|
||||
|
||||
internal async Task ExportAsync(IEnumerable<Metric> metricToExport, CancellationToken cancellationToken)
|
||||
{
|
||||
var exportResult = await this.metricExporter.ExportAsync(metricToExport, cancellationToken);
|
||||
if (exportResult != MetricExporter.ExportResult.Success)
|
||||
{
|
||||
OpenTelemetrySdkEventSource.Log.MetricExporterErrorResult((int)exportResult);
|
||||
|
||||
// we do not support retries for now and leave it up to exporter
|
||||
// as only exporter implementation knows how to retry: which items failed
|
||||
// and what is the reasonable policy for that exporter.
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Worker(CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Metric> metricToExport;
|
||||
|
||||
await Task.Delay(this.pushInterval, cancellationToken).ConfigureAwait(false);
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
foreach (var meter in this.meters.Values)
|
||||
{
|
||||
meter.Collect();
|
||||
}
|
||||
|
||||
OpenTelemetrySdkEventSource.Log.CollectionCompleted(sw.ElapsedMilliseconds);
|
||||
|
||||
// Collection is over at this point. All metrics are given
|
||||
// to the MetricProcesor(Batcher).
|
||||
// Let MetricProcessor know that this cycle is ending,
|
||||
// and send the metrics from MetricProcessor
|
||||
// to the MetricExporter.
|
||||
this.metricProcessor.FinishCollectionCycle(out metricToExport);
|
||||
|
||||
var exportResult = await this.metricExporter.ExportAsync(metricToExport, cancellationToken);
|
||||
if (exportResult != MetricExporter.ExportResult.Success)
|
||||
{
|
||||
OpenTelemetrySdkEventSource.Log.MetricExporterErrorResult((int)exportResult);
|
||||
|
||||
// we do not support retries for now and leave it up to exporter
|
||||
// as only exporter implementation knows how to retry: which items failed
|
||||
// and what is the reasonable policy for that exporter.
|
||||
}
|
||||
var metricToExport = this.Collect(sw);
|
||||
await this.ExportAsync(metricToExport, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
// <copyright file="CounterCleanUpTests.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.Diagnostics;
|
||||
using System.Linq;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Metrics.Configuration;
|
||||
using OpenTelemetry.Metrics.Export;
|
||||
using OpenTelemetry.Metrics.Test;
|
||||
using OpenTelemetry.Trace;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenTelemetry.Configuration.Tests
|
||||
{
|
||||
public class MeterFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void DefaultMeterShouldBeCollectedAsWell()
|
||||
{
|
||||
var testProcessor = new TestMetricProcessor();
|
||||
var factory = MeterFactory.Create(mb => mb.SetMetricProcessor(testProcessor));
|
||||
var controller = factory.PushMetricController;
|
||||
var defaultMeter = factory.GetMeter(string.Empty) as MeterSdk;
|
||||
|
||||
// Record some metrics using default meter
|
||||
var testCounter = defaultMeter.CreateInt64Counter("testCounter");
|
||||
var context = default(SpanContext);
|
||||
var labels = LabelSet.BlankLabelSet;
|
||||
testCounter.Add(context, 100, labels);
|
||||
testCounter.Add(context, 10, labels);
|
||||
|
||||
// Collect using PushMetricController
|
||||
var sw = Stopwatch.StartNew();
|
||||
var metricToExport = controller.Collect(sw).ToList();
|
||||
|
||||
Assert.Single(metricToExport);
|
||||
Assert.Single(metricToExport[0].Data);
|
||||
Assert.Equal(110, (metricToExport[0].Data[0] as Int64SumData).Sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue