View - support for dropping instrument (#2449)

This commit is contained in:
Cijo Thomas 2021-10-05 16:07:23 -07:00 committed by GitHub
parent 1aa4da2098
commit f70dfd378a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 129 additions and 5 deletions

View File

@ -34,7 +34,7 @@ public class Program
.AddView(instrumentName: "MyCounter", name: "MyCounterRenamed")
// Change Histogram bounds
.AddView(instrumentName: "MyHistogram", new HistogramConfiguration() { BucketBounds = new double[] { 10, 20 }, Aggregation = Aggregation.LastValue })
.AddView(instrumentName: "MyHistogram", new HistogramConfiguration() { BucketBounds = new double[] { 10, 20 } })
// For the instrument "MyCounterCustomTags", aggregate with only the keys "tag1", "tag2".
.AddView(instrumentName: "MyCounterCustomTags", new MetricStreamConfiguration() { TagKeys = new string[] { "tag1", "tag2" } })

View File

@ -0,0 +1,35 @@
// <copyright file="DropConfiguration.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
{
internal class DropConfiguration : MetricStreamConfiguration
{
private Aggregation aggregation = Aggregation.Drop;
public override Aggregation Aggregation
{
get => this.aggregation;
set
{
throw new ArgumentException($"Aggregation cannot be set.");
}
}
}
}

View File

@ -66,14 +66,14 @@ namespace OpenTelemetry.Metrics
/// </summary>
/// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
/// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
/// <param name="aggregationConfig">Aggregation configuration used to produce metrics stream.</param>
/// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
/// <returns><see cref="MeterProvider"/>.</returns>
/// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration aggregationConfig)
public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
{
if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
{
return meterProviderBuilderBase.AddView(instrumentName, aggregationConfig);
return meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration);
}
return meterProviderBuilder;

View File

@ -145,6 +145,14 @@ namespace OpenTelemetry.Metrics
continue;
}
if (metricStreamConfig?.Aggregation == Aggregation.None)
{
// TODO: Log that instrument is ignored
// as user explicitly asked to drop it
// with View.
continue;
}
var index = ++this.metricIndex;
if (index >= MaxMetrics)
{

View File

@ -31,6 +31,8 @@ namespace OpenTelemetry.Metrics
public class MetricStreamConfiguration
{
public static readonly MetricStreamConfiguration Drop = new DropConfiguration();
public string Name { get; set; }
public string Description { get; set; }

View File

@ -15,7 +15,6 @@
// </copyright>
using System;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Threading;

View File

@ -292,6 +292,86 @@ namespace OpenTelemetry.Metrics.Tests
// Single point expected.
Assert.Single(metricPoints);
}
[Fact]
public void ViewToDropSingleInstrument()
{
using var meter = new Meter("ViewToDropSingleInstrumentTest");
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource(meter.Name)
.AddView("counterNotInteresting", new MetricStreamConfiguration() { Aggregation = Aggregation.Drop })
.AddInMemoryExporter(exportedItems)
.Build();
// Expecting one metric stream.
var counterInteresting = meter.CreateCounter<long>("counterInteresting");
var counterNotInteresting = meter.CreateCounter<long>("counterNotInteresting");
counterInteresting.Add(10);
counterNotInteresting.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Single(exportedItems);
var metric = exportedItems[0];
Assert.Equal("counterInteresting", metric.Name);
}
[Fact]
public void ViewToDropMultipleInstruments()
{
using var meter = new Meter("ViewToDropMultipleInstrumentsTest");
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource(meter.Name)
.AddView("server*", new MetricStreamConfiguration() { Aggregation = Aggregation.Drop })
.AddInMemoryExporter(exportedItems)
.Build();
// Expecting two client metric streams as both server* are dropped.
var serverRequests = meter.CreateCounter<long>("server.requests");
var serverExceptions = meter.CreateCounter<long>("server.exceptions");
var clientRequests = meter.CreateCounter<long>("client.requests");
var clientExceptions = meter.CreateCounter<long>("client.exceptions");
serverRequests.Add(10);
serverExceptions.Add(10);
clientRequests.Add(10);
clientExceptions.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Equal(2, exportedItems.Count);
Assert.Equal("client.requests", exportedItems[0].Name);
Assert.Equal("client.exceptions", exportedItems[1].Name);
}
[Fact]
public void ViewToDropAndRetainInstrument()
{
using var meter = new Meter("ViewToDropAndRetainInstrumentTest");
var exportedItems = new List<Metric>();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource(meter.Name)
.AddView("server.requests", MetricStreamConfiguration.Drop)
.AddView("server.requests", "server.request_renamed")
.AddInMemoryExporter(exportedItems)
.Build();
// Expecting one metric stream even though a View is asking
// to drop the instrument, because another View is matching
// the instrument, which asks to aggregate with defaults
// and a use a new name for the resulting metric.
var serverRequests = meter.CreateCounter<long>("server.requests");
serverRequests.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Single(exportedItems);
Assert.Equal("server.request_renamed", exportedItems[0].Name);
}
[Fact]
public void MetricStreamConfigurationForDropMustNotAllowOverriding()
{
Assert.Throws<ArgumentException>(() => MetricStreamConfiguration.Drop.Aggregation = Aggregation.Histogram);
}
}
#pragma warning restore SA1000 // KeywordsMustBeSpacedCorrectly
}