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.

6.1 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.kernel._config import KernelSourceConfig
from products.warehouse_sources.sources.kernel.kernel import (
    KernelResumeConfig,
    kernel_source,
    validate_credentials as validate_kernel_credentials,
)
from products.warehouse_sources.sources.kernel.settings import ENDPOINTS, KERNEL_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class KernelSource(ResumableSource[KernelSourceConfig, KernelResumeConfig]):
    codebase_detection = detect(
        env=("^KERNEL_API_KEY$", "^KERNEL_ORG_API_KEY$"),
        npm=("@onkernel/sdk",),
        pypi=("kernel",),
        go=("github.com/kernel/kernel-go-sdk",),
    )

    lists_tables_without_credentials = True  # static endpoint catalog - safe for public docs
    api_docs_url = "https://onkernel.com/docs/"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.KERNEL

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.KERNEL,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Kernel",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Kernel API key to sync your Kernel browser-infrastructure data into the PostHog Data warehouse.

You can create an API key in your [Kernel dashboard](https://dashboard.onkernel.com) under API keys. Kernel keys are long-lived and grant organization-wide read access.""",
            iconPath="/static/services/kernel.png",
            docsUrl="https://posthog.com/docs/cdp/sources/kernel",
            keywords=["browser", "automation", "agents", "infrastructure"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="sk_...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.kernel.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.onkernel.com": "Your Kernel API key is invalid or has been revoked. Create a new key in your Kernel dashboard, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.onkernel.com": "Your Kernel API key does not have access to this data. Check the key's permissions in your Kernel dashboard, then reconnect.",
        }

    def get_schemas(
        self,
        config: KernelSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _build_schema(endpoint: str) -> SourceSchema:
            endpoint_config = KERNEL_ENDPOINTS[endpoint]
            return SourceSchema(
                name=endpoint,
                # Only audit_logs has a server-side time filter (its required `start`). The other
                # endpoints stay full refresh, since their time filters are unverified for this
                # alpha release. Append is not offered: an incremental audit-log sync re-reads
                # the records at the watermark, which merge dedupes and append would duplicate.
                supports_incremental=bool(endpoint_config.incremental_fields),
                supports_append=False,
                incremental_fields=endpoint_config.incremental_fields,
                should_sync_default=endpoint_config.should_sync_default,
                description=endpoint_config.description,
            )

        schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]
        return schemas

    def validate_credentials(
        self,
        config: KernelSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        ok, status = validate_kernel_credentials(config.api_key)
        if ok:
            return True, None

        # A valid token that lacks scope for one endpoint must not block source creation - the user
        # may only intend to sync the tables they can reach. Only reject 403 when probing a specific
        # schema; sync-time 403s are handled by get_non_retryable_errors().
        if status == 403 and schema_name is None:
            return True, None

        return False, "Invalid Kernel API key"

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[KernelResumeConfig]:
        return ResumableSourceManager[KernelResumeConfig](inputs, KernelResumeConfig)

    def source_for_pipeline(
        self,
        config: KernelSourceConfig,
        resumable_source_manager: ResumableSourceManager[KernelResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return kernel_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=(
                inputs.db_incremental_field_last_value if inputs.should_use_incremental_field else None
            ),
        )