-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): add one example to log request body when the status …
…code is non 200 (#4108) * chore(examples): add one example to log request body when the status code is non 200 * chore(examples): fix typo * chore(deps): add logging the request body for a request doc * chore(deps): update logging docs with some minor edits
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
layout: default | ||
title: Logging the request body pattern for a request | ||
nav_order: 5 | ||
parent: Operations | ||
--- | ||
|
||
# Logging the request body pattern for a request | ||
|
||
If you want to log the request body of incoming requests, you will need to buffer the body before it reaches the gateway. To log the request body, you can use a middleware `http.Handler` to buffer the request body before it's consumed. | ||
|
||
1. Create a `http.Handler` middleware. The `logRequestBody` example middleware logs the request body when the response status code is not 200. | ||
|
||
```go | ||
type logResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
func (rsp *logResponseWriter) WriteHeader(code int) { | ||
rsp.statusCode = code | ||
rsp.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
func newLogResponseWriter(w http.ResponseWriter) *logResponseWriter { | ||
return &logResponseWriter{w, http.StatusOK} | ||
} | ||
|
||
// logRequestBody logs the request body when the response status code is not 200. | ||
func logRequestBody(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
lw := newLogResponseWriter(w) | ||
|
||
// Note that buffering the entire request body could consume a lot of memory. | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("failed to read body: %v", err), http.StatusBadRequest) | ||
return | ||
} | ||
clonedR := r.Clone(r.Context()) | ||
clonedR.Body = io.NopCloser(bytes.NewReader(body)) | ||
|
||
h.ServeHTTP(lw, clonedR) | ||
|
||
if lw.statusCode != http.StatusOK { | ||
grpclog.Errorf("http error %+v request body %+v", lw.statusCode, string(body)) | ||
} | ||
}) | ||
} | ||
``` | ||
|
||
2. Wrap the gateway serve mux with the `logRequestBody` middleware: | ||
|
||
```go | ||
mux := runtime.NewServeMux() | ||
// Register generated gateway handlers | ||
|
||
s := &http.Server{ | ||
Handler: logRequestBody(mux), | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters