Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/observability/patterns.md
1# Observability Patterns23## Usage-Based Billing45```typescript6env.ANALYTICS.writeDataPoint({7blobs: [customerId, request.url, request.method],8doubles: [1], // request_count9indexes: [customerId]10});11```1213```sql14SELECT blob1 AS customer_id, SUM(_sample_interval * double1) AS total_calls15FROM api_usage WHERE timestamp >= DATE_TRUNC('month', NOW())16GROUP BY customer_id17```1819## Performance Monitoring2021```typescript22const start = Date.now();23const response = await fetch(url);24env.ANALYTICS.writeDataPoint({25blobs: [url, response.status.toString()],26doubles: [Date.now() - start, response.status]27});28```2930```sql31SELECT blob1 AS url, AVG(double1) AS avg_ms, percentile(double1, 0.95) AS p95_ms32FROM fetch_metrics WHERE timestamp >= NOW() - INTERVAL '1' HOUR33GROUP BY url34```3536## Error Tracking3738```typescript39env.ANALYTICS.writeDataPoint({40blobs: [error.name, request.url, request.method],41doubles: [1],42indexes: [error.name]43});44```4546## Multi-Tenant Tracking4748```typescript49env.ANALYTICS.writeDataPoint({50indexes: [tenantId], // efficient filtering51blobs: [tenantId, url.pathname, method, status],52doubles: [1, duration, bytesSize]53});54```5556## Tail Worker Log Filtering5758```typescript59export default {60async tail(events, env, ctx) {61const critical = events.filter(e =>62e.exceptions.length > 0 || e.event.wallTime > 100000063);64if (critical.length === 0) return;6566ctx.waitUntil(67fetch('https://logging.example.com/ingest', {68method: 'POST',69headers: { 'Authorization': `Bearer ${env.API_KEY}` },70body: JSON.stringify(critical.map(e => ({71outcome: e.event.outcome,72cpu_ms: e.event.cpuTime / 1000,73errors: e.exceptions74})))75})76);77}78};79```8081## OpenTelemetry Export8283```typescript84export default {85async tail(events, env, ctx) {86const otelSpans = events.map(e => ({87traceId: generateId(32),88spanId: generateId(16),89name: e.scriptName || 'worker.request',90attributes: [91{ key: 'worker.outcome', value: { stringValue: e.event.outcome } },92{ key: 'worker.cpu_time_us', value: { intValue: String(e.event.cpuTime) } }93]94}));9596ctx.waitUntil(97fetch('https://api.honeycomb.io/v1/traces', {98method: 'POST',99headers: { 'X-Honeycomb-Team': env.HONEYCOMB_KEY },100body: JSON.stringify({ resourceSpans: [{ scopeSpans: [{ spans: otelSpans }] }] })101})102);103}104};105```106