-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathuse_metrics_in_class_advanced.cpp
More file actions
182 lines (148 loc) · 6.55 KB
/
use_metrics_in_class_advanced.cpp
File metadata and controls
182 lines (148 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* prometheus-cpp-lite — header-only C++ library for exposing Prometheus metrics
* https://github.com/biaks/prometheus-cpp-lite
*
* Copyright (c) 2026 Yan Kryukov ianiskr@gmail.com
* Licensed under the MIT License
*
* =============================================================================
* use_metrics_in_class_advanced.cpp — Dynamic per-instance metrics with labels
*
* Demonstrates a real-world pattern: a class that manages multiple named
* connections (e.g. to upstream servers), where each connection has its own
* set of metrics distinguished by labels.
*
* Uses prometheus-cpp-lite-full target which provides pre-defined global objects.
*
* Key concept: families are NOT stored anywhere — each time we create a metric
* with a new label set, we simply construct a family_t with the same name.
* Because global_registry deduplicates families by name, the same underlying
* Family object is reused. This means:
*
* - No need to pass family references between classes.
* - No need to store families as class members.
* - Just create a metric anywhere, using the same family name — it will
* automatically join the existing family in global_registry.
*
* This pattern is common in routers, proxies, connection pools, and any
* system where the set of monitored entities is not known at compile time.
*/
#include <prometheus/prometheus.h>
#include <vector>
#include <memory>
using namespace prometheus;
// =============================================================================
// Connection — represents a single upstream connection with its own metrics
//
// Each instance creates metrics with unique label values {name, protocol}.
// Families are found (or created) by name in global_registry every time —
// no need to store or pass family references.
// =============================================================================
class Connection {
std::string name_;
std::string protocol_;
// Per-instance metrics.
counter_metric_t bytes_sent;
counter_metric_t bytes_recv;
counter_metric_t errors;
gauge_metric_t is_connected;
histogram_metric_t latency;
public:
Connection(const std::string& name, const std::string& protocol)
: name_ (name)
, protocol_ (protocol)
, bytes_sent (counter_metric_t ("upstream_bytes_sent_total", "Bytes sent to upstream", {{"name", name}, {"protocol", protocol}}))
, bytes_recv (counter_metric_t ("upstream_bytes_recv_total", "Bytes received from upstream", {{"name", name}, {"protocol", protocol}}))
, errors (counter_metric_t ("upstream_errors_total", "Upstream connection errors", {{"name", name}, {"protocol", protocol}}))
, is_connected (gauge_metric_t ("upstream_connected", "Connection status (1=up, 0=down)", {{"name", name}, {"protocol", protocol}}))
, latency (histogram_metric_t ("upstream_latency_seconds", "Upstream request latency", {{"name", name}, {"protocol", protocol}},
BucketBoundaries{0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0})) {
is_connected = 1;
std::cout << " [+] Connection created: " << name << " (" << protocol << ")\n";
}
void send(int bytes) {
double latency_sec = 0.001 * (1 + std::rand() % 100);
bytes_sent += bytes;
latency.Observe(latency_sec);
// Simulate occasional errors.
if (std::rand() % 20 == 0) {
errors++;
is_connected = 0;
}
}
void receive(int bytes) {
double latency_sec = 0.001 * (1 + std::rand() % 50);
bytes_recv += bytes;
latency.Observe(latency_sec);
}
void reconnect() {
is_connected = 1;
}
const std::string& name() const { return name_; }
};
// =============================================================================
// ConnectionPool — manages a dynamic set of connections
//
// No families stored here. Each Connection finds/creates its own families
// by name in global_registry.
// =============================================================================
class ConnectionPool {
std::vector<std::unique_ptr<Connection>> connections;
public:
ConnectionPool() = default;
/// @brief Adds a new connection — metrics are created dynamically in global_registry.
Connection& add_connection (const std::string& name, const std::string& protocol) {
connections.push_back(std::make_unique<Connection>(name, protocol));
return *connections.back();
}
/// @brief Simulates traffic on all connections.
void simulate_traffic () {
for (std::unique_ptr<Connection>& conn : connections) {
conn->send (100 + std::rand() % 900);
conn->receive(50 + std::rand() % 500);
// Occasionally reconnect downed connections.
if (std::rand() % 5 == 0)
conn->reconnect();
}
}
size_t size() const { return connections.size(); }
};
// =============================================================================
// main
// =============================================================================
int main() {
std::cout << "=== Advanced class instrumentation — dynamic per-instance metrics ===\n";
std::cout << " Metrics available at http://localhost:9100/metrics\n\n";
// One line — all metrics from all classes are exposed.
http_server.start("127.0.0.1:9100");
ConnectionPool pool;
// Phase 1: Start with two connections.
std::cout << " Phase 1: Creating initial connections...\n";
pool.add_connection("gateway-eu-west", "tcp");
pool.add_connection("gateway-us-east", "tcp");
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
pool.simulate_traffic();
}
std::cout << "\n" << global_registry.serialize() << std::endl;
// Phase 2: Add more connections dynamically (e.g. new upstream discovered).
std::cout << " Phase 2: Adding more connections dynamically...\n";
pool.add_connection("gateway-ap-south", "tcp");
pool.add_connection("cache-local", "udp");
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
pool.simulate_traffic();
}
std::cout << "\n" << global_registry.serialize() << std::endl;
// Phase 3: Continue running — all 4 connections producing metrics.
std::cout << " Phase 3: Running with " << pool.size() << " connections for 20 seconds...\n";
for (int i = 0; i < 40; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
pool.simulate_traffic();
if (i % 10 == 9)
std::cout << " tick " << (i + 1) << "/40\n";
}
std::cout << "\n Final metrics:\n" << global_registry.serialize() << std::endl;
std::cout << " Done.\n";
return 0;
}