-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
50 lines (42 loc) · 1.1 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
package async_commitment
import (
"context"
"fmt"
"time"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/code/data/commitment"
)
const (
commitmentCountMetricName = "Commitment/%s_count"
)
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 _, state := range []commitment.State{
commitment.StateUnknown,
commitment.StatePayingDestination,
commitment.StateReadyToOpen,
commitment.StateOpening,
commitment.StateOpen,
commitment.StateClosing,
commitment.StateClosed,
} {
count, err := p.data.CountCommitmentsByState(ctx, state)
if err != nil {
continue
}
recordCommitmentCountMetric(ctx, state, count)
}
delay = time.Second - time.Since(start)
}
}
}
func recordCommitmentCountMetric(ctx context.Context, state commitment.State, count uint64) {
metricName := fmt.Sprintf(commitmentCountMetricName, state.String())
metrics.RecordCount(ctx, metricName, count)
}