Prometheus Metrics in C++: The prometheus-cpp Library
In modern software development, monitoring has become an integral part of production systems. Prometheus is one of the most popular solutions for collecting and analyzing metrics. If you write in C++ and want to add monitoring to your application, the prometheus-cpp library is what you need. It provides full support for Prometheus metric types and allows you to easily export them via HTTP.
What is prometheus-cpp?
prometheus-cpp is a client library for C++ that implements the Prometheus metric export protocol. It supports all major metric types: Counter, Gauge, Histogram, and Summary. The library is written in modern C++ (C++11 and higher), has no external dependencies (except the standard library and libcurl for the HTTP server), and is actively maintained by the community.
Installation
The easiest way to install is via vcpkg or Conan. For vcpkg, run:
vcpkg install prometheus-cppIf you prefer to build manually, clone the repository and use CMake:
git clone https://github.com/jupp0r/prometheus-cpp.gitcd prometheus-cppmkdir build && cd buildcmake ..make -j4sudo make installCore Concepts
The library is built around the following components:
- Registry — a registry that stores all application metrics.
- Counter — a counter that only increases (e.g., number of requests).
- Gauge — a value that can both increase and decrease (e.g., number of active connections).
- Histogram — a histogram for measuring value distributions (e.g., response time).
- Summary — a quantile estimate (similar to a histogram but calculates exact percentiles).
Example: Creating a Counter and Gauge
Let's look at a simple example — an HTTP server that counts the number of requests and tracks the number of active connections.
#include <prometheus/counter.h>#include <prometheus/gauge.h>#include <prometheus/registry.h>#include <prometheus/exposer.h>
int main() { // Create a metric registry auto registry = std::make_shared<prometheus::Registry>();
// Create a Counter family auto& counter_family = prometheus::BuildCounter() .Name("http_requests_total") .Help("Total number of HTTP requests") .Register(*registry);
// Add a label "method" with value "GET" auto& http_requests = counter_family.Add({{"method", "GET"}});
// Create a Gauge family auto& gauge_family = prometheus::BuildGauge() .Name("active_connections") .Help("Number of active connections") .Register(*registry);
auto& active_connections = gauge_family.Add({});
// Export metrics on port 8080 prometheus::Exposer exposer("0.0.0.0:8080"); exposer.RegisterCollectable(registry);
// Simulate work while (true) { http_requests.Increment(); // +1 to counter active_connections.Set(42); // set value to 42 std::this_thread::sleep_for(std::chrono::seconds(1)); }
return 0;}Example: Histogram for Response Time
Histograms are useful for measuring latency. Let's define buckets for response time:
#include <prometheus/histogram.h>
// Create a histogram with buckets: 0.1, 0.5, 1.0, 2.5, 5.0 secondsauto& histogram_family = prometheus::BuildHistogram() .Name("http_request_duration_seconds") .Help("Request latency in seconds") .Register(*registry);
auto& request_duration = histogram_family.Add(