Analytics & Logging (ClickHouse)
Relevant source files
The following files were used as context for generating this wiki page:
Speakeasy uses ClickHouse as its primary high-performance data store for telemetry, analytics, and observability. The system implements a wide event model that captures rich, semi-structured data as JSON attributes, enabling deep inspection of tool calls, agent sessions, and cost metrics without requiring frequent schema changes.
Sources: server/internal/telemetry/README.md:1-10, README.md
Architecture Overview
The telemetry architecture centers on a "wide event" pattern where every operation—such as an LLM chat completion or a tool call—is stored as a single, richly-attributed row in the telemetry_logs table.
Data Flow
This diagram shows the flow of telemetry data from emission in the client to storage and querying in ClickHouse.
Sources: server/internal/telemetry/README.md:12-45
Data Modeling
Speakeasy follows OpenTelemetry (OTel) semantic conventions for attribute naming. Data is categorized into core log fields, trace context, and flexible JSON payloads.
Telemetry Log Structure
| Column Group | Purpose | Examples |
|---|---|---|
| Core OTel fields | Standard log record identity | id, time_unix_nano, severity_text, body |
| Trace context | Distributed tracing correlation | trace_id, span_id |
attributes (JSON) | Wide event payload (WHAT happened) | HTTP details, GenAI tokens, tool URNs |
resource_attributes | WHO/WHERE produced the event | service.name, service.version |
| Materialized columns | Fast indexing for JSON paths | project_id, deployment_id, chat_id |
Sources: server/internal/telemetry/README.md:28-40, REVIEW.md:200-210
Materialized Columns and Indexing
To maintain high performance, ClickHouse extracts frequently queried JSON paths into materialized columns at insert time. These columns use bloom filter indices for fast point lookups.
- Primary Key:
(gram_project_id, time_unix_nano, id). This ensures all queries are physically scoped to a project. - Partitioning: Data is partitioned by day (
toYYYYMMDD). - TTL: Logs are automatically retained for 30 days.
Sources: server/internal/telemetry/README.md:50-60, server/internal/telemetry/README.md:210-230
Emitting Telemetry
Developers emit telemetry through the Service.CreateLog(LogParams) method in the server's telemetry package.
Implementation Patterns
- Direct Attributes: Build a
map[attr.Key]anyusing typed constants from theattrpackage. - HTTP Log Recorder: Use
HTTPLogAttributesfor progressive recording of method, route, status codes, and duration during a request lifecycle. - Async Inserts: Production writes use ClickHouse's
async_insert=1setting. The server client buffers rows and flushes them in batches to optimize throughput.
Sources: server/internal/telemetry/README.md:120-160, server/internal/telemetry/README.md:315-325
// Example attribute recording pattern
attrs := map[attr.Key]any{
attr.GenAIRequestModelKey: "gpt-4o",
attr.GenAIUsageInputTokensKey: 150,
attr.GenAIConversationIDKey: chatID.String(),
}
svc.CreateLog(telemetry.LogParams{
Timestamp: time.Now(),
Attributes: attrs,
})
Sources: server/internal/telemetry/README.md:130-145
Querying and Insights
The Dashboard UI queries ClickHouse to generate insights and suggestions. Queries utilize the Squirrel query builder for dynamic SQL construction.
Insight Suggestions
The dashboard provides "Explore with AI" suggestions based on telemetry data. These prompts are categorized by route:
| Route | Suggestion Focus |
|---|---|
insights/costs | Spend drivers, cost by model, top spenders. |
insights/tools | Usage trends, failure rates, latency outliers (p95). |
logs/risk-events | Findings by category, severe events, user exposure. |
mcp | Server traffic, error patterns, token consumption per server. |
Sources: client/dashboard/src/lib/insights-suggestions.ts:200-450
Pagination
ClickHouse queries implement cursor-based pagination using a "limit + 1" pattern. The repository layer returns raw results, while the service layer computes the nextCursor based on the UUID of the last item.
Sources: server/internal/telemetry/README.md:295-305
Configuration
| Environment Variable | Description | Default (Local) |
|---|---|---|
GRAM_CLICKHOUSE_URL | Primary connection string with native protocol. | clickhouse://gram:gram@127.0.0.1:9440/gram |
CLICKHOUSE_HTTP_PORT | HTTP interface port for web clients. | 8123 |
GRAM_ENABLE_OTEL_METRICS | Toggle for emitting OpenTelemetry metrics. | 1 |
CLICKHOUSE_INSECURE | Disable TLS for local development. | true |
Sources: mise.toml:100-125, examples/clickhouse/README.md:110-120
Summary
The ClickHouse-based Analytics & Logging system provides the foundation for Speakeasy's observability. By combining OTel-compliant wide events with ClickHouse's materialized columns and async processing, the platform achieves high-performance querying for complex AI usage patterns, cost attribution, and security auditing.
Sources: server/internal/telemetry/README.md:1-15, README.md