Skip to content

Commit

Permalink
Autoscaling: Add metrics for E2 target tracking scaling (#6314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Lukianov authored May 13, 2023
1 parent f7b2478 commit 523225d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
32 changes: 32 additions & 0 deletions moto/autoscaling/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,38 @@ def enable_metrics_collection(self) -> str:
{% if policy.target_tracking_config["CustomizedMetricSpecification"].get("Unit") %}
<Unit>{{ policy.target_tracking_config["CustomizedMetricSpecification"].get("Unit") }}</Unit>
{% endif %}
{% if policy.target_tracking_config["CustomizedMetricSpecification"].get("Metrics") %}
<Metrics>
{% for metric in policy.target_tracking_config["CustomizedMetricSpecification"].get("Metrics", []) %}
<member>
<Id>{{ metric.get("Id") }}</Id>
{% if metric.get("MetricStat") is none %}
<Expression>{{ metric.get("Expression") }}</Expression>
{% endif %}
{% if metric.get("Expression") is none %}
<MetricStat>
<Metric>
<Namespace>{{ metric.get("MetricStat", {}).get("Metric", {}).get("Namespace") }}</Namespace>
<MetricName>{{ metric.get("MetricStat", {}).get("Metric", {}).get("MetricName") }}</MetricName>
<Dimensions>
{% for dim in metric.get("MetricStat", {}).get("Metric", {}).get("Dimensions", []) %}
<member>
<Name>{{ dim.get("Name") }}</Name>
<Value>{{ dim.get("Value") }}</Value>
</member>
{% endfor %}
</Dimensions>
</Metric>
<Stat>{{ metric.get("MetricStat", {}).get("Stat") }}</Stat>
<Unit>{{ metric.get("MetricStat", {}).get("Unit") }}</Unit>
</MetricStat>
{% endif %}
<Label>{{ metric.get("Label") }}</Label>
<ReturnData>{{ 'true' if metric.get("ReturnData") is none else metric.get("ReturnData") }}</ReturnData>
</member>
{% endfor %}
</Metrics>
{% endif %}
</CustomizedMetricSpecification>
{% endif %}
<TargetValue>{{ policy.target_tracking_config.get("TargetValue") }}</TargetValue>
Expand Down
54 changes: 54 additions & 0 deletions tests/test_autoscaling/test_autoscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,30 @@ def test_create_autoscaling_policy_with_policytype__targettrackingscaling():
"PredefinedMetricType": "ASGAverageNetworkIn",
},
"TargetValue": 1000000.0,
"CustomizedMetricSpecification": {
"Metrics": [
{
"Label": "Get ASGAverageCPUUtilization",
"Id": "cpu",
"MetricStat": {
"Metric": {
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Dimensions": [
{"Name": "AutoScalingGroupName", "Value": asg_name}
],
},
"Stat": "Average",
},
"ReturnData": False,
},
{
"Label": "Calculate square cpu",
"Id": "load",
"Expression": "cpu^2",
},
],
},
},
)

Expand All @@ -786,6 +810,36 @@ def test_create_autoscaling_policy_with_policytype__targettrackingscaling():
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageNetworkIn",
},
"CustomizedMetricSpecification": {
"MetricName": "None",
"Namespace": "None",
"Dimensions": [],
"Statistic": "None",
"Metrics": [
{
"Label": "Get ASGAverageCPUUtilization",
"Id": "cpu",
"MetricStat": {
"Metric": {
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Dimensions": [
{"Name": "AutoScalingGroupName", "Value": asg_name}
],
},
"Stat": "Average",
"Unit": "None",
},
"ReturnData": False,
},
{
"Label": "Calculate square cpu",
"Id": "load",
"Expression": "cpu^2",
"ReturnData": True,
},
],
},
"TargetValue": 1000000.0,
}
)
Expand Down

0 comments on commit 523225d

Please sign in to comment.