-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
58 lines (47 loc) · 1.26 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_webhook
import (
"context"
"time"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/code/data/webhook"
)
const (
webhookCountEventName = "WebhookCountPollingCheck"
webhookCallsEventName = "WebhookCallsPollingCheck"
)
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()
p.recordWebhookCountEvent(ctx, webhook.StatePending)
p.recordWebhookCallsEvents(ctx)
delay = time.Second - time.Since(start)
}
}
}
func (p *service) recordWebhookCountEvent(ctx context.Context, state webhook.State) {
count, err := p.data.CountWebhookByState(ctx, state)
if err != nil {
return
}
metrics.RecordEvent(ctx, webhookCountEventName, map[string]interface{}{
"count": count,
"state": state.String(),
})
}
func (p *service) recordWebhookCallsEvents(ctx context.Context) {
p.metricsMu.Lock()
successfulCalls := p.successfulWebhooks
failedCalls := p.failedWebhooks
p.successfulWebhooks = 0
p.failedWebhooks = 0
p.metricsMu.Unlock()
metrics.RecordEvent(ctx, webhookCallsEventName, map[string]interface{}{
"successes": successfulCalls,
"failures": failedCalls,
})
}