open-telemetry / opentelemetry-rust

The Rust OpenTelemetry implementation
https://opentelemetry.io
Apache License 2.0
1.9k stars 445 forks source link

[Feature]: Multiple instrument callbacks #2151

Open lh123 opened 2 months ago

lh123 commented 2 months ago

Related Problems?

No response

Describe the solution you'd like:

Currently, when there are multiple AsyncInstrument, each metric has its own independent with_callback to set an update callback. I would like to be able to update multiple related metrics in a single callback. For example, when retrieving Linux memory information, we typically only need to read from /proc once to get both total memory and free memory. However, currently, I have to register separate callbacks for total memory and free memory, which leads to multiple reads from /proc.

OpenTelemetry Java provides a batchCallback function to achieve this functionality, and I hope a similar interface can be available in Rust as well.

See: batchCallback

See: multiple-instrument-callbacks

Considered Alternatives

No response

Additional Context

No response

cijothomas commented 2 months ago

This capability was intentionally removed here to keep 1.0 scope minimal. We can consider adding this post 1.0, as this would be a additive change.

cijothomas commented 1 month ago

@stormshield-fabs Also raised interest in making this happen.

cijothomas commented 1 month ago

// call an expensive API to get some values. // let value = expensive_api_call(); // observer.observer(value); // call an expensive API to get some values and cache it // if cache is old, call expensive API again. // { // let cache = expensive_api_call(); // } // observer.observer(cache);

One suggested alternate. Not as elegant.

Consider reprioritizing this for 1.0 itself given there are many asks.