-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.go
57 lines (51 loc) · 1.67 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package traceandtracego
import (
"context"
"io"
"net/http"
"os"
"strings"
tracing "github.com/codeandcode0x/traceandtrace-go/tracer"
opentracing "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)
const (
JAEGER_TRACER = "jaeger"
ZIPKIN_TRACER = "zipkin"
SKYWALKING_TRACER = "skywalking"
)
//Add http tracing , tags is k-v map which can set in span log, param map can set trace type .
func AddHttpTracing(svcName, spanName string, header http.Header, tags map[string]string, param ...map[string]string) (context.Context, context.CancelFunc) {
//trace type
var traceType string
//start trace task
ctx, cancel := context.WithCancel(context.Background())
//create chan
ch := make(chan context.Context, 0)
//trace type
traceType = JAEGER_TRACER
//get trace type env
if tType := os.Getenv("TRACE_TYPE"); tType != "" {
traceType = tType
} else if _, exist := tags["traceType"]; exist {
traceType = strings.ToLower(tags["traceType"])
}
//create goroutine job
go GenerateTracingJobs(ch, ctx, svcName, spanName, header, tags, traceType)
//return chan
return <-ch, cancel
}
//add rpc client tracing
func AddRpcClientTracing(serviceName string, param ...map[string]string) (grpc.DialOption, io.Closer) {
//init tracer
tracer, closer := SelectInitTracer(serviceName, param...)
//return rpc options
return tracing.ClientDialOption(tracer), closer
}
//add rpc server tracing
func AddRpcServerTracing(serviceName string, param ...map[string]string) (grpc.ServerOption, io.Closer, opentracing.Tracer) {
//init jaeger
tracer, closer := SelectInitTracer(serviceName, param...)
//return rpc options
return tracing.ServerDialOption(tracer), closer, tracer
}