prometheus/prometheus
prometheus
Active contributors: roidelapluie, bjboreham, machine424, krajorama, bwplotka, aknuds1
Purpose
prometheus is the main server binary. It reads prometheus.yml, runs service discovery, scrapes targets, stores samples in the TSDB (or the agent WAL), evaluates recording and alerting rules, dispatches alerts to Alertmanagers, and serves the HTTP API and the React UI. A single instance is autonomous — Prometheus has no clustering or replication built in.
Directory layout
cmd/prometheus/
├── main.go # Entry point (~2,200 lines) — wires every subsystem.
├── features_test.go # Tests for /api/v1/features.
├── main_test.go # Wide-coverage e2e tests against a real server.
├── main_unix_test.go # Unix-specific signal-handling tests.
├── main_upgrade_test.go # End-to-end upgrade/downgrade tests.
├── query_log_test.go # Query log behaviour.
├── reload_test.go # SIGHUP reload coverage.
├── scrape_failure_log_test.go # Scrape failure logger.
├── upload_test.go # `--write-documentation` and asset upload tests.
└── testdata/ # Fixture configs and golden files.How it starts up
main() in cmd/prometheus/main.go is the largest function in the codebase (~2,200 lines, the longest single Go source file by line count). It does these things in order:
- Parse flags with
kingpininto aflagConfigstruct. Every flag's documentation is extracted by--write-documentationfordocs/command-line/prometheus.md. - Detect agent mode from
--agent. - Configure the runtime:
automaxprocsfor CPU,automemlimitfor memory, slog for logging, and the klog v1/v2 redirect. - Build the storage stack: either a
tsdb.DB(server mode) or atsdb/agent.DB(agent mode), wrapped in afanoutStoragethat fans writes to the local DB plus any remote write queues registered viastorage/remote.Storage.ApplyConfig. - Construct the managers:
discovery.NewManager,scrape.NewManager,rules.NewManager,notifier.NewManager. None of them are started yet. - Build the PromQL engine:
promql.NewEnginewithOptsfor max samples, lookback delta, query timeout, and per-step stats. - Build the HTTP API and UI:
web.New(...)with options assembled from the flag config. - Wire reload: a SIGHUP handler and the
/-/reloadendpoint both callreload(...), which callsApplyConfigon every manager. - Run everything via
oklog/run.Group: a manager per group member withExecute/Interrupt. Closing any group member cancels everything.
Two operating modes
graph LR
subgraph Server["Server mode (default)"]
SDS1[discovery.Manager] --> SCM1[scrape.Manager]
SCM1 --> ST1[fanoutStorage]
ST1 --> TSDB[(tsdb.DB)]
ST1 --> RW1[remote.QueueManager]
RM1[rules.Manager] -->|reads| TSDB
RM1 -->|alerts| NM1[notifier.Manager]
WEB1[web.Handler] -->|queries| TSDB
end
subgraph Agent["Agent mode (--agent)"]
SDS2[discovery.Manager] --> SCM2[scrape.Manager]
SCM2 --> ST2[fanoutStorage]
ST2 --> WAL[(tsdb/agent WAL)]
WAL --> RW2[remote.QueueManager]
WEB2[web.Handler<br/>limited routes]
endServer mode is the standard monitoring deployment. Agent mode is documented under Agent mode and forces enable-remote-write-receiver=false, disables the rule manager and query engine, and limits the API surface.
Key abstractions
| Type / Function | File | Role |
|---|---|---|
flagConfig |
cmd/prometheus/main.go |
All parsed CLI flags. Passed to subsystem option structs. |
reload(...) |
cmd/prometheus/main.go |
Hot-reload entry point; called by SIGHUP and /-/reload. |
loadConfig(...) |
cmd/prometheus/main.go |
Wraps config.Load with reload-failure metrics. |
runtimeInfo() |
cmd/prometheus/main.go |
Populates web/api/v1.RuntimeInfo from current process state. |
klogv1Writer |
cmd/prometheus/main.go |
Redirects klog v1 output into klog v2 / slog. |
CLI flags
Every flag is registered on a kingpin parser. Notable ones:
--config.file— path toprometheus.yml.--storage.tsdb.path/--storage.agent.path— data directory.--storage.tsdb.retention.time/--storage.tsdb.retention.size/--storage.tsdb.retention.percentage(3.11) — retention policies.--web.listen-address,--web.external-url,--web.route-prefix,--web.enable-lifecycle,--web.enable-admin-api,--web.enable-remote-write-receiver,--web.enable-otlp-receiver.--query.timeout,--query.max-concurrency,--query.max-samples,--query.lookback-delta,--query.log-file.--rules.alert.for-outage-tolerance,--rules.alert.for-grace-period,--rules.alert.resend-delay,--rules.max-concurrent-evals.--scrape.discovery-reload-interval,--scrape.timestamp-tolerance.--alertmanager.notification-queue-capacity,--alertmanager.drain-notification-queue-on-shutdown.--enable-feature=<comma-separated list>— see Feature flags.--agent— switch to agent mode.--log.level,--log.format— slog configuration.--web.config.file— path to aprometheus/exporter-toolkit/webconfig (TLS + basic auth).
The full list is regenerated to docs/command-line/prometheus.md via make cli-documentation.
Reload semantics
A reload (SIGHUP, /-/reload, or first config load) calls loadConfig -> cfg.Validate() -> for each manager, manager.ApplyConfig(cfg). If any ApplyConfig fails, the reload is aborted and the previous config remains in effect. Successful reloads update RuntimeInfo.LastConfigTime and ReloadConfigSuccess.
A reload cannot:
- Change
--storage.tsdb.path/--storage.agent.path. - Switch between server and agent mode.
- Add or remove TLS for the web listener (changes to
--web.config.fileare picked up on reload via the toolkit).
Integration points
main.go imports almost every package in the repository. Notable integrations:
config.Load(config/config.go) parsesprometheus.ymland is the source of truth for the rest of the program.discovery.NewManagerplus_ "github.com/prometheus/prometheus/plugins"registers all enabled SDs (the import has theinit()side effect; build tags determine which are included).scrape.NewManageris given anAppendable(server mode: the fanout storage; agent mode: the agent DB).rules.NewManageris constructed only outside agent mode. It's wired with the engine, the storage queryable, the notifier'sSendmethod, and the web handler's templates.web.Newregisters the API handlers and serves embedded UI assets.
Entry points for modification
If you need to:
- Add a new CLI flag: add it next to similar flags in
main.go, surface it through the relevant manager'sOptions, then runmake cli-documentation. - Change reload behaviour: find
func reload(and add your manager to the slice. Returning an error fromApplyConfigwill fail the reload. - Add or change server endpoints: edit
web/api/v1/api.go(orweb/web.gofor non-API routes), then updateweb/api/v1/openapi_paths.goand run the OpenAPI golden test. - Add an SD plugin: register it via
discovery.RegisterConfigin the SD's package, add aplugins/plugin_<name>.gofile with the appropriate build tag, list it inMAINTAINERS.md, and add config tests underconfig/testdata/conf.good.yml.
See Subsystems for the libraries main.go consumes, Configuration for the YAML schema, and Web API for the HTTP surface.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.