Skip to content

Latest commit

 

History

History
90 lines (63 loc) · 7.72 KB

architecture.md

File metadata and controls

90 lines (63 loc) · 7.72 KB

Architecture

Traffic Analytics is a web analytics proxy for Ghost. It receives page-hit events from the ghost-stats.js browser script, enriches them (user-agent parsing, referrer parsing, and a privacy-preserving user signature), filters bot traffic, and forwards the result to Tinybird's /v0/events endpoint for storage in ClickHouse.

Run modes

The same Docker image runs in three roles, selected by the WORKER_MODE environment variable in server.ts:

  • Ingest app (WORKER_MODE unset — src/app.ts) — the Fastify HTTP server that receives POST /api/v1/page_hit.
  • Worker app (WORKER_MODE=true — src/worker-app.ts) — a Pub/Sub consumer that enriches events and forwards them to Tinybird. It exposes only health endpoints (/ and /health) for Cloud Run.
  • Tinybird sync worker (WORKER_MODE=tinybird-sync — src/tinybird-sync-worker-app.ts) — consumes Tinybird sync events from Pub/Sub, buffers each event type separately, and sends batches to their mapped Tinybird datasources.

The ingest app has two request-handling strategies (see src/handlers/page-hit-handlers.ts), chosen by whether PUBSUB_TOPIC_PAGE_HITS_RAW is set:

  • Batch mode (default in dev and production) — publish events to Pub/Sub and return 202 immediately. Page-hit enrichment and forwarding happen in worker; Tinybird sync batching and forwarding happen in tinybird-sync. This is the dev:batch Compose profile: analytics-service + worker + tinybird-sync.
  • Proxy mode (synchronous) — no topic set. The request is enriched inline and proxied straight to PROXY_TARGET (/v0/events) within the same request/response cycle. This is the dev:proxy Compose profile: analytics-service-proxy.

Batch pipeline

flowchart LR
    Browser["Browser<br/>ghost-stats.js"]

    subgraph Ingest["Ingest app (src/app.ts)"]
        HMAC["HMAC validation<br/>(preValidation)"]
        Validate["Schema validation"]
        Bots["Filter bot traffic<br/>(preHandler)"]
        Pre["Populate request defaults<br/>(route preHandler)"]
        Pub["Publish raw event"]
    end

    Topic(["Pub/Sub topic<br/>PUBSUB_TOPIC_PAGE_HITS_RAW"])
    Sub(["Pub/Sub subscription<br/>PUBSUB_SUBSCRIPTION_PAGE_HITS_RAW"])

    subgraph Worker["Worker app (src/worker-app.ts)"]
        Enrich["Enrich:<br/>user agent + referrer<br/>+ user signature"]
        BotGuard["Defensive bot check"]
        Batch["Batch<br/>(BATCH_SIZE / flush interval)"]
    end

    TB["Tinybird<br/>POST /v0/events"]

    Browser -->|"POST /api/v1/page_hit"| HMAC
    HMAC --> Validate --> Bots
    Bots -->|"bot: 202 Accepted"| Browser
    Bots -->|"not bot"| Pre --> Pub
    Pub -->|"202 Accepted"| Browser
    Pub --> Topic --> Sub --> Enrich
    Enrich --> BotGuard --> Batch --> TB
Loading

Notes:

  • HMAC validation (src/plugins/hmac-validation.ts) is a global preValidation hook. It validates the signature and timestamp carried in the URL query parameters and strips them before downstream processing. It is skipped entirely if HMAC_SECRET is unset, and it can be run in log-only mode with HMAC_VALIDATION_LOG_ONLY=true.
  • Enrichment (transformPageHitRawToProcessed) parses the user agent with ua-parser-js, parses the referrer with @tryghost/referrer-parser, and computes the session_id user signature.
  • Bot filtering runs in the ingest app's bot-detection preHandler hook before the request strategy is selected, so bot events return the standard 202 accepted response without being published to Pub/Sub or proxied to Tinybird. Set ENABLE_BOT_DETECTION_HEADER=true to include x-ghost-bot-detected: true on these responses; the header is omitted by default. The worker retains a defensive check for legacy or directly published messages that bypassed the API.
  • Batching (src/services/batch-worker/BatchWorker.ts) accumulates processed events and flushes them to Tinybird as newline-delimited JSON when the batch reaches BATCH_SIZE (default 50) or the flush timer fires (BATCH_FLUSH_INTERVAL_MS, default 1000ms). Messages are acked on a successful flush and nacked on failure.
  • Tinybird sync batching keeps separate buffers for automation runs and run steps. It flushes when a buffer reaches TINYBIRD_SYNC_BATCH_SIZE (default 50) or after TINYBIRD_SYNC_BATCH_FLUSH_INTERVAL_MS (default 1000ms).

Synchronous proxy mode

When PUBSUB_TOPIC_PAGE_HITS_RAW is not set, the ingest app enriches the request inline and proxies it directly to PROXY_TARGET using @fastify/reply-from, returning Tinybird's response to the caller. There is no Pub/Sub topic and no separate worker in this mode.

Pub/Sub

  • The publisher (src/services/events/publisher.ts) and subscriber (src/services/events/subscriber.ts) both use @google-cloud/pubsub and read GOOGLE_CLOUD_PROJECT for the project ID.
  • In local development and tests, a Pub/Sub emulator is used via PUBSUB_EMULATOR_HOST. The Compose stack creates the topic traffic-analytics-page-hits-raw and subscription traffic-analytics-page-hits-raw-sub.

Salt store

User signatures depend on a daily-rotating salt, stored via an adapter pattern behind ISaltStore and selected by SALT_STORE_TYPE (see SaltStoreFactory.ts):

  • memory — in-process, lost on restart. This is the code default.
  • file — JSON file at SALT_STORE_FILE_PATH (default ./data/salts.json).
  • firestore — Google Cloud Firestore; requires GOOGLE_CLOUD_PROJECT and FIRESTORE_DATABASE_ID. This is what Docker Compose dev/test use (via the Firestore emulator at FIRESTORE_EMULATOR_HOST) and what production uses.

The user signature (UserSignatureService) is a SHA-256 hash of salt : site_uuid : ip : user-agent, where the salt is keyed by date and site UUID so it rotates daily per site. A cleanup scheduler periodically deletes expired salts (ENABLE_SALT_CLEANUP_SCHEDULER, FIRESTORE_CLEANUP_BATCH_SIZE).

Observability

OpenTelemetry is initialised in src/utils/instrumentation.ts before the app loads:

  • The trace exporter is Jaeger (OTLP HTTP) by default, or Google Cloud Trace when OTEL_TRACE_EXPORTER=gcp or K_SERVICE is set (Cloud Run sets K_SERVICE automatically).
  • Google Cloud Trace uses OTLP HTTP/protobuf at https://telemetry.googleapis.com/v1/traces. Application Default Credentials supply refreshed authentication headers and the gcp.project_id resource attribute. Before deploying, enable telemetry.googleapis.com and grant the runtime service account roles/telemetry.tracesWriter on the destination project. If ADC specifies a quota project, grant roles/serviceusage.serviceUsageConsumer there too. Send a staging request and confirm its trace arrives in Cloud Trace before promoting. See Google's migration guide.
  • OTLP endpoints default to the jaeger service (OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT).
  • The reported service name is analytics-worker in batch worker mode, analytics-tinybird-sync-worker in Tinybird sync mode, and analytics-service otherwise.

Related docs

  • Deployment — CI and the deploy pipeline.
  • AGENTS.md — module map, commands, and environment variables for contributors and coding agents.