Jaeger-分布式链路追踪系统

Screen Shot 2023-02-09 at 11.39.03.png

简单介绍

Jaeger是Uber开源的分布式链路追踪系统,其设计借鉴了Dapper和OpenZipkin,常用于监控和分布式系的故障排查,其主要的特性如下

  • 分布式上下文传播
  • 分布式事务监控
  • root原因分析
  • 服务依赖性分析
  • 性能/延迟优化

jaeger提供了可视化界面,可以很直观的观察数据链路

基本概念

  • tracer:表示一条链路
  • span:表示一个操作,隶属于tracer

系统架构

image.png

快速开始

  1. docker方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    docker run -d --name jaeger \
    -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
    -e COLLECTOR_OTLP_ENABLED=true \
    -p 6831:6831/udp \
    -p 6832:6832/udp \
    -p 5778:5778 \
    -p 16686:16686 \
    -p 4317:4317 \
    -p 4318:4318 \
    -p 14250:14250 \
    -p 14268:14268 \
    -p 14269:14269 \
    -p 9411:9411 \
    jaegertracing/all-in-one:1.42
  2. 二进制

下载链接:https://www.jaegertracing.io/download/

1
wget https://github.com/jaegertracing/jaeger/releases/download/v1.42.0/jaeger-1.42.0-darwin-arm64.tar.gz
1
jaeger-all-in-one --collector.zipkin.host-port=:9411
  1. 访问UI界面

    1
    http://localhost:16686

    截屏2023-02-09 12.51.18.png

  2. 获取案例并运行

    1
    2
    3
    git clone https://github.com/jaegertracing/jaeger.git
    cd jaeger
    go run ./examples/hotrod/main.go all
  3. 自定义实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    package main

    import (
    "context"
    "fmt"
    "github.com/opentracing/opentracing-go"
    "github.com/opentracing/opentracing-go/log"
    "github.com/uber/jaeger-client-go"
    "github.com/uber/jaeger-client-go/config"
    "io"
    "time"
    )

    func main() {
    //初始化一个tracer
    tracer, closer := initJaeger("hello-world")
    //设置全局tracer
    opentracing.SetGlobalTracer(tracer)
    //创建一个名叫say-hello的操作
    span := tracer.StartSpan("say-hello")
    //通过上面的span创建一个ctx,在后面我们可以通过其走同一条完整链路
    ctx := opentracing.ContextWithSpan(context.Background(), span)
    hello(ctx)
    sayHi := opentracing.SpanFromContext(ctx)
    sayHi.SetTag("Hi", "Hi")
    sayHi.Finish()
    defer func() {
    span.Finish()
    closer.Close()
    }()
    }

    func hello(ctx context.Context) {
    //再次创建一个span,注意这个是say-hello的子span
    span, ctx := opentracing.StartSpanFromContext(ctx, "hello hello")
    defer span.Finish()
    span.LogFields(log.String("key", "loggggggg"))
    span.SetTag("tag", "tagValue")
    sleep() //do something
    }

    // initJaeger returns an instance of Jaeger Tracer that samples 100% of traces and logs all spans to stdout.
    func initJaeger(service string) (opentracing.Tracer, io.Closer) {
    cfg := &config.Configuration{
    ServiceName: service,
    Sampler: &config.SamplerConfig{
    Type: "const",
    Param: 1,
    },
    Reporter: &config.ReporterConfig{
    LogSpans: true,
    LocalAgentHostPort: "10.10.100.28:6831", //指定本地代理ip端口
    },
    }
    tracer, closer, err := cfg.NewTracer(config.Logger(jaeger.StdLogger))
    if err != nil {
    panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
    }
    return tracer, closer
    }

    func sleep() {
    time.Sleep(2 * time.Second)
    }

image.png

相关资料

  1. 官方文档:https://www.jaegertracing.io/docs/1.42/
  2. github:https://github.com/jaegertracing/jaeger
  3. 教程:https://github.com/yurishkuro/opentracing-tutorial/tree/master/go

Jaeger-分布式链路追踪系统
https://mikeygithub.github.io/2023/02/09/yuque/Jaeger-分布式链路追踪系统/
作者
Mikey
发布于
2023年2月9日
许可协议