Skip to content

feat: add prometheus metrics to database.Store #7713

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

Merged
merged 11 commits into from
May 31, 2023
Prev Previous commit
Next Next commit
measure tx duration
  • Loading branch information
johnstcn committed May 31, 2023
commit 6b867c642c96268f9e1fc71b95ba849a9a31dbdf
16 changes: 14 additions & 2 deletions coderd/database/dbmetrics/dbmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ func New(s database.Store, reg prometheus.Registerer) database.Store {
Help: "Latency distribution of queries in seconds.",
Buckets: prometheus.DefBuckets,
}, []string{"query"})
txDuration := prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "coderd",
Subsystem: "db",
Name: "tx_duration_seconds",
Help: "Duration of transactions in seconds.",
Buckets: prometheus.DefBuckets,
})
reg.MustRegister(queryLatencies)
reg.MustRegister(txDuration)
return &metricsStore{
s: s,
queryLatencies: queryLatencies,
txDuration: txDuration,
}
}

Expand All @@ -37,6 +46,7 @@ var _ database.Store = (*metricsStore)(nil)
type metricsStore struct {
s database.Store
queryLatencies *prometheus.HistogramVec
txDuration prometheus.Histogram
}

func (m metricsStore) Ping(ctx context.Context) (time.Duration, error) {
Expand All @@ -47,8 +57,10 @@ func (m metricsStore) Ping(ctx context.Context) (time.Duration, error) {
}

func (m metricsStore) InTx(f func(database.Store) error, options *sql.TxOptions) error {
// No point in measuring this, as it's just a wrapper around the underlying store's InTx method.
return m.s.InTx(f, options)
start := time.Now()
err := m.s.InTx(f, options)
m.txDuration.Observe(time.Since(start).Seconds())
return err
}

func (m metricsStore) AcquireLock(ctx context.Context, pgAdvisoryXactLock int64) error {
Expand Down