traceandtracego

package module
v1.0.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 8 Imported by: 0

README

Go doc Gitter

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

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

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
TRACE_AGENT_HOST localhost:6831
TRACE_REPORTER_LOG_SPANS false/ture

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 \
-p 5775:5775/udp \
-p 16686:16686 \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 14268:14268 \
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.

RPC 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/wrapper/rpc"
)

//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 To call gRPC on the http server side, you need to add the parent context to the rpc 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
    }
}

Documentation

Overview

* Package traceandtrace-go is the go language tracing lib, which can integrate different tracers such as: jeager, zipkin, skywalking, etc. reporter_job can report tracing data and optimize performance to achieve low business intrusion and high performance.

// quick start

	import (
    	tracing "github.com/codeandcode0x/traceandtrace-go"
	)

	// Add in func or middleware
	_, cancel := tracing.AddHttpTracing("HttpTracingTest", [your http Header], map[string]string{"version": "v1"})
	defer cancel()

	...

	reporter_job optimizes the reported tracing data (using goroutine processing), has little business intrusion, and reports with high performance.
	After the job ends, resources can be released.

Index

Examples

Constants

View Source
const (
	JAEGER_TRACER     = "jaeger"
	ZIPKIN_TRACER     = "zipkin"
	SKYWALKING_TRACER = "skywalking"
)

Variables

This section is empty.

Functions

func AddHttpTracing

func AddHttpTracing(svcName, spanName string, header http.Header, tags map[string]string, param ...map[string]string) (context.Context, context.CancelFunc)

Add http tracing , tags is k-v map which can set in span log, param map can set trace type .

Example
package main

import (
	"io/ioutil"
	"log"
	"net/http"

	tracing "github.com/codeandcode0x/traceandtrace-go"
)

func main() {
	httpClient := &http.Client{}
	r, _ := http.NewRequest("GET", "http://www.weather.com.cn/data/sk/101010100.html", nil)
	// set tracing
	_, cancel := tracing.AddHttpTracing("HttpClent", r.Header, map[string]string{"version": "v1"})
	defer cancel()
	// send reqeust
	response, _ := httpClient.Do(r)
	if response.StatusCode == 200 {
		str, _ := ioutil.ReadAll(response.Body)
		bodystr := string(str)
		log.Println("body string", bodystr)
	}
}
Output:

func AddRpcClientTracing

func AddRpcClientTracing(serviceName string, param ...map[string]string) (grpc.DialOption, io.Closer)

add rpc client tracing

func AddRpcServerTracing

func AddRpcServerTracing(serviceName string, param ...map[string]string) (grpc.ServerOption, io.Closer, opentracing.Tracer)

add rpc server tracing

func GenerateTracingJobs

func GenerateTracingJobs(pch chan<- context.Context, parent context.Context, svc, spanName string, header http.Header, tags map[string]string, traceType string)

generate trace jobs (goroutine), use context and chan to control jobs and release goroutine .

Types

This section is empty.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL