LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
example_logit_prometheus_payload.cpp
Go to the documentation of this file.
1#include <logit.hpp>
2
3#ifdef LOGIT_WITH_PROMETHEUS
5#endif
6
7#include <iostream>
8#include <string>
9#include <vector>
10
11int main() {
12#ifndef LOGIT_WITH_PROMETHEUS
14 LOGIT_WARN("Prometheus payload example requires LOGIT_WITH_PROMETHEUS=ON");
15 LOGIT_WAIT();
16 return 0;
17#else
18 int queue_depth = 3;
19 unsigned long long jobs_processed = 41;
20
21 logit::PrometheusRegistry registry("myapp_");
22 registry.set_gauge(
23 "queue_depth",
24 "Current application queue depth",
25 [&queue_depth]() { return static_cast<double>(queue_depth); },
26 {{"queue", "orders"}});
27 registry.set_counter(
28 "jobs_processed_total",
29 "Total processed jobs",
30 [&jobs_processed]() { return static_cast<double>(jobs_processed); });
31
33 config.format.metric_prefix = "myapp_";
34 config.format.include_build_info = true;
35 config.emit_on_wait = true;
36 config.on_collect = [&registry](std::vector<logit::PrometheusMetricFamily>& families) {
37 registry.collect(families);
38 };
39 config.on_payload = [](std::string payload) {
40 std::cout << payload << std::endl;
41 };
42
45 (config),
47 ("%v")
48 );
49
50 LOGIT_INFO("Prometheus payload logger started");
51 LOGIT_WARN("Example warning message");
52 LOGIT_ERROR("Example error message");
53 queue_depth = 1;
54 ++jobs_processed;
55
56 LOGIT_WAIT();
58 return 0;
59#endif
60}
Declarative registry for custom Prometheus application metrics.
Exposes log metrics as Prometheus text exposition format via user-provided callback.
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_ERROR(...)
#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::string)> on_payload
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.