Skip to content

Commit c42c3df

Browse files
authored
restore orphanable only on older versions (#3133)
1 parent 81ad236 commit c42c3df

File tree

3 files changed

+121
-24
lines changed

3 files changed

+121
-24
lines changed

index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export declare interface Tracer extends opentracing.Tracer {
7777
* span will finish when that callback is called.
7878
* * The function doesn't accept a callback and doesn't return a promise, in
7979
* which case the span will finish at the end of the function execution.
80+
*
81+
* If the `orphanable` option is set to false, the function will not be traced
82+
* unless there is already an active span or `childOf` option. Note that this
83+
* option is deprecated and has been removed in version 4.0.
8084
*/
8185
trace<T> (name: string, fn: (span?: Span, fn?: (error?: Error) => any) => T): T;
8286
trace<T> (name: string, options: TraceOptions & SpanOptions, fn: (span?: Span, done?: (error?: Error) => string) => T): T;
@@ -490,6 +494,13 @@ export declare interface TracerOptions {
490494
*/
491495
logLevel?: 'error' | 'debug'
492496

497+
/**
498+
* If false, require a parent in order to trace.
499+
* @default true
500+
* @deprecated since version 4.0
501+
*/
502+
orphanable?: boolean
503+
493504
/**
494505
* Enables DBM to APM link using tag injection.
495506
* @default 'disabled'

packages/dd-trace/src/tracer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { storage } = require('../../datadog-core')
77
const { isError } = require('./util')
88
const { setStartupLogConfig } = require('./startup-log')
99
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
10+
const { MAJOR } = require('../../../version')
1011

1112
const SPAN_TYPE = tags.SPAN_TYPE
1213
const RESOURCE_NAME = tags.RESOURCE_NAME
@@ -26,6 +27,10 @@ class DatadogTracer extends Tracer {
2627
childOf: this.scope().active()
2728
}, options)
2829

30+
if (!options.childOf && options.orphanable === false && MAJOR < 4) {
31+
return fn(null, () => {})
32+
}
33+
2934
const span = this.startSpan(name, options)
3035

3136
addTags(span, options)
@@ -77,6 +82,10 @@ class DatadogTracer extends Tracer {
7782
optionsObj = optionsObj.apply(this, arguments)
7883
}
7984

85+
if (optionsObj && optionsObj.orphanable === false && !tracer.scope().active() && MAJOR < 4) {
86+
return fn.apply(this, arguments)
87+
}
88+
8089
const lastArgId = arguments.length - 1
8190
const cb = arguments[lastArgId]
8291

packages/dd-trace/test/tracer.spec.js

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ const Config = require('../src/config')
88
const tags = require('../../../ext/tags')
99
const { expect } = require('chai')
1010
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
11+
const { MAJOR } = require('../../../version')
1112

1213
const SPAN_TYPE = tags.SPAN_TYPE
1314
const RESOURCE_NAME = tags.RESOURCE_NAME
1415
const SERVICE_NAME = tags.SERVICE_NAME
1516

17+
const describeOrphanable = MAJOR < 4 ? describe : describe.skip
18+
1619
describe('Tracer', () => {
1720
let Tracer
1821
let tracer
@@ -244,23 +247,63 @@ describe('Tracer', () => {
244247
})
245248
})
246249

247-
describe('when there is no parent span', () => {
248-
sinon.spy(tracer, 'startSpan')
250+
describeOrphanable('when there is no parent span', () => {
251+
it('should not trace if `orphanable: false`', () => {
252+
sinon.spy(tracer, 'startSpan')
249253

250-
tracer.trace('name', {}, () => {})
254+
tracer.trace('name', { orphanable: false }, () => {})
251255

252-
expect(tracer.startSpan).to.have.been.called
253-
})
256+
expect(tracer.startSpan).to.have.not.been.called
257+
})
258+
259+
it('should trace if `orphanable: true`', () => {
260+
sinon.spy(tracer, 'startSpan')
254261

255-
describe('when there is a parent span', () => {
256-
tracer.scope().activate(tracer.startSpan('parent'), () => {
257-
// sinon.spy(tracer, 'startSpan')
262+
tracer.trace('name', { orhpanable: true }, () => {})
263+
264+
expect(tracer.startSpan).to.have.been.called
265+
})
266+
267+
it('should trace if `orphanable: undefined`', () => {
268+
sinon.spy(tracer, 'startSpan')
258269

259270
tracer.trace('name', {}, () => {})
260271

261272
expect(tracer.startSpan).to.have.been.called
262273
})
263274
})
275+
276+
describeOrphanable('when there is a parent span', () => {
277+
it('should trace if `orphanable: false`', () => {
278+
tracer.scope().activate(tracer.startSpan('parent'), () => {
279+
sinon.spy(tracer, 'startSpan')
280+
281+
tracer.trace('name', { orhpanable: false }, () => {})
282+
283+
expect(tracer.startSpan).to.have.been.called
284+
})
285+
})
286+
287+
it('should trace if `orphanable: true`', () => {
288+
tracer.scope().activate(tracer.startSpan('parent'), () => {
289+
sinon.spy(tracer, 'startSpan')
290+
291+
tracer.trace('name', { orphanable: true }, () => {})
292+
293+
expect(tracer.startSpan).to.have.been.called
294+
})
295+
})
296+
297+
it('should trace if `orphanable: undefined`', () => {
298+
tracer.scope().activate(tracer.startSpan('parent'), () => {
299+
sinon.spy(tracer, 'startSpan')
300+
301+
tracer.trace('name', {}, () => {})
302+
303+
expect(tracer.startSpan).to.have.been.called
304+
})
305+
})
306+
})
264307
})
265308

266309
describe('getRumData', () => {
@@ -403,39 +446,73 @@ describe('Tracer', () => {
403446
expect(tracer.trace).to.have.not.been.called
404447
})
405448

406-
describe('when there is no parent span', () => {
407-
const fn = tracer.wrap('name', {}, () => {})
449+
describeOrphanable('when there is no parent span', () => {
450+
it('should not trace if `orphanable: false`', () => {
451+
const fn = tracer.wrap('name', { orphanable: false }, () => {})
408452

409-
// sinon.spy(tracer, 'trace')
453+
sinon.spy(tracer, 'trace')
410454

411-
fn()
455+
fn()
412456

413-
expect(tracer.trace).to.have.been.called
414-
})
457+
expect(tracer.trace).to.have.not.been.called
458+
})
415459

416-
describe('when there is a parent span', () => {
417-
tracer.scope().activate(tracer.startSpan('parent'), () => {
460+
it('should trace if `orphanable: true`', () => {
461+
const fn = tracer.wrap('name', { orhpanable: true }, () => {})
462+
463+
sinon.spy(tracer, 'trace')
464+
465+
fn()
466+
467+
expect(tracer.trace).to.have.been.called
468+
})
469+
470+
it('should trace if `orphanable: undefined`', () => {
418471
const fn = tracer.wrap('name', {}, () => {})
419472

420-
// sinon.spy(tracer, 'trace')
473+
sinon.spy(tracer, 'trace')
421474

422475
fn()
423476

424477
expect(tracer.trace).to.have.been.called
425478
})
426479
})
427480

428-
describe('when the options object is a function returning a falsy value', () => {
429-
it('should trace', () => {
430-
const fn = tracer.wrap('name', () => false, () => {})
481+
describeOrphanable('when there is a parent span', () => {
482+
it('should trace if `orphanable: false`', () => {
483+
tracer.scope().activate(tracer.startSpan('parent'), () => {
484+
const fn = tracer.wrap('name', { orhpanable: false }, () => {})
485+
486+
sinon.spy(tracer, 'trace')
487+
488+
fn()
431489

432-
sinon.stub(tracer, 'trace').callsFake((_, options) => {
433-
expect(options).to.equal(false)
490+
expect(tracer.trace).to.have.been.called
434491
})
492+
})
435493

436-
fn()
494+
it('should trace if `orphanable: true`', () => {
495+
tracer.scope().activate(tracer.startSpan('parent'), () => {
496+
const fn = tracer.wrap('name', { orphanable: true }, () => {})
437497

438-
expect(tracer.trace).to.have.been.called
498+
sinon.spy(tracer, 'trace')
499+
500+
fn()
501+
502+
expect(tracer.trace).to.have.been.called
503+
})
504+
})
505+
506+
it('should trace if `orphanable: undefined`', () => {
507+
tracer.scope().activate(tracer.startSpan('parent'), () => {
508+
const fn = tracer.wrap('name', {}, () => {})
509+
510+
sinon.spy(tracer, 'trace')
511+
512+
fn()
513+
514+
expect(tracer.trace).to.have.been.called
515+
})
439516
})
440517
})
441518
})

0 commit comments

Comments
 (0)