/
liveness_collector.go
300 lines (273 loc) · 12.9 KB
/
liveness_collector.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
// Copyright 2021 Akamai Technologies, Inc.
// 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 collectors
import (
"fmt"
client "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
gtm "github.com/akamai/AkamaiOPEN-edgegrid-golang/reportsgtm-v1" // Note: imports ./configgtm-v1_3
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"strconv"
"time"
)
var (
gtmLivenessTrafficExporter GTMLivenessTrafficExporter
durationBuckets = []float64{60, 1800, 3600, 7200, 14400}
)
type GTMLivenessTrafficExporter struct {
GTMConfig GTMMetricsConfig
LivenessMetricPrefix string
LivenessLookbackDuration time.Duration
LastTimestamp map[string]map[string]time.Time // index by domain, liveness
LivenessRegistry *prometheus.Registry
}
func NewLivenessTrafficCollector(r *prometheus.Registry, gtmMetricsConfig GTMMetricsConfig, gtmMetricPrefix string, tstart time.Time, lookbackDuration time.Duration) *GTMLivenessTrafficExporter {
gtmLivenessTrafficExporter = GTMLivenessTrafficExporter{GTMConfig: gtmMetricsConfig, LivenessLookbackDuration: lookbackDuration}
gtmLivenessTrafficExporter.LivenessMetricPrefix = gtmMetricPrefix + "property_liveness_errors"
gtmLivenessTrafficExporter.LivenessLookbackDuration = lookbackDuration
gtmLivenessTrafficExporter.LivenessRegistry = r
// Populate LastTimestamp per domain, liveness. Start time applies to all.
domainMap := make(map[string]map[string]time.Time)
for _, domain := range gtmMetricsConfig.Domains {
tStampMap := make(map[string]time.Time) // index by property name
livenessDurationHistogramMap[domain.Name] = make(map[string]map[int]prometheus.Histogram)
livenessErrorsSummaryMap[domain.Name] = make(map[string]map[int]prometheus.Summary)
for _, prop := range domain.Liveness {
livenessDurationHistogramMap[domain.Name][prop.PropertyName] = make(map[int]prometheus.Histogram)
livenessErrorsSummaryMap[domain.Name][prop.PropertyName] = make(map[int]prometheus.Summary)
tStampMap[prop.PropertyName] = tstart
}
domainMap[domain.Name] = tStampMap
}
gtmLivenessTrafficExporter.LastTimestamp = domainMap
return >mLivenessTrafficExporter
}
// Summaries map by domain, property, datacenter
var livenessDurationHistogramMap = make(map[string]map[string]map[int]prometheus.Histogram)
var livenessErrorsSummaryMap = make(map[string]map[string]map[int]prometheus.Summary)
func (l *GTMLivenessTrafficExporter) getDatacenterHistogramMetrics(domain, property string, dcid int) map[string]interface{} {
histMap := make(map[string]interface{})
if histo, ok := livenessDurationHistogramMap[domain][property][dcid]; ok {
histMap["duration"] = histo
} else {
// doesn't exist. need to create
labels := prometheus.Labels{"domain": domain, "property": property, "datacenter": strconv.Itoa(dcid)}
livenessDurationHistogramMap[domain][property][dcid] = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: gtmLivenessTrafficExporter.LivenessMetricPrefix,
Name: "duration_per_datacenter_histogram",
Help: "Histogram of datacenter error duration (per domain and property)",
ConstLabels: labels,
Buckets: durationBuckets,
})
l.LivenessRegistry.MustRegister(livenessDurationHistogramMap[domain][property][dcid])
histMap["duration"] = livenessDurationHistogramMap[domain][property][dcid]
}
if esum, ok := livenessErrorsSummaryMap[domain][property][dcid]; ok {
histMap["errors"] = esum
} else {
// doesn't exist. need to create
labels := prometheus.Labels{"domain": domain, "property": property, "datacenter": strconv.Itoa(dcid)}
livenessErrorsSummaryMap[domain][property][dcid] = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: gtmLivenessTrafficExporter.LivenessMetricPrefix,
Name: "errors_per_datacenter_summary",
Help: "Summary of datacenter errors (per domain and property)",
ConstLabels: labels,
MaxAge: gtmLivenessTrafficExporter.LivenessLookbackDuration,
BufCap: prometheus.DefBufCap * 2,
})
l.LivenessRegistry.MustRegister(livenessErrorsSummaryMap[domain][property][dcid])
histMap["errors"] = livenessErrorsSummaryMap[domain][property][dcid]
}
return histMap
}
// Describe function
func (l *GTMLivenessTrafficExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc(l.LivenessMetricPrefix, "Akamai GTM Property Liveness Errors", nil, nil)
}
// Collect function
func (l *GTMLivenessTrafficExporter) Collect(ch chan<- prometheus.Metric) {
log.Debugf("Entering GTM Property Liveness Errors Collect")
endtime := time.Now().UTC() // Use same current time for all zones
// Collect metrics for each domain and liveness
for _, domain := range l.GTMConfig.Domains {
log.Debugf("Processing domain %s", domain.Name)
for _, prop := range domain.Liveness {
// get last timestamp recorded. make sure diff > 5 mins.
lasttime := l.LastTimestamp[domain.Name][prop.PropertyName].Add(time.Minute)
log.Debugf("Fetching liveness errors Report for property %s in domain %s.", prop.PropertyName, domain.Name)
livenessTrafficReport, err := retrieveLivenessTraffic(domain.Name, prop.PropertyName, prop.AgentIP, prop.TargetIP, lasttime)
if err != nil {
apierr, ok := err.(client.APIError)
if ok && apierr.Status == 500 {
log.Warnf("Unable to get liveness errors report for property %s. Internal error ... Skipping.", prop.PropertyName)
continue
}
log.Errorf("Unable to get liveness report for property %s ... Skipping. Error: %s", prop.PropertyName, err.Error())
continue
}
if len(livenessTrafficReport.DataRows) < 1 && endtime.Day() != lasttime.Day() {
// We've probably crossed a day boundary. Bump last time
lasttime = lasttime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
// get updated report
livenessTrafficReport, err = retrieveLivenessTraffic(domain.Name, prop.PropertyName, prop.AgentIP, prop.TargetIP, lasttime)
if err != nil {
apierr, ok := err.(client.APIError)
if ok && apierr.Status == 500 {
log.Warnf("Unable to get liveness errors report for property %s. Internal error ... Skipping.", prop.PropertyName)
continue
}
if ok && apierr.Status == 400 {
log.Warnf("Unable to get liveness errors report for property %s. ... Skipping.", prop.PropertyName)
log.Errorf("%s", err.Error())
continue
}
log.Errorf("Unable to get liveness report for property %s ... Skipping. Error: %s", prop.PropertyName, err.Error())
continue
}
}
log.Debugf("Traffic Metadata: [%v]", livenessTrafficReport.Metadata)
for _, reportInstance := range livenessTrafficReport.DataRows {
instanceTimestamp, err := parseTimeString(reportInstance.Timestamp, GTMTrafficLongTimeFormat)
if err != nil {
log.Errorf("Instance timestamp invalid ... Skipping. Error: %s", err.Error())
continue
}
if !instanceTimestamp.After(l.LastTimestamp[domain.Name][prop.PropertyName]) {
log.Debugf("Instance timestamp: [%v]. Last timestamp: [%v]", instanceTimestamp, l.LastTimestamp[domain.Name][prop.PropertyName])
log.Warnf("Attempting to re process report instance: [%v]. Skipping.", reportInstance)
continue
}
// See if we missed an interval. Log warning for low
log.Debugf("Instance timestamp: [%v]. Last timestamp: [%v]", instanceTimestamp, l.LastTimestamp[domain.Name][prop.PropertyName])
var baseLabels = []string{"domain", "property", "datacenter"}
for _, instanceDC := range reportInstance.Datacenters {
// create metrics for datacenters in property per timestamp
var tsLabels = baseLabels
labelVals := []string{domain.Name, prop.PropertyName, strconv.Itoa(instanceDC.DatacenterID)}
if prop.AgentIP == instanceDC.AgentIP {
tsLabels = append(tsLabels, "agentip")
labelVals = append(labelVals, instanceDC.AgentIP)
}
if prop.TargetIP == instanceDC.TargetIP {
tsLabels = append(tsLabels, "targetip")
labelVals = append(labelVals, instanceDC.TargetIP)
}
if prop.ErrorCode {
tsLabels = append(tsLabels, "errorcode")
codestring := fmt.Sprintf("%v", instanceDC.ErrorCode)
labelVals = append(labelVals, codestring)
}
ts := instanceTimestamp.Format(time.RFC3339)
if l.GTMConfig.TSLabel {
tsLabels = append(tsLabels, "interval_timestamp")
labelVals = append(labelVals, ts)
}
desc := prometheus.NewDesc(prometheus.BuildFQName(l.LivenessMetricPrefix, "", "datacenter_failures"), "Number of datacenter failures (per domain, property, datacenter)", tsLabels, nil)
log.Debugf("Creating error failures counter metric. Domain: %s, Property: %s, Datacenter: %d, Timestamp: %v", domain.Name, prop.PropertyName, instanceDC.DatacenterID, ts)
var errorsmetric, durmetric prometheus.Metric
errorsmetric = prometheus.MustNewConstMetric(
desc, prometheus.CounterValue, 1, labelVals...)
if l.GTMConfig.UseTimestamp != nil && !*l.GTMConfig.UseTimestamp {
ch <- errorsmetric
} else {
ch <- prometheus.NewMetricWithTimestamp(instanceTimestamp, errorsmetric)
}
desc = prometheus.NewDesc(prometheus.BuildFQName(l.LivenessMetricPrefix, "", "datacenter_failure_duration"), "Datacenter falure duration (per domain, property, datacenter)", tsLabels, nil)
log.Debugf("Creating failure duration gauge metric. Domain: %s, Property: %s, Datacenter: %d, Timestamp: %v", domain.Name, prop.PropertyName, instanceDC.DatacenterID, ts)
durmetric = prometheus.MustNewConstMetric(
desc, prometheus.GaugeValue, float64(instanceDC.Duration), labelVals...)
if l.GTMConfig.UseTimestamp != nil && !*l.GTMConfig.UseTimestamp {
ch <- durmetric
} else {
ch <- prometheus.NewMetricWithTimestamp(instanceTimestamp, durmetric)
}
maps := l.getDatacenterHistogramMetrics(domain.Name, prop.PropertyName, instanceDC.DatacenterID)
maps["duration"].(prometheus.Histogram).Observe(float64(instanceDC.Duration))
maps["errors"].(prometheus.Summary).Observe(float64(1))
} // datacenter end
// Update last timestamp processed
if instanceTimestamp.After(l.LastTimestamp[domain.Name][prop.PropertyName]) {
log.Debugf("Updating Last Timestamp from %v TO %v", l.LastTimestamp[domain.Name][prop.PropertyName], instanceTimestamp)
l.LastTimestamp[domain.Name][prop.PropertyName] = instanceTimestamp
}
// only process one each interval!
break
} // interval end
} // liveness end
} // domain end
}
func retrieveLivenessTraffic(domain, prop, agentID, targetID string, start time.Time) (*LivenessErrorsResponse, error) {
qargs := make(map[string]string)
if len(targetID) > 0 {
qargs["targetId"] = targetID // Takes priority
}
if len(agentID) > 0 {
if len(targetID) > 0 {
log.Warnf("Both agentId and targetId filters set. Using targetId ONLY")
} else {
qargs["agentId"] = agentID
}
}
// Get valid Traffic Window
var err error
livenessTrafficWindow, err := gtm.GetLivenessTestsWindow()
if err != nil {
return nil, err
}
// Make sure provided start and end are in range
if livenessTrafficWindow.StartTime.Before(start) {
if livenessTrafficWindow.EndTime.After(start) {
qargs["date"], err = convertTimeFormat(start, GTMTrafficDateFormat)
} else {
qargs["date"], err = convertTimeFormat(livenessTrafficWindow.EndTime, GTMTrafficDateFormat)
}
} else {
qargs["date"], err = convertTimeFormat(livenessTrafficWindow.StartTime, GTMTrafficDateFormat)
}
if err != nil {
return nil, err
}
resp, err := GetLivenessErrorsReport(domain, prop, qargs)
if err != nil {
return nil, err
}
/*
// DEBUG
meta := &LivenessTMeta{
Date: "2016-11-23",
Domain: "example.akadns.net",
Property: "www",
Uri: "https://akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/liveness-tests/domains/example.akadns.net/properties/www?date=2016-11-23"}
dcptr := &LivenessDRow{
DatacenterID: 3201,
AgentIP: "204.1.136.239",
TestName: "Our defences",
ErrorCode: 3101,
Duration: 0,
Nickname: "Winterfell",
TrafficTargetName: "Winterfell - 1.2.3.4",
TargetIP: "1.2.3.4"}
ldrows := []*LivenessDRow{dcptr}
ldrowptr := &LivenessTData{Timestamp: time.Now().UTC().Add(-10*time.Minute).Format(time.RFC3339), Datacenters: ldrows}
resp = &LivenessErrorsResponse{
Metadata: meta,
DataRows: []*LivenessTData{ldrowptr},
}
// END DEBUG
*/
//DataRows is list of pointers
sortLivenessDataRowsByTimestamp(resp.DataRows)
return resp, nil
}