Skip to content

Commit 519fbce

Browse files
authored
Fix MongoDB DBM propagation issue where trace comments are not properly 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
1 parent f7cc344 commit 519fbce

3 files changed

Lines changed: 38 additions & 47 deletions

File tree

packages/datadog-plugin-mongodb-core/src/index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MongodbCorePlugin extends DatabasePlugin {
2525
'out.port': options.port
2626
}
2727
})
28-
ops = this.injectDbmCommand(span, ops, service)
28+
ops.comment = this.injectDbmComment(span, ops.comment, service)
2929
}
3030

3131
getPeerService (tags) {
@@ -37,28 +37,25 @@ class MongodbCorePlugin extends DatabasePlugin {
3737
return super.getPeerService(tags)
3838
}
3939

40-
injectDbmCommand (span, command, serviceName) {
40+
injectDbmComment (span, comment, serviceName) {
4141
const dbmTraceComment = this.createDbmComment(span, serviceName)
4242

4343
if (!dbmTraceComment) {
44-
return command
44+
return comment
4545
}
4646

47-
// create a copy of the command to avoid mutating the original
48-
const dbmTracedCommand = { ...command }
49-
50-
if (dbmTracedCommand.comment) {
47+
if (comment) {
5148
// if the command already has a comment, append the dbm trace comment
52-
if (typeof dbmTracedCommand.comment === 'string') {
53-
dbmTracedCommand.comment += `,${dbmTraceComment}`
54-
} else if (Array.isArray(dbmTracedCommand.comment)) {
55-
dbmTracedCommand.comment.push(dbmTraceComment)
49+
if (typeof comment === 'string') {
50+
comment += `,${dbmTraceComment}`
51+
} else if (Array.isArray(comment)) {
52+
comment.push(dbmTraceComment)
5653
} // do nothing if the comment is not a string or an array
5754
} else {
58-
dbmTracedCommand.comment = dbmTraceComment
55+
comment = dbmTraceComment
5956
}
6057

61-
return dbmTracedCommand
58+
return comment
6259
}
6360
}
6461

packages/datadog-plugin-mongodb-core/test/core.spec.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Plugin', () => {
3333
let id
3434
let tracer
3535
let collection
36-
let injectDbmCommandSpy
36+
let startSpy
3737

3838
describe('mongodb-core (core)', () => {
3939
withTopologies(getServer => {
@@ -426,22 +426,21 @@ describe('Plugin', () => {
426426

427427
server.connect()
428428

429-
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
429+
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
430430
})
431431

432432
afterEach(() => {
433-
injectDbmCommandSpy?.restore()
433+
startSpy?.restore()
434434
})
435435

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

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

465-
expect(injectDbmCommandSpy.called).to.be.true
466-
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
467-
expect(instrumentedCommand).to.have.property('comment')
468-
expect(instrumentedCommand.comment).to.equal(
464+
expect(startSpy.called).to.be.true
465+
const { comment } = startSpy.getCall(0).args[0].ops
466+
expect(comment).to.equal(
469467
'test comment,' +
470468
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
471469
'dddbs=\'test-mongodb\',' +
@@ -493,10 +491,9 @@ describe('Plugin', () => {
493491
.use(traces => {
494492
const span = traces[0][0]
495493

496-
expect(injectDbmCommandSpy.called).to.be.true
497-
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
498-
expect(instrumentedCommand).to.have.property('comment')
499-
expect(instrumentedCommand.comment).to.deep.equal([
494+
expect(startSpy.called).to.be.true
495+
const { comment } = startSpy.getCall(0).args[0].ops
496+
expect(comment).to.deep.equal([
500497
'test comment',
501498
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
502499
'dddbs=\'test-mongodb\',' +
@@ -543,11 +540,11 @@ describe('Plugin', () => {
543540

544541
server.connect()
545542

546-
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
543+
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
547544
})
548545

549546
afterEach(() => {
550-
injectDbmCommandSpy?.restore()
547+
startSpy?.restore()
551548
})
552549

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

560-
expect(injectDbmCommandSpy.called).to.be.true
561-
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
562-
expect(instrumentedCommand).to.have.property('comment')
563-
expect(instrumentedCommand.comment).to.equal(
557+
expect(startSpy.called).to.be.true
558+
const { comment } = startSpy.getCall(0).args[0].ops
559+
expect(comment).to.equal(
564560
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
565561
'dddbs=\'test-mongodb\',' +
566562
'dde=\'tester\',' +

packages/datadog-plugin-mongodb-core/test/mongodb.spec.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Plugin', () => {
4848
let collection
4949
let db
5050
let BSON
51-
let injectDbmCommandSpy
51+
let startSpy
5252

5353
describe('mongodb-core', () => {
5454
withTopologies(createClient => {
@@ -375,22 +375,21 @@ describe('Plugin', () => {
375375
db = client.db('test')
376376
collection = db.collection(collectionName)
377377

378-
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
378+
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
379379
})
380380

381381
afterEach(() => {
382-
injectDbmCommandSpy?.restore()
382+
startSpy?.restore()
383383
})
384384

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

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

428-
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
427+
startSpy = sinon.spy(MongodbCorePlugin.prototype, 'start')
429428
})
430429

431430
afterEach(() => {
432-
injectDbmCommandSpy?.restore()
431+
startSpy?.restore()
433432
})
434433

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

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

0 commit comments

Comments
 (0)