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

Path wrong when cached response #222

Open
Fesaa opened this issue Nov 21, 2024 · 0 comments
Open

Path wrong when cached response #222

Fesaa opened this issue Nov 21, 2024 · 0 comments

Comments

@Fesaa
Copy link

Fesaa commented Nov 21, 2024

If a method is returned early(?) by a cache middleware, it'll be added by the complete path in the metric. Instead of with the generic path.

This code can reproduce it easily;

func TestFiberProm(t *testing.T) {

	app := fiber.New()
	prom := fiberprometheus.New("cache-path-test")
	prom.RegisterAt(app, "/metrics")
	app.Use(prom.Middleware)

	app.Use(cache.New())

	app.Get("/:name", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})

	req1 := httptest.NewRequest("GET", "/one", nil)
	resp, err := app.Test(req1, -1)
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != 200 {
		t.FailNow()
	}

	resp, err = app.Test(req1, -1)
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != 200 {
		t.FailNow()
	}

	req := httptest.NewRequest("GET", "/metrics", nil)
	resp, _ = app.Test(req, -1)
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	got := string(body)

	want := "http_requests_total{method=\"GET\",path=\"/:name\",service=\"cache-path-test\",status_code=\"200\"} 2"
	if !strings.Contains(got, want) {
		t.Errorf("got %s; want %s", got, want)
	}

	dontWant := "http_requests_total{method=\"GET\",path=\"/one\",service=\"cache-path-test\",status_code=\"200\"} 1"
	if strings.Contains(got, dontWant) {
		t.Errorf("got included %s, while it shoudln't", dontWant)
	}
}

I understand this may not be trivial to solve, and I can image it not really be solvable at all. Sadly, I don't know the internal of fiber good enough to gauge at this or take a stab.

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

No branches or pull requests

1 participant