2020-01-29 15:45:11 +00:00
|
|
|
package memory
|
2020-01-18 10:20:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/debug/trace"
|
|
|
|
"github.com/micro/go-micro/v2/util/ring"
|
2020-01-18 10:20:46 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
type Tracer struct {
|
|
|
|
opts trace.Options
|
2020-01-18 10:20:46 +00:00
|
|
|
|
|
|
|
// ring buffer of traces
|
|
|
|
buffer *ring.Buffer
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
func (t *Tracer) Read(opts ...trace.ReadOption) ([]*trace.Span, error) {
|
|
|
|
var options trace.ReadOptions
|
2020-01-24 21:24:51 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := t.buffer.Get(t.buffer.Size())
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
var spans []*trace.Span
|
2020-01-24 21:24:51 +00:00
|
|
|
|
|
|
|
for _, span := range sp {
|
2020-01-29 15:45:11 +00:00
|
|
|
val := span.Value.(*trace.Span)
|
2020-01-24 21:24:51 +00:00
|
|
|
// 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
|
2020-01-18 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
func (t *Tracer) Start(ctx context.Context, name string) (context.Context, *trace.Span) {
|
|
|
|
span := &trace.Span{
|
2020-01-18 10:20:46 +00:00
|
|
|
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 {
|
2020-01-24 21:44:48 +00:00
|
|
|
return context.Background(), span
|
2020-01-18 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
s, ok := trace.FromContext(ctx)
|
2020-01-18 10:20:46 +00:00
|
|
|
if !ok {
|
2020-01-24 21:44:48 +00:00
|
|
|
return ctx, span
|
2020-01-18 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set trace id
|
|
|
|
span.Trace = s.Trace
|
|
|
|
// set parent
|
|
|
|
span.Parent = s.Id
|
|
|
|
|
|
|
|
// return the sapn
|
2020-01-24 21:44:48 +00:00
|
|
|
return ctx, span
|
2020-01-18 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
func (t *Tracer) Finish(s *trace.Span) error {
|
2020-01-18 10:20:46 +00:00
|
|
|
// set finished time
|
2020-01-24 21:24:51 +00:00
|
|
|
s.Duration = time.Since(s.Started)
|
2020-01-18 10:20:46 +00:00
|
|
|
|
|
|
|
// save the span
|
|
|
|
t.buffer.Put(s)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
func NewTracer(opts ...trace.Option) trace.Tracer {
|
|
|
|
var options trace.Options
|
2020-01-18 10:20:46 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
return &Tracer{
|
2020-01-18 10:20:46 +00:00
|
|
|
opts: options,
|
|
|
|
// the last 64 requests
|
|
|
|
buffer: ring.New(64),
|
|
|
|
}
|
|
|
|
}
|