|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Advanced Prometheus metrics middleware with dimensional labels and histograms. More...
#include <stdarg.h>#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <uv.h>#include "csilk/csilk.h"
Data Structures | |
| struct | csilk_route_metric_t |
| Data structure for a specific combination of labels. More... | |
Macros | |
| #define | CSILK_METRICS_MAX_ENTRIES 1024 |
| Maximum unique combinations of {method, route, status} tracked. | |
| #define | CSILK_METRICS_BUCKET_COUNT 6 |
| Number of latency histogram buckets. | |
Functions | |
| static uint32_t | metrics_hash (const char *method, const char *route, int status) |
| Computes a hash for the metric dimension triple. | |
| static csilk_route_metric_t * | get_metric_slot (const char *method, const char *route, int status) |
| Finds or allocates a thread-safe slot in the metrics hash table. | |
| void | csilk_security_get_stats (csilk_security_stats_t *stats) |
| void | csilk_process_get_stats (csilk_process_stats_t *stats) |
| void | _csilk_metrics_inc_rate_limit_blocks (void) |
| void | _csilk_metrics_inc_csrf_violations (void) |
| void | _csilk_metrics_inc_auth_failures (void) |
| void | csilk_metrics_middleware (csilk_ctx_t *c, const char *arg) |
| Dimensional metrics middleware. | |
| static void | append_metric (char **buf, size_t *size, size_t *offset, const char *fmt,...) |
| Dynamic string builder for Prometheus text format. | |
| void | csilk_metrics_handler (csilk_ctx_t *c) |
| Handler for the /metrics endpoint. | |
| uint64_t | csilk_metrics_get_total_requests (void) |
| uint64_t | csilk_metrics_get_total_duration (void) |
Variables | |
| static const double | CSILK_METRICS_BUCKETS [CSILK_METRICS_BUCKET_COUNT] |
| Latency buckets in seconds for P99/P95 analysis. | |
| static _Atomic uint64_t | http_requests_total = 0 |
| Atomic counter for total HTTP requests across all workers. | |
| static _Atomic uint64_t | http_request_duration_microseconds = 0 |
| Atomic counter for cumulative request duration in microseconds. | |
| static _Atomic uint64_t | security_rate_limit_blocks = 0 |
| static _Atomic uint64_t | security_csrf_violations = 0 |
| static _Atomic uint64_t | security_auth_failures = 0 |
| static csilk_route_metric_t | route_metrics [CSILK_METRICS_MAX_ENTRIES] = {0} |
| Fixed-size hash table for route metrics using open addressing. | |
Advanced Prometheus metrics middleware with dimensional labels and histograms.
This module implements a high-performance metrics collection system for the csilk framework. It supports:
| struct csilk_route_metric_t |
Data structure for a specific combination of labels.
Tracks requests that match a specific HTTP method, status code, and route pattern (e.g., GET 200 /api/users/:id).
| Data Fields | ||
|---|---|---|
| _Atomic uint64_t | buckets[CSILK_METRICS_BUCKET_COUNT+1] |
Histogram bucket counts |
| _Atomic uint64_t | count |
Total requests for this dimension |
| _Atomic uint64_t | duration_us |
Total duration in microseconds |
| _Atomic int | in_use |
Lock-free flag for slot occupation |
| char | method[12] |
HTTP method (GET, POST, etc.) |
| char | route[128] |
Matched route pattern (e.g., "/users/:id") |
| int | status |
HTTP response status code |
| #define CSILK_METRICS_BUCKET_COUNT 6 |
Number of latency histogram buckets.
| #define CSILK_METRICS_MAX_ENTRIES 1024 |
Maximum unique combinations of {method, route, status} tracked.
Once this limit is reached, new combinations will not be tracked to prevent memory exhaustion. 1024 is typically sufficient for most APIs.
| void _csilk_metrics_inc_auth_failures | ( | void | ) |
| void _csilk_metrics_inc_csrf_violations | ( | void | ) |
| void _csilk_metrics_inc_rate_limit_blocks | ( | void | ) |
|
static |
Dynamic string builder for Prometheus text format.
Automatically reallocates the buffer as needed.
| uint64_t csilk_metrics_get_total_duration | ( | void | ) |
| uint64_t csilk_metrics_get_total_requests | ( | void | ) |
| void csilk_metrics_handler | ( | csilk_ctx_t * | c | ) |
Handler for the /metrics endpoint.
Prometheus /metrics endpoint handler.
Renders full system telemetry in standard Prometheus text-based format.
| void csilk_metrics_middleware | ( | csilk_ctx_t * | c, |
| const char * | arg | ||
| ) |
Dimensional metrics middleware.
Prometheus metrics middleware.
Wraps the request execution to measure latency and update atomic counters.
| void csilk_process_get_stats | ( | csilk_process_stats_t * | stats | ) |
| void csilk_security_get_stats | ( | csilk_security_stats_t * | stats | ) |
|
static |
Finds or allocates a thread-safe slot in the metrics hash table.
Implements a lock-free "claim" mechanism using atomic_compare_exchange.
| method | The HTTP method. |
| route | The route pattern. |
| status | The status code. |
|
static |
Computes a hash for the metric dimension triple.
Uses the djb2 algorithm for stable distribution across the hash table.
| method | The HTTP method string. |
| route | The route pattern string. |
| status | The HTTP status code. |
|
static |
Latency buckets in seconds for P99/P95 analysis.
These values define the upper bounds (le="...") for Prometheus histogram buckets.
|
static |
Atomic counter for cumulative request duration in microseconds.
|
static |
Atomic counter for total HTTP requests across all workers.
|
static |
Fixed-size hash table for route metrics using open addressing.
Statically allocated to ensure zero heap allocation during request processing. Initialized to zero (all slots available).
|
static |
|
static |
|
static |