Tracking DB querying time - Bookshelf/knex

前端 未结 2 1709

I would like to monitor the time taken by a query on my API\'s db. I so created the following function, using bookshelf-signals, a Bookshelf plugin. :

booksh         


        
2条回答
  •  無奈伤痛
    2021-01-16 14:19

    Based on Mikael Lepistö snippet I came up with this :

    const dbEvents = (server, sdc) => {
      knex.on('query', data => {
        server.app[data.__knexQueryUid + ''] = new Date().valueOf();
      });
    
      knex.on('query-response', (data, obj, builder) => {
        sdc.counter('db_queries_time', new Date().valueOf() - server.app[obj.__knexQueryUid + '']);
        sdc.increment('nr_db_queries');
      });
    };
    

    And I then call the function when I start the server - I am working with Hapijs.

    EDIT: sdc is a statsd client, I use it to send the DB time :)

提交回复
热议问题