1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package dbmetrics
import ( "sync" "time"
"github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus"
"{{{ .Package }}}/app/lib/telemetry" "{{{ .Package }}}/app/util" )
var ( stmtCnt *prometheus.CounterVec stmtDur *prometheus.HistogramVec stmtMu sync.Mutex
SkipDBMetrics = util.GetEnvBool("db_metrics_disabled", false) )
type Metrics struct { Key string }
func NewMetrics(key string, db StatsGetter, logger util.Logger) (*Metrics, error) { if stmtCnt == nil { stmtMu.Lock() if stmtCnt == nil { metricsKey := "database" err := registerDatabaseMetrics(metricsKey, logger) if err != nil { return nil, err } err = prometheus.Register(newStatsCollector(metricsKey, db)) if err != nil { return nil, errors.Wrap(err, "unable to register stats collector") } } stmtMu.Unlock() }
return &Metrics{Key: key}, nil }
func (m *Metrics) IncStmt(sql string, method string) { if !SkipDBMetrics { stmtCnt.WithLabelValues(m.Key, sql, method).Inc() } }
func (m *Metrics) CompleteStmt(q string, op string, started time.Time) { if !SkipDBMetrics { elapsed := float64(time.Since(started)) / float64(time.Second) stmtDur.WithLabelValues(m.Key, q, op).Observe(elapsed) } }
func (m *Metrics) Close() error { return nil }
func registerDatabaseMetrics(subsystem string, logger util.Logger) error { if !SkipDBMetrics { stmtCntHelp := "The total number of SQL statements processed." stmtCnt = telemetry.MetricsCounter(subsystem, "statements_total", stmtCntHelp, logger, "database", "sql", "method") stmtDurHelp := "The SQL statement duration in seconds." stmtDur = telemetry.MetricsHistogram(subsystem, "statement_duration_seconds", stmtDurHelp, logger, "database", "sql", "method") } return nil }
|