-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdatadog_scaler.go
413 lines (354 loc) · 11.6 KB
/
datadog_scaler.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
package scalers
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"time"
datadog "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/go-logr/logr"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/metrics/pkg/apis/external_metrics"
kedautil "github.com/kedacore/keda/v2/pkg/util"
)
type datadogScaler struct {
metadata *datadogMetadata
apiClient *datadog.APIClient
logger logr.Logger
}
type datadogMetadata struct {
apiKey string
appKey string
datadogSite string
query string
queryValue float64
queryAggegrator string
activationQueryValue float64
vType v2.MetricTargetType
metricName string
age int
timeWindowOffset int
lastAvailablePointOffset int
useFiller bool
fillValue float64
}
const maxString = "max"
const avgString = "average"
var filter *regexp.Regexp
func init() {
filter = regexp.MustCompile(`.*\{.*\}.*`)
}
// NewDatadogScaler creates a new Datadog scaler
func NewDatadogScaler(ctx context.Context, config *ScalerConfig) (Scaler, error) {
logger := InitializeLogger(config, "datadog_scaler")
meta, err := parseDatadogMetadata(config, logger)
if err != nil {
return nil, fmt.Errorf("error parsing Datadog metadata: %w", err)
}
apiClient, err := newDatadogConnection(ctx, meta, config)
if err != nil {
return nil, fmt.Errorf("error establishing Datadog connection: %w", err)
}
return &datadogScaler{
metadata: meta,
apiClient: apiClient,
logger: logger,
}, nil
}
// parseDatadogQuery checks correctness of the user query
func parseDatadogQuery(q string) (bool, error) {
// Wellformed Datadog queries require a filter (between curly brackets)
if !filter.MatchString(q) {
return false, fmt.Errorf("malformed Datadog query: missing query scope")
}
return true, nil
}
func parseDatadogMetadata(config *ScalerConfig, logger logr.Logger) (*datadogMetadata, error) {
meta := datadogMetadata{}
if val, ok := config.TriggerMetadata["age"]; ok {
age, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("age parsing error %w", err)
}
meta.age = age
if age < 0 {
return nil, fmt.Errorf("age should not be smaller than 0 seconds")
}
if age < 60 {
logger.Info("selecting a window smaller than 60 seconds can cause Datadog not finding a metric value for the query")
}
} else {
meta.age = 90 // Default window 90 seconds
}
if val, ok := config.TriggerMetadata["timeWindowOffset"]; ok {
timeWindowOffset, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("timeWindowOffset parsing error %w", err)
}
if timeWindowOffset < 0 {
return nil, fmt.Errorf("timeWindowOffset should not be smaller than 0 seconds")
}
meta.timeWindowOffset = timeWindowOffset
} else {
meta.timeWindowOffset = 0 // Default delay 0 seconds
}
if val, ok := config.TriggerMetadata["lastAvailablePointOffset"]; ok {
lastAvailablePointOffset, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("lastAvailablePointOffset parsing error %w", err)
}
if lastAvailablePointOffset < 0 {
return nil, fmt.Errorf("lastAvailablePointOffset should not be smaller than 0")
}
meta.lastAvailablePointOffset = lastAvailablePointOffset
} else {
meta.lastAvailablePointOffset = 0 // Default use the last point
}
if val, ok := config.TriggerMetadata["query"]; ok {
_, err := parseDatadogQuery(val)
if err != nil {
return nil, fmt.Errorf("error in query: %w", err)
}
meta.query = val
} else {
return nil, fmt.Errorf("no query given")
}
if val, ok := config.TriggerMetadata["queryValue"]; ok {
queryValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("queryValue parsing error %w", err)
}
meta.queryValue = queryValue
} else {
return nil, fmt.Errorf("no queryValue given")
}
if val, ok := config.TriggerMetadata["queryAggregator"]; ok && val != "" {
queryAggregator := strings.ToLower(val)
switch queryAggregator {
case avgString, maxString:
meta.queryAggegrator = queryAggregator
default:
return nil, fmt.Errorf("queryAggregator value %s has to be one of '%s, %s'", queryAggregator, avgString, maxString)
}
} else {
meta.queryAggegrator = ""
}
meta.activationQueryValue = 0
if val, ok := config.TriggerMetadata["activationQueryValue"]; ok {
activationQueryValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("queryValue parsing error %w", err)
}
meta.activationQueryValue = activationQueryValue
}
if val, ok := config.TriggerMetadata["metricUnavailableValue"]; ok {
fillValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("metricUnavailableValue parsing error %w", err)
}
meta.fillValue = fillValue
meta.useFiller = true
}
if val, ok := config.TriggerMetadata["type"]; ok {
logger.V(0).Info("trigger.metadata.type is deprecated in favor of trigger.metricType")
if config.MetricType != "" {
return nil, fmt.Errorf("only one of trigger.metadata.type or trigger.metricType should be defined")
}
val = strings.ToLower(val)
switch val {
case avgString:
meta.vType = v2.AverageValueMetricType
case "global":
meta.vType = v2.ValueMetricType
default:
return nil, fmt.Errorf("type has to be global or average")
}
} else {
metricType, err := GetMetricTargetType(config)
if err != nil {
return nil, fmt.Errorf("error getting scaler metric type: %w", err)
}
meta.vType = metricType
}
if val, ok := config.AuthParams["apiKey"]; ok {
meta.apiKey = val
} else {
return nil, fmt.Errorf("no api key given")
}
if val, ok := config.AuthParams["appKey"]; ok {
meta.appKey = val
} else {
return nil, fmt.Errorf("no app key given")
}
siteVal := "datadoghq.com"
if val, ok := config.AuthParams["datadogSite"]; ok && val != "" {
siteVal = val
}
meta.datadogSite = siteVal
metricName := meta.query[0:strings.Index(meta.query, "{")]
meta.metricName = GenerateMetricNameWithIndex(config.ScalerIndex, kedautil.NormalizeString(fmt.Sprintf("datadog-%s", metricName)))
return &meta, nil
}
// newDatddogConnection tests a connection to the Datadog API
func newDatadogConnection(ctx context.Context, meta *datadogMetadata, config *ScalerConfig) (*datadog.APIClient, error) {
ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: meta.apiKey,
},
"appKeyAuth": {
Key: meta.appKey,
},
},
)
ctx = context.WithValue(ctx,
datadog.ContextServerVariables,
map[string]string{
"site": meta.datadogSite,
})
configuration := datadog.NewConfiguration()
configuration.HTTPClient = kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, false)
apiClient := datadog.NewAPIClient(configuration)
_, _, err := apiClient.AuthenticationApi.Validate(ctx) //nolint:bodyclose
if err != nil {
return nil, fmt.Errorf("error connecting to Datadog API endpoint: %w", err)
}
return apiClient, nil
}
// No need to close connections
func (s *datadogScaler) Close(context.Context) error {
return nil
}
// getQueryResult returns result of the scaler query
func (s *datadogScaler) getQueryResult(ctx context.Context) (float64, error) {
ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: s.metadata.apiKey,
},
"appKeyAuth": {
Key: s.metadata.appKey,
},
},
)
ctx = context.WithValue(ctx,
datadog.ContextServerVariables,
map[string]string{
"site": s.metadata.datadogSite,
})
timeWindowTo := time.Now().Unix() - int64(s.metadata.timeWindowOffset)
timeWindowFrom := timeWindowTo - int64(s.metadata.age)
resp, r, err := s.apiClient.MetricsApi.QueryMetrics(ctx, timeWindowFrom, timeWindowTo, s.metadata.query) //nolint:bodyclose
if r != nil {
if r.StatusCode == 429 {
rateLimit := r.Header.Get("X-Ratelimit-Limit")
rateLimitReset := r.Header.Get("X-Ratelimit-Reset")
rateLimitPeriod := r.Header.Get("X-Ratelimit-Period")
return -1, fmt.Errorf("your Datadog account reached the %s queries per %s seconds rate limit, next limit reset will happen in %s seconds", rateLimit, rateLimitPeriod, rateLimitReset)
}
if r.StatusCode != 200 {
if err != nil {
return -1, fmt.Errorf("error when retrieving Datadog metrics: %w", err)
}
return -1, fmt.Errorf("error when retrieving Datadog metrics")
}
}
if err != nil {
return -1, fmt.Errorf("error when retrieving Datadog metrics: %w", err)
}
if resp.GetStatus() == "error" {
if msg, ok := resp.GetErrorOk(); ok {
return -1, fmt.Errorf("error when retrieving Datadog metrics: %s", *msg)
}
return -1, fmt.Errorf("error when retrieving Datadog metrics")
}
series := resp.GetSeries()
if len(series) == 0 {
if !s.metadata.useFiller {
return 0, fmt.Errorf("no Datadog metrics returned for the given time window")
}
return s.metadata.fillValue, nil
}
// Require queryAggregator be set explicitly for multi-query
if len(series) > 1 && s.metadata.queryAggegrator == "" {
return 0, fmt.Errorf("query returned more than 1 series; modify the query to return only 1 series or add a queryAggregator")
}
// Collect all latest point values from any/all series
results := make([]float64, len(series))
for i := 0; i < len(series); i++ {
points := series[i].GetPointlist()
index := len(points) - 1
// Find out the last point != nil
for j := index; j >= 0; j-- {
if len(points[j]) >= 2 && points[j][1] != nil {
index = j
break
}
}
if index < s.metadata.lastAvailablePointOffset {
return 0, fmt.Errorf("index is smaller than the lastAvailablePointOffset")
}
index -= s.metadata.lastAvailablePointOffset
if len(points) == 0 || len(points[index]) < 2 || points[index][1] == nil {
if !s.metadata.useFiller {
return 0, fmt.Errorf("no Datadog metrics returned for the given time window")
}
return s.metadata.fillValue, nil
}
// Return the last point from the series
results[i] = *points[index][1]
}
switch s.metadata.queryAggegrator {
case avgString:
return AvgFloatFromSlice(results), nil
default:
// Aggregate Results - default Max value:
return MaxFloatFromSlice(results), nil
}
}
// GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler
func (s *datadogScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: s.metadata.metricName,
},
Target: GetMetricTargetMili(s.metadata.vType, s.metadata.queryValue),
}
metricSpec := v2.MetricSpec{
External: externalMetric, Type: externalMetricType,
}
return []v2.MetricSpec{metricSpec}
}
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
func (s *datadogScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
num, err := s.getQueryResult(ctx)
if err != nil {
s.logger.Error(err, "error getting metrics from Datadog")
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error getting metrics from Datadog: %w", err)
}
metric := GenerateMetricInMili(metricName, num)
return []external_metrics.ExternalMetricValue{metric}, num > s.metadata.activationQueryValue, nil
}
// Find the largest value in a slice of floats
func MaxFloatFromSlice(results []float64) float64 {
max := results[0]
for _, result := range results {
if result > max {
max = result
}
}
return max
}
// Find the average value in a slice of floats
func AvgFloatFromSlice(results []float64) float64 {
total := 0.0
for _, result := range results {
total += result
}
return total / float64(len(results))
}