微服务架构 —— 分布式链路追踪
traceandtrace-go is go tracing lib. It integrate multi tracer such as jeager,zipkin,skywalking and so on
- only support jeager
- support http and gRPC (or both) tracing
- support sampler, sampler type and collector env setting
- support jeager and zipkin
Env | Value |
---|---|
TRACE_SAMPLER_TYPE | const/probabilistic/ratelimiting/remote |
TRACE_SAMPLER_PARAM | 0-1 |
TRACE_ENDPOINT | http://localhost:14268/api/traces (jaeger) or http://localhost:9411/api/v2/spans (zipkin) |
TRACE_AGENT_HOST | localhost:6831 (jaeger) |
TRACE_REPORTER_LOG_SPANS | false or ture |
TRACE_TYPE | jaeger or zipkin |
spanKind
component
samplingPriority
peerService
peerAddress
peerHostname
peerIpv4
peerIpv6
peerPort
httpUrl
httpStatusCode
dbStatement
dbInstance
dbType
httpMethod
dbUser
messageBusDestination
docker run -d --name jaeger \
-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 \
ethansmart-docker.pkg.coding.net/istioalltime/roandocker/jaegertracing-all-in-one:1.22.0
go get github.com/codeandcode0x/traceandtrace-go
Create a trace on the http request method side.
tags are map[string]string type, you can pass logs k-v, tag and field.
Create a trace on the rpc request method side
client
import (
tracing "github.com/codeandcode0x/traceandtrace-go"
)
// create rpc options
rpcOption, closer := tracing.AddRpcClientTracing("RpcClientExample")
defer closer.Close()
// dial
conn, err := grpc.Dial(addr, grpc.WithInsecure(), rpcOption)
if err != nil {
}
...
server
import (
tracing "github.com/codeandcode0x/traceandtrace-go"
)
//No need to request other rpc services
rpcOption, closer, _ := rpcTracing.AddRpcServerTracing(serviceName)
defer closer.Close()
//Add rpcOptions to the server-side monitoring process
s := grpc.NewServer(rpcOption)
Need to request another rpc service
rpcOption, closer, tracer := rpcTracing.AddRpcServerTracing(serviceName)
defer closer.Close()
//Add rpcOptions to the server-side monitoring process
s := grpc.NewServer(rpcOption)
//rpc 请求
newRpcServiceReq(tracer)
...
ptx is parent context, it can create sub-context trace span
To call gRPC on the http server side, you need to add the parent context to the gRPC client. For details, you can see the example .
- By context.Background() create sub-coroutine context, form a session tree (coroutine tree), which is thread-safe (there is no data race problem) ;
- By context WithCancel() create sub-coroutine sessions and manage coroutine tasks ;
- every context will carry related data of parent trace and child span ;
start and end trace job
// start job
ch := make(chan context.Context, 0)
go doTask(ch, ctx, r, svc, traceType, tags)
// end job (receive signal)
pctx := <-ch
pch <- pctx
// release job
for {
select {
case <-ctx.Done():
cancel()
return
default:
break
}
}