Skip to content

Commit

Permalink
runtime: improve log output for binary response (#4498)
Browse files Browse the repository at this point in the history
This change omits the response body when using compression on metrics endpoint and when pprof is enabled.

Signed-off-by: Christian Altamirano <[email protected]>
  • Loading branch information
christian1607 authored Mar 27, 2022
1 parent 1f4aa9a commit 3dd50d8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
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)
}
}

0 comments on commit 3dd50d8

Please sign in to comment.