Skip to content

Commit

Permalink
feat: custom tracing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 21, 2024
1 parent ff7dd70 commit 6a6205a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ No breaking changes will be made to exported APIs before v2.0.0.

## 💡 Usage

### Handler options

```go
type Config struct {
DefaultLevel slog.Level
ClientErrorLevel slog.Level
ServerErrorLevel slog.Level

WithUserAgent bool
WithRequestID bool
WithRequestBody bool
WithRequestHeader bool
WithResponseBody bool
WithResponseHeader bool
WithSpanID bool
WithTraceID bool

Filters []Filter
}
```

Attributes will be injected in log payload.

Other global parameters:

```go
slogfiber.TraceIDKey = "trace-id"
slogfiber.SpanIDKey = "span-id"
slogfiber.RequestBodyMaxSize = 64 * 1024 // 64KB
slogfiber.ResponseBodyMaxSize = 64 * 1024 // 64KB
slogfiber.HiddenRequestHeaders = map[string]struct{}{ ... }
slogfiber.HiddenResponseHeaders = map[string]struct{}{ ... }
slogfiber.RequestIDHeaderKey = "X-Request-Id"
```

### Minimal

```go
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/samber/slog-formatter v1.0.0
github.com/samber/slog-multi v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
Expand All @@ -23,7 +24,6 @@ require (
require (
github.com/gofiber/fiber/v2 v2.52.1
github.com/google/uuid v1.5.0
github.com/samber/slog-formatter v1.0.0
go.opentelemetry.io/otel/trace v1.19.0
go.uber.org/goleak v1.2.1
)
7 changes: 5 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type customAttributesCtxKeyType struct{}
var customAttributesCtxKey = customAttributesCtxKeyType{}

var (
TraceIDKey = "trace-id"
SpanIDKey = "span-id"

RequestBodyMaxSize = 64 * 1024 // 64KB
ResponseBodyMaxSize = 64 * 1024 // 64KB

Expand Down Expand Up @@ -175,11 +178,11 @@ func NewWithConfig(logger *slog.Logger, config Config) fiber.Handler {
// otel
if config.WithTraceID {
traceID := trace.SpanFromContext(c.Context()).SpanContext().TraceID().String()
baseAttributes = append(baseAttributes, slog.String("trace-id", traceID))
baseAttributes = append(baseAttributes, slog.String(TraceIDKey, traceID))
}
if config.WithSpanID {
spanID := trace.SpanFromContext(c.Context()).SpanContext().SpanID().String()
baseAttributes = append(baseAttributes, slog.String("span-id", spanID))
baseAttributes = append(baseAttributes, slog.String(SpanIDKey, spanID))
}

// request body
Expand Down

0 comments on commit 6a6205a

Please sign in to comment.