Skip to content

Commit cec84db

Browse files
feat(apigateway): allow using IRestApi instead of RestApiBase (#405)
As of 2.90.0 aws-cdk now supports restApiName in the IRestApi interface. By using this interface it becomes possible for developers to monitor APIGateway without the need to reference the actual RestAPI. Related commit: aws/aws-cdk@3bf1361 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 67c3145 commit cec84db

File tree

4 files changed

+1362
-12
lines changed

4 files changed

+1362
-12
lines changed

API.md

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/monitoring/aws-apigateway/ApiGatewayMetricFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RestApiBase } from "aws-cdk-lib/aws-apigateway";
1+
import { IRestApi } from "aws-cdk-lib/aws-apigateway";
22
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";
33

44
import {
@@ -14,9 +14,9 @@ const ApiGatewayNamespace = "AWS/ApiGateway";
1414

1515
export interface ApiGatewayMetricFactoryProps {
1616
/**
17-
* API to monitor (cannot use IRestApi, since it does not provide API name)
17+
* API to monitor
1818
*/
19-
readonly api: RestApiBase;
19+
readonly api: IRestApi;
2020
/**
2121
* @default - prod
2222
*/

test/monitoring/aws-apigateway/ApiGatewayMonitoring.test.ts

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Duration, Stack } from "aws-cdk-lib";
22
import { Template } from "aws-cdk-lib/assertions";
3-
import { RestApi } from "aws-cdk-lib/aws-apigateway";
3+
import { IRestApi, RestApi } from "aws-cdk-lib/aws-apigateway";
44

55
import { AlarmWithAnnotation, ApiGatewayMonitoring } from "../../../lib";
66
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
@@ -173,3 +173,157 @@ test("snapshot test: all alarms", () => {
173173
expect(numAlarmsCreated).toStrictEqual(22);
174174
expect(Template.fromStack(stack)).toMatchSnapshot();
175175
});
176+
177+
test("snapshot test: all alarms using interface", () => {
178+
const stack = new Stack();
179+
const api: IRestApi = RestApi.fromRestApiId(
180+
stack,
181+
"ThisShouldBeTheApiName",
182+
"thisIsNotUsed"
183+
);
184+
185+
const scope = new TestMonitoringScope(stack, "Scope");
186+
187+
let numAlarmsCreated = 0;
188+
189+
const monitoring = new ApiGatewayMonitoring(scope, {
190+
api,
191+
humanReadableName: "Dummy API Gateway for testing",
192+
alarmFriendlyName: "DummyApi",
193+
add5XXFaultRateAlarm: {
194+
Warning: {
195+
maxErrorRate: 1,
196+
datapointsToAlarm: 10,
197+
},
198+
},
199+
add5XXFaultCountAlarm: {
200+
Warning: {
201+
maxErrorCount: 2,
202+
datapointsToAlarm: 20,
203+
},
204+
},
205+
add4XXErrorRateAlarm: {
206+
Warning: {
207+
maxErrorRate: 0.01,
208+
datapointsToAlarm: 11,
209+
},
210+
},
211+
add4XXErrorCountAlarm: {
212+
Warning: {
213+
maxErrorCount: 0.02,
214+
datapointsToAlarm: 22,
215+
},
216+
},
217+
addLatencyP50Alarm: {
218+
Warning: {
219+
maxLatency: Duration.millis(150),
220+
datapointsToAlarm: 150,
221+
},
222+
},
223+
addLatencyP70Alarm: {
224+
Warning: {
225+
maxLatency: Duration.millis(170),
226+
datapointsToAlarm: 170,
227+
},
228+
},
229+
addLatencyP90Alarm: {
230+
Warning: {
231+
maxLatency: Duration.millis(190),
232+
datapointsToAlarm: 190,
233+
},
234+
},
235+
addLatencyP95Alarm: {
236+
Warning: {
237+
maxLatency: Duration.millis(195),
238+
datapointsToAlarm: 195,
239+
},
240+
},
241+
addLatencyP99Alarm: {
242+
Warning: {
243+
maxLatency: Duration.millis(199),
244+
datapointsToAlarm: 199,
245+
},
246+
},
247+
addLatencyP999Alarm: {
248+
Warning: {
249+
maxLatency: Duration.millis(1999),
250+
datapointsToAlarm: 1999,
251+
},
252+
},
253+
addLatencyP9999Alarm: {
254+
Warning: {
255+
maxLatency: Duration.millis(19999),
256+
datapointsToAlarm: 19999,
257+
},
258+
},
259+
addLatencyP100Alarm: {
260+
Warning: {
261+
maxLatency: Duration.millis(1100),
262+
datapointsToAlarm: 1100,
263+
},
264+
},
265+
addLatencyTM50Alarm: {
266+
Warning: {
267+
maxLatency: Duration.millis(250),
268+
datapointsToAlarm: 250,
269+
},
270+
},
271+
addLatencyTM70Alarm: {
272+
Warning: {
273+
maxLatency: Duration.millis(270),
274+
datapointsToAlarm: 270,
275+
},
276+
},
277+
addLatencyTM90Alarm: {
278+
Warning: {
279+
maxLatency: Duration.millis(290),
280+
datapointsToAlarm: 290,
281+
},
282+
},
283+
addLatencyTM95Alarm: {
284+
Warning: {
285+
maxLatency: Duration.millis(295),
286+
datapointsToAlarm: 295,
287+
},
288+
},
289+
addLatencyTM99Alarm: {
290+
Warning: {
291+
maxLatency: Duration.millis(299),
292+
datapointsToAlarm: 299,
293+
},
294+
},
295+
addLatencyTM999Alarm: {
296+
Warning: {
297+
maxLatency: Duration.millis(2999),
298+
datapointsToAlarm: 2999,
299+
},
300+
},
301+
addLatencyTM9999Alarm: {
302+
Warning: {
303+
maxLatency: Duration.millis(29999),
304+
datapointsToAlarm: 29999,
305+
},
306+
},
307+
addLatencyAverageAlarm: {
308+
Warning: {
309+
maxLatency: Duration.millis(20),
310+
datapointsToAlarm: 20,
311+
},
312+
},
313+
addLowTpsAlarm: {
314+
Warning: { minTps: 1 },
315+
},
316+
addHighTpsAlarm: {
317+
Warning: { maxTps: 10 },
318+
},
319+
useCreatedAlarms: {
320+
consume(alarms: AlarmWithAnnotation[]) {
321+
numAlarmsCreated = alarms.length;
322+
},
323+
},
324+
});
325+
326+
addMonitoringDashboardsToStack(stack, monitoring);
327+
expect(numAlarmsCreated).toStrictEqual(22);
328+
expect(Template.fromStack(stack)).toMatchSnapshot();
329+
});

0 commit comments

Comments
 (0)