slanatech / swagger-stats

API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices.
https://swaggerstats.io/
MIT License
906 stars 139 forks source link

Issues with Requests to the root route (/) #285

Open willwill96 opened 8 months ago

willwill96 commented 8 months ago

Problem

When using wildcards in route matchers of express, the base route is not added as one of the recorded paths. For example, the following server setup will not record requests to http://localhost:3001:

import express from 'express'
import apiSpec from './swagger.json'  assert { type: 'json' };
import swaggerStats from 'swagger-stats'

const app = express()

app.use(swaggerStats.getMiddleware({swaggerSpec: apiSpec, swaggerOnly: true}))

app.get('*', (req, res)=>{
    return res.send('hello world')
})
app.listen(3001, ()=>{
    console.log('listening')
})

Steps to reproduce

(Using node v18.19.0)

  1. Pull down the following repo: https://github.com/willwill96/swagger-stats-repro
  2. Run npm install
  3. Run node server.mjs
  4. Make requests to http://localhost:3001 & http://localhost:3001/other
  5. View http://localhost:3001/swagger-stats/metrics. Note that the path /other is present for some requests, but / is not.

Expected Behavior

I expect to see path="/" as one of the requests in metrics.

Other notes

  1. Interestingly, the only way I've been able to get metrics on the root route to work is by removing the wildcard route handler AND switching to swaggerOnly: false.
  2. I tried commenting out the code here and it started working as I expected, but I didn't test it thoroughly and don't know if removing it might be breaking something else.