Skip to content

Commit 9a57eba

Browse files
author
Sebastian Martinka
committed
issue-34: add default parameter for empty Cloudwatch metrics
1 parent d43667c commit 9a57eba

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ ENV/
9797
# mkdocs documentation
9898
/site
9999

100+
# eclipse project settings
101+
.project
102+
.pydevproject
103+
100104
# mypy
101105
.mypy_cache/
102106

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@ metric_sources:
309309
alias: cpu_usage
310310
```
311311

312+
Cloudwatch doesn't return values for some resource metrics if the resource was not used e.g. TargetResponseTime for AWS/ApplicationELB.
313+
You can add `default: <value>` so that the value is replaced with `<value>`.
314+
```
315+
metric_sources:
316+
cloudwatch:
317+
- namespace: AWS/ApplicationELB
318+
metric_name: TargetResponseTime
319+
dimensions:
320+
- name: TargetGroup
321+
value: targetgroup/ac-re-Targe-13P06UAEX2W9Y/f9002d01ab9053e0
322+
period: 60
323+
statistics:
324+
- name: Average
325+
alias: alb_response_time
326+
default: 0
327+
```
328+
312329
One thing to watch out for is how you define the `statistics` field above.
313330
The `name` part has to match exactly with a statistic used by CloudWatch,
314331
and the `alias` part is an arbitrary name you use to reference this metric when

lambda/ecsautoscale/metric_sources/cloudwatch.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def get_data(metric_name: str = "MemoryUtilization",
2626
dimensions: List[dict] = None,
2727
statistics: List[dict] = None,
2828
namespace: str = "AWS/ECS",
29-
period: int = 300) -> dict:
29+
period: int = 300,
30+
defaultValue: int = None) -> dict:
3031
"""
3132
Retreive metrics from AWS CloudWatch.
3233
@@ -72,8 +73,11 @@ def get_data(metric_name: str = "MemoryUtilization",
7273
)
7374
datapoints = res["Datapoints"]
7475
if not datapoints:
75-
raise CloudWatchError(
76-
namespace, metric_name, dimensions_, period, statistics_)
76+
if defaultValue:
77+
datapoints.append({statistics_[0] : defaultValue})
78+
else:
79+
raise CloudWatchError(
80+
namespace, metric_name, dimensions_, period, statistics_)
7781

7882
log_messages = ["Retreived the following statistics from CloudWatch:"]
7983
for stat in statistics:

0 commit comments

Comments
 (0)