feat: custom tracing keys

This commit is contained in:
Samuel Berthe
2024-04-21 21:18:47 +02:00
parent 8efbe1cd85
commit 8e1506be69
2 changed files with 40 additions and 2 deletions
+35
View File
@@ -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
+5 -2
View File
@@ -18,6 +18,9 @@ const (
)
var (
TraceIDKey = "trace-id"
SpanIDKey = "span-id"
RequestBodyMaxSize = 64 * 1024 // 64KB
ResponseBodyMaxSize = 64 * 1024 // 64KB
@@ -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