从Knex.js获取连接池统计信息

Mur*_*dle 1 knex.js

我的Knex驱动程序出现间歇性错误:

TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

我猜想,我的RDS Aurora实例暂时无法从服务器访问,或者在特别繁忙的流量增加期间我的连接池用尽了。我想在连接池中记录可用连接与已占用连接的关系并绘制图表,以查看我的连接确实用完了,以及连接使用量激增时是否有特定的问题。但是我似乎无法从Google找出是否有办法从Knex或其池管理器中获取可用连接的数量。这可能吗?如果没有,还有其他方法可以记录有关我的连接池的统计信息吗?

我看到有一个选项可以传递给连接池init log,它需要一个布尔值。我Winston习惯于将日志发送到Loggly,而不仅仅是发送stdout的内容。我不知道“ log”属性会记录我感兴趣的事件,但是无论如何,我都需要将该信息作为数据来获取,以便以有意义的方式将其发送给Loggly。

Mik*_*stö 5

通过运行DEBUG=knex:*设置了环境变量的应用程序,您可以了解有关如何获取/返回连接到池的一些信息。

Knex使用tarn.js作为其池实现。页面末尾列出了一些获取池资源信息的方法https://github.com/vincit/tarn.js/

// returns the number of non-free resources
pool.numUsed()

// returns the number of free resources
pool.numFree()

// how many acquires are waiting for a resource to be released
pool.numPendingAcquires()

// how many asynchronous create calls are running
pool.numPendingCreates()
Run Code Online (Sandbox Code Playgroud)

可以通过找到池实例knex.client.pool

const knex = require('knex')({ client: 'pg', connection: 'postgres://knex_test' });
knex.client.pool.numPendingCreates(); // returns 0
Run Code Online (Sandbox Code Playgroud)