Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: The correct path cannot be obtained from the middleware. #3138

Open
3 tasks done
KScaesar opened this issue Sep 18, 2024 · 2 comments
Open
3 tasks done

🐛 [Bug]: The correct path cannot be obtained from the middleware. #3138

KScaesar opened this issue Sep 18, 2024 · 2 comments

Comments

@KScaesar
Copy link

KScaesar commented Sep 18, 2024

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:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
Copy link

welcome bot commented Sep 18, 2024

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
Copy link
Member

for this we are working on a new middleware
gofiber/contrib#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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants