libprom  @VERSION@
C based libraries to expose metrics in Promtheus exposition format
Defines | Typedefs | Enumerations | Functions | Variables
prom/include/prom_collector_registry.h File Reference

The collector registry registers collectors for metric exposition. More...

#include <stdbool.h>
#include "prom_collector.h"
#include "prom_metric.h"
Include dependency graph for prom_collector_registry.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define METRIC_LABEL_SCRAPE   "libprom"
 Reserved label value for libprom's own scrape duration metrics.
#define METRIC_NAME_SCRAPE   "scrape_duration_seconds"
 Reserved name for libprom's own scrape duration metric.
#define COLLECTOR_NAME_DEFAULT   "default"
 Reserved name for libprom's own default prom collector, where usually new metrics get attached.
#define COLLECTOR_NAME_PROCESS   "process"
 Reserved name for libprom's own process stats prom collector.
#define REGISTRY_NAME_DEFAULT   "default"
 Reserved name for libprom's own default prom collector registry.
#define PROM_COLLECTOR_REGISTRY_DEFAULT   PROM_COLLECTOR_REGISTRY
 backward compatibility to 0.1.3 - will vanish soon.

Typedefs

typedef unsigned int PROM_INIT_FLAGS
 collection of prom collector registry features.
typedef struct pcr pcr_t
 A prom_registry_t is responsible for registering metrics and briding them to the string exposition format.

Enumerations

enum  prom_init_flag {
  PROM_NONE = 0, PROM_PROCESS = 1, PROM_SCRAPETIME = 2, PROM_SCRAPETIME_ALL = 4,
  PROM_COMPACT = 8
}
 flags for the setup of a prom collector registry. More...

Functions

int pcr_default_init (void)
 Initializes the default collector registry.
int pcr_init (PROM_INIT_FLAGS features, const char *mprefix)
 Initializes the default collector registry PROM_COLLECTOR_REGISTRY named REGISTRY_NAME_DEFAULT.
pcr_tpcr_new (const char *name)
 Constructs a registry named default which contains one empty prom collector named default , only.
int pcr_destroy (pcr_t *self)
 Destroy the given collector registry.
int pcr_enable_process_metrics (pcr_t *self)
 Enable process metrics on the given collector registry.
int pcr_enable_scrape_metrics (pcr_t *self)
 Create a scrape duration gauge metric and attach it to the given prom collector registry.
prom_metric_tpcr_must_register_metric (prom_metric_t *metric)
 Registers a metric with the default collector on PROM_COLLECTOR_REGISTRY.
int pcr_register_metric (prom_metric_t *metric)
 Registers a metric with the default collector on PROM_COLLECTOR_REGISTRY.
int pcr_register_collector (pcr_t *self, prom_collector_t *collector)
 Register a collector with the given registry.
prom_collector_tpcr_get (pcr_t *self, const char *name)
 Get a reference to the prom collector with the given name from the given prom collector registry.
char * pcr_bridge (pcr_t *self)
 Export all relevant metrics registered with the given registry in the default metric exposition format as a single string.
int pcr_validate_metric_name (pcr_t *self, const char *metric_name)
 Validates that the given metric name complies with the specification:

Variables

pcr_tPROM_COLLECTOR_REGISTRY
 Initialize the default registry by calling pcr_init() within your app.

Detailed Description

The collector registry registers collectors for metric exposition.


Define Documentation

#define COLLECTOR_NAME_DEFAULT   "default"

Reserved name for libprom's own default prom collector, where usually new metrics get attached.

Note:
Do not use unless you know, what you are doing.
#define COLLECTOR_NAME_PROCESS   "process"

Reserved name for libprom's own process stats prom collector.

Note:
Do not use unless you know, what you are doing.
#define METRIC_LABEL_SCRAPE   "libprom"

Reserved label value for libprom's own scrape duration metrics.

Note:
Do not use unless you know, what you are doing.
#define METRIC_NAME_SCRAPE   "scrape_duration_seconds"

Reserved name for libprom's own scrape duration metric.

Note:
Do not use unless you know, what you are doing.

backward compatibility to 0.1.3 - will vanish soon.

#define REGISTRY_NAME_DEFAULT   "default"

Reserved name for libprom's own default prom collector registry.

Note:
Do not use unless you know, what you are doing.

Typedef Documentation

typedef unsigned int PROM_INIT_FLAGS

collection of prom collector registry features.

See also:
prom_init_flag

Enumeration Type Documentation

flags for the setup of a prom collector registry.

See also:
pcr_init()
Enumerator:
PROM_NONE 

placeholder for 0 - implies nothing

PROM_PROCESS 

Automatically setup and attach a process collector, which collects stats of this process on pcr_bridge() and appends its to the output.

PROM_SCRAPETIME 

Automatically create and use a metric to monitor the time needed to dump all the metrics of all registered collectors in Prometheus exposition format and include it in the dump as well.

