[bug] Disposing a Meter causes all Meters with the same name to stop working
Package
OpenTelemetry
Package Version
| Package Name | Version |
|---|---|
| OpenTelemetry | 1.12.0 |
Runtime Version
net8.0
Description
In my service I was creating and disposing Meters with the same name and I found that disposing one would cause all of the Meters with the same name would stop working.
Steps to Reproduce
This small program reproduces the issue:
// See https://aka.ms/new-console-template for more information
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using System.Diagnostics.Metrics;
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
MyMetrics m1 = new MyMetrics("one");
MyMetrics m2 = new MyMetrics("two");
var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("foobar")
.AddConsoleExporter((exporterOptions, readerOptions) =>
{
readerOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 2000;
})
.Build();
int m2i = 0;
for (int i = 0; i < 1000; i++)
{
m1.Counter.Add(1, new KeyValuePair<string, object?>("color", "red"));
m2.Counter.Add(1, new KeyValuePair<string, object?>("color", "blue" + m2i));
if (i == 5)
{
Console.WriteLine("======================== Disposing m2!");
m2.Dispose();
}
Thread.Sleep(1000);
}
}
}
class MyMetrics : IDisposable
{
readonly Meter meter1;
public MyMetrics(string tagValue)
{
meter1 = new("foobar");
Counter = meter1.CreateCounter<int>("yay");
}
public Counter<int> Counter { get; }
public void Dispose()
{
((IDisposable)meter1).Dispose();
}
}
Expected Result
Disposing a Meter should only stop emitting metrics for the instruments created from it. Other Meters and their instruments should continue working, even if they have the same name.
Actual Result
All metrics from Meters with the name as the disposed Meter stop emitting.
Additional Context
No response
I'm not sure whether this is an issue or not ( or whether it's for us, as Meter is a .NET type rather than something belonging to OpenTelemetry either), but I'm curious as to why you're:
- disposing the meters, other than on application shutdown;
- have duplicate meters with the same name.
My understanding is that a Meter of a given name is effectively intended to be a singleton for the lifetime of a process that should be shared by all code intending in measure with it.
We are disposing Meters because that is the only way to unregister ObservableGauges so they stop being collected.
Our service manages a few worker tasks internally that it will spin up and down as there is work. Each worker creates its own Meter and then disposes the Meter when it spins down to unregister the ObservableGauges.
I see now in the Best Practices that it says the names should be unique. I'm going to update my code to follow the best practices. However, I'm not sure if "best practice" means "requirement" so it's not clear to me whether it should still work if you make multiple Meters with the same name.
I could see an argument for supporting this just because nothing in .NET prevents it and it seems to work if you don't Dispose() the Meter.