- Notifications
You must be signed in to change notification settings - Fork928
feat: measure pubsub latencies and expose metrics#13126
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
17 commits Select commitHold shift + click to select a range
c249b70
Measuring pubsub latency and exposing as Prometheus metric
dannykoppingf845bda
Track errors
dannykopping4f62b40
Explicitly checking latencies are >0
dannykopping3d9e3dd
Enhancements
dannykopping34083d0
Fix TestPGPubsub_Metrics test
dannykopping28a96de
Refactor to avoid global state
dannykopping65f57b1
Protect against NOTIFY races a slow receiver
dannykopping49d2002
Reliably cause notify races by delaying publishes
dannykopping68412c8
Merge branch 'main' of https://github.com/coder/coder into dk/pubsub-…
dannykoppinga7c042f
Measure latency in the background
dannykopping633365d
Fake pubsub to simulate race
dannykopping722a233
make fmt
dannykoppingff73789
Stop async measurements on pubsub close
dannykopping7d055d2
Cancel goroutine immediately on stop
dannykopping361538c
Merge branch 'main' of https://github.com/coder/coder into dk/pubsub-…
dannykopping33a2a1d
Revert to only synchronous collection; background collection is not w…
dannykoppingbad502a
Wording fixup
dannykoppingFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletionscoderd/database/pubsub/latency.go
This file contains hidden or 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,74 @@ | ||
package pubsub | ||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"time" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
) | ||
// LatencyMeasurer is used to measure the send & receive latencies of the underlying Pubsub implementation. We use these | ||
// measurements to export metrics which can indicate when a Pubsub implementation's queue is overloaded and/or full. | ||
type LatencyMeasurer struct { | ||
// Create unique pubsub channel names so that multiple coderd replicas do not clash when performing latency measurements. | ||
channel uuid.UUID | ||
logger slog.Logger | ||
} | ||
// LatencyMessageLength is the length of a UUIDv4 encoded to hex. | ||
const LatencyMessageLength = 36 | ||
func NewLatencyMeasurer(logger slog.Logger) *LatencyMeasurer { | ||
return &LatencyMeasurer{ | ||
channel: uuid.New(), | ||
logger: logger, | ||
} | ||
} | ||
// Measure takes a given Pubsub implementation, publishes a message & immediately receives it, and returns the observed latency. | ||
func (lm *LatencyMeasurer) Measure(ctx context.Context, p Pubsub) (send, recv time.Duration, err error) { | ||
var ( | ||
start time.Time | ||
res = make(chan time.Duration, 1) | ||
) | ||
msg := []byte(uuid.New().String()) | ||
lm.logger.Debug(ctx, "performing measurement", slog.F("msg", msg)) | ||
cancel, err := p.Subscribe(lm.latencyChannelName(), func(ctx context.Context, in []byte) { | ||
if !bytes.Equal(in, msg) { | ||
lm.logger.Warn(ctx, "received unexpected message", slog.F("got", in), slog.F("expected", msg)) | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return | ||
} | ||
res <- time.Since(start) | ||
}) | ||
if err != nil { | ||
return -1, -1, xerrors.Errorf("failed to subscribe: %w", err) | ||
} | ||
defer cancel() | ||
start = time.Now() | ||
err = p.Publish(lm.latencyChannelName(), msg) | ||
if err != nil { | ||
return -1, -1, xerrors.Errorf("failed to publish: %w", err) | ||
} | ||
send = time.Since(start) | ||
select { | ||
case <-ctx.Done(): | ||
lm.logger.Error(ctx, "context canceled before message could be received", slog.Error(ctx.Err()), slog.F("msg", msg)) | ||
return send, -1, ctx.Err() | ||
case recv = <-res: | ||
return send, recv, nil | ||
} | ||
} | ||
func (lm *LatencyMeasurer) latencyChannelName() string { | ||
return fmt.Sprintf("latency-measure:%s", lm.channel) | ||
} |
61 changes: 57 additions & 4 deletionscoderd/database/pubsub/pubsub.go
This file contains hidden or 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
111 changes: 111 additions & 0 deletionscoderd/database/pubsub/pubsub_linux_test.go
This file contains hidden or 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
39 changes: 28 additions & 11 deletionscoderd/database/pubsub/pubsub_test.go
This file contains hidden or 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.
Uh oh!
There was an error while loading.Please reload this page.
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.