dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.17k stars 4.72k forks source link

Document metric Instrument lifetime or make instruments disposable #97061

Closed lmolkova closed 9 months ago

lmolkova commented 9 months ago

Apparently it's common to create metric instruments in webapi controller constructor (check https://msazure.visualstudio.com/One/_search?action=contents&text=CreateCounter%20file%3A*Controller&type=code) leading to memory leaks - counter instances are kept my MeterListener/otel and are never collected.

The lifetime of instruments does not seem to be documented and it given how common erroneous usage is, it should be worth documenting it, and/or making instruments disposable, or CreateCounter to cache instances.

lmolkova commented 9 months ago

Closing this one - the bug (memory leak) was fixed in the OpenTelemetry and no longer reproduces in 1.6.0+