{"id":22347663,"url":"https://github.com/pgx-contrib/pgxprom","last_synced_at":"2026-05-02T15:31:52.094Z","repository":{"id":225619236,"uuid":"766380113","full_name":"pgx-contrib/pgxprom","owner":"pgx-contrib","description":"A Prometheus adapter for pgx","archived":false,"fork":false,"pushed_at":"2025-03-24T05:32:16.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T06:28:13.874Z","etag":null,"topics":["pgx","postgresql","prometheus"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgx-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-03T05:08:40.000Z","updated_at":"2025-03-24T05:32:13.000Z","dependencies_parsed_at":"2024-03-11T06:27:39.259Z","dependency_job_id":"9d1125a7-6eb6-43fb-9520-47f706f69e9a","html_url":"https://github.com/pgx-contrib/pgxprom","commit_stats":null,"previous_names":["pgx-contrib/pgxprom"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgx-contrib%2Fpgxprom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgx-contrib%2Fpgxprom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgx-contrib%2Fpgxprom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgx-contrib%2Fpgxprom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgx-contrib","download_url":"https://codeload.github.com/pgx-contrib/pgxprom/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245641317,"owners_count":20648637,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pgx","postgresql","prometheus"],"created_at":"2024-12-04T10:10:12.702Z","updated_at":"2026-05-02T15:31:52.088Z","avatar_url":"https://github.com/pgx-contrib.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pgxprom\n\n[![CI](https://github.com/pgx-contrib/pgxprom/actions/workflows/ci.yml/badge.svg)](https://github.com/pgx-contrib/pgxprom/actions/workflows/ci.yml)\n[![Release](https://img.shields.io/github/v/release/pgx-contrib/pgxprom?include_prereleases)](https://github.com/pgx-contrib/pgxprom/releases)\n[![Go Reference](https://pkg.go.dev/badge/github.com/pgx-contrib/pgxprom.svg)](https://pkg.go.dev/github.com/pgx-contrib/pgxprom)\n[![License](https://img.shields.io/github/license/pgx-contrib/pgxprom)](LICENSE)\n[![Go](https://img.shields.io/badge/Go-1.25-00ADD8?logo=go\u0026logoColor=white)](https://go.dev)\n[![pgx](https://img.shields.io/badge/pgx-v5-blue)](https://github.com/jackc/pgx)\n[![Prometheus](https://img.shields.io/badge/Prometheus-enabled-e6522c?logo=prometheus\u0026logoColor=white)](https://prometheus.io)\n\nPrometheus instrumentation for [pgx v5](https://github.com/jackc/pgx). Provides\ntwo collectors: `PoolCollector` exposes connection pool metrics, and\n`QueryCollector` records per-query request counts, error counts, and latency\nhistograms as Prometheus metrics.\n\n## Installation\n\n```bash\ngo get github.com/pgx-contrib/pgxprom\n```\n\n## Usage\n\n### PoolCollector\n\n`PoolCollector` implements `prometheus.Collector` and exposes connection pool\nstatistics for one or more `pgxpool.Pool` instances.\n\n```go\nconfig, err := pgxpool.ParseConfig(os.Getenv(\"PGX_DATABASE_URL\"))\nif err != nil {\n    panic(err)\n}\n\npool, err := pgxpool.NewWithConfig(context.Background(), config)\nif err != nil {\n    panic(err)\n}\n\ncollector := pgxprom.NewPoolCollector()\n// register the pool with the collector\ncollector.Add(pool)\n// register the collector with Prometheus\nif err := prometheus.Register(collector); err != nil {\n    panic(err)\n}\n```\n\nTo stop tracking a pool (e.g. on graceful shutdown):\n\n```go\ncollector.Remove(pool)\n```\n\n### QueryCollector\n\n`QueryCollector` implements both `pgx.QueryTracer` (and `pgx.BatchTracer`) and\n`prometheus.Collector`. Attach it to `ConnConfig.Tracer` to record metrics for\nevery query and batch operation.\n\n```go\nconfig, err := pgxpool.ParseConfig(os.Getenv(\"PGX_DATABASE_URL\"))\nif err != nil {\n    panic(err)\n}\n\ncollector := pgxprom.NewQueryCollector()\n// register with Prometheus\nif err := prometheus.Register(collector); err != nil {\n    panic(err)\n}\n// attach as the pgx tracer\nconfig.ConnConfig.Tracer = collector\n\npool, err := pgxpool.NewWithConfig(context.Background(), config)\nif err != nil {\n    panic(err)\n}\ndefer pool.Close()\n\nrows, err := pool.Query(context.Background(), \"SELECT * FROM customer\")\nif err != nil {\n    panic(err)\n}\ndefer rows.Close()\n```\n\n### Named queries\n\nPrefix any SQL string with `-- name: \u003cIdentifier\u003e` to set a low-cardinality\n`db_operation` label instead of `unknown`:\n\n```go\nrows, err := pool.Query(ctx,\n    \"-- name: ListActiveCustomers\\nSELECT * FROM customer WHERE active = true\",\n)\n```\n\nThis records the metric with `db_operation=\"ListActiveCustomers\"`.\n\n## Metrics reference\n\n### PoolCollector — `pgx_pool_*`\n\nAll pool metrics carry a single `database` label (the database name from the\nconnection config).\n\n| Metric | Type | Description |\n|--------|------|-------------|\n| `pgx_pool_acquire_connections` | Gauge | Connections currently being acquired |\n| `pgx_pool_canceled_acquires_total` | Counter | Acquire attempts that were canceled |\n| `pgx_pool_constructing_connections` | Gauge | Connections currently being constructed |\n| `pgx_pool_empty_acquires_total` | Counter | Acquire attempts that waited on an empty pool |\n| `pgx_pool_idle_connections` | Gauge | Idle connections in the pool |\n| `pgx_pool_max_connections` | Gauge | Maximum connections allowed in the pool |\n| `pgx_pool_total_connections` | Gauge | Total connections in the pool |\n| `pgx_pool_new_connections_total` | Counter | New connections created |\n| `pgx_pool_max_lifetime_destroys_total` | Counter | Connections destroyed due to MaxLifetime |\n| `pgx_pool_max_idle_destroys_total` | Counter | Connections destroyed due to MaxIdleTime |\n\n### QueryCollector — `pgx_conn_*`\n\nAll query metrics carry two labels:\n\n| Label | Description |\n|-------|-------------|\n| `database` | Database name from the connection config |\n| `db_operation` | Name extracted from `-- name: \u003cIdentifier\u003e` comment, or `unknown` |\n\n| Metric | Type | Description |\n|--------|------|-------------|\n| `pgx_conn_requests_total` | Counter | Total database requests |\n| `pgx_conn_request_errors_total` | Counter | Total database request errors |\n| `pgx_conn_request_duration_seconds` | Histogram | Request latency in seconds |\n\n## Development\n\n### DevContainer\n\nOpen in VS Code with the Dev Containers extension. The environment provides Go,\nPostgreSQL 18, and Nix automatically.\n\n```\nPGX_DATABASE_URL=postgres://vscode@postgres:5432/pgxprom?sslmode=disable\n```\n\n### Nix\n\n```bash\nnix develop          # enter shell with Go\ngo tool ginkgo run -r\n```\n\n### Run tests\n\n```bash\n# Unit tests only (no database required)\ngo tool ginkgo run -r\n\n# With integration tests\nexport PGX_DATABASE_URL=\"postgres://localhost/pgxprom?sslmode=disable\"\ngo tool ginkgo run -r\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgx-contrib%2Fpgxprom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgx-contrib%2Fpgxprom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgx-contrib%2Fpgxprom/lists"}