Skip to content

Commit

Permalink
visit upwards parent spans
Browse files Browse the repository at this point in the history
  • Loading branch information
iunanua committed Nov 13, 2024
1 parent 9794630 commit 5195615
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/dd-trace/src/appsec/sdk/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
'use strict'

function getRootSpan (tracer) {
const span = tracer.scope().active()
return span && span.context()._trace.started[0]
let span = tracer.scope().active()
if (!span) return

const context = span.context()
const started = context._trace.started

let parentId = context._parentId
while (parentId) {
const parent = started.find(s => s.context()._spanId === parentId)
const pContext = parent?.context()

if (pContext) {
parentId = pContext._parentId

if (!pContext._tags?._inferred_span) {
span = parent
}
}
}

return span
}

module.exports = {
Expand Down
155 changes: 155 additions & 0 deletions packages/dd-trace/test/appsec/sdk/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
'use strict'

const { assert } = require('chai')

const { getRootSpan } = require('../../../src/appsec/sdk/utils')
const Tracer = require('../../../src/proxy')
const Config = require('../../../src/config')

describe('Appsec SDK utils', () => {
let tracer

before(() => {
tracer = new Tracer(new Config({
enabled: true
}))
tracer.init()
})

describe('getRootSpan', () => {
it('should return root span if there are no childs', () => {
tracer.trace('parent', { }, parent => {
const root = getRootSpan(tracer)

assert.equal(parent, root)
})
})

it('should return root span of single child', () => {
const childOf = tracer.startSpan('parent')

tracer.trace('child1', { childOf }, child1 => {
const root = getRootSpan(tracer)

assert.equal(childOf, root)
})
})

it('should return root span of multiple child', () => {
const childOf = tracer.startSpan('parent')

tracer.trace('child1.1', { childOf }, child11 => {
tracer.trace('child1.1.2', { childOf: child11 }, child112 => {})
})
tracer.trace('child1.2', { childOf }, child12 => {
const root = getRootSpan(tracer)

assert.equal(childOf, root)
})
})

it('should return root span of single child discarding inferred spans', () => {
const childOf = tracer.startSpan('parent')
childOf.setTag('_inferred_span', {})

tracer.trace('child1', { childOf }, child1 => {
const root = getRootSpan(tracer)

assert.equal(child1, root)
})
})

it('should return root span of an inferred span', () => {
const childOf = tracer.startSpan('parent')

tracer.trace('child1', { childOf }, child1 => {
child1.setTag('_inferred_span', {})

const root = getRootSpan(tracer)

assert.equal(childOf, root)
})
})

it('should return root span of an inferred span with inferred parent', () => {
const childOf = tracer.startSpan('parent')
childOf.setTag('_inferred_span', {})

tracer.trace('child1', { childOf }, child1 => {
child1.setTag('_inferred_span', {})

const root = getRootSpan(tracer)

assert.equal(child1, root)
})
})

it('should return root span discarding inferred spans (mutiple childs)', () => {
const childOf = tracer.startSpan('parent')
childOf.setTag('_inferred_span', {})

tracer.trace('child1.1', { childOf }, child11 => {})
tracer.trace('child1.2', { childOf }, child12 => {
tracer.trace('child1.2.1', { childOf: child12 }, child121 => {
const root = getRootSpan(tracer)

assert.equal(child12, root)
})
})
})

it('should return root span discarding inferred spans if it is direct parent (mutiple childs)', () => {
const childOf = tracer.startSpan('parent')

tracer.trace('child1.1', { childOf }, child11 => {})
tracer.trace('child1.2', { childOf }, child12 => {
child12.setTag('_inferred_span', {})

tracer.trace('child1.2.1', { childOf: child12 }, child121 => {
const root = getRootSpan(tracer)

assert.equal(childOf, root)
})
})
})

it('should return root span discarding multiple inferred spans', () => {
const childOf = tracer.startSpan('parent')

tracer.trace('child1.1', { childOf }, child11 => {})
tracer.trace('child1.2', { childOf }, child12 => {
child12.setTag('_inferred_span', {})

tracer.trace('child1.2.1', { childOf: child12 }, child121 => {
child121.setTag('_inferred_span', {})

tracer.trace('child1.2.1.1', { childOf: child121 }, child1211 => {
const root = getRootSpan(tracer)

assert.equal(childOf, root)
})
})
})
})

it('should return itself as root span if all are inferred spans', () => {
const childOf = tracer.startSpan('parent')
childOf.setTag('_inferred_span', {})

tracer.trace('child1.1', { childOf }, child11 => {})
tracer.trace('child1.2', { childOf }, child12 => {
child12.setTag('_inferred_span', {})

tracer.trace('child1.2.1', { childOf: child12 }, child121 => {
child121.setTag('_inferred_span', {})

tracer.trace('child1.2.1.1', { childOf: child121 }, child1211 => {
const root = getRootSpan(tracer)

assert.equal(child1211, root)
})
})
})
})
})
})

0 comments on commit 5195615

Please sign in to comment.