-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
64 lines (54 loc) · 1.83 KB
/
types.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
package grpc
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func NewGRPCServer(tracer opentracing.Tracer, logger *logrus.Entry, options ...grpc.ServerOption) *grpc.Server {
var (
opts = append([]grpc.ServerOption{
grpc.UnaryInterceptor(defaultServerInterceptors(tracer, logger)),
}, options...)
)
return grpc.NewServer(opts...)
}
func NewGRPCClient(tracer opentracing.Tracer, logger *logrus.Entry, opts ...grpc.DialOption) func(target string) (*grpc.ClientConn, error) {
var (
o = append([]grpc.DialOption{
grpc.WithUnaryInterceptor(defaultClientInterceptors(tracer, logger)),
}, opts...)
)
return func(target string) (*grpc.ClientConn, error) {
return grpc.Dial(target, o...)
}
}
func defaultClientInterceptors(tracer opentracing.Tracer, logger *logrus.Entry) grpc.UnaryClientInterceptor {
if logger != nil {
return grpc_middleware.ChainUnaryClient(
grpc_opentracing.UnaryClientInterceptor(
grpc_opentracing.WithTracer(tracer),
),
grpc_logrus.UnaryClientInterceptor(logger),
grpc_prometheus.UnaryClientInterceptor,
)
}
return grpc_middleware.ChainUnaryClient(
grpc_opentracing.UnaryClientInterceptor(
grpc_opentracing.WithTracer(tracer),
),
grpc_prometheus.UnaryClientInterceptor,
)
}
func defaultServerInterceptors(tracer opentracing.Tracer, logger *logrus.Entry) grpc.UnaryServerInterceptor {
return grpc_middleware.ChainUnaryServer(
grpc_opentracing.UnaryServerInterceptor(
grpc_opentracing.WithTracer(tracer),
),
grpc_logrus.UnaryServerInterceptor(logger),
grpc_prometheus.UnaryServerInterceptor,
)
}