express-prom-bundle icon indicating copy to clipboard operation
express-prom-bundle copied to clipboard

No overload to metrics middleware

Open filiperochs opened this issue 1 year ago • 3 comments

Im my nodejs (typescript) project, I create the following metricsMiddleware:

import { collectDefaultMetrics, Registry } from 'prom-client';
import promBundle from 'express-prom-bundle';
import { Request, Response } from 'express';

const registry = new Registry();
collectDefaultMetrics({ register: registry });

const metricsMiddleware = promBundle({
  autoregister: false,
  includeMethod: true,
  includePath: true,
  customLabels: { service: process.env.SERVICE_NAME },
  promRegistry: registry,
});

const metricsEndpoint = (req: Request, res: Response) => {
  res.set('Content-Type', registry.contentType);
  registry.metrics().then(data => res.send(data));
};

export { metricsMiddleware, metricsEndpoint, registry };

And then imporing and using with express:

import express, { Express } from "express";
import userRouter from "./routes/messageRoutes";
import { errorConverter, errorHandler } from "./middleware";
import { metricsEndpoint, metricsMiddleware } from "./metrics";

const app: Express = express();

app.use(metricsMiddleware);

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(userRouter);

app.get("/metrics", metricsEndpoint);
app.use(errorConverter);
app.use(errorHandler);

export default app;

But the following type error is showed:

No overload matches this call. The last overload gave the following error. Argument of type 'Middleware' is not assignable to parameter of type 'PathParams'.

Anyone can help me with this?

Edit1:

package.json:

{
  "name": "chat-service",
  "version": "1.0.0",
  "description": "Chat Service - Chat Service",
  "main": "src/server.ts",
  "scripts": {
    "dev": "env NODE_ENV=development nodemon src/server.ts",
    "build": "rm -rf build/ && tsc -p .",
    "start": "env NODE_ENV=production nodemon build/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "typescript",
    "microservices",
    "javascript",
    "docker",
    "mongodb"
  ],
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.21",
    "amqplib": "^0.10.3",
    "dotenv": "^16.4.1",
    "express": "^4.18.2",
    "express-prom-bundle": "^8.0.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.1.1",
    "nodemon": "^3.0.3",
    "prom-client": "^15.1.3",
    "socket.io": "^4.7.4",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3",
    "uuid": "^9.0.1"
  },
  "devDependencies": {
    "@types/amqplib": "^0.10.4",
    "@types/body-parser": "^1.19.5",
    "@types/jsonwebtoken": "^9.0.5",
    "@types/uuid": "^9.0.8"
  }
}

filiperochs avatar Feb 08 '25 23:02 filiperochs

I'm seeing this issue, also. I'm just using the basic Install and Sample Usage example from the documentation. I'm running Node 22.12.0 and Express 4.21.2 and @types/express 4.17.21.

This may actually be an issue with the @types/express library - I honestly haven't looked into it.

There may be people who do not know a way to work around this... If you just need to get past the error, you can disable type checking when you register your middleware. The code runs just fine.

// @ts-expect-error Return type defined for promBundle() is inconsistent with Express middleware type definitions
app.use(metricsMiddleware)

bshawndt avatar Mar 14 '25 20:03 bshawndt

I found this was specific to version 8.0.0 which updated types to Express 5. Downgrading to 7.0.2 which uses Express 4 resolved it for me.

ashquarky avatar Apr 28 '25 11:04 ashquarky

adding @ashquarky's answer in the docs might be helpful to new users

arshadpakkali avatar Jun 09 '25 07:06 arshadpakkali