Skip to content

Commit

Permalink
Merge branch 'main' of github.com:evanderkoogh/otel-cf-workers
Browse files Browse the repository at this point in the history
  • Loading branch information
evanderkoogh committed Aug 10, 2024
2 parents 22a3bd1 + 47f5548 commit 6e1bb4c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
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

0 comments on commit 6e1bb4c

Please sign in to comment.