# Poco::Prometheus

# template < typename T, Metric::Type metricType >

# class CallbackMetric

Library: Prometheus
Package: Core
Header: Poco/Prometheus/CallbackMetric.h

## Description

A generic Metric implementation where the sample is obtained via a callback function or lambda.

The callback is invoked whenever the value of the metric is requested. This is typically when the metric is written to an Exporter.

This is implemented as a template to support both counter and gauge metrics with different underlying types.

Note that only metric types COUNTER and GAUGE are supported.

Labels are not supported.

Example usage:

    CallbackMetric<Poco::UInt64, Metric::Type::COUNTER> sampleCount(
        "sample_count"s,
        []()
        {
            return 42;
        }
    );

There are also pre-instantiated types available:

- CallbackIntCounter
- CallbackIntGauge
- CallbackCounter
- CallbackGauge

The following sample types are supported:

- Poco::Int32
- Poco::UInt32
- Poco::Int64
- Poco::UInt64
- double

## Inheritance

Direct Base Classes: Metric

All Base Classes: Collector, Metric

## Member Summary

Member Functions: exportTo, help, value

Inherited Functions: exportTo, help, name, setHelp, type, validateName

## Types Aliases

### Callback

using Callback = std::function < Sample ()>;

### Sample

using Sample = T;

## Constructors

### CallbackMetric

CallbackMetric(
const std::string & name,
Callback callback
);

Creates a CallbackMetric with the given type and name and registers it with the default registry.

### CallbackMetric

CallbackMetric(
const std::string & name,
const std::string & help,
Callback callback
);

Creates a CallbackMetric with the given type, name and help text, and registers it with the default registry.

### CallbackMetric

CallbackMetric(
const std::string & name,
Registry * pRegistry,
Callback callback
);

Creates a CallbackMetric with the given type and name and registers it with the given registry (if not nullptr).

### CallbackMetric

CallbackMetric(
const std::string & name,
const std::string & help,
Registry * pRegistry,
Callback callback
);

Creates a CallbackMetric with the given type, name and help text, and registers it with the given registry (if not nullptr).

## Destructor

### ~CallbackMetric

~CallbackMetric() = default;

Destroys the CallbackMetric.

## Member Functions

### exportTo

void exportTo(
Exporter & exporter
) const override;

See also: Poco::Prometheus::Collector::exportTo()

### help

CallbackMetric & help(
const std::string & text
);

Sets the CallbackMetric's help text.

Must only be set once, immediately after creating the CallbackMetric.

### value

Sample value() const;

Invokes the callback function and returns the value returned by it.