Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): drop unsupported Unit metric for non-ec2 reservation coverage (#118975)

Tom Owers9a4d53d68f37
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/aws_cost_explorer.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/aws_cost_explorer.py
index cceb34872226..392b43ece4bf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/aws_cost_explorer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/aws_cost_explorer.py
@@ -176,7 +176,10 @@ def build_payload(
if window_services is not None:
payload["Filter"] = {"Dimensions": {"Key": "SERVICE", "Values": window_services}}
if endpoint_config.metrics:
- payload["Metrics"] = list(endpoint_config.metrics)
+ metrics = endpoint_config.metrics
+ if endpoint_config.unit_metric_services and slice_key not in endpoint_config.unit_metric_services:
+ metrics = tuple(metric for metric in metrics if metric != "Unit")
+ payload["Metrics"] = list(metrics)
if endpoint_config.group_by:
payload["GroupBy"] = [{"Type": "DIMENSION", "Key": key} for key in endpoint_config.group_by]
if page_token and endpoint_config.page_token_key:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/settings.py
index c9afa6aba6e5..ccd3d6056599 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/settings.py
@@ -63,6 +63,10 @@ class CostExplorerEndpointConfig:
# GetCostAndUsageWithResources requires a filter, so each window is filtered to the services
# GetDimensionValues reports for it, which keeps every service in scope.
filter_by_window_services: bool = False
+ # AWS reports reservation coverage in normalized usage units only for services with
+ # instance-size-flexible reservations; asking for "Unit" against any other service in
+ # `services` raises a ValidationException. Empty means no service needs filtering out.
+ unit_metric_services: tuple[str, ...] = ()
AWS_COST_EXPLORER_ENDPOINTS: dict[str, CostExplorerEndpointConfig] = {
@@ -162,6 +166,7 @@ AWS_COST_EXPLORER_ENDPOINTS: dict[str, CostExplorerEndpointConfig] = {
window_days=92,
restatement_lookback_days=7,
nested_keys=(("Total", ""),),
+ unit_metric_services=("Amazon Elastic Compute Cloud - Compute",),
),
"savings_plans_coverage_daily": CostExplorerEndpointConfig(
name="savings_plans_coverage_daily",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/tests/test_aws_cost_explorer.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/tests/test_aws_cost_explorer.py
index 7a9897a4a808..f4a9092bb4b8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/tests/test_aws_cost_explorer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aws_cost_explorer/tests/test_aws_cost_explorer.py
@@ -432,3 +432,13 @@ class TestGetRows:
assert row["coverage_hours_reserved_hours"] == 18.0
assert row["coverage_hours_coverage_hours_percentage"] == 75.0
assert row["coverage_cost_on_demand_cost"] == 1.5
+
+ # AWS rejects the "Unit" metric for every reservation-coverage service except EC2, which
+ # is the only one with instance-size-flexible (normalized unit) reservations.
+ metrics = [call[0][3]["Metrics"] for call in send.call_args_list]
+ assert metrics == [
+ ["Hour", "Unit", "Cost"],
+ ["Hour", "Cost"],
+ ["Hour", "Cost"],
+ ["Hour", "Cost"],
+ ]