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

  • 相关阅读:
    你拿什么保护我的版权(写在中移动MM商用之前)
    体验Windows Mobile 6.5 (模拟器)
    Mobile Market试用后感受
    制作Windows Mobile程序安装包
    自定义.NET CF控件,美化Windows Mobile程序界面
    记《虞美人盛开的山坡》
    剑风传奇 黄金时代篇1:霸王之卵
    发现IGame中又一个大坑
    【翻译】【西川善司】3D图形的概念和渲染管线(5回完)
    【翻译】西川善司为了3D游戏粉丝的[生化危机5]图形讲座(后篇)
  • 原文地址:https://www.cnblogs.com/cqvoip/p/9969033.html
Copyright © 2020-2023  润新知