码迷,mamicode.com
首页 > 其他好文 > 详细

Golang微服务:Micro Trace使用opentracing jaeger

时间:2018-11-16 15:03:20      阅读:1160      评论:0      收藏:0      [点我收藏+]

标签:extract   base   logs   inject   port   部署   标准   ret   fun   

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的访问追踪了

Golang微服务:Micro Trace使用opentracing jaeger

标签:extract   base   logs   inject   port   部署   标准   ret   fun   

原文地址:https://www.cnblogs.com/cqvoip/p/9969033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!