opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

Repeated calls to get_tracer results in memory leak in 1.40.0

Open fpiche opened this issue 8 months ago • 11 comments

Hello!

After upgrading to 1.40.0, we began experiencing memory leaks in production. After investigation, it seems that 1.40.0 introduced TracerMetrics which is instantiated on every call to get_tracer(). When no MetricsProvider is configured, a proxy MetricsProvider is used which seems to accumulate a list indefinitely.

I confirmed this by profiling locally, there is an ever growing number of allocations under the get_tracer codepath.

Here's a screenshot of the relevant portion of the flamegraph:

Image

This issue does not exist in 1.39.1, and we reverted to that version while investigating.

For now I've worked around this by calling get_tracer once rather than on each request, but I figured you'd want to know in case this is an unintended regression. I also saw this mentioned in a comment on #5009

fpiche avatar Mar 27 '26 22:03 fpiche

If (for some reason) you don't use metrics from TracerProvider - you can configure TracerProvider with NoOpMeterProvider. This (kinda) solves the problem.

kapkekes avatar Mar 28 '26 12:03 kapkekes

Hi, I'd like to work on this.

I've investigated the root cause: TracerMetrics is instantiated inside Tracer.__init__, and since TracerProvider.get_tracer() creates a new Tracer on every call, each call triggers meter_provider.get_meter(). When no SDK MeterProvider is configured, the global ProxyMeterProvider appends every proxy meter to an internal list that is never freed — causing unbounded memory growth.

Proposed fix: add a _get_tracer_metrics() method to TracerProvider that lazily creates and caches a single TracerMetrics instance (using double-checked locking for thread safety), then pass it into Tracer.__init__ via a new optional tracer_metrics= parameter. This keeps TracerMetrics shared across all tracers from the same provider — which is correct, since it tracks provider-level span metrics, not per-tracer ones.

I've already written and locally verified the patch and regression tests (covering caching, lazy init, proxy list size, and 50-thread concurrent access). Happy to open a PR once assigned.

Could a maintainer please assign this to me? Thanks!

ravitheja4531-cell avatar Mar 28 '26 20:03 ravitheja4531-cell

@ravitheja4531-cell sure, feel free to open a PR

xrmx avatar Mar 30 '26 09:03 xrmx

I think this would also be fixed if we get this in, since the Tracer for the same instrumentation scope would be cached and reused https://github.com/open-telemetry/opentelemetry-python/pull/5007

xrmx avatar Mar 30 '26 14:03 xrmx

@fpiche get_tracer() is creating a new Tracer everytime you call it, did you expect things to be cached so that the call would be cheap?

xrmx avatar Mar 30 '26 15:03 xrmx

The main part that was surprising was that repeated "get" calls resulted in global state growing. I would expect to either return a cached state or to be returned a throwaway object that can be gc'ed later

fpiche avatar Mar 30 '26 16:03 fpiche

Hi @xrmx , I've opened a PR for this: #5027

I also saw your comment about #5007 potentially fixing this as well. My fix caches TracerMetrics on the provider side regardless of whether Tracer itself gets cached, so both fixes could complement each other or mine could be considered a standalone solution. Happy to hear your thoughts!

ravitheja4531-cell avatar Mar 31 '26 22:03 ravitheja4531-cell

@fpiche Any chance you can test latest git if things are improved?

xrmx avatar Apr 01 '26 07:04 xrmx

@cobusc ↑

xrmx avatar Apr 01 '26 15:04 xrmx

@xrmx It looks better, yes. Thanks!

cobusc avatar Apr 07 '26 06:04 cobusc

This should be handled properly in 1.41.0, closing.

xrmx avatar Apr 10 '26 08:04 xrmx