Skip to content

Monitoring C++ Applications with Prometheus

Professional

You can instrument your C++ application with jupp0r/prometheus-cpp and let the Glouton agent scrape its /metrics endpoint.

Using vcpkg:

Terminal window
vcpkg install prometheus-cpp[pull]

Or build from source — see the prometheus-cpp README for CMake and Bazel integration.

The snippet below defines a counter and exposes it on http://localhost:8080/metrics using the bundled HTTP exposer:

#include <chrono>
#include <memory>
#include <thread>
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>
int main() {
using namespace prometheus;
Exposer exposer{"0.0.0.0:8080"};
auto registry = std::make_shared<Registry>();
auto& family = BuildCounter()
.Name("myapp_requests_total")
.Help("Total number of processed requests.")
.Register(*registry);
auto& requests_total = family.Add({});
exposer.RegisterCollectable(registry);
while (true) {
requests_total.Increment();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:

metric:
prometheus:
targets:
- url: "http://localhost:8080/metrics"
name: "my_cpp_application"
allow_metrics:
- "myapp_requests_total"

Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.