Skip to content

Commit

Permalink
Add options to set tracer tags (jaegertracing#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurishkuro authored Jul 5, 2017
1 parent 1dfa107 commit 9279b8e
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 41 deletions.
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (c Configuration) New(
tracerOptions = append(tracerOptions, jaeger.TracerOptions.Observer(obs))
}

for _, tag := range opts.tags {
tracerOptions = append(tracerOptions, jaeger.TracerOptions.Tag(tag.Key, tag.Value))
}

tracer, closer := jaeger.NewTracer(
serviceName,
sampler,
Expand Down
9 changes: 9 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package config

import (
opentracing "github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-lib/metrics"

"github.com/uber/jaeger-client-go"
Expand All @@ -36,6 +37,7 @@ type Options struct {
reporter jaeger.Reporter
observers []jaeger.Observer
zipkinSharedRPCSpan bool
tags []opentracing.Tag
}

// Metrics creates an Option that initializes Metrics in the tracer,
Expand Down Expand Up @@ -78,6 +80,13 @@ func ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) Option {
}
}

// Tag creates an option that adds a tracer-level tag.
func Tag(key string, value interface{}) Option {
return func(c *Options) {
c.tags = append(c.tags, opentracing.Tag{Key: key, Value: value})
}
}

func applyOptions(options ...Option) Options {
opts := Options{}
for _, option := range options {
Expand Down
9 changes: 9 additions & 0 deletions config/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-lib/metrics"

"github.com/uber/jaeger-client-go"
Expand All @@ -45,6 +46,14 @@ func TestApplyOptions(t *testing.T) {
assert.True(t, opts.zipkinSharedRPCSpan)
}

func TestTraceTagOption(t *testing.T) {
c := Configuration{}
tracer, closer, err := c.New("test-service", Tag("tag-key", "tag-value"))
require.NoError(t, err)
defer closer.Close()
assert.Equal(t, opentracing.Tag{Key: "tag-key", Value: "tag-value"}, tracer.(*jaeger.Tracer).Tags()[0])
}

func TestApplyOptionsDefaults(t *testing.T) {
opts := applyOptions()
assert.Equal(t, jaeger.NullLogger, opts.logger)
Expand Down
2 changes: 1 addition & 1 deletion interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type formatKey int
const SpanContextFormat formatKey = iota

type jaegerTraceContextPropagator struct {
tracer *tracer
tracer *Tracer
}

func (p *jaegerTraceContextPropagator) Inject(
Expand Down
2 changes: 1 addition & 1 deletion jaeger_thrift_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func BuildJaegerProcessThrift(span *Span) *j.Process {
return buildJaegerProcessThrift(span.tracer)
}

func buildJaegerProcessThrift(tracer *tracer) *j.Process {
func buildJaegerProcessThrift(tracer *Tracer) *j.Process {
process := &j.Process{
ServiceName: tracer.serviceName,
Tags: buildTags(tracer.tags),
Expand Down
10 changes: 5 additions & 5 deletions propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ type Extractor interface {
}

type textMapPropagator struct {
tracer *tracer
tracer *Tracer
encodeValue func(string) string
decodeValue func(string) string
}

func newTextMapPropagator(tracer *tracer) *textMapPropagator {
func newTextMapPropagator(tracer *Tracer) *textMapPropagator {
return &textMapPropagator{
tracer: tracer,
encodeValue: func(val string) string {
Expand All @@ -75,7 +75,7 @@ func newTextMapPropagator(tracer *tracer) *textMapPropagator {
}
}

func newHTTPHeaderPropagator(tracer *tracer) *textMapPropagator {
func newHTTPHeaderPropagator(tracer *Tracer) *textMapPropagator {
return &textMapPropagator{
tracer: tracer,
encodeValue: func(val string) string {
Expand All @@ -92,11 +92,11 @@ func newHTTPHeaderPropagator(tracer *tracer) *textMapPropagator {
}

type binaryPropagator struct {
tracer *tracer
tracer *Tracer
buffers sync.Pool
}

func newBinaryPropagator(tracer *tracer) *binaryPropagator {
func newBinaryPropagator(tracer *Tracer) *binaryPropagator {
return &binaryPropagator{
tracer: tracer,
buffers: sync.Pool{New: func() interface{} { return &bytes.Buffer{} }},
Expand Down
5 changes: 3 additions & 2 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
type Span struct {
sync.RWMutex

tracer *tracer
tracer *Tracer

context SpanContext

Expand Down Expand Up @@ -66,7 +66,8 @@ type Span struct {
observer SpanObserver
}

// Tag is a simple key value wrapper
// Tag is a simple key value wrapper.
// TODO deprecate in the next major release, use opentracing.Tag instead.
type Tag struct {
key string
value interface{}
Expand Down
33 changes: 22 additions & 11 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import (
"github.com/uber/jaeger-client-go/utils"
)

type tracer struct {
// Tracer implements opentracing.Tracer.
type Tracer struct {
serviceName string
hostIPv4 uint32 // this is for zipkin endpoint conversion

Expand Down Expand Up @@ -73,7 +74,7 @@ func NewTracer(
reporter Reporter,
options ...TracerOption,
) (opentracing.Tracer, io.Closer) {
t := &tracer{
t := &Tracer{
serviceName: serviceName,
sampler: sampler,
reporter: reporter,
Expand Down Expand Up @@ -138,7 +139,8 @@ func NewTracer(
return t, t
}

func (t *tracer) StartSpan(
// StartSpan implements StartSpan() method of opentracing.Tracer.
func (t *Tracer) StartSpan(
operationName string,
options ...opentracing.StartSpanOption,
) opentracing.Span {
Expand All @@ -149,7 +151,7 @@ func (t *tracer) StartSpan(
return t.startSpanWithOptions(operationName, sso)
}

func (t *tracer) startSpanWithOptions(
func (t *Tracer) startSpanWithOptions(
operationName string,
options opentracing.StartSpanOptions,
) opentracing.Span {
Expand Down Expand Up @@ -245,7 +247,7 @@ func (t *tracer) startSpanWithOptions(
}

// Inject implements Inject() method of opentracing.Tracer
func (t *tracer) Inject(ctx opentracing.SpanContext, format interface{}, carrier interface{}) error {
func (t *Tracer) Inject(ctx opentracing.SpanContext, format interface{}, carrier interface{}) error {
c, ok := ctx.(SpanContext)
if !ok {
return opentracing.ErrInvalidSpanContext
Expand All @@ -257,7 +259,7 @@ func (t *tracer) Inject(ctx opentracing.SpanContext, format interface{}, carrier
}

// Extract implements Extract() method of opentracing.Tracer
func (t *tracer) Extract(
func (t *Tracer) Extract(
format interface{},
carrier interface{},
) (opentracing.SpanContext, error) {
Expand All @@ -268,15 +270,24 @@ func (t *tracer) Extract(
}

// Close releases all resources used by the Tracer and flushes any remaining buffered spans.
func (t *tracer) Close() error {
func (t *Tracer) Close() error {
t.reporter.Close()
t.sampler.Close()
return nil
}

// Tags returns a slice of tracer-level tags.
func (t *Tracer) Tags() []opentracing.Tag {
tags := make([]opentracing.Tag, len(t.tags))
for i, tag := range t.tags {
tags[i] = opentracing.Tag{Key: tag.key, Value: tag.value}
}
return tags
}

// newSpan returns an instance of a clean Span object.
// If options.PoolSpans is true, the spans are retrieved from an object pool.
func (t *tracer) newSpan() *Span {
func (t *Tracer) newSpan() *Span {
if !t.options.poolSpans {
return &Span{}
}
Expand All @@ -288,7 +299,7 @@ func (t *tracer) newSpan() *Span {
return sp
}

func (t *tracer) startSpanInternal(
func (t *Tracer) startSpanInternal(
sp *Span,
operationName string,
startTime time.Time,
Expand Down Expand Up @@ -339,7 +350,7 @@ func (t *tracer) startSpanInternal(
return sp
}

func (t *tracer) reportSpan(sp *Span) {
func (t *Tracer) reportSpan(sp *Span) {
t.metrics.SpansFinished.Inc(1)
if sp.context.IsSampled() {
t.reporter.Report(sp)
Expand All @@ -351,7 +362,7 @@ func (t *tracer) reportSpan(sp *Span) {

// randomID generates a random trace/span ID, using tracer.random() generator.
// It never returns 0.
func (t *tracer) randomID() uint64 {
func (t *Tracer) randomID() uint64 {
val := t.randomNumber()
for val == 0 {
val = t.randomNumber()
Expand Down
28 changes: 17 additions & 11 deletions tracer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// TracerOption is a function that sets some option on the tracer
type TracerOption func(tracer *tracer)
type TracerOption func(tracer *Tracer)

// TracerOptions is a factory for all available TracerOption's
var TracerOptions tracerOptions
Expand All @@ -35,30 +35,30 @@ type tracerOptions struct{}
// Metrics creates a TracerOption that initializes Metrics on the tracer,
// which is used to emit statistics.
func (tracerOptions) Metrics(m *Metrics) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.metrics = *m
}
}

// Logger creates a TracerOption that gives the tracer a Logger.
func (tracerOptions) Logger(logger Logger) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.logger = logger
}
}

// TimeNow creates a TracerOption that gives the tracer a function
// used to generate timestamps for spans.
func (tracerOptions) TimeNow(timeNow func() time.Time) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.timeNow = timeNow
}
}

// RandomNumber creates a TracerOption that gives the tracer
// a thread-safe random number generator function for generating trace IDs.
func (tracerOptions) RandomNumber(randomNumber func() uint64) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.randomNumber = randomNumber
}
}
Expand All @@ -68,7 +68,7 @@ func (tracerOptions) RandomNumber(randomNumber func() uint64) TracerOption {
// This should be used with care, only if the service is not running any async tasks
// that can access parent spans after those spans have been finished.
func (tracerOptions) PoolSpans(poolSpans bool) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.options.poolSpans = poolSpans
}
}
Expand All @@ -77,31 +77,37 @@ func (tracerOptions) PoolSpans(poolSpans bool) TracerOption {
// If not set, the factory method will obtain the current IP address.
// The TracerOption is deprecated; the tracer will attempt to automatically detect the IP.
func (tracerOptions) HostIPv4(hostIPv4 uint32) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.hostIPv4 = hostIPv4
}
}

func (tracerOptions) Injector(format interface{}, injector Injector) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.injectors[format] = injector
}
}

func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.extractors[format] = extractor
}
}

func (tracerOptions) Observer(observer Observer) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.observer.append(observer)
}
}

func (tracerOptions) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption {
return func(tracer *tracer) {
return func(tracer *Tracer) {
tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan
}
}

func (tracerOptions) Tag(key string, value interface{}) TracerOption {
return func(tracer *Tracer) {
tracer.tags = append(tracer.tags, Tag{key: key, value: value})
}
}
Loading

0 comments on commit 9279b8e

Please sign in to comment.