2020-10-16 09:38:57 +03:00
|
|
|
// Package trace provides an interface for distributed tracing
|
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
traceIDKey = "Micro-Trace-Id"
|
|
|
|
spanIDKey = "Micro-Span-Id"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromContext returns a span from context
|
|
|
|
func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) {
|
|
|
|
traceID, traceOk := metadata.Get(ctx, traceIDKey)
|
|
|
|
microID, microOk := metadata.Get(ctx, "Micro-Id")
|
|
|
|
if !traceOk && !microOk {
|
|
|
|
isFound = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !traceOk {
|
|
|
|
traceID = microID
|
|
|
|
}
|
|
|
|
parentSpanID, ok := metadata.Get(ctx, spanIDKey)
|
|
|
|
return traceID, parentSpanID, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext saves the trace and span ids in the context
|
|
|
|
func NewContext(ctx context.Context, traceID, parentSpanID string) context.Context {
|
2020-11-18 16:50:41 +03:00
|
|
|
md := metadata.New(2)
|
|
|
|
md.Set(traceIDKey, traceID)
|
|
|
|
md.Set(spanIDKey, parentSpanID)
|
|
|
|
return metadata.MergeContext(ctx, md, true)
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|