From 8d2dc8a822e6e312de271dd75b4c9501e1ac64c3 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 24 Jan 2020 21:24:51 +0000 Subject: [PATCH] trace Read endpoint --- debug/trace/default.go | 22 ++++++++++++++++++++-- debug/trace/options.go | 7 +++++++ debug/trace/trace.go | 4 ++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/debug/trace/default.go b/debug/trace/default.go index abbf1c15..05fd9d97 100644 --- a/debug/trace/default.go +++ b/debug/trace/default.go @@ -16,7 +16,25 @@ type trace struct { } func (t *trace) Read(opts ...ReadOption) ([]*Span, error) { - return []*Span{}, nil + var options ReadOptions + for _, o := range opts { + o(&options) + } + + sp := t.buffer.Get(t.buffer.Size()) + + var spans []*Span + + for _, span := range sp { + val := span.Value.(*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 *trace) Start(ctx context.Context, name string) *Span { @@ -49,7 +67,7 @@ func (t *trace) Start(ctx context.Context, name string) *Span { func (t *trace) Finish(s *Span) error { // set finished time - s.Finished = time.Now() + s.Duration = time.Since(s.Started) // save the span t.buffer.Put(s) diff --git a/debug/trace/options.go b/debug/trace/options.go index d92abad9..7a0af631 100644 --- a/debug/trace/options.go +++ b/debug/trace/options.go @@ -10,3 +10,10 @@ type ReadOptions struct { } type ReadOption func(o *ReadOptions) + +// Read the given trace +func ReadTrace(t string) ReadOption { + return func(o *ReadOptions) { + o.Trace = t + } +} diff --git a/debug/trace/trace.go b/debug/trace/trace.go index 4a4471c3..5cefad2e 100644 --- a/debug/trace/trace.go +++ b/debug/trace/trace.go @@ -28,8 +28,8 @@ type Span struct { Parent string // Start time Started time.Time - // Finish time - Finished time.Time + // Duration in nano seconds + Duration time.Duration // associated data Metadata map[string]string }