Core instrumentation for Stainless X-ray request logging. This module is runtime-agnostic and only provides the emitter, config, and types. Use it directly if you need a custom runtime or a custom OpenTelemetry exporter; otherwise prefer @stainlessdev/xray-emitter/node or @stainlessdev/xray-emitter/fetch.
pnpm add @stainlessdev/xray-emitterimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { createEmitter } from '@stainlessdev/xray-emitter';
const exporter = new OTLPTraceExporter({
url: process.env.STAINLESS_XRAY_ENDPOINT_URL,
});
const xray = createEmitter({ serviceName: 'my-service' }, exporter);
const ctx = xray.startRequest({
method: 'GET',
url: 'https://example.com/hello',
headers: { 'user-agent': 'curl/8.0' },
startTimeMs: Date.now(),
});
const log = xray.endRequest(ctx, {
statusCode: 200,
headers: { 'request-id': 'req_123' },
endTimeMs: Date.now(),
});
await xray.flush();X-ray always produces a request ID for each request. If you do not provide one, it resolves the ID in this order: explicit requestId, configured response header name (requestId.header, default: request-id), then an auto-generated UUIDv7-based ID. Runtime adapters inject the header automatically when missing; if you use the core module directly, you are responsible for writing the response header yourself.
Common knobs:
serviceName(required) andendpointUrl(falls back toSTAINLESS_XRAY_ENDPOINT_URL).exporteroverrides for OTLP headers, timeout, and span processor.captureandredactiontoggles for headers/body logging.requestId.headerfor the response header name.routenormalization options.
Notes:
endpointUrlis required;/v1/tracesis appended if missing. If both are set,endpointUrlwins overSTAINLESS_XRAY_ENDPOINT_URL.- If
endpointUrlincludes basic auth credentials, they are moved to theAuthorizationheader automatically.
Use @stainlessdev/xray-emitter (core) only if you need to integrate with a custom runtime or supply your own SpanExporter. For Node and fetch-based runtimes, use @stainlessdev/xray-emitter/node or @stainlessdev/xray-emitter/fetch for a ready-to-go emitter and request/response adapters.