Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::metrics::PrometheusBackend Class Reference

Prometheus/OpenMetrics format backend. More...

#include <metrics_backend.h>

Inheritance diagram for kcenon::thread::metrics::PrometheusBackend:
Inheritance graph
Collaboration diagram for kcenon::thread::metrics::PrometheusBackend:
Collaboration graph

Public Member Functions

 PrometheusBackend ()=default
 Default constructor.
 
 ~PrometheusBackend () override=default
 Destructor.
 
std::string name () const override
 Get the backend name.
 
std::string export_base (const BaseSnapshot &snapshot) const override
 Export base metrics snapshot.
 
std::string export_enhanced (const EnhancedSnapshot &snapshot) const override
 Export enhanced metrics snapshot.
 
- Public Member Functions inherited from kcenon::thread::metrics::MetricsBackend
virtual ~MetricsBackend ()=default
 Virtual destructor for proper cleanup.
 
virtual void set_prefix (const std::string &prefix)
 Set metric name prefix.
 
const std::string & prefix () const
 Get current metric name prefix.
 
virtual void add_label (const std::string &key, const std::string &value)
 Add a label to all exported metrics.
 
const std::map< std::string, std::string > & labels () const
 Get all configured labels.
 

Private Member Functions

std::string format_labels () const
 Format labels for Prometheus output.
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::thread::metrics::MetricsBackend
 MetricsBackend ()=default
 Default constructor.
 
- Protected Attributes inherited from kcenon::thread::metrics::MetricsBackend
std::string prefix_ {"thread_pool"}
 Metric name prefix.
 
std::map< std::string, std::string > labels_
 Labels to attach to all metrics.
 

Detailed Description

Prometheus/OpenMetrics format backend.

Exports metrics in Prometheus exposition format, suitable for scraping by Prometheus servers.

Output Format

# HELP thread_pool_tasks_submitted_total Total tasks submitted
# TYPE thread_pool_tasks_submitted_total counter
thread_pool_tasks_submitted_total 1234

Definition at line 149 of file metrics_backend.h.

Constructor & Destructor Documentation

◆ PrometheusBackend()

kcenon::thread::metrics::PrometheusBackend::PrometheusBackend ( )
default

Default constructor.

◆ ~PrometheusBackend()

kcenon::thread::metrics::PrometheusBackend::~PrometheusBackend ( )
overridedefault

Destructor.

Member Function Documentation

◆ export_base()

std::string kcenon::thread::metrics::PrometheusBackend::export_base ( const BaseSnapshot & snapshot) const
nodiscardoverridevirtual

Export base metrics snapshot.

Parameters
snapshotThe base metrics snapshot to export.
Returns
Formatted string representation.

Implements kcenon::thread::metrics::MetricsBackend.

Definition at line 35 of file metrics_backend.cpp.

35 {
36 std::ostringstream oss;
37 oss << std::fixed << std::setprecision(6);
38 const auto& p = prefix_;
39 const auto labels = format_labels();
40
41 // Task counters
42 oss << "# HELP " << p << "_tasks_submitted_total Total tasks submitted\n";
43 oss << "# TYPE " << p << "_tasks_submitted_total counter\n";
44 oss << p << "_tasks_submitted_total" << labels << " " << snapshot.tasks_submitted << "\n\n";
45
46 oss << "# HELP " << p << "_tasks_executed_total Total tasks executed\n";
47 oss << "# TYPE " << p << "_tasks_executed_total counter\n";
48 oss << p << "_tasks_executed_total" << labels << " " << snapshot.tasks_executed << "\n\n";
49
50 oss << "# HELP " << p << "_tasks_failed_total Total tasks failed\n";
51 oss << "# TYPE " << p << "_tasks_failed_total counter\n";
52 oss << p << "_tasks_failed_total" << labels << " " << snapshot.tasks_failed << "\n\n";
53
54 // Time counters
55 oss << "# HELP " << p << "_busy_time_nanoseconds_total Total busy time\n";
56 oss << "# TYPE " << p << "_busy_time_nanoseconds_total counter\n";
57 oss << p << "_busy_time_nanoseconds_total" << labels << " " << snapshot.total_busy_time_ns << "\n\n";
58
59 oss << "# HELP " << p << "_idle_time_nanoseconds_total Total idle time\n";
60 oss << "# TYPE " << p << "_idle_time_nanoseconds_total counter\n";
61 oss << p << "_idle_time_nanoseconds_total" << labels << " " << snapshot.total_idle_time_ns << "\n";
62
63 return oss.str();
64}
const std::map< std::string, std::string > & labels() const
Get all configured labels.
std::string prefix_
Metric name prefix.
std::string format_labels() const
Format labels for Prometheus output.

