/
factory.go
86 lines (81 loc) · 3.18 KB
/
factory.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
package starterJaeger
import (
"github.com/go-spring/spring-base/log"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegerCfg "github.com/uber/jaeger-client-go/config"
jaegerMetric "github.com/uber/jaeger-lib/metrics"
"os"
"os/signal"
"syscall"
)
func newConfig(config JaegerConfig) *jaegerCfg.Configuration {
cfg := &jaegerCfg.Configuration{
ServiceName: config.ServiceName,
Disabled: config.Disabled,
RPCMetrics: config.RPCMetrics,
Gen128Bit: config.Gen128Bit,
Tags: config.Tags,
Sampler: &jaegerCfg.SamplerConfig{
Type: config.Sampler.Type,
Param: config.Sampler.Param,
SamplingServerURL: config.Sampler.SamplingServerURL,
SamplingRefreshInterval: config.Sampler.SamplingRefreshInterval,
MaxOperations: config.Sampler.MaxOperations,
OperationNameLateBinding: config.Sampler.OperationNameLateBinding,
Options: config.Sampler.Options,
},
Reporter: &jaegerCfg.ReporterConfig{
QueueSize: config.Reporter.QueueSize,
BufferFlushInterval: config.Reporter.BufferFlushInterval,
LogSpans: config.Reporter.LogSpans,
LocalAgentHostPort: config.Reporter.LocalAgentHostPort,
DisableAttemptReconnecting: config.Reporter.DisableAttemptReconnecting,
AttemptReconnectInterval: config.Reporter.AttemptReconnectInterval,
CollectorEndpoint: config.Reporter.CollectorEndpoint,
User: config.Reporter.User,
Password: config.Reporter.Password,
HTTPHeaders: config.Reporter.HTTPHeaders,
},
Headers: &jaeger.HeadersConfig{
JaegerDebugHeader: config.Headers.JaegerDebugHeader,
JaegerBaggageHeader: config.Headers.JaegerBaggageHeader,
TraceContextHeaderName: config.Headers.TraceContextHeaderName,
TraceBaggageHeaderPrefix: config.Headers.TraceBaggageHeaderPrefix,
},
}
if len(config.BaggageRestrictions.HostPort) > 0 {
cfg.BaggageRestrictions = &jaegerCfg.BaggageRestrictionsConfig{
DenyBaggageOnInitializationFailure: config.BaggageRestrictions.DenyBaggageOnInitializationFailure,
HostPort: config.BaggageRestrictions.HostPort,
RefreshInterval: config.BaggageRestrictions.RefreshInterval,
}
}
if len(config.Throttler.HostPort) > 0 {
cfg.Throttler = &jaegerCfg.ThrottlerConfig{
HostPort: config.Throttler.HostPort,
RefreshInterval: config.Throttler.RefreshInterval,
SynchronousInitialization: config.Throttler.SynchronousInitialization,
}
}
return cfg
}
func NewClient(config JaegerConfig) (opentracing.Tracer, error) {
cfg := newConfig(config)
jLogger := jaeger.StdLogger
jMetricsFactory := jaegerMetric.NullFactory
tracer, cls, err := cfg.NewTracer(jaegerCfg.Logger(jLogger), jaegerCfg.Metrics(jMetricsFactory))
if err != nil {
panic(err)
}
opentracing.SetGlobalTracer(tracer)
// 监听信号量
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-c // 阻塞等待
log.Info("jaeger tracer will exit")
_ = cls.Close() // 关闭
}()
return tracer, nil
}