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

Issues with mysql2/promise #132

Closed
derN3rd opened this issue Jun 4, 2018 · 6 comments · Fixed by #133
Closed

Issues with mysql2/promise #132

derN3rd opened this issue Jun 4, 2018 · 6 comments · Fixed by #133
Assignees
Labels
bug Something isn't working community integrations

Comments

@derN3rd
Copy link

derN3rd commented Jun 4, 2018

Hey there,

We are using the mysql2 module with require('mysql2/promise') which causes the patch to fail:

/home/max/git/my-api/node_modules/dd-trace/src/plugins/mysql2.js:81
  shimmer.wrap(mysql2.Pool.prototype, 'getConnection', createWrapGetConnection(tracer, config))
                          ^

TypeError: Cannot read property 'prototype' of undefined
    at Object.patch (/home/max/git/my-api/node_modules/dd-trace/src/plugins/mysql2.js:81:27)
    at Array.from.filter.filter.forEach.plugin (/home/max/git/my-api/node_modules/dd-trace/src/instrumenter.js:72:16)
    at Array.forEach (<anonymous>)
    at Instrumenter.hookModule (/home/max/git/my-api/node_modules/dd-trace/src/instrumenter.js:67:8)
    at Function.Module._load (/home/max/git/my-api/node_modules/require-in-the-middle/index.js:69:30)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/max/git/my-api/node_modules/mysql2/lib/pool.js:1:75)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module._load (/home/max/git/my-api/node_modules/require-in-the-middle/index.js:25:24)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)

Node version: 8.4.0
dd-trace-js version: ^2.1.0
mysql2 version: ^1.5.2

I'm sure this has something todo with the Promise stuff from mysql2.

Is there any way I can use this module without rewriting my whole code?
Thanks in advance

@rochdev rochdev added bug Something isn't working community integrations labels Jun 4, 2018
@rochdev rochdev self-assigned this Jun 4, 2018
@rochdev
Copy link
Member

rochdev commented Jun 4, 2018

@derN3rd This is definitely an oversight as mysql2/promise is not supported at the moment. For now the safest would be to disable plugins and individually enable the plugins you need (except mysql2 until this is fixed). See the API docs for an example.

@derN3rd
Copy link
Author

derN3rd commented Jun 4, 2018

@rochdev Thanks for your quick reply. I already got it to work with mysql2 plugin disabled, but wasn't sure if I did something wrong.

I will wait for mysql2/promise support then 😄

@rochdev
Copy link
Member

rochdev commented Jun 4, 2018

It seems this bug actually affects even the callback based version. It doesn't happen when mysql2 is required before the plugin however, so that could be a temporary workaround.

For example:

require('mysql2/promise') // require before to work around the bug
const tracer = require('dd-trace').init()
const mysql2 = require('mysql2/promise') // require again to apply patching

@derN3rd
Copy link
Author

derN3rd commented Jun 4, 2018

It doesn't crash now, but I also get no traces of my mysql2 queries.
Can this be because I have all queries in a external module?
If so, how can I patch the dependencies of this module too?

@rochdev
Copy link
Member

rochdev commented Jun 4, 2018

@derN3rd It should still be instrumented in external modules if the external module is required after the tracer is initialized.

Could you try something like this:

require('mysql2') // require callback version to avoid race condition with promise patch
const tracer = require('dd-trace').init()
const externalModule = require('external-module')

Also make sure you have re-enabled the plugin.

@derN3rd
Copy link
Author

derN3rd commented Jun 4, 2018

@rochdev Thanks man.
Got it to work. I have to do require('mysql2') first, then initialize dd-trace. Afterwards I can use mysql2 OR mysql2/promise without any problems (Like your example).
I tried to require('mysql2/promise') first, but that doesn't work.

Thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community integrations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants