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

Improve log output for binary response when debug logging or pprof enabled #4498

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion runtime/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -145,13 +146,33 @@ func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if h.loggingEnabled(logging.Debug) {
fields["resp_body"] = recorder.buf.String()
if gzipAccepted(r.Header) || isPprofEndpoint(r) {
fields["resp_body"] = "[Payload Compressed]"
} else {
fields["resp_body"] = recorder.buf.String()
}
}

h.logger.WithFields(fields).Info("Sent response.")
}
}

func gzipAccepted(header http.Header) bool {
a := header.Get("Accept-Encoding")
parts := strings.Split(a, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "gzip" || strings.HasPrefix(part, "gzip;") {
return true
}
}
return false
}

func isPprofEndpoint(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/debug/pprof/")
}

type recorder struct {
logger logging.Logger
inner http.ResponseWriter
Expand Down
60 changes: 60 additions & 0 deletions runtime/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package runtime

import (
"fmt"
"net/http"
"net/url"
"testing"
)
Expand Down Expand Up @@ -46,3 +47,62 @@ func TestDropInputParam(t *testing.T) {
}

}

func TestValidateGzipHeader(t *testing.T) {

httpHeader := http.Header{}

httpHeader.Add("Accept", "*/*")
result := gzipAccepted(httpHeader)
expected := false

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}

httpHeader.Add("Accept-Encoding", "gzip")

result = gzipAccepted(httpHeader)
expected = true

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}

httpHeader.Set("Accept-Encoding", "gzip, deflate, br")
result = gzipAccepted(httpHeader)
expected = true

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}

httpHeader.Set("Accept-Encoding", "br;q=1.0, gzip;q=0.8, *;q=0.1")
result = gzipAccepted(httpHeader)
expected = true

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}

func TestValidatePprofUrl(t *testing.T) {

req := http.Request{}

req.URL = &url.URL{Path: "/metrics"}
result := isPprofEndpoint(&req)
expected := false

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}

req.URL = &url.URL{Path: "/debug/pprof/"}
result = isPprofEndpoint(&req)
expected = true

if result != expected {
t.Errorf("Expected %v but got: %v", expected, result)
}
}