/
builder.go
386 lines (321 loc) · 10.3 KB
/
builder.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package cadre
import (
"fmt"
"log"
"net"
stdhttp "net/http"
"os"
"strings"
"syscall"
"time"
"github.com/gin-gonic/gin"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
channelz "github.com/rantav/go-grpc-channelz"
grpc_middleware "github.com/rkollar/go-grpc-middleware"
grpc_zerolog "github.com/rkollar/go-grpc-middleware/logging/zerolog"
grpc_recovery "github.com/rkollar/go-grpc-middleware/recovery"
grpc_ctxtags "github.com/rkollar/go-grpc-middleware/tags"
"github.com/rs/zerolog"
"golang.org/x/net/context"
"google.golang.org/grpc"
channelz_service "google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"github.com/moderntv/cadre/http"
"github.com/moderntv/cadre/http/responses"
"github.com/moderntv/cadre/metrics"
"github.com/moderntv/cadre/status"
)
type ServiceRegistrator func(*grpc.Server)
type Finisher func(sig os.Signal)
type Builder struct {
name string // app name
ctx context.Context
finisherCallback Finisher
handledSigs []os.Signal
// logging
logger zerolog.Logger
// status
status *status.Status
statusHTTPServerAddr string
statusPath string
// metrics
metrics *metrics.Registry
prometheusRegistry *prometheus.Registry
metricsHTTPServerAddr string
metricsPath string
grpcOptions *grpcOptions
httpOptions []*httpOptions
}
// NewBuilder creates a new Builder instance and allows the user to configure the Cadre server by various options
func NewBuilder(name string, options ...Option) (b *Builder, err error) {
b = &Builder{
name: name,
ctx: context.Background(),
handledSigs: []os.Signal{syscall.SIGINT, syscall.SIGTERM},
logger: zerolog.Nop(),
statusPath: "/status",
metricsPath: "/metrics",
grpcOptions: nil,
httpOptions: nil,
}
for _, option := range options {
err = option(b)
if err != nil {
err = fmt.Errorf("cannot apply option: %w", err)
return
}
}
return
}
// Build validates the Builder's configuration and creates a new Cadre server
func (b *Builder) Build() (c *cadre, err error) {
err = b.ensure()
if err != nil {
err = fmt.Errorf("cadre builder validation is invalid: %w", err)
return
}
// logger, status, metrics already exist here no matter what
ctx, ctxCancel := context.WithCancel(b.ctx)
c = &cadre{
ctx: ctx,
ctxCancel: ctxCancel,
finisherCallback: b.finisherCallback,
handledSigs: b.handledSigs,
finalizerDone: make(chan bool),
logger: b.logger,
status: b.status,
metrics: b.metrics,
httpServers: make(map[string]*stdhttp.Server),
}
if b.httpOptions == nil && b.grpcOptions == nil {
err = fmt.Errorf("both grpc and http will be disabled. what do you want me to do?")
return
}
// extra http services init
if b.metricsHTTPServerAddr != "" {
err = WithHTTP("metrics_http",
WithHTTPListeningAddress(b.metricsHTTPServerAddr),
WithRoute("GET", b.metricsPath, gin.WrapH(promhttp.HandlerFor(b.prometheusRegistry, promhttp.HandlerOpts{}))),
)(b)
if err != nil {
err = fmt.Errorf("adding metrics http server failed: %w", err)
return
}
}
if b.statusHTTPServerAddr != "" {
err = WithHTTP("status_http",
WithHTTPListeningAddress(b.statusHTTPServerAddr),
WithRoute("GET", b.statusPath, func(c *gin.Context) {
report := b.status.Report()
if report.Status != status.ERROR {
responses.Ok(c, report)
return
}
c.AbortWithStatusJSON(503, gin.H{
"data": report,
})
}),
)(b)
if err != nil {
err = fmt.Errorf("adding status http server failed: %w", err)
return
}
}
if b.grpcOptions != nil && b.grpcOptions.enableChannelz {
channelzHandler := channelz.CreateHandler("/", b.grpcOptions.listeningAddress)
err = WithHTTP("channelz_http",
WithHTTPListeningAddress(b.grpcOptions.channelzHttpAddr),
WithRoute("GET", "/channelz/*path", func(c *gin.Context) {
channelzHandler.ServeHTTP(c.Writer, c.Request)
}),
)(b)
if err != nil {
err = fmt.Errorf("adding channelz http server failed: %w", err)
return
}
}
// create and configure grpc server
if b.grpcOptions != nil {
err = b.buildGrpc(c)
if err != nil {
err = fmt.Errorf("grpc server building failed: %w", err)
return
}
if b.grpcOptions.enableChannelz {
channelz_service.RegisterChannelzServiceToServer(c.grpcServer)
}
}
// create and configure http server
var httpServers map[string]*http.HttpServer
if b.httpOptions != nil {
httpServers, err = b.buildHTTP(c, ctx)
if err != nil {
return
}
}
c.httpServers = map[string]*stdhttp.Server{}
for addr, httpServer := range httpServers {
httpServer.LogRegisteredRoutes()
var h stdhttp.Handler = httpServer
// http+grpc multiplexing
if b.grpcOptions != nil && b.grpcOptions.listeningAddress == addr {
h = stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
log.Printf("handling http request. protomajor = %v; content-type = %v; headers = %v", r.ProtoMajor, r.Header.Get("content-type"), r.Header)
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
c.grpcServer.ServeHTTP(w, r)
} else {
httpServer.ServeHTTP(w, r)
}
})
log.Println("disable grpcListener")
c.grpcListener = nil
}
c.httpServers[addr] = &stdhttp.Server{
Addr: addr,
Handler: h,
ReadHeaderTimeout: 5 * time.Second,
}
}
return
}
func (b *Builder) ensure() (err error) {
// check prometheus & metrics. ensure they use the same prometheus registry
if b.metrics != nil && b.prometheusRegistry != nil {
err = fmt.Errorf("pass either existing metrics.Registry or prometheus.Registry. not both")
return
}
// initialize metrics
if b.metrics == nil {
// if b.prometheusRegistry is nil, metrics will just create a new one
b.metrics, err = metrics.NewRegistry(b.name, b.prometheusRegistry)
}
if b.prometheusRegistry == nil {
b.prometheusRegistry = b.metrics.GetPrometheusRegistry()
}
// basic checks
if b.status == nil {
b.status = status.NewStatus("TODO")
}
// grpc checks
if b.grpcOptions != nil {
err = b.grpcOptions.ensure()
if err != nil {
return
}
}
// http checks
for _, httpServerOptions := range b.httpOptions {
err = httpServerOptions.ensure()
if err != nil {
return
}
}
// configure metrics + status endpoint http servers
if len(b.httpOptions) >= 1 {
if b.statusHTTPServerAddr == "" {
b.statusHTTPServerAddr = b.httpOptions[0].listeningAddress
}
if b.metricsHTTPServerAddr == "" {
b.metricsHTTPServerAddr = b.httpOptions[0].listeningAddress
}
if b.grpcOptions != nil && b.grpcOptions.multiplexWithHTTP {
b.grpcOptions.listeningAddress = b.httpOptions[0].listeningAddress
}
}
return
}
func (b *Builder) buildGrpc(c *cadre) (err error) {
// metrics
grpcMetrics := grpc_prometheus.NewServerMetrics()
err = b.metrics.Register("grpc", grpcMetrics)
if err != nil {
err = fmt.Errorf("cannot register grpc metrics to metrics registry: %w", err)
return
}
// interceptors - always in order: tags, logging, tracing, metrics, custom, recovery => recovery should be always the last one
unaryInterceptors := []grpc.UnaryServerInterceptor{}
streamInterceptors := []grpc.StreamServerInterceptor{}
// logging
if b.grpcOptions.enableLoggingMiddleware {
unaryInterceptors = append(
unaryInterceptors,
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zerolog.UnaryServerInterceptor(b.logger, b.grpcOptions.loggingMiddlewareOptions...),
)
streamInterceptors = append(
streamInterceptors,
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zerolog.StreamServerInterceptor(b.logger, b.grpcOptions.loggingMiddlewareOptions...),
)
}
// TODO: add tracing middleware
// metrics middleware
unaryInterceptors = append(unaryInterceptors, grpcMetrics.UnaryServerInterceptor())
streamInterceptors = append(streamInterceptors, grpcMetrics.StreamServerInterceptor())
// add extra interceptors
unaryInterceptors = append(unaryInterceptors, b.grpcOptions.extraUnaryInterceptors...)
streamInterceptors = append(streamInterceptors, b.grpcOptions.extraStreamInterceptors...)
// recovery middleware
if b.grpcOptions.enableRecoveryMiddleware {
unaryInterceptors = append(unaryInterceptors, grpc_recovery.UnaryServerInterceptor(b.grpcOptions.recoveryMiddlewareOptions...))
streamInterceptors = append(streamInterceptors, grpc_recovery.StreamServerInterceptor(b.grpcOptions.recoveryMiddlewareOptions...))
}
//
//create grpc server
//
c.grpcAddr = b.grpcOptions.listeningAddress
c.grpcServer = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(unaryInterceptors...),
grpc_middleware.WithStreamServerChain(streamInterceptors...),
)
// replace gRPC logger
// grpc_zerolog.ReplaceGrpcLoggerV2(b.logger.Level(zerolog.ErrorLevel))
// register services
// health service
if b.grpcOptions.enableHealthService {
healthService := health.NewServer()
healthpb.RegisterHealthServer(c.grpcServer, healthService)
}
// reflection
if b.grpcOptions.enableReflection {
reflection.Register(c.grpcServer)
}
// user-specified grpc services
for _, registrator := range b.grpcOptions.services {
registrator(c.grpcServer)
}
// grpc listener
if b.grpcOptions.listeningAddress != "" && !b.grpcOptions.multiplexWithHTTP {
c.grpcListener, err = net.Listen("tcp", b.grpcOptions.listeningAddress)
if err != nil {
return
}
}
return
}
func (b *Builder) buildHTTP(_ *cadre, cadreContext context.Context) (httpServers map[string]*http.HttpServer, err error) {
httpServers = map[string]*http.HttpServer{}
mergedHTTPOptions := map[string]*httpOptions{}
for _, newServer := range b.httpOptions {
addr := newServer.listeningAddress
if existingServer, ok := mergedHTTPOptions[addr]; ok {
mergedHTTPOptions[addr], err = existingServer.merge(newServer)
if err != nil {
return
}
continue
}
mergedHTTPOptions[newServer.listeningAddress] = newServer
}
for _, httpOptions := range mergedHTTPOptions {
httpServers[httpOptions.listeningAddress], err = httpOptions.build(cadreContext, b.logger, b.metrics)
if err != nil {
return
}
}
return
}