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

[8.13](backport #39455) x-pack/filebeat/input/internal/httplog: improve req/resp logging #39478

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Add setup option `--force-enable-module-filesets`, that will act as if all filesets have been enabled in a module during setup. {issue}30915[30915] {pull}99999[99999]
- Made Azure Blob Storage input GA and updated docs accordingly. {pull}37128[37128]
- Made GCS input GA and updated docs accordingly. {pull}37127[37127]
- Improve logging of request and response with request trace logging in error conditions. {pull}39455[39455]

*Auditbeat*

Expand Down
33 changes: 19 additions & 14 deletions x-pack/filebeat/input/internal/httplog/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
resp.Body, body, err = copyBody(resp.Body)
if err != nil {
errorsMessages = append(errorsMessages, fmt.Sprintf("failed to read response body: %s", err))
} else {
respParts = append(respParts,
zap.ByteString("http.response.body.content", body[:min(len(body), rt.maxBodyLen)]),
zap.Bool("http.response.body.truncated", rt.maxBodyLen < len(body)),
zap.Int("http.response.body.bytes", len(body)),
zap.String("http.response.mime_type", resp.Header.Get("Content-Type")),
)
}
respParts = append(respParts,
zap.ByteString("http.response.body.content", body[:min(len(body), rt.maxBodyLen)]),
zap.Bool("http.response.body.truncated", rt.maxBodyLen < len(body)),
zap.Int("http.response.body.bytes", len(body)),
zap.String("http.response.mime_type", resp.Header.Get("Content-Type")),
)
message, err := httputil.DumpResponse(resp, false)
if err != nil {
errorsMessages = append(errorsMessages, fmt.Sprintf("failed to dump response: %s", err))
Expand Down Expand Up @@ -178,14 +177,13 @@ func logRequest(log *zap.Logger, req *http.Request, maxBodyLen int, extra ...zap
req.Body, body, err = copyBody(req.Body)
if err != nil {
errorsMessages = append(errorsMessages, fmt.Sprintf("failed to read request body: %s", err))
} else {
reqParts = append(reqParts,
zap.ByteString("http.request.body.content", body[:min(len(body), maxBodyLen)]),
zap.Bool("http.request.body.truncated", maxBodyLen < len(body)),
zap.Int("http.request.body.bytes", len(body)),
zap.String("http.request.mime_type", req.Header.Get("Content-Type")),
)
}
reqParts = append(reqParts,
zap.ByteString("http.request.body.content", body[:min(len(body), maxBodyLen)]),
zap.Bool("http.request.body.truncated", maxBodyLen < len(body)),
zap.Int("http.request.body.bytes", len(body)),
zap.String("http.request.mime_type", req.Header.Get("Content-Type")),
)
message, err := httputil.DumpRequestOut(req, false)
if err != nil {
errorsMessages = append(errorsMessages, fmt.Sprintf("failed to dump request: %s", err))
Expand All @@ -204,6 +202,13 @@ func logRequest(log *zap.Logger, req *http.Request, maxBodyLen int, extra ...zap
return req, reqParts[:0], errorsMessages
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

// TxID returns the current transaction.id value. If rt is nil, the empty string is returned.
func (rt *LoggingRoundTripper) TxID() string {
if rt == nil {
Expand Down
Loading