-
Notifications
You must be signed in to change notification settings - Fork 211
/
push.go
41 lines (37 loc) · 1004 Bytes
/
push.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
package metrics
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics/public"
)
// StartPushingMetrics begins pushing metrics to the url specified by the --metrics-push flag
// with period specified by the --metrics-push-period flag.
func StartPushingMetrics(
url, username, password string,
headers map[string]string,
period time.Duration,
nodeID, networkID string,
) {
header := http.Header{}
for k, v := range headers {
header.Add(k, v)
}
pusher := push.New(url, "go-spacemesh").Gatherer(public.Registry).
Grouping("node", nodeID).
Grouping("network", networkID).
Header(header)
if username != "" && password != "" {
pusher = pusher.BasicAuth(username, password)
}
go func() {
ticker := time.NewTicker(period)
for range ticker.C {
err := pusher.Push()
if err != nil {
log.With().Warning("failed to push metrics", log.Err(err))
}
}
}()
}