-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
157 lines (139 loc) · 5.01 KB
/
metrics.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package minimatch
import (
"context"
"fmt"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
pb "github.com/castaneai/minimatch/gen/openmatch"
"github.com/castaneai/minimatch/pkg/statestore"
)
const (
metricsScopeName = "github.com/castaneai/minimatch"
matchProfileKey = attribute.Key("match_profile")
)
var (
defaultHistogramBuckets = []float64{
.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10,
}
)
type backendMetrics struct {
meter metric.Meter
ticketsFetched metric.Int64Counter
ticketsAssigned metric.Int64Counter
ticketCount metric.Int64ObservableUpDownCounter
fetchTicketsLatency metric.Float64Histogram
matchFunctionLatency metric.Float64Histogram
assignerLatency metric.Float64Histogram
assignToRedisLatency metric.Float64Histogram
}
func newBackendMetrics(provider metric.MeterProvider, store statestore.BackendStore) (*backendMetrics, error) {
meter := provider.Meter(metricsScopeName)
ticketsFetched, err := meter.Int64Counter("minimatch.backend.tickets_fetched")
if err != nil {
return nil, err
}
ticketsAssigned, err := meter.Int64Counter("minimatch.backend.tickets_assigned")
if err != nil {
return nil, err
}
fetchTicketsLatency, err := meter.Float64Histogram("minimatch.backend.fetch_tickets_latency",
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(defaultHistogramBuckets...))
if err != nil {
return nil, err
}
matchFunctionLatency, err := meter.Float64Histogram("minimatch.backend.match_function_latency",
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(defaultHistogramBuckets...))
if err != nil {
return nil, err
}
assignerLatency, err := meter.Float64Histogram("minimatch.backend.assigner_latency",
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(defaultHistogramBuckets...))
if err != nil {
return nil, err
}
assignToRedisLatency, err := meter.Float64Histogram("minimatch.backend.assign_to_redis_latency",
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(defaultHistogramBuckets...))
if err != nil {
return nil, err
}
ticketCount, err := meter.Int64ObservableUpDownCounter("minimatch.store.tickets.count",
metric.WithDescription("Total number of tickets. Do not sum this counter, as a single backend counts all tickets."),
metric.WithInt64Callback(func(ctx context.Context, o metric.Int64Observer) error {
count, err := store.GetTicketCount(ctx)
if err != nil {
return fmt.Errorf("failed to get ticket count from store: %w", err)
}
o.Observe(count)
return nil
}))
if err != nil {
return nil, err
}
return &backendMetrics{
meter: meter,
ticketsFetched: ticketsFetched,
ticketsAssigned: ticketsAssigned,
fetchTicketsLatency: fetchTicketsLatency,
matchFunctionLatency: matchFunctionLatency,
assignerLatency: assignerLatency,
assignToRedisLatency: assignToRedisLatency,
ticketCount: ticketCount,
}, nil
}
func (m *backendMetrics) recordMatchFunctionLatency(ctx context.Context, seconds float64, matchProfile *pb.MatchProfile) {
m.matchFunctionLatency.Record(ctx, seconds, metric.WithAttributes(matchProfileKey.String(matchProfile.Name)))
}
func (m *backendMetrics) recordTicketsFetched(ctx context.Context, fetched int64) {
m.ticketsFetched.Add(ctx, fetched)
}
func (m *backendMetrics) recordTicketsAssigned(ctx context.Context, asgs []*pb.AssignmentGroup) {
ticketsAssigned := int64(0)
for _, asg := range asgs {
ticketsAssigned += int64(len(asg.TicketIds))
}
m.ticketsAssigned.Add(ctx, ticketsAssigned)
}
func (m *backendMetrics) recordFetchTicketsLatency(ctx context.Context, latency time.Duration) {
m.fetchTicketsLatency.Record(ctx, latency.Seconds())
}
func (m *backendMetrics) recordAssignToRedisLatency(ctx context.Context, latency time.Duration) {
m.assignToRedisLatency.Record(ctx, latency.Seconds())
}
type matchFunctionWithMetrics struct {
mmf MatchFunction
metrics *backendMetrics
}
func (m *matchFunctionWithMetrics) MakeMatches(ctx context.Context, profile *pb.MatchProfile, poolTickets map[string][]*pb.Ticket) ([]*pb.Match, error) {
start := time.Now()
defer func() {
m.metrics.recordMatchFunctionLatency(ctx, time.Since(start).Seconds(), profile)
}()
return m.mmf.MakeMatches(ctx, profile, poolTickets)
}
func newMatchFunctionWithMetrics(mmf MatchFunction, metrics *backendMetrics) *matchFunctionWithMetrics {
return &matchFunctionWithMetrics{mmf: mmf, metrics: metrics}
}
type assignerWithMetrics struct {
assigner Assigner
metrics *backendMetrics
}
func newAssignerWithMetrics(assigner Assigner, metrics *backendMetrics) *assignerWithMetrics {
return &assignerWithMetrics{assigner: assigner, metrics: metrics}
}
func (a *assignerWithMetrics) Assign(ctx context.Context, matches []*pb.Match) ([]*pb.AssignmentGroup, error) {
start := time.Now()
defer func() {
a.metrics.assignerLatency.Record(ctx, time.Since(start).Seconds())
}()
asgs, err := a.assigner.Assign(ctx, matches)
if err != nil {
return nil, err
}
a.metrics.recordTicketsAssigned(ctx, asgs)
return asgs, nil
}