Skip to content

Commit

Permalink
cds.spawn gets new root contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvans committed Jan 14, 2025
1 parent 3329bbd commit a16d828
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions lib/tracing/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,12 @@ function trace(req, fn, that, args, opts = {}) {
// only trace once served and there is a cds.context
if (!tracer || !cds.context) return fn.apply(that, args)

const root = opts.event === 'cds.spawn'

const parent = otel.trace.getActiveSpan()

// if the current request matches instrumentation-http's ignoreIncomingPaths -> abort
if (parent?.isRecording() === false) return fn.apply(that, args)
if (!root && parent?.isRecording() === false) return fn.apply(that, args)

// augment root span with request attributes, overwrite start time, and adjust root name
if (parent?.instrumentationLibrary.name === '@opentelemetry/instrumentation-http' && !parent[$adjusted]) {
Expand All @@ -308,26 +310,15 @@ function trace(req, fn, that, args, opts = {}) {
if (ADJUST_ROOT_NAME && parent.attributes[ATTR_URL_PATH]) parent.name += ' ' + parent.attributes[ATTR_URL_PATH]
}

/*
* create span
*/

// const parentSpan = _getParentSpan()
// if (parentSpan) debugger
// const isAsync = parentSpan?._is_async && !parentSpan?.name.match(/cds\.spawn/)
// const ctx = isAsync
// ? ROOT_CONTEXT
// : parentSpan
// ? otel.trace.setSpan(otel.context.active(), parentSpan)
// : otel.context.active()

let name = typeof req === 'string' ? req : _getSpanName(req, fn, that)
if (name.length > 80 && _truncate_span_name !== false) name = name.substring(0, 79) + '…'

const options = {
kind: _determineKind(that, req?.phase, /* isAsync */ undefined, opts),
attributes: {}
attributes: {},
root
}
if (root && parent) options.links = [{ context: parent.spanContext() }]

// TODO: improve attributes handling
const collector = {
Expand All @@ -336,15 +327,6 @@ function trace(req, fn, that, args, opts = {}) {
}
_addAttributes(collector, fn, that, opts, args, parent)

// TODO: what's this for?
// if (isAsync) {
// spanOptions.links = [{ context: parentSpan.spanContext() }]
// spanOptions.parent = undefined
// }

// TODO: what's this for?
// if (name.event?.match(/^cds\.spawn/) || name?.phase === 'emit') span._is_async = true

// start a new active span, call the original function, and finally end the span
return tracer.startActiveSpan(name, options, async span => {
// in case the span is non-recording, just call the original function
Expand Down

0 comments on commit a16d828

Please sign in to comment.