Skip to content

Commit

Permalink
Tracing: Helper function to retrieve trace ID from context (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
njvrzm authored Jul 10, 2024
1 parent db9eddd commit 7c8ae12
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/tracing/tracing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracing

import (
"context"
"sync"

"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -40,3 +41,12 @@ func DefaultTracer() trace.Tracer {
func InitDefaultTracer(tracer trace.Tracer) {
defaultTracer = &contextualTracer{tracer: tracer}
}

func TraceIDFromContext(ctx context.Context, requireSampled bool) string {
spanCtx := trace.SpanContextFromContext(ctx)
if !spanCtx.HasTraceID() || !spanCtx.IsValid() || (requireSampled && !spanCtx.IsSampled()) {
return ""
}

return spanCtx.TraceID().String()
}

0 comments on commit 7c8ae12

Please sign in to comment.