# tom-draper/api-analytics architecture

> Architecture diagram of the GitHub repository tom-draper/api-analytics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tom-draper/api-analytics
- Repository: https://github.com/tom-draper/api-analytics

## Overview

#### Purpose and Topology
API Analytics is a multi-language API-request telemetry product. Framework middleware captures request/response metadata, sends it to a central Go ingestion service, persists it, and exposes it to a SvelteKit dashboard and data API. It also supports scheduled endpoint monitoring and self-hosted deployment.

#### Instrumentation SDKs
SDKs are framework-specific adapters around common capture-and-submit behavior: measure duration, extract request metadata, apply privacy/user-ID mapping, and emit telemetry keyed by the customer API key. Major implementation families:
- Python shared core and adapters: `analytics/python/python/api_analytics/core.py`, `analytics/python/python/api_analytics/fastapi.py`, `analytics/python/python/api_analytics/flask.py`
- JavaScript framework packages: `analytics/javascript/core/index.js`, `analytics/javascript/express/index.js`, `analytics/nodejs/analytics.js`
- Go core plus router middleware: `analytics/go/core/core.go`, `analytics/go/gin/analytics.go`, `analytics/go/fiber/analytics.go`
- Other runtime bindings: `analytics/php/core/src/Client.php`, `analytics/ruby/api_analytics/lib/api_analytics.rb`, `analytics/csharp/analytics/analytics.cs`, `analytics/rust/axum/analytics/src/analytics.rs`

#### Ingestion and Public API
The Go API service is the backend entry point for telemetry, dashboard data access, key-oriented workflows, and monitoring-related operations. Runtime startup and HTTP route registration are centered in `server/api/main.go` and `server/api/lib/routes/routes.go`; environment and logging boundaries are `server/api/lib/env/env.go` and `server/api/lib/log/log.go`. Clients authenticate data retrieval with an API key in `X-AUTH-TOKEN`; query parameters constrain raw request data.

#### Persistence Boundary
Database access is isolated as its own Go module, separating SQL/query construction, sanitization, and retention-oriented storage concerns from HTTP routing:
- `server/database/database.go`
- `server/database/query.go`
- `server/database/sanitize.go`
- `server/self-hosting/database/schema.sql`

The stored domain is request analytics: method, endpoint, user agent, optional IP/location or custom user ID, timestamp, response status/time, hostname, and framework. Privacy level is enforced at SDK capture boundaries before data reaches ingestion.

#### Dashboard Application
`dashboard/` is a SvelteKit/TypeScript frontend and server-rendered route layer. Its primary entry/routing surface is `dashboard/src/routes/+layout.svelte`, `dashboard/src/routes/+page.svelte`, and `dashboard/src/hooks.server.ts`. Dashboard and explorer screens consume request data, derive aggregates, and render visualizations:
- Analytics fetch/store/aggregation: `dashboard/src/lib/fetchRequests.ts`, `dashboard/src/lib/dataStore.ts`, `dashboard/src/lib/aggregate.ts`
- Dashboard UI: `dashboard/src/routes/dashboard/[uuid]/+page.svelte`, `dashboard/src/lib/components/dashboard/`
- Raw-data exploration: `dashboard/src/routes/explorer/[uuid]/+page.svelte`, `dashboard/src/lib/components/explorer/`

Server endpoints under `dashboard/src/routes/monitoring/` and `dashboard/src/routes/frequently-asked-questions/+server.ts` form frontend-server boundaries rather than client-only pages.

#### Active Monitoring and Operations
A separate Go monitor periodically probes customer-configured endpoints, recording uptime and response time. Its executable is `server/monitor/main.go`, with schedule/container concerns in `server/monitor/crontab` and `server/monitor/Dockerfile`. Supporting operational jobs include cleanup, health/checkup, usage calculation, and monitor tooling under `server/tools/cleanup/main.go`, `server/tools/usage/usage.go`, and `server/tools/monitor/main.go`. Email capability is a separate Go module at `server/email/email.go`.

