|
cwist
Modern C web framework with libttak runtime
|
Health probe registry and JSON endpoint generation. More...
#include <cwist/sys/health/healthz.h>#include <cwist/core/utils/json_builder.h>#include <cwist/core/sstring/sstring.h>#include <cwist/core/mem/alloc.h>#include <string.h>#include <stdio.h>
Go to the source code of this file.
Macros | |
| #define | CWIST_HEALTHZ_MAX_PROBES 16 |
Typedefs | |
| typedef struct cwist_healthz_entry | cwist_healthz_entry_t |
Functions | |
| bool | cwist_healthz_register (const char *name, cwist_health_probe_fn fn, void *ctx) |
| Register or update a health probe. | |
| void | cwist_healthz_unregister (const char *name) |
| Deactivate a registered health probe. | |
| void | cwist_healthz_run (cwist_health_probe_t *out_probes, size_t max_probes, size_t *out_count, cwist_health_status_t *out_overall) |
| Run all active probes and aggregate their results. | |
| void | cwist_app_healthz (cwist_http_response *res) |
| Serve the /healthz HTTP endpoint as a JSON response. | |
Health probe registry and JSON endpoint generation.
Definition in file healthz.c.
| typedef struct cwist_healthz_entry cwist_healthz_entry_t |
| void cwist_app_healthz | ( | cwist_http_response * | res | ) |
Serve the /healthz HTTP endpoint as a JSON response.
Populate an HTTP response with the healthz JSON payload.
Runs all registered probes and writes a JSON body with an overall "status" field and a "probes" array of per-probe name, status, and optional message. The response Content-Type is set to application/json.
| res | Response to fill; NULL is a no-op. The body and headers are (re)assigned on success. |
Definition at line 168 of file healthz.c.
References cwist_http_response::body, cap, count, CWIST_HEALTH_DEGRADED, CWIST_HEALTH_FAIL, CWIST_HEALTH_OK, CWIST_HEALTHZ_MAX_PROBES, cwist_healthz_run(), cwist_http_header_add(), CWIST_HTTP_OK, CWIST_HTTP_SERVICE_UNAVAILABLE, cwist_json_add_string(), cwist_json_begin_array(), cwist_json_begin_object(), cwist_json_builder_create(), cwist_json_builder_destroy(), cwist_json_end_array(), cwist_json_end_object(), cwist_json_get_raw(), cwist_sstring_assign(), cwist_http_response::headers, and cwist_http_response::status_code.
| bool cwist_healthz_register | ( | const char * | name, |
| cwist_health_probe_fn | fn, | ||
| void * | ctx | ||
| ) |
Register or update a health probe.
Register a custom probe function.
If a probe with the same name is already active, its function and context are replaced in place. Otherwise the probe is appended to the registry.
| name | Probe name; must be non-NULL and outlive the registration. |
| fn | Probe function; must be non-NULL. |
| ctx | Opaque context passed to fn on each evaluation. |
name or fn is NULL or the registry is full. Definition at line 45 of file healthz.c.
References cap, and CWIST_HEALTHZ_MAX_PROBES.
| void cwist_healthz_run | ( | cwist_health_probe_t * | out_probes, |
| size_t | max_probes, | ||
| size_t * | out_count, | ||
| cwist_health_status_t * | out_overall | ||
| ) |
Run all active probes and aggregate their results.
Evaluate all probes and return the aggregate status.
Probes are evaluated in registration order. The overall status is FAIL if any probe reports FAIL, DEGRADED if at least one reports DEGRADED and none report FAIL, and OK otherwise.
| out_probes | Optional array receiving per-probe results; at most max_probes entries are written. |
| max_probes | Capacity of out_probes. |
| out_count | Optional output receiving the number of active probes evaluated. |
| out_overall | Optional output receiving the aggregated status. |
Definition at line 130 of file healthz.c.
References cap, count, CWIST_HEALTH_DEGRADED, CWIST_HEALTH_FAIL, CWIST_HEALTH_OK, and cwist_health_probe::status.
Referenced by cwist_app_healthz().
Deactivate a registered health probe.
Unregister a previously registered probe.
Marks every active entry with the given name as inactive; inactive entries are skipped by cwist_healthz_run and their slots may be reused by cwist_healthz_register. The name and function pointers are not cleared.
| name | Probe name to remove; NULL is a no-op. |
Definition at line 88 of file healthz.c.
References cap.