aws / aws/aws-xray-sdk-node

[mysql] Duplicate subsegments when using `createPool().query`

Open
#668 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
280
Forks
157
PR merge metrics
No merged PRs in 30d

Description

When using connection pools, the `pool.query` method can be called directly as a shortcut instead of calling `pool.getConnection`, then querying ([doc](https://github.com/mysqljs/mysql?tab=readme-ov-file#pooling-connections)):

> [pool.query] is a shortcut for the pool.getConnection() -> connection.query() -> connection.release() code flow.

As a result, the subsegments for that query are duplicated because both `pool.query` and `pool.getConnection().query` have been captured to create subsegments, and both are called.

Here's an example:

``` javascript
const AWSXRay = require('aws-xray-sdk')

AWSXRay.captureMySQL(require('mysql2'))
const mysqlPromise = require('mysql2/promise')

const ns = AWSXRay.getNamespace()

async function testPromisePoolQuery() {
try {
const pool = mysqlPromise.createPool({
host: 'localhost',
user: 'user',
password: 'password',
database: 'test',
connectionLimit: 10,
queueLimit: 0,
})

await pool.query('SELECT 1')

await pool.end()
} catch (err) {
console.error('Error connecting to database:', err)
}
}

ns.run(async () => {
let segment = new AWSXRay.Segment('testPromisePoolQuery')

AWSXRay.setSegment(segment)
await testPromisePoolQuery()
console.log(
`testPromisePoolQuery # of subsegments: ${AWSXRay.getSegment().subsegments?.length}`
)
segment.close()
})
```

The result printed from this is `testPromisePoolQuery # of subsegments: 2`, and if you view the subsegment you can see that the queries are the same, and timings are almost exactly the same.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.