Skip to content

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

@dwickr

Description

@dwickr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions