-
Notifications
You must be signed in to change notification settings - Fork 79
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
base: master
Are you sure you want to change the base?
Changes from all commits
3f88945
b119a3f
cf129e9
da055b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,8 @@ import ( | |||||
"fmt" | ||||||
"time" | ||||||
|
||||||
"github.com/opentracing/opentracing-go" | ||||||
|
||||||
"github.com/reddit/baseplate.go/randbp" | ||||||
"github.com/reddit/baseplate.go/timebp" | ||||||
) | ||||||
|
@@ -31,7 +33,7 @@ const ( | |||||
) | ||||||
|
||||||
type trace struct { | ||||||
tracer *Tracer | ||||||
tracer opentracing.Tracer | ||||||
|
||||||
name string | ||||||
traceID string | ||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't return |
||||||
} | ||||||
|
||||||
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() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||||||
|
@@ -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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(see comment above about returning a bool too) |
||||||
endpoint = tracer.endpoint | ||||||
} | ||||||
|
||||||
if t.timeAnnotationReceiveKey != "" { | ||||||
|
@@ -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. | ||||||
|
There was a problem hiding this comment.
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? 🤔