-
Notifications
You must be signed in to change notification settings - Fork 1
/
jaeger.go
144 lines (118 loc) · 3.61 KB
/
jaeger.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"net"
"context"
"fmt"
"github.com/d7561985/opentracefasthttp"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/stretchr/testify/assert"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"io"
"time"
)
//https://blog.csdn.net/liyunlong41/article/details/87932953
func initJaeger(service string) (opentracing.Tracer, io.Closer) {
cfg := &config.Configuration{
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: false,
LocalAgentHostPort: "172.16.1.216:6831", //http://172.16.1.216/
},
}
tracer, closer, err := cfg.New(service, config.Logger(jaeger.StdLogger))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
return tracer, closer
}
func foo3(req string, ctx context.Context) (reply string) {
//1.创建子span
span, _ := opentracing.StartSpanFromContext(ctx, "span_foo3")
defer func() {
//4.接口调用完,在tag中设置request和reply
span.SetTag("request", req)
span.SetTag("reply", reply)
span.Finish()
}()
println(req)
//2.模拟处理耗时
time.Sleep(time.Second / 2)
//3.返回reply
reply = "foo3Reply"
return
}
//跟foo3一样逻辑
func foo4(req string, ctx context.Context) (reply string) {
span, _ := opentracing.StartSpanFromContext(ctx, "span_foo4")
defer func() {
span.SetTag("request", req)
span.SetTag("reply", reply)
span.Finish()
}()
println(req)
time.Sleep(time.Second / 2)
reply = "foo4Reply"
return
}
func main2() {
tracer, closer := initJaeger("jaeger-demo")
defer closer.Close()
opentracing.SetGlobalTracer(tracer) //StartspanFromContext创建新span时会用到
span := tracer.StartSpan("span_root")
ctx := opentracing.ContextWithSpan(context.Background(), span)
r1 := foo3("Hello foo3", ctx)
r2 := foo4("Hello foo4", ctx)
fmt.Println(r1, r2)
span.Finish()
}
func main1() {
main2()
var t assert.TestingT
//tracer, closer := jaeger.NewTracer("fasthttp-carrier-tester", jaeger.NewConstSampler(true), jaeger.NewNullReporter())
tracer, closer := initJaeger("fasthttp-carrier-tester")
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
ok := false
ln := fasthttputil.NewInmemoryListener()
defer ln.Close() //nolint
srv := fasthttp.Server{Handler: func(ctx *fasthttp.RequestCtx) {
carrier := opentracefasthttp.New(&ctx.Request.Header)
clientContext, err := tracer.Extract(opentracing.HTTPHeaders, carrier)
fmt.Println(ctx.Request.Header.String())
assert.NoError(t, err)
span := tracer.StartSpan("HTTP "+string(ctx.Method())+" "+ctx.Request.URI().String(), ext.RPCServerOption(clientContext))
fmt.Println("3", span)
assert.NotNil(t, span)
span.LogFields(log.String("server", "request ok"))
fmt.Println("4", span)
defer span.Finish()
ok = true
}}
go srv.Serve(ln) //nolint
span := opentracing.GlobalTracer().StartSpan("client-request")
defer span.Finish()
fmt.Println("1", span)
span.SetTag("test", "test")
span.LogFields(log.String("test", "test"))
fmt.Println("2", span)
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodGet)
req.SetRequestURI("http://example.com")
carrier := opentracefasthttp.New(&req.Header)
err := opentracing.GlobalTracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier)
assert.NoError(t, err)
client := fasthttp.Client{Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
}}
err = client.Do(req, nil)
assert.NoError(t, err)
assert.True(t, ok)
}