Skip to content

Commit

Permalink
fix: panic when no pool elements have been acquired
Browse files Browse the repository at this point in the history
  • Loading branch information
turip committed Sep 9, 2024
1 parent 1e3dea3 commit 507b3bf
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/pgxpoolobserver/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,16 @@ func ObservePoolMetrics(meter metric.Meter, pool *pgxpool.Pool, additionalAttrib

_, err = meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
stat := pool.Stat()
o.ObserveInt64(acquireCountMetric, stat.AcquireCount(), metric.WithAttributes(additionalAttributes...))
o.ObserveInt64(acquiredDurationMetric, stat.AcquireDuration().Milliseconds(), metric.WithAttributes(additionalAttributes...))
o.ObserveInt64(avgAcquiredDurationMetric, stat.AcquireDuration().Milliseconds()/stat.AcquireCount(), metric.WithAttributes(additionalAttributes...))

acquireCount := stat.AcquireCount()
acquireDurationMS := stat.AcquireDuration().Milliseconds()

o.ObserveInt64(acquireCountMetric, acquireCount, metric.WithAttributes(additionalAttributes...))
o.ObserveInt64(acquiredDurationMetric, acquireDurationMS, metric.WithAttributes(additionalAttributes...))

if acquireCount > 0 {
o.ObserveInt64(avgAcquiredDurationMetric, acquireDurationMS/acquireCount, metric.WithAttributes(additionalAttributes...))
}
o.ObserveInt64(acquiredConnsMetric, int64(stat.AcquiredConns()), metric.WithAttributes(additionalAttributes...))
o.ObserveInt64(canceledAcquireCountMetric, stat.CanceledAcquireCount(), metric.WithAttributes(additionalAttributes...))
o.ObserveInt64(constructingConnsMetric, int64(stat.ConstructingConns()), metric.WithAttributes(additionalAttributes...))
Expand Down

0 comments on commit 507b3bf

Please sign in to comment.