# prometheus-community/postgres_exporter architecture

> Architecture diagram of the GitHub repository prometheus-community/postgres_exporter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prometheus-community/postgres_exporter
- Repository: https://github.com/prometheus-community/postgres_exporter

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    P["Prometheus Server"]:::external

    subgraph "Exporter Core"
        EC["Exporter Core"]:::exporter
        HTTP["HTTP Server & Endpoints (/metrics, /probe)"]:::server
        Config["Configuration Manager (Config file & Env vars)"]:::config
        DSNSelector["Dynamic DSN Selector (Multi-target)"]:::server
        Collector["Collector Modules (e.g., pg_stat_database, pg_locks, etc.)"]:::collector
        DBConn["PostgreSQL Database Connection"]:::server
    end

    DB["PostgreSQL Database"]:::db

    subgraph "Testing & Validation Tools"
        Test["Tests (Integration & Unit)"]:::test
    end

    %% Connections
    P -->|"scrape"| HTTP
    HTTP -->|"loadConfig"| Config
    HTTP -->|"triggerCollection"| Collector
    HTTP -->|"probeRequest"| DSNSelector
    DSNSelector -->|"selectTarget"| DBConn
    Collector -->|"executesQueries"| DBConn
    DBConn -->|"connects"| DB
    DB -->|"returnsResults"| Collector
    Collector -->|"aggregateMetrics"| HTTP
    Test -.->|"validates"| Collector

    %% Click Events
    click EC "https://github.com/prometheus-community/postgres_exporter/blob/master/cmd/postgres_exporter/main.go"
    click HTTP "https://github.com/prometheus-community/postgres_exporter/blob/master/cmd/postgres_exporter/server.go"
    click HTTP "https://github.com/prometheus-community/postgres_exporter/blob/master/cmd/postgres_exporter/probe.go"
    click Config "https://github.com/prometheus-community/postgres_exporter/blob/master/config/config.go"
    click Config "https://github.com/prometheus-community/postgres_exporter/blob/master/config/dsn.go"
    click Collector "https://github.com/prometheus-community/postgres_exporter/tree/master/collector/"
    click DBConn "https://github.com/prometheus-community/postgres_exporter/blob/master/cmd/postgres_exporter/datasource.go"
    click Test "https://github.com/prometheus-community/postgres_exporter/tree/master/cmd/postgres_exporter/tests/"

    %% Styles
    classDef exporter fill:#E3F2FD,stroke:#000,stroke-width:1px;
    classDef server fill:#BBDEFB,stroke:#000,stroke-width:1px;
    classDef config fill:#C8E6C9,stroke:#000,stroke-width:1px;
    classDef collector fill:#FFF9C4,stroke:#000,stroke-width:1px;
    classDef external fill:#D1C4E9,stroke:#000,stroke-width:1px;
    classDef db fill:#FFCCBC,stroke:#000,stroke-width:1px;
    classDef test fill:#F8BBD0,stroke:#000,stroke-width:1px;
```