References format_labels(), kcenon::thread::metrics::MetricsBackend::labels(), kcenon::thread::metrics::MetricsBackend::prefix_, kcenon::thread::metrics::BaseSnapshot::tasks_executed, kcenon::thread::metrics::BaseSnapshot::tasks_failed, kcenon::thread::metrics::BaseSnapshot::tasks_submitted, kcenon::thread::metrics::BaseSnapshot::total_busy_time_ns, and kcenon::thread::metrics::BaseSnapshot::total_idle_time_ns.

Here is the call graph for this function:

◆ export_enhanced()

std::string kcenon::thread::metrics::PrometheusBackend::export_enhanced ( const EnhancedSnapshot & snapshot) const
nodiscardoverridevirtual

Export enhanced metrics snapshot.

Parameters
snapshotThe enhanced metrics snapshot to export.
Returns
Formatted string representation.

Implements kcenon::thread::metrics::MetricsBackend.

Definition at line 66 of file metrics_backend.cpp.

66 {
67 std::ostringstream oss;
68 oss << std::fixed << std::setprecision(6);
69 const auto& p = prefix_;
70 const auto labels = format_labels();
71
72 // Task counters
73 oss << "# HELP " << p << "_tasks_submitted_total Total tasks submitted\n";
74 oss << "# TYPE " << p << "_tasks_submitted_total counter\n";
75 oss << p << "_tasks_submitted_total" << labels << " " << snapshot.tasks_submitted << "\n\n";
76
77 oss << "# HELP " << p << "_tasks_executed_total Total tasks executed\n";
78 oss << "# TYPE " << p << "_tasks_executed_total counter\n";
79 oss << p << "_tasks_executed_total" << labels << " " << snapshot.tasks_executed << "\n\n";
80
81 oss << "# HELP " << p << "_tasks_failed_total Total tasks failed\n";
82 oss << "# TYPE " << p << "_tasks_failed_total counter\n";
83 oss << p << "_tasks_failed_total" << labels << " " << snapshot.tasks_failed << "\n\n";
84
85 // Latency summary (enqueue)
86 oss << "# HELP " << p << "_enqueue_latency_us Enqueue latency in microseconds\n";
87 oss << "# TYPE " << p << "_enqueue_latency_us summary\n";
88 oss << p << "_enqueue_latency_us{quantile=\"0.5\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.enqueue_latency_p50_us << "\n";
89 oss << p << "_enqueue_latency_us{quantile=\"0.9\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.enqueue_latency_p90_us << "\n";
90 oss << p << "_enqueue_latency_us{quantile=\"0.99\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.enqueue_latency_p99_us << "\n\n";
91
92 // Latency summary (execution)
93 oss << "# HELP " << p << "_execution_latency_us Execution latency in microseconds\n";
94 oss << "# TYPE " << p << "_execution_latency_us summary\n";
95 oss << p << "_execution_latency_us{quantile=\"0.5\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.execution_latency_p50_us << "\n";
96 oss << p << "_execution_latency_us{quantile=\"0.9\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.execution_latency_p90_us << "\n";
97 oss << p << "_execution_latency_us{quantile=\"0.99\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.execution_latency_p99_us << "\n\n";
98
99 // Latency summary (wait time)
100 oss << "# HELP " << p << "_wait_time_us Queue wait time in microseconds\n";
101 oss << "# TYPE " << p << "_wait_time_us summary\n";
102 oss << p << "_wait_time_us{quantile=\"0.5\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.wait_time_p50_us << "\n";
103 oss << p << "_wait_time_us{quantile=\"0.9\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.wait_time_p90_us << "\n";
104 oss << p << "_wait_time_us{quantile=\"0.99\"" << (labels.empty() ? "" : "," + labels.substr(1, labels.size() - 2)) << "} " << snapshot.wait_time_p99_us << "\n\n";
105
106 // Throughput
107 oss << "# HELP " << p << "_throughput_1s Tasks per second (1s window)\n";
108 oss << "# TYPE " << p << "_throughput_1s gauge\n";
109 oss << p << "_throughput_1s" << labels << " " << snapshot.throughput_1s << "\n\n";
110
111 oss << "# HELP " << p << "_throughput_1m Tasks per second (1m window)\n";
112 oss << "# TYPE " << p << "_throughput_1m gauge\n";
113 oss << p << "_throughput_1m" << labels << " " << snapshot.throughput_1m << "\n\n";
114
115 // Queue depth
116 oss << "# HELP " << p << "_queue_depth_current Current queue depth\n";
117 oss << "# TYPE " << p << "_queue_depth_current gauge\n";
118 oss << p << "_queue_depth_current" << labels << " " << snapshot.current_queue_depth << "\n\n";
119
120 oss << "# HELP " << p << "_queue_depth_peak Peak queue depth\n";
121 oss << "# TYPE " << p << "_queue_depth_peak gauge\n";
122 oss << p << "_queue_depth_peak" << labels << " " << snapshot.peak_queue_depth << "\n\n";
123
124 // Worker utilization
125 oss << "# HELP " << p << "_worker_utilization Overall worker utilization\n";
126 oss << "# TYPE " << p << "_worker_utilization gauge\n";
127 oss << p << "_worker_utilization" << labels << " " << snapshot.worker_utilization << "\n\n";
128
129 oss << "# HELP " << p << "_active_workers Number of active workers\n";
130 oss << "# TYPE " << p << "_active_workers gauge\n";
131 oss << p << "_active_workers" << labels << " " << snapshot.active_workers << "\n\n";
132
133 // Per-worker utilization
134 oss << "# HELP " << p << "_worker_utilization_per_worker Per-worker utilization\n";
135 oss << "# TYPE " << p << "_worker_utilization_per_worker gauge\n";
136 for (std::size_t i = 0; i < snapshot.per_worker_utilization.size(); ++i) {
137 oss << p << "_worker_utilization_per_worker{worker=\"" << i << "\"";
138 if (!labels_.empty()) {
139 for (const auto& [key, value] : labels_) {
140 oss << "," << key << "=\"" << value << "\"";
141 }
142 }
143 oss << "} " << snapshot.per_worker_utilization[i] << "\n";
144 }
145
146 return oss.str();
147}
std::map< std::string, std::string > labels_
Labels to attach to all metrics.

