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 integration not instrumenting correctly #1159

Merged
merged 1 commit into from
Nov 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
175 changes: 3 additions & 172 deletions packages/datadog-plugin-mongodb-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,175 +1,6 @@
'use strict'

const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const unified = require('./unified')
const legacy = require('./legacy')

function createWrapOperation (tracer, config, operationName) {
return function wrapOperation (operation) {
return function operationWithTrace (ns, ops) {
const index = arguments.length - 1
const callback = arguments[index]

if (typeof callback !== 'function') return operation.apply(this, arguments)

const scope = tracer.scope()
const childOf = scope.active()
const span = tracer.startSpan('mongodb.query', { childOf })

addTags(span, tracer, config, ns, ops, this, operationName)

analyticsSampler.sample(span, config.analytics)

arguments[index] = wrapCallback(tracer, span, callback)

return scope.bind(operation, span).apply(this, arguments)
}
}
}

function createWrapNext (tracer, config) {
return function wrapNext (next) {
return function nextWithTrace (cb) {
const scope = tracer.scope()
const childOf = scope.active()
const span = tracer.startSpan('mongodb.query', { childOf })

addTags(span, tracer, config, this.ns, this.cmd, this.topology)

if (this.cursorState) {
span.addTags({
'mongodb.cursor.index': this.cursorState.cursorIndex
})
}

scope.bind(next, span).call(this, wrapCallback(tracer, span, cb, this))
}
}
}

function addTags (span, tracer, config, ns, cmd, topology, operationName) {
const query = getQuery(cmd)
const resource = getResource(ns, cmd, query, operationName)

span.addTags({
'service.name': config.service || `${tracer._service}-mongodb`,
'resource.name': resource,
'span.type': 'mongodb',
'db.name': ns
})

if (query) {
span.setTag('mongodb.query', query)
}

addHost(span, topology)
}

function addHost (span, topology) {
const options = topology && topology.s && topology.s.options

if (options && options.host && options.port) {
span.addTags({
'out.host': topology.s.options.host,
'out.port': topology.s.options.port
})
}
}

function wrapCallback (tracer, span, done, cursor) {
return tracer.scope().bind((err, res) => {
if (err) {
span.addTags({
'error.type': err.name,
'error.msg': err.message,
'error.stack': err.stack
})
}

if (cursor) {
addHost(span, cursor.server)
}

span.finish()

if (done) {
done(err, res)
}
})
}

function getQuery (cmd) {
return cmd.query && JSON.stringify(sanitize(cmd.query))
}

function getResource (ns, cmd, query, operationName) {
if (!operationName) {
operationName = Object.keys(cmd)[0]
}

const parts = [operationName, ns]

if (query) {
parts.push(query)
}

return parts.join(' ')
}

function sanitize (input) {
const output = {}

if (!isObject(input) || Buffer.isBuffer(input) || isBSON(input)) return '?'

for (const key in input) {
if (typeof input[key] === 'function') continue

output[key] = sanitize(input[key])
}

return output
}

function isObject (val) {
return typeof val === 'object' && val !== null && !(val instanceof Array)
}

function isBSON (val) {
return val && val._bsontype
}

function patch (core, tracer, config) {
this.wrap(core.Server.prototype, 'command', createWrapOperation(tracer, config))
this.wrap(core.Server.prototype, 'insert', createWrapOperation(tracer, config, 'insert'))
this.wrap(core.Server.prototype, 'update', createWrapOperation(tracer, config, 'update'))
this.wrap(core.Server.prototype, 'remove', createWrapOperation(tracer, config, 'remove'))

if (core.Cursor.prototype.next) {
this.wrap(core.Cursor.prototype, 'next', createWrapNext(tracer, config))
} else if (core.Cursor.prototype._next) {
this.wrap(core.Cursor.prototype, '_next', createWrapNext(tracer, config))
}
}

function unpatch (core) {
this.unwrap(core.Server.prototype, 'command')
this.unwrap(core.Server.prototype, 'insert')
this.unwrap(core.Server.prototype, 'update')
this.unwrap(core.Server.prototype, 'remove')
this.unwrap(core.Cursor.prototype, 'next')
this.unwrap(core.Cursor.prototype, '_next')
}

module.exports = [
{
name: 'mongodb',
versions: ['>=3.3'],
file: 'lib/core/index.js',
patch,
unpatch
},
{
name: 'mongodb-core',
versions: ['>=2'],
patch,
unpatch
}
]
module.exports = [].concat(unified, legacy)
59 changes: 59 additions & 0 deletions packages/datadog-plugin-mongodb-core/src/legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const { instrument } = require('./util')