#### Deployment and Infrastructure
Primary services are containerized Go API/logger/monitor processes plus the dashboard (`server/api/Dockerfile`, `server/logger/Dockerfile`, `server/monitor/Dockerfile`, `dashboard/Dockerfile`). Self-hosting composes them with a database and reverse proxy via `server/self-hosting/docker-compose.yml`; Nginx and optional Traefik configurations are provided in `server/self-hosting/nginx/` and `server/self-hosting/traefik/`. The logger service (`server/logger/main.go`) is a distinct ingestion-facing runtime with rate limiting at `server/logger/lib/ratelimit/ratelimit.go`.

Main entry points: [`server/self-hosting/docker-compose.yml`](https://github.com/tom-draper/api-analytics/blob/main/server/self-hosting/docker-compose.yml) (Self-hosted compose), [`server/api/main.go`](https://github.com/tom-draper/api-analytics/blob/main/server/api/main.go) (API service), [`analytics/python/python/api_analytics/core.py`](https://github.com/tom-draper/api-analytics/blob/main/analytics/python/python/api_analytics/core.py) (Capture and privacy), [`dashboard/src/lib/fetchRequests.ts`](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/lib/fetchRequests.ts) (Analytics data layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sdks["Instrumentation SDKs"]
  node_python_sdk["Python adapters<br/>SDK<br/>[fastapi.py]"]
  node_javascript_sdk["JavaScript adapters<br/>SDK<br/>[index.js]"]
  node_go_sdk["Go middleware<br/>SDK<br/>[analytics.go]"]
  node_other_sdks["Other runtime bindings<br/>SDK<br/>[Client.php]"]
  node_capture_core["Capture and privacy<br/>SDK core<br/>[core.py]"]
end

subgraph group_backend["Go Services"]
  node_logger{{"Logger service<br/>Go ingestion<br/>[main.go]"}}
  node_rate_limit["Rate limiting<br/>Go middleware<br/>[ratelimit.go]"]
  node_api_service{{"API service<br/>Go HTTP API<br/>[main.go]"}}
  node_api_routes["API routes<br/>Go routes<br/>[routes.go]"]
  node_database_module["Database access<br/>Go data module<br/>[database.go]"]
  node_analytics_db[("Analytics database<br/>SQL database<br/>[schema.sql]")]
  node_monitor["Endpoint monitor<br/>Go scheduled worker<br/>[main.go]"]
  node_operations["Operational jobs<br/>Go tools<br/>[main.go]"]
  node_email["Email module<br/>Go module<br/>[email.go]"]
end

subgraph group_dashboard["Dashboard"]
  node_sveltekit["SvelteKit routes<br/>SSR frontend<br/>[+layout.svelte]"]
  node_dashboard_data["Analytics data layer<br/>TypeScript data layer<br/>[fetchRequests.ts]"]
  node_dashboard_ui["Dashboard views<br/>Svelte UI<br/>[+page.svelte]"]
  node_explorer_ui["Raw-data explorer<br/>Svelte UI<br/>[+page.svelte]"]
  node_monitoring_routes["Monitoring endpoints<br/>SvelteKit server routes<br/>[+server.ts]"]
end

subgraph group_deploy["Self Hosting"]
  node_compose["Self-hosted compose<br/>Docker Compose<br/>[docker-compose.yml]"]
end

node_python_sdk --> node_capture_core
node_javascript_sdk --> node_capture_core
node_go_sdk --> node_capture_core
node_other_sdks --> node_capture_core
node_capture_core -->|"telemetry"| node_logger
node_rate_limit -->|"protects"| node_logger
node_logger -->|"persist requests"| node_database_module
node_api_service -->|"registers"| node_api_routes
node_api_routes -->|"query and mutate"| node_database_module
node_database_module -->|"SQL"| node_analytics_db
node_monitor -->|"records probe results"| node_analytics_db
node_operations -->|"cleanup and usage"| node_analytics_db
node_api_service -.->|"notifications"| node_email
node_sveltekit -->|"loads"| node_dashboard_data
node_dashboard_data -->|"X-AUTH-TOKEN API queries"| node_api_service
node_dashboard_data -->|"aggregates"| node_dashboard_ui
node_dashboard_data -->|"raw requests"| node_explorer_ui
node_monitoring_routes -->|"monitoring workflows"| node_api_service
node_compose -.->|"runs"| node_api_service
node_compose -.->|"runs"| node_logger
node_compose -.->|"runs"| node_monitor
node_compose -.->|"runs"| node_sveltekit
node_compose -.->|"provisions"| node_analytics_db

click node_python_sdk "https://github.com/tom-draper/api-analytics/blob/main/analytics/python/python/api_analytics/fastapi.py"
click node_javascript_sdk "https://github.com/tom-draper/api-analytics/blob/main/analytics/javascript/express/index.js"
click node_go_sdk "https://github.com/tom-draper/api-analytics/blob/main/analytics/go/gin/analytics.go"
click node_other_sdks "https://github.com/tom-draper/api-analytics/blob/main/analytics/php/core/src/Client.php"
click node_capture_core "https://github.com/tom-draper/api-analytics/blob/main/analytics/python/python/api_analytics/core.py"
click node_logger "https://github.com/tom-draper/api-analytics/blob/main/server/logger/main.go"
click node_rate_limit "https://github.com/tom-draper/api-analytics/blob/main/server/logger/lib/ratelimit/ratelimit.go"
click node_api_service "https://github.com/tom-draper/api-analytics/blob/main/server/api/main.go"
click node_api_routes "https://github.com/tom-draper/api-analytics/blob/main/server/api/lib/routes/routes.go"
click node_database_module "https://github.com/tom-draper/api-analytics/blob/main/server/database/database.go"
click node_analytics_db "https://github.com/tom-draper/api-analytics/blob/main/server/self-hosting/database/schema.sql"
click node_monitor "https://github.com/tom-draper/api-analytics/blob/main/server/monitor/main.go"
click node_operations "https://github.com/tom-draper/api-analytics/blob/main/server/tools/cleanup/main.go"
click node_email "https://github.com/tom-draper/api-analytics/blob/main/server/email/email.go"
click node_sveltekit "https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/%2Blayout.svelte"
click node_dashboard_data "https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/lib/fetchRequests.ts"
click node_dashboard_ui "https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/dashboard/%5Buuid%5D/%2Bpage.svelte"
click node_explorer_ui "https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/explorer/%5Buuid%5D/%2Bpage.svelte"
click node_monitoring_routes "https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/monitoring/%2Bserver.ts"
click node_compose "https://github.com/tom-draper/api-analytics/blob/main/server/self-hosting/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_python_sdk,node_javascript_sdk,node_go_sdk,node_other_sdks,node_capture_core toneBlue
class node_logger,node_rate_limit,node_api_service,node_api_routes,node_database_module,node_analytics_db,node_monitor,node_operations,node_email toneAmber
class node_sveltekit,node_dashboard_data,node_dashboard_ui,node_explorer_ui,node_monitoring_routes toneMint
class node_compose toneRose
```

## Components

### Instrumentation SDKs

Framework adapters capture and submit API telemetry.

- [Python adapters](https://github.com/tom-draper/api-analytics/blob/main/analytics/python/python/api_analytics/fastapi.py): SDK · `analytics/python/python/api_analytics/fastapi.py`
- [JavaScript adapters](https://github.com/tom-draper/api-analytics/blob/main/analytics/javascript/express/index.js): SDK · `analytics/javascript/express/index.js`
- [Go middleware](https://github.com/tom-draper/api-analytics/blob/main/analytics/go/gin/analytics.go): SDK · `analytics/go/gin/analytics.go`
- [Other runtime bindings](https://github.com/tom-draper/api-analytics/blob/main/analytics/php/core/src/Client.php): SDK · PHP, Ruby, C#, and Rust bindings. · `analytics/php/core/src/Client.php`
- [Capture and privacy](https://github.com/tom-draper/api-analytics/blob/main/analytics/python/python/api_analytics/core.py): SDK core · Measures requests, maps identities, and applies privacy before submission. · `analytics/python/python/api_analytics/core.py`

### Go Services

Ingestion, query, monitoring, and operational runtimes.

- [Logger service](https://github.com/tom-draper/api-analytics/blob/main/server/logger/main.go): Go ingestion · Ingestion-facing runtime with rate limiting. · `server/logger/main.go`
- [Rate limiting](https://github.com/tom-draper/api-analytics/blob/main/server/logger/lib/ratelimit/ratelimit.go): Go middleware · `server/logger/lib/ratelimit/ratelimit.go`
- [API service](https://github.com/tom-draper/api-analytics/blob/main/server/api/main.go): Go HTTP API · Serves telemetry, dashboard data, keys, and monitoring workflows. · `server/api/main.go`
- [API routes](https://github.com/tom-draper/api-analytics/blob/main/server/api/lib/routes/routes.go): Go routes · `server/api/lib/routes/routes.go`
- [Database access](https://github.com/tom-draper/api-analytics/blob/main/server/database/database.go): Go data module · Owns query construction, sanitization, and retention access. · `server/database/database.go`
- [Analytics database](https://github.com/tom-draper/api-analytics/blob/main/server/self-hosting/database/schema.sql): SQL database · Stores request and monitoring analytics. · `server/self-hosting/database/schema.sql`
- [Endpoint monitor](https://github.com/tom-draper/api-analytics/blob/main/server/monitor/main.go): Go scheduled worker · Probes configured endpoints for uptime and response time. · `server/monitor/main.go`
- [Operational jobs](https://github.com/tom-draper/api-analytics/blob/main/server/tools/cleanup/main.go): Go tools · Cleanup, usage, health, and monitor support jobs. · `server/tools/cleanup/main.go`
- [Email module](https://github.com/tom-draper/api-analytics/blob/main/server/email/email.go): Go module · `server/email/email.go`

### Dashboard

SvelteKit UI and server-rendered data boundary.

- [SvelteKit routes](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/%2Blayout.svelte): SSR frontend · Dashboard entry, routing, and server hooks. · `dashboard/src/routes/+layout.svelte`
- [Analytics data layer](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/lib/fetchRequests.ts): TypeScript data layer · Fetches request data and derives aggregates. · `dashboard/src/lib/fetchRequests.ts`
- [Dashboard views](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/dashboard/%5Buuid%5D/%2Bpage.svelte): Svelte UI · Visualizes request aggregates. · `dashboard/src/routes/dashboard/[uuid]/+page.svelte`
- [Raw-data explorer](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/explorer/%5Buuid%5D/%2Bpage.svelte): Svelte UI · `dashboard/src/routes/explorer/[uuid]/+page.svelte`
- [Monitoring endpoints](https://github.com/tom-draper/api-analytics/blob/main/dashboard/src/routes/monitoring/%2Bserver.ts): SvelteKit server routes · Frontend-server boundary for monitoring workflows. · `dashboard/src/routes/monitoring/+server.ts`

### Self Hosting

Containerized deployment topology.

- [Self-hosted compose](https://github.com/tom-draper/api-analytics/blob/main/server/self-hosting/docker-compose.yml): Docker Compose · Composes services, database, and reverse proxy. · `server/self-hosting/docker-compose.yml`

## Connections

- Python adapters → Capture and privacy
- JavaScript adapters → Capture and privacy
- Go middleware → Capture and privacy
- Other runtime bindings → Capture and privacy
- Capture and privacy → Logger service: telemetry
- Rate limiting → Logger service: protects
- Logger service → Database access: persist requests
- API service → API routes: registers
- API routes → Database access: query and mutate
- Database access → Analytics database: SQL
- Endpoint monitor → Analytics database: records probe results
- Operational jobs → Analytics database: cleanup and usage
- API service → Email module: notifications
- SvelteKit routes → Analytics data layer: loads
- Analytics data layer → API service: X-AUTH-TOKEN API queries
- Analytics data layer → Dashboard views: aggregates
- Analytics data layer → Raw-data explorer: raw requests
- Monitoring endpoints → API service: monitoring workflows
- Self-hosted compose → API service: runs
- Self-hosted compose → Logger service: runs
- Self-hosted compose → Endpoint monitor: runs
- Self-hosted compose → SvelteKit routes: runs
- Self-hosted compose → Analytics database: provisions
