metadata: split context to incoming and outgoing

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-02-09 01:08:45 +03:00
parent 1de9911b73
commit 0e51a79bb6
9 changed files with 84 additions and 136 deletions

View File

@@ -14,11 +14,12 @@ const (
// FromContext returns a span from context
func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) {
if ctx == nil {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", "", false
}
traceID, traceOk := metadata.Get(ctx, traceIDKey)
microID, microOk := metadata.Get(ctx, "Micro-Id")
traceID, traceOk := md.Get(traceIDKey)
microID, microOk := md.Get("Micro-Id")
if !traceOk && !microOk {
isFound = false
return
@@ -26,17 +27,17 @@ func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFo
if !traceOk {
traceID = microID
}
parentSpanID, ok := metadata.Get(ctx, spanIDKey)
parentSpanID, ok = md.Get(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 {
if ctx == nil {
ctx = context.Background()
md, ok := metadata.FromContext(ctx)
if !ok {
md = metadata.New(2)
}
md := metadata.New(2)
md.Set(traceIDKey, traceID)
md.Set(spanIDKey, parentSpanID)
return metadata.MergeContext(ctx, md, true)
return metadata.NewContext(ctx, md)
}