feat: support adding traceID and spanID (#7)

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
This commit is contained in:
Ben B
2023-10-30 10:12:15 +01:00
committed by GitHub
parent 26cd833d20
commit 6b1ec40410
3 changed files with 23 additions and 0 deletions
+2
View File
@@ -25,6 +25,8 @@ require (
github.com/samber/slog-multi v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
+4
View File
@@ -74,6 +74,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
+17
View File
@@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"go.opentelemetry.io/otel/trace"
)
const (
@@ -45,6 +46,8 @@ type Config struct {
WithRequestHeader bool
WithResponseBody bool
WithResponseHeader bool
WithSpanID bool
WithTraceID bool
Filters []Filter
}
@@ -64,6 +67,8 @@ func New(logger *slog.Logger) gin.HandlerFunc {
WithRequestHeader: false,
WithResponseBody: false,
WithResponseHeader: false,
WithSpanID: false,
WithTraceID: false,
Filters: []Filter{},
})
@@ -84,6 +89,8 @@ func NewWithFilters(logger *slog.Logger, filters ...Filter) gin.HandlerFunc {
WithRequestHeader: false,
WithResponseBody: false,
WithResponseHeader: false,
WithSpanID: false,
WithTraceID: false,
Filters: filters,
})
@@ -141,6 +148,16 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
attributes = append(attributes, slog.String("request-id", requestID))
}
if config.WithTraceID {
traceID := trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()
attributes = append(attributes, slog.String("trace-id", traceID))
}
if config.WithSpanID {
spanID := trace.SpanFromContext(c.Request.Context()).SpanContext().SpanID().String()
attributes = append(attributes, slog.String("span-id", spanID))
}
// request
if config.WithRequestBody {
attributes = append(attributes, slog.Group("request", slog.String("body", string(reqBody))))