Skip to content

Commit 4e95c87

Browse files
committed
add example test
1 parent 8a9fcb1 commit 4e95c87

File tree

6 files changed

+61
-118
lines changed

6 files changed

+61
-118
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
微服务架构 —— 分布式链路追踪
1+
# 微服务架构 —— 分布式链路追踪
22

3-
# Tracing SDK
3+
## 简介
4+
traceandtrace-go 是 go 语言 tracing lib, 可以集成不同的 tracer 如: jeager、zipkin、skywalking ... <br>
5+
6+
## 版本介绍
7+
- v1.0.0 目前只支持 jeager
8+
- 支持 http 和 gRPC (or both) 调用链路
9+
- 支持采样率、采样类型、收集器等配置
10+
11+
## API
412

5-
Go、Java Tracing SDK
613

714

815
## 快速开始
916

10-
### 启动 Jaeger or Zipkin
17+
### 启动 Jaeger
1118

1219
```shell
1320
docker run \
@@ -26,11 +33,8 @@ ethansmart-docker.pkg.coding.net/istioalltime/roandocker/jaegertracing-all-in-on
2633
```shell
2734
go get github.com/codeandcode0x/traceandtrace-go
2835
```
29-
或者
3036

31-
```shell
32-
go get github.com/codeandcode0x/traceandtrace-go
33-
```
37+
3438

3539
### HTTP 请求链路
3640
在 http request 方法侧创建 trace

example_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package traceandtracego_test
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"net/http"
7+
8+
tracing "github.com/codeandcode0x/traceandtrace-go"
9+
)
10+
11+
func ExampleAddHttpTracing() {
12+
httpClient := &http.Client{}
13+
r, _ := http.NewRequest("GET", "http://www.weather.com.cn/data/sk/101010100.html", nil)
14+
// set tracing
15+
_, cancel := tracing.AddHttpTracing("HttpClent", r.Header, map[string]string{"version": "v1"})
16+
defer cancel()
17+
// send reqeust
18+
response, _ := httpClient.Do(r)
19+
if response.StatusCode == 200 {
20+
str, _ := ioutil.ReadAll(response.Body)
21+
bodystr := string(str)
22+
log.Println("body string", bodystr)
23+
}
24+
}

reporter_jobs.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/** Package traceandtrace-go 是 go 语言 tracing lib, 可以集成不同的 tracer 如: jeager、zipkin、skywalking 等
2+
reporter_job 可以对 tracing 数据上报,做了性能优化,做到业务侵入小, 高性能等。
3+
4+
// 快速上手
5+
import (
6+
tracing "github.com/codeandcode0x/traceandtrace-go"
7+
)
8+
9+
// 在 func 中 或者 middleware 中添加
10+
_, cancel := tracing.AddHttpTracing("HttpTracingTest", [your http Header], map[string]string{"version": "v1"})
11+
defer cancel()
12+
13+
...
14+
15+
reporter_job 对上报 tracing 数据进行了优化 (采用携程任务处理),对业务侵入小,高性能上报,job 结束后, 可以对资源进行释放。
16+
*/
117
package traceandtracego
218

319
import (
@@ -10,9 +26,8 @@ import (
1026
opentracing "github.com/opentracing/opentracing-go"
1127
)
1228

13-
//生成 trace jobs
14-
func GenerateTracingJobs(pch chan<- context.Context, parent context.Context,
15-
svc string, header http.Header, tags map[string]string, traceType string) {
29+
//generate trace jobs (goroutine), use context and chan to control jobs and release goroutine .
30+
func GenerateTracingJobs(pch chan<- context.Context, parent context.Context, svc string, header http.Header, tags map[string]string, traceType string) {
1631
//设置 context
1732
ctx, cancel := context.WithCancel(parent)
1833
//设置通道
@@ -48,10 +63,8 @@ func doTask(ch chan context.Context, parent context.Context,
4863
break
4964
case "zipkin":
5065
log.Println("create zipkin tracing job")
51-
tracer, closer = tracing.InitZipkin(svc)
52-
ctx = tracing.AddTracer(svc, parent, header, tracer, tags)
5366
break
54-
case "skyWalking":
67+
case "skywalking":
5568
log.Println("create skywalking tracing job")
5669
break
5770
default:

tracer/skywalking.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

tracer/zipkin.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
httpreporter "github.com/openzipkin/zipkin-go/reporter/http"
1111
)
1212

13-
// var ctxShare context.Context
14-
// var tracer opentracing.Tracer
15-
1613
//初始化 zipkin
1714
func InitZipkin(service string) (opentracing.Tracer, reporter.Reporter) {
1815
//设置 span reporter
@@ -33,26 +30,3 @@ func InitZipkin(service string) (opentracing.Tracer, reporter.Reporter) {
3330
//返回 trace & reporter
3431
return tracer, reporter
3532
}
36-
37-
//添加 tracer
38-
// func AddTracer(r *http.Request, tracer opentracing.Tracer) {
39-
// opentracing.InitGlobalTracer(tracer)
40-
// sp := tracer.StartSpan(r.URL.Path)
41-
// spanCtx, _ := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
42-
// opentracing.HTTPHeadersCarrier(r.Header))
43-
// if spanCtx != nil {
44-
// sp = opentracing.GlobalTracer().StartSpan(r.URL.Path, opentracing.ChildOf(spanCtx))
45-
// } else {
46-
// //http inject
47-
// if err := opentracing.GlobalTracer().Inject(
48-
// sp.Context(),
49-
// opentracing.HTTPHeaders,
50-
// opentracing.HTTPHeadersCarrier(r.Header)); err != nil {
51-
// logger.Println("[traceandtrace] [Error] inject failed ...", err)
52-
// }
53-
// }
54-
// //上下文记录父spanContext
55-
// ctxShare = context.WithValue(context.Background(), "usergRpcCtx", opentracing.ContextWithSpan(context.Background(), sp))
56-
// //close span
57-
// defer sp.Finish()
58-
// }

wrapper.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,19 @@ import (
1414
const (
1515
JAEGER_TRACER = "jaeger"
1616
ZIPKIN_TRACER = "zipkin"
17-
SKYWALKING_TRACER = "skyWalking"
17+
SKYWALKING_TRACER = "skywalking"
1818
)
1919

20-
//add tracing
21-
func AddHttpTracing(
22-
svcName string,
23-
header http.Header,
24-
tags map[string]string,
25-
param ...map[string]string) (context.Context, context.CancelFunc) {
20+
// Add http tracing , tags is k-v map which can set in span log, param map can set trace type .
21+
func AddHttpTracing(svcName string, header http.Header, tags map[string]string, param ...map[string]string) (context.Context, context.CancelFunc) {
2622
// 定义 trace type
2723
var traceType string
2824
//启动 trace 任务
2925
ctx, cancel := context.WithCancel(context.Background())
3026
//创建通道
3127
ch := make(chan context.Context, 0)
3228
//选择类型和服务
33-
traceType = JAEGER_TRACER
29+
ç = JAEGER_TRACER
3430
if len(param) > 0 {
3531
if _, exist := param[0]["traceType"]; exist {
3632
traceType = strings.ToLower(param[0]["traceType"])
@@ -43,27 +39,17 @@ func AddHttpTracing(
4339
}
4440

4541
//add rpc client tracing
46-
func AddRpcClientTracing(serviceName string) (grpc.DialOption, io.Closer) {
42+
func AddRpcClientTracing(serviceName string, param ...map[string]string) (grpc.DialOption, io.Closer) {
4743
//初始化 jaeger
4844
tracer, closer := tracing.InitJaeger(serviceName)
4945
//返回 rpc options
5046
return tracing.ClientDialOption(tracer), closer
5147
}
5248

5349
//add rpc server tracing
54-
func AddRpcServerTracing(serviceName string) (grpc.ServerOption, io.Closer, opentracing.Tracer) {
50+
func AddRpcServerTracing(serviceName string, param ...map[string]string) (grpc.ServerOption, io.Closer, opentracing.Tracer) {
5551
//初始化 jaeger
5652
tracer, closer := tracing.InitJaeger(serviceName)
5753
//返回 rpc options
5854
return tracing.ServerDialOption(tracer), closer, tracer
5955
}
60-
61-
//zipkin
62-
func AddZipkinTracer(serviceName string) {
63-
//TO-DO
64-
}
65-
66-
//skywalking
67-
func AddSkyWalkingTracer(serviceName string) {
68-
//TO-DO
69-
}

0 commit comments

Comments
 (0)