Skip to content

Return type of callback functions for Asynchronous Instruments #1732

Description

@jonatan-ivanov

Right now, the Metrics API Docs says:

The callback function is responsible for reporting the Measurements.

Measurements encapsulate two things:

  • A value (result of your measurement)
  • Attributes (dimensions)

This means that if the users have a function that provides a value (provided by a library/language sdk/etc.), they can't just simply use it but they have to wrap it in another function that enhances the original one and adds the attributes.
This can result in sub-optimal user experience and API usability by forcing the users to write a bunch of boilerplate.

Can we come up with a solution (additional functionality) to improve this so that the users can register a simple function that only provides the value and if they want define the attributes separately?

For example:

With "static" attributes:

void createObservableCounter(String name, Supplier<? extends Number> valueProvider, Attributes... attributes);

You can call it like this
(the sensor instance has a getTemperature method that returns a Double and it is registered as the callback):

meter.createObservableCounter("room.temperature", sensor::getTemperature, Attributes.of("room", "living-room"));

With "dynamic" attributes:

void createObservableCounter(String name, Supplier<T> valueProvider, Function<T, Iterable<Attributes>> attributesProvider);

You can call it like this (the sensor instance has a getTemperature method that returns a TemperatureResult instance that will be passed to the attributesProvider which will provide the Attributes based on the result):

meter.createObservableCounter("room.temperature", sensor::getTemperature, t -> Attributes.of("room", r.name()));

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiCross language API specification issuespec:metricsRelated to the specification/metrics directorytriage:deciding:community-feedbackOpen to community discussion. If the community can provide sufficient reasoning, it may be accepted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions