Giter VIP home page Giter VIP logo

sqlstats's Introduction

sqlstats

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.

Installation

go get github.com/dlmiddlecote/sqlstats

Example

package main

import (
	"database/sql"
	"net/http"

	_ "github.com/lib/pq"
	"github.com/dlmiddlecote/sqlstats"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

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

func run() error {
	// Open connection to a DB (could also use the https://github.com/jmoiron/sqlx 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
	prometheus.MustRegister(collector)

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

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

Exposed Metrics

Name Description Labels Go Version
go_sql_stats_connections_max_open Maximum number of open connections to the database. db_name 1.11+
go_sql_stats_connections_open The number of established connections both in use and idle. db_name 1.11+
go_sql_stats_connections_in_use The number of connections currently in use. db_name 1.11+
go_sql_stats_connections_idle The number of idle connections. db_name 1.11+
go_sql_stats_connections_waited_for The total number of connections waited for. db_name 1.11+
go_sql_stats_connections_blocked_seconds The total time blocked waiting for a new connection. db_name 1.11+
go_sql_stats_connections_closed_max_idle The total number of connections closed due to SetMaxIdleConns. db_name 1.11+
go_sql_stats_connections_closed_max_lifetime The total number of connections closed due to SetConnMaxLifetime. db_name 1.11+
go_sql_stats_connections_closed_max_idle_time The total number of connections closed due to SetConnMaxIdleTime. db_name 1.15+

sqlstats's People

Contributors

dlmiddlecote avatar cdevienne avatar dependabot-preview[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.