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 20, 2024
1 parent 6354392 commit 31133bd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 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 @@ -177,11 +180,11 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
// 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 Expand Up @@ -216,7 +219,7 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
requestAttributes = append(requestAttributes, slog.Any("x-forwarded-for", ips))
}

// response body body
// response body
responseAttributes = append(responseAttributes, slog.Int("length", bw.bytes))
if config.WithResponseBody {
responseAttributes = append(responseAttributes, slog.String("body", bw.body.String()))
Expand Down

0 comments on commit 31133bd

Please sign in to comment.