github.com/annwntech/go-micro/v2@v2.9.5/debug/trace/memory/memory.go (about) 1 package memory 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/google/uuid" 8 "github.com/annwntech/go-micro/v2/debug/trace" 9 "github.com/annwntech/go-micro/v2/util/ring" 10 ) 11 12 type Tracer struct { 13 opts trace.Options 14 15 // ring buffer of traces 16 buffer *ring.Buffer 17 } 18 19 func (t *Tracer) Read(opts ...trace.ReadOption) ([]*trace.Span, error) { 20 var options trace.ReadOptions 21 for _, o := range opts { 22 o(&options) 23 } 24 25 sp := t.buffer.Get(t.buffer.Size()) 26 27 spans := make([]*trace.Span, 0, len(sp)) 28 29 for _, span := range sp { 30 val := span.Value.(*trace.Span) 31 // skip if trace id is specified and doesn't match 32 if len(options.Trace) > 0 && val.Trace != options.Trace { 33 continue 34 } 35 spans = append(spans, val) 36 } 37 38 return spans, nil 39 } 40 41 func (t *Tracer) Start(ctx context.Context, name string) (context.Context, *trace.Span) { 42 span := &trace.Span{ 43 Name: name, 44 Trace: uuid.New().String(), 45 Id: uuid.New().String(), 46 Started: time.Now(), 47 Metadata: make(map[string]string), 48 } 49 50 // return span if no context 51 if ctx == nil { 52 return trace.ToContext(context.Background(), span.Trace, span.Id), span 53 } 54 traceID, parentSpanID, ok := trace.FromContext(ctx) 55 // If the trace can not be found in the header, 56 // that means this is where the trace is created. 57 if !ok { 58 return trace.ToContext(ctx, span.Trace, span.Id), span 59 } 60 61 // set trace id 62 span.Trace = traceID 63 // set parent 64 span.Parent = parentSpanID 65 66 // return the span 67 return trace.ToContext(ctx, span.Trace, span.Id), span 68 } 69 70 func (t *Tracer) Finish(s *trace.Span) error { 71 // set finished time 72 s.Duration = time.Since(s.Started) 73 // save the span 74 t.buffer.Put(s) 75 76 return nil 77 } 78 79 func NewTracer(opts ...trace.Option) trace.Tracer { 80 var options trace.Options 81 for _, o := range opts { 82 o(&options) 83 } 84 85 return &Tracer{ 86 opts: options, 87 // the last 256 requests 88 buffer: ring.New(256), 89 } 90 }