Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1jehuang Jcode insertEvent

From Leeroopedia
Knowledge Sources
Domains Telemetry, Data_Pipeline
Last Updated 2026-10-18 00:00 GMT

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.
Principle
Implementation
Heuristic
Environment