Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit b58e3b1

Browse files
committed
add cloudfront metrics
1 parent b24ba50 commit b58e3b1

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@serverless/aws-sdk-extra",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "The AWS SDK that includes with a handful of extra convenience methods.",
55
"main": "src/index.js",
66
"author": "Serverless, Inc.",

src/getMetrics.js

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,145 @@ resourceHandlers.aws_lambda.transforms = (cwMetric, metric) => {
610610
}
611611

612612
return metric;
613-
}
613+
}
614+
615+
/**
616+
* AWS Cloudfront
617+
*/
618+
619+
resourceHandlers.aws_cloudfront = {}
620+
621+
/**
622+
* AWS Cloudfront Cloudwatch queries
623+
* @param {*} period
624+
* @param {*} distributionId
625+
*/
626+
resourceHandlers.aws_cloudfront.queries = (period, { distributionId, stage }) => {
627+
628+
if (!period || !distributionId) {
629+
throw new Error(`Missing required params`)
630+
}
631+
632+
return [
633+
{
634+
Id: 'distribution_requests',
635+
ReturnData: true,
636+
MetricStat: {
637+
Metric: {
638+
MetricName: 'Requests',
639+
Namespace: 'AWS/CloudFront',
640+
Dimensions: [
641+
{
642+
Name: 'Region',
643+
Value: 'Global', // Cloudfront is global by default, so this property is always "Global", adding it in just to be sure.
644+
},
645+
{
646+
Name: 'DistributionId',
647+
Value: distributionId,
648+
},
649+
],
650+
},
651+
Period: period,
652+
Stat: 'Sum',
653+
},
654+
},
655+
{
656+
Id: 'distribution_error_rate',
657+
ReturnData: true,
658+
MetricStat: {
659+
Metric: {
660+
MetricName: 'TotalErrorRate',
661+
Namespace: 'AWS/CloudFront',
662+
Dimensions: [
663+
{
664+
Name: 'Region',
665+
Value: 'Global', // Cloudfront is global by default, so this property is always "Global", adding it in just to be sure.
666+
},
667+
{
668+
Name: 'DistributionId',
669+
Value: distributionId,
670+
},
671+
],
672+
},
673+
Period: period,
674+
Stat: 'Average',
675+
Unit: 'Percent',
676+
},
677+
},
678+
{
679+
Id: 'distribution_bytes_downloaded',
680+
ReturnData: true,
681+
MetricStat: {
682+
Metric: {
683+
MetricName: 'BytesDownloaded',
684+
Namespace: 'AWS/CloudFront',
685+
Dimensions: [
686+
{
687+
Name: 'Region',
688+
Value: 'Global', // Cloudfront is global by default, so this property is always "Global", adding it in just to be sure.
689+
},
690+
{
691+
Name: 'DistributionId',
692+
Value: distributionId,
693+
},
694+
],
695+
},
696+
Period: period,
697+
Stat: 'Sum',
698+
},
699+
}
700+
// There is more data available if "additional metrics" is enabled... Something to consider for later...
701+
]
702+
}
703+
704+
/**
705+
* AWS Cloudfront Cloudwatch transforms
706+
* @param {*} period
707+
* @param {*} apiId
708+
*/
709+
resourceHandlers.aws_cloudfront.transforms = (cwMetric, metric) => {
710+
711+
if (!cwMetric || !metric) {
712+
throw new Error(`Missing required params`)
713+
}
714+
715+
// Total Requests
716+
if (cwMetric.Id === 'distribution_requests') {
717+
metric.title = 'Requests';
718+
metric.description =
719+
'The total number of viewer requests received by CloudFront, for all HTTP methods and for both HTTP and HTTPS requests. This is not a count of unique users, but of the total requests of assets from Cloudfront. A single webpage may have several assets on it (e.g. images, CSS, JS, etc.).';
720+
metric.yDataSets[0].color = '#000000';
721+
// Get sum
722+
metric.stat = metric.yDataSets[0].yData.reduce((previous, current) => current + previous);
723+
}
724+
725+
// Total Error Rate
726+
if (cwMetric.Id === 'distribution_error_rate') {
727+
metric.title = 'Request Error Rate';
728+
metric.description =
729+
'The percentage of all viewer requests for which the response’s HTTP status code is 4xx or 5xx.';
730+
metric.statColor = '#FE5850';
731+
metric.yDataSets[0].color = '#FE5850';
732+
// Get sum
733+
metric.stat = metric.yDataSets[0].yData.reduce((previous, current) => current + previous);
734+
metric.statText = '%';
735+
}
736+
737+
// Bytes Downloaded in Kilobytes
738+
if (cwMetric.Id === 'distribution_bytes_downloaded') {
739+
metric.title = 'Data Downloaded';
740+
metric.description = 'The total number of kilobytes downloaded by viewers for GET, HEAD, and OPTIONS requests.';
741+
metric.statColor = '#000000';
742+
metric.yDataSets[0].color = '#000000';
743+
// Convert to kilobytes
744+
metric.yDataSets[0].yData = metric.yDataSets[0].yData.map((val) => {
745+
const kb = val / Math.pow(1024, 1);
746+
return Math.round(kb * 100) / 100;
747+
});
748+
// Get sum of bytes
749+
metric.statText = 'kb';
750+
metric.stat = metric.yDataSets[0].yData.reduce((previous, current) => current + previous);
751+
}
752+
753+
return metric
754+
}

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class Extras {
6565
* type: 'aws_lambda',
6666
* functionName: 'myLambdaFunction',
6767
* }
68+
*
69+
* // AWS Cloudfront
70+
* {
71+
* type: 'aws_cloudfront',
72+
* distributionId: 'ja9fa9j1',
73+
* }
6874
*
6975
*/
7076
getMetrics(params) { return getMetrics(this.config, params) }

0 commit comments

Comments
 (0)