You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cluster event listeners are attached even if not using clusters #155
This results in a memory leak if require('prom-client'); is done multiples times and the require.cache is blown away, as might happen in tests.
I get the following after rerunning my tests, which blow away the require cache in order to mock a dep. (node:80194) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit
Since it's very unusual to clear the require cache, especially globally and not for one specific module that you own, I didn't think this was a problem when I added it. :-/
This would now be a breaking change, but we could require a specific function call like setupClusterMetrics() to init the event listener.
Your process comment would work; jest doesn't reload that one specifically.
If you just want to avoid that message about event listener leaks while running tests, there's some jest configuration that can be changed (or switch to something other than jest that doesn't break node's singleton module principle).
Will keep thinking but I didn't come up with much previously. This is supposed to be the right way to do singletons.
I was able to solve my particular problem by rearranging import order, which allows me to stop calling jest.resetModules(); which is good. 🙂 I still think we should do something here, though.
This is supposed to be the right way to do singletons.
That's fine if I actually imported the cluster code. I don't though, for me the cluster listener is pure overhead. I don't run node in clusters, I don't need prom-client to listen or emit events for it.
I think just requiring a module shouldn't set up listeners on global objects unless that's the module's sole purpose. If I did require('prom-client-cluster') or require('prom-client/cluster') I think it's perfectly fine to attach the listeners. But not for a general purpose thing such as prom-client.
I think just requiring a module shouldn't set up listeners on global objects unless that's the module's sole purpose.
Makes total sense. It's pretty negligible overhead, but on premise I agree with you. Unfortunately changing that will be semver major since the current behavior has already been released 😞.
Some API ideas:
require('prom-client/cluster') as you proposed
new AggregatorRegistry() implicitly adds the listener
var ar = new AggregatorRegistry(); ar.init() explicitly inits the listeners (method name TBD)
Note that in cases 2 and 3, we should take steps to only add the listeners once. Jest will still break this, but we can make it safe for a user to instantiate multiple AggregatorRegistrys or call ar.init() multiple times.
This implementation assumes that only the first AggregatorRegistry call should listen, which means most of the tests that are currently green are likely testing the wrong instance of AggregatorRegistry.
This is also a curious limitation for run-time, considering AggregatorRegistry's contentType is determined in the constructor. This means you cannot produce multiple metrics routes for different metrics formats. Which will likely be a problem for expanding OpenMetrics support.
Activity
SimenB commentedon Sep 27, 2017
/cc @zbjornson.
Quick fix is to do
const process = require('process');at the top, but I don't like that.Preferably you would do something like
Some sort of opt-in is needed, IMO.
zbjornson commentedon Sep 27, 2017
Since it's very unusual to clear the
requirecache, especially globally and not for one specific module that you own, I didn't think this was a problem when I added it. :-/This would now be a breaking change, but we could require a specific function call like
setupClusterMetrics()to init the event listener.Your process comment would work; jest doesn't reload that one specifically.
If you just want to avoid that message about event listener leaks while running tests, there's some jest configuration that can be changed (or switch to something other than jest that doesn't break node's singleton module principle).
Will keep thinking but I didn't come up with much previously. This is supposed to be the right way to do singletons.
SimenB commentedon Sep 27, 2017
I was able to solve my particular problem by rearranging import order, which allows me to stop calling
jest.resetModules();which is good. 🙂 I still think we should do something here, though.That's fine if I actually imported the cluster code. I don't though, for me the cluster listener is pure overhead. I don't run node in clusters, I don't need prom-client to listen or emit events for it.
I think just requiring a module shouldn't set up listeners on global objects unless that's the module's sole purpose. If I did
require('prom-client-cluster')orrequire('prom-client/cluster')I think it's perfectly fine to attach the listeners. But not for a general purpose thing such asprom-client.Not sure what the best solution is, though.
zbjornson commentedon Sep 27, 2017
Makes total sense. It's pretty negligible overhead, but on premise I agree with you. Unfortunately changing that will be semver major since the current behavior has already been released 😞.
Some API ideas:
require('prom-client/cluster')as you proposednew AggregatorRegistry()implicitly adds the listenervar ar = new AggregatorRegistry(); ar.init()explicitly inits the listeners (method name TBD)Note that in cases 2 and 3, we should take steps to only add the listeners once. Jest will still break this, but we can make it safe for a user to instantiate multiple
AggregatorRegistrys or callar.init()multiple times.zbjornson commentedon Sep 27, 2017
Hm, actually... we could do option 2 without it being a breaking change... I'll work on a PR for that this week.
SimenB commentedon Sep 28, 2017
Perfect, thanks!
SimenB commentedon Oct 12, 2017
@zbjornson have you had a chance to look into it?
Lazily initialize cluster event listeners
Lazily initialize cluster event listeners (#159)
jdmarshall commentedon Jul 27, 2026
This implementation assumes that only the first AggregatorRegistry call should listen, which means most of the tests that are currently green are likely testing the wrong instance of AggregatorRegistry.
This is also a curious limitation for run-time, considering AggregatorRegistry's contentType is determined in the constructor. This means you cannot produce multiple metrics routes for different metrics formats. Which will likely be a problem for expanding OpenMetrics support.
2 remaining items
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support bot prometheus#155 and prometheus#788
Rework state tracking to support both prometheus#155 and prometheus#788
Rework state tracking to support both prometheus#155 and prometheus#788