-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexporter.go
381 lines (315 loc) · 9.78 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
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/PagerDuty/go-pagerduty"
elasticsearch "github.com/elastic/go-elasticsearch/v7"
esapi "github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type (
PagerdutyElasticsearchExporter struct {
scrapeTime *time.Duration
elasticSearchClient *elasticsearch.Client
elasticsearchIndexName string
elasticsearchBatchCount int
elasticsearchRetryCount int
elasticsearchRetryDelay time.Duration
pagerdutyClient *pagerduty.Client
pagerdutyDateRange *time.Duration
prometheus struct {
incident *prometheus.CounterVec
incidentLogEntry *prometheus.CounterVec
esRequestTotal *prometheus.CounterVec
esRequestRetries *prometheus.CounterVec
duration *prometheus.GaugeVec
}
}
ElasticsearchIncident struct {
DocumentID string `json:"_id,omitempty"`
Timestamp string `json:"@timestamp,omitempty"`
IncidentId string `json:"@incident,omitempty"`
*pagerduty.Incident
}
ElasticsearchIncidentLog struct {
DocumentID string `json:"_id,omitempty"`
Timestamp string `json:"@timestamp,omitempty"`
IncidentId string `json:"@incident,omitempty"`
*pagerduty.LogEntry
}
)
func (e *PagerdutyElasticsearchExporter) Init() {
e.elasticsearchBatchCount = 10
e.elasticsearchRetryCount = 5
e.elasticsearchRetryDelay = 5 * time.Second
e.prometheus.incident = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pagerduty2es_incident_total",
Help: "PagerDuty2es incident counter",
},
[]string{},
)
e.prometheus.incidentLogEntry = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pagerduty2es_incident_logentry_total",
Help: "PagerDuty2es incident logentry counter",
},
[]string{},
)
e.prometheus.esRequestTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pagerduty2es_elasticsearch_requet_total",
Help: "PagerDuty2es elasticsearch request total counter",
},
[]string{},
)
e.prometheus.esRequestRetries = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pagerduty2es_elasticsearch_request_retries",
Help: "PagerDuty2es elasticsearch request retries counter",
},
[]string{},
)
e.prometheus.duration = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty2es_duration",
Help: "PagerDuty2es duration",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.incident)
prometheus.MustRegister(e.prometheus.incidentLogEntry)
prometheus.MustRegister(e.prometheus.esRequestTotal)
prometheus.MustRegister(e.prometheus.esRequestRetries)
prometheus.MustRegister(e.prometheus.duration)
}
func (e *PagerdutyElasticsearchExporter) SetScrapeTime(value time.Duration) {
e.scrapeTime = &value
}
func (e *PagerdutyElasticsearchExporter) ConnectPagerduty(token string, httpClient *http.Client) {
e.pagerdutyClient = pagerduty.NewClient(token)
e.pagerdutyClient.HTTPClient = httpClient
}
func (e *PagerdutyElasticsearchExporter) SetPagerdutyDateRange(value time.Duration) {
e.pagerdutyDateRange = &value
}
func (e *PagerdutyElasticsearchExporter) ConnectElasticsearch(cfg elasticsearch.Config, indexName string) {
var err error
e.elasticSearchClient, err = elasticsearch.NewClient(cfg)
if err != nil {
panic(err)
}
tries := 0
for {
_, err = e.elasticSearchClient.Info()
if err != nil {
tries++
if tries >= 5 {
panic(err)
} else {
log.Info("failed to connect to ES, retry...")
time.Sleep(5 * time.Second)
continue
}
}
break
}
e.elasticsearchIndexName = indexName
}
func (e *PagerdutyElasticsearchExporter) SetElasticsearchBatchCount(batchCount int) {
e.elasticsearchBatchCount = batchCount
}
func (e *PagerdutyElasticsearchExporter) SetElasticsearchRetry(retryCount int, retryDelay time.Duration) {
e.elasticsearchRetryCount = retryCount
e.elasticsearchRetryDelay = retryDelay
}
func (e *PagerdutyElasticsearchExporter) RunSingle() {
e.runScrape()
}
func (e *PagerdutyElasticsearchExporter) RunDaemon() {
go func() {
for {
e.runScrape()
e.sleepUntilNextCollection()
}
}()
}
func (e *PagerdutyElasticsearchExporter) sleepUntilNextCollection() {
log.Debugf("sleeping %v", e.scrapeTime)
time.Sleep(*e.scrapeTime)
}
func (e *PagerdutyElasticsearchExporter) runScrape() {
var wgProcess sync.WaitGroup
log.Info("starting scrape")
since := time.Now().Add(-*e.pagerdutyDateRange).Format(time.RFC3339)
listOpts := pagerduty.ListIncidentsOptions{
Since: since,
}
listOpts.Limit = PagerdutyIncidentLimit
listOpts.Offset = 0
esIndexRequestChannel := make(chan *esapi.IndexRequest, e.elasticsearchBatchCount)
startTime := time.Now()
// index from channel
wgProcess.Add(1)
go func() {
defer wgProcess.Done()
bulkIndexRequests := []*esapi.IndexRequest{}
for esIndexRequest := range esIndexRequestChannel {
bulkIndexRequests = append(bulkIndexRequests, esIndexRequest)
if len(bulkIndexRequests) >= e.elasticsearchBatchCount {
e.doESIndexRequestBulk(bulkIndexRequests)
bulkIndexRequests = []*esapi.IndexRequest{}
}
}
if len(bulkIndexRequests) >= 1 {
e.doESIndexRequestBulk(bulkIndexRequests)
}
}()
for {
ctx := context.Background()
incidentResponse, err := e.pagerdutyClient.ListIncidentsWithContext(ctx, listOpts)
if err != nil {
panic(err)
}
for _, incident := range incidentResponse.Incidents {
// workaround for https://github.com/PagerDuty/go-pagerduty/issues/218
contextLogger := log.WithField("incident", incident.ID)
contextLogger.Debugf("incident %v", incident.ID)
e.indexIncident(incident, esIndexRequestChannel)
listLogOpts := pagerduty.ListIncidentLogEntriesOptions{}
incidentLogResponse, err := e.pagerdutyClient.ListIncidentLogEntriesWithContext(ctx, incident.ID, listLogOpts)
if err != nil {
panic(err)
}
for _, logEntry := range incidentLogResponse.LogEntries {
contextLogger.WithField("logEntry", logEntry.ID).Debugf("logEntry %v", logEntry.ID)
e.indexIncidentLogEntry(incident, logEntry, esIndexRequestChannel)
}
}
if !incidentResponse.More {
break
}
listOpts.Offset += listOpts.Limit
}
close(esIndexRequestChannel)
wgProcess.Wait()
duration := time.Since(startTime)
e.prometheus.duration.WithLabelValues().Set(duration.Seconds())
log.WithField("duration", duration.String()).Info("finished scraping")
}
func (e *PagerdutyElasticsearchExporter) indexIncident(incident pagerduty.Incident, callback chan<- *esapi.IndexRequest) {
e.prometheus.incident.WithLabelValues().Inc()
createTime, err := time.Parse(time.RFC3339, incident.CreatedAt)
if err != nil {
panic(err)
}
esIncident := ElasticsearchIncident{
Timestamp: createTime.Format(time.RFC3339),
IncidentId: incident.ID,
Incident: &incident,
}
incidentJson, _ := json.Marshal(esIncident)
req := esapi.IndexRequest{
Index: e.buildIndexName(createTime),
DocumentID: fmt.Sprintf("incident-%v", incident.ID),
Body: bytes.NewReader(incidentJson),
}
callback <- &req
}
func (e *PagerdutyElasticsearchExporter) buildIndexName(createTime time.Time) string {
ret := e.elasticsearchIndexName
ret = strings.Replace(ret, "%y", createTime.Format("2006"), -1)
ret = strings.Replace(ret, "%m", createTime.Format("01"), -1)
ret = strings.Replace(ret, "%d", createTime.Format("02"), -1)
return ret
}
func (e *PagerdutyElasticsearchExporter) indexIncidentLogEntry(incident pagerduty.Incident, logEntry pagerduty.LogEntry, callback chan<- *esapi.IndexRequest) {
e.prometheus.incidentLogEntry.WithLabelValues().Inc()
createTime, err := time.Parse(time.RFC3339, logEntry.CreatedAt)
if err != nil {
panic(err)
}
esLogEntry := ElasticsearchIncidentLog{
Timestamp: createTime.Format(time.RFC3339),
IncidentId: incident.ID,
LogEntry: &logEntry,
}
logEntryJson, _ := json.Marshal(esLogEntry)
req := esapi.IndexRequest{
Index: e.buildIndexName(createTime),
DocumentID: fmt.Sprintf("logentry-%v", logEntry.ID),
Body: bytes.NewReader(logEntryJson),
}
callback <- &req
}
type (
BulkMetaIndex struct {
Index BulkMetaIndexIndex `json:"index,omitempty"`
}
BulkMetaIndexIndex struct {
Id string `json:"_id,omitempty"`
Type string `json:"_type,omitempty"`
Index string `json:"_index,omitempty"`
}
)
func (e *PagerdutyElasticsearchExporter) doESIndexRequestBulk(bulkRequests []*esapi.IndexRequest) {
var buf bytes.Buffer
newline := []byte("\n")
var err error
var resp *esapi.Response
for i := 0; i < e.elasticsearchRetryCount; i++ {
for _, indexRequest := range bulkRequests {
// generate bulk index action line
meta := BulkMetaIndex{
Index: BulkMetaIndexIndex{
Id: indexRequest.DocumentID,
Type: indexRequest.DocumentType,
Index: indexRequest.Index,
},
}
metaJson, _ := json.Marshal(meta)
// generate document line
document := new(bytes.Buffer)
_, readErr := document.ReadFrom(indexRequest.Body)
if readErr != nil {
panic(readErr)
}
// generate index line
buf.Grow(len(metaJson) + len(newline) + document.Len() + len(newline))
buf.Write(metaJson)
buf.Write(newline)
buf.Write(document.Bytes())
buf.Write(newline)
}
e.prometheus.esRequestTotal.WithLabelValues().Inc()
resp, err = e.elasticSearchClient.Bulk(bytes.NewReader(buf.Bytes()))
if err == nil && resp.StatusCode == http.StatusOK {
if err := resp.Body.Close(); err != nil {
log.Errorf(err.Error())
}
// success
return
}
if resp != nil {
log.Errorf("unexpected HTTP %v response: %v", resp.StatusCode, resp.String())
}
// got an error
log.Errorf("retrying ES index error: %v", err)
e.prometheus.esRequestRetries.WithLabelValues().Inc()
// wait until retry
time.Sleep(e.elasticsearchRetryDelay)
}
// must be an error
if err != nil {
log.Panicf("fatal ES index error: %v", err)
} else {
panic("Unable to process ES request")
}
}