-
Notifications
You must be signed in to change notification settings - Fork 435
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
contrib/jackc/pgx.v5: publish pgxpool stats to statsd client #2692
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aa7795e
contrib/jackc/pgx.v5: publish pgxpool stats to statsd client
bengentree 5b79b22
Merge branch 'main' into pgxpool-stats
bengentree 62d7b97
Fix typo on package name
bengentree 21a9a20
Update log statement prefixes
bengentree f09c451
Do not publish metrics if statsd client is not set
bengentree 41b9618
Merge branch 'main' into pgxpool-stats
darccio 6fa54b9
Merge branch 'main' into pgxpool-stats
mtoffl01 0653d88
Merge branch 'main' into pgxpool-stats
darccio 300daba
Merge branch 'main' into pgxpool-stats
darccio 6d80c6b
Merge branch 'main' into pgxpool-stats
darccio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package pgx | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
const tracerPrefix = "datadog.tracer." | ||
|
||
const ( | ||
AcquireCount = tracerPrefix + "pgx.pool.connections.acquire" | ||
AcquireDuration = tracerPrefix + "pgx.pool.connections.acquire_duration" | ||
AcquiredConns = tracerPrefix + "pgx.pool.connections.acquired_conns" | ||
CanceledAcquireCount = tracerPrefix + "pgx.pool.connections.canceled_acquire" | ||
ConstructingConns = tracerPrefix + "pgx.pool.connections.constructing_conns" | ||
EmptyAcquireCount = tracerPrefix + "pgx.pool.connections.empty_acquire" | ||
IdleConns = tracerPrefix + "pgx.pool.connections.idle_conns" | ||
MaxConns = tracerPrefix + "pgx.pool.connections.max_conns" | ||
TotalConns = tracerPrefix + "pgx.pool.connections.total_conns" | ||
NewConnsCount = tracerPrefix + "pgx.pool.connections.new_conns" | ||
MaxLifetimeDestroyCount = tracerPrefix + "pgx.pool.connections.max_lifetime_destroy" | ||
MaxIdleDestroyCount = tracerPrefix + "pgx.pool.connections.max_idle_destroy" | ||
) | ||
|
||
var interval = 10 * time.Second | ||
|
||
// pollPoolStats calls (*pgxpool).Stats on the pool at a predetermined interval. It pushes the pool Stats off to the statsd client. | ||
func pollPoolStats(statsd internal.StatsdClient, pool *pgxpool.Pool) { | ||
if pool == nil { | ||
log.Debug("No traced pool connection found; cannot pull pool stats.") | ||
return | ||
} | ||
log.Debug("Traced pool connection found: Pool stats will be gathered and sent every %v.", interval) | ||
bengentree marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for range time.NewTicker(interval).C { | ||
log.Debug("Reporting pgxpool.Stat metrics...") | ||
bengentree marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stat := pool.Stat() | ||
statsd.Gauge(AcquireCount, float64(stat.AcquireCount()), []string{}, 1) | ||
statsd.Timing(AcquireDuration, stat.AcquireDuration(), []string{}, 1) | ||
statsd.Gauge(AcquiredConns, float64(stat.AcquiredConns()), []string{}, 1) | ||
statsd.Gauge(CanceledAcquireCount, float64(stat.CanceledAcquireCount()), []string{}, 1) | ||
statsd.Gauge(ConstructingConns, float64(stat.ConstructingConns()), []string{}, 1) | ||
statsd.Gauge(EmptyAcquireCount, float64(stat.EmptyAcquireCount()), []string{}, 1) | ||
statsd.Gauge(IdleConns, float64(stat.IdleConns()), []string{}, 1) | ||
statsd.Gauge(MaxConns, float64(stat.MaxConns()), []string{}, 1) | ||
statsd.Gauge(TotalConns, float64(stat.TotalConns()), []string{}, 1) | ||
statsd.Gauge(NewConnsCount, float64(stat.NewConnsCount()), []string{}, 1) | ||
statsd.Gauge(MaxLifetimeDestroyCount, float64(stat.MaxLifetimeDestroyCount()), []string{}, 1) | ||
statsd.Gauge(MaxIdleDestroyCount, float64(stat.MaxIdleDestroyCount()), []string{}, 1) | ||
} | ||
} | ||
|
||
func statsTags(c *config) []string { | ||
tags := globalconfig.StatsTags() | ||
if c.serviceName != "" { | ||
tags = append(tags, "service:"+c.serviceName) | ||
} | ||
return tags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package pgx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWithPoolStats(t *testing.T) { | ||
t.Run("default off", func(t *testing.T) { | ||
cfg := defaultConfig() | ||
assert.False(t, cfg.poolStats) | ||
}) | ||
t.Run("on", func(t *testing.T) { | ||
cfg := new(config) | ||
WithPoolStats()(cfg) | ||
assert.True(t, cfg.poolStats) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,14 @@ func NewPool(ctx context.Context, connString string, opts ...Option) (*pgxpool.P | |||||
} | ||||||
|
||||||
func NewPoolWithConfig(ctx context.Context, config *pgxpool.Config, opts ...Option) (*pgxpool.Pool, error) { | ||||||
config.ConnConfig.Tracer = newPgxTracer(opts...) | ||||||
return pgxpool.NewWithConfig(ctx, config) | ||||||
tracer := newPgxTracer(opts...) | ||||||
config.ConnConfig.Tracer = tracer | ||||||
pool, err := pgxpool.NewWithConfig(ctx, config) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
if tracer.cfg.poolStats { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If we failed to generate a statsd client, we don't want to attempt to submit metrics on it. |
||||||
go pollPoolStats(tracer.cfg.statsdClient, pool) | ||||||
} | ||||||
return pool, nil | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove lines 38 - 41 as they essentially duplicate the
err != nil
check done by the caller,NewPoolWithConfig
(Note: We also removed a similar check in the database/sql DB Stats feature in more recent versions , PR)