summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-10-13 16:10:22 +0200
committerJulien Dessaux2024-10-13 16:10:22 +0200
commit71702002cfdad4cf4a55a92c03ac660903a65744 (patch)
tree40e1d926c9d06f3cf29235eed173b7726db40a09
parentfix(tfstated): add unlock handler tests (diff)
downloadtfstated-71702002cfdad4cf4a55a92c03ac660903a65744.tar.gz
tfstated-71702002cfdad4cf4a55a92c03ac660903a65744.tar.bz2
tfstated-71702002cfdad4cf4a55a92c03ac660903a65744.zip
feat(logger): implement optional body logging
Diffstat (limited to '')
-rw-r--r--cmd/tfstated/main.go2
-rw-r--r--pkg/logger/body_writer.go23
-rw-r--r--pkg/logger/middleware.go8
3 files changed, 28 insertions, 5 deletions
diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go
index 57c4461..5ead1ed 100644
--- a/cmd/tfstated/main.go
+++ b/cmd/tfstated/main.go
@@ -51,7 +51,7 @@ func run(
httpServer := &http.Server{
Addr: net.JoinHostPort(config.Host, config.Port),
- Handler: logger.Middleware(mux),
+ Handler: logger.Middleware(mux, false),
}
go func() {
log.Printf("listening on %s\n", httpServer.Addr)
diff --git a/pkg/logger/body_writer.go b/pkg/logger/body_writer.go
index 60da151..f57a03e 100644
--- a/pkg/logger/body_writer.go
+++ b/pkg/logger/body_writer.go
@@ -2,6 +2,7 @@ package logger
import (
"bufio"
+ "bytes"
"errors"
"net"
"net/http"
@@ -9,11 +10,20 @@ import (
type bodyWriter struct {
http.ResponseWriter
- status int
+ body *bytes.Buffer
+ maxSize int
+ bytes int
+ status int
}
-func newBodyWriter(writer http.ResponseWriter) *bodyWriter {
+func newBodyWriter(writer http.ResponseWriter, recordBody bool) *bodyWriter {
+ var body *bytes.Buffer
+ if recordBody {
+ body = bytes.NewBufferString("")
+ }
return &bodyWriter{
+ body: body,
+ maxSize: 64000,
ResponseWriter: writer,
status: http.StatusNotImplemented,
}
@@ -21,6 +31,15 @@ func newBodyWriter(writer http.ResponseWriter) *bodyWriter {
// implements http.ResponseWriter
func (w *bodyWriter) Write(b []byte) (int, error) {
+ if w.body != nil {
+ if w.body.Len()+len(b) > w.maxSize {
+ w.body.Write(b[:w.maxSize-w.body.Len()])
+ } else {
+ w.body.Write(b)
+ }
+ }
+
+ w.bytes += len(b)
return w.ResponseWriter.Write(b)
}
diff --git a/pkg/logger/middleware.go b/pkg/logger/middleware.go
index d0a6a65..54cca96 100644
--- a/pkg/logger/middleware.go
+++ b/pkg/logger/middleware.go
@@ -8,7 +8,7 @@ import (
"time"
)
-func Middleware(next http.Handler) http.Handler {
+func Middleware(next http.Handler, recordBody bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/healthz" {
next.ServeHTTP(w, r)
@@ -29,7 +29,7 @@ func Middleware(next http.Handler) http.Handler {
path := r.URL.Path
query := r.URL.RawQuery
- bw := newBodyWriter(w)
+ bw := newBodyWriter(w, recordBody)
next.ServeHTTP(bw, r)
@@ -45,8 +45,12 @@ func Middleware(next http.Handler) http.Handler {
responseAttributes := []slog.Attr{
slog.Time("time", end.UTC()),
slog.Duration("latency", end.Sub(start)),
+ slog.Int("length", bw.bytes),
slog.Int("status", bw.status),
}
+ if recordBody {
+ responseAttributes = append(responseAttributes, slog.String("body", bw.body.String()))
+ }
attributes := []slog.Attr{
{
Key: "request",