Skip to content

Commit

Permalink
Fix MongoDB DBM propagation issue where trace comments are not proper…
Browse files Browse the repository at this point in the history
…ly injected into commands (#5306)

* comment has to be assigned back to the input ops to ensure incoming command is modified before execution

* input comment to injectDbmCommand

* rename injectDbmCommand to injectDbmComment
  • Loading branch information
lu-zhengda authored and watson committed Feb 27, 2025
1 parent 1a297b0 commit f0221bc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
23 changes: 10 additions & 13 deletions packages/datadog-plugin-mongodb-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MongodbCorePlugin extends DatabasePlugin {
'out.port': options.port
}
})
ops = this.injectDbmCommand(span, ops, service)
ops.comment = this.injectDbmComment(span, ops.comment, service)
}

getPeerService (tags) {
Expand All @@ -37,28 +37,25 @@ class MongodbCorePlugin extends DatabasePlugin {
return super.getPeerService(tags)
}

injectDbmCommand (span, command, serviceName) {
injectDbmComment (span, comment, serviceName) {
const dbmTraceComment = this.createDbmComment(span, serviceName)

if (!dbmTraceComment) {
return command
return comment
}

// create a copy of the command to avoid mutating the original
const dbmTracedCommand = { ...command }

if (dbmTracedCommand.comment) {
if (comment) {
// if the command already has a comment, append the dbm trace comment
if (typeof dbmTracedCommand.comment === 'string') {
dbmTracedCommand.comment += `,${dbmTraceComment}`
} else if (Array.isArray(dbmTracedCommand.comment)) {
dbmTracedCommand.comment.push(dbmTraceComment)
if (typeof comment === 'string') {
comment += `,${dbmTraceComment}`
} else if (Array.isArray(comment)) {
comment.push(dbmTraceComment)
} // do nothing if the comment is not a string or an array
} else {
dbmTracedCommand.comment = dbmTraceComment
comment = dbmTraceComment
}

return dbmTracedCommand
return comment
}
}

Expand Down
38 changes: 17 additions & 21 deletions packages/datadog-plugin-mongodb-core/test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Plugin', () => {
let id
let tracer
let collection
let injectDbmCommandSpy
let startSpy

describe('mongodb-core (core)', () => {
withTopologies(getServer => {
Expand Down Expand Up @@ -426,22 +426,21 @@ describe('Plugin', () => {

server.connect()

injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
})

afterEach(() => {
injectDbmCommandSpy?.restore()
startSpy?.restore()
})

it('DBM propagation should inject service mode as comment', done => {
agent
.use(traces => {
const span = traces[0][0]

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.equal(
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.equal(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
Expand All @@ -462,10 +461,9 @@ describe('Plugin', () => {
.use(traces => {
const span = traces[0][0]

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.equal(
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.equal(
'test comment,' +
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
Expand Down Expand Up @@ -493,10 +491,9 @@ describe('Plugin', () => {
.use(traces => {
const span = traces[0][0]

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.deep.equal([
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.deep.equal([
'test comment',
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
Expand Down Expand Up @@ -543,11 +540,11 @@ describe('Plugin', () => {

server.connect()

injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
})

afterEach(() => {
injectDbmCommandSpy?.restore()
startSpy?.restore()
})

it('DBM propagation should inject full mode with traceparent as comment', done => {
Expand All @@ -557,10 +554,9 @@ describe('Plugin', () => {
const traceId = span.meta['_dd.p.tid'] + span.trace_id.toString(16).padStart(16, '0')
const spanId = span.span_id.toString(16).padStart(16, '0')

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.equal(
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.equal(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
Expand Down
24 changes: 11 additions & 13 deletions packages/datadog-plugin-mongodb-core/test/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Plugin', () => {
let collection
let db
let BSON
let injectDbmCommandSpy
let startSpy

describe('mongodb-core', () => {
withTopologies(createClient => {
Expand Down Expand Up @@ -375,22 +375,21 @@ describe('Plugin', () => {
db = client.db('test')
collection = db.collection(collectionName)

injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
})

afterEach(() => {
injectDbmCommandSpy?.restore()
startSpy?.restore()
})

it('DBM propagation should inject service mode as comment', done => {
agent
.use(traces => {
const span = traces[0][0]

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.equal(
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.equal(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
Expand Down Expand Up @@ -425,11 +424,11 @@ describe('Plugin', () => {
db = client.db('test')
collection = db.collection(collectionName)

injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
})

afterEach(() => {
injectDbmCommandSpy?.restore()
startSpy?.restore()
})

it('DBM propagation should inject full mode with traceparent as comment', done => {
Expand All @@ -439,10 +438,9 @@ describe('Plugin', () => {
const traceId = span.meta['_dd.p.tid'] + span.trace_id.toString(16).padStart(16, '0')
const spanId = span.span_id.toString(16).padStart(16, '0')

expect(injectDbmCommandSpy.called).to.be.true
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
expect(instrumentedCommand).to.have.property('comment')
expect(instrumentedCommand.comment).to.equal(
expect(startSpy.called).to.be.true
const { comment } = startSpy.getCall(0).args[0].ops
expect(comment).to.equal(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
Expand Down

0 comments on commit f0221bc

Please sign in to comment.