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 8efbe1c commit 8e1506b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
sloggin.TraceIDKey = "trace-id"
sloggin.SpanIDKey = "span-id"
sloggin.RequestBodyMaxSize = 64 * 1024 // 64KB
sloggin.ResponseBodyMaxSize = 64 * 1024 // 64KB
sloggin.HiddenRequestHeaders = map[string]struct{}{ ... }
sloggin.HiddenResponseHeaders = map[string]struct{}{ ... }
sloggin.RequestIDHeaderKey = "X-Request-Id"
```

### Minimal

```go
Expand Down
7 changes: 5 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
)

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

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

Expand Down Expand Up @@ -168,11 +171,11 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
// otel
if config.WithTraceID {
traceID := trace.SpanFromContext(c.Request.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.Request.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 8e1506b

Please sign in to comment.