-
Notifications
You must be signed in to change notification settings - Fork 227
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: ensure correct run context for mysql2 instrumentation #2487
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env node --unhandled-rejections=strict | ||
|
||
// A small example showing Elastic APM tracing of a script the `mysql2` package. | ||
// | ||
// By default this will use a MySQL on localhost with user 'root'. You can use: | ||
// npm run docker:start mysql | ||
// to start a MySQL container. Then `npm run docker:stop` to stop it. | ||
|
||
const apm = require('../').start({ // elastic-apm-node | ||
serviceName: 'example-trace-mysql2' | ||
}) | ||
|
||
const mysql = require('mysql2') | ||
const mysqlPromise = require('mysql2/promise') | ||
|
||
const conn = mysql.createConnection({ user: 'root' }) | ||
|
||
// 1. Simple queries, callback-style. | ||
// Note: For tracing spans to be created, there must be an active transaction. | ||
// Typically, a transaction is automatically started for incoming HTTP | ||
// requests to a Node.js server. However, because this script is not running | ||
// an HTTP server, we manually start transactions. More details at: | ||
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html | ||
const t1 = apm.startTransaction('t1-callback-style') | ||
conn.query('SELECT 1 + 1 AS solution', (err, results, _fields) => { | ||
console.log('SELECT 1+1: err=%s results=%o', err, results) | ||
}) | ||
conn.query('SELECT ? + ? AS solution', [2, 2], (err, results, _fields) => { | ||
console.log('SELECT 2+2: err=%s results=%o', err, results) | ||
t1.end() | ||
}) | ||
|
||
// 2. Tracing of prepared statements can show that subsequent executions of the | ||
// same query can be much faster. This example is derived from: | ||
// https://github.com/sidorares/node-mysql2/blob/master/examples/execute.js | ||
const t2 = apm.startTransaction('t2-prepared-statements') | ||
conn.execute( | ||
'select ?+1 as qqq, ? as rrr, ? as yyy', | ||
[1, null, 3], | ||
(err, rows, fields) => { | ||
console.log('execute 1: err=%s results=%o', err, rows) | ||
conn.execute( | ||
'select ?+1 as qqq, ? as rrr, ? as yyy', | ||
[3, null, 3], | ||
(err, rows, fields) => { | ||
console.log('execute 2: err=%s results=%o', err, rows) | ||
conn.unprepare('select ?+1 as qqq, ? as rrr, ? as yyy') | ||
conn.execute( | ||
'select ?+1 as qqq, ? as rrr, ? as yyy', | ||
[3, null, 3], | ||
(err, rows, fields) => { | ||
console.log('execute 3: err=%s results=%o', err, rows) | ||
t2.end() | ||
} | ||
) | ||
} | ||
) | ||
} | ||
) | ||
|
||
// 3. Promise style | ||
async function promiseStyle () { | ||
const conn2 = await mysqlPromise.createConnection({ user: 'root' }) | ||
const t3 = apm.startTransaction('t3-promise-style') | ||
|
||
const [rows] = await conn2.query('select 3 + 3 as solution') | ||
console.log('select 3+3: rows=%o', rows) | ||
|
||
// "upgrade" from non-promise connection | ||
conn.promise().query('select 4 + 4 as solution') | ||
.then(([rows, _fields]) => { | ||
console.warn('XXX in then: %s', apm._instrumentation._runCtxMgr) | ||
console.log('select 4+4: rows=%o', rows) | ||
}) | ||
.catch(err => { | ||
console.log('select 4+4: err=%s', err) | ||
}) | ||
|
||
t3.end() | ||
conn2.close() | ||
} | ||
promiseStyle() | ||
|
||
// Lazily shutdown client after everything above is finished. | ||
setTimeout(() => { | ||
console.log('Done') | ||
conn.end() | ||
}, 1000) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REVIEW NOTE: This change to return early if
!enabled
-- as is done in most instrumentations -- is a similar issue as was discussed for the recent ioredis change here: #2460 (comment)Before this change the
mysql2
instrumentation would still do some work, even ifdisableInstrumentations=mysql2,...
. This is presumably one of the "continuation patches" mentioned at #353 (comment) without further details.My understanding of the intent is to avoid a user's callback to a
mysql2.query(..., cb)
call from being attached to the run context of unrelated code due to the mysql2 package's internal callback queuing (which it uses to serialize queries to MySQL) when:mysql2
package, anddisableInstrumentations=mysql2,...
I imagine this is a rare configuration. Also, it is slightly odd that "disableInstrumentations=mysql2" doesn't turn off that code path completely. I don't believe there are any test cases that cover this.
@astorm What do you think: Is this change a blocker/breaking change or could it be deferred to a separate issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@astorm I don't think there's an easy answer to that, and as a result I have no strong opinions. My own bias would tend to be conservative and not change the behavior of long standing code legacy-ish code, but if you think there's value in fixing this slight oddity then I don't object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@astorm Thanks. I've played with this a little bit more and have some cases to show. I think it is subtle enough and debatable enough that I'd like a separate ticket to hold the discussion. I'll give that issue some time before merging this one, in case we decide we want to attempt to somewhat persist the old edge-case behaviour.
I'll link the ticket back here when I have it. I started writing it up and it got long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2498