From 5d47d353e0fd940deee55c491fc5465deeae66c8 Mon Sep 17 00:00:00 2001 From: chiao Date: Wed, 6 Mar 2024 15:06:18 +0800 Subject: [PATCH] config option for max body size --- .traefik.yml | 1 + README.md | 30 +++++++++++++++++++++++++----- main.go | 8 +++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/.traefik.yml b/.traefik.yml index 7ec4308..d0b6c94 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -5,3 +5,4 @@ import: github.com/joy2fun/traefik-plugin-log-request testData: ResponseBody: false RequestIDHeaderName: X-Request-Id + MaxLineSize: 16384 diff --git a/README.md b/README.md index 4c267b6..3bac282 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ http: log-request: ResponseBody: false # also including response body RequestIDHeaderName: X-Request-Id + MaxLineSize: 16384 ``` crd example: @@ -24,20 +25,39 @@ spec: log-request: ResponseBody: false RequestIDHeaderName: X-Request-Id + MaxLineSize: 16384 ``` -helm chart values example (local plugin mode): +configMap via helm chart + +```yml +apiVersion: v1 +kind: ConfigMap +metadata: + name: traefik-plugin-log-request +data: +{{ (.Files.Glob "plugin-log-request/*").AsConfig | indent 2 }} +``` + +traefik helm chart values example (local plugin mode): ```yaml additionalArguments: - >- --experimental.localplugins.log-request.modulename=github.com/joy2fun/traefik-plugin-log-request additionalVolumeMounts: - - mountPath: /plugins-local + - mountPath: /plugins-local/src/github.com/joy2fun/traefik-plugin-log-request name: plugins deployment: additionalVolumes: - - hostPath: - path: /data/plugins-local + - configMap: + name: traefik-plugin-log-request + items: + - key: dot.traefik.yml + path: .traefik.yml + - key: go.mod + path: go.mod + - key: main.go + path: main.go name: plugins -``` \ No newline at end of file +``` diff --git a/main.go b/main.go index 3cdc626..1608d15 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( // Config holds the plugin configuration. type Config struct { ResponseBody bool `json:"responseBody,omitempty"` + MaxLineSize int `json:"maxLineSize,omitempty"` RequestIDHeaderName string `json:"requestIDHeaderName,omitempty"` } @@ -30,6 +31,7 @@ type logRequest struct { next http.Handler responseBody bool requestIDHeaderName string + maxLineSize int } type RequestData struct { @@ -51,6 +53,7 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt next: next, responseBody: config.ResponseBody, requestIDHeaderName: config.RequestIDHeaderName, + maxLineSize: config.MaxLineSize, }, nil } @@ -99,9 +102,12 @@ func (p *logRequest) ServeHTTP(rw http.ResponseWriter, req *http.Request) { responseBody := io.NopCloser(bytes.NewBuffer(bodyBytes)) responseBodyBytes, err := io.ReadAll(responseBody) if err != nil { + // ignore } - requestData.ResponseBody = string(responseBodyBytes) + if len(responseBodyBytes) < p.maxLineSize { + requestData.ResponseBody = string(responseBodyBytes) + } } jsonData, err := json.Marshal(requestData)