When using connection pools, the pool.query method can be called directly as a shortcut instead of calling pool.getConnection, then querying (doc):
[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:
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.
When using connection pools, the
pool.querymethod can be called directly as a shortcut instead of callingpool.getConnection, then querying (doc):As a result, the subsegments for that query are duplicated because both
pool.queryandpool.getConnection().queryhave been captured to create subsegments, and both are called.Here's an example:
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.