feat: include tracing only for the recording spans

This commit is contained in:
Samuel Berthe
2024-04-21 23:13:31 +02:00
parent 0a948b00ea
commit 812b3ffb5d
+3 -3
View File
@@ -1,6 +1,7 @@
package sloggin package sloggin
import ( import (
"context"
"log/slog" "log/slog"
"net/http" "net/http"
"strings" "strings"
@@ -168,7 +169,7 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
} }
// otel // otel
baseAttributes = append(baseAttributes, extractTraceSpanID(c, config.WithTraceID, config.WithSpanID)...) baseAttributes = append(baseAttributes, extractTraceSpanID(c.Request.Context(), config.WithTraceID, config.WithSpanID)...)
// request body // request body
requestAttributes = append(requestAttributes, slog.Int("length", br.bytes)) requestAttributes = append(requestAttributes, slog.Int("length", br.bytes))
@@ -283,12 +284,11 @@ func AddCustomAttributes(c *gin.Context, attr slog.Attr) {
} }
} }
func extractTraceSpanID(c *gin.Context, withTraceID bool, withSpanID bool) []slog.Attr { func extractTraceSpanID(ctx context.Context, withTraceID bool, withSpanID bool) []slog.Attr {
if !(withTraceID || withSpanID) { if !(withTraceID || withSpanID) {
return []slog.Attr{} return []slog.Attr{}
} }
ctx := c.Request.Context()
span := trace.SpanFromContext(ctx) span := trace.SpanFromContext(ctx)
if !span.IsRecording() { if !span.IsRecording() {
return []slog.Attr{} return []slog.Attr{}