$ show prometheus/common
This repository is a collection of shared Go libraries used across Prometheus components. It is not a standalone application but a modular library providing core infrastructure for metric exposition formatting (text, protobuf, OpenMetrics), HTTP client configuration (authentication, TLS, OAuth2, proxy), metric model types and validation, logging, routing, and version information. The architecture is package-oriented with clear interfaces and separation of concerns. Key entry points include `config.NewClientFromConfig` for constructing HTTP round trippers with hot-reloadable TLS and credentials, `expfmt.NewTextParser` for parsing Prometheus text format, and `model.EscapeName` for metric name escaping. The codebase is well-tested, actively maintained, and exhibits strong engineering practices such as interface-based design, table-driven tests, and reusable build infrastructure (Makefile.common). It is primarily consumed by Prometheus server, exporters, and other ecosystem tools.
The LLM identified 5 files worth highlighting for reuse:
Provides `NewClientFromConfig` and `NewRoundTripperFromConfig` to construct HTTP clients with configurable authentication (basic, bearer, OAuth2, JWT), TLS with certificate hot-reload, proxy settings, and custom headers. The `TLSRoundTripper` re-reads TLS files on every request using SHA256 hash checks to detect changes, enabling certificate rotation without restart. This pattern of composable round tripper decorators is reusable for any Go HTTP client that needs dynamic security configuration.
Implements `EscapeName` and `UnescapeName` with three escaping schemes (underscore, dots, value encoding) used to convert between Prometheus metric names and their string representations. Also provides `ValidationScheme` for pluggable metric/label name validation (legacy vs UTF-8). This is a self-contained utility that can be extracted for any system needing to handle Prometheus-compatible naming conventions.
A hand-written state machine parser for the Prometheus text exposition format, converting raw text into `dto.MetricFamily` protobuf messages. It handles edge cases like comments, timestamps, and `_created` lines. Despite its complexity (~800 lines), it is isolated (depends only on external protobuf types) and demonstrates a robust parsing pattern that could be adapted for other line-based metric formats.
Provides a configurable logging wrapper around Go's `log/slog` package, supporting different output styles (GoKitStyle, LogStyle) and source file logging. Includes `AddFlags` for Kingpin flag registration. This is a clean abstraction that can be reused in any Go application needing structured logging with style selection.
A reusable Makefile template included by all Prometheus Go projects, providing standardized targets for building, testing, linting, Docker image creation (with multi-architecture and variant support), and release. It defines variables like `PROMU_VERSION` and `GOLANGCI_LINT_VERSION` and includes precheck commands. This pattern is directly reusable for any Go project wanting consistent CI/CD.