-
Notifications
You must be signed in to change notification settings - Fork 221
/
helpers.go
95 lines (78 loc) · 2.37 KB
/
helpers.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
package metrics
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/superfly/flyctl/terminal"
)
var (
unmatchedStatusesMtx = sync.Mutex{}
unmatchedStatuses = map[string]struct{}{}
)
func withUnmatchedStatuses[T any](cb func(map[string]struct{}) T) T {
unmatchedStatusesMtx.Lock()
defer unmatchedStatusesMtx.Unlock()
return cb(unmatchedStatuses)
}
func Started(ctx context.Context, metricSlug string) {
ok := withUnmatchedStatuses(func(unmatchedStatuses map[string]struct{}) bool {
if _, ok := unmatchedStatuses[metricSlug]; ok {
return false
}
unmatchedStatuses[metricSlug] = struct{}{}
return true
})
if !ok {
terminal.Debugf("Metrics: Attempted to send start event for %s, but it was already started", metricSlug)
return
}
SendNoData(ctx, metricSlug+"/started")
}
func Status(ctx context.Context, metricSlug string, success bool) {
ok := withUnmatchedStatuses(func(unmatchedStatuses map[string]struct{}) bool {
if _, ok := unmatchedStatuses[metricSlug]; ok {
delete(unmatchedStatuses, metricSlug)
return true
}
return false
})
if !ok {
terminal.Debugf("Metrics: Attempted to send status for %s, but no start event was sent", metricSlug)
return
}
Send(ctx, metricSlug+"/status", map[string]bool{"success": success})
}
func Send[T any](ctx context.Context, metricSlug string, value T) {
valJson, err := json.Marshal(value)
if err != nil {
return
}
SendJson(ctx, metricSlug, valJson)
}
func SendNoData(ctx context.Context, metricSlug string) {
SendJson(ctx, metricSlug, nil)
}
func SendJson(ctx context.Context, metricSlug string, payload json.RawMessage) {
rawSend(ctx, metricSlug, payload)
}
func StartTiming(ctx context.Context, metricSlug string) func() {
start := time.Now()
return func() {
Send(ctx, metricSlug, map[string]float64{"duration_seconds": time.Since(start).Seconds()})
}
}
type disableFlushMetricsKey struct{}
// WithDisableFlushMetrics returns a context with a flag that disables
// FlushMetrics call after a command completes.
func WithDisableFlushMetrics(ctx context.Context) context.Context {
return context.WithValue(ctx, disableFlushMetricsKey{}, true)
}
// IsFlushMetricsDisabled returns true if FlushMetrics should not be called after a command completes.
func IsFlushMetricsDisabled(ctx context.Context) bool {
val, ok := ctx.Value(disableFlushMetricsKey{}).(bool)
if !ok {
return false
}
return val
}