gofiber / fiber

⚑️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.25k stars 1.64k forks source link

πŸ› [Bug]: The correct path cannot be obtained from the middleware. #3138

Open KScaesar opened 5 days ago

KScaesar commented 5 days ago

Bug Description

Currently, I'm working on observability (o11y) and need to metric the current API path to track API performance.

In Prometheus' recommendations, it's mentioned that labels should ideally have a static string; if labels are dynamic, performance may degrade.

However, when I use fiber middleware, I'm unable to obtain the expected path. When I use c.Route().Path, I expect to get /users/:id, but I keep getting /. When I use c.Path(), I can get the correct path /users/123, but it doesn't meet Prometheus' requirement for static labels.

How to Reproduce

Steps to reproduce the behavior:

  1. setup middleware and get c.Route().Path

Expected Behavior

expected path should be /users/:id

Fiber Version

v2.52.5

Code Snippet (optional)

router := fiber.New()

prometheusMiddleware := func(c *fiber.Ctx) error {
    path1 := c.Path()
    path2 := c.Route().Path
    log.Printf("path1=%v path2=%v in prometheus middleware\n", path1, path2)
    return c.Next()
}
router.Use(prometheusMiddleware)

userHandler := func(c *fiber.Ctx) error {
    path1 := c.Path()
    path2 := c.Route().Path
    log.Printf("path1=%v path2=%v in user handler\n", path1, path2)
    return c.SendString("User ID: " + c.Params("id"))
}
router.Get("/users/:id", userHandler)

req := httptest.NewRequest(http.MethodGet, "/users/123", nil)
router.Test(req)

// 2024/09/18 14:42:27 path1=/users/123 path2=/          in prometheus middleware
// 2024/09/18 14:42:27 path1=/users/123 path2=/users/:id in user handler

playground

Checklist:

welcome[bot] commented 5 days ago

Thanks for opening your first issue here! πŸŽ‰ Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

ReneWerner87 commented 5 days ago

for this we are working on a new middleware https://github.com/gofiber/contrib/pull/1032

the behavior you see during routing is normal

because the route of the middleware itself is the β€œroot” this is also how it works in expressjs

@gaby what was the development status?