[misc] register the metrics sweeping interval handle for later cleanup

This commit is contained in:
Jakob Ackermann 2020-10-03 19:40:46 +02:00 committed by Jakob Ackermann
parent 2e9c4a5d4f
commit 29742d6a87
2 changed files with 10 additions and 3 deletions

View file

@ -30,6 +30,7 @@ function initialize(appName, opts = {}) {
appName = appName || DEFAULT_APP_NAME
configure({ ...opts, appName })
collectDefaultMetrics({ timeout: 5000, prefix: '' })
promWrapper.setupSweeping()
console.log(`ENABLE_TRACE_AGENT set to ${process.env.ENABLE_TRACE_AGENT}`)
if (process.env.ENABLE_TRACE_AGENT === 'true') {

View file

@ -138,12 +138,15 @@ class MetricWrapper {
}
}
if (!PromWrapper.sweepRegistered) {
let sweepingInterval
PromWrapper.setupSweeping = function() {
if (sweepingInterval) {
clearInterval(sweepingInterval)
}
if (process.env.DEBUG_METRICS) {
console.log('Registering sweep method')
}
PromWrapper.sweepRegistered = true
setInterval(function() {
sweepingInterval = setInterval(function() {
if (PromWrapper.ttlInMinutes) {
if (process.env.DEBUG_METRICS) {
console.log('Sweeping metrics')
@ -153,6 +156,9 @@ if (!PromWrapper.sweepRegistered) {
})
}
}, 60000)
const Metrics = require('./index')
Metrics.registerDestructor(() => clearInterval(sweepingInterval))
}
module.exports = PromWrapper