代码炼金工坊

链路追踪建设分享

背景

链路追踪如何解决问题

原理简述

Jaeger参考文档

落地实践

go服务端gin中间件使用方法

代码库地址:yuchanns/bullets

go get -u github.com/yuchanns/bullets
package main

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/yuchanns/bullets/common"
	"github.com/yuchanns/bullets/common/middlewares"
	"os"
)

func main() {
	g := gin.Default()
	// 服务名
	serviceName := "openapi-service"
	// 上报agent地址
	agentAddr := os.Getenv("OPENTRACING_AGENT")
	// 操作前缀
	operationPrefix := []byte("api-request-")
	opentracerCloseFunc, opentracerMiddleware, err := middlewares.BuildOpenTracerInterceptor(serviceName, agentAddr, operationPrefix)
	if err != nil {
		common.Logger.Error(context.Background(), err)
	} else {
		defer opentracerCloseFunc()
		g.Use(opentracerMiddleware)
	}
}
import (
	"github.com/gin-gonic/gin"
	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/log"
	"github.com/pkg/errors"
)

func CustomTag(ctx *gin.Context) {
	if cspan, ok := ctx.Get("tracing-context"); ok {
		if span, ok := cspan.(opentracing.Span); ok {
			span.SetTag("error", true)
			span.LogFields(log.Error(errors.New("err")))
			span.LogFields(log.String("exampleKey", "stringValue"))
		}
	}
}