moleculerjs / moleculer-metrics

:chart_with_upwards_trend: Official monitoring & metrics addons for Moleculer framework
MIT License
48 stars 23 forks source link

moleculer-prometheus using global registry #21

Closed ACaller closed 5 years ago

ACaller commented 5 years ago

Issue: Global registry is being used instead of registry instance

Why is this an issue: Using a global registry means we don't have the ability to stop and start a service on the same process. This was revealed as when running unit tests we start a broker and then create and destroy the necessary services for each test.

What's going wrong?: The metrics are being assigned to the global registry. Default metrics have a statement that deletes them before attempting to recreate, but this doesn't happen for any other metrics. This causes a fatal error when the service spins up again because the metrics already exist. (see code sample).

Proposal Create a field on the service instance which has a bespoke registry so it can be passed in to metrics and destroyed when stopped is called

Code Sample: https://codesandbox.io/s/moleculer-sample-fpcjd?fontsize=14

I've kind of already gone through and done some of the work for this so I can put a PR so you can see what I mean better. 🙂

icebob commented 5 years ago

Good catch! Could you make a PR?