From 2f53d918ecc733507c0c7340ce144d2cfc8cf7e2 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 24 Feb 2026 21:37:32 +0100 Subject: [PATCH] fix: record stats meter default attribute override Default metric attribute was appended at the end, making it impossible to override the default one from the user provided StatsOption. This commit ensure the default attributes, are always provided at the beginning, making it possible for the user to override those. --- meter.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/meter.go b/meter.go index fbce22d..2e65d51 100644 --- a/meter.go +++ b/meter.go @@ -37,11 +37,18 @@ const ( // RecordStats records database statistics for provided pgxpool.Pool at a default 1 second interval // unless otherwise specified by the WithMinimumReadDBStatsInterval StatsOption. func RecordStats(db PoolStats, opts ...StatsOption) error { + serverAddress := semconv.ServerAddress(db.Config().ConnConfig.Host) + serverPort := semconv.ServerPort(int(db.Config().ConnConfig.Port)) + dbNamespace := semconv.DBNamespace(db.Config().ConnConfig.Database) + poolName := fmt.Sprintf("%s:%d/%s", serverAddress.Value.AsString(), serverPort.Value.AsInt64(), dbNamespace.Value.AsString()) + dbClientConnectionPoolName := semconv.DBClientConnectionPoolName(poolName) + o := statsOptions{ meterProvider: otel.GetMeterProvider(), minimumReadDBStatsInterval: defaultMinimumReadDBStatsInterval, defaultAttributes: []attribute.KeyValue{ semconv.DBSystemPostgreSQL, + dbClientConnectionPoolName, }, } @@ -93,12 +100,6 @@ func recordStats( lock sync.Mutex ) - serverAddress := semconv.ServerAddress(db.Config().ConnConfig.Host) - serverPort := semconv.ServerPort(int(db.Config().ConnConfig.Port)) - dbNamespace := semconv.DBNamespace(db.Config().ConnConfig.Database) - poolName := fmt.Sprintf("%s:%d/%s", serverAddress.Value.AsString(), serverPort.Value.AsInt64(), dbNamespace.Value.AsString()) - dbClientConnectionPoolName := semconv.DBClientConnectionPoolName(poolName) - lock.Lock() defer lock.Unlock() @@ -195,11 +196,6 @@ func recordStats( return fmt.Errorf("failed to create asynchronous metric: %s with error: %w", pgxPoolEmptyAcquireWaitTime, err) } - attrs = append(attrs, []attribute.KeyValue{ - semconv.DBSystemPostgreSQL, - dbClientConnectionPoolName, - }...) - observeOptions = []metric.ObserveOption{ metric.WithAttributeSet(attribute.NewSet(attrs...)), }