3#ifdef LOGIT_WITH_PROMETHEUS_SERVER
13#ifndef LOGIT_WITH_PROMETHEUS_SERVER
15 LOGIT_WARN(
"Prometheus server example requires LOGIT_WITH_PROMETHEUS_SERVER=ON");
19 const auto started_at = std::chrono::steady_clock::now();
20 std::atomic<unsigned long long> jobs_processed{0};
21 std::atomic<int> queue_depth{0};
26 "Application uptime in seconds",
28 return std::chrono::duration<double>(
29 std::chrono::steady_clock::now() - started_at).count();
33 "Current application queue depth",
34 [&queue_depth]() {
return static_cast<double>(queue_depth.load()); },
35 {{
"queue",
"orders"}});
37 "jobs_processed_total",
38 "Total processed jobs",
39 [&jobs_processed]() {
return static_cast<double>(jobs_processed.load()); });
43 config.
path =
"/metrics";
47 config.
on_collect = [®istry](std::vector<logit::PrometheusMetricFamily>& families) {
58 LOGIT_INFO(
"Prometheus HTTP server started on port 9090");
59 LOGIT_WARN(
"Scrape metrics at http://localhost:9090/metrics");
61 for (
int i = 0; i < 5; ++i) {
62 queue_depth.store(5 - i);
63 jobs_processed.fetch_add(1);
65 std::this_thread::sleep_for(std::chrono::seconds(1));
Declarative registry for custom Prometheus application metrics.
Serves Prometheus metrics via an embedded HTTP server.
Stores custom gauge and counter callbacks for Prometheus collection.
void collect(std::vector< PrometheusMetricFamily > &out) const
Appends current registry metrics to the output vector.
void set_counter(const std::string &name, const std::string &help, std::function< double()> value_fn, std::vector< PrometheusLabel > labels={})
Registers or replaces a counter metric callback.
void set_gauge(const std::string &name, const std::string &help, std::function< double()> value_fn, std::vector< PrometheusLabel > labels={})
Registers or replaces a gauge metric callback.
#define LOGIT_ADD_CONSOLE_DEFAULT()
Macro for adding the default console logger.
#define LOGIT_WAIT()
Macro for waiting for all asynchronous loggers to finish processing.
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_ADD_LOGGER(logger_type, logger_args, formatter_type, formatter_args)
Macro for adding a logger with a specific formatter.
Unified umbrella header for the LogIt++ library.
PrometheusTextFormatConfig format
std::function< void(std::vector< PrometheusMetricFamily > &)> on_collect
bool include_build_info
Emit logit_build_info metric.
std::string metric_prefix
Prefix applied to built-in metric names.