Packages
prometheus
3.0.0-alpha7
6.1.3
6.1.2
6.1.1
6.1.0
6.0.3
6.0.2
6.0.1
6.0.0
5.1.1
5.1.0
5.0.0
4.13.0
retired
4.12.0
4.11.0
4.10.0
4.9.1
4.9.0
4.8.2
4.8.1
4.8.0
4.6.0
4.5.0
4.4.1
4.4.0
4.3.0
4.2.2
4.2.0
4.1.0
4.0.1
4.0.0
3.5.1
3.5.0
3.4.6
3.4.5
3.4.4
3.4.3
3.4.2
3.4.1
3.4.0
3.3.2
3.3.1
3.3.0
3.2.3
3.2.2
3.2.1
3.1.1
3.1.0
3.0.1
3.0.0
3.0.0-rc1
3.0.0-alpha9
3.0.0-alpha8
3.0.0-alpha7
3.0.0-alpha6
3.0.0-alpha5
3.0.0-alpha4
3.0.0-alpha3
3.0.0-alpha2
3.0.0-alpha10
3.0.0-alpha1
2.2.0
2.1.0
2.0.0
1.7.0
1.6.0
1.5.0
1.0.2
1.0.1
1.0.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Prometheus.io client in Erlang
Current section
Files
Jump to
Current section
Files
src/prometheus_metric.erl
-module(prometheus_metric).
-export([insert_new_mf/3,
insert_mf/3,
deregister_mf/2,
check_mf_exists/4,
mf_call_timeout/1,
mf_duration_unit/1,
mf_data/1,
metrics/2,
remove_labels/4]).
-export_type([name/0,
help/0,
value/0,
duration_unit/0,
call_enabled/0]).
-include("prometheus.hrl").
%%====================================================================
%% Types
%%====================================================================
-type name() :: atom() | binary() | nonempty_string().
-type help() :: binary() | nonempty_string().
-type duration_unit() :: microseconds |
milliseconds |
seconds |
minutes |
days.
-type call_enabled() :: boolean().
-type counter_value() :: number().
-type gauge_value() :: number().
-type summary_value() :: {Count :: number(), Sum :: number()}.
-type histogram_value() :: {Buckets :: [number(), ...], Sum :: number()}.
-type value() :: counter_value()
| gauge_value()
| summary_value()
| histogram_value()
| undefined.
%%====================================================================
%% Callbacks
%%====================================================================
-callback new(Spec :: prometheus_metric_spec:spec()) -> ok.
-callback declare(Spec :: prometheus_metric_spec:spec()) -> boolean().
-callback remove(Name :: name()) -> boolean() | no_return().
-callback remove(Name :: name(), LValues :: list()) -> boolean() | no_return().
-callback remove(Registry, Name, LValues) -> boolean() | no_return() when
Registry :: prometheus_registry:registry(),
Name :: name(),
LValues :: list().
-callback reset(Name :: name()) -> boolean() | no_return().
-callback reset(Name :: name(), LValues :: list()) -> boolean() | no_return().
-callback reset(Registry, Name, LValues) -> boolean() | no_return() when
Registry :: prometheus_registry:registry(),
Name :: name(),
LValues :: list().
-callback value(Name :: name()) -> value() | no_return().
-callback value(Name :: name(), LValues :: list()) -> value() | no_return().
-callback value(Registry, Name, LValues) -> value() | no_return() when
Registry :: prometheus_registry:registry(),
Name :: name(),
LValues :: list().
%%====================================================================
%% Public API
%%====================================================================
%% @private
insert_new_mf(Table, Module, Spec) ->
case insert_mf(Table, Module, Spec) of
true ->
ok;
false ->
Registry = prometheus_metric_spec:registry(Spec),
Name = prometheus_metric_spec:name(Spec),
erlang:error({mf_already_exists, {Registry, Name},
"Consider using declare instead."})
end.
%% @private
insert_mf(Table, Module, Spec) ->
{Registry, Name, Labels, Help, UseCall, DurationUnit, Data} =
prometheus_metric_spec:extract_common_params(Spec),
prometheus_registry:register_collector(Registry, Module),
ets:insert_new(Table, {{Registry, mf, Name},
{Labels, Help},
UseCall,
DurationUnit,
Data}).
%% @private
deregister_mf(Table, Registry) ->
ets:match_delete(Table, {{Registry, mf, '_'},
'_',
'_',
'_',
'_'}).
%% @private
check_mf_exists(Table, Registry, Name, LabelValues) ->
case ets:lookup(Table, {Registry, mf, Name}) of
[] ->
erlang:error({unknown_metric, Registry, Name});
[{_, {Labels, _}, _, _, _} = MF] ->
LVLength = length(LabelValues),
case length(Labels) of
LVLength ->
MF;
LabelsLength ->
erlang:error({invalid_metric_arity, LVLength, LabelsLength})
end
end.
%% @private
mf_data(MF) ->
element(5, MF).
mf_call_timeout(MF) ->
element(3, MF).
mf_duration_unit(MF) ->
element(4, MF).
%% @private
metrics(Table, Registry) ->
ets:match(Table, {{Registry, mf, '$1'}, '$2', '$3', '$4', '$5'}).
%%====================================================================
%% Private Parts
%%===================================================================
-spec remove_labels(Table, Registry, Name, LValues) ->
boolean() | no_return() when
Table :: atom(),
Registry :: prometheus_registry:registry(),
Name :: name(),
LValues :: list().
remove_labels(Table, Registry, Name, LabelValues) ->
check_mf_exists(Table, Registry, Name, LabelValues),
case ets:take(Table, {Registry, Name, LabelValues}) of
[] -> false;
_ -> true
end.