-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
58 lines (49 loc) · 1.3 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
package async_nonce
import (
"context"
"time"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/code/data/nonce"
)
const (
nonceCountMetricName = "Nonce/%s_count"
nonceCountCheckEventName = "NonceCountPollingCheck"
)
func (p *service) metricsGaugeWorker(ctx context.Context) error {
delay := time.Second
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
start := time.Now()
for _, useCase := range []nonce.Purpose{
nonce.PurposeClientTransaction,
nonce.PurposeInternalServerProcess,
nonce.PurposeOnDemandTransaction,
} {
for _, state := range []nonce.State{
nonce.StateUnknown,
nonce.StateReleased,
nonce.StateAvailable,
nonce.StateReserved,
nonce.StateInvalid,
} {
count, err := p.data.GetNonceCountByStateAndPurpose(ctx, state, useCase)
if err != nil {
continue
}
recordNonceCountEvent(ctx, state, useCase, count)
}
}
delay = time.Second - time.Since(start)
}
}
}
func recordNonceCountEvent(ctx context.Context, state nonce.State, useCase nonce.Purpose, count uint64) {
metrics.RecordEvent(ctx, nonceCountCheckEventName, map[string]interface{}{
"use_case": useCase.String(),
"state": state.String(),
"count": count,
})
}