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(warehouse-sources): stop sending limit on Metronome usage requests (#99337)

Daniel RC070ea0095b45
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/metronome.py b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/metronome.py
index 042da11c0993..34b643bdb0f5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/metronome.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/metronome.py
@@ -255,7 +255,9 @@ def _rest_client(api_key: str) -> RESTClient:
def _list_params(config: MetronomeEndpointConfig) -> dict[str, Any]:
- params: dict[str, Any] = {} if not config.paginated else {"limit": config.page_size}
+ params: dict[str, Any] = {}
+ if config.paginated and config.accepts_page_size:
+ params["limit"] = config.page_size
params.update(config.extra_params)
return params
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/settings.py
index 50af1a602237..2bd77b974b89 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/settings.py
@@ -11,7 +11,7 @@ from products.warehouse_sources.backend.types import IncrementalField
METRONOME_BASE_URL = "https://api.metronome.com"
-# Every paginated Metronome list endpoint caps `limit` at 100.
+# Metronome caps `limit` at 100 on every list endpoint that accepts it.
PAGE_SIZE = 100
# List responses share a `{"data": [...], "next_page": "..."}` envelope, and the cursor goes back
@@ -63,12 +63,17 @@ class MetronomeEndpointConfig:
path: str
primary_key: list[str]
method: Literal["get", "post"] = "get"
- # Filter payload for the POST list endpoints. `limit` and `next_page` stay in the query string
- # on these; only the filters move into the body.
+ # Filter payload for the POST list endpoints. Only the filters move into the body, and
+ # `next_page` stays in the query string. See `accepts_page_size` for whether `limit` joins it.
json_body: dict[str, Any] = field(default_factory=dict)
extra_params: dict[str, Any] = field(default_factory=dict)
# Endpoints whose response carries a `next_page` cursor; the rest return the whole collection.
paginated: bool = True
+ # Whether the endpoint takes a `limit` page-size parameter. `POST /v1/usage` follows
+ # `next_page` but rejects `limit`, and answers the whole request with a 400 when it is
+ # present. Following the cursor and setting a page size are separate capabilities, so
+ # `paginated` must not decide both.
+ accepts_page_size: bool = True
# Server-side lower bound for incremental syncs. Metronome rejects this window when a cursor
# is also sent, so it only rides the first request of a run.
incremental_start_param: str | None = None
@@ -182,6 +187,7 @@ METRONOME_ENDPOINTS: dict[str, MetronomeEndpointConfig] = {
method="post",
# One aggregate per customer and billable metric — the endpoint carries no row id.
primary_key=["customer_id", "billable_metric_id"],
+ accepts_page_size=False,
# `none` returns a single lifetime aggregate per customer/metric over the requested window,
# which starts at the epoch, so the row count stays bounded by the account. The two tables
# below carry the same usage split into periods, over a window bounded per table.
@@ -197,6 +203,7 @@ METRONOME_ENDPOINTS: dict[str, MetronomeEndpointConfig] = {
# lifetime table identifies a row by.
primary_key=["customer_id", "billable_metric_id", "start_timestamp"],
partition_key="start_timestamp",
+ accepts_page_size=False,
window_size="day",
incremental_fields=[incremental_field("start_timestamp")],
default_incremental_field="start_timestamp",
@@ -210,6 +217,7 @@ METRONOME_ENDPOINTS: dict[str, MetronomeEndpointConfig] = {
method="post",
primary_key=["customer_id", "billable_metric_id", "start_timestamp"],
partition_key="start_timestamp",
+ accepts_page_size=False,
window_size="hour",
incremental_fields=[incremental_field("start_timestamp")],
default_incremental_field="start_timestamp",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/tests/test_metronome.py b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/tests/test_metronome.py
index d096523a3ded..ca388581c9ba 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/tests/test_metronome.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/metronome/tests/test_metronome.py
@@ -150,6 +150,19 @@ class TestMetronomeResources:
assert resource["endpoint"]["json"] == expected_body
assert resource["endpoint"]["params"] == {"limit": 100}
+ @parameterized.expand([("usage",), ("usage_daily",), ("usage_hourly",)])
+ def test_usage_endpoints_page_by_cursor_and_send_no_page_size(self, endpoint) -> None:
+ # `POST /v1/usage` takes `next_page` in the query string but accepts no `limit`, and it
+ # rejects the whole request when `limit` is present. Answering that by dropping the cursor
+ # instead of the page size would import the first page and report success.
+ resource = cast(
+ dict[str, Any],
+ get_resource(endpoint, should_use_incremental_field=False, window_starting_on=EPOCH_RFC_3339),
+ )
+
+ assert resource["endpoint"]["params"] == {}
+ assert isinstance(resource["endpoint"]["paginator"], MetronomeCursorPaginator)
+
@parameterized.expand([("invoices",), ("contracts",)])
def test_get_resource_rejects_fanout_endpoints(self, endpoint) -> None:
with pytest.raises(ValueError, match="Fan-out endpoint"):