Merge pull request 'tracer: minimize overhead on noop tracer usage' (#381) from tr into v3
All checks were successful
test / test (push) Successful in 4m20s

Reviewed-on: #381
This commit is contained in:
Василий Толстов 2024-12-19 19:08:22 +03:00
commit 8d747c64a8
3 changed files with 39 additions and 35 deletions

View File

@ -37,6 +37,15 @@ func NewContext(ctx context.Context, tracer Tracer) context.Context {
type spanKey struct{} 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 // SpanFromContext returns a span from context
func SpanFromContext(ctx context.Context) (Span, bool) { func SpanFromContext(ctx context.Context) (Span, bool) {
if ctx == nil { if ctx == nil {

View File

@ -25,6 +25,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOpti
name: name, name: name,
ctx: ctx, ctx: ctx,
tracer: t, tracer: t,
labels: options.Labels,
kind: options.Kind, kind: options.Kind,
startTime: time.Now(), 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 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 { func (t *Tracer) Flush(_ context.Context) error {
return nil return nil
} }
@ -52,14 +61,6 @@ func (t *Tracer) Name() string {
return t.opts.Name return t.opts.Name
} }
type noopStringer struct {
s string
}
func (s noopStringer) String() string {
return s.s
}
type Span struct { type Span struct {
ctx context.Context ctx context.Context
tracer tracer.Tracer tracer tracer.Tracer
@ -67,8 +68,8 @@ type Span struct {
statusMsg string statusMsg string
startTime time.Time startTime time.Time
finishTime time.Time finishTime time.Time
traceID noopStringer traceID memoryStringer
spanID noopStringer spanID memoryStringer
events []*Event events []*Event
labels []interface{} labels []interface{}
logs []interface{} logs []interface{}

View File

@ -2,6 +2,7 @@ package tracer
import ( import (
"context" "context"
"time"
"go.unistack.org/micro/v3/util/id" "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) { func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
options := NewSpanOptions(opts...) options := NewSpanOptions(opts...)
span := &noopSpan{ span := &noopSpan{
name: name, name: name,
ctx: ctx, ctx: ctx,
tracer: t, tracer: t,
labels: options.Labels, startTime: time.Now(),
kind: options.Kind, labels: options.Labels,
kind: options.Kind,
} }
span.spanID.s, _ = id.New() span.spanID.s, _ = id.New()
span.traceID.s, _ = id.New() span.traceID.s, _ = id.New()
if span.ctx == nil { if span.ctx == nil {
span.ctx = context.Background() span.ctx = context.Background()
} }
t.spans = append(t.spans, span)
return NewSpanContext(ctx, span), span return NewSpanContext(ctx, span), span
} }
@ -58,23 +59,18 @@ func (t *noopTracer) Name() string {
return t.opts.Name return t.opts.Name
} }
type noopEvent struct {
name string
labels []interface{}
}
type noopSpan struct { type noopSpan struct {
ctx context.Context ctx context.Context
tracer Tracer tracer Tracer
name string name string
statusMsg string statusMsg string
traceID noopStringer startTime time.Time
spanID noopStringer finishTime time.Time
events []*noopEvent traceID noopStringer
labels []interface{} spanID noopStringer
logs []interface{} labels []interface{}
kind SpanKind kind SpanKind
status SpanStatus status SpanStatus
} }
func (s *noopSpan) TraceID() string { func (s *noopSpan) TraceID() string {
@ -86,6 +82,7 @@ func (s *noopSpan) SpanID() string {
} }
func (s *noopSpan) Finish(_ ...SpanOption) { func (s *noopSpan) Finish(_ ...SpanOption) {
s.finishTime = time.Now()
} }
func (s *noopSpan) Context() context.Context { func (s *noopSpan) Context() context.Context {
@ -97,8 +94,6 @@ func (s *noopSpan) Tracer() Tracer {
} }
func (s *noopSpan) AddEvent(name string, opts ...EventOption) { 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) { func (s *noopSpan) SetName(name string) {
@ -106,7 +101,6 @@ func (s *noopSpan) SetName(name string) {
} }
func (s *noopSpan) AddLogs(kv ...interface{}) { func (s *noopSpan) AddLogs(kv ...interface{}) {
s.logs = append(s.logs, kv...)
} }
func (s *noopSpan) AddLabels(kv ...interface{}) { func (s *noopSpan) AddLabels(kv ...interface{}) {