move out tracers

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-09-10 00:06:29 +03:00
parent f1fde75567
commit caec730248
12 changed files with 125 additions and 356 deletions

View File

@@ -1,90 +0,0 @@
package memory
import (
"context"
"time"
"github.com/google/uuid"
"github.com/unistack-org/micro/v3/debug/trace"
"github.com/unistack-org/micro/v3/util/ring"
)
type Tracer struct {
opts trace.Options
// ring buffer of traces
buffer *ring.Buffer
}
func (t *Tracer) Read(opts ...trace.ReadOption) ([]*trace.Span, error) {
var options trace.ReadOptions
for _, o := range opts {
o(&options)
}
sp := t.buffer.Get(t.buffer.Size())
spans := make([]*trace.Span, 0, len(sp))
for _, span := range sp {
val := span.Value.(*trace.Span)
// skip if trace id is specified and doesn't match
if len(options.Trace) > 0 && val.Trace != options.Trace {
continue
}
spans = append(spans, val)
}
return spans, nil
}
func (t *Tracer) Start(ctx context.Context, name string) (context.Context, *trace.Span) {
span := &trace.Span{
Name: name,
Trace: uuid.New().String(),
Id: uuid.New().String(),
Started: time.Now(),
Metadata: make(map[string]string),
}
// return span if no context
if ctx == nil {
return trace.ToContext(context.Background(), span.Trace, span.Id), span
}
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 trace.ToContext(ctx, span.Trace, span.Id), span
}
// set trace id
span.Trace = traceID
// set parent
span.Parent = parentSpanID
// 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)
return nil
}
func NewTracer(opts ...trace.Option) trace.Tracer {
var options trace.Options
for _, o := range opts {
o(&options)
}
return &Tracer{
opts: options,
// the last 256 requests
buffer: ring.New(256),
}
}

View File

@@ -1,34 +0,0 @@
package trace
type Options struct {
// Size is the size of ring buffer
Size int
}
type Option func(o *Options)
type ReadOptions struct {
// Trace id
Trace string
}
type ReadOption func(o *ReadOptions)
// Read the given trace
func ReadTrace(t string) ReadOption {
return func(o *ReadOptions) {
o.Trace = t
}
}
const (
// DefaultSize of the buffer
DefaultSize = 64
)
// DefaultOptions returns default options
func DefaultOptions() Options {
return Options{
Size: DefaultSize,
}
}

View File

@@ -1,99 +0,0 @@
// Package trace provides an interface for distributed tracing
package trace
import (
"context"
"time"
"github.com/unistack-org/micro/v3/metadata"
)
// Tracer is an interface for distributed tracing
type Tracer interface {
// Start a trace
Start(ctx context.Context, name string) (context.Context, *Span)
// Finish the trace
Finish(*Span) error
// Read the traces
Read(...ReadOption) ([]*Span, error)
}
// SpanType describe the nature of the trace span
type SpanType int
const (
// SpanTypeRequestInbound is a span created when serving a request
SpanTypeRequestInbound SpanType = iota
// SpanTypeRequestOutbound is a span created when making a service call
SpanTypeRequestOutbound
)
// 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
// Duration in nano seconds
Duration time.Duration
// associated data
Metadata map[string]string
// Type
Type SpanType
}
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
}
// 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 (
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
}