From f10e4cdca673d8186c51e0e6329837098683e6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20F=C3=B6ldi?= Date: Mon, 29 Apr 2024 14:17:05 +0200 Subject: [PATCH] Allow disabling global fetch and cache instrumentation (#130) --- .changeset/violet-bananas-attend.md | 5 +++++ src/sdk.ts | 12 ++++++++++-- src/types.ts | 7 +++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 .changeset/violet-bananas-attend.md diff --git a/.changeset/violet-bananas-attend.md b/.changeset/violet-bananas-attend.md new file mode 100644 index 0000000..4b03412 --- /dev/null +++ b/.changeset/violet-bananas-attend.md @@ -0,0 +1,5 @@ +--- +"@microlabs/otel-cf-workers": minor +--- + +Add option to disable auto-instrumentation of global fetch and cache API diff --git a/src/sdk.ts b/src/sdk.ts index baaa937..c769444 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -76,8 +76,12 @@ function isSpanExporter(exporterConfig: ExporterConfig): exporterConfig is SpanE let initialised = false function init(config: ResolvedTraceConfig): void { if (!initialised) { - instrumentGlobalCache() - instrumentGlobalFetch() + if (config.instrumentation.instrumentGlobalCache) { + instrumentGlobalCache() + } + if (config.instrumentation.instrumentGlobalFetch) { + instrumentGlobalFetch() + } propagation.setGlobalPropagator(config.propagator) const resource = createResource(config) @@ -135,6 +139,10 @@ function parseConfig(supplied: TraceConfig): ResolvedTraceConfig { service: supplied.service, spanProcessors, propagator: supplied.propagator || new W3CTraceContextPropagator(), + instrumentation: { + instrumentGlobalCache: supplied.instrumentation?.instrumentGlobalCache ?? true, + instrumentGlobalFetch: supplied.instrumentation?.instrumentGlobalFetch ?? true, + }, } } else { const exporter = isSpanExporter(supplied.exporter) ? supplied.exporter : new OTLPExporter(supplied.exporter) diff --git a/src/types.ts b/src/types.ts index 6af328e..035881b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,6 +29,11 @@ export interface SamplingConfig { tailSampler?: TailSampleFn } +export interface InstrumentationOptions { + instrumentGlobalFetch?: boolean + instrumentGlobalCache?: boolean +} + interface TraceConfigBase { service: ServiceConfig handlers?: HandlerConfig @@ -36,6 +41,7 @@ interface TraceConfigBase { postProcessor?: PostProcessorFn sampling?: SamplingConfig propagator?: TextMapPropagator + instrumentation?: InstrumentationOptions } interface TraceConfigExporter extends TraceConfigBase { @@ -59,6 +65,7 @@ export interface ResolvedTraceConfig extends TraceConfigBase { sampling: Required> spanProcessors: SpanProcessor[] propagator: TextMapPropagator + instrumentation: InstrumentationOptions } export interface DOConstructorTrigger {