/* * Minimal smoke test for libprom + libpromhttp. * * smoke_test -> register metrics, print exposition text, exit * smoke_test -> additionally serve /metrics on for seconds */ #include #include #include #include "microhttpd.h" #include "prom.h" #include "promhttp.h" int main(int argc, char **argv) { if (prom_collector_registry_default_init() != 0) return 1; prom_counter_t *requests = prom_collector_registry_must_register_metric( prom_counter_new("smoke_requests_total", "Requests handled by the smoke test", 1, (const char *[]){"path"})); prom_gauge_t *temperature = prom_collector_registry_must_register_metric(prom_gauge_new("smoke_temperature", "A test gauge", 0, NULL)); if (prom_counter_add(requests, 3, (const char *[]){"/api"})) return 1; if (prom_gauge_set(temperature, 21.5, NULL)) return 1; const char *text = prom_collector_registry_bridge(PROM_COLLECTOR_REGISTRY_DEFAULT); if (text == NULL) return 1; printf("%s", text); free((void *)text); if (argc == 3) { promhttp_set_active_collector_registry(NULL); struct MHD_Daemon *daemon = promhttp_start_daemon(MHD_USE_SELECT_INTERNALLY, (unsigned short)atoi(argv[1]), NULL, NULL); if (daemon == NULL) { fprintf(stderr, "failed to start HTTP daemon on port %s\n", argv[1]); return 1; } sleep((unsigned int)atoi(argv[2])); MHD_stop_daemon(daemon); } prom_collector_registry_destroy(PROM_COLLECTOR_REGISTRY_DEFAULT); return 0; }