brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.33k stars 1.23k forks source link

[Question/Help] How to extract metrics from query times? #2929

Open rafaell-lycan opened 1 year ago

rafaell-lycan commented 1 year ago

Can we add events on how long it takes to process queries?

Scenario:

Similarly with how we track errors events pool.on('error', ... ), is there any way to track down query events and send them to your favorite stats aggregator (e.g.: StatsD) or tracer (e.g.: Datadog)?

e.g.:


interface QueryInfo {
  name: string
  duration: string | number
  status: 'complete' | 'error' | 'timeout'
}

pool.on('complete', (info: QueryInfo) => {
  const tags = {
    ...getQueryMetaInfoTags(info),
    status: info.status
  }
  myCollector.gauge('pg.query_time', 1, tags)
}
brianc commented 1 year ago

At my company we use newrelic and they perfectly integrate w/ node-postgres w/o having any changes to our code (via their own package doing significant monkey patching and so on for runtime metrics on query times). I'd be surprised if datadog didn't have the same thing.

For a custom statsd aggregator you could use some custom events on pool.on('aquire') and pool.on('release') to measure how long connections are used...but that doesn't track individual queries on each client (say you check out a client and run multiple queries).

You could do something like this:

pool.on('connect', (client) => {
  //monkey-patch the client query function and track before each query is executed and after it completes
})