-
Notifications
You must be signed in to change notification settings - Fork 9
/
tracing.go
102 lines (87 loc) · 2.58 KB
/
tracing.go
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
// Utilities for working with tracing.
// dependencies:
// go get github.com/opentracing/opentracing-go
// go get github.com/uber/jaeger-client-go
package ginopentracing
import (
"fmt"
"io"
jaegerprom "github.com/uber/jaeger-lib/metrics/prometheus"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
jaeger "github.com/uber/jaeger-client-go"
)
// LogrusAdapter - an adapter to log span info
type LogrusAdapter struct {
InfoLevel bool
}
// Error - logrus adapter for span errors
func (l LogrusAdapter) Error(msg string) {
logrus.Error(msg)
}
// Infof - logrus adapter for span info logging
func (l LogrusAdapter) Infof(msg string, args ...interface{}) {
if l.InfoLevel {
logrus.Infof(msg, args...)
}
}
// Option - define options for NewJWTCache()
type Option func(*options)
type options struct {
sampleProbability float64
enableInfoLog bool
}
// defaultOptions - some defs options to NewJWTCache()
var defaultOptions = options{
sampleProbability: 0.0,
enableInfoLog: false,
}
// WithSampleProbability - optional sample probability
func WithSampleProbability(sampleProbability float64) Option {
return func(o *options) {
o.sampleProbability = sampleProbability
}
}
// WithEnableInfoLog - optional: enable Info logging for tracing
func WithEnableInfoLog(enable bool) Option {
return func(o *options) {
o.enableInfoLog = enable
}
}
// InitTracing - init opentracing with options (WithSampleProbability, WithEnableInfoLog) defaults: constant sampling, no info logging
func InitTracing(serviceName string, tracingAgentHostPort string, opt ...Option) (
tracer opentracing.Tracer,
reporter jaeger.Reporter,
closer io.Closer,
err error) {
opts := defaultOptions
for _, o := range opt {
o(&opts)
}
factory := jaegerprom.New()
metrics := jaeger.NewMetrics(factory, map[string]string{"lib": "jaeger"})
transport, err := jaeger.NewUDPTransport(tracingAgentHostPort, 0)
if err != nil {
return tracer, reporter, closer, err
}
logAdapt := LogrusAdapter{InfoLevel: opts.enableInfoLog}
reporter = jaeger.NewCompositeReporter(
jaeger.NewLoggingReporter(logAdapt),
jaeger.NewRemoteReporter(transport,
jaeger.ReporterOptions.Metrics(metrics),
jaeger.ReporterOptions.Logger(logAdapt),
),
)
var sampler jaeger.Sampler
sampler = jaeger.NewConstSampler(true)
if opts.sampleProbability > 0 {
fmt.Println("probable")
sampler, err = jaeger.NewProbabilisticSampler(opts.sampleProbability)
}
tracer, closer = jaeger.NewTracer(serviceName,
sampler,
reporter,
jaeger.TracerOptions.Metrics(metrics),
)
return tracer, reporter, closer, nil
}