function createWrapCommand (tracer, config, name) {
return function wrapCommand (command) {
return function commandWithTrace (ns, ops) {
return instrument(command, this, arguments, this, ns, ops, tracer, config, { name })
}
}
}

function createWrapQuery (tracer, config) {
return function wrapQuery (query) {
return function queryWithTrace () {
const pool = this.server.s.pool
const ns = this.ns
const ops = this.cmd

return instrument(query, this, arguments, pool, ns, ops, tracer, config)
}
}
}

function createWrapCursor (tracer, config, name) {
return function wrapCursor (cursor) {
return function cursorWithTrace () {
const pool = this.server.s.pool
const ns = this.ns

return instrument(cursor, this, arguments, pool, ns, {}, tracer, config, { name })
}
}
}

module.exports = [
{
name: 'mongodb-core',
versions: ['2 - 3.1.9'],
patch ({ Cursor, Server }, tracer, config) {
this.wrap(Server.prototype, 'command', createWrapCommand(tracer, config))
this.wrap(Server.prototype, 'insert', createWrapCommand(tracer, config, 'insert'))
this.wrap(Server.prototype, 'update', createWrapCommand(tracer, config, 'update'))
this.wrap(Server.prototype, 'remove', createWrapCommand(tracer, config, 'remove'))
this.wrap(Cursor.prototype, '_getmore', createWrapCursor(tracer, config, 'getMore'))
this.wrap(Cursor.prototype, '_find', createWrapQuery(tracer, config))
this.wrap(Cursor.prototype, 'kill', createWrapCursor(tracer, config, 'killCursors'))
},
unpatch ({ Cursor, Server }) {
this.unwrap(Server.prototype, 'command')
this.unwrap(Server.prototype, 'insert')
this.unwrap(Server.prototype, 'update')
this.unwrap(Server.prototype, 'remove')
this.unwrap(Cursor.prototype, '_getmore')
this.unwrap(Cursor.prototype, '_find')
this.unwrap(Cursor.prototype, 'kill')
}
}
]
70 changes: 70 additions & 0 deletions packages/datadog-plugin-mongodb-core/src/unified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

const { instrument } = require('./util')

function createWrapCommand (tracer, config, name) {
return function wrapCommand (command) {
return function commandWithTrace (server, ns, ops) {
return instrument(command, this, arguments, server, ns, ops, tracer, config, { name })
}
}
}

function patch (wp, tracer, config) {
this.wrap(wp, 'command', createWrapCommand(tracer, config))
this.wrap(wp, 'insert', createWrapCommand(tracer, config, 'insert'))
this.wrap(wp, 'update', createWrapCommand(tracer, config, 'update'))
this.wrap(wp, 'remove', createWrapCommand(tracer, config, 'remove'))
this.wrap(wp, 'query', createWrapCommand(tracer, config))
this.wrap(wp, 'getMore', createWrapCommand(tracer, config, 'getMore'))
this.wrap(wp, 'killCursors', createWrapCommand(tracer, config, 'killCursors'))
}

function unpatch (wp) {
this.unwrap(wp, 'command')
this.unwrap(wp, 'insert')
this.unwrap(wp, 'update')
this.unwrap(wp, 'remove')
this.unwrap(wp, 'query')
this.unwrap(wp, 'getMore')
this.unwrap(wp, 'killCursors')
}

function patchClass (WireProtocol, tracer, config) {
this.wrap(WireProtocol.prototype, 'command', createWrapCommand(tracer, config))
}

function unpatchClass (WireProtocol) {
this.unwrap(WireProtocol.prototype, 'command')
}

module.exports = [
{
name: 'mongodb',
versions: ['>=3.3'],
file: 'lib/core/wireprotocol/index.js',
patch,
unpatch
},
{
name: 'mongodb-core',
versions: ['>=3.2'],
file: 'lib/wireprotocol/index.js',
patch,
unpatch
},
{
name: 'mongodb-core',
versions: ['~3.1.10'],
file: 'lib/wireprotocol/3_2_support.js',
patch: patchClass,
unpatch: unpatchClass
},
{
name: 'mongodb-core',
versions: ['~3.1.10'],
file: 'lib/wireprotocol/2_6_support.js',
patch: patchClass,
unpatch: unpatchClass
}
]