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): trace source catalog construction (#107314)

Paul D'Ambra51b6145aece6
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source/connection_options.py b/products/warehouse_sources/backend/presentation/views/external_data_source/connection_options.py
index b7fbe51aaaac..1c9c9b236902 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source/connection_options.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source/connection_options.py
@@ -7,6 +7,7 @@ from typing import Any, cast
from django.db.models import Q
from drf_spectacular.utils import extend_schema, extend_schema_field
+from opentelemetry import trace
from rest_framework import serializers, status
from rest_framework.request import Request
from rest_framework.response import Response
@@ -37,6 +38,8 @@ from products.warehouse_sources.backend.presentation.views.public_source_configs
from . import base, helpers
+tracer = trace.get_tracer(__name__)
+
class ExternalDataSourceRevenueAnalyticsConfigSerializer(serializers.ModelSerializer):
class Meta:
@@ -236,14 +239,16 @@ class ExternalDataSourceConnectionOptionsMixin(base.ExternalDataSourceViewSetBas
def direct_connection_options(self, request: Request, *args: Any, **kwargs: Any) -> Response:
"""Source types the user can add as a direct connection, driven by the direct-SQL capability
surface so the picker never drifts from the engines we actually support."""
- direct_types = direct_capable_source_types()
+ with tracer.start_as_current_span("warehouse_sources.direct_connection_options.catalog"):
+ direct_types = direct_capable_source_types()
+ configs = build_source_configs(include_tables=False)
options = [
{
"source_type": source_type,
"label": config.get("label") or source_type,
"icon_path": config.get("iconPath"),
}
- for source_type, config in build_source_configs(include_tables=False).items()
+ for source_type, config in configs.items()
if source_type in direct_types
]
options.sort(key=lambda option: str(option["label"]).lower())
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source/job_runs.py b/products/warehouse_sources/backend/presentation/views/external_data_source/job_runs.py
index dff191deb4de..b73ab6ee42c5 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source/job_runs.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source/job_runs.py
@@ -8,6 +8,7 @@ from django.utils.cache import patch_cache_control
from dateutil import parser
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_field
+from opentelemetry import trace
from rest_framework import serializers, status
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request
@@ -24,6 +25,14 @@ from products.warehouse_sources.backend.presentation.views.public_source_configs
from . import base
+tracer = trace.get_tracer(__name__)
+
+
+class WizardResponse(Response):
+ def render(self) -> Response:
+ with tracer.start_as_current_span("warehouse_sources.wizard.render_response"):
+ return super().render()
+
class ExternalDataJobSerializers(serializers.ModelSerializer):
schema = serializers.SerializerMethodField(read_only=True)
@@ -193,7 +202,8 @@ class ExternalDataSourceJobRunsMixin(base.ExternalDataSourceViewSetBase):
def wizard(self, request: Request, *arg: Any, **kwargs: Any):
# The documented-tables catalog is only consumed by the posthog.com docs build (via the
# public endpoint) — skipping it here cuts ~40% off an already >1 MB response.
- configs = build_source_configs(include_tables=False)
+ with tracer.start_as_current_span("warehouse_sources.wizard.catalog"):
+ configs = build_source_configs(include_tables=False)
requested = request.query_params.get("source_type")
if requested:
@@ -209,7 +219,7 @@ class ExternalDataSourceJobRunsMixin(base.ExternalDataSourceViewSetBase):
)
configs = {st: config for st, config in configs.items() if st in requested_types}
- response = Response(status=status.HTTP_200_OK, data=configs)
+ response = WizardResponse(status=status.HTTP_200_OK, data=configs)
# The catalog is deploy-static and identical for every user (no team/user input), so let the
# browser reuse it across navigations instead of re-downloading and re-parsing several hundred
# KB on each visit to the new-source page. `private` because the route is auth-gated; a new
diff --git a/products/warehouse_sources/backend/presentation/views/public_source_configs.py b/products/warehouse_sources/backend/presentation/views/public_source_configs.py
index 52f867e23904..7f2eb70c2402 100644
--- a/products/warehouse_sources/backend/presentation/views/public_source_configs.py
+++ b/products/warehouse_sources/backend/presentation/views/public_source_configs.py
@@ -2,6 +2,7 @@ import functools
import structlog
from drf_spectacular.utils import extend_schema
+from opentelemetry import trace
from rest_framework import permissions, status, viewsets
from rest_framework.request import Request
from rest_framework.response import Response
@@ -10,6 +11,7 @@ from products.warehouse_sources.backend.facade.source_config import SourceConfig
from products.warehouse_sources.backend.facade.source_management import SourceRegistry
logger = structlog.get_logger(__name__)
+tracer = trace.get_tracer(__name__)
@functools.cache
@@ -25,27 +27,31 @@ def build_source_configs(*, include_tables: bool = True) -> dict[str, dict]:
The catalog is deploy-static (it only changes when source code ships), so the result is
memoized per process — callers must treat it as read-only.
"""
- sources = SourceRegistry.get_all_sources()
+ with tracer.start_as_current_span("warehouse_sources.catalog.registry"):
+ sources = SourceRegistry.get_all_sources()
results: dict[str, dict] = {}
- for source_type, source in sources.items():
- config = source.get_source_config.model_dump()
- config["supportsColumnSelection"] = bool(source.supports_column_selection)
- config["versions"] = list(source.supported_versions)
- config["defaultVersion"] = source.default_version
- config["apiDocsUrl"] = source.api_docs_url
- config["deprecatedVersions"] = [
- {"version": d.version, "sunsetAt": d.sunset_at.isoformat() if d.sunset_at else None}
- for d in source.deprecated_versions
- ]
- if include_tables:
- # Per-source guard: a single misbehaving source must never break the whole catalog.
- try:
- config["tables"] = source.get_documented_tables()
- except Exception:
- logger.exception("build_source_configs: get_documented_tables failed", source_type=str(source_type))
- config["tables"] = []
- results[str(source_type)] = config
+ with tracer.start_as_current_span("warehouse_sources.catalog.build") as span:
+ span.set_attribute("include_tables", include_tables)
+ span.set_attribute("source_count", len(sources))
+ for source_type, source in sources.items():
+ config = source.get_source_config.model_dump()
+ config["supportsColumnSelection"] = bool(source.supports_column_selection)
+ config["versions"] = list(source.supported_versions)
+ config["defaultVersion"] = source.default_version
+ config["apiDocsUrl"] = source.api_docs_url
+ config["deprecatedVersions"] = [
+ {"version": d.version, "sunsetAt": d.sunset_at.isoformat() if d.sunset_at else None}
+ for d in source.deprecated_versions
+ ]
+ if include_tables:
+ # Per-source guard: a single misbehaving source must never break the whole catalog.
+ try:
+ config["tables"] = source.get_documented_tables()
+ except Exception:
+ logger.exception("build_source_configs: get_documented_tables failed", source_type=str(source_type))
+ config["tables"] = []
+ results[str(source_type)] = config
return results
diff --git a/products/warehouse_sources/backend/tests/api/test_public_source_configs.py b/products/warehouse_sources/backend/tests/api/test_public_source_configs.py
index 65f8607a302b..33ffbf06737f 100644
--- a/products/warehouse_sources/backend/tests/api/test_public_source_configs.py
+++ b/products/warehouse_sources/backend/tests/api/test_public_source_configs.py
@@ -1,11 +1,27 @@
from posthog.test.base import APIBaseTest
+from unittest.mock import patch
from rest_framework import status
from products.warehouse_sources.backend.facade.source_config import SourceConfigMapResponse
+from products.warehouse_sources.backend.presentation.views.public_source_configs import build_source_configs, tracer
class TestPublicSourceConfigs(APIBaseTest):
+ def test_catalog_spans_only_cover_cache_misses(self):
+ build_source_configs.cache_clear()
+ try:
+ with patch.object(tracer, "start_as_current_span") as start_span:
+ cold = build_source_configs(include_tables=False)
+ assert build_source_configs(include_tables=False) is cold
+
+ assert [call.args[0] for call in start_span.call_args_list] == [
+ "warehouse_sources.catalog.registry",
+ "warehouse_sources.catalog.build",
+ ]
+ finally:
+ build_source_configs.cache_clear()
+
def test_list_returns_source_configs(self):
response = self.client.get("/api/public_source_configs/")
assert response.status_code == status.HTTP_200_OK