Skip to content

codeandcode0x/traceandtrace-go

Repository files navigation

Go doc Gitter

MSA Distributed link tracking

微服务架构 —— 分布式链路追踪

Introduction

traceandtrace-go is go tracing lib. It integrate multi tracer such as jeager,zipkin,skywalking and so on

Version introduction

v1.0.0

  • only support jeager
  • support http and gRPC (or both) tracing
  • support sampler, sampler type and collector env setting

v1.0.3

  • support jeager and zipkin

API

godoc

Env Setting

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

Jaeger Ext field

spanKind
component
samplingPriority
peerService
peerAddress
peerHostname
peerIpv4
peerIpv6
peerPort
httpUrl
httpStatusCode
dbStatement
dbInstance
dbType
httpMethod
dbUser
messageBusDestination

Quick Start

Start Jaeger

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

Import Package

go get github.com/codeandcode0x/traceandtrace-go

HTTP tracing

Create a trace on the http request method side. http to grpc client
tags are map[string]string type, you can pass logs k-v, tag and field.

gRPC tracing

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)

...

Http to gRPC tracing

http to grpc client
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 .

Concurrent Processing

goroutine context control

  • 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 ;

goroutine session

Trace Job Control

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
    }
}

Example

Jaeger or Zipkin Tracing

tracing

Maintainer