Skip to content
A Go library for collecting sql.DBStats in Prometheus format
Branch: master
Clone or download
Latest commit 5c0fc7f Dec 26, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update CODEOWNERS Dec 26, 2019
.gitignore Initial commit Dec 25, 2019
LICENSE Add Go Report Card Dec 26, 2019
go.mod Initial implementation (#1) Dec 25, 2019
go.sum Initial implementation (#1) Dec 25, 2019


GoDoc Go Report Card License

A Go library for collecting sql.DBStats and exporting them in Prometheus format.

A sql.DB object represents a pool of zero or more underlying connections that get created and freed automatically. Connections in the pool may also be idle. There are a few settings (SetMaxOpenConns(), SetMaxIdleConns() and SetConnMaxLifetime()) that can be used to control the pool of connections. This library exposes stats about this pool in Prometheus format, to aid with understanding of the pool.


go get


package main

import (

	_ ""

func main() {
	if err := run(); err != nil {

func run() error {
	// Open connection to a DB (could also use the library)
	db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/postgres")
	if err != nil {
		return err

	// Create a new collector, the name will be used as a label on the metrics
	collector := sqlstats.NewStatsCollector("db_name", db)

	// Register it with Prometheus

	// Register the metrics handler
	http.Handle("/metrics", promhttp.Handler())

	// Run the web server
	return http.ListenAndServe(":8080", nil)

Exposed Metrics

Name Description Labels
go_sql_stats_connections_max_open Maximum number of open connections to the database. db_name
go_sql_stats_connections_open The number of established connections both in use and idle. db_name
go_sql_stats_connections_in_use The number of connections currently in use. db_name
go_sql_stats_connections_idle The number of idle connections. db_name
go_sql_stats_connections_waited_for The total number of connections waited for. db_name
go_sql_stats_connections_blocked_seconds The total time blocked waiting for a new connection. db_name
go_sql_stats_connections_closed_max_idle The total number of connections closed due to SetMaxIdleConns. db_name
go_sql_stats_connections_closed_max_lifetime The total number of connections closed due to SetConnMaxLifetime. db_name
You can’t perform that action at this time.