Skip to content

Commit

Permalink
feat: log REST request headers (#1497)
Browse files Browse the repository at this point in the history
Note that the REST server logs metadata unconditionally, so logging the request headers is not gated on passing `-v` to Showcase. Thus, to show headers for both the gRPC and REST transports, `gapic-showcase run -v` will work.
  • Loading branch information
vchudnov-g committed Apr 12, 2024
1 parent c1d5e78 commit 05605c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions util/genrest/goviewcreator.go
Expand Up @@ -91,6 +91,7 @@ func NewView(model *gomodel.Model) (*goview.View, error) {
// number of path variables, etc.)
source.P(` backend.StdLog.Printf("Received %%s request matching '%s': %%q", r.Method, r.URL)`, handler.URIPattern)
source.P(` backend.StdLog.Printf(" urlPathParams (expect %d, have %%d): %%q", numUrlPathParams, urlPathParams)`, len(allURLVariables))
source.P(` backend.StdLog.Printf(" urlRequestHeaders:\n%%s", resttools.PrettyPrintHeaders(r, " "))`)
source.P("")
source.P(" if numUrlPathParams!=%d {", len(allURLVariables))
source.P(` backend.Error(w, http.StatusBadRequest, "found unexpected number of URL variables: expected %d, have %%d: %%#v", numUrlPathParams, urlPathParams)`, len(allURLVariables))
Expand Down
15 changes: 15 additions & 0 deletions util/genrest/resttools/headers.go
Expand Up @@ -83,3 +83,18 @@ func CheckAPIClientHeader(header http.Header) error {
}
return fmt.Errorf("internal inconsistency")
}

// PrettyPrintHeaders prints all the HTTP headers in `request` in
// lines preceded by `indentation`. Each line has one header key and a
// quoted list of all values for that key.
func PrettyPrintHeaders(request *http.Request, indentation string) string {
lines := []string{}
for key, values := range request.Header {
newLine := fmt.Sprintf("%s%s:", indentation, key)
for _, oneValue := range values {
newLine += fmt.Sprintf(" %q", oneValue)
}
lines = append(lines, newLine)
}
return strings.Join(lines, "\n")
}

0 comments on commit 05605c6

Please sign in to comment.