-
Notifications
You must be signed in to change notification settings - Fork 499
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
support/db: Add round_trip_time_seconds metric #4009
Changes from 8 commits
cbaf9cb
be70e80
7873e73
1f8cd1f
6fa7e7c
b7c7002
9d9d724
0d4a931
5433118
21399ee
778c7eb
ef840a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type roundTripProbe struct { | ||
session SessionInterface | ||
roundTripTimeSummary prometheus.Summary | ||
|
||
closeChan chan struct{} | ||
closeOnce sync.Once | ||
} | ||
|
||
func (p *roundTripProbe) start() { | ||
p.closeChan = make(chan struct{}) | ||
// session must be cloned because will be used concurrently in a | ||
// separate go routine in roundTripProbe | ||
p.session = p.session.Clone() | ||
bartekn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
go func() { | ||
for { | ||
select { | ||
case <-time.After(time.Second): | ||
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. 💡 This is probably not a significant concern, but this will create a new channel on every call. For a repeating timer I typically use The one difference is that the way you're using time.After here will result in ~1 second of time between the end of the last probe and beginning of the next, but if you use Ticker it'll result in ~1 second of time between the start of each probe. 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. Good call! |
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
startTime := time.Now() | ||
_, err := p.session.ExecRaw(ctx, "select 1") | ||
if err == nil { | ||
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. should we check for context deadline exceeded errors? If the select is taking longer than 1 second we would be blind to this if we're looking just at prometheus. maybe we should still call 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. Good call. Added |
||
p.roundTripTimeSummary.Observe(time.Since(startTime).Seconds()) | ||
} | ||
cancel() | ||
case <-p.closeChan: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (p *roundTripProbe) close() { | ||
p.closeOnce.Do(func() { | ||
close(p.closeChan) | ||
}) | ||
} |
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: clonned -> cloned