Writing Client Libraries
Conventions
The common use cases are (in order):
Counters
without labels spread liberally around libraries/applications.- Timing functions/blocks of code in
Summaries/Histograms
. Gauges
to track current states of things (and their limits).- Monitoring of batch jobs
Overall structure
Clients MUST be written to be callback based internally
The key class is the Collector
. This has a method (typically called collect
) that returns zero or more metrics and their samples. Collectors get registered with a CollectorRegistry
. Data is exposed by passing a CollectorRegistry to a class/method/function "bridge", which returns the metrics in a format Prometheus supports. Every time the CollectorRegistry is scraped it must callback to each of the Collectors’ collect method
The interface most users interact with are the Counter, Gauge, Summary, and Histogram Collectors
. These represent a single metric, and should cover the vast majority of use cases where a user is instrumenting their own code
CollectorRegistry
SHOULD offer register()/unregister()
functions, and a Collector SHOULD be allowed to be registered to multiple CollectorRegistrys.
Client libraries MUST be thread safe
Metrics
The Counter
, Gauge
, Summary and Histogram
metric types are the primary interface by users.
These should be primarily used as file-static variables, that is, global variables defined in the same file as the code they’re instrumenting
There MUST be a default CollectorRegistry, the standard metrics MUST by default implicitly register into it with no special work required by the user. There MUST be a way to have metrics not register to the default CollectorRegistry, for use in batch jobs and unittests