autotel-cloudflare
Version:
The #1 OpenTelemetry package for Cloudflare Workers - complete bindings coverage, native CF OTel integration, advanced sampling
140 lines (130 loc) • 4.53 kB
TypeScript
/**
* Auto-instrumentation for Cloudflare Workers bindings
*
* Note: This file uses Cloudflare Workers types (KVNamespace, R2Bucket, D1Database, Fetcher, etc.)
* which are globally available via @cloudflare/workers-types when listed in tsconfig.json.
* These types are devDependencies only - they're not runtime dependencies.
* At runtime, Cloudflare Workers runtime provides the actual implementations.
*
* This module provides automatic tracing for Cloudflare bindings:
* - KV (key-value operations)
* - R2 (object storage operations)
* - D1 (database operations)
* - Service Bindings
* - Events Engine
* - Workers AI
* - Vectorize
* - Hyperdrive
*/
/**
* Instrument KV namespace
*/
declare function instrumentKV<K extends KVNamespace>(kv: K, namespaceName?: string): K;
/**
* Instrument R2 bucket
*/
declare function instrumentR2<R extends R2Bucket>(r2: R, bucketName?: string): R;
/**
* Instrument D1 database
*/
declare function instrumentD1<D extends D1Database>(d1: D, databaseName?: string): D;
/**
* Instrument service binding (Fetcher)
*
* Unlike other bindings, Fetcher objects are native Cloudflare C++ bindings
* whose methods throw "Illegal invocation" when called through a Proxy with
* a different `this` reference. We work around this by calling `target.fetch()`
* directly on the original binding instead of using `Reflect.apply` on a
* detached function reference.
*/
declare function instrumentServiceBinding<F extends Fetcher>(fetcher: F, serviceName?: string): F;
declare function instrumentBindings(env: Record<string, any>): Record<string, any>;
/**
* Workers AI binding instrumentation
*/
/**
* Instrument Workers AI binding
*/
declare function instrumentAI<T extends Ai>(ai: T, bindingName?: string): T;
/**
* Vectorize binding instrumentation
*/
/**
* Instrument Vectorize index binding
*/
declare function instrumentVectorize<T extends VectorizeIndex>(vectorize: T, indexName?: string): T;
/**
* Hyperdrive binding instrumentation
*/
/**
* Instrument Hyperdrive binding
*/
declare function instrumentHyperdrive<T extends Hyperdrive>(hyperdrive: T, bindingName?: string): T;
/**
* Queue producer binding instrumentation
*/
/**
* Instrument Queue producer binding
*/
declare function instrumentQueueProducer<T extends Queue>(queue: T, queueName?: string): T;
/**
* Analytics Engine binding instrumentation
*/
/**
* Instrument Analytics Engine binding
*/
declare function instrumentAnalyticsEngine<T extends AnalyticsEngineDataset>(ae: T, datasetName?: string): T;
/**
* Images binding instrumentation
*
* The Images binding uses a fluent chain: input() -> transform() -> draw() -> output()
* We only create a span at the terminal output() call to avoid intermediate noise.
* info() is a standalone operation and gets its own span.
*/
interface ImagesLike {
info(blob: ReadableStream | ArrayBuffer | Blob): Promise<{
width: number;
height: number;
format: string;
}>;
input(blob: ReadableStream | ArrayBuffer | Blob): ImageTransformerLike;
}
interface ImageTransformerLike {
transform(options: unknown): ImageTransformerLike;
draw(image: unknown, options?: unknown): ImageTransformerLike;
output(options?: unknown): Promise<ImageOutputLike>;
}
interface ImageOutputLike {
response(): Response;
blob(): Promise<Blob>;
arrayBuffer(): Promise<ArrayBuffer>;
}
/**
* Instrument Images binding
*/
declare function instrumentImages<T extends ImagesLike>(images: T, bindingName?: string): T;
/**
* Rate Limiter binding instrumentation
*/
interface RateLimiterLike {
limit(options: {
key: string;
}): Promise<{
success: boolean;
}>;
}
/**
* Instrument Rate Limiter binding (manual only — not auto-detected)
*/
declare function instrumentRateLimiter<T extends RateLimiterLike>(limiter: T, bindingName?: string): T;
/**
* Browser Rendering binding instrumentation
*/
interface BrowserRenderingLike {
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
}
/**
* Instrument Browser Rendering binding (manual only — not auto-detected)
*/
declare function instrumentBrowserRendering<T extends BrowserRenderingLike>(browser: T, bindingName?: string): T;
export { instrumentAI, instrumentAnalyticsEngine, instrumentBindings, instrumentBrowserRendering, instrumentD1, instrumentHyperdrive, instrumentImages, instrumentKV, instrumentQueueProducer, instrumentR2, instrumentRateLimiter, instrumentServiceBinding, instrumentVectorize };