forked from jfindley/newrelic_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrelic_exporter.go
561 lines (442 loc) · 11.8 KB
/
newrelic_exporter.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package main
// TODO: implement JSON parser that loops through the output from api.Get()
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"sync"
"time"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
// Chunk size of metric requests
const ChunkSize = 10
// Namespace for metrics
const NameSpace = "newrelic"
// User-Agent string
const UserAgent = "NewRelic Exporter"
// Regular expression to parse Link headers
var rexp = `<([[:graph:]]+)>; rel="next"`
var LinkRexp *regexp.Regexp
func init() {
LinkRexp = regexp.MustCompile(rexp)
}
type Metric struct {
App string
Name string
Value float64
Label string
}
type AppList struct {
Applications []struct {
ID int
Name string
Health string `json:"health_status"`
AppSummary map[string]float64 `json:"application_summary"`
UsrSummary map[string]float64 `json:"end_user_summary"`
}
}
func (a *AppList) get(api *newRelicAPI,appIds string) error {
log.Debugf("Requesting application list from %s.", api.server.String())
var requestParamas string
if appIds != "" {
params := url.Values{}
params.Add("filter[ids]", appIds)
requestParamas = params.Encode()
}
body, err := api.req("/v2/applications.json", requestParamas )
if err != nil {
log.Error("Error getting application list: ", err)
return err
}
dec := json.NewDecoder(bytes.NewReader(body))
for {
page := new(AppList)
if err := dec.Decode(page); err == io.EOF {
break
} else if err != nil {
log.Error("Error decoding application list: ", err)
return err
}
a.Applications = append(a.Applications, page.Applications...)
}
return nil
}
func (a *AppList) sendMetrics(ch chan<- Metric) {
for _, app := range a.Applications {
for name, value := range app.AppSummary {
ch <- Metric{
App: app.Name,
Name: name,
Value: value,
Label: "application_summary",
}
}
for name, value := range app.UsrSummary {
ch <- Metric{
App: app.Name,
Name: name,
Value: value,
Label: "end_user_summary",
}
}
}
}
type MetricNames struct {
Metrics []struct {
Name string
Values []string
}
}
func (m *MetricNames) get(api *newRelicAPI, appID int) error {
log.Debugf("Requesting metrics names for application id %d.", appID)
path := fmt.Sprintf("/v2/applications/%s/metrics.json", strconv.Itoa(appID))
body, err := api.req(path, "")
if err != nil {
log.Error("Error getting metric names: ", err)
return err
}
dec := json.NewDecoder(bytes.NewReader(body))
for {
var part MetricNames
if err = dec.Decode(&part); err == io.EOF {
break
} else if err != nil {
log.Error("Error decoding metric names: ", err)
return err
}
tmpMetrics := append(m.Metrics, part.Metrics...)
m.Metrics = tmpMetrics
}
return nil
}
type MetricData struct {
Metric_Data struct {
Metrics []struct {
Name string
Timeslices []struct {
Values map[string]interface{}
}
}
}
}
func (m *MetricData) get(api *newRelicAPI, appID int, names MetricNames) error {
path := fmt.Sprintf("/v2/applications/%s/metrics/data.json", strconv.Itoa(appID))
var nameList []string
for i := range names.Metrics {
// We urlencode the metric names as the API will return
// unencoded names which it cannot read
nameList = append(nameList, names.Metrics[i].Name)
}
log.Debugf("Requesting %d metrics for application id %d.", len(nameList), appID)
// Because the Go client does not yet support 100-continue
// ( see issue #3665 ),
// we have to process this in chunks, to ensure the response
// fits within a single request.
chans := make([]chan MetricData, 0)
for i := 0; i < len(nameList); i += ChunkSize {
chans = append(chans, make(chan MetricData))
var thisList []string
if i+ChunkSize > len(nameList) {
thisList = nameList[i:]
} else {
thisList = nameList[i : i+ChunkSize]
}
go func(names []string, ch chan<- MetricData) {
var data MetricData
params := url.Values{}
for _, thisName := range names {
params.Add("names[]", thisName)
}
params.Add("raw", "true")
params.Add("summarize", "true")
params.Add("period", strconv.Itoa(api.period))
params.Add("from", api.from.Format(time.RFC3339))
params.Add("to", api.to.Format(time.RFC3339))
body, err := api.req(path, params.Encode())
if err != nil {
log.Error("Error requesting metrics: ", err)
close(ch)
return
}
dec := json.NewDecoder(bytes.NewReader(body))
for {
page := new(MetricData)
if err := dec.Decode(page); err == io.EOF {
break
} else if err != nil {
log.Error("Error decoding metrics data: ", err)
close(ch)
return
}
data.Metric_Data.Metrics = append(data.Metric_Data.Metrics, page.Metric_Data.Metrics...)
}
ch <- data
close(ch)
}(thisList, chans[len(chans)-1])
}
allData := m.Metric_Data.Metrics
for _, ch := range chans {
m := <-ch
allData = append(allData, m.Metric_Data.Metrics...)
}
m.Metric_Data.Metrics = allData
return nil
}
func (m *MetricData) sendMetrics(ch chan<- Metric, app string) {
for _, set := range m.Metric_Data.Metrics {
if len(set.Timeslices) == 0 {
continue
}
// As we set summarise=true there will only be one timeseries.
for name, value := range set.Timeslices[0].Values {
if v, ok := value.(float64); ok {
ch <- Metric{
App: app,
Name: name,
Value: v,
Label: set.Name,
}
}
}
}
}
type Exporter struct {
mu sync.Mutex
duration, error prometheus.Gauge
totalScrapes prometheus.Counter
metrics map[string]prometheus.GaugeVec
api *newRelicAPI
appIds string
metricsName string
}
func NewExporter() *Exporter {
return &Exporter{
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "exporter_last_scrape_duration_seconds",
Help: "The last scrape duration.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: NameSpace,
Name: "exporter_scrapes_total",
Help: "Total scraped metrics",
}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "exporter_last_scrape_error",
Help: "The last scrape error status.",
}),
metrics: map[string]prometheus.GaugeVec{},
}
}
func (e *Exporter) scrape(ch chan<- Metric) {
e.error.Set(0)
e.totalScrapes.Inc()
now := time.Now().UnixNano()
log.Debugf("Starting new scrape at %d.", now)
var apps AppList
err := apps.get(e.api, e.appIds)
if err != nil {
log.Error(err)
e.error.Set(1)
}
apps.sendMetrics(ch)
var wg sync.WaitGroup
for i := range apps.Applications {
app := apps.Applications[i]
wg.Add(1)
api := e.api
go func() {
defer wg.Done()
var names MetricNames
err = names.get(api, app.ID)
if err != nil {
log.Error(err)
e.error.Set(1)
}
var data MetricData
var filterNames MetricNames
metricsNameFilter := strings.Split(e.metricsName, ",")
if len(metricsNameFilter) <= 0 {
filterNames = names
} else {
for i := range names.Metrics {
for j := range metricsNameFilter {
if names.Metrics[i].Name == metricsNameFilter [j] {
tmpfilterNames := append(filterNames.Metrics, names.Metrics[i] )
filterNames.Metrics = tmpfilterNames
}
}
}
}
err = data.get(api, app.ID, filterNames)
if err != nil {
log.Error(err)
e.error.Set(1)
}
data.sendMetrics(ch, app.Name)
}()
}
wg.Wait()
close(ch)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
}
func (e *Exporter) recieve(ch <-chan Metric) {
for metric := range ch {
id := fmt.Sprintf("%s_%s", NameSpace, metric.Name)
if m, ok := e.metrics[id]; ok {
m.WithLabelValues(metric.App, metric.Label).Set(metric.Value)
} else {
g := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: metric.Name,
},
[]string{"app", "component"})
e.metrics[id] = *g
}
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.mu.Lock()
defer e.mu.Unlock()
for _, m := range e.metrics {
m.Describe(ch)
}
ch <- e.duration.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.error.Desc()
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mu.Lock()
defer e.mu.Unlock()
// Align requests to minute boundary.
// As time.Round rounds to the nearest integar rather than floor or ceil,
// subtract 30 seconds from the time before rounding.
e.api.to = time.Now().Add(-time.Second * 30).Round(time.Minute).UTC()
e.api.from = e.api.to.Add(-time.Duration(e.api.period) * time.Second)
metricChan := make(chan Metric)
go e.scrape(metricChan)
e.recieve(metricChan)
ch <- e.duration
ch <- e.totalScrapes
ch <- e.error
for _, m := range e.metrics {
m.Collect(ch)
}
}
type newRelicAPI struct {
server url.URL
apiKey string
from time.Time
to time.Time
period int
unreportingApps bool
client *http.Client
}
func NewNewRelicAPI(server string, apikey string, timeout time.Duration) *newRelicAPI {
parsed, err := url.Parse(server)
if err != nil {
log.Fatal("Could not parse API URL: ", err)
}
if apikey == "" {
log.Fatal("Cannot continue without an API key.")
}
return &newRelicAPI{
server: *parsed,
apiKey: apikey,
client: &http.Client{Timeout: timeout},
}
}
func (a *newRelicAPI) req(path string, params string) ([]byte, error) {
u, err := url.Parse(a.server.String() + path)
if err != nil {
return nil, err
}
u.RawQuery = params
log.Debug("Making API call: ", u.String())
req := &http.Request{
Method: "GET",
URL: u,
Header: http.Header{
"User-Agent": {UserAgent},
"X-Api-Key": {a.apiKey},
},
}
var data []byte
return a.httpget(req, data)
}
func (a *newRelicAPI) httpget(req *http.Request, in []byte) (out []byte, err error) {
resp, err := a.client.Do(req)
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
resp.Body.Close()
out = append(in, body...)
// Read the link header to see if we need to read more pages.
link := resp.Header.Get("Link")
vals := LinkRexp.FindStringSubmatch(link)
if len(vals) == 2 {
// Regexp matched, read get next page
u := new(url.URL)
u, err = url.Parse(vals[1])
if err != nil {
return
}
req.URL = u
return a.httpget(req, out)
}
return
}
func main() {
var server, apikey, listenAddress, appIds, metricsName, metricPath string
var period int
var timeout time.Duration
var err error
flag.StringVar(&apikey, "api.key", "", "NewRelic API key")
flag.StringVar(&appIds, "api.appIds", "", "NewRelic appIds")
flag.StringVar(&metricsName, "api.metricsName", "", "NewRelic metricsName")
flag.StringVar(&server, "api.server", "https://api.newrelic.com", "NewRelic API URL")
flag.IntVar(&period, "api.period", 60, "Period of data to extract in seconds")
flag.DurationVar(&timeout, "api.timeout", 30*time.Second, "Period of time to wait for an API response in seconds")
flag.StringVar(&listenAddress, "web.listen-address", ":9126", "Address to listen on for web interface and telemetry.")
flag.StringVar(&metricPath, "web.telemetry-path", "/metrics", "Path under which to expose metrics.")
flag.Parse()
api := NewNewRelicAPI(server, apikey, timeout)
api.period = period
exporter := NewExporter()
exporter.api = api
exporter.appIds = appIds
exporter.metricsName = metricsName
prometheus.MustRegister(exporter)
http.Handle(metricPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>NewRelic exporter</title></head>
<body>
<h1>NewRelic exporter</h1>
<p><a href='` + metricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Printf("Listening on %s.", listenAddress)
err = http.ListenAndServe(listenAddress, nil)
if err != nil {
log.Fatal(err)
}
log.Print("HTTP server stopped.")
}