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

No output if callback function ObserverResultT does not match ObservableInstrument primitive .

Open aaaugustine29 opened this issue 1 year ago • 2 comments

There is an "issue" to where if the primitive used in the instrument is not the same as the primitive used in the observer result template, when observe is called, there is no output. I was unable to trace exactly where there should be an exception or something similar. You can reproduce the error using the lines below. Please notice that the instrument is of type double, while the observer result is of type long (int64).

for instance, if we have the instrument: static auto ramObs = meter2->CreateDoubleObservableGauge("RAM", "Free RAM", "MB");

And the in the callback function, we have the line: opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<opentelemetry::metrics::ObserverResultT>>(observer_result)->Observe(value_, labels);

Then there is no output. My thought would be to have a comparison between the instrument primitive and the observer result primitive, but I cannot find a way to do so without changing any function signatures since the instrument has no knowledge of the workings of the callback function.

I'm happy to try and resolve this myself but can't seem to find a way that does not majorly change any of the existing code to where portability and compatibility would be maintained. Thanks in advance!

aaaugustine29 avatar Jun 05 '24 20:06 aaaugustine29

And the in the callback function, we have the line: opentelemetry::nostd::get<opentelemetry::nostd::shared_ptropentelemetry::metrics::ObserverResultT>(observer_result)->Observe(value_, labels);

There is no such thing as a opentelemetry::nostd::shared_ptr<opentelemetry::metrics::ObserverResultT>

ObserverResultT is a template, and it is used with:

  • either ObserverResultT<int64_t>
  • or ObserverResultT<double>

In the measurement callback, the code gets a variant:

using ObserverResult = nostd::variant<nostd::shared_ptr<ObserverResultT<int64_t>>,
                                      nostd::shared_ptr<ObserverResultT<double>>>;

so when calling opentelemetry::nostd::get<xxx>, the callback only gets:

  • a pointer on a double result if the instrument is of type double
  • a pointer on an int result if the instrument is of type int.

Make sure to test for holds_alternative before calling get, or use get_if. This will detect mismatch between the instrument type and the callback logic.

A callback function MUST match the instrument type. Consider defining different callback functions for different types, and register the proper callback with an instrument.

If it helps, the caller can also provide arbitrary state in the state parameter when registering the callback. The callback can then use the state passed to it to guide the logic.

If the problem still persists, please paste the relevant code from the instrumentation registration and the callback implementation, to clarify exactly what was attempted.

marcalff avatar Jun 25 '24 21:06 marcalff

This issue was marked as stale due to lack of activity.

github-actions[bot] avatar Aug 25 '24 01:08 github-actions[bot]