References kcenon::thread::metrics::EnhancedSnapshot::active_workers, kcenon::thread::metrics::EnhancedSnapshot::current_queue_depth, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p50_us, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p90_us, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p99_us, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p50_us, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p90_us, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p99_us, format_labels(), kcenon::thread::metrics::MetricsBackend::labels(), kcenon::thread::metrics::MetricsBackend::labels_, kcenon::thread::metrics::EnhancedSnapshot::peak_queue_depth, kcenon::thread::metrics::EnhancedSnapshot::per_worker_utilization, kcenon::thread::metrics::MetricsBackend::prefix_, kcenon::thread::metrics::EnhancedSnapshot::tasks_executed, kcenon::thread::metrics::EnhancedSnapshot::tasks_failed, kcenon::thread::metrics::EnhancedSnapshot::tasks_submitted, kcenon::thread::metrics::EnhancedSnapshot::throughput_1m, kcenon::thread::metrics::EnhancedSnapshot::throughput_1s, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p50_us, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p90_us, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p99_us, and kcenon::thread::metrics::EnhancedSnapshot::worker_utilization.

Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::to_prometheus().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_labels()

std::string kcenon::thread::metrics::PrometheusBackend::format_labels ( ) const
nodiscardprivate

Format labels for Prometheus output.

Returns
Formatted label string (e.g., {key="value"}).

Definition at line 16 of file metrics_backend.cpp.

16 {
17 if (labels_.empty()) {
18 return "";
19 }
20
21 std::ostringstream oss;
22 oss << "{";
23 bool first = true;
24 for (const auto& [key, value] : labels_) {
25 if (!first) {
26 oss << ",";
27 }
28 oss << key << "=\"" << value << "\"";
29 first = false;
30 }
31 oss << "}";
32 return oss.str();
33}

References kcenon::thread::metrics::MetricsBackend::labels_.

Referenced by export_base(), and export_enhanced().

Here is the caller graph for this function:

◆ name()

std::string kcenon::thread::metrics::PrometheusBackend::name ( ) const
inlinenodiscardoverridevirtual

Get the backend name.

Returns
Human-readable backend identifier (e.g., "prometheus", "json").

Implements kcenon::thread::metrics::MetricsBackend.

Definition at line 161 of file metrics_backend.h.

161 {
162 return "prometheus";
163 }

The documentation for this class was generated from the following files: