-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
152 lines (120 loc) · 4.46 KB
/
router.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
147
148
149
150
151
152
package main
import (
"context"
"fmt"
"net/http"
"time"
"google.golang.org/grpc"
pb "github.com/episub/gedoc/gedoc/lib"
"github.com/go-chi/chi"
grpcOpentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
)
const (
address = "localhost:50051"
)
// startRouters Starts each of the external and internal routers
func startRouters(tracer opentracing.Tracer) {
log.Info().Msg("Starting routers")
internalRouter := newRouter(tracer)
internalRouter.Get("/health", healthHandler)
internalRouter.Get("/live", liveHandler)
internalRouter.Handle("/metrics", promhttp.Handler())
log.Info().Int("internal_port", cfg.InternalPort).Int("external_port", cfg.ExternalPort).Msg("listening on ports")
err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.InternalPort), internalRouter)
if err != nil {
log.Fatal().Err(err).Msg("server crashed")
}
}
// newRouter returns a new router with all default values set
func newRouter(tracer opentracing.Tracer) chi.Router {
router := chi.NewRouter()
router.Use(Opentracing(tracer))
return router
}
// Opentracing Adds opentracing to context
func Opentracing(tracer opentracing.Tracer) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return nethttp.Middleware(tracer, next)
}
}
// liveHandler Returns true when the service is live and ready to receive requests
// Works by acting as a client, and actually performing a request
func liveHandler(w http.ResponseWriter, r *http.Request) {
span, _ := opentracing.StartSpanFromContext(r.Context(), "liveHandler")
defer span.Finish()
log.Info().Msg("liveness request received")
healthy, err := checkGRPCHealth(opentracing.ContextWithSpan(r.Context(), span))
if err != nil {
log.Warn().Bool("healthy", healthy).Err(err).Msg("liveness report encountered error")
}
log.Info().Bool("healthy", healthy).Msg("health reply")
if !healthy {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
// healthHandler Returns true when the service is live and ready to receive requests
// Works by acting as a client, and actually performing a request
func healthHandler(w http.ResponseWriter, r *http.Request) {
span, _ := opentracing.StartSpanFromContext(r.Context(), "healthHandler")
defer span.Finish()
log.Printf("Health request received")
healthy, err := checkGRPCHealth(opentracing.ContextWithSpan(r.Context(), span))
if err != nil {
log.Warn().Bool("healthy", healthy).Err(err).Msg("liveness report encountered error")
}
log.Info().Bool("healthy", healthy).Msg("health reply")
if !healthy {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
func checkGRPCHealth(ctx context.Context) (bool, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "checkGRPCHealth")
defer span.Finish()
// Set up a local grpc client so that server can query itself for liveness. This is a better simulation, to ensure that the grpc server is still receiving at least some requests
//conn, err := grpc.Dial(address, grpc.WithInsecure())
conn, err := createClientGRPCConn(opentracing.ContextWithSpan(ctx, span), address)
if err != nil {
return false, err
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Fatal().Err(err).Msg("closing grpc client connection")
}
}(conn)
c := pb.NewBuilderClient(conn)
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
r, err := c.Health(opentracing.ContextWithSpan(ctx, span), &pb.HealthRequest{})
if err != nil {
return false, err
}
return r.Healthy, nil
}
// ctx is the incoming gRPC request's context
// addr is the address for the new outbound request
func createClientGRPCConn(ctx context.Context, addr string) (*grpc.ClientConn, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "createGRPCConn")
defer span.Finish()
var opts []grpc.DialOption
opts = append(opts, grpc.WithStreamInterceptor(grpcOpentracing.StreamClientInterceptor()))
opts = append(opts, grpc.WithUnaryInterceptor(grpcOpentracing.UnaryClientInterceptor()))
opts = append(opts, grpc.WithInsecure())
if !cfg.Debug {
log.Warn().Msg("using grpc in insecure mode")
}
conn, err := grpc.DialContext(ctx, addr, opts...)
if err != nil {
log.Error().Err(err).Msg("connection to application address")
return nil, err
}
return conn, nil
}