-
Notifications
You must be signed in to change notification settings - Fork 939
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c249b70
Measuring pubsub latency and exposing as Prometheus metric
dannykopping f845bda
Track errors
dannykopping 4f62b40
Explicitly checking latencies are >0
dannykopping 3d9e3dd
Enhancements
dannykopping 34083d0
Fix TestPGPubsub_Metrics test
dannykopping 28a96de
Refactor to avoid global state
dannykopping 65f57b1
Protect against NOTIFY races a slow receiver
dannykopping 49d2002
Reliably cause notify races by delaying publishes
dannykopping 68412c8
Merge branch 'main' of https://github.com/coder/coder into dk/pubsub-…
dannykopping a7c042f
Measure latency in the background
dannykopping 633365d
Fake pubsub to simulate race
dannykopping 722a233
make fmt
dannykopping ff73789
Stop async measurements on pubsub close
dannykopping 7d055d2
Cancel goroutine immediately on stop
dannykopping 361538c
Merge branch 'main' of https://github.com/coder/coder into dk/pubsub-…
dannykopping 33a2a1d
Revert to only synchronous collection; background collection is not w…
dannykopping bad502a
Wording fixup
dannykopping 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 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)) | ||
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) | ||
} |
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.