Skip to content

Commit

Permalink
Merge pull request #1485 from openmeterio/chore/fix-metric-panic
Browse files Browse the repository at this point in the history
fix: panic when no pool elements have been acquired
  • Loading branch information
turip authored Sep 9, 2024
2 parents 1e3dea3 + 507b3bf commit ef914df
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 ef914df

Please sign in to comment.