Skip to content

Latest commit

 

History

History
129 lines (83 loc) · 4.04 KB

prometheus_collector.md

File metadata and controls

129 lines (83 loc) · 4.04 KB

Module prometheus_collector

A collector for a set of metrics.

This module defines the prometheus_collector behaviour.
Required callback functions: collect_mf/2, deregister_cleanup/1.

Description

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).

Callbacks:

  • collect_mf(Registry, Callback) - called by exporters and formats. Should call Callback for each MetricFamily of this collector;
  • collect_metrics(Name, Data) - called by MetricFamily constructor. Should return Metric list for each MetricFamily identified by Name. Data is a term associated with MetricFamily by collect_mf.
  • deregister_cleanup(Registry) - called when collector unregistered by Registry. If collector is stateful you can put cleanup code here.

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).

Data Types


collect_mf_callback() = fun((prometheus_model:'MetricFamily'()) -> any())

collector() = atom()

data() = any()

Function Index

collect_mf/3Calls Callback for each MetricFamily of this collector.

Function Details

collect_mf/3


collect_mf(Registry, Collector, Callback) -> ok

Calls Callback for each MetricFamily of this collector.