PayU / prometheus-api-metrics

API and process monitoring with Prometheus for Node.js micro-service
Apache License 2.0
130 stars 44 forks source link

Express: Use req.originalUrl if available instead of req.url #42

Closed robbiet480 closed 4 years ago

robbiet480 commented 4 years ago

req.url comes from NodeJS and doesn't provide the right value (/ instead of /metrics or /metrics.json) to check the metricsPath against if prometheus-api-metrics is attached like this:

const express = require('express')
const basicAuth = require('express-basic-auth')
const apiMetrics = require('prometheus-api-metrics')

const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.use(['/metrics', '/metrics.json'], basicAuth({ users: { foo: 'bar' } }), apiMetrics())

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

In this case, req.url is simply /. req.originalUrl is /metrics or /metrics.json. This PR will attempt to use req.originalUrl if set and fallback to req.url.

A workaround would be to simply set metricsPath to / but that both feels not great and doesn't work for the JSON path.

More info on req.originalUrl from the ExpressJS docs

robbiet480 commented 4 years ago

@kobik Can this get merged?