move out tracers
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
25
tracer/noop.go
Normal file
25
tracer/noop.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package tracer
|
||||
|
||||
import "context"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func newTracer(opts ...Option) Tracer {
|
||||
return &noop{}
|
||||
}
|
||||
34
tracer/options.go
Normal file
34
tracer/options.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package tracer
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
81
tracer/trace.go
Normal file
81
tracer/trace.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// Package trace provides an interface for distributed tracing
|
||||
package tracer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/unistack-org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultTracer Tracer = newTracer()
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user