Skip to content

Database connection pool waiting time measurement by sentry.io #15724

Description

@FilipHavel

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Feature Description

Describe the feature you'd like to see implemented

Hello, i’m trying to measure time spent waiting for connection to the database pool and send measured time to Sentry.

Problem

I came across such a measurement in sentry.

my_issue

I realized that this could be waiting time for connection pool. So I sent two independent parallel requests to my app and set pool to 1, to simulate this issue locally. First query starts to execute, while another was waiting for pool connection. I used beforeQuery hook, in which i start and create new span. In afterQuery hook i finished span. When I looked into sentry after this process i get incorrect result.

When I dug deeper and looked into code in sequelize/packages/core/src/sequelize.js and found beforeQuery hook. Than I saw in code that measure time spent waiting for connection to the pool, is not possible without interfering with the library code, because establishing of connection is not part of the before hook and is executed before the hook. Here is code which i found.

return await retry(async () => {
      if (options.transaction === undefined) {
        options.transaction = this.getCurrentClsTransaction();
      }

      checkTransaction();

			// --------- i want to measure this connection / waiting time ---------

      const connection = await (options.transaction ? options.transaction.connection : this.connectionManager.getConnection({
        useMaster: options.useMaster,
        type: options.type === 'SELECT' ? 'read' : 'write',
      }));

			// --------------------------------------------------------------------

      if (this.options.dialect === 'db2' && options.alter && options.alter.drop === false) {
        connection.dropTable = false;
      }

      const query = new this.dialect.Query(connection, this, options);

      try {
        await this.hooks.runAsync('beforeQuery', options, query);
        checkTransaction();

        return await query.run(sql, bindParameters);
      } finally {
        await this.hooks.runAsync('afterQuery', options, query);
        if (!options.transaction) {
          this.connectionManager.releaseConnection(connection);
        }
      }
    }, retryOptions);

When I added some time measurement into code in library, i was able to get clearly measured time waiting for connection pool in Sentry.
expected_with_added _code_to_lib

Describe why you would like this feature to be added to Sequelize

Based on my experience, it seems that measuring the time spent waiting for a connection to the pool is not possible without modifying the library code. However, it may be possible to modify your current approach to get a more accurate measurement of the time spent waiting.

Is there a way that you can recommend me, which can give me expected measured time without interfering with library code?

Alternatively, if you are comfortable modifying the library code, I could create a pull request that adds a functionality for measuring the time spent waiting for a connection. This could potentially benefit other users of the library as well.

The feature could be implemented through an async hook, and thus not at the level of the query, but at the place where the actual acquire is done on the connection pool. It could be called for example beforePoolConnection / afterPoolConnection.

Please let me know if you have any further questions or concerns.

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    pending-approval
    type: feature
    on Feb 28, 2023
  2. changed the title [-]Pool access and connection waiting time measurement[/-] [+]Database connection pool waiting time measurement by sentry.io[/+] on Feb 28, 2023
  3. ephys commented on Mar 2, 2023

    @ephys
    Member

    I'm personally not against adding a beforePoolConnection /afterPoolConnection hook to Sequelize, sounds useful and generic enough, but you'd need a way to determine that both events are about the same connection

    It should not run for transactions, because transactions don't request a connection from the pool. They keep their transaction for as long as they are running

  4. removed
    pending-approval
    on Mar 2, 2023
  5. ephys commented on Mar 28, 2023

    @ephys
    Member

    To follow up on my above note about determining that both events are about the same connection, after PR #15859, you should be able to monitor how long acquiring takes by writing this:

    const acquireAttempts = new WeakMap();
    
    sequelize.hooks.addListener('beforePoolAcquire', options => {
      acquireAttempts.set(options, Date.now());
    })
    
    sequelize.hooks.addListener('afterPoolAcquire', _connection, options => {
      const elapsedTime = Date.now() - acquireAttempts.get(options);
      console.log(`Connection acquired in ${elapsedTime}ms`);
    })

    (Note that this PR currenly only targets Sequelize 7)

  6. added theissue type on Mar 9, 2025
  7. removed
    type: feature
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions