Skip to content

fix: wrap mysql2 addCommand only once#7459

Merged
BridgeAR merged 8 commits into
masterfrom
BridgeAR/2026-02-07-fix-mysql2
Feb 12, 2026
Merged

fix: wrap mysql2 addCommand only once#7459
BridgeAR merged 8 commits into
masterfrom
BridgeAR/2026-02-07-fix-mysql2

Conversation

@BridgeAR

@BridgeAR BridgeAR commented Feb 7, 2026

Copy link
Copy Markdown
Member

fix: wrap mysql2 addCommand only once

Prepare calls would currently cause the method to be rewrapped
frequently. This is now only done a single time.

In addition this adds types to the instrumentation and adds a few
more safe guards for type safety.

perf: improve database plugin performance

The database plugin has a few intermediate objects that are not
necessary as well as calling methods when they do not have to be
called.
This is not a huge difference but it is not needed.

In addition, add types and make private methods actually privat.

chore: add types to tracing and outbound plugins

Fixes: #7074

Prepare calls would currently cause the method to be rewrapped
frequently. This is now only done a single time.

In addition this adds types to the instrumentation and adds a few
more safe guards for type safety.
The database plugin has a few intermediate objects that are not
necessary as well as calling methods when they do not have to be
called.
This is not a huge difference but it is not needed.

In addition, add types and make private methods actually privat.
@codecov

codecov Bot commented Feb 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.34%. Comparing base (e3344ae) to head (85fa607).
⚠️ Report is 27 commits behind head on master.

Files with missing lines Patch % Lines
packages/datadog-instrumentations/src/mysql2.js 96.87% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7459   +/-   ##
=======================================
  Coverage   80.34%   80.34%           
=======================================
  Files         731      731           
  Lines       31093    31100    +7     
=======================================
+ Hits        24981    24988    +7     
  Misses       6112     6112           
Flag Coverage Δ
aiguard-macos 39.08% <25.00%> (-0.10%) ⬇️
aiguard-ubuntu 39.12% <25.00%> (-0.10%) ⬇️
aiguard-windows 38.97% <25.00%> (-0.10%) ⬇️
apm-capabilities-tracing-macos 48.86% <25.00%> (+0.01%) ⬆️
apm-capabilities-tracing-ubuntu 48.87% <25.00%> (+0.02%) ⬆️
apm-capabilities-tracing-windows 48.59% <25.00%> (+0.01%) ⬆️
apm-integrations-child-process 38.57% <75.00%> (-0.11%) ⬇️
apm-integrations-couchbase-18 37.47% <100.00%> (-0.11%) ⬇️
apm-integrations-couchbase-eol 37.81% <100.00%> (-0.26%) ⬇️
apm-integrations-oracledb 38.03% <12.50%> (-0.07%) ⬇️
appsec-express 55.36% <100.00%> (-0.08%) ⬇️
appsec-fastify 52.02% <12.50%> (-0.05%) ⬇️
appsec-graphql 52.32% <25.00%> (-0.07%) ⬇️
appsec-kafka 44.64% <25.00%> (-0.08%) ⬇️
appsec-ldapjs 44.31% <0.00%> (-0.08%) ⬇️
appsec-lodash 43.99% <0.00%> (-0.08%) ⬇️
appsec-macos 58.48% <25.00%> (-0.07%) ⬇️
appsec-mongodb-core 49.22% <12.50%> (-0.05%) ⬇️
appsec-mongoose 49.91% <12.50%> (-0.05%) ⬇️
appsec-mysql 51.31% <63.75%> (-0.03%) ⬇️
appsec-node-serialize 43.50% <0.00%> (-0.08%) ⬇️
appsec-passport 48.11% <25.00%> (-0.09%) ⬇️
appsec-postgres 51.04% <12.50%> (-0.09%) ⬇️
appsec-sourcing 42.85% <25.00%> (-0.08%) ⬇️
appsec-template 43.67% <0.00%> (-0.08%) ⬇️
appsec-ubuntu 58.51% <25.00%> (-0.07%) ⬇️
appsec-windows 58.35% <25.00%> (-0.09%) ⬇️
instrumentations-instrumentation-bluebird 32.25% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-body-parser 40.74% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-child_process 37.88% <25.00%> (-0.11%) ⬇️
instrumentations-instrumentation-cookie-parser 34.51% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-express 34.86% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.65% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-express-session 40.42% <0.00%> (-0.09%) ⬇️
instrumentations-instrumentation-fs 31.85% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-generic-pool 29.81% <ø> (ø)
instrumentations-instrumentation-http 39.63% <25.00%> (-0.10%) ⬇️
instrumentations-instrumentation-knex 32.26% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-mongoose 33.64% <18.75%> (-0.08%) ⬇️
instrumentations-instrumentation-multer 40.47% <0.00%> (-0.09%) ⬇️
instrumentations-instrumentation-mysql2 38.37% <80.00%> (+<0.01%) ⬆️
instrumentations-instrumentation-passport 40.78% <0.00%> (-0.09%) ⬇️
instrumentations-instrumentation-passport-http 40.75% <0.00%> (-0.09%) ⬇️
instrumentations-instrumentation-passport-local 40.75% <0.00%> (-0.09%) ⬇️
instrumentations-instrumentation-pg 37.78% <12.50%> (-0.11%) ⬇️
instrumentations-instrumentation-promise 32.18% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-promise-js 32.19% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-q 32.23% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-url 32.15% <0.00%> (-0.10%) ⬇️
instrumentations-instrumentation-when 32.20% <0.00%> (-0.10%) ⬇️
llmobs-ai 41.42% <0.00%> (-0.09%) ⬇️
llmobs-anthropic 40.63% <0.00%> (-0.09%) ⬇️
llmobs-bedrock 39.52% <25.00%> (-0.08%) ⬇️
llmobs-google-genai 40.12% <0.00%> (-0.09%) ⬇️
llmobs-langchain 39.66% <0.00%> (-0.08%) ⬇️
llmobs-openai 44.49% <0.00%> (-0.09%) ⬇️
llmobs-vertex-ai 40.41% <0.00%> (-0.09%) ⬇️
platform-core 28.09% <ø> (ø)
platform-esbuild 31.49% <ø> (ø)
platform-instrumentations-misc 39.62% <ø> (ø)
platform-shimmer 34.83% <ø> (ø)
platform-unit-guardrails 30.37% <ø> (ø)
plugins-azure-event-hubs 22.64% <ø> (ø)
plugins-azure-service-bus 22.08% <ø> (ø)
plugins-bullmq 43.72% <25.00%> (-0.10%) ⬇️
plugins-cassandra 38.07% <12.50%> (-0.07%) ⬇️
plugins-cookie 23.69% <ø> (ø)
plugins-cookie-parser 23.50% <ø> (ø)
plugins-crypto 22.88% <ø> (ø)
plugins-dd-trace-api 38.42% <0.00%> (-0.10%) ⬇️
plugins-express-mongo-sanitize 23.67% <ø> (ø)
plugins-express-session 23.46% <ø> (ø)
plugins-fastify 42.54% <25.00%> (-0.09%) ⬇️
plugins-fetch 38.60% <25.00%> (-0.10%) ⬇️
plugins-fs 38.68% <50.00%> (-0.10%) ⬇️
plugins-generic-pool 22.68% <ø> (ø)
plugins-google-cloud-pubsub 45.73% <100.00%> (-0.10%) ⬇️
plugins-grpc 41.29% <100.00%> (-0.10%) ⬇️
plugins-handlebars 23.71% <ø> (ø)
plugins-hapi 40.44% <0.00%> (-0.10%) ⬇️
plugins-hono 40.63% <0.00%> (-0.10%) ⬇️
plugins-ioredis 38.48% <25.00%> (-0.10%) ⬇️
plugins-knex 23.51% <ø> (ø)
plugins-ldapjs 21.28% <ø> (ø)
plugins-light-my-request 23.15% <ø> (ø)
plugins-limitd-client 32.55% <0.00%> (-0.10%) ⬇️
plugins-lodash 22.74% <ø> (ø)
plugins-mariadb 39.58% <12.50%> (-0.13%) ⬇️
plugins-memcached 38.22% <25.00%> (-0.09%) ⬇️
plugins-microgateway-core 39.45% <0.00%> (-0.10%) ⬇️
plugins-moleculer 40.85% <75.00%> (-0.10%) ⬇️
plugins-mongodb 39.49% <100.00%> (-0.17%) ⬇️
plugins-mongodb-core 39.09% <81.25%> (-0.14%) ⬇️
plugins-mongoose 39.16% <12.50%> (-0.12%) ⬇️
plugins-multer 23.46% <ø> (ø)
plugins-mysql 39.23% <75.00%> (-0.14%) ⬇️
plugins-mysql2 39.33% <57.50%> (-0.08%) ⬇️
plugins-node-serialize 23.73% <ø> (ø)
plugins-opensearch 37.89% <31.25%> (-0.08%) ⬇️
plugins-passport-http 23.56% <ø> (ø)
plugins-postgres 35.68% <75.00%> (-0.13%) ⬇️
plugins-process 22.88% <ø> (ø)
plugins-pug 23.69% <ø> (ø)
plugins-redis 38.96% <75.00%> (-0.10%) ⬇️
plugins-router 43.25% <25.00%> (-0.10%) ⬇️
plugins-sequelize 22.27% <ø> (ø)
plugins-test-and-upstream-amqp10 38.55% <75.00%> (-0.10%) ⬇️
plugins-test-and-upstream-amqplib 43.90% <25.00%> (-0.10%) ⬇️
plugins-test-and-upstream-apollo 39.33% <0.00%> (-0.09%) ⬇️
plugins-test-and-upstream-avsc 38.82% <25.00%> (-0.10%) ⬇️
plugins-test-and-upstream-bunyan 33.86% <0.00%> (-0.10%) ⬇️
plugins-test-and-upstream-connect 41.05% <0.00%> (-0.10%) ⬇️
plugins-test-and-upstream-graphql 40.23% <50.00%> (-0.10%) ⬇️
plugins-test-and-upstream-koa 40.63% <0.00%> (-0.10%) ⬇️
plugins-test-and-upstream-protobufjs 39.05% <25.00%> (-0.10%) ⬇️
plugins-test-and-upstream-rhea 44.16% <75.00%> (-0.13%) ⬇️
plugins-undici 39.40% <25.00%> (-0.09%) ⬇️
plugins-url 22.88% <ø> (ø)
plugins-valkey 38.13% <25.00%> (-0.10%) ⬇️
plugins-vm 22.88% <ø> (ø)
plugins-winston 34.27% <0.00%> (-0.10%) ⬇️
plugins-ws 42.21% <0.00%> (-0.10%) ⬇️
profiling-macos 40.07% <75.00%> (-0.10%) ⬇️
profiling-ubuntu 40.12% <75.00%> (-0.10%) ⬇️
profiling-windows 41.47% <75.00%> (-0.45%) ⬇️
serverless-azure-functions-client 22.38% <ø> (ø)
serverless-azure-functions-eventhubs 22.38% <ø> (ø)
serverless-azure-functions-servicebus 22.38% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Feb 7, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 4.58 MB
Deduped: 5.42 MB
No deduping: 5.42 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 2.0.6 | 81.92 kB | 813.08 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-official

