-
Notifications
You must be signed in to change notification settings - Fork 5
/
exporter.go
307 lines (278 loc) · 7.83 KB
/
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
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package main
import (
"context"
"crypto/tls"
"fmt"
_ "net/http/pprof"
"strings"
"sync"
"time"
driver "github.com/arangodb/go-driver"
driver_http "github.com/arangodb/go-driver/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const (
namespace = "arangodb" // For Prometheus metrics.
)
// metricKey returns a key into the map of metrics for the given figure & group.
func metricKey(group StatisticGroup, figure StatisticFigure, postfix string) string {
result := strings.Replace(strings.ToLower(group.Name+"_"+figure.Name), " ", "_", -1)
if postfix != "" {
result = result + postfix
}
if figure.Units != "" {
return result + "_" + strings.ToLower(figure.Units)
}
return result
}
// newMetric creates one or more metrics for the given figure & group.
func newMetric(group StatisticGroup, figure StatisticFigure) []prometheus.Collector {
switch figure.Type {
case FigureTypeDistribution:
return []prometheus.Collector{
// _sum
prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: metricKey(group, figure, "_sum"),
Help: figure.Description,
}),
// _count
prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: metricKey(group, figure, "_count"),
Help: figure.Description,
}),
// _bucket
prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: metricKey(group, figure, "_bucket"),
Help: figure.Description,
}, []string{"le"}),
}
default:
return []prometheus.Collector{
prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: metricKey(group, figure, ""),
Help: figure.Description,
}),
}
}
}
// Exporter collects ArangoDB statistics from the given endpoint and exports them using
// the prometheus metrics package.
type Exporter struct {
factory connClientFactory
timeout time.Duration
mutex sync.RWMutex
metrics map[string][]prometheus.Collector
up prometheus.Gauge
totalScrapes, failedScrapes prometheus.Counter
}
// NewExporter returns an initialized Exporter.
func NewExporter(arangodbEndpoint string, jwt Authentication, sslVerify bool, timeout time.Duration) (*Exporter, error) {
return &Exporter{
factory: newConnClientFactory(arangodbEndpoint, jwt, sslVerify, timeout),
timeout: timeout,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the last scrape of ArangoDB successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_total_scrapes",
Help: "Current total ArangoDB scrapes.",
}),
failedScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_failed_scrapes",
Help: "Number of failed ArangoDB scrapes",
}),
metrics: make(map[string][]prometheus.Collector),
}, nil
}
type connClientFactory func() (driver.Connection, error)
func newConnClientFactory(arangodbEndpoint string, auth Authentication, sslVerify bool, timeout time.Duration) connClientFactory {
return func() (driver.Connection, error) {
connCfg := driver_http.ConnectionConfig{
Endpoints: []string{arangodbEndpoint},
}
if !sslVerify {
connCfg.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
jwt, err := auth()
if err != nil {
return nil, err
}
conn, err := driver_http.NewConnection(connCfg)
if err != nil {
return nil, maskAny(err)
}
if jwt != "" {
hdr, err := CreateArangodJwtAuthorizationHeader(jwt)
if err != nil {
return nil, maskAny(err)
}
auth := driver.RawAuthentication(hdr)
conn, err = conn.SetAuthentication(auth)
if err != nil {
return nil, maskAny(err)
}
}
return conn, nil
}
}
// Describe describes all the metrics ever exported by the HAProxy exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, ms := range e.metrics {
for _, m := range ms {
m.Describe(ch)
}
}
ch <- e.up.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.failedScrapes.Desc()
}
// Collect fetches the stats from ArangoDB statistics 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()
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
e.resetMetrics()
e.scrape(ctx)
ch <- e.up
ch <- e.totalScrapes
ch <- e.failedScrapes
e.collectMetrics(ch)
}
// scrape performs a single query of all statistics.
func (e *Exporter) scrape(ctx context.Context) {
e.totalScrapes.Inc()
conn, err := e.factory()
if err != nil {
e.up.Set(0)
log.Errorf("Failed to create client: %v", err)
return
}
// Gather descriptions
descr, err := GetStatisticsDescription(ctx, conn)
if err != nil {
e.up.Set(0)
log.Errorf("Failed to fetch statistic descriptions: %v", err)
return
}
// Collect statistics
stats, err := GetStatistics(ctx, conn)
if err != nil {
e.up.Set(0)
log.Errorf("Failed to fetch statistics: %v", err)
return
}
// Mark ArangoDB as up.
e.up.Set(1)
// Now parse the statistics & put them in the correct metrics
groups := make(map[string]StatisticGroup)
for _, g := range descr.Groups {
groups[g.Group] = g
}
for _, f := range descr.Figures {
group, found := groups[f.Group]
if !found {
// Skip figure with unknown group
continue
}
groupStats := stats.GetGroup(f.Group)
if groupStats == nil {
// Skip no group is found in the statistics
}
key := metricKey(group, f, "")
ms, found := e.metrics[key]
if !found {
ms = newMetric(group, f)
e.metrics[key] = ms
}
switch f.Type {
case FigureTypeCurrent, FigureTypeAccumulated:
if value, ok := groupStats.GetFloat(f.Identifier); ok {
gauge := ms[0].(prometheus.Gauge)
gauge.Set(value)
}
case FigureTypeDistribution:
distStats := groupStats.GetGroup(f.Identifier)
if distStats != nil {
// _sum comes first
if sum, ok := distStats.GetFloat("sum"); ok {
gauge := ms[0].(prometheus.Gauge)
gauge.Set(sum)
}
// _count comes second
if sum, ok := distStats.GetFloat("count"); ok {
gauge := ms[1].(prometheus.Gauge)
gauge.Set(sum)
}
// _bucket comes third
if counts, ok := distStats.GetCounts("counts"); ok {
gaugeVec := ms[2].(*prometheus.GaugeVec)
cummulative := int64(0)
for i, v := range counts {
var leValue string
if i < len(f.Cuts) {
leValue = fmt.Sprintf("%v", f.Cuts[i])
} else {
leValue = "+Inf"
}
gaugeVec.WithLabelValues(leValue).Set(float64(cummulative + v))
cummulative += v
}
}
}
}
}
}
type resetter interface {
Reset()
}
func (e *Exporter) resetMetrics() {
for _, ms := range e.metrics {
for _, m := range ms {
if r, ok := m.(resetter); ok {
r.Reset()
}
}
}
}
func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
for _, ms := range e.metrics {
for _, m := range ms {
if c, ok := m.(prometheus.Collector); ok {
c.Collect(metrics)
}
}
}
}