Skip to content

Commit

Permalink
Use logger from request context
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Aug 28, 2022
1 parent cbe7f4f commit 0e6e62f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Support go 1.18, 1.19. (@miry)
* `toxiproxy.NewProxy` now accepts `name`, `listen addr` and `upstream addr`. (#418, @miry)
* Replace logrus with zerolog. (#413, @miry)
* Log HTTP requests to API server. (#413, @miry)
* Log HTTP requests to API server. (#413, #421, @miry)

# [2.4.0] - 2022-03-07

Expand Down
46 changes: 27 additions & 19 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/gorilla/mux"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -55,10 +56,6 @@ func StopBrowsersMiddleware(h http.Handler) http.Handler {
})
}

func timeoutMiddleware(next http.Handler) http.Handler {
return http.TimeoutHandler(next, 5*time.Second, "")
}

func (server *ApiServer) Listen(host string, port string) {
r := mux.NewRouter()
r.Use(hlog.NewHandler(*server.Logger))
Expand All @@ -73,7 +70,6 @@ func (server *ApiServer) Listen(host string, port string) {
Dur("duration", duration).
Msg("")
}))
r.Use(timeoutMiddleware)

r.HandleFunc("/reset", server.ResetState).Methods("POST")
r.HandleFunc("/proxies", server.ProxyIndex).Methods("GET")
Expand Down Expand Up @@ -125,7 +121,8 @@ func (server *ApiServer) ProxyIndex(response http.ResponseWriter, request *http.
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ProxyIndex: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ProxyIndex: Failed to write response to client")
}
}

Expand All @@ -144,7 +141,8 @@ func (server *ApiServer) ResetState(response http.ResponseWriter, request *http.
response.WriteHeader(http.StatusNoContent)
_, err := response.Write(nil)
if err != nil {
server.Logger.Warn().Err(err).Msg("ResetState: Failed to write headers to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ResetState: Failed to write headers to client")
}
}

Expand Down Expand Up @@ -181,7 +179,8 @@ func (server *ApiServer) ProxyCreate(response http.ResponseWriter, request *http
response.WriteHeader(http.StatusCreated)
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ProxyCreate: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ProxyCreate: Failed to write response to client")
}
}

Expand All @@ -190,7 +189,8 @@ func (server *ApiServer) Populate(response http.ResponseWriter, request *http.Re

apiErr, ok := err.(*ApiError)
if !ok && err != nil {
server.Logger.Warn().Err(err).Msg("Error did not include status code")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("Error did not include status code")
apiErr = &ApiError{err.Error(), http.StatusInternalServerError}
}

Expand All @@ -211,7 +211,8 @@ func (server *ApiServer) Populate(response http.ResponseWriter, request *http.Re
response.WriteHeader(responseCode)
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("Populate: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("Populate: Failed to write response to client")
}
}

Expand Down Expand Up @@ -263,7 +264,8 @@ func (server *ApiServer) ProxyUpdate(response http.ResponseWriter, request *http
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ProxyUpdate: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ProxyUpdate: Failed to write response to client")
}
}

Expand All @@ -278,7 +280,8 @@ func (server *ApiServer) ProxyDelete(response http.ResponseWriter, request *http
response.WriteHeader(http.StatusNoContent)
_, err = response.Write(nil)
if err != nil {
server.Logger.Warn().Err(err).Msg("ProxyDelete: Failed to write headers to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ProxyDelete: Failed to write headers to client")
}
}

Expand All @@ -299,7 +302,8 @@ func (server *ApiServer) ToxicIndex(response http.ResponseWriter, request *http.
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ToxicIndex: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ToxicIndex: Failed to write response to client")
}
}

Expand All @@ -324,7 +328,8 @@ func (server *ApiServer) ToxicCreate(response http.ResponseWriter, request *http
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ToxicCreate: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ToxicCreate: Failed to write response to client")
}
}

Expand All @@ -350,7 +355,8 @@ func (server *ApiServer) ToxicShow(response http.ResponseWriter, request *http.R
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ToxicShow: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ToxicShow: Failed to write response to client")
}
}

Expand All @@ -375,7 +381,8 @@ func (server *ApiServer) ToxicUpdate(response http.ResponseWriter, request *http
response.Header().Set("Content-Type", "application/json")
_, err = response.Write(data)
if err != nil {
server.Logger.Warn().Err(err).Msg("ToxicUpdate: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ToxicUpdate: Failed to write response to client")
}
}

Expand All @@ -395,16 +402,17 @@ func (server *ApiServer) ToxicDelete(response http.ResponseWriter, request *http
response.WriteHeader(http.StatusNoContent)
_, err = response.Write(nil)
if err != nil {
server.Logger.Warn().Err(err).Msg("ToxicDelete: Failed to write headers to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("ToxicDelete: Failed to write headers to client")
}
}

func (server *ApiServer) Version(response http.ResponseWriter, request *http.Request) {
log := zerolog.Ctx(request.Context())
response.Header().Set("Content-Type", "text/plain;charset=utf-8")
_, err := response.Write([]byte(Version))
if err != nil {
log.Err(err).Msg("Version: Failed to write response to client")
log := zerolog.Ctx(request.Context())
log.Warn().Err(err).Msg("Version: Failed to write response to client")
}
}

Expand Down

0 comments on commit 0e6e62f

Please sign in to comment.