Skip to content

Commit 265a6df

Browse files
iliapologithub-actions
andauthored
fix: cloudfront request metric is using an invalid statistic (#1771)
The `Requests` metric that CloudFront emits should use the `Sum` statistic, not `Average`. > See **Requests** in https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/programming-cloudwatch-metrics.html#cloudfront-metrics-distribution-values In addition, change the period of the CloudFront metrics in the overview dashboard to 1 day (from 5 minutes): - We usually like to talk about "requests per day" to asses usage, over "requests per 5 minutes". - It allows for easier trend detection when looking at a large window. ### Before <img width="1189" height="150" alt="Screenshot 2025-07-30 at 9 36 52 PM" src="https://github.com/user-attachments/assets/2db44fb7-f3a5-47b3-ac9c-3d57efacbf91" /> ### After <img width="1201" height="134" alt="Screenshot 2025-07-30 at 9 37 18 PM" src="https://github.com/user-attachments/assets/ab806112-6408-416e-a26e-0f704c986218" /> ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent d5c14b7 commit 265a6df

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

src/__tests__/__snapshots__/construct-hub.test.ts.snap

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

src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap

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

src/__tests__/overview-dashboard.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ test('It adds cloud front distribution to the dashboard when present', () => {
133133
{
134134
Ref: 'DistributionCFDistribution882A7313',
135135
},
136-
'","Region","Global",{"region":"us-east-1"}],["AWS/CloudFront","4xxErrorRate","DistributionId","',
136+
'","Region","Global",{"region":"us-east-1","period":86400,"stat":"Sum"}],["AWS/CloudFront","4xxErrorRate","DistributionId","',
137137
{
138138
Ref: 'DistributionCFDistribution882A7313',
139139
},
140-
'","Region","Global",{"region":"us-east-1","yAxis":"right"}],["AWS/CloudFront","5xxErrorRate","DistributionId","',
140+
'","Region","Global",{"region":"us-east-1","period":86400,"yAxis":"right"}],["AWS/CloudFront","5xxErrorRate","DistributionId","',
141141
{
142142
Ref: 'DistributionCFDistribution882A7313',
143143
},
144-
'","Region","Global",{"region":"us-east-1","yAxis":"right"}]],"yAxis":{"left":{"label":"Requests count"},"right":{"label":"Request Percent","min":0,"max":100}}}}]}',
144+
'","Region","Global",{"region":"us-east-1","period":86400,"yAxis":"right"}]],"yAxis":{"left":{"label":"Requests count"},"right":{"label":"Request Percent","min":0,"max":100}}}}]}',
145145
],
146146
],
147147
},

src/overview-dashboard/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IOverviewDashboard } from './api';
1515
import { SQSDLQWidget } from './sqs-dlq-widget';
1616
import { Inventory } from '../backend/inventory';
1717
import { RUNBOOK_URL } from '../runbook-url';
18+
import { Duration } from 'aws-cdk-lib';
1819

1920
/**
2021
* Properties for OverviewDashboard
@@ -127,10 +128,12 @@ export class OverviewDashboard extends Construct implements IOverviewDashboard {
127128
this.dashboard.addWidgets(this.cloudFrontMetricWidget);
128129
}
129130

131+
// see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/programming-cloudwatch-metrics.html#cloudfront-metrics-distribution-values
132+
130133
const totalRequest = new Metric({
131134
metricName: 'Requests',
132135
namespace: 'AWS/CloudFront',
133-
statistic: Statistic.AVERAGE,
136+
statistic: Statistic.SUM,
134137
dimensionsMap: {
135138
DistributionId: distribution.distributionId,
136139
Region: 'Global',
@@ -160,9 +163,9 @@ export class OverviewDashboard extends Construct implements IOverviewDashboard {
160163
region: 'us-east-1', // global metric
161164
});
162165

163-
this.cloudFrontMetricWidget.addLeftMetric(totalRequest);
164-
this.cloudFrontMetricWidget.addRightMetric(errorRate4xx);
165-
this.cloudFrontMetricWidget.addRightMetric(errorRate5xx);
166+
this.cloudFrontMetricWidget.addLeftMetric(totalRequest.with({ period: Duration.days(1) }));
167+
this.cloudFrontMetricWidget.addRightMetric(errorRate4xx.with({ period: Duration.days(1) }));
168+
this.cloudFrontMetricWidget.addRightMetric(errorRate5xx.with({ period: Duration.days(1) }));
166169
}
167170

168171
private addCloudFrontMetricWidget() {

0 commit comments

Comments
 (0)