Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metrics API/SDK] ASync instruments - pass state to the callback #1389

Closed
marcalff opened this issue May 11, 2022 · 1 comment · Fixed by #1408
Closed

[Metrics API/SDK] ASync instruments - pass state to the callback #1389

marcalff opened this issue May 11, 2022 · 1 comment · Fixed by #1408
Assignees
Labels
bug Something isn't working

Comments

@marcalff
Copy link
Member

Greetings.

From the spec:

https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#asynchronous-counter-creation

The API SHOULD provide some way to pass state to the callback. OpenTelemetry API authors MAY decide what is the idiomatic approach (e.g. it could be an additional parameter to the callback function, or captured by the lambda closure, or something else).

For async counters creation, please change:

  • the callback functions to add a void * state parameter,
  • the create observable counter methods to pass a void * state parameter
    so this gets easier to use:
long counter_foo;
long counter_bar;
long counter_baz;

void generic_callback(void *state,
                      opentelemetry::metrics::ObserverResult<long> &obs) {
  long *counter = static_cast<long *>(state);
  obs.Observe(*counter);
}

void register_my_counter(const char *name, long *counter) {
  void *state = counter;
  g_otel_meter->CreateLongObservableCounter(name, generic_callback, state, "",
                                            "");
}

register_my_counter("foo", &counter_foo);
register_my_counter("bar", &counter_bar);
register_my_counter("baz", &counter_baz);

Without it, the user code would have to define a different callback function for each observable counter,
which is not only inconvenient, but assumes the list of counters is static and known at compile time (it's not).

Regards.

@marcalff marcalff added the bug Something isn't working label May 11, 2022
@lalitb lalitb self-assigned this May 11, 2022
@lalitb
Copy link
Member

lalitb commented May 12, 2022

Thanks for raising the issue. I am moving it to Beta Release milestone as there is alternative solution (though this require multiple callback functions). Alpha release is very close now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants