Skip to content

Commit

Permalink
Disallow multiple metric streams of same name. (#2392)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Sep 21, 2021
1 parent 29110dc commit a538016
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
32 changes: 22 additions & 10 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal sealed class MeterProviderSdk : MeterProvider
private readonly Metric[] metrics;
private readonly List<object> instrumentations = new List<object>();
private readonly object collectLock = new object();
private readonly object instrumentCreationLock = new object();
private readonly Dictionary<string, bool> metricStreamNames = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
private readonly MeterListener listener;
private readonly MetricReader reader;
private int metricIndex = -1;
Expand Down Expand Up @@ -94,16 +96,26 @@ internal MeterProviderSdk(
{
if (meterSourcesToSubscribe.ContainsKey(instrument.Meter.Name))
{
var index = Interlocked.Increment(ref this.metricIndex);
if (index >= MaxMetrics)
lock (this.instrumentCreationLock)
{
// Log that all measurements are dropped from this instrument.
}
else
{
var metric = new Metric(instrument, temporality);
this.metrics[index] = metric;
listener.EnableMeasurementEvents(instrument, metric);
if (this.metricStreamNames.ContainsKey(instrument.Name))
{
// log and ignore this instrument.
return;
}

var index = Interlocked.Increment(ref this.metricIndex);
if (index >= MaxMetrics)
{
// Log that all measurements are dropped from this instrument.
}
else
{
var metric = new Metric(instrument, temporality);
this.metrics[index] = metric;
this.metricStreamNames.Add(instrument.Name, true);
listener.EnableMeasurementEvents(instrument, metric);
}
}
}
},
Expand Down Expand Up @@ -173,7 +185,7 @@ internal Batch<Metric> Collect()
this.metrics[i].SnapShot();
}

return (target > 0) ? new Batch<Metric>(this.metrics, indexSnapShot + 1) : default;
return (target > 0) ? new Batch<Metric>(this.metrics, target) : default;
}
catch (Exception)
{
Expand Down
10 changes: 5 additions & 5 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void ProcessExport(Batch<Metric> batch)
PreferredAggregationTemporality = exportDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative,
};

var meter = new Meter("TestMeter");
using var meter = new Meter("TestMeter");
var counterLong = meter.CreateCounter<long>("mycounter");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestMeter")
Expand Down Expand Up @@ -135,7 +135,7 @@ void ProcessExport(Batch<Metric> batch)
PreferredAggregationTemporality = exportDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative,
};

var meter = new Meter(meterName);
using var meter = new Meter(meterName);
int i = 1;
var counterLong = meter.CreateObservableCounter<long>(
"observable-counter",
Expand Down Expand Up @@ -204,7 +204,7 @@ void ProcessExport(Batch<Metric> batch)
{
PreferredAggregationTemporality = temporality,
};
var meter = new Meter("TestPointCapMeter");
using var meter = new Meter("TestPointCapMeter");
var counterLong = meter.CreateCounter<long>("mycounterCapTest");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestPointCapMeter")
Expand Down Expand Up @@ -256,7 +256,7 @@ void ProcessExport(Batch<Metric> batch)
PreferredAggregationTemporality = AggregationTemporality.Cumulative,
};

var meter = new Meter("TestLongCounterMeter");
using var meter = new Meter("TestLongCounterMeter");
var counterLong = meter.CreateCounter<long>("mycounter");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestLongCounterMeter")
Expand Down Expand Up @@ -326,7 +326,7 @@ void ProcessExport(Batch<Metric> batch)
PreferredAggregationTemporality = AggregationTemporality.Cumulative,
};

var meter = new Meter("TestDoubleCounterMeter");
using var meter = new Meter("TestDoubleCounterMeter");
var counterDouble = meter.CreateCounter<double>("mycounter");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestDoubleCounterMeter")
Expand Down

0 comments on commit a538016

Please sign in to comment.