-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgjaeger.go
121 lines (108 loc) · 3.03 KB
/
rgjaeger.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package rgjaeger
import (
"errors"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"io"
"net/http"
"time"
"github.com/jackylee92/rgo/core/rgconfig"
"github.com/jackylee92/rgo/core/rgglobal"
"github.com/jackylee92/rgo/core/rgglobal/rgconst"
"github.com/jackylee92/rgo/core/rgglobal/rgerror"
"github.com/jackylee92/rgo/core/rglog"
jaegercfg "github.com/uber/jaeger-client-go/config"
// jaegerlog "github.com/uber/jaeger-client-go/log"
)
// 是否开启
var jaegerStatus bool
func JaegerStatus() bool {
return jaegerStatus
}
func SetJaegerStatus(status bool) {
if status {
rglog.SystemInfo("启动项【jaeger_status】| 开启")
}
jaegerStatus = status
}
type Client struct {
ctx opentracing.SpanContext
tracer opentracing.Tracer
}
// 从上下文中解析并创建一个新的 trace,获得传播的 上下文(SpanContext)
func GetTracer(header http.Header) (opentracing.Tracer, opentracing.SpanContext, io.Closer, error) {
tracer, closer, err := createTracer()
if err != nil {
return nil, nil, nil, err
}
// 继承别的进程传递过来的上下文
spanContext, _ := tracer.Extract(opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(header))
return tracer, spanContext, closer, err
}
// 创建一个Tracer
func createTracer() (opentracing.Tracer, io.Closer, error) {
appName := rgglobal.AppName
jaegerHost := rgconfig.GetStr(rgconst.ConfigKeyJaegerHost)
if jaegerHost == "" {
return nil, nil, errors.New(rgerror.ErrorJaegerHostNil)
}
if appName == "" {
return nil, nil, errors.New(rgerror.ErrorAppNameNil)
}
var cfg = jaegercfg.Configuration{
ServiceName: appName,
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
CollectorEndpoint: jaegerHost,
},
}
// jLogger := jaegerlog.StdLogger
tracer, closer, err := cfg.NewTracer(
// jaegercfg.Logger(jLogger),
jaegercfg.MaxTagValueLength(65535),
)
return tracer, closer, err
}
func New(ctx opentracing.SpanContext, tracer opentracing.Tracer) *Client {
if !jaegerStatus || ctx == nil || tracer == nil {
return nil
} else {
return &Client{
ctx: ctx,
tracer: tracer,
}
}
}
func (c *Client) Reset(ctx opentracing.SpanContext, tracer opentracing.Tracer) {
c.ctx = ctx
c.tracer = tracer
return
}
func (c *Client) Send(action string, param map[string]interface{}, parentSpanInterface interface{}) opentracing.SpanContext {
if !jaegerStatus {
return nil
}
param["time"] = time.Now().Format(rgconst.GoTimeFormat)
param["time.nano"] = time.Now().UnixNano()
param["local.ip"] = rgglobal.LocalIp
if len(param) == 0 {
return nil
}
tags := opentracing.Tags{}
for key, value := range param {
tags[key] = value
}
span := opentracing.ChildOf(c.ctx)
parentSpan, ok := parentSpanInterface.(opentracing.SpanContext)
if parentSpan != nil && ok {
span = opentracing.ChildOf(parentSpan)
}
childSpan := c.tracer.StartSpan(action, span, tags)
defer childSpan.Finish() // 可手动调用 Finish()
return childSpan.Context()
}