Skip to content

Commit

Permalink
feat: add some traceparent utils
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Oct 13, 2024
1 parent 6248be4 commit 269158b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions telemetry/tracev2/memory/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ func (t *memoryTrace) Start(ctx context.Context, name string) context.Context {
parentCtxCfg := trace.SpanContextConfig{}

if t.span == nil {
if traceparent, ok := tracev2.TraceParentFromContext(ctx); ok {
parentCtxCfg.TraceID = traceparent
}

newCtx, span := t.start(ctx, name, parentCtxCfg)

t.span = span

return newCtx
}

Expand Down
28 changes: 28 additions & 0 deletions telemetry/tracev2/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tracev2

import (
"context"

"github.com/w-h-a/pkg/utils/metadatautils"
)

const (
traceParentKey = "traceparent"
)

func ContextWithTraceParent(ctx context.Context, traceparent [16]byte) (context.Context, error) {
return metadatautils.MergeContext(ctx, map[string]string{
traceParentKey: string(traceparent[:]),
}, true), nil
}

func TraceParentFromContext(ctx context.Context) (traceparent [16]byte, found bool) {
traceId, ok := metadatautils.GetContext(ctx, traceParentKey)
if !ok {
return
}

copy(traceparent[:], traceId)

return
}

0 comments on commit 269158b

Please sign in to comment.