Skip to content
Open
Changes from all commits
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
24 changes: 14 additions & 10 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
LogTargetURL string `json:"LogTargetUrl,omitempty"` //nolint:tagliatelle // This is a configuration option
SkipHeaders []string `json:"SkipHeaders,omitempty"` //nolint:tagliatelle // This is a configuration option
Limits ConfigLimit `json:"Limits,omitempty"` //nolint:tagliatelle // This is a configuration option
SkipBodyParse bool `json:"SkipBodyParse,omitempty"` //nolint:tagliatelle // This is a configuration option
}

// ConfigLimit holds the plugin configuration.
Expand All @@ -48,6 +49,7 @@ type logRequest struct {
logTarget string
logTargetURL string
skipHeaders []string
skipBodyParse bool
}

// RequestLog holds the plugin configuration.
Expand Down Expand Up @@ -90,6 +92,7 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
logTarget: config.LogTarget,
logTargetURL: config.LogTargetURL,
skipHeaders: config.SkipHeaders,
skipBodyParse: config.SkipBodyParse,
}, nil
}

Expand Down Expand Up @@ -134,7 +137,7 @@ func (p *logRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Verb: r.Method,
}

reqData.Body = allowedBody(requestBody, r.Header.Get("Content-Type"), p.maxBodySize, p.contentTypes)
reqData.Body = allowedBody(requestBody, r.Header.Get("Content-Type"), p.maxBodySize, p.contentTypes, p.skipBodyParse)

responseBody := io.NopCloser(bytes.NewBuffer(respBodyBytes))
responseBodyBytes, _ := io.ReadAll(responseBody)
Expand All @@ -152,7 +155,7 @@ func (p *logRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Time: time.Now().Format(time.RFC3339),
}

resData.Body = allowedBody(responseBodyBytes, resp.Header().Get("Content-Type"), p.maxBodySize, p.contentTypes)
resData.Body = allowedBody(responseBodyBytes, resp.Header().Get("Content-Type"), p.maxBodySize, p.contentTypes, p.skipBodyParse)

log := requestLog{
RequestID: requestID,
Expand Down Expand Up @@ -252,23 +255,24 @@ func allowBodySize(bodySize, maxBodySize int) bool {
return bodySize <= maxBodySize
}

func allowedBody(body []byte, contentType string, maxBodySize int, contentTypes []string) interface{} {
func allowedBody(body []byte, contentType string, maxBodySize int, contentTypes []string, skipBodyParse bool) interface{} {
if len(body) == 0 {
return nil
}
if !allowBodySize(len(body), maxBodySize) || !allowContentType(contentType, contentTypes) {
return fmt.Sprintf("Request body too large to log or wrong content type. Size: %d bytes, Content-type: %s", len(body), contentType)
}

// Try to parse the body as JSON
var parsedBody interface{}
if contentType == "application/json" {
err := json.Unmarshal(body, &parsedBody)
if err == nil {
return parsedBody
if skipBodyParse == true {
// Try to parse the body as JSON
var parsedBody interface{}
if contentType == "application/json" {
err := json.Unmarshal(body, &parsedBody)
if err == nil {
return parsedBody
}
}
}

// If not JSON, return as string
return string(body)
}
Expand Down