Skip to content

Commit

Permalink
fix: apply account/region to more metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
echeung-amzn committed Jul 12, 2024
1 parent 7b87395 commit 8e391ea
Show file tree
Hide file tree
Showing 30 changed files with 259 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ The order of precedence of the region/account values is:
1. The facade's `metricFactoryDefaults` props.
1. The region/account that the stack is deployed to.

Note that certain metrics are based on [math expressions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html) and cannot be alarmed upon in a cross-account cross-Region context, and you will see an error at synthesis time.
Note that while this allows for cross-account cross-Region dashboarding, cross-Region alarming is not supported by CloudWatch.

### Monitoring scopes

Expand Down
24 changes: 24 additions & 0 deletions lib/common/metric/MetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ export class MetricFactory {
avgLabel,
avgMetric.color,
avgMetric.period,
this.getRegion(avgMetric),
this.getAccount(avgMetric),
);
}
return avgMetric;
Expand All @@ -370,6 +372,8 @@ export class MetricFactory {
perSecondLabel,
metric.color,
metric.period,
this.getRegion(metric),
this.getAccount(metric),
);
case RateComputationMethod.PER_MINUTE:
return this.createMetricMath(
Expand All @@ -378,6 +382,8 @@ export class MetricFactory {
`${labelPrefix}/m${labelAppendix}`,
metric.color,
metric.period,
this.getRegion(metric),
this.getAccount(metric),
);
case RateComputationMethod.PER_HOUR:
return this.createMetricMath(
Expand All @@ -386,6 +392,8 @@ export class MetricFactory {
`${labelPrefix}/h${labelAppendix}`,
metric.color,
metric.period,
this.getRegion(metric),
this.getAccount(metric),
);
case RateComputationMethod.PER_DAY:
return this.createMetricMath(
Expand All @@ -394,6 +402,8 @@ export class MetricFactory {
`${labelPrefix}/d${labelAppendix}`,
metric.color,
metric.period,
this.getRegion(metric),
this.getAccount(metric),
);
}
}
Expand Down Expand Up @@ -452,4 +462,18 @@ export class MetricFactory {

return copy;
}

private getAccount(metric: MetricWithAlarmSupport): string | undefined {
if (metric instanceof MathExpression) {
return metric.searchAccount;
}
return metric.account;
}

private getRegion(metric: MetricWithAlarmSupport): string | undefined {
if (metric instanceof MathExpression) {
return metric.searchRegion;
}
return metric.region;
}
}
6 changes: 2 additions & 4 deletions lib/monitoring/aws-apigateway/ApiGatewayMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
}

