: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(data-warehouse): add 3 missing tables to the Kubecost source (#124518)
Tom Owersbd4a3176983b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.mdindex 4e043cbcd8d4..098949680c6d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md@@ -7027,10 +7027,10 @@ Diffed against: <https://docs.kubecost.com/apis/apis-overview>- [x] `GET /model/allocation?aggregate=cluster` — cluster is the top breakdown dimension and the only aggregation missing from namespace/controller/pod (high). Added as `allocation_by_cluster`.- [ ] `GET /model/allocation?aggregate=label:<name>` — label-based chargeback is the standard way teams attribute Kubernetes spend (high). Not added: the label name is per deployment, so it is not a fixed table. Pod and controller allocation rows already carry `properties.labels` for label chargeback in SQL.- [x] `GET /model/allocation?aggregate=service and ?aggregate=node` — remaining first-class allocation breakdown dimensions the API documents (medium). Added as `allocation_by_service` and `allocation_by_node`.-- [ ] `GET /model/audit/events (Cost Events Audit API)` — cluster-level change history with estimated cost impact, i.e. why spend moved (medium)-- [ ] `GET /model/savings and /model/savings/requestSizingV2` — headline savings-opportunity numbers surfaced in the Savings dashboard (medium)-- [ ] `GET /model/customCost/timeseries (External Costs API)` — third-party service costs joined into total spend (medium)-- [ ] `GET /model/allocation/trends` — period-over-period percentage change, the metric the UI's total-cost column shows (medium)+- [x] `GET /model/audit/events (Cost Events Audit API)` — cluster-level change history with estimated cost impact, i.e. why spend moved (medium). Added as `audit_events` (full refresh; the API is Enterprise-only, off by default, and holds at most the 1000 most recent events).+- [x] `GET /model/savings and /model/savings/requestSizingV2` — headline savings-opportunity numbers surfaced in the Savings dashboard (medium). Added `/model/savings/requestSizingV2` as `request_sizing_recommendations` (full refresh, 7-day window). `/model/savings` not added: the docs list it with no parameters or response schema, and it is a total of the other savings endpoints.+- [x] `GET /model/customCost/timeseries (External Costs API)` — third-party service costs joined into total spend (medium). Added as `external_costs` (one-day windows, incremental on `window_start`).+- [ ] `GET /model/allocation/trends` — period-over-period percentage change, the metric the UI's total-cost column shows (medium). Not added: the API requires a `names` list of the items to compare, and it returns only a ratio of two windows (`current/previous - 1`) that SQL can compute from the `allocation_by_*` tables.- [ ] `GET /model/savings/clusterSizingETL and /model/savings/abandonedWorkloads` — cluster right-sizing and abandoned-workload recommendations (medium)- [ ] `GET /model/budget (Budget API)` — budget rules to join against actual allocation spend for over/under reporting (low)- [ ] `GET /model/forecast (Forecast API)` — predicted future spend for allocation, assets and cloud cost (low)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/canonical_descriptions.pyindex 9e9007bbb87d..a17730896df4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/canonical_descriptions.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/canonical_descriptions.py@@ -102,4 +102,57 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {"amortizedCost": "Effective cost with upfront payments spread across the billing period, with its Kubernetes share (kubernetesPercent).",},},+ "external_costs": {+ "description": "Costs of connected third-party services (such as Datadog) from the Kubecost External Costs API, one row per line item per day.",+ "docs_url": "https://www.ibm.com/docs/en/kubecost/self-hosted/2.x?topic=apis-external-costs-api",+ "columns": {+ "key": "The line item's aggregate value, built from all of its dimensions. Unique per window.",+ "window_start": "Start of the window this row covers (RFC3339). Used as the incremental cursor and partition key.",+ "window_end": "End of the window this row covers (RFC3339).",+ "aggregate": "Slash-separated values of the aggregation dimensions, with __unallocated__ for an empty dimension.",+ "zone": "Zone of the external cost.",+ "account_name": "Account name at the third-party service.",+ "charge_category": "Charge category of the cost, such as usage or billing.",+ "description": "Description of the charge.",+ "resource_name": "Name of the resource that the cost applies to.",+ "resource_type": "Type of the resource that the cost applies to.",+ "provider_id": "Identifier of the resource at the provider.",+ "billedCost": "Cost billed by the third-party service.",+ "listCost": "Cost at the list price of the third-party service.",+ "list_unit_price": "List price for one unit of usage.",+ "usage_quantity": "Quantity of usage charged.",+ "usage_unit": "Unit of the usage quantity.",+ "domain": "The third-party service the cost comes from, such as datadog.",+ "cost_source": "Category of the third-party service, such as observability.",+ },+ },+ "request_sizing_recommendations": {+ "description": "Container resource request recommendations and their estimated monthly savings from the Kubecost Container Request Right Sizing Recommendation API (V2), based on the last 7 days of usage. One row per container spec per controller.",+ "docs_url": "https://www.ibm.com/docs/en/kubecost/self-hosted/2.x?topic=apis-container-request-right-sizing-recommendation-api",+ "columns": {+ "clusterID": "Identifier of the cluster the container runs in.",+ "namespace": "Namespace of the controller.",+ "controllerKind": "Kind of the controller, such as deployment or statefulset.",+ "controllerName": "Name of the controller.",+ "containerName": "Name of the container.",+ "recommendedRequest": "Recommended CPU and memory requests for the container.",+ "monthlySavings": "Estimated monthly savings for CPU and memory if the recommendation is applied.",+ "latestKnownRequest": "The container's most recent CPU and memory requests.",+ "currentEfficiency": "Current CPU, memory, and total efficiency of the container's requests.",+ },+ },+ "audit_events": {+ "description": "Recent cluster changes (deployment and statefulset creation and deletion) with their predicted cost impact, from the Kubecost Cost Events Audit API. Holds up to the 1000 most recent events of the local cluster.",+ "docs_url": "https://www.ibm.com/docs/en/kubecost/self-hosted/2.x?topic=apis-cost-events-audit-api",+ "columns": {+ "key": "Identifier built from the event timestamp, event type, resource type, cluster, namespace, and resource name.",+ "eventType": "Type of change, such as add or delete.",+ "resourceType": "Type of the changed Kubernetes resource, such as deployment.",+ "timestamp": "Time the change event occurred (RFC3339).",+ "properties": "The changed resource's cluster, name, and namespace.",+ "totalRequests": "Total CPU and memory requests of the changed resource.",+ "resourceCosts": "Hourly cost rates used for the prediction: cost per CPU core, per RAM byte, and per GPU.",+ "costPrediction": "Predicted monthly cost rate of the change: total, CPU, RAM, and GPU.",+ },+ },}diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/kubecost.py b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/kubecost.pyindex 8d430d41fbfb..f7eae78bd831 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/kubecost.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/kubecost.py@@ -1,5 +1,5 @@import dataclasses-from collections.abc import Iterator+from collections.abc import Callable, Iteratorfrom datetime import UTC, date, datetime, timedeltafrom typing import Any, Optionalfrom urllib.parse import urlparse@@ -170,6 +170,81 @@ def _flatten_result_sets(return rows+def _flatten_timeseries(data: Any, requested_window: SyncWindow[str], items_key: str) -> list[dict[str, Any]]:+ """Flatten External Costs `timeseries` windows, each holding a list of line items, into rows."""+ rows: list[dict[str, Any]] = []+ timeseries = data.get("timeseries") if isinstance(data, dict) else None+ if not isinstance(timeseries, list):+ return rows+ for entry in timeseries:+ if not isinstance(entry, dict):+ continue+ items = entry.get(items_key)+ if not isinstance(items, list):+ continue+ window = entry.get("window")+ if not isinstance(window, dict):+ window = {}+ for item in items:+ if not isinstance(item, dict):+ continue+ rows.append(+ {+ **item,+ "key": item.get("aggregate") or item.get("id"),+ "window_start": window.get("start") or requested_window.start,+ "window_end": window.get("end") or requested_window.end,+ }+ )+ return rows+++def _audit_event_key(event: dict[str, Any]) -> str:+ # Audit events carry no id, so the key is built from what identifies the change.+ properties = event.get("properties")+ if not isinstance(properties, dict):+ properties = {}+ parts = [+ event.get("timestamp"),+ event.get("eventType"),+ event.get("resourceType"),+ properties.get("cluster"),+ properties.get("namespace"),+ properties.get("name"),+ ]+ return "/".join("" if part is None else str(part) for part in parts)+++_SNAPSHOT_KEY_BUILDERS: dict[str, Callable[[dict[str, Any]], str]] = {+ "audit_events": _audit_event_key,+}+++def _snapshot_rows(endpoint: str, data: Any) -> list[dict[str, Any]]:+ # Request sizing documents a bare list, and its topline variant wraps the list+ # in `Recommendations`, so both shapes are accepted.+ if isinstance(data, dict):+ data = data.get("Recommendations", data.get("recommendations"))+ if not isinstance(data, list):+ return []+ rows = [item for item in data if isinstance(item, dict)]+ key_builder = _SNAPSHOT_KEY_BUILDERS.get(endpoint)+ if key_builder is not None:+ rows = [{**row, "key": key_builder(row)} for row in rows]+ return rows+++def _unwrap_response(body: Any, allow_bare: bool) -> Any:+ if isinstance(body, dict) and "code" in body:+ if body.get("code") != 200:+ raise ValueError(f"Kubecost API error: {body.get('message')}")+ return body.get("data")+ if allow_bare and isinstance(body, list | dict):+ return body+ message = body.get("message") if isinstance(body, dict) else body+ raise ValueError(f"Kubecost API error: {message}")++def get_rows(host: str,api_key: Optional[str],@@ -193,12 +268,8 @@ def get_rows(wait=wait_exponential_jitter(initial=2, max=90),reraise=True,)- def call(window: str) -> Any:- response = session.get(- url,- params={**config.params, "window": window, "accumulate": "true"},- timeout=REQUEST_TIMEOUT_SECONDS,- )+ def call(params: dict[str, str]) -> Any:+ response = session.get(url, params=params, timeout=REQUEST_TIMEOUT_SECONDS)if response.status_code == 429 or response.status_code >= 500:raise KubecostRetryableError(f"Kubecost API error (retryable): status={response.status_code}")@@ -207,11 +278,13 @@ def get_rows(logger.error(f"Kubecost API error: status={response.status_code}, body={response.text[:500]}")response.raise_for_status()- body = response.json()- if not isinstance(body, dict) or body.get("code") != 200:- message = body.get("message") if isinstance(body, dict) else body- raise ValueError(f"Kubecost API error: {message}")- return body.get("data")+ return response.json()++ if config.snapshot_primary_keys is not None:+ rows = _snapshot_rows(endpoint, _unwrap_response(call(dict(config.params)), allow_bare=True))+ if rows:+ yield rows+ return# No cursor or pagination: the query window itself is the incremental filter, so we# walk contiguous one-day windows oldest-first. Recent days restate as cloud-billing@@ -233,8 +306,12 @@ def get_rows(day = startwhile day <= today:window = _day_window(day)- data = call(f"{window.start},{window.end}")- rows = _flatten_result_sets(data, window, config.result_set_key)+ body = call({**config.params, "window": f"{window.start},{window.end}", "accumulate": "true"})+ data = _unwrap_response(body, allow_bare=False)+ if config.timeseries_items_key is not None:+ rows = _flatten_timeseries(data, window, config.timeseries_items_key)+ else:+ rows = _flatten_result_sets(data, window, config.result_set_key)if rows:yield rows@@ -254,9 +331,10 @@ def kubecost_source(should_use_incremental_field: bool = False,db_incremental_field_last_value: Optional[Any] = None,) -> SourceResponse:- return SourceResponse(- name=endpoint,- items=lambda: get_rows(+ config = KUBECOST_ENDPOINTS[endpoint]++ def items() -> Iterator[list[dict[str, Any]]]:+ return get_rows(host=host,api_key=api_key,endpoint=endpoint,@@ -264,7 +342,14 @@ def kubecost_source(resumable_source_manager=resumable_source_manager,should_use_incremental_field=should_use_incremental_field,db_incremental_field_last_value=db_incremental_field_last_value,- ),+ )++ if config.snapshot_primary_keys is not None:+ return SourceResponse(name=endpoint, items=items, primary_keys=list(config.snapshot_primary_keys))++ return SourceResponse(+ name=endpoint,+ items=items,# The result-set key is only unique within one window, so the window start is# part of the key. `window_start` never changes for a given row, making it a# stable partition key too.diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/settings.pyindex c21399c78c6c..aecbc8ef6dc8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/settings.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/settings.py@@ -30,6 +30,12 @@ class KubecostEndpointConfig:# Set when `data` is an object of `sets`, each holding its result set under this key# (Cloud Cost API), rather than a bare list of result sets (Allocation/Assets APIs).result_set_key: str | None = None+ # Set when `data.timeseries` holds windows whose line items sit in a list under this+ # key (External Costs API), rather than in result sets keyed by name.+ timeseries_items_key: str | None = None+ # Snapshot endpoints return the current state in one request with no query window,+ # so they sync as a full refresh keyed on these columns.+ snapshot_primary_keys: tuple[str, ...] | None = NoneKUBECOST_ENDPOINTS: dict[str, KubecostEndpointConfig] = {@@ -73,10 +79,37 @@ KUBECOST_ENDPOINTS: dict[str, KubecostEndpointConfig] = {path="/model/cloudCost",result_set_key="cloudCosts",),+ "external_costs": KubecostEndpointConfig(+ name="external_costs",+ path="/model/customCost/timeseries",+ # Aggregating by every dimension keeps each line item distinct and gives it a+ # unique `aggregate` value within its window.+ params={+ "aggregate": "zone,accountName,chargeCategory,description,resourceName,"+ "resourceType,providerId,usageUnit,domain,costSource"+ },+ timeseries_items_key="customCosts",+ ),+ "request_sizing_recommendations": KubecostEndpointConfig(+ name="request_sizing_recommendations",+ path="/model/savings/requestSizingV2",+ # Kubecost recommends more than 2 days of usage for accurate sampling.+ params={"window": "7d"},+ snapshot_primary_keys=("clusterID", "namespace", "controllerKind", "controllerName", "containerName"),+ ),+ # Enterprise-only and disabled by default. It holds at most the 1000 most recent events+ # of the local cluster, and they do not survive a Kubecost restart.+ "audit_events": KubecostEndpointConfig(+ name="audit_events",+ path="/model/audit/events",+ snapshot_primary_keys=("key",),+ ),}ENDPOINTS = tuple(KUBECOST_ENDPOINTS.keys())INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {- name: list(_WINDOW_INCREMENTAL_FIELDS) for name in KUBECOST_ENDPOINTS+ name: list(_WINDOW_INCREMENTAL_FIELDS)+ for name, config in KUBECOST_ENDPOINTS.items()+ if config.snapshot_primary_keys is None}diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/tests/test_kubecost.py b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/tests/test_kubecost.pyindex 7facdfe09cb4..641d53433395 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/tests/test_kubecost.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/kubecost/tests/test_kubecost.py@@ -20,6 +20,13 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.kubecost.kfrom products.warehouse_sources.backend.temporal.data_imports.sources.kubecost.settings import ENDPOINTSfrom products.warehouse_sources.backend.temporal.data_imports.sources.kubecost.source import KubecostSource+_TODAY_WINDOW = "2026-07-15T00:00:00Z,2026-07-16T00:00:00Z"++_SNAPSHOT_PRIMARY_KEYS = {+ "request_sizing_recommendations": ["clusterID", "namespace", "controllerKind", "controllerName", "containerName"],+ "audit_events": ["key"],+}+def _driver(host: str = "https://k.example.com") -> SourceDriver:return SourceDriver(KubecostSource(), KubecostSourceConfig(host=host, api_key="token"))@@ -152,19 +159,36 @@ class TestGetRows:assert all(row["totalCost"] == 1.5 for row in rows)@pytest.mark.parametrize(- "endpoint, expected_path, expected_aggregate",+ "endpoint, expected_path, expected_aggregate, expected_window, expected_accumulate",[- ("allocation_by_namespace", "/model/allocation", "namespace"),- ("allocation_by_controller", "/model/allocation", "controller"),- ("allocation_by_pod", "/model/allocation", "pod"),- ("allocation_by_cluster", "/model/allocation", "cluster"),- ("allocation_by_node", "/model/allocation", "node"),- ("allocation_by_service", "/model/allocation", "service"),- ("assets", "/model/assets", None),- ("cloud_costs", "/model/cloudCost", None),+ ("allocation_by_namespace", "/model/allocation", "namespace", _TODAY_WINDOW, "true"),+ ("allocation_by_controller", "/model/allocation", "controller", _TODAY_WINDOW, "true"),+ ("allocation_by_pod", "/model/allocation", "pod", _TODAY_WINDOW, "true"),+ ("allocation_by_cluster", "/model/allocation", "cluster", _TODAY_WINDOW, "true"),+ ("allocation_by_node", "/model/allocation", "node", _TODAY_WINDOW, "true"),+ ("allocation_by_service", "/model/allocation", "service", _TODAY_WINDOW, "true"),+ ("assets", "/model/assets", None, _TODAY_WINDOW, "true"),+ ("cloud_costs", "/model/cloudCost", None, _TODAY_WINDOW, "true"),+ (+ "external_costs",+ "/model/customCost/timeseries",+ "zone,accountName,chargeCategory,description,resourceName,resourceType,providerId,usageUnit,domain,costSource",+ _TODAY_WINDOW,+ "true",+ ),+ # Snapshot endpoints ignore the saved day and query their fixed lookback, or no window at all.+ ("request_sizing_recommendations", "/model/savings/requestSizingV2", None, "7d", None),+ ("audit_events", "/model/audit/events", None, None, None),],)- def test_endpoint_path_and_params(self, endpoint: str, expected_path: str, expected_aggregate: str | None) -> None:+ def test_endpoint_path_and_params(+ self,+ endpoint: str,+ expected_path: str,+ expected_aggregate: str | None,+ expected_window: str | None,+ expected_accumulate: str | None,+ ) -> None:result = _driver().run(endpoint,[ScriptedResponse(json={"code": 200, "data": [None]})],@@ -175,19 +199,143 @@ class TestGetRows:assert result.paths == [expected_path]assert result.requests[0].origin == "https://k.example.com"assert result.requests[0].param("aggregate") == expected_aggregate- assert result.requests[0].param("accumulate") == "true"+ assert result.requests[0].param("window") == expected_window+ assert result.requests[0].param("accumulate") == expected_accumulate++ @pytest.mark.parametrize(+ "timeseries, expected_rows",+ [+ (+ [+ {+ "window": {"start": "2026-07-15T00:00:00Z", "end": "2026-07-15T01:00:00Z"},+ "customCosts": [+ {"id": "1", "aggregate": "us-east/acct/usage/datadog/observability", "billedCost": 2.5},+ # No aggregate: the id is the key.+ {"id": "2", "billedCost": 0.5},+ "not-a-line-item",+ ],+ },+ # A window with no data and a window without its own bounds.+ {"window": {"start": "2026-07-15T01:00:00Z", "end": "2026-07-15T02:00:00Z"}, "customCosts": None},+ {"customCosts": [{"aggregate": "eu-west/acct/usage/datadog/observability", "billedCost": 1.0}]},+ ],+ [+ (+ "us-east/acct/usage/datadog/observability",+ "2026-07-15T00:00:00Z",+ "2026-07-15T01:00:00Z",+ 2.5,+ ),+ ("2", "2026-07-15T00:00:00Z", "2026-07-15T01:00:00Z", 0.5),+ (+ "eu-west/acct/usage/datadog/observability",+ "2026-07-15T00:00:00Z",+ "2026-07-16T00:00:00Z",+ 1.0,+ ),+ ],+ ),+ (None, []),+ ],+ )+ def test_external_costs_flattens_timeseries_line_items(+ self, timeseries: Any, expected_rows: list[tuple[str, str, str, float]]+ ) -> None:+ result = _driver().run(+ "external_costs",+ [ScriptedResponse(json={"code": 200, "data": {"window": {}, "timeseries": timeseries}})],+ resume_state=KubecostResumeConfig(next_date="2026-07-15"),+ )- def test_api_key_over_plain_http_raises_before_any_request(self) -> None:- result = _driver("http://kubecost.example.com").run("allocation_by_namespace", [])+ assert result.raised is None+ assert [+ (row["key"], row["window_start"], row["window_end"], row["billedCost"]) for row in result.rows+ ] == expected_rows++ @pytest.mark.parametrize(+ "body, expected_keys",+ [+ # The documented shape is a bare list.+ (+ [+ {"clusterID": "c1", "namespace": "web", "controllerName": "api", "containerName": "app"},+ {"clusterID": "c1", "namespace": "web", "controllerName": "api", "containerName": "sidecar"},+ None,+ ],+ ["app", "sidecar"],+ ),+ (+ {"code": 200, "data": [{"clusterID": "c1", "containerName": "app"}]},+ ["app"],+ ),+ (+ {"TotalMonthlySavings": 4.2, "Count": 1, "Recommendations": [{"containerName": "app"}]},+ ["app"],+ ),+ ({"TotalMonthlySavings": 0, "Count": 0, "Recommendations": None}, []),+ ],+ )+ def test_request_sizing_accepts_documented_response_shapes(self, body: Any, expected_keys: list[str]) -> None:+ result = _driver().run(+ "request_sizing_recommendations",+ [ScriptedResponse(json=body)],+ resume_state=KubecostResumeConfig(next_date="2026-07-01"),+ )++ assert result.raised is None+ assert [row["containerName"] for row in result.rows] == expected_keys+ # One request for the current state: no day walk and no checkpoint.+ assert len(result.requests) == 1+ assert result.saved_states == []++ @pytest.mark.parametrize(+ "event, expected_key",+ [+ (+ {+ "eventType": "add",+ "resourceType": "deployment",+ "timestamp": "2026-07-14T13:58:44.552788548Z",+ "properties": {"cluster": "cluster-one", "name": "web", "namespace": "shop"},+ },+ "2026-07-14T13:58:44.552788548Z/add/deployment/cluster-one/shop/web",+ ),+ (+ {"eventType": "delete", "timestamp": "2026-07-14T14:00:00Z", "properties": None},+ "2026-07-14T14:00:00Z/delete////",+ ),+ ],+ )+ def test_audit_events_get_a_key_built_from_the_change(self, event: dict[str, Any], expected_key: str) -> None:+ result = _driver().run("audit_events", [ScriptedResponse(json={"code": 200, "data": [event]})])++ assert result.raised is None+ assert [row["key"] for row in result.rows] == [expected_key]+ assert result.rows[0]["eventType"] == event["eventType"]++ @pytest.mark.parametrize("endpoint", ["allocation_by_namespace", "audit_events"])+ def test_api_key_over_plain_http_raises_before_any_request(self, endpoint: str) -> None:+ result = _driver("http://kubecost.example.com").run(endpoint, [])assert isinstance(result.raised, ValueError)assert "https" in str(result.raised)assert result.requests == []- def test_error_envelope_raises(self) -> None:+ @pytest.mark.parametrize(+ "endpoint, body",+ [+ ("allocation_by_namespace", {"code": 400, "message": "bad request"}),+ # A windowed endpoint must not take a bare body for a Kubecost response.+ ("allocation_by_namespace", {"message": "bad request"}),+ ("audit_events", {"code": 400, "message": "bad request"}),+ ("request_sizing_recommendations", {"code": 400, "message": "bad request"}),+ ],+ )+ def test_error_envelope_raises(self, endpoint: str, body: dict[str, Any]) -> None:result = _driver().run(- "allocation_by_namespace",- [ScriptedResponse(json={"code": 400, "message": "bad request"})],+ endpoint,+ [ScriptedResponse(json=body)],resume_state=KubecostResumeConfig(next_date="2026-07-15"),)@@ -216,7 +364,7 @@ class TestKubecostSourceResponse:with time_machine.travel("2026-07-15T10:00:00Z", tick=False):yield- @pytest.mark.parametrize("endpoint", list(ENDPOINTS))+ @pytest.mark.parametrize("endpoint", [e for e in ENDPOINTS if e not in _SNAPSHOT_PRIMARY_KEYS])def test_response_metadata_per_endpoint(self, endpoint: str) -> None:result = _driver().run(endpoint,@@ -234,3 +382,15 @@ class TestKubecostSourceResponse:assert response.sort_mode == "asc"assert response.partition_mode == "datetime"assert response.partition_keys == ["window_start"]++ @pytest.mark.parametrize("endpoint, expected_primary_keys", list(_SNAPSHOT_PRIMARY_KEYS.items()))+ def test_snapshot_response_metadata(self, endpoint: str, expected_primary_keys: list[str]) -> None:+ result = _driver().run(endpoint, [ScriptedResponse(json={"code": 200, "data": []})])++ assert result.raised is None+ response = result.response+ assert response is not None+ assert response.primary_keys == expected_primary_keys+ # A snapshot is not read in window order, so it makes no ordering promise.+ assert response.sort_mode is None+ assert response.partition_keys is None