-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjaeger.go
84 lines (69 loc) · 2.36 KB
/
jaeger.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gosdk
import (
"context"
"github.com/thinc-org/newbie-gosdk/pkg"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
tr "go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc"
"time"
)
var tracerService pkg.Service
// NewGRPUnaryClientInterceptor returns unary client interceptor. It is used
// with `grpc.WithUnaryInterceptor` method.
func NewGRPUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return otelgrpc.UnaryClientInterceptor()
}
// NewGRPUnaryServerInterceptor returns unary server interceptor. It is used
// with `grpc.UnaryInterceptor` method.
func NewGRPUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return otelgrpc.UnaryServerInterceptor()
}
// NewGRPCStreamClientInterceptor returns stream client interceptor. It is used
// with `grpc.WithStreamInterceptor` method.
func NewGRPCStreamClientInterceptor() grpc.StreamClientInterceptor {
return otelgrpc.StreamClientInterceptor()
}
// NewGRPCStreamServerInterceptor returns stream server interceptor. It is used
// with `grpc.StreamInterceptor` method.
func NewGRPCStreamServerInterceptor() grpc.StreamServerInterceptor {
return otelgrpc.StreamServerInterceptor()
}
type JaegerConfig struct {
Host string `mapstructure:"host"`
Environment string `mapstructure:"env"`
ServiceName string `mapstructure:"service-name"`
}
// SetUpTracer set up the jaeger url and resource setting
func SetUpTracer(conf *JaegerConfig) error {
logger, _ := NewLogger()
service, err := pkg.NewTracerService(conf.Host, conf.Environment, conf.ServiceName)
if err != nil {
return err
}
tracerService = service
logger.Info(
"successfully setup jaeger tracing",
zap.String("service", "jaeger"),
zap.String("host", conf.Host),
zap.String("environment", conf.Environment),
zap.String("service_name", conf.ServiceName),
)
return nil
}
// StartTracer create new trace span
func StartTracer(tracerName string, ctx context.Context, name string, opt ...tr.SpanStartOption) (context.Context, tr.Span) {
if tracerService == nil {
return nil, nil
}
return tracerService.Tracer(tracerName, ctx, name, opt...)
}
// CloseTracer close connection from tracer provider
func CloseTracer() error {
if tracerService == nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
return tracerService.Shutdown(ctx)
}