-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcm.go
280 lines (263 loc) · 7.67 KB
/
gcm.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//Package gcm Provides a way to send your go-metrics to google cloud monitoring
//
// Histograms are not implemented yet because not available in custom metrics,
// see https://cloud.google.com/monitoring/api/metrics#value-types
//
// Timer is to do
package gcm
import (
"fmt"
"log"
"strings"
"sync"
"time"
cloudmonitoring "google.golang.org/api/monitoring/v3"
"encoding/json"
"github.com/rcrowley/go-metrics"
)
//Config contains configration to reports metrics to gcm
//
//Look at the Monitor func for an example usage.
type Config struct {
Service *cloudmonitoring.Service // report to that service
//Project measured.
//you might want to set it to `projects/` + project name
//when creating the reporter manually
Project string
//Labels to send along with queries,
//usefull to know who sent what ex: ["source":$HOSTNAME].
//labels are created on the first query,
//to add or remove a label you need to recreate metric.
Labels map[string]string
//MonitoredRessource identifies the matchine/service/resource
//that is monitored.
//Different possible settings are defined here:
//https://cloud.google.com/monitoring/api/resources
//
//setting a nil MonitoredRessource will default
//to a "GlobalMonitoredResource" resource type.
//
//setting for MonitoredRessource can change without
//you having to recreate the metric.
MonitoredRessource *cloudmonitoring.MonitoredResource
}
var (
timersNotImplemented sync.Once
histogramsNotImplemented sync.Once
//GlobalMonitoredResource is the default monitored
//resource that will be sent with the metrics
//it doesn't allow much specification
GlobalMonitoredResource = &cloudmonitoring.MonitoredResource{Type: "global"}
)
//Monitor starts a new single threaded monitoring process.
//See Config for parameters explanation (service, project, labels, monitoredRessource).
//
//maxErrors is maximum number of consecutive retries if send fails.
//
//Example:
// client, err := google.DefaultClient(ctx, cloudmonitoring.MonitoringScope)
// if err != nil {
// log.Fatal("Failed to get cloudmonitoring default client:", err)
// }
// s, err := cloudmonitoring.New(client)
// if err != nil {
// log.Fatal("Failed to get cloudmonitoring default service:", err)
// }
//
// hostname, _ := os.Hostname()
// if hostname == "" {
// hostname = "unknown-hostname"
// }
// go googlecloudmetrics.Monitor(metrics.DefaultRegistry, 15*time.Second, 3, s, gcpProject, map[string]string{"source": hostname, "service": service}, nil)
func Monitor(r metrics.Registry, tick time.Duration, maxErrors int, service *cloudmonitoring.Service, project string, labels map[string]string, monitoredRessource *cloudmonitoring.MonitoredResource) error {
if monitoredRessource == nil {
monitoredRessource = GlobalMonitoredResource
}
reporter := Config{
Service: service,
Project: "projects/" + project,
Labels: labels,
MonitoredRessource: monitoredRessource,
}
ticker := time.NewTicker(tick)
var errors int
var err error
for range ticker.C {
err = reporter.Report(r)
if err != nil {
errors++
} else {
errors = 0
}
if errors >= maxErrors {
ticker.Stop()
log.Printf("GCM got %d successive errors, leaving", maxErrors)
}
}
return err
}
//Report every metric from registry to gcm
func (config *Config) Report(registry metrics.Registry) error {
now := time.Now()
reqs, err := config.buildTimeSeries(now.Format(time.RFC3339), registry)
if err != nil {
log.Printf("ERROR sending gcm request %s", err)
return err
}
if len(reqs) == 0 {
// nothing to send
return nil
}
wr := &cloudmonitoring.CreateTimeSeriesRequest{
TimeSeries: reqs,
}
_, err = cloudmonitoring.NewProjectsTimeSeriesService(config.Service).Create(config.Project, wr).Do()
if err != nil {
b, _ := json.Marshal(wr)
log.Printf("ERROR reporting metrics to gcm: %s.Req: %s", err, b)
}
return err
}
//DotSlashes makes golang metrics look like gcm metrics.
//
//ex : runtime.MemStats.StackSys -> runtime/MemStats/StackSys
func DotSlashes(name string) string {
return strings.Replace(name, ".", "/", -1)
}
//customMetric names your custom metric
func customMetric(name string) string {
return fmt.Sprintf("custom.googleapis.com/%s", DotSlashes(name))
}
//newTimeSeries creates a time serie named 'name' with labels from Config.
func (config *Config) newTimeSeries(name string) *cloudmonitoring.TimeSeries {
if config.MonitoredRessource == nil {
//global doesn't allow much settings
//but is a working default
config.MonitoredRessource = GlobalMonitoredResource
}
return &cloudmonitoring.TimeSeries{
Metric: &cloudmonitoring.Metric{
Type: customMetric(name),
Labels: config.Labels,
},
Resource: config.MonitoredRessource,
}
}
//buildTimeSeries returns an array of cloudmonitoring.TimeSeries containing
//eery metric to send to gcm.
func (config *Config) buildTimeSeries(start string, r metrics.Registry) (pts []*cloudmonitoring.TimeSeries, err error) {
r.Each(func(name string, metric interface{}) {
switch m := metric.(type) {
case metrics.Counter:
v := m.Count()
if v == 0 {
return
}
p := config.newTimeSeries(name)
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
Int64Value: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
case metrics.Gauge:
v := m.Value()
if v == 0 {
return
}
p := config.newTimeSeries(name)
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
Int64Value: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
case metrics.GaugeFloat64:
v := m.Value()
if v == 0 {
return
}
p := config.newTimeSeries(name)
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
DoubleValue: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
case metrics.Meter:
m = m.Snapshot()
if v := m.RateMean(); v != 0 {
p := config.newTimeSeries(name + ".mean")
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
DoubleValue: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
}
if v := m.Rate1(); v != 0 {
p := config.newTimeSeries(name + ".1min")
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
DoubleValue: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
}
if v := m.Rate5(); v != 0 {
p := config.newTimeSeries(name + ".5min")
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
DoubleValue: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
}
if v := m.Rate15(); v != 0 {
p := config.newTimeSeries(name + ".15min")
p.Points = append(p.Points, &cloudmonitoring.Point{
Value: &cloudmonitoring.TypedValue{
DoubleValue: &v,
},
Interval: &cloudmonitoring.TimeInterval{
EndTime: start,
},
})
pts = append(pts, p)
}
case metrics.Histogram:
if m.Count() > 0 {
histogramsNotImplemented.Do(func() {
log.Printf("Histograms are not available in custom metrics, see https://cloud.google.com/monitoring/api/metrics#value-types")
})
}
case metrics.Timer:
if m.Count() > 0 {
timersNotImplemented.Do(func() {
log.Printf("Timers are not implemented yet")
})
}
default:
log.Printf("unknown metric ? %#v", metric)
}
})
return
}