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

Added integration for mysql2 #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
133 changes: 132 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"mongodb-core": "2.1.17",
"monk": "6.0.5",
"mysql": "2.15.0",
"mysql2": "^1.5.3",
"nock": "9.0.25",
"npm-run-all": "4.1.1",
"nyc": "11.2.1",
Expand Down
69 changes: 40 additions & 29 deletions src/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ const uuidv4 = require('uuid/v4')
const instrumentations = require('./instrumentation')

/**
* @class Instrument
*/
* @class Instrument
*/
class Instrument {
constructor ({
tracers = [],
httpTimings = false
}) {
constructor ({ tracers = [], httpTimings = false }) {
if (!_.isArray(tracers)) {
throw new Error('tracers is required')
}
Expand All @@ -38,66 +35,80 @@ class Instrument {
}

/**
* Applies specified fn of instrumentations
* @method patch
*/
* Applies specified fn of instrumentations
* @method patch
*/
patch () {
const instrumentedModules = _.uniq(instrumentations.map((instrumentation) => instrumentation.module))

// Instrunent modules: hook require
hook(instrumentedModules, (moduleExports, moduleName, moduleBaseDir) =>
this.hookModule(moduleExports, moduleName, moduleBaseDir))
hook(instrumentedModules, this.hookModule.bind(this))

debug('Patched')
}

/**
* Manually hook a module, useful when require-in-the-middle does not work,
* such as when you are using ESM.
* @method hookModule
* @param {Object} module
* @param {string} moduleName
* @param {string} moduleBaseDir
*/
* Manually hook a module, useful when require-in-the-middle does not work,
* such as when you are using ESM.
* @method hookModule
* @param {Object} module
* @param {string} moduleName
* @param {string} moduleBaseDir
*/
hookModule (moduleExports, moduleName, moduleBaseDir) {
let moduleVersion

// Look for version in package.json
if (moduleBaseDir) {
const packageJSON = path.join(moduleBaseDir, 'package.json')
// eslint-disable-next-line
moduleVersion = require(packageJSON).version
moduleVersion = require(packageJSON).version
}

// Apply instrumentations
instrumentations
.filter((instrumentation) => instrumentation.module === moduleName)
.filter((instrumentation) => {
// Core modules don't have version number
if (_.isUndefined(moduleVersion) || !_.isArray(instrumentation.supportedVersions)) {
if (
_.isUndefined(moduleVersion) ||
!_.isArray(instrumentation.supportedVersions)
) {
return true
}

return instrumentation.supportedVersions.some((supportedVersion) =>
semver.satisfies(moduleVersion, supportedVersion))
})
.forEach((instrumentation) => {
instrumentation.patch(moduleExports, this._tracers, this._options)
let moduleToPatch = moduleExports
if (instrumentation.file) {
// eslint-disable-next-line
moduleToPatch = require(path.join(
moduleBaseDir,
instrumentation.file
))
}
instrumentation.patch(moduleToPatch, this._tracers, this._options)
this._instrumented.set(moduleExports, instrumentation)

debug(`Instrumentation "${instrumentation.name}" applied on module "${moduleName}"`, {
moduleVersion,
supportedVersions: instrumentation.supportedVersions
})
debug(
`Instrumentation "${
instrumentation.name
}" applied on module "${moduleName}"`,
{
moduleVersion,
supportedVersions: instrumentation.supportedVersions
}
)
})

return moduleExports
}

/**
* Applies unpatch fn of instrumentations
* @method unpatch
*/
* Applies unpatch fn of instrumentations
* @method unpatch
*/
unpatch () {
this._instrumented.forEach((instrumentation, moduleExports) => {
instrumentation.unpatch(moduleExports)
Expand Down
2 changes: 2 additions & 0 deletions src/instrumentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const expressError = require('./expressError')
const httpClient = require('./httpClient')
const mongodbCore = require('./mongodbCore')
const mysql = require('./mysql')
const mysql2 = require('./mysql2')
const pg = require('./pg')
const redis = require('./redis')
const restify = require('./restify')
Expand All @@ -15,6 +16,7 @@ module.exports = [
httpClient,
mongodbCore,
mysql,
mysql2,
pg,
redis,
restify
Expand Down
Loading