Poco::Prometheus

template < typename T, Metric::Type metricType >

class CallbackMetric

File Information

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:

The following sample types are supported:

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 inline

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

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

CallbackMetric inline

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 inline

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 inline

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 virtual

~CallbackMetric() = default;

Destroys the CallbackMetric.

Member Functions

exportTo virtual inline

void exportTo(
    Exporter & exporter
) const override;

help inline

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

Sets the CallbackMetric's help text.

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

value inline

Sample value() const;

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

Securely control IoT edge devices from anywhere   Connect a Device