instrument_meter (instrument v0.6.1)

View Source

OpenTelemetry-compatible MeterProvider and Meter API.

This module provides an OTel-style API for creating and using metrics, backed by the existing NIF infrastructure.

Example Usage

  Meter = instrument_meter:get_meter(<<"my_service">>),
  Counter = instrument_meter:create_counter(Meter, <<"requests_total">>, #{
    description => <<"Total requests">>,
    unit => <<"1">>
  }),
  instrument_meter:add(Counter, 1, #{method => <<"GET">>, status => 200}).

Summary

Functions

Adds a value to a Counter or UpDownCounter.

Adds a value to a Counter or UpDownCounter with attributes.

Invokes all observable instrument callbacks. This should be called before metrics collection to update observable values.

Creates a Counter instrument.

Creates a Counter instrument with options.

Creates a Gauge instrument.

Creates a Gauge instrument with options.

Creates a Histogram instrument.

Creates a Histogram instrument with options.

Creates an ObservableCounter with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

Creates an ObservableGauge with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

Creates an ObservableUpDownCounter with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

Creates an UpDownCounter instrument.

Creates an UpDownCounter instrument with options.

Forces a flush of all pending metrics. This is a no-op in the current implementation.

Gets an instrument by name.

Gets or creates a Meter with the given name.

Gets or creates a Meter with the given name and options.

Lists all registered instruments.

Records a value in a Histogram.

Records a value in a Histogram with attributes.

Sets a value on a Gauge.

Sets a value on a Gauge with attributes.

Shuts down the MeterProvider. This is a no-op in the current implementation.

Unregisters all OTel instruments.

Unregisters an instrument by name. This removes the instrument from persistent_term, unregisters the underlying metric, and cleans up any associated vec metrics created for attributed operations.

Types

instrument/0

-type instrument() ::
          #otel_instrument{name :: binary(),
                           kind ::
                               counter | up_down_counter | histogram | gauge | observable_counter |
                               observable_gauge | observable_up_down_counter,
                           description :: binary() | undefined,
                           unit :: binary() | undefined,
                           meter ::
                               #meter{name :: binary(),
                                      version :: binary() | undefined,
                                      schema_url :: binary() | undefined,
                                      resource ::
                                          #resource{attributes :: map(),
                                                    schema_url :: binary() | undefined} |
                                          undefined} |
                               undefined,
                           handle :: term(),
                           temporality :: cumulative | delta}.

instrument_opts/0

-type instrument_opts() ::
          #{description => binary(),
            unit => binary(),
            boundaries => [number()],
            temporality => cumulative | delta}.

meter/0

-type meter() ::
          #meter{name :: binary(),
                 version :: binary() | undefined,
                 schema_url :: binary() | undefined,
                 resource ::
                     #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined}.

Functions

add(Instrument, Value)

-spec add(instrument(), number()) -> ok.

Adds a value to a Counter or UpDownCounter.

add(Otel_instrument, Value, Attrs)

-spec add(instrument(), number(), map()) -> ok.

Adds a value to a Counter or UpDownCounter with attributes.

collect_observables()

-spec collect_observables() -> ok.

Invokes all observable instrument callbacks. This should be called before metrics collection to update observable values.

create_counter(Meter, Name)

-spec create_counter(meter(), binary() | atom()) -> instrument().

Creates a Counter instrument.

create_counter(Meter, Name, Opts)

-spec create_counter(meter(), binary() | atom(), instrument_opts()) -> instrument().

Creates a Counter instrument with options.

create_gauge(Meter, Name)

-spec create_gauge(meter(), binary() | atom()) -> instrument().

Creates a Gauge instrument.

create_gauge(Meter, Name, Opts)

-spec create_gauge(meter(), binary() | atom(), instrument_opts()) -> instrument().

Creates a Gauge instrument with options.

create_histogram(Meter, Name)

-spec create_histogram(meter(), binary() | atom()) -> instrument().

Creates a Histogram instrument.

create_histogram(Meter, Name, Opts)

-spec create_histogram(meter(), binary() | atom(), instrument_opts()) -> instrument().

Creates a Histogram instrument with options.

create_observable_counter(Meter, Name, Callback)

-spec create_observable_counter(meter(), binary() | atom(), fun()) -> instrument().

Creates an ObservableCounter with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

create_observable_gauge(Meter, Name, Callback)

-spec create_observable_gauge(meter(), binary() | atom(), fun()) -> instrument().

Creates an ObservableGauge with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

create_observable_up_down_counter(Meter, Name, Callback)

-spec create_observable_up_down_counter(meter(), binary() | atom(), fun()) -> instrument().

Creates an ObservableUpDownCounter with a callback. Callback can be: - 0-arity: fun() -> number() - returns a single value - 1-arity: fun(Observe) -> ok - calls Observe(Value, Attrs) for each observation

create_up_down_counter(Meter, Name)

-spec create_up_down_counter(meter(), binary() | atom()) -> instrument().

Creates an UpDownCounter instrument.

create_up_down_counter(Meter, Name, Opts)

-spec create_up_down_counter(meter(), binary() | atom(), instrument_opts()) -> instrument().

Creates an UpDownCounter instrument with options.

force_flush()

-spec force_flush() -> ok.

Forces a flush of all pending metrics. This is a no-op in the current implementation.

get_instrument(Name)

-spec get_instrument(binary() | atom()) -> instrument() | undefined.

Gets an instrument by name.

get_meter(Name)

-spec get_meter(binary() | atom()) -> meter().

Gets or creates a Meter with the given name.

get_meter(Name, Opts)

-spec get_meter(binary() | atom(), map()) -> meter().

Gets or creates a Meter with the given name and options.

list_instruments()

-spec list_instruments() -> [instrument()].

Lists all registered instruments.

record(Instrument, Value)

-spec record(instrument(), number()) -> ok.

Records a value in a Histogram.

record(Otel_instrument, Value, Attrs)

-spec record(instrument(), number(), map()) -> ok.

Records a value in a Histogram with attributes.

set(Instrument, Value)

-spec set(instrument(), number()) -> ok.

Sets a value on a Gauge.

set(Otel_instrument, Value, Attrs)

-spec set(instrument(), number(), map()) -> ok.

Sets a value on a Gauge with attributes.

shutdown()

-spec shutdown() -> ok.

Shuts down the MeterProvider. This is a no-op in the current implementation.

unregister_all_instruments()

-spec unregister_all_instruments() -> ok.

Unregisters all OTel instruments.

unregister_instrument(Name)

-spec unregister_instrument(binary() | atom()) -> ok | {error, not_found}.

Unregisters an instrument by name. This removes the instrument from persistent_term, unregisters the underlying metric, and cleans up any associated vec metrics created for attributed operations.