Refactor debug/trace ready for Jaeger
This commit is contained in:
@@ -25,14 +25,14 @@ type Debug struct {
|
||||
// the stats collector
|
||||
stats stats.Stats
|
||||
// the tracer
|
||||
trace trace.Trace
|
||||
trace trace.Tracer
|
||||
}
|
||||
|
||||
func newDebug() *Debug {
|
||||
return &Debug{
|
||||
log: log.DefaultLog,
|
||||
stats: stats.DefaultStats,
|
||||
trace: trace.DefaultTrace,
|
||||
trace: trace.DefaultTracer,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,32 +1,33 @@
|
||||
package trace
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/go-micro/debug/trace"
|
||||
"github.com/micro/go-micro/util/ring"
|
||||
)
|
||||
|
||||
type trace struct {
|
||||
opts Options
|
||||
type Tracer struct {
|
||||
opts trace.Options
|
||||
|
||||
// ring buffer of traces
|
||||
buffer *ring.Buffer
|
||||
}
|
||||
|
||||
func (t *trace) Read(opts ...ReadOption) ([]*Span, error) {
|
||||
var options ReadOptions
|
||||
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())
|
||||
|
||||
var spans []*Span
|
||||
var spans []*trace.Span
|
||||
|
||||
for _, span := range sp {
|
||||
val := span.Value.(*Span)
|
||||
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
|
||||
@@ -37,8 +38,8 @@ func (t *trace) Read(opts ...ReadOption) ([]*Span, error) {
|
||||
return spans, nil
|
||||
}
|
||||
|
||||
func (t *trace) Start(ctx context.Context, name string) (context.Context, *Span) {
|
||||
span := &Span{
|
||||
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(),
|
||||
@@ -51,7 +52,7 @@ func (t *trace) Start(ctx context.Context, name string) (context.Context, *Span)
|
||||
return context.Background(), span
|
||||
}
|
||||
|
||||
s, ok := FromContext(ctx)
|
||||
s, ok := trace.FromContext(ctx)
|
||||
if !ok {
|
||||
return ctx, span
|
||||
}
|
||||
@@ -65,7 +66,7 @@ func (t *trace) Start(ctx context.Context, name string) (context.Context, *Span)
|
||||
return ctx, span
|
||||
}
|
||||
|
||||
func (t *trace) Finish(s *Span) error {
|
||||
func (t *Tracer) Finish(s *trace.Span) error {
|
||||
// set finished time
|
||||
s.Duration = time.Since(s.Started)
|
||||
|
||||
@@ -75,13 +76,13 @@ func (t *trace) Finish(s *Span) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewTrace(opts ...Option) Trace {
|
||||
var options Options
|
||||
func NewTracer(opts ...trace.Option) trace.Tracer {
|
||||
var options trace.Options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &trace{
|
||||
return &Tracer{
|
||||
opts: options,
|
||||
// the last 64 requests
|
||||
buffer: ring.New(64),
|
@@ -1,6 +1,9 @@
|
||||
package trace
|
||||
|
||||
type Options struct{}
|
||||
type Options struct {
|
||||
// Size is the size of ring buffer
|
||||
Size int
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
|
||||
@@ -17,3 +20,15 @@ func ReadTrace(t string) ReadOption {
|
||||
o.Trace = t
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
// DefaultSize of the buffer
|
||||
DefaultSize = 64
|
||||
)
|
||||
|
||||
// DefaultOptions returns default options
|
||||
func DefaultOptions() Options {
|
||||
return Options{
|
||||
Size: DefaultSize,
|
||||
}
|
||||
}
|
||||
|
@@ -6,8 +6,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Trace is an interface for distributed tracing
|
||||
type Trace interface {
|
||||
// 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
|
||||
@@ -36,11 +36,6 @@ type Span struct {
|
||||
|
||||
type spanKey struct{}
|
||||
|
||||
var (
|
||||
// Default tracer
|
||||
DefaultTrace = NewTrace()
|
||||
)
|
||||
|
||||
// FromContext returns a span from context
|
||||
func FromContext(ctx context.Context) (*Span, bool) {
|
||||
s, ok := ctx.Value(spanKey{}).(*Span)
|
||||
@@ -51,3 +46,25 @@ func FromContext(ctx context.Context) (*Span, bool) {
|
||||
func NewContext(ctx context.Context, s *Span) context.Context {
|
||||
return context.WithValue(ctx, spanKey{}, s)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user