Skip to content

Hot Module Reload problem with prom-client #196

Description

@cabrinoob

Hi, I'am using your library and I love it, but I found this little problem (not really a big one, but ...).

I'am using your library in a NestJS project. This framework proposes to use HMR with webpack.
I have a provider class for prometheus-things which have this contructor :

constructor(){
        this.counter = new Counter({
            name: 'fred_total_method_calls',
            help: 'Example of a counter',
            labelNames: ['method','path']
        });

        this.gauge = new Gauge({
            name: 'fred_method_response_time',
            help: 'Example of a gauge',
            labelNames: ['method','path']
        });
    }

When I'am running my project in my develop environment, and when I modify something somewhere in the project I have this error :

[HMR] Updated modules:
[HMR]  - ./src/app.controller.ts
[HMR]  - ./src/app.module.ts
[HMR]  - ./src/main.hmr.ts
[HMR]  - ./src/app.service.ts
[HMR] Update applied.
[Nest] 8624   - 2018-5-31 15:22:03   [ExceptionHandler] A metric with the name fred_total_method_calls has already been registered.

When the Hot Module Reload event is handled, webpack regenerate the changing code but existing metrics are still there.

I have found a workarround :

constructor(){
       register.clear();
        this.counter = new Counter({
            name: 'fred_total_method_calls',
            help: 'Example of a counter',
            labelNames: ['method','path']
        });
...

I call register.clear() in my constructor and it works in HMR mode. But I don't like this solution because this line is only here for my comfort allowing me to use HMR in my development phase. It has no direct buisness intrest.

So, do you have a better workarround for this problem?

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions