jochen-schweizer / express-prom-bundle

express middleware with standard prometheus metrics in one bundle
MIT License
303 stars 67 forks source link

Breaking change in prom-client 13.0.0 returns empty response for /metrics #71

Closed lunae closed 3 years ago

lunae commented 3 years ago

Hi all! Awesome work with express-prom-bundle. Was giving it a try this weekend and it seems like the latest release of prom-client (13.0.0) broke the /metrics endpoint. Digging into the issue I found that prom-client@13.0.0 has breaking changes to metrics(). It now returns a promise. https://github.com/siimon/prom-client/releases/tag/v13.0.0. When using curl to test the endpoint it responds with an empty reply.

package.json

{
  "name": "express-prom-bundle-with-13-0-0",
  "version": "1.0.0",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.17.1",
    "express-prom-bundle": "^6.3.0",
    "prom-client": "^13.0.0"
  }
}

app.js

const express = require('express')
const prometheus = require("express-prom-bundle")

const app = express()

const metricsMiddleware = prometheus({
    includeStatusCode: true,
    includeMethod: true,
    includePath: true
})

app.use(metricsMiddleware)

app.listen(3000)

I believe the issue is in metricsMiddleware https://github.com/jochen-schweizer/express-prom-bundle/blob/master/src/index.js#L125-L128. I'm sort of new to both prom-client and express-prom-bundle and not quite sure if this is the only spot affected by the change but hope that this helps.

const metricsMiddleware = async function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(await opts.promRegistry.metrics());
};
palmer-cl commented 3 years ago

I am also seeing this issue. Error:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Promise

Relevant stack: at metricsMiddleware (/usr/src/app/node_modules/express-prom-bundle/src/index.js:127:9) at middleware (/usr/src/app/node_modules/express-prom-bundle/src/index.js:137:16)

disjunction commented 3 years ago

Thanks for the tips guys. I made a compatibility patch, so the library now supports both 12 and 13 versions of prom-client. I've just published it on npm as version 6.3.1.

As for the future, I hope to rewrite this whole thing in typescript as version 7. At that point I'll simply drop the support for the old library. (testing different version of peer dependency is a pain).