Skip to content

Commit

Permalink
Allow disabling global fetch and cache instrumentation (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniFoldi authored Apr 29, 2024
1 parent 922ba72 commit f10e4cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-bananas-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@microlabs/otel-cf-workers": minor
---

Add option to disable auto-instrumentation of global fetch and cache API
12 changes: 10 additions & 2 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ export interface SamplingConfig<HS extends HeadSamplerConf = HeadSamplerConf> {
tailSampler?: TailSampleFn
}

export interface InstrumentationOptions {
instrumentGlobalFetch?: boolean
instrumentGlobalCache?: boolean
}

interface TraceConfigBase {
service: ServiceConfig
handlers?: HandlerConfig
fetch?: FetcherConfig
postProcessor?: PostProcessorFn
sampling?: SamplingConfig
propagator?: TextMapPropagator
instrumentation?: InstrumentationOptions
}

interface TraceConfigExporter extends TraceConfigBase {
Expand All @@ -59,6 +65,7 @@ export interface ResolvedTraceConfig extends TraceConfigBase {
sampling: Required<SamplingConfig<Sampler>>
spanProcessors: SpanProcessor[]
propagator: TextMapPropagator
instrumentation: InstrumentationOptions
}

export interface DOConstructorTrigger {
Expand Down

0 comments on commit f10e4cd

Please sign in to comment.