Lesson 36: Telemetry: the same run, as an OTLP envelope on disk
the OTLP envelope, the gen_ai span tree, redaction, batching
1. Why This Feature Exists
One real model turn runs through .withTelemetry({ exporter }). The exporter is not mocked: it builds the OTLP/JSON body itself and POSTs it to a collector that writes every envelope to scratchpad/examples/22-telemetry/. The file is then read back and parsed โ the proof is the bytes on disk.
2. What the Example Demonstrates
invoke_agent with one CLIENT chat {model} span under it (the ApiObserver seam), gen_ai attributes and a duration, and the whole envelope is a file we can read.otelAttribute encodes a string / intotelAttribute encodes a string / int / double / bool, drops a non-scalar, and caps a long string.batchSize posts on the fill, flushIntervalMs posts on the timer โ no explicit flush() needed.CompositeApiObserver delivers the same call to an EventEmitterApiObserver (the app's own bus) AND to the exporter, so the app's events and the trace are one source, not two code paths. Public API: createOtlpExporter, otelAttribute, CompositeApiObserver, EventEmitterApiObserver, withTelemetry, EVENTS. Run: node examples/22_telemetry.mjs Needs: PROVIDER + provider credentials in secret.env. @version 0.27.03. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import { EventEmitter } from 'node:events';
import { appendFile, mkdir, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
import {
CompositeApiObserver, EVENTS, EventEmitterApiObserver, createOtlpExporter, initializeClient, loadCredentials,
log, otelAttribute,
} from 'merci-sdk';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';
const OUT_DIR = join(SCRATCH_DIR, '22-telemetry');
const LIVE_FILE = join(OUT_DIR, 'live-spans.ndjson');
const BATCH_FILE = join(OUT_DIR, 'batched-spans.ndjson');
const COMPOSITE_FILE = join(OUT_DIR, 'composite-spans.ndjson');
/** The marker the run must return, and the credential-shaped string it must swallow. */
const MARKER = 'OTEL-LIVE-22';
const LEAK = 'sk-live-DO-NOT-LEAK-22';
/**
* A collector that writes each OTLP/JSON envelope as one line of a file โ the
* exporter's real HTTP contract, with the bytes kept for inspection.
* @param {{file: string}} arg0
* @returns {{posts: Array<{url: string, raw: string, headers: Record<string, string>}>, fetch: (url: string, init: any) => Promise<{ok: boolean, status: number}>}}
*/
function fileCollector({ file }) {
/** @type {Array<{url: string, raw: string, headers: Record<string, string>}>} */
const posts = [];
/** @param {string} url @param {any} init */
const fetch = async (url, init) => {
const raw = String(init?.body ?? '');
posts.push({ url: String(url), raw, headers: init?.headers ?? {} });
await mkdir(OUT_DIR, { recursive: true });
await appendFile(file, `${raw}\n`, 'utf8');
return { ok: true, status: 200 };
};
return { posts, fetch };
}
/**
* Every OTLP line of a collector file, parsed. A corrupt line is reported, never4. Behavior the Code Fixes
One real model turn runs through .withTelemetry({ exporter }). The exporter is not mocked: it builds the OTLP/JSON body itself and POSTs it to a collector that writes every envelope to scratchpad/examples/22-telemetry/. The file is then read back and parsed โ the proof is the bytes on disk.
5. Run the Example
Execute the runnable example from the repository root:
node examples/22_telemetry.mjs