Overview
Concrete tool for ingesting telemetry events into Cloudflare D1 and Analytics Engine with dual-write, provided by the telemetry worker.
Description
The insertEvent and insertEventRow functions are the core of the telemetry ingestion pipeline. Each validated event is written to both the D1 relational database (events table plus a detail table like session_details or turn_details) and the Analytics Engine firehose. Daily active user records are also updated. D1 size is observed after each insert to trigger emergency pruning if the soft limit is exceeded.
Usage
- Event ingestion: Called by the fetch handler for
POST /v1/event and POST /v2/usage.
- Transcript ingestion: Called by
ingestTranscript for POST /v1/transcript.
Code Reference
Source Location
- Repository: jcode
- File: telemetry-worker/src/worker.js
Signature
async function insertEvent(env, body)
async function insertEventRow(env, body, entries)
async function recordDailyActivity(env, body)
// Analytics Engine dual-write:
env.FIREHOSE.put({ blob, indexes })
Import
// Internal worker functions, called from the fetch handler.
// telemetry-worker/src/worker.js
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| env.DB |
D1 |
Yes /// Cloudflare D1 binding
|
| env.FIREHOSE |
AnalyticsEngine |
Yes /// Analytics Engine binding
|
| body |
object |
Yes /// Validated event body with telemetry_id, event, version, os, arch
|
Outputs
| Name |
Type |
Description
|
| D1 rows |
database /// Row in events table + detail table
|
| firehose |
analytics /// Analytics Engine write with flattened schema
|
| daily_active |
database /// daily_active_users update
|
Usage Examples
Fetch Handler Routing
// In the Cloudflare Worker fetch handler:
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/v1/event") {
const body = await request.json();
// Validate body...
await insertEvent(env, body);
return new Response(JSON.stringify({ ok: true }), { status: 200 });
}
}
};
Related Pages
Implements Principle
Requires Environment
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.