View Source PrometheusExometer.Metrics (prometheus_exometer v0.3.1)

Interface functions to update Exometer metrics with labels.

The underlying Exometer API to set values is simple. It doesn't change depending on the type of the metric, you can call the update/3 function for everything. This function is a simple wrapper which calls combine_name_labels/2 on its arguments, then calls :exometer.update_or_create/2. Calling e.g. inc/3 if you are incrementing a counter makes the intent clearer, though.

There are convenience functions to help with duration calculations as well.

This interface largely implements the recommended public API for Prometheus client libraries

Differences with the Prometheus API recommendations:

Erlang handles times in microseconds internally, so that's what we store, and that's the default output unit instead of seconds.

Summary

Functions

Combine base metric name with labels.

Decrement counter or gauge metric.

Ensure that a metric exists with the specified keyword under the parent metric

Get Exometer metric value

Get Exometer metric value with labels

Increment counter or gauge metric.

Observe current value for histogram or summary.

Observe time difference in ms between starting time and current time.

Set gauge to specified value.

Set metric to the difference between the specified timestamp and the current time in ms.

Set metric to the current Unix time in seconds.

Record duration in ms of a function call, like Erlang :timer.tc/3

Update Exometer metric with labels

Types

@type error() :: {:error, any()}
@type labels() :: Keyword.t()
@type name() :: :exometer.name()
@type value() :: any()

Functions

Link to this function

combine_name_labels(name, labels)

View Source
@spec combine_name_labels(name(), labels()) :: name()

Combine base metric name with labels.

@spec dec(name()) :: :ok
@spec dec(name(), labels() | value()) :: :ok
Link to this function

dec(name, labels, value)

View Source
@spec dec(name(), labels(), value()) :: :ok

Decrement counter or gauge metric.

Returns :ok.

Examples

iex> PrometheusExometer.Metrics.dec([:active_requests])
:ok
Link to this function

ensure_child(name, labels)

View Source
@spec ensure_child(name(), labels()) :: :ok | error()

Ensure that a metric exists with the specified keyword under the parent metric

@spec get_value(name()) :: {:ok, any()} | {:error, :not_found}

Get Exometer metric value

@spec get_value(name(), labels()) :: {:ok, any()} | {:error, :not_found}

Get Exometer metric value with labels

@spec inc(name()) :: :ok
@spec inc(name(), labels() | value()) :: :ok
Link to this function

inc(name, labels, value)

View Source
@spec inc(name(), labels(), value()) :: :ok

Increment counter or gauge metric.

Returns :ok.

Examples

Increment simple requests metric by one:

iex> PrometheusExometer.Metrics.inc([:requests])
:ok
@spec observe(name(), value()) :: :ok | error()
Link to this function

observe(name, labels, value)

View Source
@spec observe(name(), labels(), number()) :: :ok | error()

Observe current value for histogram or summary.

Link to this function

observe_duration(name, start_time)

View Source
@spec observe_duration(name(), :erlang.timestamp()) :: :ok | error()

Observe time difference in ms between starting time and current time.

Link to this function

observe_duration(name, labels, start_time)

View Source
@spec observe_duration(name(), labels(), :erlang.timestamp()) :: :ok | error()
@spec set(name(), float()) :: :ok
Link to this function

set(name, labels, value)

View Source
@spec set(list(), labels(), float()) :: :ok

Set gauge to specified value.

Returns :ok.

Examples

iex> PrometheusExometer.Metrics.set([:records], 1000)
:ok
Link to this function

set_duration(name, start_time)

View Source
@spec set_duration(name(), :erlang.timestamp()) :: :ok

Set metric to the difference between the specified timestamp and the current time in ms.

Returns :ok.

Examples

start_time = :os.timestamp()
do_some_work()
PrometheusExometer.Metrics.set_duration([:duration], start_time)
Link to this function

set_duration(name, labels, start_time)

View Source
@spec set_duration(name(), labels(), :erlang.timestamp()) :: :ok
Link to this function

set_to_current_time(name)

View Source
@spec set_to_current_time(name()) :: :ok
Link to this function

set_to_current_time(name, labels)

View Source
@spec set_to_current_time(name(), labels()) :: :ok

Set metric to the current Unix time in seconds.

Link to this function

tc(name, mod, fun, args)

View Source
@spec tc(name(), module(), atom(), list()) :: any()

Record duration in ms of a function call, like Erlang :timer.tc/3

Link to this function

tc(name, labels, mod, fun, args)

View Source
@spec tc(name(), labels(), module(), atom(), list()) :: any()
Link to this function

update(name, tuple_value)

View Source
@spec update(name(), value()) :: :ok | error()
Link to this function

update(name, labels, value)

View Source
@spec update(name(), labels(), value()) :: :ok | error()

Update Exometer metric with labels