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

Inject trace info as comment to MongoDB operation when dbm propagation is enabled. #5230

Merged
merged 18 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 16 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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ services:
ports:
- "127.0.0.1:6379:6379"
mongo:
image: circleci/mongo:3.6
image: circleci/mongo:4.4
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump mongo image to 4.4 because prior to 4.4 only str comments are allowed. newer mongo version allows us to test array comments.

platform: linux/amd64
ports:
- "127.0.0.1:27017:27017"
Expand Down
30 changes: 28 additions & 2 deletions packages/datadog-plugin-mongodb-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class MongodbCorePlugin extends DatabasePlugin {
start ({ ns, ops, options = {}, name }) {
const query = getQuery(ops)
const resource = truncate(getResource(this, ns, query, name))
this.startSpan(this.operationName(), {
service: this.serviceName({ pluginConfig: this.config }),
const service = this.serviceName({ pluginConfig: this.config })
const span = this.startSpan(this.operationName(), {
service,
resource,
type: 'mongodb',
kind: 'client',
Expand All @@ -24,6 +25,7 @@ class MongodbCorePlugin extends DatabasePlugin {
'out.port': options.port
}
})
ops = this.injectDbmCommand(span, ops, service)
lu-zhengda marked this conversation as resolved.
Show resolved Hide resolved
}

getPeerService (tags) {
Expand All @@ -34,6 +36,30 @@ class MongodbCorePlugin extends DatabasePlugin {
}
return super.getPeerService(tags)
}

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

if (!dbmTraceComment) {
return command
}

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

if (dbmTracedCommand.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(command.comment)) {
lu-zhengda marked this conversation as resolved.
Show resolved Hide resolved
dbmTracedCommand.comment.push(dbmTraceComment)
} // do nothing if the comment is not a string or an array
} else {
dbmTracedCommand.comment = dbmTraceComment
}

return dbmTracedCommand
}
}

function sanitizeBigInt (data) {
Expand Down
180 changes: 180 additions & 0 deletions packages/datadog-plugin-mongodb-core/test/core.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict'

const sinon = require('sinon')
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
const { expectedSchema, rawExpectedSchema } = require('./naming')

const MongodbCorePlugin = require('../../datadog-plugin-mongodb-core/src/index')
const ddpv = require('mocha/package.json').version

const withTopologies = fn => {
withVersions('mongodb-core', ['mongodb-core', 'mongodb'], '<4', (version, moduleName) => {
describe('using the server topology', () => {
Expand All @@ -29,6 +33,7 @@
let id
let tracer
let collection
let injectDbmCommandSpy

describe('mongodb-core (core)', () => {
withTopologies(getServer => {
Expand Down Expand Up @@ -397,6 +402,181 @@
}
)
})

describe('with dbmPropagationMode service', () => {
before(() => {
return agent.load('mongodb-core', { dbmPropagationMode: 'service' })
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(done => {
const Server = getServer()

server = new Server({
host: '127.0.0.1',
port: 27017,
reconnect: false
})

server.on('connect', () => done())
server.on('error', done)

server.connect()

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

afterEach(() => {
injectDbmCommandSpy?.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(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}'`
)
})
.then(done)
.catch(done)

server.insert(`test.${collection}`, [{ a: 1 }], () => {})
})

it('DBM propagation should inject service mode after eixsting str 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(
`test comment,` +

Check failure on line 469 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}'`
)
})
.then(done)
.catch(done)

server.command(`test.${collection}`, {

Check failure on line 482 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 10 spaces but found 12
find: `test.${collection}`,

Check failure on line 483 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 12 spaces but found 14
query: {

Check failure on line 484 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 12 spaces but found 14
_id: Buffer.from('1234')

Check failure on line 485 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 14 spaces but found 16
},

Check failure on line 486 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 12 spaces but found 14
comment: "test comment"

Check failure on line 487 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 12 spaces but found 14

Check failure on line 487 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
}, () => {})

Check failure on line 488 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 10 spaces but found 12
})

it('DBM propagation should inject service mode after eixsting array 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.deep.equal([
`test comment`,

Check failure on line 500 in packages/datadog-plugin-mongodb-core/test/core.spec.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}'`
])
})
.then(done)
.catch(done)

server.command(`test.${collection}`, {
find: `test.${collection}`,
query: {
_id: Buffer.from('1234')
},
comment: ["test comment"]
}, () => {})
})
})

describe('with dbmPropagationMode full', () => {
before(() => {
return agent.load('mongodb-core', { dbmPropagationMode: 'full' })
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(done => {
const Server = getServer()

server = new Server({
host: '127.0.0.1',
port: 27017,
reconnect: false
})

server.on('connect', () => done())
server.on('error', done)

server.connect()

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

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

it('DBM propagation should inject full mode with traceparent as comment', done => {
agent
.use(traces => {
const span = traces[0][0]
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(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}',` +
`traceparent='00-${traceId}-${spanId}-00'`
)
})
.then(done)
.catch(done)

server.insert(`test.${collection}`, [{ a: 1 }], () => {})
})
})
})
})
})
108 changes: 108 additions & 0 deletions packages/datadog-plugin-mongodb-core/test/mongodb.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict'

const sinon = require('sinon')
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { expectedSchema, rawExpectedSchema } = require('./naming')

const MongodbCorePlugin = require('../../datadog-plugin-mongodb-core/src/index')
const ddpv = require('mocha/package.json').version

const withTopologies = fn => {
const isOldNode = semver.satisfies(process.version, '<=14')
const range = isOldNode ? '>=2 <6' : '>=2' // TODO: remove when 3.x support is removed.
Expand Down Expand Up @@ -44,6 +48,7 @@ describe('Plugin', () => {
let collection
let db
let BSON
let injectDbmCommandSpy

describe('mongodb-core', () => {
withTopologies(createClient => {
Expand Down Expand Up @@ -334,6 +339,109 @@ describe('Plugin', () => {
}
)
})

describe('with dbmPropagationMode service', () => {
before(() => {
return agent.load('mongodb-core', {
dbmPropagationMode: 'service'
})
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(async () => {
client = await createClient()
db = client.db('test')
collection = db.collection(collectionName)

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

afterEach(() => {
injectDbmCommandSpy?.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(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}'`
)
})
.then(done)
.catch(done)

collection.find({
_id: Buffer.from('1234')
}).toArray()
})
})

describe('with dbmPropagationMode full', () => {
before(() => {
return agent.load('mongodb-core', {
dbmPropagationMode: 'full'
})
})

after(() => {
return agent.close({ ritmReset: false })
})

beforeEach(async () => {
client = await createClient()
db = client.db('test')
collection = db.collection(collectionName)

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

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

it('DBM propagation should inject full mode with traceparent as comment', done => {
agent
.use(traces => {
const span = traces[0][0]
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(
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
'dddbs=\'test-mongodb\',' +
'dde=\'tester\',' +
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
`ddps='${encodeURIComponent(span.meta.service)}',` +
`ddpv='${ddpv}',` +
`ddprs='${encodeURIComponent(span.meta['peer.service'])}',` +
`traceparent='00-${traceId}-${spanId}-00'`
)
})
.then(done)
.catch(done)

collection.find({
_id: Buffer.from('1234')
}).toArray()
})
})
})
})
})
Loading
Loading