Skip to content

Commit

Permalink
tracing: use opentracing.GlobalTracer instead of globalTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
redloaf committed Nov 10, 2023
1 parent ccc5cd0 commit 3f88945
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
4 changes: 3 additions & 1 deletion tracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func (s Span) StopTime() time.Time {
// This uses the the logger provided by the underlying tracing.Tracer used to
// publish the Span.
func (s Span) logError(ctx context.Context, msg string, err error) {
s.trace.tracer.logger.Log(ctx, msg+err.Error())
if tracer := s.trace.getTracer(); tracer != nil {
tracer.logger.Log(ctx, msg+err.Error())
}
}

// AddHooks adds hooks into the Span.
Expand Down
41 changes: 33 additions & 8 deletions tracing/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/opentracing/opentracing-go"

"github.com/reddit/baseplate.go/randbp"
"github.com/reddit/baseplate.go/timebp"
)
Expand All @@ -31,7 +33,7 @@ const (
)

type trace struct {
tracer *Tracer
tracer opentracing.Tracer

name string
traceID string
Expand All @@ -49,16 +51,36 @@ type trace struct {
tags map[string]string
}

func (t *trace) getTracer() *Tracer {
if t == nil {
return nil
}
if tracer, ok := t.tracer.(*Tracer); ok && tracer != nil {
return tracer
}
return nil
}

func newTrace(tracer *Tracer, name string) *trace {
var (
otTracer opentracing.Tracer
traceID, spanID string
)
if tracer == nil {
tracer = &globalTracer
otTracer = opentracing.GlobalTracer()
traceID = globalTracer.newTraceID()
spanID = globalTracer.newSpanID()
} else {
otTracer = tracer
traceID = tracer.newTraceID()
spanID = tracer.newSpanID()
}
return &trace{
tracer: tracer,
tracer: otTracer,

name: name,
traceID: tracer.newTraceID(),
spanID: tracer.newSpanID(),
traceID: traceID,
spanID: spanID,
start: time.Now(),

counters: make(map[string]float64),
Expand Down Expand Up @@ -91,8 +113,8 @@ func (t *trace) toZipkinSpan() ZipkinSpan {
zs.Duration = timebp.DurationMicrosecond(end.Sub(t.start))

var endpoint ZipkinEndpointInfo
if t.tracer != nil {
endpoint = t.tracer.endpoint
if tracer := t.getTracer(); tracer != nil {
endpoint = tracer.endpoint
}

if t.timeAnnotationReceiveKey != "" {
Expand Down Expand Up @@ -159,7 +181,10 @@ func (t *trace) publish(ctx context.Context) error {
if !t.shouldSample() || t.tracer == nil {
return nil
}
return t.tracer.Record(ctx, t.toZipkinSpan())
if tracer := t.getTracer(); tracer != nil {
return tracer.Record(ctx, t.toZipkinSpan())
}
return nil
}

// In opentracing spec, zero trace/span/parent ids have special meanings.
Expand Down

0 comments on commit 3f88945

Please sign in to comment.