-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpusher.go
75 lines (61 loc) · 1.58 KB
/
pusher.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
package metrics
import (
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/trustwallet/go-libs/client"
)
type MetricsPusherClient struct {
client client.Request
}
func NewMetricsPusherClient(pushURL, key string, errorHandler client.HttpErrorHandler) *MetricsPusherClient {
client := client.InitClient(pushURL, errorHandler, client.WithExtraHeader("X-API-Key", key))
return &MetricsPusherClient{
client: client,
}
}
func (c *MetricsPusherClient) Do(req *http.Request) (*http.Response, error) {
for key, value := range c.client.Headers {
req.Header.Set(key, value)
}
return c.client.HttpClient.Do(req)
}
type Pusher interface {
Push() error
Close() error
}
type pusher struct {
pusher *push.Pusher
}
func NewPusher(pushgatewayURL, jobName string) Pusher {
return &pusher{
pusher: push.New(pushgatewayURL, jobName).
Grouping("instance", instanceID()).
Gatherer(prometheus.DefaultGatherer),
}
}
func NewPusherWithCustomClient(pushgatewayURL, jobName string, client client.HTTPClient) Pusher {
return &pusher{
pusher: push.New(pushgatewayURL, string(jobName)).
Grouping("instance", instanceID()).
Gatherer(prometheus.DefaultGatherer).
Client(client),
}
}
func (p *pusher) Push() error {
return p.pusher.Push()
}
func (p *pusher) Close() error {
return p.pusher.Delete()
}
func instanceID() string {
envKeysToTry := []string{"DYNO", "INSTANCE_ID", "HOSTNAME"}
for _, key := range envKeysToTry {
curr := os.Getenv(key)
if curr != "" {
return curr
}
}
return "local"
}