Skip to content

add automatic promethues metrics for roundtrip db query times #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/golang-migrate/migrate/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

// Since we are most likely going to be only retrieving migrations from file source,
// it's prudent that we include this side effect inside of this package and not
Expand All @@ -24,11 +26,83 @@ const (
var errTimeout = fmt.Errorf("could not connect to database: timed out")

// DB represents a wrapper for SQL DB providing extra methods.
// DB satisfies the sql.DB interface.
type DB struct {
*sql.DB
hist prometheus.Histogram
}

// Check will attempt to ping the database to see if the connection is still alive.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you accidentally removed comment/docs for Check function, which broke the build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you fix this, I can get this merged. This is really useful.

// new creates a new DB, wrapping the provided sql.DB and adding metrics.
func newDB(driverName string, db *sql.DB) *DB {
hist := promauto.NewHistogram(prometheus.HistogramOpts{
Name: "database_query_roundtrip_duration_seconds",
Help: "A histogram of roundtrip query times for this database.",
ConstLabels: map[string]string{"driver": driverName},
// Divide buckets by doubling the threshold, in ms: 5, 10, 20, 40, 80, 160, 320.
Buckets: prometheus.ExponentialBuckets(.05, 2, 7),
})

return &DB{
DB: db,
hist: hist,
}
}

func (db *DB) observe(start time.Time) {
db.hist.Observe(float64(time.Since(start).Milliseconds() / 1000))
}

// Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
start := time.Now()
defer db.observe(start)
return db.DB.Exec(query, args...)
}

// ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
start := time.Now()
defer db.observe(start)
return db.DB.ExecContext(ctx, query, args...)
}

// Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
start := time.Now()
defer db.observe(start)
return db.DB.Query(query, args...)
}

// QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
start := time.Now()
defer db.observe(start)
return db.DB.QueryContext(ctx, query, args...)
}

// QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value.
// Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows.
// Otherwise, the *Row's Scan scans the first selected row and discards the rest.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row {
start := time.Now()
defer db.observe(start)
return db.DB.QueryRow(query, args...)
}

// QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value.
// Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows.
// Otherwise, the *Row's Scan scans the first selected row and discards the rest.
// The duration of the call to the underlying database is added to the metrics for this DB.
func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
start := time.Now()
defer db.observe(start)
return db.DB.QueryRowContext(ctx, query, args...)
}
func (db *DB) Check() ([]string, bool) {
if err := db.Ping(); err != nil {
return []string{err.Error()}, false
Expand Down Expand Up @@ -105,7 +179,7 @@ func Open(driverName, dsn string) (*DB, error) {
case db := <-dbC:
// see: https://github.com/go-sql-driver/mysql/issues/674
db.SetMaxIdleConns(0)
return &DB{db}, nil
return newDB(driverName, db), nil
case err := <-errC:
return nil, err
case <-ctx.Done():
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/DATA-DOG/go-sqlmock v1.3.3
github.com/golang-migrate/migrate/v4 v4.2.5
github.com/prometheus/client_golang v1.2.1
)