metric4XXErrorRate() {
const metric = this.metric4XXErrorCount();
return this.metricFactory.toRate(
metric,
this.metric4XXErrorCount(),
this.rateComputationMethod,
false,
"errors",
Expand All @@ -139,9 +138,8 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
}

metric5XXFaultRate() {
const metric = this.metric5XXFaultCount();
return this.metricFactory.toRate(
metric,
this.metric5XXFaultCount(),
this.rateComputationMethod,
false,
"faults",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
}

metric4xxRate() {
const metric = this.metric4xxCount();
return this.metricFactory.toRate(
metric,
this.metric4xxCount(),
this.rateComputationMethod,
false,
"errors",
Expand All @@ -132,9 +131,8 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
}

metric5xxRate() {
const metric = this.metric5xxCount();
return this.metricFactory.toRate(
metric,
this.metric5xxCount(),
this.rateComputationMethod,
false,
"faults",
Expand Down
6 changes: 2 additions & 4 deletions lib/monitoring/aws-appsync/AppSyncMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
}

metric4XXErrorRate() {
const metric = this.metric4XXErrorCount();
return this.metricFactory.toRate(
metric,
this.metric4XXErrorCount(),
this.rateComputationMethod,
false,
"errors",
Expand All @@ -162,9 +161,8 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
}

metric5XXFaultRate() {
const metric = this.metric5XXFaultCount();
return this.metricFactory.toRate(
metric,
this.metric5XXFaultCount(),
this.rateComputationMethod,
false,
"faults",
Expand Down
1 change: 0 additions & 1 deletion lib/monitoring/aws-billing/BillingMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class BillingMetricFactory {

metricTotalCostInUsd(): MetricWithAlarmSupport {
// not using metric factory because we customize everything

return new Metric({
namespace: BillingNamespace,
metricName: BillingMetric,
Expand Down
6 changes: 2 additions & 4 deletions lib/monitoring/aws-docdb/DocumentDbMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ export class DocumentDbMetricFactory extends BaseMetricFactory<DocumentDbMetricF
}

metricReadLatencyInMillis(latencyType: LatencyType) {
const label = "Read " + getLatencyTypeLabel(latencyType);
return this.metric(
"ReadLatency",
getLatencyTypeStatistic(latencyType),
label,
`Read ${getLatencyTypeLabel(latencyType)}`,
);
}

metricWriteLatencyInMillis(latencyType: LatencyType) {
const label = "Write " + getLatencyTypeLabel(latencyType);
return this.metric(
"WriteLatency",
getLatencyTypeStatistic(latencyType),
label,
`Write ${getLatencyTypeLabel(latencyType)}`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class DynamoTableGlobalSecondaryIndexMetricFactory extends BaseMetricFact
consumed_rcu_sum: this.table.metricConsumedReadCapacityUnits({
statistic: MetricStatistic.SUM,
dimensionsMap: this.dimensionsMap,
region: this.region,
account: this.account,
}),
},
"Consumed",
Expand All @@ -81,6 +83,8 @@ export class DynamoTableGlobalSecondaryIndexMetricFactory extends BaseMetricFact
consumed_wcu_sum: this.table.metricConsumedWriteCapacityUnits({
statistic: MetricStatistic.SUM,
dimensionsMap: this.dimensionsMap,
region: this.region,
account: this.account,
}),
},
"Consumed",
Expand Down
20 changes: 20 additions & 0 deletions lib/monitoring/aws-dynamo/DynamoTableMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class DynamoTableMetricFactory extends BaseMetricFactory<DynamoTableMetri
}),
},
ConsumedLabel,
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -86,6 +90,10 @@ export class DynamoTableMetricFactory extends BaseMetricFactory<DynamoTableMetri
}),
},
ConsumedLabel,
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -97,6 +105,10 @@ export class DynamoTableMetricFactory extends BaseMetricFactory<DynamoTableMetri
provisioned_read_cap: this.metricProvisionedReadCapacityUnits(),
},
"Utilization",
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -108,6 +120,10 @@ export class DynamoTableMetricFactory extends BaseMetricFactory<DynamoTableMetri
provisioned_write_cap: this.metricProvisionedWriteCapacityUnits(),
},
"Utilization",
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -121,6 +137,10 @@ export class DynamoTableMetricFactory extends BaseMetricFactory<DynamoTableMetri
},
MetricStatistic.AVERAGE,
DynamoDbNamespace,
undefined,
undefined,
this.region,
this.account,
);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/monitoring/aws-ecs-patterns/BaseServiceMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ export class BaseServiceMetricFactory extends BaseMetricFactory<BaseServiceMetri
}

metricEphemeralStorageUsageInPercent() {
const total = this.metricEphemeralStorageReserved();
const used = this.metricEphemeralStorageUtilized();

return this.metricFactory.createMetricMath(
"100 * (used/total)",
{ used, total },
{
used: this.metricEphemeralStorageUtilized(),
total: this.metricEphemeralStorageReserved(),
},
"Ephemeral Storage Usage",
);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/monitoring/aws-kinesis/KinesisFirehoseMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export class KinesisFirehoseMetricFactory extends BaseMetricFactory<KinesisFireh
bytes_max: this.metricBytesPerSecondLimit(),
},
"Incoming Bytes / Limit",
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -162,6 +166,10 @@ export class KinesisFirehoseMetricFactory extends BaseMetricFactory<KinesisFireh
records_max: this.metricRecordsPerSecondLimit(),
},
"Incoming Records / Limit",
undefined,
undefined,
this.region,
this.account,
);
}

