Skip to content

Commit

Permalink
Update otel trace_id logging because of deprecation
Browse files Browse the repository at this point in the history
Signed-off-by: Nicole Hubbard <[email protected]>
  • Loading branch information
nicolerenee committed Apr 2, 2024
1 parent 8470f29 commit 2a0cf39
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ginx/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ginprometheus "github.com/zsais/go-gin-prometheus"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

Expand Down Expand Up @@ -88,6 +89,12 @@ func DefaultEngine(lgr *zap.Logger, f LogFunc) *gin.Engine {
fields := []zapcore.Field{
zap.String("request_id", requestid.Get(c)),
}

if trace.SpanFromContext(c.Request.Context()).SpanContext().IsValid() {
fields = append(fields, zap.String("trace_id", trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()))
fields = append(fields, zap.String("span_id", trace.SpanFromContext(c.Request.Context()).SpanContext().SpanID().String()))
}

fields = append(fields, f(c)...)

return fields
Expand All @@ -99,7 +106,6 @@ func DefaultEngine(lgr *zap.Logger, f LogFunc) *gin.Engine {
ginzap.GinzapWithConfig(lgr, &ginzap.Config{
TimeFormat: time.RFC3339,
UTC: true,
TraceID: true,
Context: logF,
}),
ginzap.RecoveryWithZap(lgr, true),
Expand Down

0 comments on commit 2a0cf39

Please sign in to comment.