Golang微服务:Micro Trace使用opentracing jaeger

trace

Micro通过Wrapper实现了三种trace接口,aswxray,opencensus,opentracing,这里主要关注opentracing,opentracing已成为行业标准。

opentracing

要引入OpenTracing方案,只需要opentracing.NewClientWrapper加入Client或者Server即可。

func NewClientWrapper(ot opentracing.Tracer) client.Wrapper {
return func(c client.Client) client.Client {
return &otWrapper{ot, c}
}
}

opentracing.NewClientWrapper方法入参是实现OpenTracing的接口opentracing.Tracer

type Tracer interface {
StartSpan(operationName string, opts ...StartSpanOption) Span
Inject(sm SpanContext, format interface{}, carrier interface{}) error
Extract(format interface{}, carrier interface{}) (SpanContext, error)
}

jaeger

部署一个测试的jaeger,服务器地址:192.168.8.144,Agent的默认端口是5775。

/home/service/base/jaeger
[root@localhost jaeger]# ll
total 158444
-rwxr-xr-x 1 root root 14565068 Nov 16 19:35 example-hotrod
-rwxr-xr-x 1 root root 20387089 Nov 16 19:35 jaeger-agent
-rwxr-xr-x 1 root root 38456594 Nov 16 19:35 jaeger-all-in-one
-rwxr-xr-x 1 root root 29731462 Nov 16 19:35 jaeger-collector
-rwxr-xr-x 1 root root 25523281 Nov 16 19:35 jaeger-ingester
-rwxr-xr-x 1 root root 33563911 Nov 16 19:35 jaeger-query
-rwxr-xr-x 1 root root 43 Nov 16 19:36 run
-rw-r--r-- 1 root root 1939 Nov 16 19:36 x.log
[root@localhost jaeger]# cat run
nohup ./jaeger-all-in-one >> x.log 2>&1 &

jaeger是一款分布式的追踪系统,实现了opentracingAPI标准,也是Go生态的,是CNCF成员。

func TestJaeger(t *testing.T) {
cfg := config.Configuration{
ServiceName: "MicroTestService",//自定义服务名称
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
BufferFlushInterval: 1 * time.Second,
LocalAgentHostPort: "192.168.8.144:5775",//jaeger agent
},
}
tracer, closer, err := cfg.NewTracer()
if err != nil {
t.Error(err)
return
}
defer closer.Close() r := mock.NewRegistry()
s := selector.NewSelector(selector.Registry(r)) c := client.NewClient(
// set the selector
client.Selector(s),
// add the trace wrapper
client.Wrap(NewClientWrapper(tracer)),
) req := c.NewRequest("test.service", "Test.Method", map[string]string{
"foo": "bar",
}, client.WithContentType("application/json")) var rsp map[string]interface{}
c.Call(context.TODO(), req, rsp)
t.Log(rsp)
}

浏览器访问:http://192.168.8.144:16686/search可以发现有服务MicroTestService的访问追踪了

上一篇:从OneNote走出,技术博客养成记


下一篇:jquery重置表单