Expand All @@ -173,6 +181,10 @@ export class KinesisFirehoseMetricFactory extends BaseMetricFactory<KinesisFireh
requests_max: this.metricPutRequestsPerSecondLimit(),
},
"Incoming PutRequests / Limit",
undefined,
undefined,
this.region,
this.account,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export class LambdaFunctionEnhancedMetricFactory extends BaseMetricFactory<Lambd
return this.metricFactory.adaptMetric(
this.lambdaFunction.metricDuration({
statistic: MetricStatistic.SUM,
region: this.region,
account: this.account,
}),
);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/monitoring/aws-lambda/LambdaFunctionMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class LambdaFunctionMetricFactory extends BaseMetricFactory<LambdaFunctio
return this.metricFactory.adaptMetric(
this.lambdaFunction.metricInvocations({
label: "Invocations",
region: this.region,
account: this.account,
}),
);
}
Expand All @@ -92,9 +94,8 @@ export class LambdaFunctionMetricFactory extends BaseMetricFactory<LambdaFunctio
}

metricThrottlesRate() {
const metric = this.metricThrottlesCount();
return this.metricFactory.toRate(
metric,
this.metricThrottlesCount(),
this.rateComputationMethod,
false,
"throttles",
Expand All @@ -112,9 +113,8 @@ export class LambdaFunctionMetricFactory extends BaseMetricFactory<LambdaFunctio
}

metricFaultRate() {
const metric = this.metricFaultCount();
return this.metricFactory.toRate(
metric,
this.metricFaultCount(),
this.rateComputationMethod,
false,
"faults",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export class ApplicationLoadBalancerMetricFactory
}

metricHealthyTaskInPercent() {
const healthyTaskCount = this.metricHealthyTaskCount();
const unhealthyTaskCount = this.metricUnhealthyTaskCount();
return this.metricFactory.createMetricMath(
"(healthyTaskCount / (healthyTaskCount + unhealthyTaskCount)) * 100",
{ healthyTaskCount, unhealthyTaskCount },
{
healthyTaskCount: this.metricHealthyTaskCount(),
unhealthyTaskCount: this.metricUnhealthyTaskCount(),
},
"Healthy Task Percent (avg: ${AVG})",
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export class NetworkLoadBalancerMetricFactory
}

metricHealthyTaskInPercent() {
const healthyTaskCount = this.metricHealthyTaskCount();
const unhealthyTaskCount = this.metricUnhealthyTaskCount();
return this.metricFactory.createMetricMath(
"(healthyTaskCount / (healthyTaskCount + unhealthyTaskCount)) * 100",
{ healthyTaskCount, unhealthyTaskCount },
{
healthyTaskCount: this.metricHealthyTaskCount(),
unhealthyTaskCount: this.metricUnhealthyTaskCount(),
},
"Healthy Task Percent (avg: ${AVG})",
);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/monitoring/aws-rds/RdsClusterMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ export class RdsClusterMetricFactory extends BaseMetricFactory<RdsClusterMetricF
}

metricDiskSpaceUsageInPercent() {
const used = this.metricUsedStorageInBytes();
const free = this.metricFreeStorageInBytes();
return this.metricFactory.createMetricMath(
"100 * (used/(used+free))",
{ used, free },
{
used: this.metricUsedStorageInBytes(),
free: this.metricFreeStorageInBytes(),
},
"Disk Usage",
);
}
Expand Down Expand Up @@ -118,6 +119,7 @@ export class RdsClusterMetricFactory extends BaseMetricFactory<RdsClusterMetricF
"Cluster is not of type `ServerlessCluster`. Metric is not applicable",
);
}

return this.metric(
"ServerlessDatabaseCapacity",
MetricStatistic.AVERAGE,
Expand Down
Loading

0 comments on commit 8e391ea

Please sign in to comment.