|
| 1 | +import type { Env } from './types' |
| 2 | + |
| 3 | +// Cloudflare Workers can't run the OpenTelemetry Node SDK, so errors are sent as |
| 4 | +// OTLP/HTTP JSON log records via fetch. Vendor-neutral: point |
| 5 | +// OTEL_EXPORTER_OTLP_ENDPOINT at any OTLP backend (Sentry OTLP, Grafana, a Collector, ...). |
| 6 | +const SERVICE_NAME = 'yearn-prices' |
| 7 | +const SEVERITY_ERROR = 17 // OTLP severityNumber for ERROR |
| 8 | + |
| 9 | +type OtlpAttribute = { key: string; value: { stringValue: string } } |
| 10 | + |
| 11 | +function attr(key: string, value: string): OtlpAttribute { |
| 12 | + return { key, value: { stringValue: value } } |
| 13 | +} |
| 14 | + |
| 15 | +// OTEL_EXPORTER_OTLP_HEADERS format: "key1=value1,key2=value2". |
| 16 | +function parseHeaders(raw?: string): Record<string, string> { |
| 17 | + const headers: Record<string, string> = { 'content-type': 'application/json' } |
| 18 | + if (!raw) return headers |
| 19 | + for (const pair of raw.split(',')) { |
| 20 | + const idx = pair.indexOf('=') |
| 21 | + if (idx > 0) headers[pair.slice(0, idx).trim()] = pair.slice(idx + 1).trim() |
| 22 | + } |
| 23 | + return headers |
| 24 | +} |
| 25 | + |
| 26 | +function buildPayload(serviceName: string, err: Error): unknown { |
| 27 | + const attributes = [attr('exception.type', err.name), attr('exception.message', err.message)] |
| 28 | + if (err.stack) attributes.push(attr('exception.stacktrace', err.stack)) |
| 29 | + |
| 30 | + return { |
| 31 | + resourceLogs: [ |
| 32 | + { |
| 33 | + resource: { attributes: [attr('service.name', serviceName)] }, |
| 34 | + scopeLogs: [ |
| 35 | + { |
| 36 | + scope: { name: serviceName }, |
| 37 | + logRecords: [ |
| 38 | + { |
| 39 | + timeUnixNano: String(Date.now() * 1_000_000), |
| 40 | + severityNumber: SEVERITY_ERROR, |
| 41 | + severityText: 'ERROR', |
| 42 | + body: { stringValue: err.message }, |
| 43 | + attributes, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + ], |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export function captureError(ctx: ExecutionContext, env: Env, error: unknown): void { |
| 54 | + const endpoint = env.OTEL_EXPORTER_OTLP_ENDPOINT |
| 55 | + if (!endpoint) return |
| 56 | + |
| 57 | + const err = error instanceof Error ? error : new Error(String(error)) |
| 58 | + const url = `${endpoint.replace(/\/$/, '')}/v1/logs` |
| 59 | + const body = JSON.stringify(buildPayload(env.OTEL_SERVICE_NAME || SERVICE_NAME, err)) |
| 60 | + |
| 61 | + // waitUntil lets the export finish after the response is returned (no added latency). |
| 62 | + ctx.waitUntil( |
| 63 | + fetch(url, { |
| 64 | + method: 'POST', |
| 65 | + headers: parseHeaders(env.OTEL_EXPORTER_OTLP_HEADERS), |
| 66 | + body, |
| 67 | + }).catch(() => {}), |
| 68 | + ) |
| 69 | +} |
0 commit comments