Skip to content

Commit

Permalink
Check that handlers are only called once in middleware/requestlog.
Browse files Browse the repository at this point in the history
Fix a typo in middleware/httpgauge.
  • Loading branch information
Mikhail Mayorov authored and slon committed Apr 13, 2024
1 parent b6453d0 commit 837dc79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion middleware/httpgauge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Но мы будем писать упрощённую версию, которая будет хранить метрики в `map[string]int`.

Ключём в такой `map` можно было бы сделать `Path` запроса, но это не очень хорошее решение.
Ключом в такой `map` можно было бы сделать `Path` запроса, но это не очень хорошее решение.
В пути часто встречаются изменяемые параметры. Например `/user/0`, `/user/1`, etc. В этом случае,
мы не хотим отдельно измерять, что пришло 5 запросов к `/user/0` и 10 запросов к `/user/1`.
Для мониторинга за сервером нам нужно знать, что пришло 15 запросов к пути `/user/{userID}`.
Expand Down
27 changes: 24 additions & 3 deletions middleware/requestlog/requestlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,36 @@ import (
"gitlab.com/slon/shad-go/middleware/requestlog"
)

type oneShotHandler struct {
inner http.HandlerFunc

t *testing.T
used bool
}

func (h *oneShotHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.used {
h.t.Errorf("handler used twice")
return
}

h.used = true
h.inner(w, r)
}

func TestRequestLog(t *testing.T) {
core, obs := observer.New(zap.DebugLevel)

m := chi.NewRouter()
m.Use(requestlog.Log(zap.New(core)))

m.Get("/simple", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
m.Get("/simple", (&oneShotHandler{
inner: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
t: t,
}).ServeHTTP)

m.Post("/post", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
Expand Down

0 comments on commit 837dc79

Please sign in to comment.