forked from mackerelio/mackerel-agent-plugins
/
aws-dynamo-db.go
466 lines (426 loc) · 15.2 KB
/
aws-dynamo-db.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package mpawsdynamodb
import (
"flag"
"log"
"strings"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
)
const (
namespace = "AWS/DynamoDB"
metricsTypeAverage = "Average"
metricsTypeSum = "Sum"
metricsTypeMaximum = "Maximum"
metricsTypeMinimum = "Minimum"
metricsTypeSampleCount = "SampleCount"
)
// has 1 CloudWatch MetricName and corresponding N Mackerel Metrics
type metricsGroup struct {
CloudWatchName string
Metrics []metric
}
type metric struct {
MackerelName string
Type string
FillZero bool
}
// DynamoDBPlugin mackerel plugin for aws kinesis
type DynamoDBPlugin struct {
TableName string
Prefix string
AccessKeyID string
SecretAccessKey string
Region string
CloudWatch *cloudwatch.CloudWatch
}
// MetricKeyPrefix interface for PluginWithPrefix
func (p DynamoDBPlugin) MetricKeyPrefix() string {
return p.Prefix
}
// prepare creates CloudWatch instance
func (p *DynamoDBPlugin) prepare() error {
sess, err := session.NewSession()
if err != nil {
return err
}
config := aws.NewConfig()
if p.AccessKeyID != "" && p.SecretAccessKey != "" {
config = config.WithCredentials(credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, ""))
}
if p.Region != "" {
config = config.WithRegion(p.Region)
}
p.CloudWatch = cloudwatch.New(sess, config)
return nil
}
func transformAndAppendDatapoint(stats map[string]interface{}, dp *cloudwatch.Datapoint, dataType string, label string, fillZero bool) map[string]interface{} {
if dp != nil {
switch dataType {
case metricsTypeAverage:
stats[label] = *dp.Average
case metricsTypeSum:
stats[label] = *dp.Sum
case metricsTypeMaximum:
stats[label] = *dp.Maximum
case metricsTypeMinimum:
stats[label] = *dp.Minimum
case metricsTypeSampleCount:
stats[label] = *dp.SampleCount
}
} else if fillZero {
stats[label] = 0.0
}
return stats
}
// fetch metrics which takes "Operation" dimensions querying both ListMetrics and GetMetricsStatistics
func fetchOperationWildcardMetrics(cw cloudwatchiface.CloudWatchAPI, mg metricsGroup, baseDimensions []*cloudwatch.Dimension) (map[string]interface{}, error) {
// get available dimensions
dimensionFilters := make([]*cloudwatch.DimensionFilter, len(baseDimensions))
for i, dimension := range baseDimensions {
dimensionFilters[i] = &cloudwatch.DimensionFilter{
Name: dimension.Name,
Value: dimension.Value,
}
}
input := &cloudwatch.ListMetricsInput{
Dimensions: dimensionFilters,
Namespace: aws.String(namespace),
MetricName: aws.String(mg.CloudWatchName),
}
// ListMetrics can retrieve up to 500 metrics, but DynamoDB Operations are apparently less than 500
res, err := cw.ListMetrics(input)
if err != nil {
return nil, err
}
stats := make(map[string]interface{})
// get datapoints with retrieved dimensions
for _, cwMetric := range res.Metrics {
dimensions := cwMetric.Dimensions
// extract operation name
var operation *string
for _, d := range dimensions {
if *d.Name == "Operation" {
operation = d.Value
break
}
}
if operation == nil {
log.Printf("Unexpected dimension, skip: %s", dimensions)
continue
}
dp, err := getLastPointFromCloudWatch(cw, mg, dimensions)
if err != nil {
return nil, nil
}
if dp != nil {
for _, met := range mg.Metrics {
label := strings.Replace(met.MackerelName, "#", *operation, 1)
stats = transformAndAppendDatapoint(stats, dp, met.Type, label, met.FillZero)
}
}
}
return stats, nil
}
// getLastPoint fetches a CloudWatch metric and parse
func getLastPointFromCloudWatch(cw cloudwatchiface.CloudWatchAPI, metric metricsGroup, dimensions []*cloudwatch.Dimension) (*cloudwatch.Datapoint, error) {
now := time.Now()
statsInput := make([]*string, len(metric.Metrics))
for i, typ := range metric.Metrics {
statsInput[i] = aws.String(typ.Type)
}
input := &cloudwatch.GetMetricStatisticsInput{
// 8 min, since some metrics are aggregated over 5 min
StartTime: aws.Time(now.Add(time.Duration(480) * time.Second * -1)),
EndTime: aws.Time(now),
MetricName: aws.String(metric.CloudWatchName),
Period: aws.Int64(60),
Statistics: statsInput,
Namespace: aws.String(namespace),
Dimensions: dimensions,
}
response, err := cw.GetMetricStatistics(input)
if err != nil {
return nil, err
}
datapoints := response.Datapoints
if len(datapoints) == 0 {
return nil, nil
}
latest := new(time.Time)
var latestDp *cloudwatch.Datapoint
for _, dp := range datapoints {
if dp.Timestamp.Before(*latest) {
continue
}
latest = dp.Timestamp
latestDp = dp
}
return latestDp, nil
}
var defaultMetricsGroup = []metricsGroup{
{CloudWatchName: "ConditionalCheckFailedRequests", Metrics: []metric{
{MackerelName: "ConditionalCheckFailedRequests", Type: metricsTypeSum},
}},
{CloudWatchName: "ConsumedReadCapacityUnits", Metrics: []metric{
{MackerelName: "ConsumedReadCapacityUnitsSum", Type: metricsTypeSum},
{MackerelName: "ConsumedReadCapacityUnitsAverage", Type: metricsTypeAverage},
}},
{CloudWatchName: "ConsumedWriteCapacityUnits", Metrics: []metric{
{MackerelName: "ConsumedWriteCapacityUnitsSum", Type: metricsTypeSum},
{MackerelName: "ConsumedWriteCapacityUnitsAverage", Type: metricsTypeAverage},
}},
{CloudWatchName: "ProvisionedReadCapacityUnits", Metrics: []metric{
{MackerelName: "ProvisionedReadCapacityUnits", Type: metricsTypeMinimum},
}},
{CloudWatchName: "ProvisionedWriteCapacityUnits", Metrics: []metric{
{MackerelName: "ProvisionedWriteCapacityUnits", Type: metricsTypeMinimum},
}},
{CloudWatchName: "SystemErrors", Metrics: []metric{
{MackerelName: "SystemErrors", Type: metricsTypeSum},
}},
{CloudWatchName: "UserErrors", Metrics: []metric{
{MackerelName: "UserErrors", Type: metricsTypeSum},
}},
{CloudWatchName: "ReadThrottleEvents", Metrics: []metric{
{MackerelName: "ReadThrottleEvents", Type: metricsTypeSum, FillZero: true},
}},
{CloudWatchName: "WriteThrottleEvents", Metrics: []metric{
{MackerelName: "WriteThrottleEvents", Type: metricsTypeSum, FillZero: true},
}},
{CloudWatchName: "TimeToLiveDeletedItemCount", Metrics: []metric{
{MackerelName: "TimeToLiveDeletedItemCount", Type: metricsTypeSum},
}},
}
var operationalMetricsGroup = []metricsGroup{
{CloudWatchName: "SuccessfulRequestLatency", Metrics: []metric{
{MackerelName: "SuccessfulRequests.#", Type: metricsTypeSampleCount},
{MackerelName: "SuccessfulRequestLatency.#.Minimum", Type: metricsTypeMinimum},
{MackerelName: "SuccessfulRequestLatency.#.Maximum", Type: metricsTypeMaximum},
{MackerelName: "SuccessfulRequestLatency.#.Average", Type: metricsTypeAverage},
}},
{CloudWatchName: "ThrottledRequests", Metrics: []metric{
{MackerelName: "ThrottledRequests.#", Type: metricsTypeSampleCount},
}},
{CloudWatchName: "SystemErrors", Metrics: []metric{
{MackerelName: "SystemErrors.#", Type: metricsTypeSampleCount},
}},
{CloudWatchName: "UserErrors", Metrics: []metric{
{MackerelName: "UserErrors.#", Type: metricsTypeSampleCount},
}},
{CloudWatchName: "ReturnedItemCount", Metrics: []metric{
{MackerelName: "ReturnedItemCount.#", Type: metricsTypeAverage},
}},
}
// FetchMetrics fetch the metrics
func (p DynamoDBPlugin) FetchMetrics() (map[string]interface{}, error) {
stats := make(map[string]interface{})
tableDimensions := []*cloudwatch.Dimension{{
Name: aws.String("TableName"),
Value: aws.String(p.TableName),
}}
var mu sync.Mutex
var eg errgroup.Group
for _, met := range defaultMetricsGroup {
met := met
eg.Go(func() error {
dp, err := getLastPointFromCloudWatch(p.CloudWatch, met, tableDimensions)
if err != nil {
return err
}
for _, m := range met.Metrics {
mu.Lock()
stats = transformAndAppendDatapoint(stats, dp, m.Type, m.MackerelName, m.FillZero)
mu.Unlock()
}
return nil
})
}
for _, met := range operationalMetricsGroup {
met := met
eg.Go(func() error {
operationalStats, err := fetchOperationWildcardMetrics(p.CloudWatch, met, tableDimensions)
if err != nil {
return err
}
if operationalStats != nil {
for name, s := range operationalStats {
mu.Lock()
stats[name] = s
mu.Unlock()
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return transformMetrics(stats), nil
}
// TransformMetrics converts some of datapoints to post differences of two metrics
func transformMetrics(stats map[string]interface{}) map[string]interface{} {
// Although stats are interface{}, those values from cloudwatch.Datapoint are guaranteed to be numerical
if consumedReadCapacitySum, ok := stats["ConsumedReadCapacityUnitsSum"].(float64); ok {
stats["ConsumedReadCapacityUnitsNormalized"] = consumedReadCapacitySum / 60.0
}
if consumedWriteCapacitySum, ok := stats["ConsumedWriteCapacityUnitsSum"].(float64); ok {
stats["ConsumedWriteCapacityUnitsNormalized"] = consumedWriteCapacitySum / 60.0
}
return stats
}
// GraphDefinition of DynamoDBPlugin
func (p DynamoDBPlugin) GraphDefinition() map[string]mp.Graphs {
labelPrefix := strings.Title(p.Prefix)
labelPrefix = strings.Replace(labelPrefix, "-", " ", -1)
labelPrefix = strings.Replace(labelPrefix, "Dynamodb", "DynamoDB", -1)
var graphdef = map[string]mp.Graphs{
"ReadCapacity": {
Label: (labelPrefix + " Read Capacity Units"),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "ProvisionedReadCapacityUnits", Label: "Provisioned"},
{Name: "ConsumedReadCapacityUnitsNormalized", Label: "Consumed"},
{Name: "ConsumedReadCapacityUnitsAverage", Label: "Consumed (Average per request)"},
},
},
"WriteCapacity": {
Label: (labelPrefix + " Write Capacity Units"),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "ProvisionedWriteCapacityUnits", Label: "Provisioned"},
{Name: "ConsumedWriteCapacityUnitsNormalized", Label: "Consumed"},
{Name: "ConsumedWriteCapacityUnitsAverage", Label: "Consumed (Average per request)"},
},
},
"TimeToLiveDeletedItemCount": {
Label: (labelPrefix + " TimeToLiveDeletedItemCount"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "TimeToLiveDeletedItemCount", Label: "Count"},
},
},
"ThrottledEvents": {
Label: (labelPrefix + " Throttle Events"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "ReadThrottleEvents", Label: "Read"},
{Name: "WriteThrottleEvents", Label: "Write"},
},
},
"ConditionalCheckFailedRequests": {
Label: (labelPrefix + " ConditionalCheckFailedRequests"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "ConditionalCheckFailedRequests", Label: "Counts"},
},
},
"ThrottledRequests": {
Label: (labelPrefix + " ThrottledRequests"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "PutItem", Label: "PutItem", Stacked: true, AbsoluteName: true},
{Name: "DeleteItem", Label: "DeleteItem", Stacked: true, AbsoluteName: true},
{Name: "UpdateItem", Label: "UpdateItem", Stacked: true, AbsoluteName: true},
{Name: "GetItem", Label: "GetItem", Stacked: true, AbsoluteName: true},
{Name: "BatchGetItem", Label: "BatchGetItem", Stacked: true, AbsoluteName: true},
{Name: "Scan", Label: "Scan", Stacked: true, AbsoluteName: true},
{Name: "Query", Label: "Query", Stacked: true, AbsoluteName: true},
{Name: "BatchWriteItem", Label: "BatchWriteItem", Stacked: true, AbsoluteName: true},
},
},
"SystemErrors": {
Label: (labelPrefix + " SystemErrors"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "PutItem", Label: "PutItem", Stacked: true, AbsoluteName: true},
{Name: "DeleteItem", Label: "DeleteItem", Stacked: true, AbsoluteName: true},
{Name: "UpdateItem", Label: "UpdateItem", Stacked: true, AbsoluteName: true},
{Name: "GetItem", Label: "GetItem", Stacked: true, AbsoluteName: true},
{Name: "BatchGetItem", Label: "BatchGetItem", Stacked: true, AbsoluteName: true},
{Name: "Scan", Label: "Scan", Stacked: true, AbsoluteName: true},
{Name: "Query", Label: "Query", Stacked: true, AbsoluteName: true},
{Name: "BatchWriteItem", Label: "BatchWriteItem", Stacked: true, AbsoluteName: true},
},
},
"UserErrors": {
Label: (labelPrefix + " UserErrors"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "PutItem", Label: "PutItem", Stacked: true, AbsoluteName: true},
{Name: "DeleteItem", Label: "DeleteItem", Stacked: true, AbsoluteName: true},
{Name: "UpdateItem", Label: "UpdateItem", Stacked: true, AbsoluteName: true},
{Name: "GetItem", Label: "GetItem", Stacked: true, AbsoluteName: true},
{Name: "BatchGetItem", Label: "BatchGetItem", Stacked: true, AbsoluteName: true},
{Name: "Scan", Label: "Scan", Stacked: true, AbsoluteName: true},
{Name: "Query", Label: "Query", Stacked: true, AbsoluteName: true},
{Name: "BatchWriteItem", Label: "BatchWriteItem", Stacked: true, AbsoluteName: true},
},
},
"ReturnedItemCount": {
Label: (labelPrefix + " ReturnedItemCount"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "PutItem", Label: "PutItem", AbsoluteName: true},
{Name: "DeleteItem", Label: "DeleteItem", AbsoluteName: true},
{Name: "UpdateItem", Label: "UpdateItem", AbsoluteName: true},
{Name: "GetItem", Label: "GetItem", AbsoluteName: true},
{Name: "BatchGetItem", Label: "BatchGetItem", AbsoluteName: true},
{Name: "Scan", Label: "Scan", AbsoluteName: true},
{Name: "Query", Label: "Query", AbsoluteName: true},
{Name: "BatchWriteItem", Label: "BatchWriteItem", AbsoluteName: true},
},
},
"SuccessfulRequests": {
Label: (labelPrefix + " SuccessfulRequests"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "PutItem", Label: "PutItem", AbsoluteName: true},
{Name: "DeleteItem", Label: "DeleteItem", AbsoluteName: true},
{Name: "UpdateItem", Label: "UpdateItem", AbsoluteName: true},
{Name: "GetItem", Label: "GetItem", AbsoluteName: true},
{Name: "BatchGetItem", Label: "BatchGetItem", AbsoluteName: true},
{Name: "Scan", Label: "Scan", AbsoluteName: true},
{Name: "Query", Label: "Query", AbsoluteName: true},
{Name: "BatchWriteItem", Label: "BatchWriteItem", AbsoluteName: true},
},
},
"SuccessfulRequestLatency.#": {
Label: (labelPrefix + " SuccessfulRequestLatency"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "Minimum", Label: "Min"},
{Name: "Maximum", Label: "Max"},
{Name: "Average", Label: "Average"},
},
},
}
return graphdef
}
// Do the plugin
func Do() {
optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
optRegion := flag.String("region", "", "AWS Region")
optTableName := flag.String("table-name", "", "DynamoDB Table Name")
optPrefix := flag.String("metric-key-prefix", "dynamodb", "Metric key prefix")
flag.Parse()
var plugin DynamoDBPlugin
plugin.AccessKeyID = *optAccessKeyID
plugin.SecretAccessKey = *optSecretAccessKey
plugin.Region = *optRegion
plugin.TableName = *optTableName
plugin.Prefix = *optPrefix
err := plugin.prepare()
if err != nil {
log.Fatalln(err)
}
helper := mp.NewMackerelPlugin(plugin)
helper.Run()
}