How prometheus/pushgateway works
Project type and purpose
• This is a single‐binary, monolithic HTTP service written in Go.
• Purpose: Accept Prometheus‐style metric “pushes” over HTTP, cache them (optionally persist), and expose them back for Prometheus to scrape.
File structure
• Top-level:
• main.go – starts HTTP server and routing
• api/v1 – JSON query & admin API handlers
• handler – HTTP endpoints under /metrics/... and /api/... for push, delete, status, wipe, metrics, etc.
• storage – in-memory metric store with optional file persistence
• asset & resources – embedded VFS assets and static web UI (HTML/CSS/JS)
• histogram – helpers for Prometheus histogram models
• testutil – testing helpers
• Config/build: Dockerfile, Makefile, .circleci, promu.yml for release builds
• README describes flags, usage, TLS/basic‐auth via external web.config
README for architecture cues
• Explains HTTP interface (push endpoints, admin/query API, management API)
• Mentions embedded static UI served under root (/)
• Talks about Prometheus client_golang for internal metrics, and optional persistence
Main components
a. Pushgateway HTTP Server
• Uses Go net/http + mux
• Config: flags (--web.listen-address, --persistence.file, TLS/auth)
b. HTTP Handlers
• Push handlers: POST/PUT /metrics/job/... → parse text or protobuf → update Storage
• Delete handler: DELETE /metrics/job/... → modify Storage
• Admin API: PUT /api/v1/admin/wipe → clear Storage
• Query API: GET /api/v1/status, /api/v1/metrics → read from Storage + build_info from Go client
• Management: /-/healthy, /-/ready, /-/quit
• Static UI handler: serves embedded or on‐disk assets from resources/static via Asset VFS layer
c. Storage Layer
• In‐memory map keyed by grouping labels
• Optional persistence module writes/reads to disk (--persistence.file)
• Ensures consistency checks on push
d. Internal Metrics Registry
• Exposes Prometheus Go client metrics (process_, go_, promhttp_, pushgateway_* metrics) on /metrics
• Augments pushed metrics with push_time_seconds and push_failure_time_seconds
e. Prometheus Scraper (external)
• Regularly scrapes /metrics on Pushgateway
Relationships and interactions
• Client libraries or curl → HTTP Pushgateway server (POST, PUT, DELETE) → Handlers → Storage
• Admin/Query clients → HTTP server → Handlers → Storage or internal state
• HTTP server → Internal Prometheus registry → /metrics endpoint
• Persistence module ↔ Storage (periodic serialize, load at startup)
• External Prometheus → Scrape /metrics → ingest pushed metrics + Pushgateway’s own metrics
Architectural patterns/design principles
• Layered architecture:
• Presentation/API Layer (HTTP handlers)
• Business logic (parsing, consistency checks)
• Storage abstraction (interface + disk store implementation)
• Single‐binary, dependency‐injection via flags & interfaces
• VFS embedding for static assets (asset package + go:generate)
• Use of Prometheus client library for internal metrics
Technologies, frameworks, libraries
• Go 1.x, net/http, gorilla/mux (or built‐in router)
• Prometheus client_golang
• go:embed / vfsdata for assets (asset package)
• Docker for containerization
• Makefile/promu for builds and release artifacts
• CircleCI, GitHub Actions for CI/CD