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): add Aiven membership and events tables (#98101)

Tom Owersb7a883a90326
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.md
index d85c04425a3a..5807e53a49b4 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
@@ -175,14 +175,14 @@ Note: AirOps has repositioned around AI search visibility; the entire brand-kit
## Aiven — gaps
-Today (9): `billing_groups`, `clouds`, `invoice_lines`, `invoices`, `organization_users`, `organizations`, `projects`, `services`, `user_groups`
+Today (13): `billing_group_projects`, `billing_groups`, `clouds`, `invoice_lines`, `invoices`, `organization_users`, `organizations`, `project_events`, `project_users`, `projects`, `services`, `user_group_members`, `user_groups`
Diffed against: <https://api.aiven.io/doc/openapi.json>
-- [ ] `GET /project/{project}/users` — project membership join - who can access each project we already sync (high)
-- [ ] `GET /organization/{organization_id}/user-groups/{user_group_id}/members` — membership rows for the user_groups table already synced (high)
-- [ ] `GET /billing-group/{billing_group_id}/projects` — lookup mapping projects to billing groups, needed to attribute invoice_lines to projects (high)
-- [ ] `GET /project/{project}/events` — project/service audit and state-change history (high)
+- [x] `GET /project/{project}/users` — project membership join - who can access each project we already sync (high)
+- [x] `GET /organization/{organization_id}/user-groups/{user_group_id}/members` — membership rows for the user_groups table already synced (high)
+- [x] `GET /billing-group/{billing_group_id}/projects` — lookup mapping projects to billing groups, needed to attribute invoice_lines to projects (high)
+- [x] `GET /project/{project}/events` — project/service audit and state-change history (high)
- [ ] `GET /billing-group/{billing_group_id}/credits` — credit transactions that offset the invoices already synced (medium)
- [ ] `GET /project/{project}/service-types and /project/{project}/service-types/{service_type}/plans` — lookup resolving the service_type/plan identifiers carried on services (medium)
- [ ] `GET /project/{project}/service/{service_name}/tags and /project/{project}/tags` — tag dimensions used for cost and ownership attribution (medium)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/aiven.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/aiven.py
index 12421ea56acc..3b3dae2c62a6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/aiven.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/aiven.py
@@ -41,10 +41,10 @@ def _auth_header_value(api_token: str) -> str:
return f"aivenv1 {api_token}"
-def _client_config(api_token: str) -> ClientConfig:
+def _client_config(api_token: str, *, capture: bool) -> ClientConfig:
# The credential travels via framework api_key auth (not a hand-built header) so its value is
# registered for redaction wherever it surfaces in logs; only non-secret headers go here.
- return {
+ config: ClientConfig = {
"base_url": AIVEN_BASE_URL,
"headers": {"Accept": "application/json"},
"auth": {
@@ -54,6 +54,12 @@ def _client_config(api_token: str) -> ClientConfig:
"location": "header",
},
}
+ if not capture:
+ # Rows carry identity PII (email, real name, or an opaque nested profile object) that a
+ # name-based sample scrubber can't reliably strip out of a nested field -- same pattern as
+ # gusto/cliniko. Keep bodies out of HTTP sample capture; requests are still metered and logged.
+ config["capture"] = False
+ return config
def _stamp_parent_field(
@@ -194,6 +200,71 @@ def _invoice_lines_resource(
return next(r for r in resources if r.name == config.name)
+def _two_level_resource(
+ config: AivenEndpointConfig, client_config: ClientConfig, team_id: int, job_id: str
+) -> Resource:
+ """Generic organization -> intermediate list -> child fan-out (see ``TwoLevelFanOut``).
+
+ The intermediate list is fetched per organization and carries its ``organization_id`` down; the
+ child endpoint is fetched per intermediate row, binding each ``child_params`` placeholder from
+ that row and stamping the resolved value onto every child row. ``organization_id`` is stamped
+ with ``setdefault`` (a row that already has a real org id keeps it); every other placeholder is
+ set unconditionally, matching the single-parent fan-out convention.
+ """
+ two = config.two_level
+ assert two is not None, f"{config.name} is fan_out=two_level but carries no two_level config"
+
+ child_fields = list(dict.fromkeys(two.child_params.values()))
+ stamps = [
+ _stamp_parent_field(two.intermediate_name, field_name, placeholder, overwrite=placeholder != "organization_id")
+ for placeholder, field_name in two.child_params.items()
+ ]
+
+ rest_config: RESTAPIConfig = {
+ "client": client_config,
+ "resources": [
+ {
+ "name": "organizations",
+ "endpoint": {
+ "path": _ORG_PARENT["path"],
+ "data_selector": _ORG_PARENT["data_key"],
+ "paginator": _SINGLE_PAGE(),
+ },
+ },
+ {
+ "name": two.intermediate_name,
+ "endpoint": {
+ "path": two.intermediate_path,
+ "params": {
+ "organization_id": {"type": "resolve", "resource": "organizations", "field": "organization_id"}
+ },
+ "data_selector": two.intermediate_data_key,
+ "paginator": _SINGLE_PAGE(),
+ },
+ "include_from_parent": ["organization_id"],
+ # Land the org id as a clean column so the child can resolve/inject it.
+ "data_map": _stamp_parent_field("organizations", "organization_id", "organization_id", overwrite=True),
+ },
+ {
+ "name": config.name,
+ "endpoint": {
+ "path": config.path_template,
+ "params": {
+ placeholder: {"type": "resolve", "resource": two.intermediate_name, "field": field_name}
+ for placeholder, field_name in two.child_params.items()
+ },
+ "data_selector": config.data_key,
+ "paginator": _SINGLE_PAGE(),
+ },
+ "include_from_parent": child_fields,
+ "data_map": _compose(*stamps),
+ },
+ ],
+ }
+ resources = rest_api_resources(rest_config, team_id, job_id, None)
+ return next(r for r in resources if r.name == config.name)
+
+
def _compose(
*maps: Callable[[dict[str, Any]], dict[str, Any]],
) -> Callable[[dict[str, Any]], dict[str, Any]]:
@@ -206,7 +277,7 @@ def _compose(
def _build_resource(config: AivenEndpointConfig, api_token: str, team_id: int, job_id: str) -> Resource:
- client_config = _client_config(api_token)
+ client_config = _client_config(api_token, capture=config.capture)
if config.fan_out == "none":
return _standard_resource(config, client_config, team_id, job_id)
@@ -227,6 +298,8 @@ def _build_resource(config: AivenEndpointConfig, api_token: str, team_id: int, j
)
if config.fan_out == "invoice":
return _invoice_lines_resource(config, client_config, team_id, job_id)
+ if config.fan_out == "two_level":
+ return _two_level_resource(config, client_config, team_id, job_id)
raise ValueError(f"Unknown fan_out mode: {config.fan_out}")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/canonical_descriptions.py
index 0d5d3dda02be..720b9520d9f0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/canonical_descriptions.py
@@ -50,6 +50,55 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"node_count": "Number of service nodes in the active plan.",
},
},
+ "project_users": {
+ "description": "A member of an Aiven project and the access they hold on it.",
+ "docs_url": "https://api.aiven.io/doc/#tag/Project/operation/ProjectUserList",
+ "columns": {
+ "project_name": "Name of the project the membership is in (injected from the parent).",
+ "user_id": "Unique identifier of the user.",
+ "user_email": "Email address of the user.",
+ "real_name": "Display name of the user.",
+ "member_type": "Role of the user on the project (e.g. admin, developer, operator).",
+ "team_id": "Identifier of the team the membership is granted through, if any.",
+ "team_name": "Name of the team the membership is granted through, if any.",
+ "billing_contact": "Whether the user is a billing contact for the project.",
+ "create_time": "Timestamp when the membership was created.",
+ },
+ },
+ "project_events": {
+ "description": "An entry in a project's audit and state-change event log.",
+ "docs_url": "https://api.aiven.io/doc/#tag/Project/operation/ProjectGetEventLogs",
+ "columns": {
+ "project_name": "Name of the project the event belongs to (injected from the parent).",
+ "id": "Unique identifier of the event within the project.",
+ "actor": "Who or what triggered the event.",
+ "event_type": "Machine-readable event type code.",
+ "event_desc": "Human-readable description of the event.",
+ "service_name": "Name of the service the event relates to, if any.",
+ "time": "Timestamp when the event occurred.",
+ },
+ },
+ "user_group_members": {
+ "description": "Membership rows linking users to an organization's user groups.",
+ "docs_url": "https://api.aiven.io/doc/#tag/Organizations/operation/UserGroupMemberList",
+ "columns": {
+ "organization_id": "Organization the group belongs to (injected from the parent).",
+ "user_group_id": "User group the membership is in (injected from the parent).",
+ "user_id": "Unique identifier of the member.",
+ "user_info": "Nested object with the member's profile details.",
+ "last_activity_time": "Timestamp of the member's last activity.",
+ },
+ },
+ "billing_group_projects": {
+ "description": "Mapping of projects to the billing group they are billed under.",
+ "docs_url": "https://api.aiven.io/doc/#tag/BillingGroup/operation/BillingGroupProjectList",
+ "columns": {
+ "billing_group_id": "Billing group the project is billed under (injected from the parent).",
+ "project_name": "Name of the project billed under this billing group.",
+ "estimated_balance": "Estimated unbilled balance for the project in the current period.",
+ "available_credits": "Credits available to offset the project's charges.",
+ },
+ },
"billing_groups": {
"description": "A billing group that aggregates charges for one or more projects.",
"docs_url": "https://api.aiven.io/doc/#tag/BillingGroup",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/settings.py
index 8c63daf46ef5..7e22f3b86ce2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/settings.py
@@ -1,6 +1,8 @@
-from dataclasses import dataclass, field
+from dataclasses import field
from typing import Literal, Optional
+from posthog.dataclasses import frozen
+
from products.warehouse_sources.backend.types import IncrementalField
# How an endpoint's request URLs are derived:
@@ -8,15 +10,34 @@ from products.warehouse_sources.backend.types import IncrementalField
# "project" -> one request per project the token can see
# "organization" -> one request per organization the token can see
# "invoice" -> one request per (organization, invoice) pair (two-level fan-out)
-FanOut = Literal["none", "project", "organization", "invoice"]
+# "two_level" -> organization -> intermediate list -> child, driven by ``two_level`` below
+FanOut = Literal["none", "project", "organization", "invoice", "two_level"]
+
+
+@frozen
+class TwoLevelFanOut:
+ """Describes a generic organization -> intermediate-list -> child fan-out.
+
+ Organizations are the top parent. ``intermediate_path`` (carrying ``{organization_id}``) is
+ fetched once per organization; the endpoint's ``path_template`` is then fetched once per
+ intermediate row, binding each placeholder in ``child_params`` from that row.
+ """
+ intermediate_name: str
+ intermediate_path: str
+ intermediate_data_key: str
+ # child path placeholder -> field on the intermediate row it resolves from. The resolved value
+ # is also stamped onto each child row so composite primary keys stay unique table-wide.
+ child_params: dict[str, str]
-@dataclass
+
+@frozen
class AivenEndpointConfig:
name: str
fan_out: FanOut
# Relative path appended to the API base. Fan-out endpoints carry ``{project}``,
- # ``{organization_id}`` and/or ``{invoice_number}`` placeholders filled in per parent.
+ # ``{organization_id}``, ``{invoice_number}``, ``{user_group_id}`` and/or ``{billing_group_id}``
+ # placeholders filled in per parent.
path_template: str
# Response wrapper key: Aiven list endpoints return ``{"<data_key>": [...]}``.
data_key: str
@@ -25,6 +46,12 @@ class AivenEndpointConfig:
# field, which would rewrite partitions on every sync.
partition_key: Optional[str] = None
should_sync_default: bool = True
+ # Only for ``fan_out="two_level"``: the intermediate list to fan out through.
+ two_level: Optional[TwoLevelFanOut] = None
+ # False for endpoints whose rows carry identity PII (email, real name, or an opaque nested
+ # profile object) that a name-based sample scrubber can't reliably strip — keeps request/response
+ # bodies out of HTTP sample capture. See ``ClientConfig.capture``.
+ capture: bool = True
AIVEN_ENDPOINTS: dict[str, AivenEndpointConfig] = {
@@ -55,6 +82,29 @@ AIVEN_ENDPOINTS: dict[str, AivenEndpointConfig] = {
primary_keys=["project_name", "service_name"],
partition_key="create_time",
),
+ "project_users": AivenEndpointConfig(
+ name="project_users",
+ fan_out="project",
+ path_template="/project/{project}/users",
+ data_key="users",
+ # A user can belong to more than one project, so the injected `project_name` keeps the
+ # membership row unique across projects.
+ primary_keys=["project_name", "user_id"],
+ partition_key="create_time",
+ # Rows carry `user_email` and `real_name` directly.
+ capture=False,
+ ),
+ "project_events": AivenEndpointConfig(
+ name="project_events",
+ fan_out="project",
+ path_template="/project/{project}/events",
+ data_key="events",
+ # Event ids are unique within a project; the injected `project_name` keeps them unique
+ # table-wide across projects.
+ primary_keys=["project_name", "id"],
+ # `time` is the immutable timestamp the event happened at — a safe partition key.
+ partition_key="time",
+ ),
# Fan out one request per organization.
"billing_groups": AivenEndpointConfig(
name="billing_groups",
@@ -92,6 +142,41 @@ AIVEN_ENDPOINTS: dict[str, AivenEndpointConfig] = {
primary_keys=["organization_id", "user_group_id"],
partition_key="create_time",
),
+ # Two-level fan-out: per organization, per user group. Membership rows for the user_groups table.
+ "user_group_members": AivenEndpointConfig(
+ name="user_group_members",
+ fan_out="two_level",
+ path_template="/organization/{organization_id}/user-groups/{user_group_id}/members",
+ data_key="members",
+ # A member row is unique per (organization, group, user); the group and org are injected
+ # from the parents so the key stays unique table-wide.
+ primary_keys=["organization_id", "user_group_id", "user_id"],
+ two_level=TwoLevelFanOut(
+ intermediate_name="user_groups",
+ intermediate_path="/organization/{organization_id}/user-groups",
+ intermediate_data_key="user_groups",
+ child_params={"organization_id": "organization_id", "user_group_id": "user_group_id"},
+ ),
+ # `user_info` is an opaque nested object carrying the member's profile details.
+ capture=False,
+ ),
+ # Two-level fan-out: per organization, per billing group. Maps projects to the billing group
+ # they are billed under, which attributes invoice_lines back to projects.
+ "billing_group_projects": AivenEndpointConfig(
+ name="billing_group_projects",
+ fan_out="two_level",
+ path_template="/billing-group/{billing_group_id}/projects",
+ data_key="projects",
+ # The row is the (billing_group, project) mapping; the injected billing_group_id keeps it
+ # unique table-wide since a project name repeats across billing groups' project lists.
+ primary_keys=["billing_group_id", "project_name"],
+ two_level=TwoLevelFanOut(
+ intermediate_name="billing_groups",
+ intermediate_path="/organization/{organization_id}/billing-groups",
+ intermediate_data_key="billing_groups",
+ child_params={"billing_group_id": "billing_group_id"},
+ ),
+ ),
# Two-level fan-out: per organization, per invoice. Aiven markets these per-line cost rows
# as the canonical way to export billing into a warehouse/BI tool.
"invoice_lines": AivenEndpointConfig(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/tests/test_aiven.py b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/tests/test_aiven.py
index 17f9f73f9ff7..b10b1d324fce 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/tests/test_aiven.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/aiven/tests/test_aiven.py
@@ -158,6 +158,38 @@ class TestFanOut:
{"line_type": "credit", "organization_id": "org1", "invoice_number": "inv2"},
]
+ @patch(CLIENT_SESSION_PATCH)
+ def test_two_level_injects_both_org_and_group(self, MockSession: MagicMock) -> None:
+ # user_group_members fans out organization -> user-groups -> members; the child rows carry
+ # neither id, so both must be injected to keep the composite key unique table-wide.
+ session = MockSession.return_value
+ _wire(
+ session,
+ {
+ "/organizations": _response(200, {"organizations": [{"organization_id": "org1"}]}),
+ "/organization/org1/user-groups": _response(200, {"user_groups": [{"user_group_id": "g1"}]}),
+ "/organization/org1/user-groups/g1/members": _response(200, {"members": [{"user_id": "u1"}]}),
+ },
+ )
+ assert _rows("user_group_members") == [
+ {"user_id": "u1", "organization_id": "org1", "user_group_id": "g1"},
+ ]
+
+ @patch(CLIENT_SESSION_PATCH)
+ def test_two_level_single_placeholder_injects_only_declared_param(self, MockSession: MagicMock) -> None:
+ # billing_group_projects binds only {billing_group_id}, so only that id is injected — the
+ # grandparent organization_id must not leak onto the row.
+ session = MockSession.return_value
+ _wire(
+ session,
+ {
+ "/organizations": _response(200, {"organizations": [{"organization_id": "org1"}]}),
+ "/organization/org1/billing-groups": _response(200, {"billing_groups": [{"billing_group_id": "bg1"}]}),
+ "/billing-group/bg1/projects": _response(200, {"projects": [{"project_name": "p1"}]}),
+ },
+ )
+ assert _rows("billing_group_projects") == [{"project_name": "p1", "billing_group_id": "bg1"}]
+
@patch(CLIENT_SESSION_PATCH)
def test_empty_child_batches_are_not_yielded(self, MockSession: MagicMock) -> None:
session = MockSession.return_value