Skip to content

Commit

Permalink
feat(request-id): generate request id as uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
jindrichskupa committed Apr 8, 2024
1 parent d3b2222 commit 1d7e586
Show file tree
Hide file tree
Showing 25 changed files with 1,457 additions and 11 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/cookielab/traefik-middleware-request-logger

go 1.19

require github.com/google/uuid v1.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14 changes: 3 additions & 11 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"time"

"github.com/google/uuid" //nolint:depguard
)

// Config holds the plugin configuration.
Expand Down Expand Up @@ -91,7 +91,7 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
}

func (p *logRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestID, _ := generateRandomID(16)
requestID := uuid.NewString()

if r.Header.Get(p.requestIDHeaderName) != "" {
requestID = r.Header.Get(p.requestIDHeaderName)
Expand Down Expand Up @@ -215,14 +215,6 @@ func (w *wrappedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return hijacker.Hijack()
}

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

func allowContentType(contentType string, contentTypes []string) bool {
if len(contentTypes) == 0 {
return true
Expand Down
41 changes: 41 additions & 0 deletions vendor/github.com/google/uuid/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/google/uuid/marshal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d7e586

Please sign in to comment.