Integrating Prometheus Metrics into C++ Applications: The prometheus-cpp Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

The prometheus-cpp Library: Collecting Prometheus Metrics in C++ Applications



In modern software development, monitoring application health plays a critical role. Prometheus is an open-source monitoring and alerting system that collects metrics from target objects via HTTP endpoints. For integrating Prometheus into C++ projects, there is a powerful and flexible library called prometheus-cpp. It allows developers to easily add metric collection (counters, gauges, histograms) to their code and export them in a format understandable by the Prometheus server.



What is prometheus-cpp?



prometheus-cpp is a client library for Prometheus, written in C++17. It provides all the necessary tools for creating, updating, and exporting metrics. The library supports three main metric types defined in the Prometheus data model:



  • Counter — a monotonically increasing value (e.g., number of requests, errors, completed tasks).
  • Gauge — a value that can arbitrarily increase and decrease (e.g., current temperature, number of active connections).
  • Histogram — a distribution of values across predefined intervals (e.g., request execution time).


Additionally, Summary (quantiles) and Family (groups of metrics of the same type with different labels) are supported.



Installation and Setup



The library is distributed via package managers (vcpkg, Conan) or can be built from source. Example installation via vcpkg:



vcpkg install prometheus-cpp


After installation, the library must be linked in the CMakeLists.txt file:



find_package(prometheus-cpp CONFIG REQUIRED)target_link_libraries(your_target PRIVATE prometheus-cpp::push prometheus-cpp::pull prometheus-cpp::core)


Here, push is used for sending metrics to the Pushgateway, pull for creating an HTTP server from which Prometheus will scrape metrics, and core contains the main classes (Registry, Counter, Gauge, Histogram).



Usage Example: HTTP Server for Metric Collection



Consider a simple example that creates an HTTP server on port 8080, which exposes metrics in Prometheus format. The library uses a built-in mechanism based on simple-web-server or libmicrohttpd for HTTP handling.



#include <prometheus/exposer.h>#include <prometheus/registry.h>#include <prometheus/counter.h>#include <prometheus/gauge.h>#include <prometheus/histogram.h>#include <thread>#include <chrono>

int main() { // Create an HTTP server on port 8080 prometheus::Exposer exposer{"0.0.0.0:8080"};

// Create a metric registry auto registry = std::make_shared<prometheus::Registry>();

// 1. Counter auto& counter_family = prometheus::BuildCounter() .Name("http_requests_total") .Help("Total number of HTTP requests") .Register(*registry);

auto& counter = counter_family.Add({{"method", "GET"}, {"endpoint", "/api"}});

// 2. Gauge auto& gauge_family = prometheus::BuildGauge() .Name("memory_usage_bytes") .Help("Current memory usage in bytes") .Register(*registry);

auto& gauge = gauge_family.Add({});

// 3. Histogram auto& histogram_family = prometheus::BuildHistogram() .Name("request_duration_seconds") .Help("Request duration in seconds") .Buckets({0.1, 0.5, 1.0, 2.5, 5.0}) .Register(*registry);

auto& histogram = histogram_family.Add({{"handler", "p

Recommendations