mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Tracing: traces now correctly form a tree (#1170)
* First cut of trace * Dial it back yo * Defensive programming
This commit is contained in:
parent
16552620e0
commit
92571db693
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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 (
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user