From 94beb5ed3b72c9fb17d3f8d358e5298d85713147 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 19 Dec 2024 18:47:03 +0300 Subject: [PATCH] tracer: minimize overhead on noop tracer usage Signed-off-by: Vasiliy Tolstov --- tracer/context.go | 9 +++++++++ tracer/memory/memory.go | 21 ++++++++++---------- tracer/noop.go | 44 ++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/tracer/context.go b/tracer/context.go index 3340dfb9..4537f1c7 100644 --- a/tracer/context.go +++ b/tracer/context.go @@ -37,6 +37,15 @@ func NewContext(ctx context.Context, tracer Tracer) context.Context { type spanKey struct{} +// SpanFromContext returns a span from context +func SpanMustContext(ctx context.Context) Span { + sp, ok := SpanFromContext(ctx) + if !ok { + panic("missing span") + } + return sp +} + // SpanFromContext returns a span from context func SpanFromContext(ctx context.Context) (Span, bool) { if ctx == nil { diff --git a/tracer/memory/memory.go b/tracer/memory/memory.go index 4216d863..260755a2 100644 --- a/tracer/memory/memory.go +++ b/tracer/memory/memory.go @@ -25,6 +25,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOpti name: name, ctx: ctx, tracer: t, + labels: options.Labels, kind: options.Kind, startTime: time.Now(), } @@ -37,6 +38,14 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOpti return tracer.NewSpanContext(ctx, span), span } +type memoryStringer struct { + s string +} + +func (s memoryStringer) String() string { + return s.s +} + func (t *Tracer) Flush(_ context.Context) error { return nil } @@ -52,14 +61,6 @@ func (t *Tracer) Name() string { return t.opts.Name } -type noopStringer struct { - s string -} - -func (s noopStringer) String() string { - return s.s -} - type Span struct { ctx context.Context tracer tracer.Tracer @@ -67,8 +68,8 @@ type Span struct { statusMsg string startTime time.Time finishTime time.Time - traceID noopStringer - spanID noopStringer + traceID memoryStringer + spanID memoryStringer events []*Event labels []interface{} logs []interface{} diff --git a/tracer/noop.go b/tracer/noop.go index de4756cc..4bab1cd3 100644 --- a/tracer/noop.go +++ b/tracer/noop.go @@ -2,6 +2,7 @@ package tracer import ( "context" + "time" "go.unistack.org/micro/v3/util/id" ) @@ -20,18 +21,18 @@ func (t *noopTracer) Spans() []Span { func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) { options := NewSpanOptions(opts...) span := &noopSpan{ - name: name, - ctx: ctx, - tracer: t, - labels: options.Labels, - kind: options.Kind, + name: name, + ctx: ctx, + tracer: t, + startTime: time.Now(), + labels: options.Labels, + kind: options.Kind, } span.spanID.s, _ = id.New() span.traceID.s, _ = id.New() if span.ctx == nil { span.ctx = context.Background() } - t.spans = append(t.spans, span) return NewSpanContext(ctx, span), span } @@ -58,23 +59,18 @@ func (t *noopTracer) Name() string { return t.opts.Name } -type noopEvent struct { - name string - labels []interface{} -} - type noopSpan struct { - ctx context.Context - tracer Tracer - name string - statusMsg string - traceID noopStringer - spanID noopStringer - events []*noopEvent - labels []interface{} - logs []interface{} - kind SpanKind - status SpanStatus + ctx context.Context + tracer Tracer + name string + statusMsg string + startTime time.Time + finishTime time.Time + traceID noopStringer + spanID noopStringer + labels []interface{} + kind SpanKind + status SpanStatus } func (s *noopSpan) TraceID() string { @@ -86,6 +82,7 @@ func (s *noopSpan) SpanID() string { } func (s *noopSpan) Finish(_ ...SpanOption) { + s.finishTime = time.Now() } func (s *noopSpan) Context() context.Context { @@ -97,8 +94,6 @@ func (s *noopSpan) Tracer() Tracer { } func (s *noopSpan) AddEvent(name string, opts ...EventOption) { - options := NewEventOptions(opts...) - s.events = append(s.events, &noopEvent{name: name, labels: options.Labels}) } func (s *noopSpan) SetName(name string) { @@ -106,7 +101,6 @@ func (s *noopSpan) SetName(name string) { } func (s *noopSpan) AddLogs(kv ...interface{}) { - s.logs = append(s.logs, kv...) } func (s *noopSpan) AddLabels(kv ...interface{}) { -- 2.45.2