Skip to content

Commit 3d51a6e

Browse files
committed
Add class instrumentation examples; add configurable socket log level to http_server_t/http_pusher_t; update example file headers and console output
1 parent 3b159cc commit 3d51a6e

21 files changed

Lines changed: 744 additions & 209 deletions

3rdparty/ip-sockets-cpp-lite/readme.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ip-sockets-cpp-lite 🔌
22

33
[![Build examples](https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake.yml/badge.svg)](https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake.yml)
4+
[![Build multi-platform](https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake-multi-platform.yml)
5+
46

57
**Simple. Lightweight. Cross-platform.**
68

@@ -107,9 +109,9 @@ Keep it simple. Keep it portable.
107109
108110
from /include folder copy:
109111
110-
* `ip_address.h`
111-
* `udp_socket.h`
112-
* `tcp_socket.h`
112+
* [`ip_address.h`](include/ip_address.h)
113+
* [`udp_socket.h`](include/udp_socket.h)
114+
* [`tcp_socket.h`](include/tcp_socket.h)
113115
114116
**Option 2 — Use CMake**
115117
@@ -255,13 +257,12 @@ if (server.open("0.0.0.0:8080") == no_error) {
255257
```
256258

257259
### 📚 More Examples
258-
Check out the `examples/` directory for complete working examples:
259-
* `ip_address.cpp` - all IP address manipulation features
260-
* `udp_socket.cpp` - UDP client-server interaction
261-
* `tcp_socket.cpp` - TCP client-server interaction
262-
* `resolve_host.cpp` - resolving host to ipv4/ipv6 address example
263-
* `http_server.cpp` - compact multi-page HTTP server
264-
260+
Check out the [`examples/`](examples) directory for complete working examples:
261+
* [`ip_address.cpp`](examples/ip_address.cpp) - all IP address manipulation features
262+
* [`udp_socket.cpp`](examples/udp_socket.cpp) - UDP client-server interaction
263+
* [`tcp_socket.cpp`](examples/tcp_socket.cpp) - TCP client-server interaction
264+
* [`resolve_host.cpp`](examples/resolve_host.cpp) - resolving host to ipv4/ipv6 address example
265+
* [`http_server.cpp`](examples/http_server.cpp) - compact multi-page HTTP server
265266
---
266267

267268
## 🤔 Why is this convenient?

README.md

Lines changed: 88 additions & 32 deletions
Large diffs are not rendered by default.

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ if(PROMETHEUS_USE_IP_SOCKETS)
2626
add_example(provide_via_http_push_advanced prometheus-cpp-lite)
2727
add_example(legacy_prometheus_cpp prometheus-cpp-lite)
2828
add_example(check_global_objects prometheus-cpp-lite-full)
29+
add_example(use_benchmark_in_class_simple prometheus-cpp-lite-full)
30+
add_example(use_metrics_in_class_simple prometheus-cpp-lite-full)
31+
add_example(use_metrics_in_class_advanced prometheus-cpp-lite-full)
2932
endif()
3033

examples/add_custom_metric_class.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* prometheus-cpp-lite — header-only C++ library for exposing Prometheus metrics
33
* https://github.com/biaks/prometheus-cpp-lite
44
*
5-
* Copyright (c) 2025 Yan Kryukov ianiskr@gmail.com
5+
* Copyright (c) 2026 Yan Kryukov ianiskr@gmail.com
66
* Licensed under the MIT License
77
*
88
* =============================================================================
@@ -28,8 +28,6 @@
2828
#include <algorithm>
2929
#include <limits>
3030

31-
using namespace prometheus;
32-
3331
// =============================================================================
3432
// Global registry definition
3533
// =============================================================================
@@ -38,6 +36,8 @@ namespace prometheus {
3836
registry_t global_registry;
3937
}
4038

39+
using namespace prometheus;
40+
4141
// =============================================================================
4242
// min_max_t — custom Prometheus metric that tracks minimum and maximum values
4343
//
@@ -138,7 +138,6 @@ class min_max_t : public Metric {
138138
++count;
139139
}
140140

141-
142141
value_type GetMin() const { return current_min.load(); } ///< @brief Returns the current minimum observed value.
143142
value_type GetMax() const { return current_max.load(); } ///< @brief Returns the current maximum observed value.
144143
value_type GetCount() const { return count.load(); } ///< @brief Returns the total number of observations.
@@ -185,7 +184,7 @@ using BuildMinMax = Builder<min_max_t<double>>; ///< @brief
185184
// Chain: registry → (implicit family) → metric
186185
// =============================================================================
187186

188-
void test_main_1() {
187+
void test_main_1 () {
189188
std::cout << "\n=== test_main_1 — User-owned registry, implicit family ===\n";
190189

191190
registry_t registry;
@@ -208,13 +207,13 @@ void test_main_1() {
208207
// Chain: registry → family → metric
209208
// =============================================================================
210209

211-
void test_main_2() {
210+
void test_main_2 () {
212211
std::cout << "\n=== test_main_2 — Explicit untyped family wrapper ===\n";
213212

214213
registry_t registry;
215-
family_t sensors (registry, "sensor_reading", "Sensor min/max readings", {{"location", "lab"}});
216-
min_max_metric_t humidity (sensors, {{"type", "humidity"}});
217-
min_max_metric_t pressure (sensors, {{"type", "pressure"}});
214+
family_t sensors (registry, "sensor_reading", "Sensor min/max readings", {{"location", "lab"}});
215+
min_max_metric_t humidity (sensors, {{"type", "humidity"}});
216+
min_max_metric_t pressure (sensors, {{"type", "pressure"}});
218217

219218
humidity.Observe(45.0);
220219
humidity.Observe(62.3);
@@ -235,13 +234,13 @@ void test_main_2() {
235234
// Chain: registry → typed family → metric
236235
// =============================================================================
237236

238-
void test_main_3() {
237+
void test_main_3 () {
239238
std::cout << "\n=== test_main_3 — Typed family wrapper (compile-time safety) ===\n";
240239

241240
registry_t registry;
242-
min_max_family_t latency (registry, "request_latency_ms", "Request latency range", {{"service", "api"}});
243-
min_max_metric_t latency_get (latency, {{"method", "GET"}});
244-
min_max_metric_t latency_post (latency, {{"method", "POST"}});
241+
min_max_family_t latency (registry, "request_latency_ms", "Request latency range", {{"service", "api"}});
242+
min_max_metric_t latency_get (latency, {{"method", "GET"}});
243+
min_max_metric_t latency_post (latency, {{"method", "POST"}});
245244

246245
// If you uncomment this, you'll get a compile-time type mismatch error:
247246
// counter_metric_t wrong (latency, {{"method", "DELETE"}});
@@ -254,7 +253,7 @@ void test_main_3() {
254253
latency_post.Observe(55.3);
255254
latency_post.Observe(200.7);
256255

257-
std::cout << " - GET min: " << latency_get.GetMin() << ", max: " << latency_get.GetMax() << std::endl;
256+
std::cout << " - GET min: " << latency_get.GetMin() << ", max: " << latency_get.GetMax() << std::endl;
258257
std::cout << " - POST min: " << latency_post.GetMin() << ", max: " << latency_post.GetMax() << std::endl;
259258
std::cout << " - output serialized data:\n" << registry.serialize();
260259
}
@@ -265,10 +264,10 @@ void test_main_3() {
265264
// Chain: global_registry → (implicit family) → metric
266265
// =============================================================================
267266

268-
void test_main_4() {
267+
void test_main_4 () {
269268
std::cout << "\n=== test_main_4 — Global registry (shortest form) ===\n";
270269

271-
global_registry = Registry(); // Clear global registry for a clean test.
270+
global_registry.RemoveAll(); // Clear global registry for a clean test.
272271

273272
min_max_metric_t cpu_freq ("cpu_frequency_mhz", "CPU frequency range");
274273

@@ -289,20 +288,20 @@ void test_main_4() {
289288
// Chain: Registry& → BuildMinMax().Register() → CustomFamily& → min_max_t&
290289
// =============================================================================
291290

292-
void test_main_5() {
291+
void test_main_5 () {
293292
std::cout << "\n=== test_main_5 — Legacy Builder API ===\n";
294293

295294
using MinMax = min_max_t<double>;
296295
using MinMaxFamily = CustomFamily<MinMax>;
297296

298-
Registry registry;
297+
Registry registry;
299298
MinMaxFamily& response_size = BuildMinMax().Name("response_size_bytes")
300299
.Help("Response size range")
301300
.Labels({{"service", "web"}})
302301
.Register(registry);
303302

304-
MinMax& size_html = response_size.Add({{"content_type", "html"}});
305-
MinMax& size_json = response_size.Add({{"content_type", "json"}});
303+
MinMax& size_html = response_size.Add({{"content_type", "html"}});
304+
MinMax& size_json = response_size.Add({{"content_type", "json"}});
306305

307306
size_html.Observe(1024);
308307
size_html.Observe(512);
@@ -321,7 +320,7 @@ void test_main_5() {
321320
// main
322321
// =============================================================================
323322

324-
int main() {
323+
int main () {
325324
std::cout << "=== Custom metric class (min_max_t) — all usage variants ===\n";
326325

327326
try {

examples/check_global_objects.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
/*
2+
* prometheus-cpp-lite — header-only C++ library for exposing Prometheus metrics
3+
* https://github.com/biaks/prometheus-cpp-lite
4+
*
5+
* Copyright (c) 2026 Yan Kryukov ianiskr@gmail.com
6+
* Licensed under the MIT License
7+
*
8+
* =============================================================================
9+
* check_global_objects.cpp — Global objects usage example
10+
*
11+
* Demonstrates using pre-defined global objects from prometheus-cpp-lite-full:
12+
* global_registry, file_saver, http_server, http_pusher.
13+
* All metric types are created with the global registry and exposed via
14+
* all three export modes simultaneously (HTTP pull, HTTP push, file).
15+
*
16+
*/
117

218
#include <prometheus/prometheus.h>
319

420
using namespace prometheus;
521

6-
int main() {
22+
int main () {
723

824
counter_metric_t requests ("http_requests_total", "Total requests");
925
gauge_metric_t active ("active_connections", "Open connections");

examples/provide_via_http_pull_advanced.cpp

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,34 @@ using namespace prometheus;
3939
// curl http://localhost:9100/metrics
4040
// =============================================================================
4141

42-
void example_simple() {
43-
std::cout << "\n=== Example 1: Simple mode — single registry at /metrics ===\n";
44-
std::cout << " Listening on http://localhost:9100/metrics for 10 seconds...\n\n";
42+
void example_simple () {
43+
std::cout << "\n=== Example 1: Simple mode — single registry at /metrics ===\n";
44+
std::cout << " Listening on http://localhost:9100/metrics for 10 seconds...\n\n";
4545

4646
// 1. Create a shared registry.
47-
std::shared_ptr<registry_t> registry = std::make_shared<registry_t>();
47+
auto registry = std::make_shared<registry_t>();
4848

4949
// 2. Create metrics inside it.
5050
counter_metric_t requests (registry, "http_requests_total", "Total HTTP requests");
5151
gauge_metric_t active (registry, "active_connections", "Currently open connections");
5252

5353
// 3. Start the HTTP server — metrics are now available at http://localhost:9100/metrics
54-
http_server_t server({{127,0,0,1}, 9100}, registry);
54+
http_server_t server ({{127,0,0,1}, 9100}, registry, "/metrics", log_e::info);
5555

5656
// Simulate application work.
5757
for (int i = 0; i < 10; ++i) {
5858
std::this_thread::sleep_for(std::chrono::seconds(1));
5959

60-
int rnd = std::rand() % 10;
61-
requests += rnd;
62-
active.Set(5 + std::rand() % 20);
60+
requests += std::rand() % 10;
61+
active = 5 + std::rand() % 20;
6362

6463
std::cout << " [simple] tick " << (i + 1) << "/10"
65-
<< " requests=" << requests.Get()
66-
<< " active=" << active.Get() << std::endl;
64+
<< " requests=" << requests.Get()
65+
<< " active=" << active.Get() << std::endl;
6766
}
6867

6968
// Server stops automatically when it goes out of scope.
70-
std::cout << " Stopped.\n";
69+
std::cout << " Stopped.\n";
7170
}
7271

7372
// =============================================================================
@@ -80,55 +79,55 @@ void example_simple() {
8079
// curl http://localhost:9200/metrics/sys
8180
// =============================================================================
8281

83-
void example_multipath() {
84-
std::cout << "\n=== Example 2: Multi-path mode — multiple registries at different paths ===\n"
85-
<< " Listening on http://localhost:9200 for 10 seconds...\n"
86-
<< " /metrics/app — application metrics\n"
87-
<< " /metrics/sys — system metrics\n\n";
82+
void example_multipath () {
83+
std::cout << "\n=== Example 2: Multi-path mode — multiple registries at different paths ===\n"
84+
<< " Listening on http://localhost:9200 for 10 seconds...\n"
85+
<< " /metrics/app — application metrics\n"
86+
<< " /metrics/sys — system metrics\n\n";
8887

8988
// 1. Create separate registries for different metric domains.
90-
std::shared_ptr<registry_t> app_registry = std::make_shared<registry_t>();
91-
std::shared_ptr<registry_t> sys_registry = std::make_shared<registry_t>();
89+
auto app_registry = std::make_shared<registry_t>();
90+
auto sys_registry = std::make_shared<registry_t>();
9291

9392
// 2. Populate each registry with its own metrics.
94-
counter_metric_t orders_total (app_registry, "orders_total", "Total orders processed");
95-
gauge_metric_t queue_size (app_registry, "queue_size", "Current queue depth");
93+
counter_metric_t orders_total (app_registry, "orders_total", "Total orders processed");
94+
gauge_metric_t queue_size (app_registry, "queue_size", "Current queue depth");
9695

9796
gauge_metric_t cpu_usage (sys_registry, "cpu_usage_percent", "CPU usage percentage");
9897
gauge_metric_t memory_used_mb (sys_registry, "memory_used_mb", "Memory usage in MB");
9998

10099
// 3. Create the server and register each registry under its own path.
101100
http_server_t server;
102-
server.add_endpoint(app_registry, "/metrics/app");
103-
server.add_endpoint(sys_registry, "/metrics/sys");
104-
server.start({{127,0,0,1}, 9200});
101+
server.add_endpoint (app_registry, "/metrics/app");
102+
server.add_endpoint (sys_registry, "/metrics/sys");
103+
server.start ({{127,0,0,1}, 9200}, log_e::info);
105104

106105
// Simulate application work.
107106
for (int i = 0; i < 10; ++i) {
108107
std::this_thread::sleep_for(std::chrono::seconds(1));
109108

110-
orders_total += 1 + std::rand() % 5;
111-
queue_size.Set(std::rand() % 50);
109+
orders_total += 1 + std::rand() % 5;
110+
queue_size = std::rand() % 50;
112111

113-
cpu_usage.Set(10 + std::rand() % 80);
114-
memory_used_mb.Set(200 + std::rand() % 300);
112+
cpu_usage = 10 + std::rand() % 80;
113+
memory_used_mb = 200 + std::rand() % 300;
115114

116115
std::cout << " [multi] tick " << (i + 1) << "/10"
117-
<< " orders=" << orders_total.Get()
118-
<< " queue=" << queue_size.Get()
119-
<< " cpu=" << cpu_usage.Get() << "%"
120-
<< " mem=" << memory_used_mb.Get() << "MB" << std::endl;
116+
<< " orders=" << orders_total.Get()
117+
<< " queue=" << queue_size.Get()
118+
<< " cpu=" << cpu_usage.Get() << "%"
119+
<< " mem=" << memory_used_mb.Get() << "MB" << std::endl;
121120
}
122121

123122
// Server stops automatically when it goes out of scope.
124-
std::cout << " Stopped.\n";
123+
std::cout << " Stopped.\n";
125124
}
126125

127126
// =============================================================================
128127
// main
129128
// =============================================================================
130129

131-
int main() {
130+
int main () {
132131
std::cout << "=== HTTP Pull endpoint examples ===\n";
133132
std::cout << "Use curl or a browser to fetch metrics while the examples run.\n";
134133

examples/provide_via_http_pull_simple.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
* prometheus-cpp-lite — header-only C++ library for exposing Prometheus metrics
43
* https://github.com/biaks/prometheus-cpp-lite
@@ -19,18 +18,21 @@
1918

2019
using namespace prometheus;
2120

22-
int main() {
21+
int main () {
2322

2423
registry_t registry;
2524
counter_metric_t metric (registry, "metric1_name", "description1");
26-
http_server_t puller (registry, "127.0.0.1:9100");
25+
http_server_t puller (registry, "127.0.0.1:9100", "/metrics", log_e::info);
2726

2827
// now our metrics always available at http://localhost:9100/metrics
2928

29+
std::cout << "HTTP pull server started on http://localhost:9100/metrics\n"
30+
<< "Use: curl http://localhost:9100/metrics or open in browser\n" << std::endl;
31+
3032
for (;; ) {
3133
std::this_thread::sleep_for(std::chrono::seconds(1));
32-
const int random_value = std::rand();
33-
metric += random_value % 10;
34+
metric += std::rand() % 10;
35+
std::cout << " metric1_name = " << metric.Get() << std::endl;
3436
}
3537

3638
}

0 commit comments

Comments
 (0)