How prometheus-community/postgres_exporter works
Main Components
• Exporter Core – Represent the primary process running the exporter (located in cmd/postgres_exporter). This is the executable that handles the HTTP server and its endpoints.
• HTTP Server & Endpoints – Show the REST endpoints that the exporter exposes:
– /metrics: The primary telemetry endpoint for Prometheus.
– /probe: An endpoint used to support multi-target scraping (allowing one exporter to monitor several PostgreSQL instances).
• Collector Modules – Indicate the various collectors (as seen in the collector directory) which are responsible for running specific queries against a PostgreSQL database (e.g., pg_stat_database, pg_locks, pg_replication, etc.). Each collector encapsulates a set of metrics retrieval routines.
• Testing/Validation Tools – Optionally show that both integration tests (located under cmd/…/tests) and unit tests (spread across collector, config, etc.) ensure correctness, though these might be abstracted in a secondary/testing layer if needed.
Relationships and Interactions
– Client (Prometheus server) → HTTP Server: the scraped metrics request to /metrics or /probe endpoints.
– HTTP Server → Configuration Manager: at startup, the exporter loads configuration and credentials.
– HTTP Server → Collector Modules: upon receiving a request, the server triggers one or more collectors to run SQL queries against PostgreSQL.
– Collector Modules → PostgreSQL Database: collectors execute queries to gather metrics.
– PostgreSQL Database → Collector Modules: results and metrics data flow back.
– Collector Modules → HTTP Server: aggregated metrics eventually returned as a structured response to Prometheus.
Architectural Patterns & Design Principles
• Separation of concerns:
– The HTTP server is solely for handling incoming HTTP requests and delegating to the correct handlers.
– The collectors are modular and each is responsible for a specific group of PostgreSQL metrics.
– The configuration is isolated so that changing data source details or collector options does not affect the collector logic.
• Extensibility:
– The modular design of individual collector files (each with its own tests) makes it easy to add or remove metrics.
• Technologies and frameworks:
– Built using Go (referenced in go.mod, with many Go files).