-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinger.go
146 lines (121 loc) · 3.29 KB
/
pinger.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/prometheus/client_golang/prometheus/promhttp"
pb "github.com/mad01/pingpong/com"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
type pingServer struct {
cc *grpc.ClientConn
closer io.Closer
}
func (p *pingServer) MsgConn(addr string) error {
cc, closer, err := clientGRPCconn(addr, "pinger")
if err != nil {
return err
}
p.closer = closer
p.cc = cc
return nil
}
func (p *pingServer) Ping(ctx context.Context, in *pb.PingRequest) (*pb.PongResponse, error) {
client := pb.NewRandomMsgClient(p.cc)
msgResp, err := client.GetRandomMsg(ctx, &pb.RandomMsgRequest{}) // use incomming context to take span for tracing
if err != nil {
return nil, fmt.Errorf("Fail to get msg from downstream: %v", err.Error())
}
response := pb.PongResponse{Msg: msgResp.Msg}
return &response, nil
}
func servePingGRPC(conf *config, errChan chan error) {
lis, err := net.Listen("tcp", conf.grpcPingerAddr)
if err != nil {
errChan <- err
}
//
// opentracing
tracer, closer, err := getTracer("pinger")
if err != nil {
errChan <- err
}
//
//
//
// zap
zapOpts := []grpc_zap.Option{
grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field {
return zap.Int64("grpc.time_ns", duration.Nanoseconds())
}),
}
zapLogger, _ := zap.NewProduction()
defer zapLogger.Sync() // flushes buffer, if any
//
//
middlewareServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(zapLogger, zapOpts...),
grpc_prometheus.StreamServerInterceptor,
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_ctxtags.UnaryServerInterceptor(),
otgrpc.OpenTracingServerInterceptor(*tracer),
grpc_zap.UnaryServerInterceptor(zapLogger, zapOpts...),
grpc_prometheus.UnaryServerInterceptor,
)),
)
pinger := pingServer{}
pinger.MsgConn(conf.grpcMsgAddr)
pb.RegisterPingerServer(middlewareServer, &pinger)
grpc_prometheus.Register(middlewareServer)
grpc_prometheus.EnableHandlingTimeHistogram()
// Register reflection service on gRPC server.
reflection.Register(middlewareServer)
go func() {
defer pinger.cc.Close()
defer pinger.closer.Close()
defer closer.Close()
errChan <- middlewareServer.Serve(lis)
}()
}
func servePingHTTP(conf *config, errChan chan error) {
http.Handle("/metrics2", promhttp.Handler())
go func() {
errChan <- http.ListenAndServe(conf.httpPingerAddr, nil)
}()
}
func servePingAll(c *config) {
errChan := make(chan error, 10)
go servePingHTTP(c, errChan)
go servePingGRPC(c, errChan)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case err := <-errChan:
if err != nil {
fmt.Printf("%s \n", err)
os.Exit(1)
}
case <-signalChan:
fmt.Println("Shutdown signal received, exiting...")
os.Exit(0)
}
}
}