前提条件
完成vpce接入。
接入步骤
- 引入go依赖包。
import (
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/transport"
)
- 查看接入点信息。
应用列表的接入指引会根据您所在资源池提供“通过 HTTP 上报数据”的ENDPOINT(天翼云vpc网络接入点)、鉴权TOKEN信息。
- 初始化Jaeger SDK。
注意需将token和endpoint替换成相应地域的接入点信息。
func NewJaegerTracer(service string) opentracing.Tracer {
auth := map[string]string{
"x-ctg-authorization":"<token>", //上面获取到的token
}
sender := transport.NewHTTPTransport(
"<endpoint>", //jaeger 上报的http地址
transport.HTTPHeaders(auth),
)
tracer, _ := jaeger.NewTracer(service,
jaeger.NewConstSampler(true),
jaeger.NewRemoteReporter(sender, jaeger.ReporterOptions.Logger(jaeger.StdLogger)),)
return tracer
}
- client 端上报demo。
func runClient(tracer opentracing.Tracer, httpUrl string, spanName string, r *http.Request) {
c := &http.Client{Transport: &nethttp.Transport{}}
span := tracer.StartSpan(spanName)
if r != nil {
spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
span = tracer.StartSpan(spanName, opentracing.ChildOf(spanCtx))
}
span.SetTag(string(ext.Component), spanName)
defer span.Finish()
ctx := opentracing.ContextWithSpan(context.Background(), span)
req, err := http.NewRequest(
"GET",
httpUrl,
nil,
)
tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if err != nil {
onError(span, err)
return
}
req = req.WithContext(ctx)
// wrap the request in nethttp.TraceRequest
req, ht := nethttp.TraceRequest(tracer, req)
defer ht.Finish()
res, err := c.Do(req)
if err != nil {
onError(span, err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
onError(span, err)
return
}
fmt.Printf("Received result: %s\n", string(body))
}
func run() {
for {
time.Sleep(3 * time.Second)
tracer := NewJaegerTracer(HttpClientName)
runClient(tracer, "http://localhost:" + *serverPort + "/", HttpClientName, nil)
}
}
- server端上报demo。
注意需将url1和url2替换成下游调用地址。
//server端接收到请求后继续再向java服务发起请求,最终实现client-》go_server-》java_server的请求链路
func getJavaDemo(tracer opentracing.Tracer) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Header)
fmt.Println(opentracing.HTTPHeadersCarrier(r.Header))
span := opentracing.SpanFromContext(r.Context())
span.SetTag("http.server_addr", "server_addr") //填写您的服务端地址
span.SetTag("http.client_addr", "client_addr") //填写您的客户端地址
runClient(tracer, "<url1>", HttpServerName, r)
time.Sleep(10*time.Millisecond)
runClient(tracer, "<url2>", HttpServerName, r)
log.Print("Received getTime request")
io.WriteString(w, "get java demo")
}
}
func runServer(tracer opentracing.Tracer) {
http.HandleFunc("/", getJavaDemo(tracer))
log.Printf("Starting server on port %s", *serverPort)
err := http.ListenAndServe(
fmt.Sprintf(":%s", *serverPort),
// use nethttp.Middleware to enable OpenTracing for server
nethttp.Middleware(tracer, http.DefaultServeMux))
if err != nil {
log.Fatalf("Cannot start server: %s", err)
}
}
- 通过以上例子,我们就可以实现jaeger的方式采集go的监控数据。