forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set "destination" context for "mongodb" instrumentation (elasti…
…c#1893) Fixes elastic#1892
- Loading branch information
Showing
4 changed files
with
56 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
'use strict' | ||
|
||
const semver = require('semver') | ||
const { getDBDestination } = require('../context') | ||
|
||
// Match expected `<hostname>:<port>`. | ||
const HOSTNAME_PORT_RE = /^([^:]+):(\d+)$/ | ||
|
||
module.exports = (mongodb, agent, { version, enabled }) => { | ||
if (!enabled) return mongodb | ||
if (!semver.satisfies(version, '>=3.3')) { | ||
agent.logger.debug('mongodb version %s not supported - aborting...', version) | ||
agent.logger.debug('mongodb version %s not instrumented (mongodb <3.3 is instrumented via mongodb-core)', version) | ||
return mongodb | ||
} | ||
|
||
|
@@ -19,6 +23,18 @@ module.exports = (mongodb, agent, { version, enabled }) => { | |
return mongodb | ||
|
||
function onStart (event) { | ||
// `event` is a `CommandStartedEvent` | ||
// https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst#api | ||
// E.g. with [email protected]: | ||
// CommandStartedEvent { | ||
// address: '127.0.0.1:27017', | ||
// connectionId: 1, | ||
// requestId: 1, | ||
// databaseName: 'test', | ||
// commandName: 'insert', | ||
// command: | ||
// { ... } } | ||
|
||
const name = [ | ||
event.databaseName, | ||
collectionFor(event), | ||
|
@@ -28,6 +44,19 @@ module.exports = (mongodb, agent, { version, enabled }) => { | |
const span = agent.startSpan(name, 'db', 'mongodb', 'query') | ||
if (span) { | ||
activeSpans.set(event.requestId, span) | ||
|
||
// Per the following code it looks like "<hostname>:<port>" should be | ||
// available via the `address` or `connectionId` field. | ||
// https://github.com/mongodb/node-mongodb-native/blob/dd356f0ede/lib/core/connection/apm.js#L155-L169 | ||
const address = event.address || event.connectionId | ||
let match | ||
if (address && typeof (address) === 'string' && | ||
(match = HOSTNAME_PORT_RE.exec(address))) { | ||
span.setDestinationContext( | ||
getDBDestination(span, match[1], match[2])) | ||
} else { | ||
agent.logger.trace('could not set destination context on mongodb span from address=%j', address) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters