Skip to content

Commit

Permalink
config option for max body size
Browse files Browse the repository at this point in the history
  • Loading branch information
joy2fun committed Mar 6, 2024
1 parent afec111 commit 5d47d35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import: github.com/joy2fun/traefik-plugin-log-request
testData:
ResponseBody: false
RequestIDHeaderName: X-Request-Id
MaxLineSize: 16384
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ http:
log-request:
ResponseBody: false # also including response body
RequestIDHeaderName: X-Request-Id
MaxLineSize: 16384
```
crd example:
Expand All @@ -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
```
```
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -30,6 +31,7 @@ type logRequest struct {
next http.Handler
responseBody bool
requestIDHeaderName string
maxLineSize int
}

type RequestData struct {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5d47d35

Please sign in to comment.