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

[ASM] Discard inferred spans when resolving the root span of a trace #4881

Merged
merged 5 commits into from
Nov 21, 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
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) {
simon-id marked this conversation as resolved.
Show resolved Hide resolved
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) break

parentId = pContext._parentId

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

return span
}

module.exports = {
Expand Down
166 changes: 166 additions & 0 deletions packages/dd-trace/test/appsec/sdk/utils.spec.js
CarlesDD marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
'use strict'

const { assert } = require('chai')

const { getRootSpan } = require('../../../src/appsec/sdk/utils')
const DatadogTracer = require('../../../src/tracer')
const Config = require('../../../src/config')
const id = require('../../../src/id')

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

before(() => {
tracer = new DatadogTracer(new Config({
enabled: true
}))
})

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

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

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

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

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

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

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

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

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(root, childOf)
})
})

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(root, child1)
})
})

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(root, childOf)
})
})

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(root, child1)
})
})

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(root, child12)
})
})
})

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(root, childOf)
})
})
})

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(root, childOf)
})
})
})
})

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(root, child1211)
})
})
})
})
})
})
Loading