LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
example_logit_prometheus_server.cpp
Go to the documentation of this file.
1#include <logit.hpp>
2
3#ifdef LOGIT_WITH_PROMETHEUS_SERVER
5#endif
6
7#include <chrono>
8#include <atomic>
9#include <thread>
10#include <vector>
11
12int main() {
13#ifndef LOGIT_WITH_PROMETHEUS_SERVER
15 LOGIT_WARN("Prometheus server example requires LOGIT_WITH_PROMETHEUS_SERVER=ON");
16 LOGIT_WAIT();
17 return 0;
18#else
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};
22
23 logit::PrometheusRegistry registry("myapp_");
24 registry.set_gauge(
25 "uptime_seconds",
26 "Application uptime in seconds",
27 [started_at]() {
28 return std::chrono::duration<double>(
29 std::chrono::steady_clock::now() - started_at).count();
30 });
31 registry.set_gauge(
32 "queue_depth",
33 "Current application queue depth",
34 [&queue_depth]() { return static_cast<double>(queue_depth.load()); },
35 {{"queue", "orders"}});
36 registry.set_counter(
37 "jobs_processed_total",
38 "Total processed jobs",
39 [&jobs_processed]() { return static_cast<double>(jobs_processed.load()); });
40
42 config.port = 9090;
43 config.path = "/metrics";
44 config.format.metric_prefix = "myapp_";
45 config.format.include_build_info = true;
46
47 config.on_collect = [&registry](std::vector<logit::PrometheusMetricFamily>& families) {
48 registry.collect(families);
49 };
50
53 (config),
55 ("%v")
56 );
57
58 LOGIT_INFO("Prometheus HTTP server started on port 9090");
59 LOGIT_WARN("Scrape metrics at http://localhost:9090/metrics");
60
61 for (int i = 0; i < 5; ++i) {
62 queue_depth.store(5 - i);
63 jobs_processed.fetch_add(1);
64 LOGIT_INFO("Logging iteration %d", (i + 1));
65 std::this_thread::sleep_for(std::chrono::seconds(1));
66 }
67
69 return 0;
70#endif
71}
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.
A simple log formatter that formats log messages based on a user-defined pattern.
#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_INFO(...)
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_WARN(...)
#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.
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.