Skip to content

Commit a45db33

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 a45db33

21 files changed

Lines changed: 689 additions & 193 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: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
[![Build examples](https://github.com/biaks/prometheus-cpp-lite/actions/workflows/cmake.yml/badge.svg)](https://github.com/biaks/prometheus-cpp-lite/actions/workflows/cmake.yml)
44
[![Build multi-platform](https://github.com/biaks/prometheus-cpp-lite/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/biaks/prometheus-cpp-lite/actions/workflows/cmake-multi-platform.yml)
55

6-
**Crossplatform header-only C++ library for quickly adding metrics (and profiling) functionality to C++ projects - simple, fast, dependency-free.**
76

8-
```cpp
7+
**Crossplatform header-only C++ library for quickly adding metrics (and profiling) functionality to C++ projects — simple, fast, dependency-free.**
8+
9+
**Simplest usage — global registry, no boilerplate:**
10+
11+
```
912
#include <prometheus/prometheus.h>
1013
1114
int main() {
12-
prometheus::registry_t registry;
13-
prometheus::counter_metric_t requests (registry, "http_requests_total", "Total HTTP requests");
14-
prometheus::http_server_t server (registry, "127.0.0.1:9100");
15-
16-
// metrics are now available at http://localhost:9100/metrics
15+
prometheus::counter_metric_t requests ("http_requests_total", "Total HTTP requests");
16+
prometheus::http_server.start ("127.0.0.1:9100");
1717
1818
for (;;) {
1919
std::this_thread::sleep_for(std::chrono::seconds(1));
@@ -22,6 +22,28 @@ int main() {
2222
}
2323
```
2424

25+
**Full control — registry, families, labels, value types:**
26+
27+
```
28+
#include <prometheus/prometheus.h>
29+
30+
int main() {
31+
prometheus::registry_t registry;
32+
prometheus::family_t reqs (registry, "http_requests_total", "HTTP requests", {{"host", "web01"}});
33+
prometheus::counter_t<double&> get (reqs, {{"method", "GET"}, {"status", "200"}});
34+
prometheus::counter_t<double&> post (reqs, {{"method", "POST"}, {"status", "201"}});
35+
prometheus::gauge_metric_t conn (registry, "active_connections", "Open connections", {{"host", "web01"}});
36+
prometheus::http_server_t server (registry, "127.0.0.1:9100", "/metrics", log_e::info);
37+
38+
for (;;) {
39+
std::this_thread::sleep_for(std::chrono::seconds(1));
40+
get += 1.5;
41+
post += 0.75;
42+
conn = 10 + std::rand() % 50;
43+
}
44+
}
45+
```
46+
2547
---
2648

2749

@@ -37,7 +59,7 @@ The main C++ Prometheus library - [jupp0r/prometheus-cpp](https://github.com/jup
3759
| Build system | Bazel (primary) + CMake | CMake or **just copy files** |
3860
| Minimum boilerplate | ~20 lines to create one counter | **2 lines** to create a counter and expose it (with global registry, see below) |
3961
| Value types | `double` only | `uint64_t` (default), `double`, `int64_t`, or any arithmetic type |
40-
| Metric types | counter, gauge, histogram, summary | `counter`, `gauge`, `histogram`, `summary`, `info`, **`benchmark`** or you can make your own custom metric class (see `add_custom_metric_class.cpp` for a complete working example of a new user-defined metric `min_max_t`) |
62+
| Metric types | `counter`, `gauge`, `histogram`, `summary`, `info` | [`counter`](examples/test_counter.cpp), [`gauge`](examples/test_gauge.cpp), [`histogram`](examples/test_histogram.cpp), [`summary`](examples/test_summary.cpp), `info`, **[`benchmark`](examples/test_benchmark.cpp)** - or [make your own](examples/add_custom_metric_class.cpp) |
4163
| C++ standard | C++11 | C++11 |
4264
| Thread-safe | Yes | Yes |
4365

@@ -62,12 +84,12 @@ will work.
6284
- **Low entry barrier** - a working counter with HTTP export is 2 lines of code (with global registry, see below)
6385
- **Gradual complexity** - start simple, add families/labels/registries/custom types when needed
6486
- **Multiple value types** - `uint64_t` (fast integer), `double` (Prometheus-compatible), `int64_t`, or custom
65-
- **Six metric types** - `counter`, `gauge`, `histogram`, `summary`, `benchmark`, `info`
87+
- **Six metric types** - [`counter`](examples/test_counter.cpp), [`gauge`](examples/test_gauge.cpp), [`histogram`](examples/test_histogram.cpp), [`summary`](examples/test_summary.cpp), `info`, [`benchmark`](examples/test_benchmark.cpp)
6688
- **Three export modes** - HTTP pull server, HTTP push (Pushgateway), file (node_exporter textfile)
6789
- **Simplest way with global_registry** - add metrics anywhere in your code without passing registry references
6890
- **prometheus-cpp compatible** - supports the same Builder/Family/Registry API style
69-
- **Extensible** - each metric type is self-contained in one header; **you can add your own metric by following the same pattern** (see `add_custom_metric_class.cpp` example)
70-
- **Detailed examples** - see the examples folder for usage patterns
91+
- **Extensible** - each metric type is self-contained in one header; **you can [add your own](examples/add_custom_metric_class.cpp) metric by following the same pattern**
92+
- **Detailed examples** - see the [examples](examples) folder for usage patterns
7193

7294
---
7395

@@ -149,7 +171,7 @@ int main() {
149171

150172
### 1. HTTP pull (recommended for long-running services)
151173

152-
The application runs an HTTP server. Prometheus scrapes it periodically.
174+
The application runs an HTTP server. Prometheus scrapes it periodically ([example](examples/provide_via_http_pull_simple.cpp)).
153175

154176
```cpp
155177
#include <prometheus/prometheus.h>
@@ -160,7 +182,7 @@ prometheus::http_server_t server (registry, "127.0.0.1:9100");
160182
// → http://localhost:9100/metrics
161183
```
162184
163-
**Multiple endpoints** for different metric domains:
185+
**Multiple endpoints** for different metric domains ([example](examples/provide_via_http_pull_advanced.cpp)):
164186
165187
```cpp
166188
using namespace prometheus;
@@ -177,7 +199,7 @@ server.start({{127,0,0,1}, 9200});
177199

178200
### 2. HTTP push (for short-lived jobs, edge devices)
179201

180-
Metrics are POSTed to a Pushgateway or VictoriaMetrics at a fixed interval.
202+
Metrics are POSTed to a Pushgateway or VictoriaMetrics at a fixed interval ([example](examples/provide_via_http_push_simple.cpp)).
181203

182204
```cpp
183205
#include <prometheus/prometheus.h>
@@ -189,7 +211,7 @@ prometheus::http_pusher_t pusher (registry, std::chrono::seconds(5), "http://
189211
190212
### 3. File export (for node_exporter textfile collector)
191213
192-
Metrics are written to a `.prom` file at a fixed interval.
214+
Metrics are written to a `.prom` file at a fixed interval ([example](examples/provide_via_textfile.cpp)).
193215
194216
```cpp
195217
#include <prometheus/prometheus.h>
@@ -275,7 +297,8 @@ With prometheus-cpp-lite's global registry approach, none of that is
275297
necessary. The `prometheus-cpp-lite-full` CMake target provides ready-made
276298
global objects — `global_registry`, `file_saver`, `http_pusher`, and
277299
`http_server` — so you can create metrics anywhere in your code and start
278-
exposing them with a single call from any place. No plumbing required.
300+
exposing them with a single call from any place ([example](examples/check_global_objects.cpp)).
301+
No plumbing required.
279302

280303
### The workflow
281304

@@ -319,7 +342,8 @@ prometheus::http_server.start("127.0.0.1:9100");
319342
```
320343

321344
That's it. Every metric you created in step 2 is automatically available
322-
at the HTTP endpoint. You can also use the other pre-defined global
345+
at the HTTP endpoint ([example](examples/use_metrics_in_class_simple.cpp)).
346+
You can also use the other pre-defined global
323347
exporters:
324348

325349
```cpp
@@ -387,7 +411,8 @@ void process_order(const Order& order) {
387411

388412
The accumulated elapsed time is immediately available at your `/metrics`
389413
endpoint — no separate profiling tool, no recompilation with special flags,
390-
no post-hoc analysis. You get real production latency data from live traffic.
414+
no post-hoc analysis. You get real production latency data from live traffic
415+
([example](examples/use_benchmark_in_class_simple.cpp)).
391416

392417
**Best practice for multithreaded code:** the `start()`/`stop()` state
393418
machine is local to each metric instance and is intentionally not
@@ -481,7 +506,7 @@ not even the `#include` lines need to be modified.
481506

482507
### Example 1: Exposer (HTTP pull)
483508

484-
The code below is **valid prometheus-cpp code**. It compiles and runs with
509+
The code below is **[valid prometheus-cpp code](examples/legacy_prometheus_cpp.cpp)**. It compiles and runs with
485510
prometheus-cpp-lite without touching a single line — just swap the library
486511
in your build system:
487512

@@ -748,16 +773,45 @@ prometheus::counter_metric_t metric2 (registry, "standalone", "help", labels);
748773
749774
750775
751-
## Building examples
776+
## Examples
752777
753-
```cmake
778+
All examples are in the [`examples/`](examples/) directory. Build them with:
779+
780+
```
754781
cmake -B build -G Ninja -DPROMETHEUS_BUILD_EXAMPLES=ON
755782
cmake --build build
756783
```
757784
758-
The examples use `prometheus-cpp-lite` (header-only target) and define
759-
`global_registry` in each `.cpp` file. To use the pre-defined global
760-
objects instead, link against `prometheus-cpp-lite-full`.
785+
786+
| Example | Description |
787+
|---------|-------------|
788+
| **Quick start** | |
789+
| [`quick_start.cpp`](examples/quick_start.cpp) | All metric types + HTTP server in one file. The code from the README "All metric types at a glance" section. |
790+
| **Export modes** | |
791+
| [`provide_via_http_pull_simple.cpp`](examples/provide_via_http_pull_simple.cpp) | Shortest HTTP pull server — 3 lines of setup, `curl http://localhost:9100/metrics`. |
792+
| [`provide_via_http_pull_advanced.cpp`](examples/provide_via_http_pull_advanced.cpp) | Single-registry and multi-path (multiple registries at different URLs) HTTP pull examples. |
793+
| [`provide_via_http_push_simple.cpp`](examples/provide_via_http_push_simple.cpp) | Shortest periodic push to Pushgateway — 3 lines of setup. |
794+
| [`provide_via_http_push_advanced.cpp`](examples/provide_via_http_push_advanced.cpp) | Periodic push, on-demand Push/PushAdd/Delete (Gateway API), and async push examples. |
795+
| [`provide_via_textfile.cpp`](examples/provide_via_textfile.cpp) | Periodic save to `.prom` file for node_exporter textfile collector. |
796+
| **Metric types — all API levels** | |
797+
| [`test_counter.cpp`](examples/test_counter.cpp) | Counter (`uint64_t`) — every way to create: global/explicit registry, untyped/typed family, all legacy APIs. |
798+
| [`test_gauge.cpp`](examples/test_gauge.cpp) | Gauge (`int64_t`) — same full set of API variants. |
799+
| [`test_histogram.cpp`](examples/test_histogram.cpp) | Histogram (`double`) — buckets, custom boundaries, same full set of API variants. |
800+
| [`test_summary.cpp`](examples/test_summary.cpp) | Summary (`double`) — quantiles, custom quantile definitions, same full set of API variants. |
801+
| [`test_benchmark.cpp`](examples/test_benchmark.cpp) | Benchmark (`double`) — profile method execution time, same full set of API variants. |
802+
| **Global objects** | |
803+
| [`check_global_objects.cpp`](examples/check_global_objects.cpp) | Uses `prometheus-cpp-lite-full` cmake target with pre-defined globals: `global_registry`, `file_saver`, `http_server`, `http_pusher`. |
804+
| **Using metrics in classes** | |
805+
| [`use_metrics_in_class_simple.cpp`](examples/use_metrics_in_class_simple.cpp) | Simplest way to add metrics to a class — declare as members, increment in methods, expose via `http_server.start()`. |
806+
| [`use_benchmark_in_class_simple.cpp`](examples/use_benchmark_in_class_simple.cpp) | Profile method execution time with `benchmark_metric_t` — `start()`/`stop()` around code phases. |
807+
| [`use_metrics_in_class_advanced.cpp`](examples/use_metrics_in_class_advanced.cpp) | Dynamic per-instance metrics with labels — connection pool where each connection creates metrics at runtime via `make_metric<>()` without storing families. |
808+
| **Compatibility** | |
809+
| [`legacy_prometheus_cpp.cpp`](examples/legacy_prometheus_cpp.cpp) | Unmodified prometheus-cpp code (`Exposer`, `BuildCounter`, `Registry`) — compiles as-is with prometheus-cpp-lite. |
810+
| **Extensibility** | |
811+
| [`add_custom_metric_class.cpp`](examples/add_custom_metric_class.cpp) | How to create your own metric type (`min_max_t` — tracks min/max of observed values). Demonstrates owning/reference forms, all family wrappers, and the Builder API. |
812+
813+
814+
761815
762816
---
763817

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

0 commit comments

Comments
 (0)