/
librato.go
101 lines (84 loc) · 1.9 KB
/
librato.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
96
97
98
99
100
101
package reporter
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/heroku/instruments"
)
const defaultLibratoURL = "https://metrics-api.librato.com/v1/metrics"
type batch struct {
Gauges []map[string]interface{} `json:"gauges,omitempty"`
MeasureTime int64 `json:"measure_time"`
Source string `json:"source"`
}
type client struct {
Email string
Token string
}
func (c *client) URL() string {
uri, found := os.LookupEnv("LIBRATO_API_URL")
if !found {
return defaultLibratoURL
}
return uri
}
func (c *client) Post(b batch) error {
if len(b.Gauges) == 0 {
return nil
}
body, err := json.Marshal(b)
if err != nil {
return err
}
req, err := http.NewRequest("POST", fmt.Sprint(c.URL()), bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Email, c.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("librato: failed to communicate %d / %s", resp.StatusCode, resp.Status)
}
return nil
}
// Librato logs metrics to librato every given duration.
func Librato(email, token, source string, r *Registry, d time.Duration) {
client := &client{
Email: email,
Token: token,
}
for now := range time.Tick(d) {
b := batch{
Source: source,
Gauges: []map[string]interface{}{},
MeasureTime: now.Unix(),
}
for k, m := range r.Instruments() {
var s int64
switch i := m.(type) {
case instruments.Discrete:
s = i.Snapshot()
case instruments.Sample:
s = instruments.Quantile(i.Snapshot(), 0.95)
}
b.Gauges = append(b.Gauges, map[string]interface{}{
"name": k,
"value": float64(s),
"period": d.Seconds(),
})
}
err := client.Post(b)
if err != nil {
log.Println(err)
}
}
}