Skip to content

Commit

Permalink
+random-string-based request id as headers
Browse files Browse the repository at this point in the history
  • Loading branch information
joy2fun committed Jan 12, 2024
1 parent 264e966 commit afec111
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ summary: Send request info to stdout.
type: middleware
import: github.com/joy2fun/traefik-plugin-log-request
testData:
responseBody: true
ResponseBody: false
RequestIDHeaderName: X-Request-Id
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ http:
my-plugin:
plugin:
log-request:
responseBody: true # also including response body
ResponseBody: false # also including response body
RequestIDHeaderName: X-Request-Id
```
crd example:
Expand All @@ -21,10 +22,11 @@ metadata:
spec:
plugin:
log-request:
responseBody: true
ResponseBody: false
RequestIDHeaderName: X-Request-Id
```
helm chart values example:
helm chart values example (local plugin mode):
```yaml
additionalArguments:
Expand Down
49 changes: 38 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -14,7 +16,8 @@ import (

// Config holds the plugin configuration.
type Config struct {
ResponseBody bool `json:"responseBody,omitempty"`
ResponseBody bool `json:"responseBody,omitempty"`
RequestIDHeaderName string `json:"requestIDHeaderName,omitempty"`
}

// CreateConfig creates and initializes the plugin configuration.
Expand All @@ -23,9 +26,10 @@ func CreateConfig() *Config {
}

type logRequest struct {
name string
next http.Handler
responseBody bool
name string
next http.Handler
responseBody bool
requestIDHeaderName string
}

type RequestData struct {
Expand All @@ -34,17 +38,31 @@ type RequestData struct {
Body string `json:"body"`
Headers string `json:"headers"`
ResponseBody string `json:"response_body"`
RequestID string `json:"request_id"`
}

func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.RequestIDHeaderName == "" {
config.RequestIDHeaderName = "X-Request-Id"
}

return &logRequest{
name: name,
next: next,
responseBody: config.ResponseBody,
name: name,
next: next,
responseBody: config.ResponseBody,
requestIDHeaderName: config.RequestIDHeaderName,
}, nil
}

func (p *logRequest) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
requestId, _ := generateRandomID(16)

if req.Header.Get(p.requestIDHeaderName) != "" {
requestId = req.Header.Get(p.requestIDHeaderName)
}

req.Header.Set(p.requestIDHeaderName, requestId)

body, err := io.ReadAll(req.Body)
if err != nil {
}
Expand All @@ -70,10 +88,11 @@ func (p *logRequest) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

requestData := RequestData{
URL: req.URL.String(),
Host: req.Host,
Body: string(body),
Headers: string(jsonHeader),
URL: req.URL.String(),
Host: req.Host,
Body: string(body),
Headers: string(jsonHeader),
RequestID: requestId,
}

if p.responseBody {
Expand Down Expand Up @@ -116,3 +135,11 @@ func (r *responseWriter) Flush() {
flusher.Flush()
}
}

func generateRandomID(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}

0 comments on commit afec111

Please sign in to comment.