otel-cf-workers icon indicating copy to clipboard operation
otel-cf-workers copied to clipboard

Support importing `env` globally

Open dmaretskyi opened this issue 3 months ago • 1 comments

Cloudflare now supports importing env globally:

import { env } from 'cloudflare:workers'

The global env could be instrumented using provided withEnv wrapper:

import { env, withEnv } from 'cloudflare:workers'

// ...

const instrumentFn = (fn) => withEnv(instrumentEnv(env), fn)

dmaretskyi avatar Sep 02 '25 12:09 dmaretskyi

I patched the package using pnpm's patch command.

diff --git a/dist/index.d.ts b/dist/index.d.ts
index 02ae1b229f443e0dd327b6c179196fdfa007bbf5..c810dcacd3ce81221c24faed1fbf232fe25ce804 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -232,4 +232,6 @@ declare class BatchTraceSpanProcessor implements TraceFlushableSpanProcessor {
 
 declare function withNextSpan(attrs: Attributes): void;
 
-export { BatchTraceSpanProcessor, type ConfigurationOption, type DOConstructorTrigger, type ExporterConfig, type HandlerConfig, type HandlerInstrumentation, type InitialSpanInfo, type InstrumentationOptions, type LocalTrace, MultiSpanExporter, MultiSpanExporterAsync, OTLPExporter, type OTLPExporterConfig, type OrPromise, type ParentRatioSamplingConfig, type PostProcessorFn, type ResolveConfigFn, type ResolvedTraceConfig, type SamplingConfig, type ServiceConfig, SpanImpl, type TailSampleFn, type TraceConfig, type TraceFlushableSpanProcessor, type Trigger, __unwrappedFetch, createSampler, exportSpans, instrument, instrumentDO, isAlarm, isHeadSampled, isMessageBatch, isRequest, isRootErrorSpan, isSpanProcessorConfig, multiTailSampler, withNextSpan };
+declare function instrumentEnv<E>(env: E): E;
+
+export { BatchTraceSpanProcessor, type ConfigurationOption, type DOConstructorTrigger, type ExporterConfig, type HandlerConfig, type HandlerInstrumentation, type InitialSpanInfo, type InstrumentationOptions, type LocalTrace, MultiSpanExporter, MultiSpanExporterAsync, OTLPExporter, type OTLPExporterConfig, type OrPromise, type ParentRatioSamplingConfig, type PostProcessorFn, type ResolveConfigFn, type ResolvedTraceConfig, type SamplingConfig, type ServiceConfig, SpanImpl, type TailSampleFn, type TraceConfig, type TraceFlushableSpanProcessor, type Trigger, __unwrappedFetch, createSampler, exportSpans, instrument, instrumentDO, isAlarm, isHeadSampled, isMessageBatch, isRequest, isRootErrorSpan, isSpanProcessorConfig, multiTailSampler, withNextSpan, instrumentEnv };
diff --git a/dist/index.js b/dist/index.js
index dc464f8c3e54c427c77d79f76e9fff9cec7964c5..a0e3b224c6f0bbb750de3faa1a140616b1005873 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1537,7 +1537,7 @@ var isAnalyticsEngineDataset = (item) => {
 var isD1Database = (item) => {
   return !!item?.exec && !!item?.prepare;
 };
-var instrumentEnv = (env) => {
+export var instrumentEnv = (env) => {
   const envHandler = {
     get: (target, prop, receiver) => {
       const item = Reflect.get(target, prop, receiver);

Which allows you to:

// env.ts
import { instrumentEnv } from "@microlabs/otel-cf-workers";
import { env as _env } from "cloudflare:workers";

export const env = instrumentEnv(_env)

And replace all your imports:

-import { env } from "cloudflare:workers";
+import { env } from "~/env";

ariesclark avatar Oct 22 '25 17:10 ariesclark