Giter Club home page Giter Club logo

tracing's Introduction

tracing

功能

  • 提供http.Handler函数,OpenTracingHandler可以很方便集成到http中间件.
  • 提供grpc.UnaryClientInterceptor函数,一元RPC客户端拦截器OpenTracingClientInterceptor
  • 提供grpc.UnaryServerInterceptor函数,一元RPC服务端拦截器OpenTracingServerInterceptor.
  • 函数GetSpanFromContextcontext中获取opentracing.Span对象,主要适用于http.Handlergrpc.UnaryServerInterceptor的context,然后可以根据需要调用SetTagLog来设置相关的信息.
  • 函数ChildOfSpanFromContextcontext中生成ClildOfopentracing.Span对象,用于跟踪某些子操作过程.
  • 函数FollowsSpanFromContextcontext中生成FollewsFromopentracing.Span对象,用于跟踪某些操作过程.

例子

创建好Tracer对象之后,一定要调用opentracing.SetGlobalTracer(tracer),把tracer对象注册到opentracing的GlobalTracer中.

连接zipkin

func main() {
	// 采用HTTP方式来传输.
	zipkinReporter := zipkinhttp.NewReporter("http://127.0.0.1:9411/api/v2/spans")
	// 本地端点.
	endpoint, err := zipkin.NewEndpoint("goods", "127.0.0.1:8888")
	if err != nil {
		fmt.Println(err)

		return
	}

	// 创建zipkin的tracer对象.
	nativeTracer, err := zipkin.NewTracer(zipkinReporter, zipkin.WithLocalEndpoint(endpoint))
	if err != nil {
		fmt.Println(err)

		return
	}

	// 包装为opentracing规范的tracer对象.
	tracer := zipkinot.Wrap(nativeTracer)

	// 注册为opentracing里的GlobalTracer对象.
	opentracing.SetGlobalTracer(tracer)

	// 业务代码.
}

连接jaeger

func main() {
	cfg := jaegercfg.Configuration{
		ServiceName: "goods",
		// 采样策略,这里使用Const,全部采样.
		Sampler: &jaegercfg.SamplerConfig{
			Type:  "const",
			Param: 1.0,
		},
		Reporter: &jaegercfg.ReporterConfig{
			BufferFlushInterval: time.Second,
			LocalAgentHostPort:  "192.168.20.153:6831", // 采用UDP协议连接Agent.
			//CollectorEndpoint:   "http://192.168.20.153:14268/api/traces", // 采用HTTP协议直连Collector
		},
	}

	// 根据配置生成Tracer对象,启用Span的内存池.
	tracer, closer, err := cfg.NewTracer(jaegercfg.PoolSpans(true))
	if err != nil {
		fmt.Println(err)

		return
	}

	// 调用Close,释放资源.
	defer closer.Close()

	// 注册为opentracing里的GlobalTracer对象.
	opentracing.SetGlobalTracer(tracer)

	// 业务代码.
}

tracing's People

Contributors

shenbaise9527 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.