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

Fix MongoDB DBM propagation issue where trace comments are not properly injected into commands #5306

Merged
merged 3 commits into from
Feb 24, 2025
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: 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
Loading