diff --git a/debug/trace/memory/memory.go b/debug/trace/memory/memory.go index 11413adc..99e67c03 100644 --- a/debug/trace/memory/memory.go +++ b/debug/trace/memory/memory.go @@ -49,27 +49,27 @@ func (t *Tracer) Start(ctx context.Context, name string) (context.Context, *trac // return span if no context if ctx == nil { - return context.Background(), span + return trace.ToContext(context.Background(), span.Trace, span.Id), span } - - s, ok := trace.FromContext(ctx) + traceID, parentSpanID, ok := trace.FromContext(ctx) + // If the trace can not be found in the header, + // that means this is where the trace is created. if !ok { - return ctx, span + return trace.ToContext(ctx, span.Trace, span.Id), span } // set trace id - span.Trace = s.Trace + span.Trace = traceID // set parent - span.Parent = s.Id + span.Parent = parentSpanID - // return the sapn - return ctx, span + // return the span + return trace.ToContext(ctx, span.Trace, span.Id), span } func (t *Tracer) Finish(s *trace.Span) error { // set finished time s.Duration = time.Since(s.Started) - // save the span t.buffer.Put(s) @@ -84,7 +84,7 @@ func NewTracer(opts ...trace.Option) trace.Tracer { return &Tracer{ opts: options, - // the last 64 requests - buffer: ring.New(64), + // the last 256 requests + buffer: ring.New(256), } } diff --git a/debug/trace/trace.go b/debug/trace/trace.go index 1c1f85bc..efb8c432 100644 --- a/debug/trace/trace.go +++ b/debug/trace/trace.go @@ -3,6 +3,7 @@ package trace import ( "context" + "github.com/micro/go-micro/v2/metadata" "time" ) @@ -34,17 +35,32 @@ type Span struct { Metadata map[string]string } -type spanKey struct{} +const ( + traceIDKey = "Micro-Trace-Id" + spanIDKey = "Micro-Span-Id" +) // FromContext returns a span from context -func FromContext(ctx context.Context) (*Span, bool) { - s, ok := ctx.Value(spanKey{}).(*Span) - return s, ok +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 creates a new context with the span -func NewContext(ctx context.Context, s *Span) context.Context { - return context.WithValue(ctx, spanKey{}, s) +// ToContext saves the trace and span ids in the context +func ToContext(ctx context.Context, traceID, parentSpanID string) context.Context { + return metadata.MergeContext(ctx, map[string]string{ + traceIDKey: traceID, + spanIDKey: parentSpanID, + }, true) } var ( diff --git a/metadata/metadata.go b/metadata/metadata.go index 3d6357ec..2f1f234b 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -63,6 +63,9 @@ func NewContext(ctx context.Context, md Metadata) context.Context { // MergeContext merges metadata to existing metadata, overwriting if specified func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context.Context { + if ctx == nil { + ctx = context.Background() + } md, _ := ctx.Value(metaKey{}).(Metadata) cmd := make(Metadata) for k, v := range md {