: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.fastly._config import FastlySourceConfig
from products.warehouse_sources.sources.fastly.fastly import (
FastlyResumeConfig,
fastly_source,
validate_credentials as validate_fastly_credentials,
)
from products.warehouse_sources.sources.fastly.settings import ENDPOINTS, FASTLY_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 FastlySource(ResumableSource[FastlySourceConfig, FastlyResumeConfig]):
codebase_detection = detect(
env=("^FASTLY_",),
npm=("fastly", "@fastly/js-compute"),
pypi=("fastly",),
cargo=("fastly", "fastly-kv-preview", "fastly-macros", "fastly-shared", "fastly-sys", "log-fastly"),
composer=("fastly/fastly",),
go=("github.com/fastly/go-fastly", "github.com/fastly/fastly-exporter", "github.com/fastly/fastly-go"),
rubygems=("fastly", "fastly-rails"),
)
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
api_docs_url = "https://www.fastly.com/documentation/reference/api/"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.FASTLY
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.FASTLY,
category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
label="Fastly",
releaseStatus=ReleaseStatus.BETA,
caption="""Enter your Fastly API token to automatically pull your Fastly account and service configuration into the PostHog Data warehouse.
You can create an API token in your [Fastly account settings](https://manage.fastly.com/account/personal/tokens).
A read-only token with **global** scope is sufficient to sync every table.""",
iconPath="/static/services/fastly.png",
docsUrl="https://posthog.com/docs/cdp/sources/fastly",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_key",
label="API token",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.fastly.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# A missing, invalid, or under-scoped Fastly token surfaces as an HTTPError when
# `_fetch` calls `raise_for_status()`. Retrying can never satisfy a credential
# problem. Match the stable status text and base host, not the per-request path.
"401 Client Error: Unauthorized for url: https://api.fastly.com": "Your Fastly API token is missing or invalid. Create a new token in your Fastly account settings, then reconnect.",
"403 Client Error: Forbidden for url: https://api.fastly.com": "Your Fastly API token does not have the scope needed to sync this data. Grant it read access, then reconnect.",
}
def get_schemas(
self,
config: FastlySourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
# Only the metrics endpoints take a server-side time filter. Every config endpoint is full
# refresh only, partitioned on the stable `created_at` each object carries. Append is never
# offered: Fastly revises a reported bucket after the fact, which append would duplicate.
def _build_schema(endpoint: str) -> SourceSchema:
endpoint_config = FASTLY_ENDPOINTS[endpoint]
return SourceSchema(
name=endpoint,
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,
default_incremental_lookback_seconds=endpoint_config.incremental_lookback_seconds,
)
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: FastlySourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
if validate_fastly_credentials(config.api_key):
return True, None
return False, "Invalid Fastly API token"
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[FastlyResumeConfig]:
return ResumableSourceManager[FastlyResumeConfig](inputs, FastlyResumeConfig)
def source_for_pipeline(
self,
config: FastlySourceConfig,
resumable_source_manager: ResumableSourceManager[FastlyResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return fastly_source(
api_key=config.api_key,
endpoint=inputs.schema_name,
logger=inputs.logger,
resumable_source_manager=resumable_source_manager,
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value,
)