This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bunnycdn_exporter.go
405 lines (351 loc) · 13.8 KB
/
bunnycdn_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
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "bunnycdn" // For Prometheus metrics.
metricBandwidthUsed = "bandwidthUsed"
metricBandwidthCached = "bandwidthCached"
metricRequestsServer = "requestsServed"
metricPullRequestsPulled = "pullRequestsPulled"
metricErr3xx = "error3xx"
metricErr4xx = "error4xx"
metricErr5xx = "error5xx"
metricGeoTrafficDist = "geoTrafficDistribution"
metricBalance = "balance"
metricStorageUsed = "storageUsed"
)
func newMetric(metricName string, docString string, variableLabels []string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "", metricName), docString, variableLabels, constLabels)
}
type bunnyPullZone struct {
ID int64 `json:"Id"`
Name string `json:"Name"`
}
type bunnyLocation struct {
Region string
Location string
Requests float64
}
type bunnyStatistics struct {
BandwidthUsed map[string]float64 `json:"BandwidthUsedChart"`
BandwidthCached map[string]float64 `json:"BandwidthCachedChart"`
CacheHitRate map[string]float64 `json:"CacheHitRateChart"`
RequestsServed map[string]float64 `json:"RequestsServedChart"`
PullRequestsPulled map[string]float64 `json:"PullRequestsPulledChart"`
UserBalanceHistory map[string]float64 `json:"UserBalanceHistoryChart"`
UserStorageUsed map[string]float64 `json:"UserStorageUsedChart"`
GeoTrafficDistribution map[string]float64 `json:"GeoTrafficDistribution"`
Error3Xx map[string]float64 `json:"Error3xxChart"`
Error4Xx map[string]float64 `json:"Error4xxChart"`
Error5Xx map[string]float64 `json:"Error5xxChart"`
}
func (s bunnyStatistics) trafficLocations() []bunnyLocation {
locations := make([]bunnyLocation, 0, len(s.GeoTrafficDistribution))
for loc, req := range s.GeoTrafficDistribution {
parts := strings.Split(loc, ":")
locations = append(
locations,
bunnyLocation{
Region: parts[0],
Location: strings.TrimSpace(parts[1]),
Requests: req,
})
}
return locations
}
func extractFromMap(data map[string]float64) float64 {
for _, v := range data {
return v
}
return -1
}
type metricsCollection map[string]*prometheus.Desc
func (m metricsCollection) String() string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return strings.Join(keys, ",")
}
var (
accountMetrics = metricsCollection{
metricBalance: newMetric("account_balance", "Current account balance", nil, nil),
metricStorageUsed: newMetric("storage_used_bytes", "Storage usage in bytes", nil, nil),
}
pullZoneMetrics = metricsCollection{
metricBandwidthUsed: newMetric("bandwidth_used_bytes_total", "Total bandwidth used in bytes serving traffic.", []string{"pull_zone"}, nil),
metricBandwidthCached: newMetric("bandwidth_cached_bytes_total", "Total bandwidth used in bytes serving cached data.", []string{"pull_zone"}, nil),
metricRequestsServer: newMetric("requests_served_total", "Number of requests served.", []string{"pull_zone"}, nil),
metricPullRequestsPulled: newMetric("pull_requests_pulled", "Number of pull requests from origin.", []string{"pull_zone"}, nil),
metricErr3xx: newMetric("request_error_count", "Request error by code.", []string{"pull_zone"}, prometheus.Labels{"code": "3xx"}),
metricErr4xx: newMetric("request_error_count", "Request error by code.", []string{"pull_zone"}, prometheus.Labels{"code": "4xx"}),
metricErr5xx: newMetric("request_error_count", "Request error by code.", []string{"pull_zone"}, prometheus.Labels{"code": "5xx"}),
metricGeoTrafficDist: newMetric("requests_served", "Request by location.", []string{"pull_zone", "region", "location"}, nil),
}
bunnyUp = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "up"), "Was the last scrape of bunnyCDN successful.", nil, nil)
)
// Exporter collects BunnyCDN stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
URI string
mutex sync.RWMutex
fetch func(path string) (io.ReadCloser, error)
up prometheus.Gauge
totalScrapes, totalErrors, totalAPICalls prometheus.Counter
accountMetrics metricsCollection
pullZoneMetrics metricsCollection
}
// NewExporter returns an initialized Exporter.
func NewExporter(uri string, bunnyAPIKey string, sslVerify bool, accountMetrics metricsCollection, pullZoneMetrics metricsCollection, timeout time.Duration) (*Exporter, error) {
var fetch func(path string) (io.ReadCloser, error)
fetch = fetchHTTP(uri, bunnyAPIKey, sslVerify, timeout)
return &Exporter{
URI: uri,
fetch: fetch,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the last scrape of bunny successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_total_scrapes",
Help: "Current total BunnyCDN scrapes.",
}),
totalErrors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_total_errors",
Help: "Number of errors while making API calls.",
}),
totalAPICalls: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_api_calls_total",
Help: "Number of calls made to BunnyCDN API",
}),
accountMetrics: accountMetrics,
pullZoneMetrics: pullZoneMetrics,
}, nil
}
// Describe describes all the metrics ever exported by the BunnyCDN exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.accountMetrics {
ch <- m
}
for _, m := range e.pullZoneMetrics {
ch <- m
}
ch <- e.up.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.totalErrors.Desc()
ch <- e.totalAPICalls.Desc()
}
// Collect fetches the stats from BunnyCDN API and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
up := e.scrape(ch)
e.up.Set(up)
// ch <- prometheus.MustNewConstMetric(bunnyUp, prometheus.GaugeValue, up)
ch <- e.up
ch <- e.totalScrapes
ch <- e.totalErrors
ch <- e.totalAPICalls
}
func getStatisticsForPullZone(fetch func(path string) (io.ReadCloser, error), pz bunnyPullZone) (*bunnyStatistics, error) {
return rawGetStatistics(fetch, map[string]string{"pullZone": fmt.Sprintf("%d", pz.ID)})
}
func getStatistics(fetch func(path string) (io.ReadCloser, error)) (*bunnyStatistics, error) {
return rawGetStatistics(fetch, nil)
}
func rawGetStatistics(fetch func(path string) (io.ReadCloser, error), extraParams map[string]string) (*bunnyStatistics, error) {
today := time.Now()
p := url.Values{}
p.Add("dateFrom", today.Format("2006-01-02"))
p.Add("dateTo", today.Format("2006-01-02"))
p.Add("loadErrors", "true")
for k, v := range extraParams {
p.Add(k, v)
}
body, err := fetch(
fmt.Sprintf(
"/statistics?%s",
p.Encode(),
),
)
if err != nil {
return nil, err
}
bodyContent, err := ioutil.ReadAll(body)
if err != nil {
fmt.Printf("Reading failed (read: %d): %s\n", len(bodyContent), err)
return nil, err
}
var stats bunnyStatistics
err = json.Unmarshal(bodyContent, &stats)
return &stats, err
}
func listPullZones(fetch func(path string) (io.ReadCloser, error)) ([]bunnyPullZone, error) {
// body, err := fetchHTTP("https://bunnycdn.com/api/pullzone", true, time.Seconds*5)
body, err := fetch("/pullzone")
if err != nil {
return nil, err
}
bodyContent, err := ioutil.ReadAll(body)
if err != nil {
fmt.Printf("Reading failed (read: %d): %s\n", len(bodyContent), err)
return nil, err
}
var pullZones []bunnyPullZone
err = json.Unmarshal(bodyContent, &pullZones)
return pullZones, err
}
func fetchHTTP(uri string, bunnyAPIKey string, sslVerify bool, timeout time.Duration) func(path string) (io.ReadCloser, error) {
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !sslVerify}}
client := http.Client{
Timeout: timeout,
Transport: tr,
}
return func(path string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", uri+path, nil)
req.Header.Set("AccessKey", bunnyAPIKey)
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
resp.Body.Close()
return nil, fmt.Errorf("HTTP status %d (%s)", resp.StatusCode, resp.Request.URL)
}
return resp.Body, nil
}
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
e.totalScrapes.Inc()
pullZones, err := listPullZones(e.fetch)
e.totalAPICalls.Inc()
// body, err := e.fetch("/metrics")
if err != nil {
log.Errorf("Unable to list pull zones: %v", err)
e.totalErrors.Inc()
return 0
}
var aStatsObj *bunnyStatistics
for _, pullZone := range pullZones {
stats, err := getStatisticsForPullZone(e.fetch, pullZone)
e.totalAPICalls.Inc()
if err != nil {
log.Errorf("Unable to collect stats: %v", err)
e.totalErrors.Inc()
}
aStatsObj = stats
for name, metric := range e.pullZoneMetrics {
switch name {
case metricBandwidthUsed:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.BandwidthUsed), pullZone.Name)
case metricBandwidthCached:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.BandwidthUsed), pullZone.Name)
case metricRequestsServer:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.RequestsServed), pullZone.Name)
case metricPullRequestsPulled:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.PullRequestsPulled), pullZone.Name)
case metricErr3xx:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.Error3Xx), pullZone.Name)
case metricErr4xx:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.Error4Xx), pullZone.Name)
case metricErr5xx:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(stats.Error5Xx), pullZone.Name)
case metricGeoTrafficDist:
for _, loc := range stats.trafficLocations() {
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, loc.Requests, pullZone.Name, loc.Region, loc.Location)
}
}
}
}
if aStatsObj == nil {
aStatsObj, err = getStatistics(e.fetch)
e.totalAPICalls.Inc()
if err != nil {
log.Errorf("Unable to collect global stats (since no pullzone was found): %v", err)
e.totalErrors.Inc()
}
}
if aStatsObj != nil {
for name, metric := range e.accountMetrics {
switch name {
case metricBalance:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(aStatsObj.UserBalanceHistory))
case metricStorageUsed:
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, extractFromMap(aStatsObj.UserStorageUsed))
}
}
}
return 1
}
func main() {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9584").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
bunnyAPIURI = kingpin.Flag("bunnycdn.api-uri", "API URI on which to get stats from.").Default("https://bunnycdn.com/api").String()
bunnyAPIKey = kingpin.Flag("bunnycdn.api-key", "API key to connect to bunny.").Default(os.Getenv("BUNNYCDN_API_KEY")).String()
bunnySSLVerify = kingpin.Flag("bunnycdn.ssl-verify", "Flag that enables SSL certificate verification for the API URI").Default("true").Bool()
bunnyTimeout = kingpin.Flag("bunnycdn.timeout", "Timeout for trying to get stats from BunnyCDN.").Default("10s").Duration()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("bunnycdn_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting bunnycdn_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
exporter, err := NewExporter(*bunnyAPIURI, *bunnyAPIKey, *bunnySSLVerify, accountMetrics, pullZoneMetrics, *bunnyTimeout)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
prometheus.MustRegister(version.NewCollector("bunnycdn_exporter"))
log.Infoln("Listening on", *listenAddress)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>BunnyCDN Exporter</title></head>
<body>
<h1>BunnyCDN Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}