File: tracer_options.go

package info (click to toggle)
golang-github-openzipkin-zipkin-go 0.1.5%2Bgit20190103.2fd7f4a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 532 kB
  • sloc: makefile: 22
file content (124 lines) | stat: -rw-r--r-- 3,194 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package zipkin

import (
	"errors"

	"github.com/openzipkin/zipkin-go/idgenerator"
	"github.com/openzipkin/zipkin-go/model"
)

// Tracer Option Errors
var (
	ErrInvalidEndpoint             = errors.New("requires valid local endpoint")
	ErrInvalidExtractFailurePolicy = errors.New("invalid extract failure policy provided")
)

// ExtractFailurePolicy deals with Extraction errors
type ExtractFailurePolicy int

// ExtractFailurePolicyOptions
const (
	ExtractFailurePolicyRestart ExtractFailurePolicy = iota
	ExtractFailurePolicyError
	ExtractFailurePolicyTagAndRestart
)

// TracerOption allows for functional options to adjust behavior of the Tracer
// to be created with NewTracer().
type TracerOption func(o *Tracer) error

// WithLocalEndpoint sets the local endpoint of the tracer.
func WithLocalEndpoint(e *model.Endpoint) TracerOption {
	return func(o *Tracer) error {
		if e == nil {
			o.localEndpoint = nil
			return nil
		}
		ep := *e
		o.localEndpoint = &ep
		return nil
	}
}

// WithExtractFailurePolicy allows one to set the ExtractFailurePolicy.
func WithExtractFailurePolicy(p ExtractFailurePolicy) TracerOption {
	return func(o *Tracer) error {
		if p < 0 || p > ExtractFailurePolicyTagAndRestart {
			return ErrInvalidExtractFailurePolicy
		}
		o.extractFailurePolicy = p
		return nil
	}
}

// WithNoopSpan if set to true will switch to a NoopSpan implementation
// if the trace is not sampled.
func WithNoopSpan(unsampledNoop bool) TracerOption {
	return func(o *Tracer) error {
		o.unsampledNoop = unsampledNoop
		return nil
	}
}

// WithSharedSpans allows to place client-side and server-side annotations
// for a RPC call in the same span (Zipkin V1 behavior) or different spans
// (more in line with other tracing solutions). By default this Tracer
// uses shared host spans (so client-side and server-side in the same span).
func WithSharedSpans(val bool) TracerOption {
	return func(o *Tracer) error {
		o.sharedSpans = val
		return nil
	}
}

// WithSampler allows one to set a Sampler function
func WithSampler(sampler Sampler) TracerOption {
	return func(o *Tracer) error {
		o.sampler = sampler
		return nil
	}
}

// WithTraceID128Bit if set to true will instruct the Tracer to start traces
// with 128 bit TraceID's. If set to false the Tracer will start traces with
// 64 bits.
func WithTraceID128Bit(val bool) TracerOption {
	return func(o *Tracer) error {
		if val {
			o.generate = idgenerator.NewRandom128()
		} else {
			o.generate = idgenerator.NewRandom64()
		}
		return nil
	}
}

// WithIDGenerator allows one to set a custom ID Generator
func WithIDGenerator(generator idgenerator.IDGenerator) TracerOption {
	return func(o *Tracer) error {
		o.generate = generator
		return nil
	}
}

// WithTags allows one to set default tags to be added to each created span
func WithTags(tags map[string]string) TracerOption {
	return func(o *Tracer) error {
		for k, v := range tags {
			o.defaultTags[k] = v
		}
		return nil
	}
}

// WithNoopTracer allows one to start the Tracer as Noop implementation.
func WithNoopTracer(tracerNoop bool) TracerOption {
	return func(o *Tracer) error {
		if tracerNoop {
			o.noop = 1
		} else {
			o.noop = 0
		}
		return nil
	}
}