question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hot Module Reload problem with prom-client

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:9

github_iconTop GitHub Comments

5reactions
cadorncommented, Jun 3, 2019

I call client.register.removeSingleMetric('<name>') before I register metrics which seems to solve the issue.

4reactions
adambisekcommented, Jan 15, 2020

This works (you have to call clear on every HMR reload on SSR):

import promBundle from 'express-prom-bundle'
import prom from 'prom-client'
import { RequestHandler } from 'express'

const applyPrometheusMiddleware = (): RequestHandler => {
  prom.register.clear() // to avoid breaking SSR HMR
  return promBundle({ includeMethod: true, includePath: true })
}

export default applyPrometheusMiddleware

in express initialization:

const server = express()
server
  .disable('x-powered-by')
  .use(applyPrometheusMiddleware()) // don't put this middleware into global variable, or you'll break SSR HMR!
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Module Reload problem with prom-client - Bountysource
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 ......
Read more >
Hot module reload is not working on my nextjs app
There was issue because of my folders were case was not matching route case. Fixed the issues by changing folder name to route...
Read more >
9 Best Monitoring Tools for NodeJS Application - Geekflare
... and error log management; Web interface integration for monitoring application health and metrics; Auto-clustering for NodeJS applications; Hot reload ...
Read more >
The State of HMR in Angular - JavaScript in Plain English
Hot Module Replacement speeds up development and debugging. It is a Webpack feature that updates changed modules without reloading the whole page.
Read more >
Debugging development performance in a NextJS application
Other projects similar in size and technologies seem to not have this problem. Comparison table. Project, Boot time, Hot Reload time. Fresh ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found