package
0.0.12
Repository: https://github.com/fankane/go-utils.git
Documentation: pkg.go.dev

# README

jaeger

  1. 在入口,比如 main.go 里面隐式导入 jaeger 包路径
import _ "github.com/fankane/go-utils/plugin/distributed/jaeger"
  1. 在运行文件根目录下的 system_plugin.yaml 文件(没有则新建一个)里面添加如下内容
plugins:
  distributed:  # 插件类型
    jaeger: # 插件名
      default:                # M连接名称:default,可以是其他名字
        service_name: hufan_ttt
        sampler_type: const
        sampler_param: 1
        log_span: true
        collector_endpoint: "http://localhost:14268/api/traces"
        buffer_flush_interval_ms: 100 #强制刷新时间间隔:毫秒, 不填默认 1000
        user: ""
        password: ""
  1. 使用样例

3.1 (单个服务)

func Root() {
	ctx := GenTraceCTX(context.Background())
	span := Tracer.StartSpan(ctx, "root", Tags(map[string]interface{}{"tag1": "test1"}))
	defer SpanFinish(span)

	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		defer wg.Done()
		A1(CloneTraceCTX(ctx)) //并发处理时 Clone ctx
	}()
	go func() {
		defer wg.Done()
		A2(CloneTraceCTX(ctx))
	}()
	wg.Wait()
	fmt.Println("done")
}

func A1(ctx context.Context) {
	span := Tracer.StartSpan(ctx, "A1", Tags(map[string]interface{}{"tagA1": "testA1"}))
	defer SpanFinish(span)
	time.Sleep(time.Millisecond * 600)
	B(ctx)
}

func A2(ctx context.Context) {
	span := Tracer.StartSpan(ctx, "A2", Tags(map[string]interface{}{"tagA2": "test2"}))
	defer SpanFinish(span)
	time.Sleep(time.Millisecond * 1200)
}

func B(ctx context.Context) {
	span := Tracer.StartSpan(ctx, "B", Logs(map[string]string{"log1": "log 001"}))
	defer SpanFinish(span)
	time.Sleep(time.Millisecond * 500)
	C(ctx)
}

func C(ctx context.Context) {
	span := Tracer.StartSpan(ctx, "C", Logs(map[string]string{"log1": "log 002"}), Tags(map[string]interface{}{"tagA2": "test2"}))
	defer SpanFinish(span)
	time.Sleep(time.Millisecond * 400)
}

效果展示 (单个服务)

avatar avatar

3.2 (Http调用)

import (
  "context"
  "fmt"
  
  "github.com/fankane/go-utils/http"
)

// Client 端
header, err := InjectHttpHeader(tracer.Tracer, span)
if err != nil {
  fmt.Println("Inject err:", err)
  return
}

_, res, err := http.NewClient(http.WithTransport(j2.HttpTransport(nil))).CTXGet(ctx, "http://localhost:8888/test1", http.BaseHeader(header))
if err != nil {
  fmt.Println("get err:", err)
  return
}


// Server 端
span, err := traceClient.StartSpanFromHttpHeader(context.Background(), header, "GET_Server")
if err != nil {
    fmt.Println(err)
    return
}
defer span.Finish()
// do buiness

效果展示 (Http跨服务)

avatar

  1. jaeger UI docker安装
docker run -d --name jaeger2 \
  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 14250:14250 \
  -p 9411:9411 jaegertracing/all-in-one:latest

# Functions

CloneTraceCTX 复制 上下文,获取新的同内容的 TraceInfo 使用场景:当并发多个child function需要进行 span创建的时候,先Clone上下文,让并发时创建的child span父子关系不发生错乱.
GenTraceCTX 生成带 trace信息的上下文,在各级Span创建时,自动创建child span.
No description provided by the author
No description provided by the author
HttpTransport http调用的时候,client 设置 transport,可以跨服务链路追踪,同 InjectHttpHeader 配合使用.
InjectHttpHeader http调用的时候,header构造,可以跨服务链路追踪,同 HttpTransport 配合使用.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author