Skip to content

Commit f37991c

Browse files
authored
chore(cloudwatch): make examples compile (#17158)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3259523 commit f37991c

File tree

2 files changed

+100
-45
lines changed

2 files changed

+100
-45
lines changed

packages/@aws-cdk/aws-cloudwatch/README.md

+83-45
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ For example, `lambda.Function` objects have the `fn.metricErrors()` method, whic
2525
represents the amount of errors reported by that Lambda function:
2626

2727
```ts
28+
declare const fn: lambda.Function;
29+
2830
const errors = fn.metricErrors();
2931
```
3032

@@ -37,7 +39,7 @@ For example:
3739

3840
```ts
3941
const hostedZone = new route53.HostedZone(this, 'MyHostedZone', { zoneName: "example.org" });
40-
const metric = new Metric({
42+
const metric = new cloudwatch.Metric({
4143
namespace: 'AWS/Route53',
4244
metricName: 'DNSQueries',
4345
dimensionsMap: {
@@ -52,7 +54,7 @@ If you want to reference a metric that is not yet exposed by an existing constru
5254
you can instantiate a `Metric` object to represent it. For example:
5355

5456
```ts
55-
const metric = new Metric({
57+
const metric = new cloudwatch.Metric({
5658
namespace: 'MyNamespace',
5759
metricName: 'MyMetric',
5860
dimensionsMap: {
@@ -67,11 +69,13 @@ Math expressions are supported by instantiating the `MathExpression` class.
6769
For example, a math expression that sums two other metrics looks like this:
6870

6971
```ts
70-
const allProblems = new MathExpression({
71-
expression: "errors + faults",
72+
declare const fn: lambda.Function;
73+
74+
const allProblems = new cloudwatch.MathExpression({
75+
expression: "errors + throttles",
7276
usingMetrics: {
73-
errors: myConstruct.metricErrors(),
74-
faults: myConstruct.metricFaults(),
77+
errors: fn.metricErrors(),
78+
faults: fn.metricThrottles(),
7579
}
7680
});
7781
```
@@ -80,11 +84,14 @@ You can use `MathExpression` objects like any other metric, including using
8084
them in other math expressions:
8185

8286
```ts
83-
const problemPercentage = new MathExpression({
87+
declare const fn: lambda.Function;
88+
declare const allProblems: cloudwatch.MathExpression;
89+
90+
const problemPercentage = new cloudwatch.MathExpression({
8491
expression: "(problems / invocations) * 100",
8592
usingMetrics: {
8693
problems: allProblems,
87-
invocations: myConstruct.metricInvocations()
94+
invocations: fn.metricInvocations()
8895
}
8996
});
9097
```
@@ -96,7 +103,7 @@ search expression returns all CPUUtilization metrics that it finds, with the
96103
graph showing the Average statistic with an aggregation period of 5 minutes:
97104

98105
```ts
99-
const cpuUtilization = new MathExpression({
106+
const cpuUtilization = new cloudwatch.MathExpression({
100107
expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)"
101108
});
102109
```
@@ -120,6 +127,8 @@ the function or the period), you can do so by passing additional parameters
120127
to the metric function call:
121128

122129
```ts
130+
declare const fn: lambda.Function;
131+
123132
const minuteErrorRate = fn.metricErrors({
124133
statistic: 'avg',
125134
period: Duration.minutes(1),
@@ -153,9 +162,10 @@ useful when embedding them in graphs, see below).
153162
Alarms can be created on metrics in one of two ways. Either create an `Alarm`
154163
object, passing the `Metric` object to set the alarm on:
155164

156-
157165
```ts
158-
new Alarm(this, 'Alarm', {
166+
declare const fn: lambda.Function;
167+
168+
new cloudwatch.Alarm(this, 'Alarm', {
159169
metric: fn.metricErrors(),
160170
threshold: 100,
161171
evaluationPeriods: 2,
@@ -165,6 +175,8 @@ new Alarm(this, 'Alarm', {
165175
Alternatively, you can call `metric.createAlarm()`:
166176

167177
```ts
178+
declare const fn: lambda.Function;
179+
168180
fn.metricErrors().createAlarm(this, 'Alarm', {
169181
threshold: 100,
170182
evaluationPeriods: 2,
@@ -188,33 +200,36 @@ an SNS topic when an alarm breaches, do the following:
188200

189201
```ts
190202
import * as cw_actions from '@aws-cdk/aws-cloudwatch-actions';
203+
declare const alarm: cloudwatch.Alarm;
191204

192-
// ...
193-
const topic = new sns.Topic(stack, 'Topic');
194-
const alarm = new cloudwatch.Alarm(stack, 'Alarm', { /* ... */ });
195-
205+
const topic = new sns.Topic(this, 'Topic');
196206
alarm.addAlarmAction(new cw_actions.SnsAction(topic));
197207
```
198208

199209
### Composite Alarms
200210

201-
[Composite Alarms](https://aws.amazon.com/about-aws/whats-new/2020/03/amazon-cloudwatch-now-allows-you-to-combine-multiple-alarms/)
211+
[Composite Alarms](https://aws.amazon.com/about-aws/whats-new/2020/03/amazon-cloudwatch-now-allows-you-to-combine-multiple-alarms/)
202212
can be created from existing Alarm resources.
203213

204214
```ts
205-
const alarmRule = AlarmRule.anyOf(
206-
AlarmRule.allOf(
207-
AlarmRule.anyOf(
215+
declare const alarm1: cloudwatch.Alarm;
216+
declare const alarm2: cloudwatch.Alarm;
217+
declare const alarm3: cloudwatch.Alarm;
218+
declare const alarm4: cloudwatch.Alarm;
219+
220+
const alarmRule = cloudwatch.AlarmRule.anyOf(
221+
cloudwatch.AlarmRule.allOf(
222+
cloudwatch.AlarmRule.anyOf(
208223
alarm1,
209-
AlarmRule.fromAlarm(alarm2, AlarmState.OK),
224+
cloudwatch.AlarmRule.fromAlarm(alarm2, cloudwatch.AlarmState.OK),
210225
alarm3,
211226
),
212-
AlarmRule.not(AlarmRule.fromAlarm(alarm4, AlarmState.INSUFFICIENT_DATA)),
227+
cloudwatch.AlarmRule.not(cloudwatch.AlarmRule.fromAlarm(alarm4, cloudwatch.AlarmState.INSUFFICIENT_DATA)),
213228
),
214-
AlarmRule.fromBoolean(false),
229+
cloudwatch.AlarmRule.fromBoolean(false),
215230
);
216231

217-
new CompositeAlarm(this, 'MyAwesomeCompositeAlarm', {
232+
new cloudwatch.CompositeAlarm(this, 'MyAwesomeCompositeAlarm', {
218233
alarmRule,
219234
});
220235
```
@@ -260,15 +275,19 @@ A graph widget can display any number of metrics on either the `left` or
260275
`right` vertical axis:
261276

262277
```ts
263-
dashboard.addWidgets(new GraphWidget({
278+
declare const dashboard: cloudwatch.Dashboard;
279+
declare const executionCountMetric: cloudwatch.Metric;
280+
declare const errorCountMetric: cloudwatch.Metric;
281+
282+
dashboard.addWidgets(new cloudwatch.GraphWidget({
264283
title: "Executions vs error rate",
265284

266285
left: [executionCountMetric],
267286

268287
right: [errorCountMetric.with({
269288
statistic: "average",
270289
label: "Error rate",
271-
color: Color.GREEN
290+
color: cloudwatch.Color.GREEN
272291
})]
273292
}));
274293
```
@@ -278,12 +297,13 @@ Using the methods `addLeftMetric()` and `addRightMetric()` you can add metrics t
278297
Graph widgets can also display annotations attached to the left or the right y-axis.
279298

280299
```ts
281-
dashboard.addWidgets(new GraphWidget({
282-
// ...
300+
declare const dashboard: cloudwatch.Dashboard;
301+
302+
dashboard.addWidgets(new cloudwatch.GraphWidget({
283303
// ...
284304

285305
leftAnnotations: [
286-
{ value: 1800, label: Duration.minutes(30).toHumanString(), color: Color.RED, },
306+
{ value: 1800, label: Duration.minutes(30).toHumanString(), color: cloudwatch.Color.RED, },
287307
{ value: 3600, label: '1 hour', color: '#2ca02c', }
288308
],
289309
}));
@@ -292,19 +312,21 @@ dashboard.addWidgets(new GraphWidget({
292312
The graph legend can be adjusted from the default position at bottom of the widget.
293313

294314
```ts
295-
dashboard.addWidgets(new GraphWidget({
296-
// ...
315+
declare const dashboard: cloudwatch.Dashboard;
316+
317+
dashboard.addWidgets(new cloudwatch.GraphWidget({
297318
// ...
298319

299-
legendPosition: LegendPosition.RIGHT,
320+
legendPosition: cloudwatch.LegendPosition.RIGHT,
300321
}));
301322
```
302323

303324
The graph can publish live data within the last minute that has not been fully aggregated.
304325

305326
```ts
306-
dashboard.addWidgets(new GraphWidget({
307-
// ...
327+
declare const dashboard: cloudwatch.Dashboard;
328+
329+
dashboard.addWidgets(new cloudwatch.GraphWidget({
308330
// ...
309331

310332
liveData: true,
@@ -314,11 +336,12 @@ dashboard.addWidgets(new GraphWidget({
314336
The graph view can be changed from default 'timeSeries' to 'bar' or 'pie'.
315337

316338
```ts
317-
dashboard.addWidgets(new GraphWidget({
318-
// ...
339+
declare const dashboard: cloudwatch.Dashboard;
340+
341+
dashboard.addWidgets(new cloudwatch.GraphWidget({
319342
// ...
320343

321-
view: GraphWidgetView.BAR,
344+
view: cloudwatch.GraphWidgetView.BAR,
322345
}));
323346
```
324347

@@ -327,7 +350,10 @@ dashboard.addWidgets(new GraphWidget({
327350
An alarm widget shows the graph and the alarm line of a single alarm:
328351

329352
```ts
330-
dashboard.addWidgets(new AlarmWidget({
353+
declare const dashboard: cloudwatch.Dashboard;
354+
declare const errorAlarm: cloudwatch.Alarm;
355+
356+
dashboard.addWidgets(new cloudwatch.AlarmWidget({
331357
title: "Errors",
332358
alarm: errorAlarm,
333359
}));
@@ -339,17 +365,22 @@ A single-value widget shows the latest value of a set of metrics (as opposed
339365
to a graph of the value over time):
340366

341367
```ts
342-
dashboard.addWidgets(new SingleValueWidget({
368+
declare const dashboard: cloudwatch.Dashboard;
369+
declare const visitorCount: cloudwatch.Metric;
370+
declare const purchaseCount: cloudwatch.Metric;
371+
372+
dashboard.addWidgets(new cloudwatch.SingleValueWidget({
343373
metrics: [visitorCount, purchaseCount],
344374
}));
345375
```
346376

347377
Show as many digits as can fit, before rounding.
348378

349379
```ts
350-
dashboard.addWidgets(new SingleValueWidget({
351-
// ..
352-
// ..
380+
declare const dashboard: cloudwatch.Dashboard;
381+
382+
dashboard.addWidgets(new cloudwatch.SingleValueWidget({
383+
metrics: [ /* ... */ ],
353384

354385
fullPrecision: true,
355386
}));
@@ -361,7 +392,9 @@ A text widget shows an arbitrary piece of MarkDown. Use this to add explanations
361392
to your dashboard:
362393

363394
```ts
364-
dashboard.addWidgets(new TextWidget({
395+
declare const dashboard: cloudwatch.Dashboard;
396+
397+
dashboard.addWidgets(new cloudwatch.TextWidget({
365398
markdown: '# Key Performance Indicators'
366399
}));
367400
```
@@ -372,8 +405,11 @@ An alarm status widget displays instantly the status of any type of alarms and g
372405
ability to aggregate one or more alarms together in a small surface.
373406

374407
```ts
408+
declare const dashboard: cloudwatch.Dashboard;
409+
declare const errorAlarm: cloudwatch.Alarm;
410+
375411
dashboard.addWidgets(
376-
new AlarmStatusWidget({
412+
new cloudwatch.AlarmStatusWidget({
377413
alarms: [errorAlarm],
378414
})
379415
);
@@ -384,9 +420,11 @@ dashboard.addWidgets(
384420
A `LogQueryWidget` shows the results of a query from Logs Insights:
385421

386422
```ts
387-
dashboard.addWidgets(new LogQueryWidget({
423+
declare const dashboard: cloudwatch.Dashboard;
424+
425+
dashboard.addWidgets(new cloudwatch.LogQueryWidget({
388426
logGroupNames: ['my-log-group'],
389-
view: LogQueryVisualizationType.TABLE,
427+
view: cloudwatch.LogQueryVisualizationType.TABLE,
390428
// The lines will be automatically combined using '\n|'.
391429
queryLines: [
392430
'fields @message',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Fixture with packages imported, but nothing else
2+
import { Construct } from 'constructs';
3+
import { Stack, Duration } from '@aws-cdk/core';
4+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
5+
import * as route53 from '@aws-cdk/aws-route53';
6+
import * as sns from '@aws-cdk/aws-sns';
7+
import * as lambda from '@aws-cdk/aws-lambda';
8+
9+
class Fixture extends Stack {
10+
constructor(scope: Construct, id: string) {
11+
super(scope, id);
12+
13+
/// here
14+
}
15+
}
16+
17+

0 commit comments

Comments
 (0)