The metric gets labeled as METRIC_LABEL_SCRAPE , the metric's implied name is METRIC_NAME_SCRAPE .

PROM_SCRAPETIME_ALL 

Implies PROM_SCRAPETIME, but measures and dumps the dump time of every single collector as well.

The related metrics get labeled with the name of the collector.

PROM_COMPACT 

Omit any "# HELP" and "# TYPE" lines in metric exports.

These are wrt. the Prometheus exposition format optional and e.g. Victoria-Metrics vmagent as well as timeseries DB ignore them completely because simply not needed. So allows less trash and communication overhead.


Function Documentation

char* pcr_bridge ( pcr_t self)

Export all relevant metrics registered with the given registry in the default metric exposition format as a single string.

This string MUST be freed to avoid unnecessary heap memory growth.

Reference: https://prometheus.io/docs/instrumenting/exposition_formats/

Parameters:
selfThe registry containing the collectors with the relevant metrics.
Returns:
NULL on failure, the export otherwise.
int pcr_default_init ( void  )

Initializes the default collector registry.

PROM_COLLECTOR_REGISTRY and enables metric collection on the executing process and sets the metric name prefix to METRIC_LABEL_SCRAPE + "_". Same as pcr_init(PROM_PROCESS|PROM_SCRAPETIME, METRIC_LABEL_SCRAPE "_").

Returns:
A non-zero integer value upon failure. 0 otherwise.
int pcr_destroy ( pcr_t self)

Destroy the given collector registry.

Before it releases its handles to registered collectors, it calls prom_collector_destroy() on them, and this in turn calls prom_metric_destroy() for all the collector's metrics. So keep in mind, that after this call all registered collectors and related metrics will not work anymore.

Parameters:
selfRegistry to destroy.
Returns:
A non-zero integer value upon failure, 0 otherwise.
Note:
No matter what is returned, one should always set the pointer of the given registry to NULL because it gets always passed to free() and thus points to an invalid memory location on return.

Enable process metrics on the given collector registry.

Parameters:
selfThe registry, where to attach the process metrics.
Returns:
A non-zero integer value upon failure, 0 otherwise.

Create a scrape duration gauge metric and attach it to the given prom collector registry.

If available, pcr_bridge() measures the time needed to collect and export all metrics of the registry, updates the metric and appends it to the export.

Parameters:
selfWhere to enable scrape duration monitoring.
Returns:
A non-zero integer if the given registry is NULL, or the metric could not be added to its default collector, 0 otherwise.
prom_collector_t* pcr_get ( pcr_t self,
const char *  name 
)

Get a reference to the prom collector with the given name from the given prom collector registry.

Parameters:
selfRegistry to query.
nameName of the collector to lookup.
Returns:
NULL if not found, a reference to the related prom collector otherwise.
int pcr_init ( PROM_INIT_FLAGS  features,
const char *  mprefix 
)

Initializes the default collector registry PROM_COLLECTOR_REGISTRY named REGISTRY_NAME_DEFAULT.

Parameters:
featuresIf 0, a prom collector registry gets created, which just contains a single empty collector named COLLECTOR_NAME_DEFAULT where per default all new metrics get attached.
mprefixIf not NULL, prefix each metric's name with this string when metrics get exposed. E.g. one may use "appname_".
Returns:
A non-zero integer value upon failure - the registry is unusable.

Registers a metric with the default collector on PROM_COLLECTOR_REGISTRY.

The metric to be registered MUST NOT already be registered with the given. If so, the function calls exit() - probably not what you want! NOTE: PROM_COLLECTOR_REGISTRY must be registered via pcr_init() prior to calling this function. The metric will be added to the default registry's default collector.

Parameters:
metricThe metric to register.
Returns:
The registered metric, or NULL if registration failed.
pcr_t* pcr_new ( const char *  name)

Constructs a registry named default which contains one empty prom collector named default , only.

Parameters:
nameName of the collector registry. It MUST NOT be default.
Returns:
The new registry on success, NULL otherwise.
int pcr_register_collector ( pcr_t self,
prom_collector_t collector 
)

Register a collector with the given registry.

If the registry already contains a collector with the same name, the registration will fail.

Parameters:
selfWhere to register the collector.
collectorThe collector to register.
Returns:
A non-zero integer value upon failure, 0 otherwise.

Registers a metric with the default collector on PROM_COLLECTOR_REGISTRY.

See also: pcr_must_register_metric.

Parameters:
metricThe metric to register on PROM_COLLECTOR_REGISTRY*
Returns:
A non-zero integer value upon failure.
int pcr_validate_metric_name ( pcr_t self,
const char *  metric_name 
)

Validates that the given metric name complies with the specification:

Reference: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels

Returns a non-zero integer value on failure, 0 otherwise.

Parameters:
selfRegistry to use.
metric_nameName to validate
Returns:
A non-zero integer value upon failure, 0 otherwise.

Variable Documentation

Initialize the default registry by calling pcr_init() within your app.

Note:
You MUST NOT modify this value.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines