Repository navigation
Expand file tree
/
Copy pathlatency.go
More file actions
103 lines (88 loc) · 3.38 KB
/
Copy pathlatency.go
File metadata and controls
103 lines (88 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package pubsub
import (
"bytes"
"context"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/google/uuid"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
)
// 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
// seq distinguishes consecutive measurements from each other so that a
// subscription whose teardown is still in flight cannot receive (and
// count) the next measurement's message.
seq atomic.Int64
}
// 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))
channel := lm.nextChannelName()
cancel, err := p.Subscribe(channel, 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(channel, 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
}
}
// channelPrefix is the common prefix of every channel name this measurer
// uses. Uniqueness across replicas comes from the channel UUID.
func (lm *LatencyMeasurer) channelPrefix() string {
return fmt.Sprintf("latency-measure:%s.", lm.channel)
}
// nextChannelName returns a channel name unique to this measurement.
// Uniqueness across replicas comes from the channel UUID; uniqueness
// across consecutive measurements of the same replica comes from the
// sequence number. The name must stay within Postgres's 63-byte
// identifier limit: 16 (prefix) + 36 (UUID) + 1 (dot) leaves 10 digits
// for the sequence.
func (lm *LatencyMeasurer) nextChannelName() string {
return fmt.Sprintf("%s%d", lm.channelPrefix(), lm.seq.Add(1))
}
// isMeasurementChannel reports whether event is one of this measurer's
// internal probe channels. Because each measurement uses a unique
// sequence-suffixed name, this matches on the shared per-measurer prefix.
// Callers use it to exclude the latency probe's traffic from every metric so
// an operator sees only real pubsub activity, not the noise the measurement
// itself generates.
func (lm *LatencyMeasurer) isMeasurementChannel(event string) bool {
return strings.HasPrefix(event, lm.channelPrefix())
}