|
cwist
Modern C web framework with libttak runtime
|
Production-grade metrics engine for CWIST. More...
#include <stddef.h>#include <stdint.h>#include <stdbool.h>#include <stdatomic.h>

Go to the source code of this file.
Classes | |
| struct | cwist_metric_value |
| Atomic metric value container. More... | |
| struct | cwist_metric |
| Single metric definition. More... | |
Typedefs | |
| typedef enum cwist_metric_type | cwist_metric_type_t |
| Supported Prometheus metric kinds. | |
| typedef struct cwist_metric_value | cwist_metric_value_t |
| Atomic metric value container. | |
| typedef struct cwist_metric | cwist_metric_t |
| Single metric definition. | |
| typedef enum cwist_metric_id | cwist_metric_id_t |
| Pre-defined framework metrics. | |
| typedef struct cwist_metrics_registry | cwist_metrics_registry_t |
| Global metrics registry (singleton, lazy-initialised). | |
Production-grade metrics engine for CWIST.
Exposes Prometheus-compatible text format via a /metrics endpoint. All counters are updated with lock-free atomic operations to ensure zero performance degradation on the hot path.
Definition in file metrics.h.
Pre-defined framework metrics.
Single metric definition.
Supported Prometheus metric kinds.
Atomic metric value container.
| typedef struct cwist_metrics_registry cwist_metrics_registry_t |
Pre-defined framework metrics.
| void cwist_metric_add | ( | cwist_metrics_registry_t * | reg, |
| cwist_metric_id_t | id, | ||
| uintmax_t | delta | ||
| ) |
Increment a counter metric by delta.
| reg | Registry. |
| id | Metric identifier. |
| delta | Amount to add. |
Increment a counter metric by delta.
Lock-free relaxed atomic update; safe to call from any thread.
| reg | Registry holding the metric; NULL is a no-op. |
| id | Metric identifier; out-of-range ids are ignored. |
| delta | Value increment to add. |
Definition at line 194 of file metrics.c.
References cap, and CWIST_METRIC_COUNT.
Referenced by cwist_mw_metrics_handler().
| void cwist_metric_inc | ( | cwist_metrics_registry_t * | reg, |
| cwist_metric_id_t | id | ||
| ) |
Increment a counter metric by 1.
| reg | Registry. |
| id | Metric identifier. |
Increment a counter metric by 1.
Lock-free relaxed atomic update; safe to call from any thread.
| reg | Registry holding the metric; NULL is a no-op. |
| id | Metric identifier; out-of-range ids are ignored. |
Definition at line 179 of file metrics.c.
References cap, and CWIST_METRIC_COUNT.
Referenced by cwist_http_async_rearm(), cwist_http_receive_request(), cwist_http_receive_request_nb(), and cwist_mw_metrics_handler().
| uintmax_t cwist_metric_load | ( | const cwist_metrics_registry_t * | reg, |
| cwist_metric_id_t | id | ||
| ) |
Atomically load the current raw value of a metric.
| reg | Registry. |
| id | Metric identifier. |
Atomically load the current raw value of a metric.
Acquire load of the stored raw (fixed-point scaled) value. For CWIST_METRIC_HTTP_CONTINUATION_SHED the value is read live from the net/http shed counter instead of the registry.
| reg | Registry holding the metric; NULL returns 0. |
| id | Metric identifier; out-of-range ids return 0. |
Definition at line 247 of file metrics.c.
References cap, cwist_http_continuation_shed_count(), CWIST_METRIC_COUNT, and CWIST_METRIC_HTTP_CONTINUATION_SHED.
| void cwist_metric_observe | ( | cwist_metrics_registry_t * | reg, |
| cwist_metric_id_t | id, | ||
| long double | value | ||
| ) |
Observe a value into a histogram-like metric.
| reg | Registry. |
| id | Metric identifier. |
| value | Observed value. |
Observe a value into a histogram-like metric.
The value is stored with fixed-point scaling (roundl(value * 1000)), added to both the raw accumulated value and the sum, while the observation count is incremented by one. Lock-free relaxed atomic update; safe to call from any thread.
| reg | Registry holding the metric; NULL is a no-op. |
| id | Metric identifier; out-of-range ids are ignored. |
| value | Observed sample value. |
Definition at line 228 of file metrics.c.
References cap, and CWIST_METRIC_COUNT.
| void cwist_metric_set | ( | cwist_metrics_registry_t * | reg, |
| cwist_metric_id_t | id, | ||
| long double | value | ||
| ) |
Set a gauge metric to an absolute value.
| reg | Registry. |
| id | Metric identifier. |
| value | New absolute value (internally scaled by 1000). |
The value is stored with fixed-point scaling: the raw value is roundl(value * 1000), so one unit is 0.001 in rendered output.
| reg | Registry holding the metric; NULL is a no-op. |
| id | Metric identifier; out-of-range ids are ignored. |
| value | New absolute value. |
Definition at line 210 of file metrics.c.
References cap, and CWIST_METRIC_COUNT.
| cwist_metrics_registry_t * cwist_metrics_registry | ( | void | ) |
Obtain the global registry, creating it on first call.
Obtain the global registry, creating it on first call.
Allocates and initializes the registry on first call; later calls return the cached pointer. All metric atomics are zero-initialized from the static metric_defaults table.
Definition at line 112 of file metrics.c.
References cap, cwist_alloc(), CWIST_METRIC_COUNT, and cwist_metrics_registry().
Referenced by cwist_http_async_rearm(), cwist_http_receive_request(), cwist_http_receive_request_nb(), cwist_metrics_registry(), cwist_metrics_serve_http(), and cwist_mw_metrics_handler().
| char * cwist_metrics_render_prometheus | ( | const cwist_metrics_registry_t * | reg | ) |
Render all metrics in Prometheus text exposition format.
The returned string is heap-allocated and must be freed by the caller using standard free().
| reg | Registry to render. |
Render all metrics in Prometheus text exposition format.
Before rendering, synchronizes the HTTP_CONTINUATION_SHED metric from the live net/http shed counter into the registry (cast away const for that store). The output buffer grows by doubling and is heap-allocated; the caller owns it.
| reg | Registry to render; NULL returns NULL. |
Definition at line 286 of file metrics.c.
References cap, count, cwist_http_continuation_shed_count(), CWIST_METRIC_COUNT, and CWIST_METRIC_HTTP_CONTINUATION_SHED.
Referenced by cwist_metrics_serve_http().
| void cwist_metrics_reset | ( | cwist_metrics_registry_t * | reg | ) |
Reset all metrics in the registry to zero.
| reg | Registry to clear. |
Reset all metrics in the registry to zero.
| reg | Registry to reset; NULL is a no-op. |
Definition at line 133 of file metrics.c.
References cap, and CWIST_METRIC_COUNT.
| void cwist_metrics_serve_http | ( | cwist_http_response * | res | ) |
Convenience helper: populate an HTTP response with /metrics body.
Sets Content-Type to text/plain; version=0.0.4 and fills the body with the current Prometheus snapshot.
| res | Response to populate. |
Convenience helper: populate an HTTP response with /metrics body.
On success sets the response status to 200 OK with the exposition text as body and a Prometheus text Content-Type header. On render failure sets 500 INTERNAL_ERROR with a "metrics render failed" body.
| res | Response to fill; NULL is a no-op. The body sstring is assigned via cwist_sstring_assign. |
Definition at line 366 of file metrics.c.
References cwist_http_response::body, cap, cwist_http_header_add(), CWIST_HTTP_INTERNAL_ERROR, CWIST_HTTP_OK, cwist_metrics_registry(), cwist_metrics_render_prometheus(), cwist_sstring_assign(), cwist_http_response::headers, and cwist_http_response::status_code.