open-telemetry / opentelemetry-cpp

The OpenTelemetry C++ Client
https://opentelemetry.io/
Apache License 2.0
811 stars 391 forks source link

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

Open aaaugustine29 opened 3 weeks ago

aaaugustine29 commented 3 weeks ago

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>>(observerresult)->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!

marcalff commented 5 days ago

And the in the callback function, we have the line: opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr>(observerresult)->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:

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:

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.