Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cloudwatch): compute extended-statistic percentiles instead of aliasing to Average (#1405)

MGSousa7985e55c3b8e
diff --git a/ministack/services/cloudwatch.py b/ministack/services/cloudwatch.py
index 41d8766d..1b5be623 100644
--- a/ministack/services/cloudwatch.py
+++ b/ministack/services/cloudwatch.py
@@ -210,11 +210,44 @@ def _calc_stats(values):
}
-def _stat_value(stats, stat_name):
- """Extract a single statistic from a stats dict. Handles pNN percentiles as Average."""
+def _percentile(values, pct):
+ """Extended-statistic percentile of values (0-100), computes the way
+ that CloudWatch documents it: sort ascending and linearly interpolate
+ between the two nearest ranks.
+
+ Matches e.g. numpy's default ("linear") interpolation method."""
+ if not values:
+ return 0
+ ordered = sorted(values)
+ n = len(ordered)
+ if n == 1:
+ return ordered[0]
+ rank = (pct / 100.0) * (n - 1)
+ lower = int(rank)
+ upper = min(lower + 1, n - 1)
+ frac = rank - lower
+ return ordered[lower] + frac * (ordered[upper] - ordered[lower])
+
+
+def _is_percentile_stat(stat_name):
+ return bool(stat_name) and stat_name[0] in ("p", "P") and stat_name[1:].replace(".", "", 1).isdigit()
+
+
+def _stat_value(stats, stat_name, values=None):
+ """Extract a single statistic. stat_name is either a basic statistic name
+ already present in stats (Average/Sum/Minimum/Maximum/SampleCount) or an
+ extended-statistic percentile (e.g. "p95", "p99.9"), computed from the raw
+ sample values for this period via _percentile.
+
+ Falls back to Average if values are not available (e.g. callers that only
+ have pre-aggregated stats, not the raw samples) so behavior degrades
+ gracefully rather than raising.
+ """
if stat_name in stats:
return stats[stat_name]
- if stat_name.startswith("p") and stat_name[1:].replace(".", "").isdigit():
+ if _is_percentile_stat(stat_name):
+ if values:
+ return _percentile(values, float(stat_name[1:]))
return stats.get("Average", 0)
return stats.get("Average", 0)
@@ -244,8 +277,12 @@ def _evaluate_alarm(alarm):
if not recent:
return
- stats = _calc_stats([p["Value"] for p in recent])
- val = _stat_value(stats, alarm.get("Statistic", "Average"))
+ recent_values = [p["Value"] for p in recent]
+ stats = _calc_stats(recent_values)
+ # an alarm is configured with either Statistic (basic) or ExtendedStatistic (percentile),
+ # never both thus prefer whichever is set.
+ stat_name = alarm.get("ExtendedStatistic") or alarm.get("Statistic", "Average")
+ val = _stat_value(stats, stat_name, recent_values)
threshold = alarm.get("Threshold", 0)
op = alarm.get("ComparisonOperator", "")
@@ -811,9 +848,10 @@ def _get_metric_data(params, cbor_data, is_cbor, is_json=False):
timestamps = []
values = []
for ts in sorted(buckets):
- stats = _calc_stats(buckets[ts])
+ bucket_values = buckets[ts]
+ stats = _calc_stats(bucket_values)
timestamps.append(_ts_iso(ts))
- values.append(_stat_value(stats, stat_name))
+ values.append(_stat_value(stats, stat_name, bucket_values))
if return_data:
results.append(
diff --git a/tests/test_cloudwatch.py b/tests/test_cloudwatch.py
index 575a11af..5c82a330 100644
--- a/tests/test_cloudwatch.py
+++ b/tests/test_cloudwatch.py
@@ -221,6 +221,51 @@ def test_cloudwatch_get_metric_data_v2(cw):
assert resp["MetricDataResults"][0]["StatusCode"] == "Complete"
assert len(resp["MetricDataResults"][0]["Values"]) >= 1
+def test_cloudwatch_get_metric_data_extended_percentile(cw):
+ """Extended-statistic percentiles (pNN) must be computed from the raw sample
+ values via linear interpolation between the two nearest ranks, not silently
+ aliased to Average regardless of which percentile is requested.
+ Expected values below are hand-computed for the dataset
+ [1, 2, 3, 4, 90, 95, 98, 99] (n=8):
+ p50: rank=3.5 -> 4 + 0.5*(90-4) = 47.0
+ p95: rank=6.65 -> 98 + 0.65*(99-98) = 98.65
+ p5: rank=0.35 -> 1 + 0.35*(2-1) = 1.35
+ """
+ ns = "Test/GMDPercentile"
+ now = time.time()
+ for i, v in enumerate([1, 2, 3, 4, 90, 95, 98, 99]):
+ cw.put_metric_data(Namespace=ns, MetricData=[{
+ "MetricName": "Latency",
+ "Dimensions": [{"Name": "Host", "Value": "h1"}],
+ "Timestamp": now - i,
+ "Value": float(v),
+ }])
+
+ def _value(stat):
+ resp = cw.get_metric_data(
+ MetricDataQueries=[{
+ "Id": "q1",
+ "MetricStat": {
+ "Metric": {
+ "Namespace": ns, "MetricName": "Latency",
+ "Dimensions": [{"Name": "Host", "Value": "h1"}],
+ },
+ "Period": 3600,
+ "Stat": stat,
+ },
+ "ReturnData": True,
+ }],
+ StartTime=now - 3600,
+ EndTime=now + 3600,
+ )
+ return resp["MetricDataResults"][0]["Values"][0]
+
+ assert _value("Average") == pytest.approx(49.0)
+ assert _value("p50") == pytest.approx(47.0)
+ assert _value("p95") == pytest.approx(98.65)
+ assert _value("p5") == pytest.approx(1.35)
+ assert _value("p95") != _value("p5")
+
def test_cloudwatch_get_metric_data_honors_dimensions(cw):
"""GetMetricData resolves by the query's dimension set, like GetMetricStatistics.