This comment has been minimized.

@pr-commenter

pr-commenter Bot commented Feb 7, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-02-09 13:17:26

Comparing candidate commit 85fa607 in PR branch BridgeAR/2026-02-07-fix-mysql2 with baseline commit e3344ae in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 226 metrics, 34 unstable metrics.

@BridgeAR
BridgeAR marked this pull request as ready for review February 7, 2026 19:58
@BridgeAR
BridgeAR requested review from a team as code owners February 7, 2026 19:58
Comment thread packages/datadog-instrumentations/src/mysql2.js
Comment thread packages/datadog-instrumentations/src/mysql2.js
errorCh.publish(ctx)
})
this.once('end', () => finishCh.publish(ctx))
const command = /** @type {{ once?: Function }} */ (this)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does TS not provide a nice way to do this, say on line 164, so that you don't need to do this kinda weird re-assignment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the issue here is something else: we actually truly do not know about the type because they might change in any version. Due to that, we have to 'detect' each individual type and even when we detect it, we have to cast that part so that this access is accepted.

})
if (typeof cb === 'function') {
process.nextTick(() => {
/** @type {Function} */ (cb)(abortController.signal.reason)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does line 270 not make this type comment unnecessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow the types detect that the cb might have changed its content due to the async operation in between.

addHook({ name: 'mysql2', file: 'lib/pool.js', versions: ['1 - 3.11.4'] }, wrapPool)
addHook(
{ name: 'mysql2', file: 'lib/base/connection.js', versions: ['>=3.11.5'] },
/** @type {(moduleExports: unknown, version: string) => unknown} */ (wrapConnection)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty repetitive. Can we not make this implicit via addHooks's definition?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the definition in addHook. The reason is that the receiving method defines more concrete types that do not match these 1-to-1. Thus the need for the cast.

Comment thread packages/dd-trace/src/plugins/database.js

@crysmags crysmags left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick review, good to go!

@BridgeAR
BridgeAR merged commit 875e585 into master Feb 12, 2026
791 checks passed
@BridgeAR
BridgeAR deleted the BridgeAR/2026-02-07-fix-mysql2 branch February 12, 2026 15:59
dd-octo-sts Bot pushed a commit that referenced this pull request Feb 13, 2026
* fix: wrap mysql2 addCommand only once

Prepare calls would currently cause the method to be rewrapped
frequently. This is now only done a single time.

In addition this adds types to the instrumentation and adds a few
more safe guards for type safety.

* perf: improve database plugin performance

The database plugin has a few intermediate objects that are not
necessary as well as calling methods when they do not have to be
called.
This is not a huge difference but it is not needed.

* chore: make private methods actually private

* chore: add types to tracing and outbound plugins

* test: add regression test for single addCommand wrapping

Fixes: #7074
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Feb 13, 2026
juan-fernandez pushed a commit that referenced this pull request Feb 18, 2026
* fix: wrap mysql2 addCommand only once

Prepare calls would currently cause the method to be rewrapped
frequently. This is now only done a single time.

In addition this adds types to the instrumentation and adds a few
more safe guards for type safety.

* perf: improve database plugin performance

The database plugin has a few intermediate objects that are not
necessary as well as calling methods when they do not have to be
called.
This is not a huge difference but it is not needed.

* chore: make private methods actually private

* chore: add types to tracing and outbound plugins

* test: add regression test for single addCommand wrapping

Fixes: #7074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: mysql2 instrumentation causes stack overflow with large prepared statement params

4 participants