How open5gs/prometheus-client-c works
Project Type & Purpose
• Type: An open-source C library for instrumenting applications with Prometheus metrics.
• Purpose:
– libprom: core API for creating counters, gauges, histograms, collector registries, etc.
– libpromhttp: thin HTTP handler (built on libmicrohttpd) to expose metrics over HTTP for scraping by Prometheus.
– promtest & example: test harnesses and usage examples.
– auto / Docker / Make: development automation (build, test, smoke tests).
High-Level Components
A. Application Code (example/, user binaries)
B. Core Library (libprom)
• Metric types (counter, gauge, histogram)
• Collector registry, process/OS collectors (fds, limits, stat)
• Internal data structures (linked list, map, string builder)
• pthreads for concurrency
C. HTTP Layer (libpromhttp)
• Wraps libmicrohttpd to expose /metrics endpoint
• Pulls data from libprom registry, formats for Prometheus
D. External Services & Dependencies
• Prometheus Server (scrapes HTTP endpoint)
• libmicrohttpd (HTTP server library)
• POSIX (pthreads, procfs for process stats)
E. Development Tooling
• auto/ scripts, Makefile, Dockerfile – orchestrate build, test, docs
• CI pipeline (.github/workflows/ci.yaml)
• Doxygen docs (docs/*)
• promtest – unit/integration tests
Relationships & Data Flow
1. Application invokes libprom API to register metrics and update values at runtime.
2. libprom stores metrics in in-memory registry.
3. libpromhttp registers an HTTP handler with libmicrohttpd; on HTTP GET /metrics:
a. libpromhttp queries libprom registry for current metric values.
b. libpromhttp formats metrics in Prometheus text exposition format.
4. Prometheus server periodically issues HTTP GET requests to the libpromhttp endpoint, collects metrics.
5. Development pipeline leverages Docker/Make to: compile both libraries, run unit tests (prom/test, promtest), run smoke tests (example), and generate Doxygen docs.
Architectural Patterns & Principles
• Layered modular design:
– Core (“business logic”) layer (libprom)
– Adapter layer (libpromhttp + libmicrohttpd)
– Application layer (example, user code)
• Separation of concerns: metric creation vs. HTTP exposure vs. storage vs. build tooling.
• Dependency inversion: libprom has zero third-party dependencies; libpromhttp depends on libmicrohttpd.
Critical Elements to Capture
• Separation between libprom and libpromhttp.
• The registry as central data store.
• libmicrohttpd as external HTTP dependency.
• Prometheus as external scraper.
• Build automation and CI orchestrating compilation, testing, and docs.