/
metric-math.test.ts
692 lines (599 loc) · 17 KB
/
metric-math.test.ts
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import { Template } from '@aws-cdk/assertions';
import { Duration, Stack } from '@aws-cdk/core';
import { Alarm, GraphWidget, IWidget, MathExpression, Metric } from '../lib';
const a = new Metric({ namespace: 'Test', metricName: 'ACount' });
const b = new Metric({ namespace: 'Test', metricName: 'BCount', statistic: 'Average' });
const c = new Metric({ namespace: 'Test', metricName: 'CCount' });
const b99 = new Metric({ namespace: 'Test', metricName: 'BCount', statistic: 'p99' });
let stack: Stack;
describe('Metric Math', () => {
beforeEach(() => {
stack = new Stack();
});
test('can not use invalid variable names in MathExpression', () => {
expect(() => {
new MathExpression({
expression: 'HAPPY + JOY',
usingMetrics: {
HAPPY: a,
JOY: b,
},
});
}).toThrow(/Invalid variable names in expression/);
});
test('cannot reuse variable names in nested MathExpressions', () => {
// WHEN
expect(() => {
new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'a + c',
usingMetrics: { a: b, c },
}),
},
});
}).toThrow(/The ID 'a' used for two metrics in the expression: 'BCount' and 'ACount'. Rename one/);
});
test('can not use invalid period in MathExpression', () => {
expect(() => {
new MathExpression({
expression: 'a+b',
usingMetrics: { a, b },
period: Duration.seconds(20),
});
}).toThrow(/'period' must be 1, 5, 10, 30, or a multiple of 60 seconds, received 20/);
});
test('MathExpression optimization: "with" with the same period returns the same object', () => {
const m = new MathExpression({ expression: 'SUM(METRICS())', usingMetrics: {}, period: Duration.minutes(10) });
// Note: object equality, NOT deep equality on purpose
expect(m.with({})).toEqual(m);
expect(m.with({ period: Duration.minutes(10) })).toEqual(m);
expect(m.with({ period: Duration.minutes(5) })).not.toEqual(m);
});
test('math expression referring to unknown expressions produces a warning', () => {
const m = new MathExpression({
expression: 'm1 + m2',
});
expect(m.warnings).toContainEqual(expect.stringContaining("'m1 + m2' references unknown identifiers"));
});
test('metrics insights expression does not produce warning for unknown identifier', () => {
const m = new MathExpression({
expression: "SELECT AVG(CpuUsage) FROM EC2 WHERE Instance = '123456'",
});
expect(m.warnings).toBeUndefined();
});
test('math expression referring to unknown expressions produces a warning, even when nested', () => {
const m = new MathExpression({
expression: 'e1 + 5',
usingMetrics: {
e1: new MathExpression({
expression: 'm1 + m2',
}),
},
});
expect(m.warnings).toContainEqual(expect.stringContaining("'m1 + m2' references unknown identifiers"));
});
describe('in graphs', () => {
test('MathExpressions can be added to a graph', () => {
// GIVEN
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + b',
usingMetrics: { a, b },
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ expression: 'a + b', label: 'a + b' }],
['Test', 'ACount', { visible: false, id: 'a' }],
['Test', 'BCount', { visible: false, id: 'b' }],
]);
});
test('can nest MathExpressions in a graph', () => {
// GIVEN
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'b + c',
usingMetrics: { b, c },
}),
},
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ label: 'a + e', expression: 'a + e' }],
['Test', 'ACount', { visible: false, id: 'a' }],
[{ expression: 'b + c', visible: false, id: 'e' }],
['Test', 'BCount', { visible: false, id: 'b' }],
['Test', 'CCount', { visible: false, id: 'c' }],
]);
});
test('can add the same metric under different ids', () => {
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'b + c',
usingMetrics: { b: a, c },
}),
},
}),
],
});
graphMetricsAre(graph, [
[{ label: 'a + e', expression: 'a + e' }],
['Test', 'ACount', { visible: false, id: 'a' }],
[{ expression: 'b + c', visible: false, id: 'e' }],
['Test', 'ACount', { visible: false, id: 'b' }],
['Test', 'CCount', { visible: false, id: 'c' }],
]);
});
test('passing an empty string as the label of a MathExpressions does not emit a label', () => {
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + e',
label: '',
usingMetrics: {
a,
},
}),
],
});
graphMetricsAre(graph, [
[{ expression: 'a + e' }],
['Test', 'ACount', { visible: false, id: 'a' }],
]);
});
test('can reuse identifiers in MathExpressions if metrics are the same', () => {
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'a + c',
usingMetrics: { a, c },
}),
},
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ label: 'a + e', expression: 'a + e' }],
['Test', 'ACount', { visible: false, id: 'a' }],
[{ expression: 'a + c', visible: false, id: 'e' }],
['Test', 'CCount', { visible: false, id: 'c' }],
]);
});
test('MathExpression and its constituent metrics can both be added to a graph', () => {
const graph = new GraphWidget({
left: [
a,
new MathExpression({
expression: 'a + b',
usingMetrics: { a, b },
}),
],
});
// THEN
graphMetricsAre(graph, [
['Test', 'ACount', { id: 'a' }],
[{ label: 'a + b', expression: 'a + b' }],
['Test', 'BCount', { visible: false, id: 'b' }],
]);
});
test('MathExpression controls period of metrics directly used in it', () => {
// Check that if we add A with { period: 10s } to a mathexpression of period 5m
// then two metric lines are added for A, one at 10s and one at 5m
const graph = new GraphWidget({
left: [
a.with({ period: Duration.seconds(10) }),
new MathExpression({
expression: 'a + b',
usingMetrics: { a: a.with({ period: Duration.seconds(10) }), b },
}),
],
});
// THEN
graphMetricsAre(graph, [
['Test', 'ACount', { period: 10 }],
[{ label: 'a + b', expression: 'a + b' }],
['Test', 'ACount', { visible: false, id: 'a' }],
['Test', 'BCount', { visible: false, id: 'b' }],
]);
});
test('top level period in a MathExpression is respected in its metrics', () => {
const graph = new GraphWidget({
left: [
a,
new MathExpression({
expression: 'a + b',
usingMetrics: { a, b },
period: Duration.minutes(1),
}),
],
});
// THEN
graphMetricsAre(graph, [
['Test', 'ACount'],
[{ label: 'a + b', expression: 'a + b', period: 60 }],
['Test', 'ACount', { visible: false, id: 'a', period: 60 }],
['Test', 'BCount', { visible: false, id: 'b', period: 60 }],
]);
});
test('MathExpression controls period of metrics transitively used in it', () => {
// Same as the previous test, but recursively
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'a + b',
period: Duration.minutes(1),
usingMetrics: { a, b },
}),
},
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ expression: 'a + e', label: 'a + e' }],
['Test', 'ACount', { visible: false, id: 'a' }],
[{ expression: 'a + b', visible: false, id: 'e' }],
['Test', 'BCount', { visible: false, id: 'b' }],
]);
});
test('can use percentiles in expression metrics in graphs', () => {
// GIVEN
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + b99',
usingMetrics: { a, b99 },
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ expression: 'a + b99', label: 'a + b99' }],
['Test', 'ACount', { visible: false, id: 'a' }],
['Test', 'BCount', { visible: false, id: 'b99', stat: 'p99' }],
]);
});
test('can reuse the same metric between left and right axes', () => {
// GIVEN
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + 1',
usingMetrics: { a },
}),
],
right: [
new MathExpression({
expression: 'a + 2',
usingMetrics: { a },
}),
],
});
// THEN
graphMetricsAre(graph, [
[{ label: 'a + 1', expression: 'a + 1' }],
['Test', 'ACount', { visible: false, id: 'a' }],
[{ label: 'a + 2', expression: 'a + 2', yAxis: 'right' }],
]);
});
test('detect name conflicts between left and right axes', () => {
// GIVEN
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'm1 + 1',
usingMetrics: { m1: a },
}),
],
right: [
new MathExpression({
expression: 'm1 + 1',
usingMetrics: { m1: b },
}),
],
});
// THEN
expect(() => {
graphMetricsAre(graph, []);
}).toThrow(/Cannot have two different metrics share the same id \('m1'\)/);
});
});
describe('in alarms', () => {
test('MathExpressions can be used for an alarm', () => {
// GIVEN
new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
expression: 'a + b',
usingMetrics: { a, b },
}),
});
// THEN
alarmMetricsAre([
{
Expression: 'a + b',
Id: 'expr_1',
},
{
Id: 'a',
MetricStat: {
Metric: {
MetricName: 'ACount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
{
Id: 'b',
MetricStat: {
Metric: {
MetricName: 'BCount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
]);
});
test('can nest MathExpressions in an alarm', () => {
// GIVEN
new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'b + c',
usingMetrics: { b, c },
}),
},
}),
});
// THEN
alarmMetricsAre([
{
Expression: 'a + e',
Id: 'expr_1',
},
{
Id: 'a',
MetricStat: {
Metric: {
MetricName: 'ACount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
{
Expression: 'b + c',
Id: 'e',
ReturnData: false,
},
{
Id: 'b',
MetricStat: {
Metric: {
MetricName: 'BCount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
{
Id: 'c',
MetricStat: {
Metric: {
MetricName: 'CCount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
]);
});
test('MathExpression controls period of metrics transitively used in it with alarms', () => {
// GIVEN
new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
expression: 'a + e',
usingMetrics: {
a,
e: new MathExpression({
expression: 'b + c',
usingMetrics: { b, c },
period: Duration.minutes(1),
}),
},
period: Duration.seconds(30),
}),
});
// THEN
alarmMetricsAre([
{
Expression: 'a + e',
Id: 'expr_1',
},
{
Id: 'a',
MetricStat: {
Metric: {
MetricName: 'ACount',
Namespace: 'Test',
},
Period: 30,
Stat: 'Average',
},
ReturnData: false,
},
{
Expression: 'b + c',
Id: 'e',
ReturnData: false,
},
{
Id: 'b',
MetricStat: {
Metric: {
MetricName: 'BCount',
Namespace: 'Test',
},
Period: 30,
Stat: 'Average',
},
ReturnData: false,
},
{
Id: 'c',
MetricStat: {
Metric: {
MetricName: 'CCount',
Namespace: 'Test',
},
Period: 30,
Stat: 'Average',
},
ReturnData: false,
},
]);
});
test('MathExpression without inner metrics emits its own period', () => {
// WHEN
new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
expression: 'INSIGHT_RULE_METRIC("SomeId", UniqueContributors)',
usingMetrics: {},
}),
});
// THEN
alarmMetricsAre([
{
Expression: 'INSIGHT_RULE_METRIC("SomeId", UniqueContributors)',
Id: 'expr_1',
Period: 300,
},
]);
});
test('annotation for a mathexpression alarm is calculated based upon constituent metrics', () => {
// GIVEN
const alarm = new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
period: Duration.minutes(10),
expression: 'a + b',
usingMetrics: { a, b: b.with({ period: Duration.minutes(20) }) }, // This is overridden
}),
});
// WHEN
const alarmLabel = alarm.toAnnotation().label;
// THEN
expect(alarmLabel).toEqual('a + b >= 1 for 1 datapoints within 10 minutes');
});
test('can use percentiles in expression metrics in alarms', () => {
// GIVEN
new Alarm(stack, 'Alarm', {
threshold: 1,
evaluationPeriods: 1,
metric: new MathExpression({
expression: 'a + b99',
usingMetrics: { a, b99 },
}),
});
// THEN
alarmMetricsAre([
{
Expression: 'a + b99',
Id: 'expr_1',
},
{
Id: 'a',
MetricStat: {
Metric: {
MetricName: 'ACount',
Namespace: 'Test',
},
Period: 300,
Stat: 'Average',
},
ReturnData: false,
},
{
Id: 'b99',
MetricStat: {
Metric: {
MetricName: 'BCount',
Namespace: 'Test',
},
Period: 300,
Stat: 'p99',
},
ReturnData: false,
},
]);
});
});
});
function graphMetricsAre(w: IWidget, metrics: any[]) {
expect(stack.resolve(w.toJson())).toEqual([{
type: 'metric',
width: 6,
height: 6,
properties:
{
view: 'timeSeries',
region: { Ref: 'AWS::Region' },
metrics,
yAxis: {},
},
}]);
}
function alarmMetricsAre(metrics: any[]) {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Metrics: metrics,
});
}