-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecc_metrics.go
486 lines (467 loc) · 17.4 KB
/
ecc_metrics.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package window
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/elasticache"
)
type (
ECCNodeStats struct {
Cluster *ElasticCacheCluster
Node *elasticache.CacheNode
// host
CPUUtilization float64
FreeableMemory int64
NetworkBytesInPerSecond int64
NetworkBytesOutPerSecond int64
SwapUsage int64
Memcached *ECCMemcachedStats
Redis *ECCRedisStats
}
ECCMemcachedStats struct {
CurrItems int64 // (Count) A count of the number of items currently stored in the cache. Count
NewItems int64 // (Count) The number of new items the cache has stored. This is derived from the memcached total_items statistic by recording the change in total_items across a period of time. Count
BytesUsedForCacheItems int64 // (Bytes) The number of bytes used to store cache items. Bytes
CurrConnections int64 // (Count) A count of the number of connections connected to the cache at an instant in time. Count
NewConnections int64 // (Count) The number of new connections the cache has received. This is derived from the memcached total_connections statistic by recording the change in total_connections across a period of time. This will always be at least 1, due to a connection reserved for a ElastiCache. Count
CmdFlushPerSecond float64 // (Count) The number of flush commands the cache has received. Count
CmdGetPerSecond float64 // (Count) The number of get commands the cache has received. Count
CmdSetPerSecond float64 // (Count) The number of set commands the cache has received. Count
EvictionsPerSecond float64 // (Count) The number of non-expired items the cache evicted to allow space for new writes. Count
GetHitsPerSecond float64 // (Count) The number of get requests the cache has received where the key requested was found. Count
GetMissesPerSecond float64 // (Count) The number of get requests the cache has received where the key requested was not found. Count
ReclaimedPerSecond float64 // (Count) The number of expired items the cache evicted to allow space for new writes. Count
EvictedUnfetchedPerSecond float64 // (Count) The number of valid items evicted from the least recently used cache (LRU) which were never touched after being set. Count
ExpiredUnfetchedPerSecond float64 // (Count) The number of expired items reclaimed from the LRU which were never touched after being set. Count
}
ECCRedisStats struct {
// redis
CacheHitsPerSecond float64 // (Count) The number of successful key lookups. Count
CacheMissesPerSecond float64 // (Count) The number of unsuccessful key lookups. Count
CurrConnections int64 // (Count) The number of client connections, excluding connections from read replicas. Count
NewConnections int64 // (Count) The total number of connections that have been accepted by the server during this period. Count
CurrItems int64 // (Count) The number of items in the cache. This is derived from the Redis keyspace statistic, summing all of the keys in the entire keyspace. Count
BytesUsedForCache int64 // (Bytes) The total number of bytes allocated by Redis. Bytes
EvictionsPerSecond float64 // (Count) The number of keys that have been evicted due to the maxmemory limit. Count
GetTypeCmdsPerSecond float64 // (Count) The total number of get types of commands. This is derived from the Redis commandstats statistic by summing all of the get types of commands (get, mget, hget, etc.) Count
SetTypeCmdsPerSecond float64 // (Count) The total number of set types of commands. This is derived from the Redis commandstats statistic by summing all of the set types of commands (set, hset, etc.) Count
ReclaimedPerSecond float64 // (Count) The total number of key expiration events. Count
ReplicationBytesPerSecond int64 // (Bytes) For primaries with attached replicas, ReplicationBytes reports the number of bytes that the primary is sending to all of its replicas. This metric is representative of the write load on the replication group. For replicas and standalone primaries, ReplicationBytes is always 0. Bytes
ReplicationLag struct {
Min, Max, Avg time.Duration
} // (Seconds) This metric is only applicable for a cache node running as a read replica. It represents how far behind, in seconds, the replica is in applying changes from the primary cache cluster. Seconds
SaveInProgress bool // (Count) This binary metric returns 1 whenever a background save (forked or forkless) is in progress, and 0 otherwise. A background save process is typically used during snapshots and syncs. These operations can cause degraded performance. Using the SaveInProgress metric, you can diagnose whether or not degraded performance was caused by a background save process. Count
}
eccmetric struct {
name *string
engine string
statistics []*string
unit *string
processor func(*ECCNodeStats, *cloudwatch.Datapoint)
}
)
const (
ECCEngineMemcached = "memcached"
ECCEngineRedis = "redis"
)
var (
ECCMetrics = []*eccmetric{
// host
{
name: aws.String("CPUUtilization"),
statistics: []*string{aws.String("Average")},
unit: aws.String("Percent"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.CPUUtilization = *point.Average
}
},
},
{
name: aws.String("FreeableMemory"),
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.FreeableMemory = int64(*point.Average)
}
},
},
{
name: aws.String("NetworkBytesIn"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.NetworkBytesInPerSecond = int64(*point.Sum / PeriodInMinutes / 60)
}
},
},
{
name: aws.String("NetworkBytesOut"),
statistics: []*string{aws.String("Sum")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.NetworkBytesOutPerSecond = int64(*point.Sum / PeriodInMinutes / 60)
}
},
},
{
name: aws.String("SwapUsage"),
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.SwapUsage = int64(*point.Average)
}
},
},
// memcached
{
name: aws.String("CurrItems"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Memcached.CurrItems = int64(*point.Average)
}
},
},
{
name: aws.String("NewItems"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Memcached.NewItems = int64(*point.Average)
}
},
},
{
name: aws.String("BytesUsedForCacheItems"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Memcached.BytesUsedForCacheItems = int64(*point.Average)
}
},
},
{
name: aws.String("CurrConnections"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Memcached.CurrConnections = int64(*point.Average)
}
},
},
{
name: aws.String("NewConnections"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Memcached.NewConnections = int64(*point.Average)
}
},
},
{
name: aws.String("CmdFlush"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.CmdFlushPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("CmdGet"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.CmdGetPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("CmdSet"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.CmdSetPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("Evictions"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.EvictionsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("GetHits"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.GetHitsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("GetMisses"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.GetMissesPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("Reclaimed"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.ReclaimedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("EvictedUnfetched"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.EvictedUnfetchedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("ExpiredUnfetched"),
engine: ECCEngineMemcached,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Memcached.ExpiredUnfetchedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
// redis
{
name: aws.String("CacheHits"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.CacheHitsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("CacheMisses"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.CacheMissesPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("CurrConnections"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Redis.CurrConnections = int64(*point.Average)
}
},
},
{
name: aws.String("NewConnections"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Redis.NewConnections = int64(*point.Average)
}
},
},
{
name: aws.String("CurrItems"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Average")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Redis.CurrItems = int64(*point.Average)
}
},
},
{
name: aws.String("BytesUsedForCache"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Average")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Average != nil {
stats.Redis.BytesUsedForCache = int64(*point.Average)
}
},
},
{
name: aws.String("Evictions"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.EvictionsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("GetTypeCmds"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.GetTypeCmdsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("SetTypeCmds"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.SetTypeCmdsPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("Reclaimed"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.ReclaimedPerSecond = *point.Sum / PeriodInMinutes / 60
}
},
},
{
name: aws.String("ReplicationBytes"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Bytes"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil {
stats.Redis.ReplicationBytesPerSecond = int64(*point.Sum / PeriodInMinutes / 60)
}
},
},
{
name: aws.String("ReplicationLag"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Minimum"), aws.String("Maximum"), aws.String("Average")},
unit: aws.String("Seconds"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Minimum != nil {
stats.Redis.ReplicationLag.Min = roundTime(time.Duration(*point.Minimum * float64(time.Second)))
}
if point.Maximum != nil {
stats.Redis.ReplicationLag.Max = roundTime(time.Duration(*point.Maximum * float64(time.Second)))
}
if point.Average != nil {
stats.Redis.ReplicationLag.Avg = roundTime(time.Duration(*point.Average * float64(time.Second)))
}
},
},
{
name: aws.String("SaveInProgress"),
engine: ECCEngineRedis,
statistics: []*string{aws.String("Sum")},
unit: aws.String("Count"),
processor: func(stats *ECCNodeStats, point *cloudwatch.Datapoint) {
if point.Sum != nil && *point.Sum > 0 {
stats.Redis.SaveInProgress = true
}
},
},
}
)
func NewECCNodeStats(ecc *ElasticCacheCluster, node *elasticache.CacheNode) *ECCNodeStats {
stats := &ECCNodeStats{
Cluster: ecc,
Node: node,
}
switch ecc.Engine {
case ECCEngineMemcached:
stats.Memcached = &ECCMemcachedStats{}
case ECCEngineRedis:
stats.Redis = &ECCRedisStats{}
}
return stats
}
func (m *eccmetric) RunFor(stats *ECCNodeStats) error {
if len(m.engine) > 0 && m.engine != stats.Cluster.Engine {
return nil
}
resp, err := CloudWatchClient.GetMetricStatistics(&cloudwatch.GetMetricStatisticsInput{
StartTime: aws.Time(time.Now().Add(-PeriodInMinutes * time.Minute)),
EndTime: aws.Time(time.Now()),
Period: aws.Int64(PeriodInMinutes * 60),
Namespace: aws.String("AWS/ElastiCache"),
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("CacheClusterId"),
Value: aws.String(stats.Cluster.CacheClusterId),
},
{
Name: aws.String("CacheNodeId"),
Value: stats.Node.CacheNodeId,
},
},
MetricName: m.name,
Statistics: m.statistics,
Unit: m.unit, // fuck this in teh face
})
if err == nil && len(resp.Datapoints) > 0 {
m.processor(stats, resp.Datapoints[0])
}
return err
}