Skip to content

Commit

Permalink
fix: ensure correct run context for 'mongodb-core' instrumentation (#…
Browse files Browse the repository at this point in the history
…2548)

Refs: #2430
  • Loading branch information
trentm authored and Alan Storm committed Feb 3, 2022
1 parent bdf4a34 commit f132ae8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ Notes:
[float]
===== Bug fixes
* Fixes for run context handling for 'mongodb-core' instrumentation.
({issues}2430[#2430])
[[release-notes-3.28.0]]
[[release-notes-3.27.0]]
==== 3.27.0 2022/01/17
[float]
Expand Down
14 changes: 8 additions & 6 deletions lib/instrumentation/modules/mongodb-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports = function (mongodb, agent, { version, enabled }) {
return mongodb
}

const ins = agent._instrumentation

if (mongodb.Server) {
agent.logger.debug('shimming mongodb-core.Server.prototype.command')
shimmer.wrap(mongodb.Server.prototype, 'command', wrapCommand)
Expand Down Expand Up @@ -49,9 +51,9 @@ module.exports = function (mongodb, agent, { version, enabled }) {
else if (cmd.count) type = 'count'
else type = 'command'

span = agent.startSpan(ns + '.' + type, 'db', 'mongodb', 'query')
span = ins.createSpan(ns + '.' + type, 'db', 'mongodb', 'query')
if (span) {
arguments[index] = wrappedCallback
arguments[index] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
}
}
}
Expand All @@ -78,9 +80,9 @@ module.exports = function (mongodb, agent, { version, enabled }) {
var index = arguments.length - 1
var cb = arguments[index]
if (typeof cb === 'function') {
span = agent.startSpan(ns + '.' + name, 'db', 'mongodb', 'query')
span = ins.createSpan(ns + '.' + name, 'db', 'mongodb', 'query')
if (span) {
arguments[index] = wrappedCallback
arguments[index] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
}
}
}
Expand All @@ -107,10 +109,10 @@ module.exports = function (mongodb, agent, { version, enabled }) {
if (typeof cb === 'function') {
if (name !== 'next' || !this[firstSpan]) {
var spanName = `${this.ns}.${this.cmd.find ? 'find' : name}`
span = agent.startSpan(spanName, 'db', 'mongodb', 'query')
span = ins.createSpan(spanName, 'db', 'mongodb', 'query')
}
if (span) {
arguments[0] = wrappedCallback
arguments[0] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
if (name === 'next') {
this[firstSpan] = true
}
Expand Down
36 changes: 22 additions & 14 deletions test/instrumentation/modules/mongodb-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ test('instrument simple command', function (t) {

t.strictEqual(data.transactions.length, 1)
var trans = data.transactions[0]
t.strictEqual(trans.name, 'foo')
t.strictEqual(trans.type, 'bar')
t.strictEqual(trans.result, 'success')
t.strictEqual(trans.name, 'foo', 'transaction.name')
t.strictEqual(trans.type, 'bar', 'transaction.type')
t.strictEqual(trans.result, 'success', 'transaction.result')

// Ensure spans are sorted by start time.
data.spans = data.spans.sort((a, b) => {
Expand Down Expand Up @@ -69,6 +69,7 @@ test('instrument simple command', function (t) {
t.strictEqual(span.type, 'db', 'span has expected type')
t.strictEqual(span.subtype, 'mongodb', 'span has expected subtype')
t.strictEqual(span.action, 'query', 'span has expected action')
t.strictEqual(span.parent_id, trans.id, 'span is a direct child of transaction')
var offset = span.timestamp - trans.timestamp
t.ok(offset + span.duration * 1000 < trans.duration * 1000,
`span ends (${span.timestamp / 1000 + span.duration}ms) before the transaction (${trans.timestamp / 1000 + trans.duration}ms)`)
Expand All @@ -86,47 +87,54 @@ test('instrument simple command', function (t) {
// test example lifted from https://github.com/christkv/mongodb-core/blob/2.0/README.md#connecting-to-mongodb
server.on('connect', function (_server) {
_server.command('system.$cmd', { ismaster: true }, function (err, results) {
t.error(err)
t.strictEqual(results.result.ismaster, true)
t.error(err, 'no error from system.$cmd')
t.strictEqual(results.result.ismaster, true, 'result.ismaster')

_server.insert('elasticapm.test', [{ a: 1 }, { a: 2 }, { a: 3 }], { writeConcern: { w: 1 }, ordered: true }, function (err, results) {
t.error(err)
t.error(err, 'no error from insert')
t.strictEqual(results.result.n, 3)

_server.update('elasticapm.test', [{ q: { a: 1 }, u: { $set: { b: 1 } } }], { writeConcern: { w: 1 }, ordered: true }, function (err, results) {
t.error(err)
t.error(err, 'no error from update')
t.strictEqual(results.result.n, 1)

_server.remove('elasticapm.test', [{ q: { a: 1 }, limit: 1 }], { writeConcern: { w: 1 }, ordered: true }, function (err, results) {
t.error(err)
t.error(err, 'no error from remove')
t.strictEqual(results.result.n, 1)

var cursor = _server.cursor('elasticapm.test', { find: 'elasticapm.test', query: {} })

cursor.next(function (err, doc) {
t.error(err)
t.strictEqual(doc.a, 2)
t.error(err, 'no error from cursor.next')
t.strictEqual(doc.a, 2, 'doc.a')

cursor.next(function (err, doc) {
t.error(err)
t.strictEqual(doc.a, 3)
t.error(err, 'no error from cursor.next')
t.strictEqual(doc.a, 3, 'doc.a')

_server.command('system.$cmd', { ismaster: true }, function (err, result) {
t.error(err)
t.error(err, 'no error from system.$cmd')
agent.endTransaction()

// Cleanup
_server.remove('elasticapm.test', [{ q: {}, limit: 0 }], { writeConcern: { w: 1 }, ordered: true }, function (err, results) {
if (err) throw err
_server.destroy()
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})
t.ok(agent.currentSpan === null, 'no currentSpan in sync code after mongodb-core client command')
})

server.connect()
Expand Down

0 comments on commit f132ae8

Please sign in to comment.