micro/debug/trace/trace.go

87 lines
1.8 KiB
Go
Raw Normal View History

2020-01-18 10:20:46 +00:00
// Package trace provides an interface for distributed tracing
package trace
import (
"context"
"github.com/micro/go-micro/v2/metadata"
2020-01-18 10:20:46 +00:00
"time"
)
2020-01-29 15:45:11 +00:00
// Tracer is an interface for distributed tracing
type Tracer interface {
2020-01-18 10:20:46 +00:00
// Start a trace
2020-01-24 21:44:48 +00:00
Start(ctx context.Context, name string) (context.Context, *Span)
2020-01-18 10:20:46 +00:00
// Finish the trace
Finish(*Span) error
// Read the traces
Read(...ReadOption) ([]*Span, error)
}
// Span is used to record an entry
type Span struct {
// Id of the trace
Trace string
// name of the span
Name string
// id of the span
Id string
// parent span id
Parent string
// Start time
Started time.Time
2020-01-24 21:24:51 +00:00
// Duration in nano seconds
Duration time.Duration
2020-01-18 10:20:46 +00:00
// associated data
Metadata map[string]string
}
const (
traceIDKey = "Micro-Trace-Id"
spanIDKey = "Micro-Span-Id"
)
2020-01-18 10:20:46 +00:00
// 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
2020-01-18 10:20:46 +00:00
}
// 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)
2020-01-18 10:20:46 +00:00
}
2020-01-29 15:45:11 +00:00
var (
DefaultTracer Tracer = new(noop)
)
type noop struct{}
func (n *noop) Init(...Option) error {
return nil
}
func (n *noop) Start(ctx context.Context, name string) (context.Context, *Span) {
return nil, nil
}
func (n *noop) Finish(*Span) error {
return nil
}
func (n *noop) Read(...ReadOption) ([]*Span, error) {
return nil, nil
}