Golang集成Opentracing(分布式链路追踪)

2023-05-20  本文已影响0人  robertzhai
image.png
image.png
image.png
image.png

demo

启动服务

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.14

``
> app demo

package main

import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"

)

// initJaeger 将jaeger tracer设置为全局tracer
func initJaeger(service string) io.Closer {
cfg := jaegercfg.Configuration{
// 将采样频率设置为1,每一个span都记录,方便查看测试结果
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
// 将span发往jaeger-collector的服务地址
CollectorEndpoint: "http://localhost:14268/api/traces",
},
}
closer, err := cfg.InitGlobalTracer(service, jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
return closer
}

func main() {
closer := initJaeger("api")
defer closer.Close()
// 获取jaeger tracer
t := opentracing.GlobalTracer()
// 创建root span
sp := t.StartSpan("UserService")
// main执行完结束这个span
defer sp.Finish()
// 将span传递给Foo
ctx := opentracing.ContextWithSpan(context.Background(), sp)
Foo(ctx)
}

func Foo(ctx context.Context) {

// 开始一个span, 设置span的operation_name=Foo
span, ctx := opentracing.StartSpanFromContext(ctx, "Foo")
defer span.Finish()
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
    defer wg.Done()
    // 将context传递给Bar
    Bar(ctx)
}()
go func() {
    defer wg.Done()
    // 将context传递给Test
    Test(ctx)
}()

wg.Wait()
// 模拟执行耗时
time.Sleep(1 * time.Second)

}
func Bar(ctx context.Context) {
// 开始一个span,设置span的operation_name=Bar
span, ctx := opentracing.StartSpanFromContext(ctx, "Bar")
defer span.Finish()
// 模拟执行耗时
time.Sleep(2 * time.Second)
// 假设Bar发生了某些错误
err := errors.New("something wrong")
span.LogFields(
log.String("event", "error"),
log.String("message", err.Error()),
)
span.SetTag("error", true)
}

func Test(ctx context.Context) {
// 开始一个span, 设置span的operation_name=Foo
span, ctx := opentracing.StartSpanFromContext(ctx, "Test")
defer span.Finish()
// 模拟执行耗时
time.Sleep(1 * time.Second)
}


### 收集数据效果截图
> go run main.go
>访问 http://localhost:16686/search

![image.png](https://img.haomeiwen.com/i3840925/4e3130e7db3689bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://img.haomeiwen.com/i3840925/0ea5f9e903ef62f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://img.haomeiwen.com/i3840925/97666d295d0cdea1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://img.haomeiwen.com/i3840925/7960178204d4b053.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

### trace json

{
"data": [
{
"traceID": "4da51d7e3439fa95",
"spans": [
{
"traceID": "4da51d7e3439fa95",
"spanID": "747f937912e693c0",
"flags": 1,
"operationName": "Test",
"references": [
{
"refType": "CHILD_OF",
"traceID": "4da51d7e3439fa95",
"spanID": "5370f4994521a1e9"
}
],
"startTime": 1684631004933177,
"duration": 1002993,
"tags": [
{
"key": "internal.span.format",
"type": "string",
"value": "jaeger"
}
],
"logs": [],
"processID": "p1",
"warnings": null
},
{
"traceID": "4da51d7e3439fa95",
"spanID": "2313988bbe4207a",
"flags": 1,
"operationName": "Bar",
"references": [
{
"refType": "CHILD_OF",
"traceID": "4da51d7e3439fa95",
"spanID": "5370f4994521a1e9"
}
],
"startTime": 1684631004933333,
"duration": 2002919,
"tags": [
{
"key": "error",
"type": "bool",
"value": true
},
{
"key": "internal.span.format",
"type": "string",
"value": "jaeger"
}
],
"logs": [
{
"timestamp": 1684631006936289,
"fields": [
{
"key": "event",
"type": "string",
"value": "error"
},
{
"key": "message",
"type": "string",
"value": "something wrong"
}
]
}
],
"processID": "p1",
"warnings": null
},
{
"traceID": "4da51d7e3439fa95",
"spanID": "5370f4994521a1e9",
"flags": 1,
"operationName": "Foo",
"references": [
{
"refType": "CHILD_OF",
"traceID": "4da51d7e3439fa95",
"spanID": "4da51d7e3439fa95"
}
],
"startTime": 1684631004933106,
"duration": 3004627,
"tags": [
{
"key": "internal.span.format",
"type": "string",
"value": "jaeger"
}
],
"logs": [],
"processID": "p1",
"warnings": null
},
{
"traceID": "4da51d7e3439fa95",
"spanID": "4da51d7e3439fa95",
"flags": 1,
"operationName": "UserService",
"references": [],
"startTime": 1684631004933087,
"duration": 3004954,
"tags": [
{
"key": "sampler.type",
"type": "string",
"value": "const"
},
{
"key": "sampler.param",
"type": "bool",
"value": true
},
{
"key": "internal.span.format",
"type": "string",
"value": "jaeger"
}
],
"logs": [],
"processID": "p1",
"warnings": null
}
],
"processes": {
"p1": {
"serviceName": "api",
"tags": [
{
"key": "client-uuid",
"type": "string",
"value": "77af7d618b5dd7a9"
},
{
"key": "hostname",
"type": "string",
"value": "robertdeair"
},
{
"key": "ip",
"type": "string",
"value": "192.168.1.7"
},
{
"key": "jaeger.version",
"type": "string",
"value": "Go-2.30.0"
}
]
}
},
"warnings": null
}
],
"total": 0,
"limit": 0,
"offset": 0,
"errors": null
}


### trace  span 字段含义

{
"traceID": "4da51d7e3439fa95",
"spanID": "747f937912e693c0", // 当前span id
"flags": 1,
"operationName": "Test", // span 名称
"references": [ // 引用
{
"refType": "CHILD_OF", // 父子
"traceID": "4da51d7e3439fa95",
"spanID": "5370f4994521a1e9" // 父span
}
],
"startTime": 1684631004933177, // 开始时间
"duration": 1002993, // 持续时间 微秒
"tags": [
{
"key": "internal.span.format",
"type": "string",
"value": "jaeger"
}
],
"logs": [],
"processID": "p1",
"warnings": null
}

>trace id:77af7d618b5dd7a9
![image.png](https://img.haomeiwen.com/i3840925/ec6fcf827cb0d763.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

### ref
- https://www.jaegertracing.io/docs/1.45/
- https://github.com/opentracing/specification/blob/master/semantic_conventions.md
- https://juejin.cn/post/6844903942309019661
- https://opentelemetry.io/docs/what-is-opentelemetry/
上一篇下一篇

猜你喜欢

热点阅读