-
Notifications
You must be signed in to change notification settings - Fork 14
/
tracing.go
72 lines (61 loc) · 1.93 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
// Package tracing provides func to initialize opentracing tracer using jaeger client
package tracing
import (
"fmt"
"io"
"net"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegerconfig "github.com/uber/jaeger-client-go/config"
jaegerzap "github.com/uber/jaeger-client-go/log/zap"
"github.com/uber/jaeger-lib/metrics/prometheus"
"go.uber.org/zap"
)
var (
// Tracer is used for creating spans for distributed tracing
tracer opentracing.Tracer
// Closer holds an instance to the RequestTracing object's Closer.
closer io.Closer
)
// Config ... struct expected by Init func to initialize jaeger tracing client.
type Config struct {
LogSpans bool // when set to true, reporter logs all submitted spans
Host string // jaeger-agent UDP binary thrift protocol endpoint
Port string // jaeger-agent UDP binary thrift protocol server port
ServiceName string // name of this service used by tracer.
Disabled bool // to mock tracer
}
// Init initialises opentracing tracer. Returns tracer for tracing spans &
// closer for flushing in-memory spans before app shutdown.
func Init(env string, cnf Config, zlog *zap.Logger) error {
hostPortPath := net.JoinHostPort(cnf.Host, cnf.Port)
config := &jaegerconfig.Configuration{
ServiceName: fmt.Sprintf("%s-%s", cnf.ServiceName, env),
Sampler: &jaegerconfig.SamplerConfig{
Type: jaeger.SamplerTypeProbabilistic,
Param: 0.05,
},
Reporter: &jaegerconfig.ReporterConfig{
LogSpans: cnf.LogSpans,
LocalAgentHostPort: hostPortPath,
},
Disabled: cnf.Disabled,
}
var err error
tracer, closer, err = config.NewTracer(
jaegerconfig.Logger(jaegerzap.NewLogger(zlog)),
jaegerconfig.Metrics(prometheus.New()),
)
if err != nil {
return err
}
opentracing.SetGlobalTracer(tracer)
return nil
}
// Close calls the closer function if initialized
func Close() error {
if closer == nil {
return nil
}
return closer.Close()
}