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.

feat(warehouse_sources): support better stack API version v3 (#119194)

Tom Owersa56ceeb94687
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/better_stack.py b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/better_stack.py
index 0faf79c842e4..455f3de6247c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/better_stack.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/better_stack.py
@@ -169,7 +169,7 @@ def _client_config(api_token: str, base_url: str) -> ClientConfig:
}
-def probe_credentials(api_token: str, endpoint: str | None = None) -> int | None:
+def probe_credentials(api_token: str, api_version: str, endpoint: str | None = None) -> int | None:
"""Cheap probe of a Better Stack collection. Returns the HTTP status code, or None on a
connection failure. Probes the given endpoint's path when set, else the monitors collection."""
config = BETTER_STACK_ENDPOINTS.get(endpoint) if endpoint else None
@@ -178,7 +178,7 @@ def probe_credentials(api_token: str, endpoint: str | None = None) -> int | None
# reachable thing to probe is the parent the child is enumerated from.
config = BETTER_STACK_ENDPOINTS[config.fanout.parent_name]
base_url = config.base_url if config else BETTER_STACK_BASE_URL
- path = config.path if config else "/v2/monitors"
+ path = config.path_for_version(api_version) if config else "/v2/monitors"
_ok, status = validate_via_probe(
lambda: make_tracked_session(capture=False, redact_values=(api_token,)),
f"{base_url}{path}?per_page=1",
@@ -328,10 +328,12 @@ def better_stack_source(
team_id: int,
job_id: str,
resumable_source_manager: ResumableSourceManager[BetterStackResumeConfig],
+ api_version: str,
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
- config = BETTER_STACK_ENDPOINTS[endpoint]
+ base_config = BETTER_STACK_ENDPOINTS[endpoint]
+ config = dataclasses.replace(base_config, path=base_config.path_for_version(api_version))
build = _fanout_source if config.fanout is not None else _top_level_source
return build(
api_token,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/settings.py
index 4521299a67ba..686f2b636750 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/settings.py
@@ -16,6 +16,11 @@ BETTER_STACK_BASE_URL = "https://uptime.betterstack.com/api"
# while this one returns 401 on a bad token.
BETTER_STACK_ORG_BASE_URL = "https://betterstack.com/api"
+# Source-level version labels. Both pins read incidents from /v3. The v3 pin also reads
+# escalation policies from /v3/policies, which adds team and policy-group fields over /v2/policies.
+BETTER_STACK_V2 = "v2"
+BETTER_STACK_V3 = "v3"
+
# A parent row deleted between the parent listing and the child fetch 404s. Skip that parent
# instead of failing the whole fan-out.
_SKIP_MISSING_PARENT: list[ResponseAction] = [{"status_code": 404, "action": "ignore"}]
@@ -25,6 +30,8 @@ _SKIP_MISSING_PARENT: list[ResponseAction] = [{"status_code": 404, "action": "ig
class BetterStackEndpointConfig:
name: str
path: str
+ # Path served under the v3 pin, where it differs from `path`.
+ v3_path: Optional[str] = None
# Host serving this resource. Defaults to the Uptime API.
base_url: str = BETTER_STACK_BASE_URL
# Only True where Better Stack exposes a genuine server-side date filter on this endpoint's
@@ -45,6 +52,11 @@ class BetterStackEndpointConfig:
# Set where the resource only exists per parent row (e.g. /monitors/{monitor_id}/sla).
fanout: Optional[DependentEndpointConfig] = None
+ def path_for_version(self, api_version: str) -> str:
+ if api_version == BETTER_STACK_V3 and self.v3_path is not None:
+ return self.v3_path
+ return self.path
+
@property
def default_incremental_field(self) -> str | None:
return self.incremental_fields[0]["field"] if self.incremental_fields else None
@@ -221,6 +233,7 @@ BETTER_STACK_ENDPOINTS: dict[str, BetterStackEndpointConfig] = {
"escalation_policies": BetterStackEndpointConfig(
name="escalation_policies",
path="/v2/policies",
+ v3_path="/v3/policies",
),
# The routes still carry the feature's former name, `urgencies`.
"severities": BetterStackEndpointConfig(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/source.py
index 7a0b2cf90067..f30e32a60af6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/source.py
@@ -20,6 +20,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.better_sta
)
from products.warehouse_sources.backend.temporal.data_imports.sources.better_stack.settings import (
BETTER_STACK_ENDPOINTS,
+ BETTER_STACK_V2,
+ BETTER_STACK_V3,
ENDPOINTS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType, ResumableSource
@@ -73,8 +75,8 @@ class BetterStackSource(ResumableSource[BetterStackSourceConfig, BetterStackResu
alternative_to=(ProductKey.LOGS,),
)
- supported_versions = ("v2",)
- default_version = "v2"
+ supported_versions = (BETTER_STACK_V2, BETTER_STACK_V3)
+ default_version = BETTER_STACK_V3
api_docs_url = "https://betterstack.com/docs/uptime/api/getting-started-with-uptime-api/"
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
@@ -162,7 +164,7 @@ You can create an Uptime API token in your [Better Stack dashboard](https://upti
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- status = probe_credentials(config.api_token, schema_name)
+ status = probe_credentials(config.api_token, self.resolve_api_version(api_version), schema_name)
if status == 200:
return True, None
@@ -191,6 +193,7 @@ You can create an Uptime API token in your [Better Stack dashboard](https://upti
team_id=inputs.team_id,
job_id=inputs.job_id,
resumable_source_manager=resumable_source_manager,
+ api_version=self.resolve_api_version(inputs.api_version),
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack.py b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack.py
index 28b352ce3446..13c164e2f85a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack.py
@@ -193,7 +193,7 @@ class TestProbeCredentials:
@parameterized.expand([("ok", 200), ("unauthorized", 401), ("forbidden", 403)])
def test_returns_status_code(self, _name: str, status_code: int) -> None:
with scripted_network([ScriptedResponse(status=status_code)]) as network:
- assert probe_credentials("bs_test", "incidents") == status_code
+ assert probe_credentials("bs_test", "v3", "incidents") == status_code
assert network.requests_log[0].path == "/api/v3/incidents"
@@ -246,6 +246,27 @@ class TestOrganizationHost:
assert result.requests[0].path == f"/api{path}"
+class TestApiVersionDispatch:
+ @parameterized.expand(
+ [
+ ("policies_v2", "escalation_policies", "v2", "/api/v2/policies"),
+ ("policies_v3", "escalation_policies", "v3", "/api/v3/policies"),
+ ("policies_unpinned_uses_default", "escalation_policies", None, "/api/v3/policies"),
+ ("incidents_v2", "incidents", "v2", "/api/v3/incidents"),
+ ("incidents_v3", "incidents", "v3", "/api/v3/incidents"),
+ ("monitors_v2", "monitors", "v2", "/api/v2/monitors"),
+ ("monitors_v3", "monitors", "v3", "/api/v2/monitors"),
+ ]
+ )
+ def test_request_path_follows_the_pin(
+ self, _name: str, endpoint: str, api_version: str | None, expected_path: str
+ ) -> None:
+ result = _driver().run(endpoint, [_response([{"id": "1", "attributes": {}}])], api_version=api_version)
+
+ assert result.raised is None
+ assert result.paths == [expected_path]
+
+
class TestFanout:
@parameterized.expand(
[
@@ -327,19 +348,21 @@ class TestFanout:
class TestProbeCredentialsForFanoutEndpoints:
@parameterized.expand(
[
- ("monitor_availability", f"{BETTER_STACK_BASE_URL}/v2/monitors?per_page=1"),
- ("monitor_response_times", f"{BETTER_STACK_BASE_URL}/v2/monitors?per_page=1"),
- ("incident_comments", f"{BETTER_STACK_BASE_URL}/v3/incidents?per_page=1"),
- ("heartbeat_availability", f"{BETTER_STACK_BASE_URL}/v2/heartbeats?per_page=1"),
- ("status_page_resources", f"{BETTER_STACK_BASE_URL}/v2/status-pages?per_page=1"),
- ("on_call_events", f"{BETTER_STACK_BASE_URL}/v2/on-calls?per_page=1"),
- ("on_call_rotations", f"{BETTER_STACK_BASE_URL}/v2/on-calls?per_page=1"),
- ("team_members", f"{BETTER_STACK_ORG_BASE_URL}/v2/team-members?per_page=1"),
+ ("monitor_availability", "v3", f"{BETTER_STACK_BASE_URL}/v2/monitors?per_page=1"),
+ ("monitor_response_times", "v3", f"{BETTER_STACK_BASE_URL}/v2/monitors?per_page=1"),
+ ("incident_comments", "v3", f"{BETTER_STACK_BASE_URL}/v3/incidents?per_page=1"),
+ ("heartbeat_availability", "v3", f"{BETTER_STACK_BASE_URL}/v2/heartbeats?per_page=1"),
+ ("status_page_resources", "v3", f"{BETTER_STACK_BASE_URL}/v2/status-pages?per_page=1"),
+ ("on_call_events", "v3", f"{BETTER_STACK_BASE_URL}/v2/on-calls?per_page=1"),
+ ("on_call_rotations", "v3", f"{BETTER_STACK_BASE_URL}/v2/on-calls?per_page=1"),
+ ("team_members", "v3", f"{BETTER_STACK_ORG_BASE_URL}/v2/team-members?per_page=1"),
+ ("escalation_policies", "v2", f"{BETTER_STACK_BASE_URL}/v2/policies?per_page=1"),
+ ("escalation_policies", "v3", f"{BETTER_STACK_BASE_URL}/v3/policies?per_page=1"),
]
)
- def test_probes_a_reachable_collection(self, endpoint: str, expected_url: str) -> None:
+ def test_probes_a_reachable_collection(self, endpoint: str, api_version: str, expected_url: str) -> None:
with scripted_network([ScriptedResponse()]) as network:
- probe_credentials("bs_test", endpoint)
+ probe_credentials("bs_test", api_version, endpoint)
# A fan-out child has no collection of its own, so its parent is probed instead.
assert network.requests_log[0].url == expected_url
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack_source.py
index 4091338ff5f0..8fa5b7e33923 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/better_stack/tests/test_better_stack_source.py
@@ -78,6 +78,17 @@ class TestBetterStackValidateCredentials:
assert (error is None) is expected_ok
assert network.requests_log[0].headers["authorization"] == "Bearer bs_test"
+ @parameterized.expand([("v2", "/api/v2/policies"), ("v3", "/api/v3/policies"), (None, "/api/v3/policies")])
+ def test_probe_follows_the_pin(self, api_version: str | None, expected_path: str) -> None:
+ with scripted_network([ScriptedResponse()]) as network:
+ BetterStackSource().validate_credentials(
+ BetterStackSourceConfig(api_token="bs_test"),
+ team_id=1,
+ schema_name="escalation_policies",
+ api_version=api_version,
+ )
+ assert network.requests_log[0].path == expected_path
+
class TestBetterStackNonRetryableErrors:
@parameterized.expand(