Skip to content

Commit 74c1d30

Browse files
committed
feat(http,logging,har): add configurable request/response body limits and enhanced access logging
Add support for form parameter tracing, configurable body size limits via properties, and improved access logging with error-only mode. Key changes: - HAR middleware now respects http.har.maxBodySize property for per-body capture limits - HTTP logger supports form parameter tracing independently of raw body capture - New AccessLog and AccessLogErrorsOnly config options for single-line access logs - Error responses (>= 400) and transport errors always log with body at INFO level - Successful requests silent at INFO, visible at DEBUG and above - Query parameters and form data displayed separately in pretty logging - Enhanced header redaction with wildcard pattern support - Improved verbosity level calculation for trace configuration BREAKING CHANGE: NewLogger no longer accepts AccessLog config as a shortcut; use TraceConfig.AccessLog instead. The access log behavior has changed: error-only mode (AccessLogErrorsOnly) now surfaces failures at -v=0 without per-request spam.
1 parent 1999d10 commit 74c1d30

7 files changed

Lines changed: 588 additions & 74 deletions

File tree

har/har.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
// capturing outbound request/response pairs for troubleshooting.
33
package har
44

5+
import "github.com/flanksource/commons/properties"
6+
57
const defaultMaxBodySize = 64 * 1024 // 64 KB
68

9+
// MaxBodySizeProperty is the -P/properties key that overrides the default
10+
// per-body capture cap (in bytes). Set e.g. -P http.har.maxBodySize=1048576
11+
// to capture request/response bodies larger than the 64 KB default, or
12+
// -P http.har.maxBodySize=0 to capture full bodies with no cap.
13+
const MaxBodySizeProperty = "http.har.maxBodySize"
14+
715
// HARConfig controls what the HAR middleware captures and how it redacts.
816
type HARConfig struct {
917
// MaxBodySize is the maximum number of bytes captured per body.
@@ -20,10 +28,13 @@ type HARConfig struct {
2028
RedactedHeaders []string
2129
}
2230

23-
// DefaultConfig returns a HARConfig with sensible defaults.
31+
// DefaultConfig returns a HARConfig with sensible defaults. The per-body
32+
// capture cap honours the MaxBodySizeProperty (-P http.har.maxBodySize=…)
33+
// override; an unset or unparseable value keeps the 64 KB default, and a
34+
// value <= 0 disables truncation (full bodies captured).
2435
func DefaultConfig() HARConfig {
2536
return HARConfig{
26-
MaxBodySize: defaultMaxBodySize,
37+
MaxBodySize: int64(properties.Int(defaultMaxBodySize, MaxBodySizeProperty)),
2738
CaptureContentTypes: []string{"application/json", "application/x-www-form-urlencoded"},
2839
}
2940
}

har/har_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package har
2+
3+
import (
4+
"testing"
5+
6+
"github.com/flanksource/commons/properties"
7+
)
8+
9+
func TestDefaultConfig_MaxBodySizeProperty(t *testing.T) {
10+
cases := []struct {
11+
name string
12+
value string
13+
set bool
14+
want int64
15+
}{
16+
{name: "unset keeps default", set: false, want: defaultMaxBodySize},
17+
{name: "override raises cap", value: "1048576", set: true, want: 1048576},
18+
{name: "zero disables truncation", value: "0", set: true, want: 0},
19+
{name: "unparseable keeps default", value: "huge", set: true, want: defaultMaxBodySize},
20+
}
21+
22+
for _, tc := range cases {
23+
t.Run(tc.name, func(t *testing.T) {
24+
value := ""
25+
if tc.set {
26+
value = tc.value
27+
}
28+
properties.Set(MaxBodySizeProperty, value)
29+
defer properties.Set(MaxBodySizeProperty, "")
30+
31+
if got := DefaultConfig().MaxBodySize; got != tc.want {
32+
t.Errorf("MaxBodySize = %d, want %d", got, tc.want)
33+
}
34+
})
35+
}
36+
}

0 commit comments

Comments
 (0)