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 modal app deployments and workspace members tables (#123049)

Tom Owers721783def30a
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 975cfc7fc1f4..a7ea3ce7e27a 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
@@ -8117,8 +8117,8 @@ Diffed against: <https://raw.githubusercontent.com/modal-labs/modal-client/main/
- [x] `Workspace billing summary (SDK RPC WorkspaceBillingSummary, Workspace.billing.summary)` — reconcile metered cost with invoiced cost, credits, and other adjustments (high)
- [ ] `Function statistics (SDK RPC FunctionGetTimeRangeStats, Function.stats)` — measure successful calls, failures, timeouts, latency, and resource utilization, which billing costs cannot show (high)
- [x] `Sandboxes (SDK RPC SandboxList/SandboxListV2, Sandbox.list)` — resolve sandbox IDs in billing reports and compare sandbox populations across applications (high)
-- [ ] `App deployment history (SDK RPC AppDeploymentHistory)` — connect deployment revisions with cost changes, failed calls, and incident periods (medium)
-- [ ] `Workspace members (SDK RPC WorkspaceMembersList, Workspace.members.list)` — join application creators with workspace member identities for ownership and adoption reports (medium)
+- [x] `App deployment history (SDK RPC AppDeploymentHistory)` — connect deployment revisions with cost changes, failed calls, and incident periods (medium)
+- [x] `Workspace members (SDK RPC WorkspaceMembersList, Workspace.members.list)` — join application creators with workspace member identities for ownership and adoption reports (medium)
- [ ] `App logs (SDK RPC AppFetchLogs, App.logs.fetch)` — analyze execution errors and runtime activity by function, container, and time (medium)
Note: Compared the official SDK protobuf service and its documented read methods. Both current tables call Workspace.billing.report at different time resolutions. App and sandbox collections resolve billing object IDs; statistics, history, and logs need requests for each object. Billing summaries include credits and adjustments, and billing access requires an eligible plan.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/canonical_descriptions.py
index 04e86552b45b..a21e687943de 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/canonical_descriptions.py
@@ -59,3 +59,37 @@ CANONICAL_DESCRIPTIONS["sandboxes"] = {
"tags": "User-defined tags of the Sandbox.",
},
}
+CANONICAL_DESCRIPTIONS["app_deployments"] = {
+ "description": "Deployment history of each deployed Modal App, one row per deployed version.",
+ "docs_url": "https://modal.com/docs/cli/latest/app#modal-app-history",
+ "columns": {
+ "app_id": "Identifier of the App.",
+ "app_name": "Name of the App.",
+ "environment_name": "Name of the Modal environment that contains the App.",
+ "version": "Version number of the deployment.",
+ "is_production": "Whether this version serves the App at sync time.",
+ "deployed_at": "Time when the version was deployed.",
+ "deployed_by": "Workspace member who deployed the version.",
+ "client_version": "Version of the Modal client that deployed the version.",
+ "tag": "Tag given to the deployment.",
+ "deployment_type": "How the version was created, such as standard or rollback.",
+ "rollback_version": "Version that a rollback deployment restored.",
+ "commit_hash": "Git commit of the deployed code.",
+ "commit_dirty": "Whether the working tree had uncommitted changes at deploy time.",
+ "committed_at": "Time of the Git commit of the deployed code.",
+ },
+}
+CANONICAL_DESCRIPTIONS["workspace_members"] = {
+ "description": "Members of the Modal workspace and their roles.",
+ "docs_url": "https://modal.com/docs/sdk/py/latest/Workspace#memberslist",
+ "columns": {
+ "user_id": "Identifier of the Modal user.",
+ "name": "Display name of the member.",
+ "email": "Email address of the member.",
+ "role": "Role of the member in the workspace, such as user, manager, or owner.",
+ "identity_provider": "Identity provider the member signs in with, such as GitHub or SAML.",
+ "joined_at": "Time when the member joined the workspace.",
+ "last_active_at": "Time when the member was last active.",
+ "deleted_at": "Time when the member was removed from the workspace.",
+ },
+}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/modal.py b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/modal.py
index 739a1359d7a7..3dbbbf951401 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/modal.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/modal.py
@@ -2,14 +2,16 @@ from collections.abc import Iterator
from contextlib import contextmanager
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING
+from urllib.parse import urlsplit, urlunsplit
import modal
from google.protobuf.descriptor import EnumDescriptor
+from google.protobuf.empty_pb2 import Empty
from grpclib import GRPCError, Status
from modal._utils.async_utils import synchronizer
from modal.client import _Client
from modal.environments import list_environments
-from modal.exception import AuthError, PermissionDeniedError
+from modal.exception import AuthError, NotFoundError, PermissionDeniedError
from modal_proto import api_pb2
from posthog.dataclasses import frozen
@@ -17,6 +19,7 @@ from posthog.dataclasses import frozen
from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import schema_for_resource
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
from products.warehouse_sources.backend.temporal.data_imports.sources.modal.settings import (
+ APP_DEPLOYMENTS,
APPS,
BILLING_REPORT_PARTITION_KEY,
BILLING_REPORT_PRIMARY_KEYS,
@@ -24,6 +27,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.modal.sett
BILLING_SUMMARY,
BILLING_SUMMARY_DEFAULT_CYCLES,
SANDBOXES,
+ WORKSPACE_MEMBERS,
)
if TYPE_CHECKING:
@@ -99,8 +103,19 @@ async def _list_sandbox_page(
return list(response.sandboxes)
+async def _get_deployment_history(client: _Client, app_id: str) -> api_pb2.AppDeploymentHistoryResponse:
+ return await client.stub.AppDeploymentHistory(api_pb2.AppDeploymentHistoryRequest(app_id=app_id))
+
+
+async def _list_workspace_members(client: _Client) -> list[api_pb2.WorkspaceMembersListItem]:
+ response = await client.stub.WorkspaceMembersList(Empty())
+ return list(response.members)
+
+
_list_apps_blocking = synchronizer.create_blocking(_list_apps)
_list_sandbox_page_blocking = synchronizer.create_blocking(_list_sandbox_page)
+_get_deployment_history_blocking = synchronizer.create_blocking(_get_deployment_history)
+_list_workspace_members_blocking = synchronizer.create_blocking(_list_workspace_members)
def list_apps(client: modal.Client, environment_name: str) -> list[api_pb2.AppListResponse.AppListItem]:
@@ -113,6 +128,14 @@ def list_sandbox_page(
return _list_sandbox_page_blocking(client, app_id, environment_name, before_timestamp)
+def get_deployment_history(client: modal.Client, app_id: str) -> api_pb2.AppDeploymentHistoryResponse:
+ return _get_deployment_history_blocking(client, app_id)
+
+
+def list_workspace_members(client: modal.Client) -> list[api_pb2.WorkspaceMembersListItem]:
+ return _list_workspace_members_blocking(client)
+
+
@contextmanager
def billing_client(config: ModalSourceConfig) -> Iterator[modal.Client]:
try:
@@ -159,6 +182,10 @@ def modal_source(
return apps_source(config, inputs, resumable_source_manager)
if inputs.schema_name == SANDBOXES:
return sandboxes_source(config, inputs, resumable_source_manager)
+ if inputs.schema_name == APP_DEPLOYMENTS:
+ return app_deployments_source(config, inputs, resumable_source_manager)
+ if inputs.schema_name == WORKSPACE_MEMBERS:
+ return workspace_members_source(config, inputs)
return billing_report_source(config, inputs, resumable_source_manager)
@@ -396,3 +423,117 @@ def sandboxes_source(
partition_mode="datetime",
partition_format="month",
)
+
+
+# Apps in these states come from `modal run` or `modal serve`, which never deploy.
+UNDEPLOYED_APP_STATES = frozenset(
+ {
+ api_pb2.APP_STATE_EPHEMERAL,
+ api_pb2.APP_STATE_DETACHED,
+ api_pb2.APP_STATE_DETACHED_DISCONNECTED,
+ }
+)
+
+
+def strip_url_credentials(url: str) -> str:
+ # The SDK copies the Git origin verbatim, so an HTTPS origin can carry an access token.
+ parts = urlsplit(url)
+ if not parts.scheme or "@" not in parts.netloc:
+ return url
+ return urlunsplit(parts._replace(netloc=parts.netloc.rpartition("@")[2]))
+
+
+def deployment_row(
+ deployment: api_pb2.AppDeploymentHistory,
+ app: api_pb2.AppListResponse.AppListItem,
+ environment_name: str,
+ production_version: int,
+) -> dict[str, object]:
+ has_commit = deployment.HasField("commit_info")
+ commit = deployment.commit_info
+ return {
+ "app_id": app.app_id,
+ "app_name": app.name or None,
+ "environment_name": environment_name,
+ "version": deployment.version,
+ "is_production": deployment.version == production_version,
+ "deployed_at": from_epoch(deployment.deployed_at),
+ "deployed_by": deployment.deployed_by or None,
+ "client_version": deployment.client_version or None,
+ "tag": deployment.tag or None,
+ "deployment_type": enum_label(
+ api_pb2.DeploymentType.DESCRIPTOR, deployment.deployment_type, "DEPLOYMENT_TYPE_"
+ ),
+ "rollback_version": deployment.rollback_version or None,
+ "rollback_allowed": deployment.rollback_allowed,
+ "commit_hash": commit.commit_hash or None,
+ "commit_branch": commit.branch or None,
+ "commit_repo_url": strip_url_credentials(commit.repo_url) or None,
+ "commit_dirty": commit.dirty if has_commit else None,
+ "commit_author_name": commit.author_name or None,
+ "committed_at": from_epoch(commit.commit_timestamp),
+ }
+
+
+def app_deployments_source(
+ config: ModalSourceConfig,
+ inputs: SourceInputs,
+ resumable_source_manager: ResumableSourceManager[ModalResumeConfig],
+) -> SourceResponse:
+ def get_rows() -> Iterator[list[dict[str, object]]]:
+ with billing_client(config) as client:
+ for environment in list_environments(client=client):
+ for app in list_apps(client, environment.name):
+ if app.state in UNDEPLOYED_APP_STATES:
+ continue
+ try:
+ history = get_deployment_history(client, app.app_id)
+ except NotFoundError:
+ # The App can be deleted after the listing.
+ history = None
+ if history and history.app_deployment_histories:
+ yield [
+ deployment_row(deployment, app, environment.name, history.production_app_version)
+ for deployment in history.app_deployment_histories
+ ]
+ resumable_source_manager.safe_point()
+
+ return SourceResponse(
+ name=inputs.schema_name,
+ items=get_rows,
+ primary_keys=["app_id", "version"],
+ column_hints={"deployed_at": "timestamp", "committed_at": "timestamp"},
+ partition_keys=["deployed_at"],
+ partition_mode="datetime",
+ partition_format="month",
+ )
+
+
+def member_row(member: api_pb2.WorkspaceMembersListItem) -> dict[str, object]:
+ return {
+ "user_id": member.user_id,
+ "name": member.member_displayname or None,
+ "email": member.email or None,
+ "role": enum_label(api_pb2.MemberRole.DESCRIPTOR, member.member_role, "MEMBER_ROLE_"),
+ "identity_provider": enum_label(
+ api_pb2.IdentityProviderType.DESCRIPTOR, member.identity_provider_type, "IDENTITY_PROVIDER_TYPE_"
+ ),
+ "joined_at": from_epoch(member.joined_at),
+ "last_active_at": from_epoch(member.last_active_at),
+ "deleted_at": from_epoch(member.deleted_at),
+ }
+
+
+def workspace_members_source(config: ModalSourceConfig, inputs: SourceInputs) -> SourceResponse:
+ def get_rows() -> Iterator[list[dict[str, object]]]:
+ with billing_client(config) as client:
+ members = list_workspace_members(client)
+ if members:
+ yield [member_row(member) for member in members]
+
+ return SourceResponse(
+ name=inputs.schema_name,
+ items=get_rows,
+ primary_keys=["user_id"],
+ column_hints={"joined_at": "timestamp", "last_active_at": "timestamp", "deleted_at": "timestamp"},
+ )
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/settings.py
index 6ded92e7c5a7..0a1e78ad3633 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/settings.py
@@ -37,7 +37,9 @@ BILLING_REPORT_TABLES = {
BILLING_SUMMARY = "billing_summary"
APPS = "apps"
SANDBOXES = "sandboxes"
-ENDPOINTS = (*BILLING_REPORT_TABLES, BILLING_SUMMARY, APPS, SANDBOXES)
+APP_DEPLOYMENTS = "app_deployments"
+WORKSPACE_MEMBERS = "workspace_members"
+ENDPOINTS = (*BILLING_REPORT_TABLES, BILLING_SUMMARY, APPS, SANDBOXES, APP_DEPLOYMENTS, WORKSPACE_MEMBERS)
BILLING_SUMMARY_DEFAULT_CYCLES = 12
INCREMENTAL_FIELDS = {
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/tests/test_modal.py b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/tests/test_modal.py
index 2ed4ceb6df63..120798e5b37e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/modal/tests/test_modal.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/modal/tests/test_modal.py
@@ -10,7 +10,7 @@ from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
import modal
from grpclib import GRPCError, Status
from modal.client import _Client
-from modal.exception import AuthError, PermissionDeniedError, ResourceExhaustedError, ServiceError
+from modal.exception import AuthError, NotFoundError, PermissionDeniedError, ResourceExhaustedError, ServiceError
from modal.types import BillingReportItem, WorkspaceBillingSummary
from modal_proto import api_pb2
@@ -23,6 +23,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.modal.moda
ModalPermissionError,
ModalResumeConfig,
modal_source,
+ strip_url_credentials,
validate_credentials,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.modal.source import ModalSource
@@ -558,3 +559,167 @@ def test_sandbox_row_shape(
"tags": {"team": "example"},
}
]
+
+
+def test_app_deployments_skip_undeployed_and_deleted_apps(
+ config: ModalSourceConfig, inputs: SourceInputs, manager: MagicMock, real_client: modal.Client, stub: MagicMock
+) -> None:
+ inputs.schema_name = "app_deployments"
+ apps = {
+ "main": [
+ app_item("ap-deployed"),
+ app_item("ap-ephemeral", state=api_pb2.APP_STATE_EPHEMERAL),
+ app_item("ap-deleted", state=api_pb2.APP_STATE_STOPPED),
+ ],
+ "dev": [],
+ }
+ stub.AppList = AsyncMock(side_effect=lambda request: api_pb2.AppListResponse(apps=apps[request.environment_name]))
+ deployed_at = NOW.timestamp() - 3600
+ histories = {
+ "ap-deployed": api_pb2.AppDeploymentHistoryResponse(
+ app_deployment_histories=[
+ api_pb2.AppDeploymentHistory(
+ app_id="ap-deployed",
+ version=2,
+ deployed_at=deployed_at,
+ deployed_by="example-user",
+ client_version="1.5.4",
+ deployment_type=api_pb2.DEPLOYMENT_TYPE_ROLLBACK,
+ rollback_version=1,
+ ),
+ api_pb2.AppDeploymentHistory(
+ app_id="ap-deployed",
+ version=1,
+ deployed_at=deployed_at - 60,
+ deployed_by="example-user",
+ deployment_type=api_pb2.DeploymentType.ValueType(99),
+ commit_info=api_pb2.CommitInfo(
+ vcs="git",
+ branch="main",
+ commit_hash="abc123",
+ commit_timestamp=int(deployed_at) - 120,
+ dirty=True,
+ ),
+ ),
+ ],
+ production_app_version=2,
+ ),
+ }
+
+ def deployment_history(request: api_pb2.AppDeploymentHistoryRequest) -> api_pb2.AppDeploymentHistoryResponse:
+ if request.app_id not in histories:
+ raise NotFoundError("App not found")
+ return histories[request.app_id]
+
+ stub.AppDeploymentHistory = AsyncMock(side_effect=deployment_history)
+
+ batches = list(source_items(modal_source(config, inputs, manager)))
+
+ assert [call.args[0].app_id for call in stub.AppDeploymentHistory.call_args_list] == ["ap-deployed", "ap-deleted"]
+ common = {
+ "app_id": "ap-deployed",
+ "app_name": "name-ap-deployed",
+ "environment_name": "main",
+ "deployed_by": "example-user",
+ "tag": None,
+ "rollback_allowed": False,
+ "commit_repo_url": None,
+ "commit_author_name": None,
+ }
+ assert batches == [
+ [
+ {
+ **common,
+ "version": 2,
+ "is_production": True,
+ "deployed_at": NOW - timedelta(hours=1),
+ "client_version": "1.5.4",
+ "deployment_type": "rollback",
+ "rollback_version": 1,
+ "commit_hash": None,
+ "commit_branch": None,
+ "commit_dirty": None,
+ "committed_at": None,
+ },
+ {
+ **common,
+ "version": 1,
+ "is_production": False,
+ "deployed_at": NOW - timedelta(hours=1, minutes=1),
+ "client_version": None,
+ "deployment_type": "99",
+ "rollback_version": None,
+ "commit_hash": "abc123",
+ "commit_branch": "main",
+ "commit_dirty": True,
+ "committed_at": NOW - timedelta(hours=1, minutes=2),
+ },
+ ]
+ ]
+ assert manager.safe_point.call_count == 2
+
+
+ ("url", "expected"),
+ [
+ ("https://x-access-token:[email protected]/example/repo.git", "https://github.com/example/repo.git"),
+ ("https://[email protected]:8443/example/repo", "https://gitlab.example.com:8443/example/repo"),
+ ("https://github.com/example/repo.git", "https://github.com/example/repo.git"),
+ ("[email protected]:example/repo.git", "[email protected]:example/repo.git"),
+ ("", ""),
+ ],
+)
+def test_strip_url_credentials(url: str, expected: str) -> None:
+ assert strip_url_credentials(url) == expected
+
+
+def test_workspace_members(
+ config: ModalSourceConfig, inputs: SourceInputs, manager: MagicMock, real_client: modal.Client, stub: MagicMock
+) -> None:
+ inputs.schema_name = "workspace_members"
+ joined_at = NOW.timestamp() - 86400
+ stub.WorkspaceMembersList = AsyncMock(
+ return_value=api_pb2.WorkspaceMembersListResponse(
+ members=[
+ api_pb2.WorkspaceMembersListItem(
+ user_id="us-one",
+ member_displayname="example-owner",
+ member_role=api_pb2.MEMBER_ROLE_OWNER,
+ identity_provider_type=api_pb2.IDENTITY_PROVIDER_TYPE_GITHUB,
+ joined_at=joined_at,
+ last_active_at=joined_at + 60,
+ ),
+ api_pb2.WorkspaceMembersListItem(
+ user_id="us-two", member_role=api_pb2.MemberRole.ValueType(99), joined_at=joined_at
+ ),
+ ]
+ )
+ )
+
+ batches = list(source_items(modal_source(config, inputs, manager)))
+
+ assert batches == [
+ [
+ {
+ "user_id": "us-one",
+ "name": "example-owner",
+ "email": "[email protected]",
+ "role": "owner",
+ "identity_provider": "github",
+ "joined_at": NOW - timedelta(days=1),
+ "last_active_at": NOW - timedelta(days=1) + timedelta(minutes=1),
+ "deleted_at": None,
+ },
+ {
+ "user_id": "us-two",
+ "name": None,
+ "email": None,
+ "role": "99",
+ "identity_provider": "unspecified",
+ "joined_at": NOW - timedelta(days=1),
+ "last_active_at": None,
+ "deleted_at": None,
+ },
+ ]
+ ]