-
Notifications
You must be signed in to change notification settings - Fork 12
/
librato.go
158 lines (130 loc) · 3 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"sort"
)
type Measurement struct {
Counters []*Counter `json:"counters"`
Gauges []interface{} `json:"gauges"`
Source string `json:"source,omitempty"`
}
func (m *Measurement) Count() int {
return (len(m.Counters) + len(m.Gauges))
}
type Counter struct {
Name string `json:"name"`
Source string `json:"source,omitempty"`
Value float64 `json:"value"`
}
type Gauge struct {
Name string `json:"name"`
Source string `json:"source,omitempty"`
Value float64 `json:"value"`
}
type ComplexGauge struct {
Name string `json:"name"`
Source string `json:"source,omitempty"`
Count int `json:"count"`
Sum float64 `json:"sum"`
Min float64 `json:"min"`
Max float64 `json:"max"`
SumSquares float64 `json:"sum_squares"`
}
func submitLibrato() (err error) {
m := buildMeasurement()
if m.Count() == 0 {
return
}
payload, err := json.MarshalIndent(m, "", " ")
if err != nil {
return
}
if *debug {
log.Printf("sending payload:\n%s\n", string(payload))
}
req, err := http.NewRequest("POST", "https://metrics-api.librato.com/v1/metrics", bytes.NewBuffer(payload))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("User-Agent", "statsd/1.0")
req.SetBasicAuth(*libratoUser, *libratoToken)
req.Close = true
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
raw, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("%s: %s", resp.Status, string(raw))
}
log.Printf("%d measurements sent to librato\n", m.Count())
resetTimers()
return
}
func buildMeasurement() (m *Measurement) {
m = &Measurement{}
if libratoSource != nil {
m.Source = *libratoSource
}
m.Counters = make([]*Counter, len(counters))
m.Gauges = make([]interface{}, len(gauges))
n := 0
for k, v := range counters {
c := &Counter{}
c.Name, c.Source = parseSource(k)
c.Value = v
m.Counters[n] = c
n++
}
n = 0
for k, v := range gauges {
g := &Gauge{}
g.Name, g.Source = parseSource(k)
g.Value = v
m.Gauges[n] = g
n++
}
for k, t := range timers {
for _, pct := range tiles {
if g := buildComplexGauge(k, t, pct); g != nil {
m.Gauges = append(m.Gauges, g)
}
}
}
return
}
func buildComplexGauge(k string, t []float64, pct float64) *ComplexGauge {
threshold := ((100.0 - pct) / 100.0) * float64(len(t))
threshold = math.Floor(threshold + 0.5)
count := len(t) - int(threshold)
if count <= 0 {
return nil
}
g := &ComplexGauge{}
g.Name, g.Source = parseSource(k)
if pct != 100.0 {
if float64(int(pct)) != pct {
rem := int(math.Ceil((pct - float64(int(pct))) * 10))
g.Name += fmt.Sprintf(".%d_%d", int(pct), rem)
} else {
g.Name += fmt.Sprintf(".%d", int(pct))
}
}
g.Count = count
sort.Float64s(t)
g.Min = t[0]
g.Max = t[count-1]
for i := 0; i < count; i++ {
g.Sum += t[i]
g.SumSquares += (t[i] * t[i])
}
return g
}