-
Notifications
You must be signed in to change notification settings - Fork 444
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/database/sql: Submit DBStats as Datadog metrics #2543
Merged
Merged
Changes from 38 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
6eafc80
Implementation 1: Run goroutine for polling metrics from within contr…
mtoffl01 8ed75ef
nits: Added public fn definition and cleaned up if/else statement
mtoffl01 11cd3a1
database/sql & tracer: implemented channel of comms between contrib a…
mtoffl01 f92d158
nits from github bot
mtoffl01 9bb05ba
foundation for sql_test tests
mtoffl01 df2f0b2
update other calls to reportContribMetrics now that the fn accepts ju…
mtoffl01 4f64c73
Implementing new type StatsCarrier to share statsd client between con…
mtoffl01 d1a72bf
fix panic on attempt to close a closed channel on statscarrier
mtoffl01 57b83b8
removed superfluous log test
mtoffl01 0296193
Update contrib/database/sql/sql.go
mtoffl01 38b969b
Fleshing out tests and making changes as a result of tests
mtoffl01 321e2a7
Changed Stat implementation to interface, with gauge count and timing…
mtoffl01 015bcfc
go fmt'd
mtoffl01 8d3a473
Merge branch 'main' into mtoff/sql_metrics
mtoffl01 1b1a612
Moved metrics collection out of sql.go file and into its own metrics …
mtoffl01 ad442c4
Remove TestContribStats from metrics_test.go
mtoffl01 041f64d
added lock to globalconfig's statscarrier to protect against data race
mtoffl01 55501ea
fix locks on globalconfig & change pollDBStats function order
mtoffl01 8ae57ad
Added new TestDBStats fn
mtoffl01 e361c8d
Fixed TestWithDBStats tests
mtoffl01 59f5192
Merge branch 'main' into mtoff/sql_metrics
mtoffl01 9aa52a1
Adding test for WithDBStats
mtoffl01 82c9107
Added documentation to WithDBStats about Open function v Register
mtoffl01 494dd35
Add definition to public types/functions and change tests slightly
mtoffl01 58cedac
Expanded godoc comments, examples and fixed bug in dbstats feature wh…
mtoffl01 fc7c049
Add last todos and tags
mtoffl01 7c800b7
gofmt
mtoffl01 098595d
Made db polling interval nonconfigurable, and fixed lock issue in sta…
mtoffl01 6541d9d
go fmt
mtoffl01 441d233
Merge branch 'main' into mtoff/sql_metrics
mtoffl01 92547d3
Fixed flaky tests
mtoffl01 0becfbd
merge with remote branch
mtoffl01 50daa34
Remove slices dependency
mtoffl01 41cac05
Update example_test.go
mtoffl01 9a85b14
Update metrics.go
mtoffl01 77cfd1e
Update globalconfig.go
mtoffl01 0e8dbc2
Update statsd.go
mtoffl01 2e356e9
change all stats to gauge
mtoffl01 c6ed0ad
Update example_test.go
mtoffl01 8ebc177
Merge branch 'mtoff/sql_metrics' of github.com:DataDog/dd-trace-go in…
mtoffl01 b7eaf30
Reduce pollDBStats interval for testing purposes
mtoffl01 3cdae9f
use t.Cleanup in globalconfig_test.go
mtoffl01 df8d794
rework TestOpenOptions in sql_test.go
katiehockman f016742
Merge branch 'main' into mtoff/sql_metrics
katiehockman 9523300
reworked TestOpenOptions logic
mtoffl01 834354b
Merge branch 'main' into mtoff/sql_metrics
katiehockman 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
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,54 @@ | ||
// 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 2016 Datadog, Inc. | ||
|
||
package sql // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql" | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"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." | ||
|
||
// ref: https://pkg.go.dev/database/sql#DBStats | ||
const ( | ||
MaxOpenConnections = tracerPrefix + "sql.db.connections.max_open" | ||
OpenConnections = tracerPrefix + "sql.db.connections.open" | ||
InUse = tracerPrefix + "sql.db.connections.in_use" | ||
Idle = tracerPrefix + "sql.db.connections.idle" | ||
WaitCount = tracerPrefix + "sql.db.connections.waiting" | ||
WaitDuration = tracerPrefix + "sql.db.connections.wait_duration" | ||
MaxIdleClosed = tracerPrefix + "sql.db.connections.closed.max_idle_conns" | ||
MaxIdleTimeClosed = tracerPrefix + "sql.db.connections.closed.max_idle_time" | ||
MaxLifetimeClosed = tracerPrefix + "sql.db.connections.closed.max_lifetime" | ||
) | ||
|
||
const interval = 10 * time.Second | ||
|
||
// pollDBStats calls (*DB).Stats on the db at a predetermined interval. It pushes the DBStats off to the StatsCarrier which ultimately sends them through a statsd client. | ||
// TODO: Perhaps grant a way for pollDBStats to grab the drivername so that it doesn't have to be passed in as a param | ||
func pollDBStats(db *sql.DB, tags []string) { | ||
if db == nil { | ||
log.Debug("No traced DB connection found; cannot pull DB stats.") | ||
return | ||
} | ||
log.Debug("Traced DB connection found: DB stats will be gathered and sent every %v.", interval) | ||
for range time.NewTicker(interval).C { | ||
stat := db.Stats() | ||
globalconfig.PushStat(internal.NewGauge(MaxOpenConnections, float64(stat.MaxOpenConnections), tags, 1)) | ||
globalconfig.PushStat(internal.NewGauge(OpenConnections, float64(stat.OpenConnections), tags, 1)) | ||
globalconfig.PushStat(internal.NewGauge(InUse, float64(stat.InUse), tags, 1)) | ||
globalconfig.PushStat(internal.NewGauge(Idle, float64(stat.Idle), tags, 1)) | ||
globalconfig.PushStat(internal.NewGauge(WaitCount, float64(stat.WaitCount), tags, 1)) | ||
globalconfig.PushStat(internal.NewTiming(WaitDuration, stat.WaitDuration, tags, 1)) | ||
globalconfig.PushStat(internal.NewCount(MaxIdleClosed, int64(stat.MaxIdleClosed), tags, 1)) | ||
globalconfig.PushStat(internal.NewCount(MaxIdleTimeClosed, int64(stat.MaxIdleTimeClosed), tags, 1)) | ||
globalconfig.PushStat(internal.NewCount(MaxLifetimeClosed, int64(stat.MaxLifetimeClosed), tags, 1)) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
NIT: Should these be public? (if not, we could still make them private before the next release starts on Monday, cc @dianashevchenko )
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.
No, there's no need for these to be public, that was just an oversight!