2020-07-17 15:01:58 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-09-11 18:18:22 +00:00
|
|
|
let OpenSocketsMonitor
|
|
|
|
const seconds = 1000
|
2020-07-17 15:01:58 +00:00
|
|
|
|
|
|
|
// In Node 0.10 the default is 5, which means only 5 open connections at one.
|
|
|
|
// Node 0.12 has a default of Infinity. Make sure we have no limit set,
|
|
|
|
// regardless of Node version.
|
2020-09-11 18:18:22 +00:00
|
|
|
require('http').globalAgent.maxSockets = Infinity
|
|
|
|
require('https').globalAgent.maxSockets = Infinity
|
2020-07-17 15:01:58 +00:00
|
|
|
|
2020-10-03 16:58:28 +00:00
|
|
|
const SOCKETS_HTTP = require('http').globalAgent.sockets
|
|
|
|
const SOCKETS_HTTPS = require('https').globalAgent.sockets
|
2023-05-22 10:36:42 +00:00
|
|
|
const FREE_SOCKETS_HTTP = require('http').globalAgent.freeSockets
|
|
|
|
const FREE_SOCKETS_HTTPS = require('https').globalAgent.freeSockets
|
2020-10-03 16:58:28 +00:00
|
|
|
|
|
|
|
// keep track of set gauges and reset them in the next collection cycle
|
|
|
|
const SEEN_HOSTS_HTTP = new Set()
|
|
|
|
const SEEN_HOSTS_HTTPS = new Set()
|
2023-05-22 10:36:42 +00:00
|
|
|
const FREE_SEEN_HOSTS_HTTP = new Set()
|
|
|
|
const FREE_SEEN_HOSTS_HTTPS = new Set()
|
2020-10-03 16:58:28 +00:00
|
|
|
|
2023-05-22 10:36:51 +00:00
|
|
|
function collectConnectionsCount(
|
|
|
|
sockets,
|
|
|
|
seenHosts,
|
|
|
|
status,
|
|
|
|
https,
|
|
|
|
emitLegacyMetric
|
|
|
|
) {
|
2020-10-03 16:58:28 +00:00
|
|
|
const Metrics = require('./index')
|
|
|
|
Object.keys(sockets).forEach(host => seenHosts.add(host))
|
|
|
|
seenHosts.forEach(host => {
|
|
|
|
// host: 'HOST:PORT:'
|
|
|
|
const hostname = host.split(':')[0]
|
|
|
|
const openConnections = (sockets[host] || []).length
|
|
|
|
if (!openConnections) {
|
|
|
|
seenHosts.delete(host)
|
|
|
|
}
|
2023-05-22 10:36:42 +00:00
|
|
|
Metrics.gauge('sockets', openConnections, 1, {
|
|
|
|
path: hostname,
|
|
|
|
method: https,
|
|
|
|
status,
|
|
|
|
})
|
2023-05-22 10:36:51 +00:00
|
|
|
if (status === 'open' && emitLegacyMetric) {
|
2023-05-22 10:36:42 +00:00
|
|
|
// Emit legacy metric to keep old time series intact.
|
|
|
|
Metrics.gauge(
|
|
|
|
`${status}_connections.${https}.${hostname}`,
|
|
|
|
openConnections
|
|
|
|
)
|
|
|
|
}
|
2020-10-03 16:58:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:18:22 +00:00
|
|
|
module.exports = OpenSocketsMonitor = {
|
2023-05-22 10:36:51 +00:00
|
|
|
monitor(emitLegacyMetric) {
|
2020-09-11 18:18:22 +00:00
|
|
|
const interval = setInterval(
|
2023-05-22 10:36:51 +00:00
|
|
|
() => OpenSocketsMonitor.gaugeOpenSockets(emitLegacyMetric),
|
2020-09-11 18:18:22 +00:00
|
|
|
5 * seconds
|
|
|
|
)
|
|
|
|
const Metrics = require('./index')
|
|
|
|
return Metrics.registerDestructor(() => clearInterval(interval))
|
|
|
|
},
|
2020-07-17 15:01:58 +00:00
|
|
|
|
2023-05-22 10:36:51 +00:00
|
|
|
gaugeOpenSockets(emitLegacyMetric) {
|
|
|
|
collectConnectionsCount(
|
|
|
|
SOCKETS_HTTP,
|
|
|
|
SEEN_HOSTS_HTTP,
|
|
|
|
'open',
|
|
|
|
'http',
|
|
|
|
emitLegacyMetric
|
|
|
|
)
|
|
|
|
collectConnectionsCount(
|
|
|
|
SOCKETS_HTTPS,
|
|
|
|
SEEN_HOSTS_HTTPS,
|
|
|
|
'open',
|
|
|
|
'https',
|
|
|
|
emitLegacyMetric
|
|
|
|
)
|
2023-05-22 10:36:42 +00:00
|
|
|
collectConnectionsCount(
|
|
|
|
FREE_SOCKETS_HTTP,
|
|
|
|
FREE_SEEN_HOSTS_HTTP,
|
|
|
|
'free',
|
2023-05-22 10:36:51 +00:00
|
|
|
'http',
|
|
|
|
false
|
2023-05-22 10:36:42 +00:00
|
|
|
)
|
|
|
|
collectConnectionsCount(
|
|
|
|
FREE_SOCKETS_HTTPS,
|
|
|
|
FREE_SEEN_HOSTS_HTTPS,
|
|
|
|
'free',
|
2023-05-22 10:36:51 +00:00
|
|
|
'https',
|
|
|
|
false
|
2023-05-22 10:36:42 +00:00
|
|
|
)
|
2021-12-16 09:04:32 +00:00
|
|
|
},
|
2020-09-11 18:18:22 +00:00
|
|
|
}
|