-
Notifications
You must be signed in to change notification settings - Fork 221
/
api.go
60 lines (46 loc) · 936 Bytes
/
api.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
package metrics
import (
"context"
"encoding/json"
"sync"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/config"
)
var (
Enabled = true
done sync.WaitGroup
)
type metricsMessage struct {
Metric string `json:"m"`
Payload json.RawMessage `json:"p"`
}
func rawSend(parentCtx context.Context, metricSlug string, payload json.RawMessage) {
if !shouldSendMetrics(parentCtx) {
return
}
message := metricsMessage{
Metric: metricSlug,
Payload: payload,
}
queueMetric(message)
}
func shouldSendMetrics(ctx context.Context) bool {
if !Enabled {
return false
}
cfg := config.FromContext(ctx)
if !cfg.SendMetrics {
return false
}
// never send metrics to the production collector from dev builds
if buildinfo.IsDev() && cfg.MetricsBaseURLIsProduction() {
return false
}
return true
}
func FlushPending() {
if !Enabled {
return
}
done.Wait()
}