Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: use opentracing.GlobalTracer instead of globalTracer #635

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.internalTracer(); tracer != nil {
tracer.logger.Log(ctx, msg+err.Error())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the interest of fallbacks, should this also do something if internalTracer isn't found? like fall back to the global logger?

Why does this inject its own logger anyway? 🤔

}

// AddHooks adds hooks into the Span.
Expand Down
46 changes: 38 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,41 @@ type trace struct {
tags map[string]string
}

func (t *trace) internalTracer() *Tracer {
if t == nil {
return nil
}
if tracer, ok := t.tracer.(*Tracer); ok && tracer != nil {
return tracer
}
return nil
Comment on lines +58 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't return nil in-band, that's a recipe for nil pointer dereferences. Return *Tracer, bool

}

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

// opentracing.Tracer is an interface that has only StartSpan, Inject, and Extract methods.
// This function expects a *tracing.Tracer, but the global opentracing.Tracer may not be one.
// That's why it's relying on the globalTracer: even if the opentracing.GlobalTracer() has been overridden,
// the IDs generated by baseplate will still conform to the specified configuration.
traceID = globalTracer.newTraceID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're not using globalTracer as a tracer, can we shield it somehow so that it only exposes the methods we want to be used on it, so it can't accidentally get missed again?

spanID = globalTracer.newSpanID()
redloaf marked this conversation as resolved.
Show resolved Hide resolved
} 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 +118,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.internalTracer(); tracer != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if tracer := t.internalTracer(); tracer != nil {
if tracer, ok := t.internalTracer(); ok {

(see comment above about returning a bool too)

endpoint = tracer.endpoint
}

if t.timeAnnotationReceiveKey != "" {
Expand Down Expand Up @@ -159,7 +186,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.internalTracer(); tracer != nil {
return tracer.Record(ctx, t.toZipkinSpan())
}
return nil
}

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