prometheus_collector behaviour (prometheus v5.1.1)
View SourceA collector for a set of metrics.
Normal users should use prometheus_gauge
, prometheus_counter
,
prometheus_summary
and prometheus_histogram
.
Implementing prometheus_collector
behaviour is for advanced uses such as proxying
metrics from another monitoring system. It is it the responsibility of the implementer
to ensure produced metrics are valid.
You will be working with Prometheus data model directly (see prometheus_model_helpers
).
Example (simplified prometheus_vm_memory_collector
):
-module(prometheus_vm_memory_collector).
-export([deregister_cleanup/1, collect_mf/2, collect_metrics/2]).
-behaviour(prometheus_collector).
%%====================================================================
%% Collector API
%%====================================================================
deregister_cleanup(_) -> ok.
collect_mf(_Registry, Callback) ->
Memory = erlang:memory(),
Callback(create_gauge(erlang_vm_bytes_total,
\"The total amount of memory currently allocated. \"
\"This is the same as the sum of the memory size \"
\"for processes and system.\",
Memory)),
ok.
collect_metrics(erlang_vm_bytes_total, Memory) ->
prometheus_model_helpers:gauge_metrics(
[
{[{kind, system}], proplists:get_value(system, Memory)},
{[{kind, processes}], proplists:get_value(processes, Memory)}
]).
%%====================================================================
%% Private Parts
%%====================================================================
create_gauge(Name, Help, Data) ->
prometheus_model_helpers:create_mf(Name, Help, gauge, ?MODULE, Data).
Summary
Callbacks
Should return Metric list for each MetricFamily identified by Name
.
Should call Callback
for each MetricFamily
of this collector
Called when collector is deregistered. If collector is stateful you can put cleanup code here
Functions
Calls Callback
for each MetricFamily of this collector.
Types
-type collect_mf_callback() :: fun((prometheus_model:'MetricFamily'()) -> dynamic()).
Callback for collect_mf/3
-type collector() :: atom().
Represents a Prometheus collector
-type data() :: dynamic().
Data associated with a collector
Callbacks
-callback collect_metrics(Name, Data) -> Metrics when Name :: prometheus_metric:name(), Data :: data(), Metrics :: prometheus_model:'Metric'() | [prometheus_model:'Metric'()].
Should return Metric list for each MetricFamily identified by Name
.
Data
is a term associated with MetricFamily by collect_mf/2
.
-callback collect_mf(Registry, Callback) -> ok when Registry :: prometheus_registry:registry(), Callback :: collect_mf_callback().
Should call Callback
for each MetricFamily
of this collector
-callback deregister_cleanup(Registry) -> ok when Registry :: prometheus_registry:registry().
Called when collector is deregistered. If collector is stateful you can put cleanup code here
Functions
-spec collect_mf(Registry, Collector, Callback) -> ok when Registry :: prometheus_registry:registry(), Collector :: collector(), Callback :: collect_mf_callback().
Calls Callback
for each MetricFamily of this collector.