open-telemetry / open-telemetry/opentelemetry-python

Support for gauge/callback shutdown in python

Open
#4,120 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request metrics
Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
4d 15h
Merged PRs (30d)
19

Description

Is your feature request related to a problem?

Discussed in cpp api:#1836, #2232
Current python implementation do not support close callback or gauge from api endpoint. While Java version has suppoerted this feature:

//create gauge
ObservableDoubleGauge gauge = gaugeBuilder
                    .setDescription("gauge example")
                    .buildWithCallback(result -> result.record(valueHolder, attr));

//remove gauge
guage.close()

When I register a corresponding gauge to the SDK, my application needs to run for a long time and may register multiple different instruments. I use local caches like Guava and Caffeine to store objects used to process metrics. When these objects reach a certain size, they are evicted from memory. However, when I report a metric with the same name again, it prompts that a duplicate instrument has been created, causing the report to fail. The original callback monitoring the valueHolder is no longer accessible by the API due to the object being released, leading to a memory leak issue.

In python, it outputs warning:

[WARNING][None][/otel-auto-instrumentation-python/opentelemetry/sdk/metrics/_internal/__init__.py:274][create_observable_gauge()][msg] An instrument with name avg-metric-python, type ObservableGauge, unit 1 and description gauge reporter: avg-metric-python has been created already.

I notice that the validation of duplicate metric is different in java and python, and seems that python api doesn't supply a interface like "unregister_callback", .

Describe the solution you'd like

solution 1:
Can use close() interface to close instrument.
solution2:
Allow to re-link the new observed value to the previous created instrument

Describe alternatives you've considered

Actually, register instrument process in both meter and measurement_consumer module.

Additional Context

No response

Would you like to implement a fix?

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at create_observable_gauge() in opentelemetry/sdk/metrics/_internal/init.py, then trace instrument registration through the meter and measurement_consumer modules mentioned in the issue. Compare the requested lifecycle with the Java close behavior and determine whether shutdown or callback replacement is the intended API; done means the chosen behavior prevents duplicate instruments and releases the obsolete callback.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.