Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where calling fetch in functions that weren't just instrumented threw an error #152

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function setConfig(config: ResolvedTraceConfig, ctx = context.active()) {
return ctx.setValue(configSymbol, config)
}

export function getActiveConfig(): ResolvedTraceConfig {
export function getActiveConfig(): ResolvedTraceConfig | undefined {
const config = context.active().getValue(configSymbol) as ResolvedTraceConfig
return config
return config || undefined
}
8 changes: 8 additions & 0 deletions src/instrumentation/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ export function getParentContextFromHeaders(headers: Headers): Context {

function getParentContextFromRequest(request: Request) {
const workerConfig = getActiveConfig()

if (workerConfig === undefined) {
return api_context.active()
}

const acceptTraceContext =
typeof workerConfig.handlers.fetch.acceptTraceContext === 'function'
? workerConfig.handlers.fetch.acceptTraceContext(request)
Expand Down Expand Up @@ -208,6 +213,9 @@ export function instrumentClientFetch(
}

const workerConfig = getActiveConfig()
if (!workerConfig) {
return Reflect.apply(target, thisArg, [request])
}
const config = configFn(workerConfig)

const tracer = trace.getTracer('fetcher')
Expand Down
5 changes: 4 additions & 1 deletion src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export class BatchTraceSpanProcessor implements SpanProcessor {
}

private export(localRootSpanId: string) {
const { sampling, postProcessor } = getActiveConfig()
const config = getActiveConfig()
if (!config) throw new Error('Config is undefined. This is a bug in the instrumentation logic')

const { sampling, postProcessor } = config
const exportArgs = { exporter: this.exporter, tailSampler: sampling.tailSampler, postProcessor }
const newState = this.action(localRootSpanId, { actionName: 'startExport', args: exportArgs })
if (newState.stateName === 'exporting') {
Expand Down
5 changes: 4 additions & 1 deletion src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export class WorkerTracer implements Tracer {
const spanKind = options.kind || SpanKind.INTERNAL
const sanitisedAttrs = sanitizeAttributes(options.attributes)

const sampler = getActiveConfig().sampling.headSampler
const config = getActiveConfig()
if (!config) throw new Error('Config is undefined. This is a bug in the instrumentation logic')

const sampler = config.sampling.headSampler
const samplingDecision = sampler.shouldSample(context, traceId, name, spanKind, sanitisedAttrs, [])
const { decision, traceState, attributes: attrs } = samplingDecision
const attributes = Object.assign({}, sanitisedAttrs, attrs)
Expand Down