Skip to content

Commit f939166

Browse files
committed
Deprecated WithMinimumReadDBStatsInterval option
This option is not necessary since database/sql.DB.Stats calls are cheap. Note that go.opentelemetry.io/contrib/instrumentation/runtime instrumentation has a similar option WithMinimumReadMemStatsInterval, but for runtime.ReadMemStats that actually has a noticable performance impact (it does stopTheWorld to read stats). database/sql.DB.Stats, on the other hand, simply acquires a mutex for a short duration to return a consistent snapshot of database statistics.
1 parent 198e703 commit f939166

2 files changed

Lines changed: 9 additions & 24 deletions

File tree

options.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,18 @@ func TraceLastInsertID() DriverOption {
227227
})
228228
}
229229

230-
// WithMinimumReadDBStatsInterval sets the minimum interval between calls to db.Stats(). Negative values are ignored.
231-
func WithMinimumReadDBStatsInterval(interval time.Duration) StatsOption {
232-
return statsOptionFunc(func(o *statsOptions) {
233-
o.minimumReadDBStatsInterval = interval
234-
})
230+
// WithMinimumReadDBStatsInterval does nothing.
231+
//
232+
// Deprecated: does not have any effect since [database/sql.DB.Stats] calls are
233+
// cheap.
234+
func WithMinimumReadDBStatsInterval(_ time.Duration) StatsOption {
235+
return statsOptionFunc(func(_ *statsOptions) {})
235236
}
236237

237238
type statsOptions struct {
238239
// meterProvider sets the metric.MeterProvider. If nil, the global Provider will be used.
239240
meterProvider metric.MeterProvider
240241

241-
// minimumReadDBStatsInterval sets the minimum interval between calls to db.Stats(). Negative values are ignored.
242-
minimumReadDBStatsInterval time.Duration
243-
244242
// defaultAttributes will be set to each metrics as default.
245243
defaultAttributes []attribute.KeyValue
246244
}

stats.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import (
44
"context"
55
"database/sql"
66
"sync"
7-
"time"
87

98
"go.opentelemetry.io/otel"
109
"go.opentelemetry.io/otel/attribute"
1110
"go.opentelemetry.io/otel/metric"
1211
)
1312

14-
// defaultMinimumReadDBStatsInterval is the default minimum interval between calls to db.Stats().
15-
const defaultMinimumReadDBStatsInterval = time.Second
16-
1713
const (
1814
dbSQLConnectionsOpen = "db.sql.connections.open"
1915
dbSQLConnectionsIdle = "db.sql.connections.idle"
@@ -28,8 +24,7 @@ const (
2824
// RecordStats records database statistics for provided sql.DB at the provided interval.
2925
func RecordStats(db *sql.DB, opts ...StatsOption) error {
3026
o := statsOptions{
31-
meterProvider: otel.GetMeterProvider(),
32-
minimumReadDBStatsInterval: defaultMinimumReadDBStatsInterval,
27+
meterProvider: otel.GetMeterProvider(),
3328
}
3429

3530
for _, opt := range opts {
@@ -38,14 +33,13 @@ func RecordStats(db *sql.DB, opts ...StatsOption) error {
3833

3934
meter := o.meterProvider.Meter(instrumentationName)
4035

41-
return recordStats(meter, db, o.minimumReadDBStatsInterval, o.defaultAttributes...)
36+
return recordStats(meter, db, o.defaultAttributes...)
4237
}
4338

4439
// nolint: funlen
4540
func recordStats(
4641
meter metric.Meter,
4742
db *sql.DB,
48-
minimumReadDBStatsInterval time.Duration,
4943
attrs ...attribute.KeyValue,
5044
) error {
5145
var (
@@ -60,9 +54,6 @@ func recordStats(
6054
idleTimeClosed metric.Int64ObservableCounter
6155
lifetimeClosed metric.Int64ObservableCounter
6256

63-
dbStats sql.DBStats
64-
lastDBStats time.Time
65-
6657
// lock prevents a race between batch observer and instrument registration.
6758
lock sync.Mutex
6859
)
@@ -130,11 +121,7 @@ func recordStats(
130121
lock.Lock()
131122
defer lock.Unlock()
132123

133-
now := time.Now()
134-
if now.Sub(lastDBStats) >= minimumReadDBStatsInterval {
135-
dbStats = db.Stats()
136-
lastDBStats = now
137-
}
124+
dbStats := db.Stats()
138125

139126
obs.ObserveInt64(openConnections, int64(dbStats.OpenConnections), metric.WithAttributes(attrs...))
140127
obs.ObserveInt64(idleConnections, int64(dbStats.Idle), metric.WithAttributes(attrs...))

0 commit comments

Comments
 (0)