-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathserver.go
560 lines (492 loc) · 23.3 KB
/
server.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package server
import (
"crypto/tls"
"flag"
"fmt"
"math"
"net"
"net/http"
_ "net/http/pprof" // anonymous import to get the pprof handler registered
"strings"
"time"
"github.com/gorilla/mux"
otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
"github.com/soheilhy/cmux"
"golang.org/x/net/context"
"golang.org/x/net/netutil"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"github.com/weaveworks/common/httpgrpc"
httpgrpc_server "github.com/weaveworks/common/httpgrpc/server"
"github.com/weaveworks/common/logging"
"github.com/weaveworks/common/middleware"
"github.com/weaveworks/common/signals"
)
// Listen on the named network
const (
// DefaultNetwork the host resolves to multiple IP addresses,
// Dial will try each IP address in order until one succeeds
DefaultNetwork = "tcp"
// NetworkTCPV4 for IPV4 only
NetworkTCPV4 = "tcp4"
)
// SignalHandler used by Server.
type SignalHandler interface {
// Starts the signals handler. This method is blocking, and returns only after signal is received,
// or "Stop" is called.
Loop()
// Stop blocked "Loop" method.
Stop()
}
// TLSConfig contains TLS parameters for Config.
type TLSConfig struct {
TLSCertPath string `yaml:"cert_file"`
TLSKeyPath string `yaml:"key_file"`
ClientAuth string `yaml:"client_auth_type"`
ClientCAs string `yaml:"client_ca_file"`
}
// Config for a Server
type Config struct {
MetricsNamespace string `yaml:"-"`
// Set to > 1 to add native histograms to requestDuration.
// See documentation for NativeHistogramBucketFactor in
// https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#HistogramOpts
// for details. A generally useful value is 1.1.
MetricsNativeHistogramFactor float64 `yaml:"-"`
HTTPListenNetwork string `yaml:"http_listen_network"`
HTTPListenAddress string `yaml:"http_listen_address"`
HTTPListenPort int `yaml:"http_listen_port"`
HTTPConnLimit int `yaml:"http_listen_conn_limit"`
GRPCListenNetwork string `yaml:"grpc_listen_network"`
GRPCListenAddress string `yaml:"grpc_listen_address"`
GRPCListenPort int `yaml:"grpc_listen_port"`
GRPCConnLimit int `yaml:"grpc_listen_conn_limit"`
CipherSuites string `yaml:"tls_cipher_suites"`
MinVersion string `yaml:"tls_min_version"`
HTTPTLSConfig TLSConfig `yaml:"http_tls_config"`
GRPCTLSConfig TLSConfig `yaml:"grpc_tls_config"`
RegisterInstrumentation bool `yaml:"register_instrumentation"`
ExcludeRequestInLog bool `yaml:"-"`
DisableRequestSuccessLog bool `yaml:"-"`
ServerGracefulShutdownTimeout time.Duration `yaml:"graceful_shutdown_timeout"`
HTTPServerReadTimeout time.Duration `yaml:"http_server_read_timeout"`
HTTPServerWriteTimeout time.Duration `yaml:"http_server_write_timeout"`
HTTPServerIdleTimeout time.Duration `yaml:"http_server_idle_timeout"`
GRPCOptions []grpc.ServerOption `yaml:"-"`
GRPCMiddleware []grpc.UnaryServerInterceptor `yaml:"-"`
GRPCStreamMiddleware []grpc.StreamServerInterceptor `yaml:"-"`
HTTPMiddleware []middleware.Interface `yaml:"-"`
Router *mux.Router `yaml:"-"`
DoNotAddDefaultHTTPMiddleware bool `yaml:"-"`
RouteHTTPToGRPC bool `yaml:"-"`
GPRCServerMaxRecvMsgSize int `yaml:"grpc_server_max_recv_msg_size"`
GRPCServerMaxSendMsgSize int `yaml:"grpc_server_max_send_msg_size"`
GPRCServerMaxConcurrentStreams uint `yaml:"grpc_server_max_concurrent_streams"`
GRPCServerMaxConnectionIdle time.Duration `yaml:"grpc_server_max_connection_idle"`
GRPCServerMaxConnectionAge time.Duration `yaml:"grpc_server_max_connection_age"`
GRPCServerMaxConnectionAgeGrace time.Duration `yaml:"grpc_server_max_connection_age_grace"`
GRPCServerTime time.Duration `yaml:"grpc_server_keepalive_time"`
GRPCServerTimeout time.Duration `yaml:"grpc_server_keepalive_timeout"`
GRPCServerMinTimeBetweenPings time.Duration `yaml:"grpc_server_min_time_between_pings"`
GRPCServerPingWithoutStreamAllowed bool `yaml:"grpc_server_ping_without_stream_allowed"`
LogFormat logging.Format `yaml:"log_format"`
LogLevel logging.Level `yaml:"log_level"`
Log logging.Interface `yaml:"-"`
LogSourceIPs bool `yaml:"log_source_ips_enabled"`
LogSourceIPsHeader string `yaml:"log_source_ips_header"`
LogSourceIPsRegex string `yaml:"log_source_ips_regex"`
LogRequestHeaders bool `yaml:"log_request_headers"`
LogRequestAtInfoLevel bool `yaml:"log_request_at_info_level_enabled"`
LogRequestExcludeHeadersList string `yaml:"log_request_exclude_headers_list"`
// If not set, default signal handler is used.
SignalHandler SignalHandler `yaml:"-"`
// If not set, default Prometheus registry is used.
Registerer prometheus.Registerer `yaml:"-"`
Gatherer prometheus.Gatherer `yaml:"-"`
PathPrefix string `yaml:"http_path_prefix"`
}
var infinty = time.Duration(math.MaxInt64)
// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.HTTPListenAddress, "server.http-listen-address", "", "HTTP server listen address.")
f.StringVar(&cfg.HTTPListenNetwork, "server.http-listen-network", DefaultNetwork, "HTTP server listen network, default tcp")
f.StringVar(&cfg.CipherSuites, "server.tls-cipher-suites", "", "Comma-separated list of cipher suites to use. If blank, the default Go cipher suites is used.")
f.StringVar(&cfg.MinVersion, "server.tls-min-version", "", "Minimum TLS version to use. Allowed values: VersionTLS10, VersionTLS11, VersionTLS12, VersionTLS13. If blank, the Go TLS minimum version is used.")
f.StringVar(&cfg.HTTPTLSConfig.TLSCertPath, "server.http-tls-cert-path", "", "HTTP server cert path.")
f.StringVar(&cfg.HTTPTLSConfig.TLSKeyPath, "server.http-tls-key-path", "", "HTTP server key path.")
f.StringVar(&cfg.HTTPTLSConfig.ClientAuth, "server.http-tls-client-auth", "", "HTTP TLS Client Auth type.")
f.StringVar(&cfg.HTTPTLSConfig.ClientCAs, "server.http-tls-ca-path", "", "HTTP TLS Client CA path.")
f.StringVar(&cfg.GRPCTLSConfig.TLSCertPath, "server.grpc-tls-cert-path", "", "GRPC TLS server cert path.")
f.StringVar(&cfg.GRPCTLSConfig.TLSKeyPath, "server.grpc-tls-key-path", "", "GRPC TLS server key path.")
f.StringVar(&cfg.GRPCTLSConfig.ClientAuth, "server.grpc-tls-client-auth", "", "GRPC TLS Client Auth type.")
f.StringVar(&cfg.GRPCTLSConfig.ClientCAs, "server.grpc-tls-ca-path", "", "GRPC TLS Client CA path.")
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.")
f.IntVar(&cfg.HTTPConnLimit, "server.http-conn-limit", 0, "Maximum number of simultaneous http connections, <=0 to disable")
f.StringVar(&cfg.GRPCListenNetwork, "server.grpc-listen-network", DefaultNetwork, "gRPC server listen network")
f.StringVar(&cfg.GRPCListenAddress, "server.grpc-listen-address", "", "gRPC server listen address.")
f.IntVar(&cfg.GRPCListenPort, "server.grpc-listen-port", 9095, "gRPC server listen port.")
f.IntVar(&cfg.GRPCConnLimit, "server.grpc-conn-limit", 0, "Maximum number of simultaneous grpc connections, <=0 to disable")
f.BoolVar(&cfg.RegisterInstrumentation, "server.register-instrumentation", true, "Register the intrumentation handlers (/metrics etc).")
f.DurationVar(&cfg.ServerGracefulShutdownTimeout, "server.graceful-shutdown-timeout", 30*time.Second, "Timeout for graceful shutdowns")
f.DurationVar(&cfg.HTTPServerReadTimeout, "server.http-read-timeout", 30*time.Second, "Read timeout for HTTP server")
f.DurationVar(&cfg.HTTPServerWriteTimeout, "server.http-write-timeout", 30*time.Second, "Write timeout for HTTP server")
f.DurationVar(&cfg.HTTPServerIdleTimeout, "server.http-idle-timeout", 120*time.Second, "Idle timeout for HTTP server")
f.IntVar(&cfg.GPRCServerMaxRecvMsgSize, "server.grpc-max-recv-msg-size-bytes", 4*1024*1024, "Limit on the size of a gRPC message this server can receive (bytes).")
f.IntVar(&cfg.GRPCServerMaxSendMsgSize, "server.grpc-max-send-msg-size-bytes", 4*1024*1024, "Limit on the size of a gRPC message this server can send (bytes).")
f.UintVar(&cfg.GPRCServerMaxConcurrentStreams, "server.grpc-max-concurrent-streams", 100, "Limit on the number of concurrent streams for gRPC calls (0 = unlimited)")
f.DurationVar(&cfg.GRPCServerMaxConnectionIdle, "server.grpc.keepalive.max-connection-idle", infinty, "The duration after which an idle connection should be closed. Default: infinity")
f.DurationVar(&cfg.GRPCServerMaxConnectionAge, "server.grpc.keepalive.max-connection-age", infinty, "The duration for the maximum amount of time a connection may exist before it will be closed. Default: infinity")
f.DurationVar(&cfg.GRPCServerMaxConnectionAgeGrace, "server.grpc.keepalive.max-connection-age-grace", infinty, "An additive period after max-connection-age after which the connection will be forcibly closed. Default: infinity")
f.DurationVar(&cfg.GRPCServerTime, "server.grpc.keepalive.time", time.Hour*2, "Duration after which a keepalive probe is sent in case of no activity over the connection., Default: 2h")
f.DurationVar(&cfg.GRPCServerTimeout, "server.grpc.keepalive.timeout", time.Second*20, "After having pinged for keepalive check, the duration after which an idle connection should be closed, Default: 20s")
f.DurationVar(&cfg.GRPCServerMinTimeBetweenPings, "server.grpc.keepalive.min-time-between-pings", 5*time.Minute, "Minimum amount of time a client should wait before sending a keepalive ping. If client sends keepalive ping more often, server will send GOAWAY and close the connection.")
f.BoolVar(&cfg.GRPCServerPingWithoutStreamAllowed, "server.grpc.keepalive.ping-without-stream-allowed", false, "If true, server allows keepalive pings even when there are no active streams(RPCs). If false, and client sends ping when there are no active streams, server will send GOAWAY and close the connection.")
f.StringVar(&cfg.PathPrefix, "server.path-prefix", "", "Base path to serve all API routes from (e.g. /v1/)")
cfg.LogFormat.RegisterFlags(f)
cfg.LogLevel.RegisterFlags(f)
f.BoolVar(&cfg.LogSourceIPs, "server.log-source-ips-enabled", false, "Optionally log the source IPs.")
f.StringVar(&cfg.LogSourceIPsHeader, "server.log-source-ips-header", "", "Header field storing the source IPs. Only used if server.log-source-ips-enabled is true. If not set the default Forwarded, X-Real-IP and X-Forwarded-For headers are used")
f.StringVar(&cfg.LogSourceIPsRegex, "server.log-source-ips-regex", "", "Regex for matching the source IPs. Only used if server.log-source-ips-enabled is true. If not set the default Forwarded, X-Real-IP and X-Forwarded-For headers are used")
f.BoolVar(&cfg.LogRequestHeaders, "server.log-request-headers", false, "Optionally log request headers.")
f.StringVar(&cfg.LogRequestExcludeHeadersList, "server.log-request-headers-exclude-list", "", "Comma separated list of headers to exclude from loggin. Only used if server.log-request-headers is true.")
f.BoolVar(&cfg.LogRequestAtInfoLevel, "server.log-request-at-info-level-enabled", false, "Optionally log requests at info level instead of debug level. Applies to request headers as well if server.log-request-headers is enabled.")
}
func (cfg *Config) registererOrDefault() prometheus.Registerer {
// If user doesn't supply a Registerer/gatherer, use Prometheus' by default.
if cfg.Registerer != nil {
return cfg.Registerer
}
return prometheus.DefaultRegisterer
}
// Server wraps a HTTP and gRPC server, and some common initialization.
//
// Servers will be automatically instrumented for Prometheus metrics.
type Server struct {
cfg Config
handler SignalHandler
grpcListener net.Listener
httpListener net.Listener
// These fields are used to support grpc over the http server
// if RouteHTTPToGRPC is set. the fields are kept here
// so they can be initialized in New() and started in Run()
grpchttpmux cmux.CMux
grpcOnHTTPListener net.Listener
GRPCOnHTTPServer *grpc.Server
HTTP *mux.Router
HTTPServer *http.Server
GRPC *grpc.Server
Log logging.Interface
Registerer prometheus.Registerer
Gatherer prometheus.Gatherer
}
// New makes a new Server. It will panic if the metrics cannot be registered.
func New(cfg Config) (*Server, error) {
metrics := NewServerMetrics(cfg)
metrics.MustRegister(cfg.registererOrDefault())
return newServer(cfg, metrics)
}
// NewWithMetrics makes a new Server using the provided Metrics. It will not attempt to register the metrics,
// the user is responsible for doing so.
func NewWithMetrics(cfg Config, metrics *Metrics) (*Server, error) {
return newServer(cfg, metrics)
}
func newServer(cfg Config, metrics *Metrics) (*Server, error) {
// If user doesn't supply a logging implementation, by default instantiate
// logrus.
log := cfg.Log
if log == nil {
log = logging.NewLogrus(cfg.LogLevel)
}
gatherer := cfg.Gatherer
if gatherer == nil {
gatherer = prometheus.DefaultGatherer
}
network := cfg.HTTPListenNetwork
if network == "" {
network = DefaultNetwork
}
// Setup listeners first, so we can fail early if the port is in use.
httpListener, err := net.Listen(network, fmt.Sprintf("%s:%d", cfg.HTTPListenAddress, cfg.HTTPListenPort))
if err != nil {
return nil, err
}
httpListener = middleware.CountingListener(httpListener, metrics.TcpConnections.WithLabelValues("http"))
metrics.TcpConnectionsLimit.WithLabelValues("http").Set(float64(cfg.HTTPConnLimit))
if cfg.HTTPConnLimit > 0 {
httpListener = netutil.LimitListener(httpListener, cfg.HTTPConnLimit)
}
var grpcOnHTTPListener net.Listener
var grpchttpmux cmux.CMux
if cfg.RouteHTTPToGRPC {
grpchttpmux = cmux.New(httpListener)
httpListener = grpchttpmux.Match(cmux.HTTP1Fast())
grpcOnHTTPListener = grpchttpmux.Match(cmux.HTTP2())
}
network = cfg.GRPCListenNetwork
if network == "" {
network = DefaultNetwork
}
grpcListener, err := net.Listen(network, fmt.Sprintf("%s:%d", cfg.GRPCListenAddress, cfg.GRPCListenPort))
if err != nil {
return nil, err
}
grpcListener = middleware.CountingListener(grpcListener, metrics.TcpConnections.WithLabelValues("grpc"))
metrics.TcpConnectionsLimit.WithLabelValues("grpc").Set(float64(cfg.GRPCConnLimit))
if cfg.GRPCConnLimit > 0 {
grpcListener = netutil.LimitListener(grpcListener, cfg.GRPCConnLimit)
}
cipherSuites, err := stringToCipherSuites(cfg.CipherSuites)
if err != nil {
return nil, err
}
minVersion, err := stringToTLSVersion(cfg.MinVersion)
if err != nil {
return nil, err
}
// Setup TLS
var httpTLSConfig *tls.Config
if len(cfg.HTTPTLSConfig.TLSCertPath) > 0 && len(cfg.HTTPTLSConfig.TLSKeyPath) > 0 {
// Note: ConfigToTLSConfig from prometheus/exporter-toolkit is awaiting security review.
httpTLSConfig, err = web.ConfigToTLSConfig(&web.TLSConfig{
TLSCertPath: cfg.HTTPTLSConfig.TLSCertPath,
TLSKeyPath: cfg.HTTPTLSConfig.TLSKeyPath,
ClientAuth: cfg.HTTPTLSConfig.ClientAuth,
ClientCAs: cfg.HTTPTLSConfig.ClientCAs,
CipherSuites: cipherSuites,
MinVersion: minVersion,
})
if err != nil {
return nil, fmt.Errorf("error generating http tls config: %v", err)
}
}
var grpcTLSConfig *tls.Config
if len(cfg.GRPCTLSConfig.TLSCertPath) > 0 && len(cfg.GRPCTLSConfig.TLSKeyPath) > 0 {
// Note: ConfigToTLSConfig from prometheus/exporter-toolkit is awaiting security review.
grpcTLSConfig, err = web.ConfigToTLSConfig(&web.TLSConfig{
TLSCertPath: cfg.GRPCTLSConfig.TLSCertPath,
TLSKeyPath: cfg.GRPCTLSConfig.TLSKeyPath,
ClientAuth: cfg.GRPCTLSConfig.ClientAuth,
ClientCAs: cfg.GRPCTLSConfig.ClientCAs,
CipherSuites: cipherSuites,
MinVersion: minVersion,
})
if err != nil {
return nil, fmt.Errorf("error generating grpc tls config: %v", err)
}
}
log.WithField("http", httpListener.Addr()).WithField("grpc", grpcListener.Addr()).Infof("server listening on addresses")
// Setup gRPC server
serverLog := middleware.GRPCServerLog{
Log: log,
WithRequest: !cfg.ExcludeRequestInLog,
DisableRequestSuccessLog: cfg.DisableRequestSuccessLog,
}
grpcMiddleware := []grpc.UnaryServerInterceptor{
serverLog.UnaryServerInterceptor,
otgrpc.OpenTracingServerInterceptor(opentracing.GlobalTracer()),
middleware.UnaryServerInstrumentInterceptor(metrics.RequestDuration),
}
grpcMiddleware = append(grpcMiddleware, cfg.GRPCMiddleware...)
grpcStreamMiddleware := []grpc.StreamServerInterceptor{
serverLog.StreamServerInterceptor,
otgrpc.OpenTracingStreamServerInterceptor(opentracing.GlobalTracer()),
middleware.StreamServerInstrumentInterceptor(metrics.RequestDuration),
}
grpcStreamMiddleware = append(grpcStreamMiddleware, cfg.GRPCStreamMiddleware...)
grpcKeepAliveOptions := keepalive.ServerParameters{
MaxConnectionIdle: cfg.GRPCServerMaxConnectionIdle,
MaxConnectionAge: cfg.GRPCServerMaxConnectionAge,
MaxConnectionAgeGrace: cfg.GRPCServerMaxConnectionAgeGrace,
Time: cfg.GRPCServerTime,
Timeout: cfg.GRPCServerTimeout,
}
grpcKeepAliveEnforcementPolicy := keepalive.EnforcementPolicy{
MinTime: cfg.GRPCServerMinTimeBetweenPings,
PermitWithoutStream: cfg.GRPCServerPingWithoutStreamAllowed,
}
grpcOptions := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(grpcMiddleware...),
grpc.ChainStreamInterceptor(grpcStreamMiddleware...),
grpc.KeepaliveParams(grpcKeepAliveOptions),
grpc.KeepaliveEnforcementPolicy(grpcKeepAliveEnforcementPolicy),
grpc.MaxRecvMsgSize(cfg.GPRCServerMaxRecvMsgSize),
grpc.MaxSendMsgSize(cfg.GRPCServerMaxSendMsgSize),
grpc.MaxConcurrentStreams(uint32(cfg.GPRCServerMaxConcurrentStreams)),
grpc.StatsHandler(middleware.NewStatsHandler(
metrics.ReceivedMessageSize,
metrics.SentMessageSize,
metrics.InflightRequests,
)),
}
grpcOptions = append(grpcOptions, cfg.GRPCOptions...)
if grpcTLSConfig != nil {
grpcCreds := credentials.NewTLS(grpcTLSConfig)
grpcOptions = append(grpcOptions, grpc.Creds(grpcCreds))
}
grpcServer := grpc.NewServer(grpcOptions...)
grpcOnHttpServer := grpc.NewServer(grpcOptions...)
// Setup HTTP server
var router *mux.Router
if cfg.Router != nil {
router = cfg.Router
} else {
router = mux.NewRouter()
}
if cfg.PathPrefix != "" {
// Expect metrics and pprof handlers to be prefixed with server's path prefix.
// e.g. /loki/metrics or /loki/debug/pprof
router = router.PathPrefix(cfg.PathPrefix).Subrouter()
}
if cfg.RegisterInstrumentation {
RegisterInstrumentationWithGatherer(router, gatherer)
}
var sourceIPs *middleware.SourceIPExtractor
if cfg.LogSourceIPs {
sourceIPs, err = middleware.NewSourceIPs(cfg.LogSourceIPsHeader, cfg.LogSourceIPsRegex)
if err != nil {
return nil, fmt.Errorf("error setting up source IP extraction: %v", err)
}
}
defaultLogMiddleware := middleware.NewLogMiddleware(log, cfg.LogRequestHeaders, cfg.LogRequestAtInfoLevel, sourceIPs, strings.Split(cfg.LogRequestExcludeHeadersList, ","))
defaultLogMiddleware.DisableRequestSuccessLog = cfg.DisableRequestSuccessLog
defaultHTTPMiddleware := []middleware.Interface{
middleware.Tracer{
RouteMatcher: router,
SourceIPs: sourceIPs,
},
defaultLogMiddleware,
middleware.Instrument{
RouteMatcher: router,
Duration: metrics.RequestDuration,
RequestBodySize: metrics.ReceivedMessageSize,
ResponseBodySize: metrics.SentMessageSize,
InflightRequests: metrics.InflightRequests,
},
}
var httpMiddleware []middleware.Interface
if cfg.DoNotAddDefaultHTTPMiddleware {
httpMiddleware = cfg.HTTPMiddleware
} else {
httpMiddleware = append(defaultHTTPMiddleware, cfg.HTTPMiddleware...)
}
httpServer := &http.Server{
ReadTimeout: cfg.HTTPServerReadTimeout,
WriteTimeout: cfg.HTTPServerWriteTimeout,
IdleTimeout: cfg.HTTPServerIdleTimeout,
Handler: middleware.Merge(httpMiddleware...).Wrap(router),
}
if httpTLSConfig != nil {
httpServer.TLSConfig = httpTLSConfig
}
handler := cfg.SignalHandler
if handler == nil {
handler = signals.NewHandler(log)
}
return &Server{
cfg: cfg,
httpListener: httpListener,
grpcListener: grpcListener,
grpcOnHTTPListener: grpcOnHTTPListener,
handler: handler,
grpchttpmux: grpchttpmux,
HTTP: router,
HTTPServer: httpServer,
GRPC: grpcServer,
GRPCOnHTTPServer: grpcOnHttpServer,
Log: log,
Registerer: cfg.registererOrDefault(),
Gatherer: gatherer,
}, nil
}
// RegisterInstrumentation on the given router.
func RegisterInstrumentation(router *mux.Router) {
RegisterInstrumentationWithGatherer(router, prometheus.DefaultGatherer)
}
// RegisterInstrumentationWithGatherer on the given router.
func RegisterInstrumentationWithGatherer(router *mux.Router, gatherer prometheus.Gatherer) {
router.Handle("/metrics", promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
router.PathPrefix("/debug/pprof").Handler(http.DefaultServeMux)
}
// Run the server; blocks until SIGTERM (if signal handling is enabled), an error is received, or Stop() is called.
func (s *Server) Run() error {
errChan := make(chan error, 1)
// Wait for a signal
go func() {
s.handler.Loop()
select {
case errChan <- nil:
default:
}
}()
go func() {
var err error
if s.HTTPServer.TLSConfig == nil {
err = s.HTTPServer.Serve(s.httpListener)
} else {
err = s.HTTPServer.ServeTLS(s.httpListener, s.cfg.HTTPTLSConfig.TLSCertPath, s.cfg.HTTPTLSConfig.TLSKeyPath)
}
if err == http.ErrServerClosed {
err = nil
}
select {
case errChan <- err:
default:
}
}()
// Setup gRPC server
// for HTTP over gRPC, ensure we don't double-count the middleware
httpgrpc.RegisterHTTPServer(s.GRPC, httpgrpc_server.NewServer(s.HTTP))
go func() {
err := s.GRPC.Serve(s.grpcListener)
handleGRPCError(err, errChan)
}()
// grpchttpmux will only be set if grpchttpmux RouteHTTPToGRPC is set
if s.grpchttpmux != nil {
go func() {
err := s.grpchttpmux.Serve()
handleGRPCError(err, errChan)
}()
go func() {
err := s.GRPCOnHTTPServer.Serve(s.grpcOnHTTPListener)
handleGRPCError(err, errChan)
}()
}
return <-errChan
}
// handleGRPCError consolidates GRPC Server error handling by sending
// any error to errChan except for grpc.ErrServerStopped which is ignored.
func handleGRPCError(err error, errChan chan error) {
if err == grpc.ErrServerStopped {
err = nil
}
select {
case errChan <- err:
default:
}
}
// HTTPListenAddr exposes `net.Addr` that `Server` is listening to for HTTP connections.
func (s *Server) HTTPListenAddr() net.Addr {
return s.httpListener.Addr()
}
// GRPCListenAddr exposes `net.Addr` that `Server` is listening to for GRPC connections.
func (s *Server) GRPCListenAddr() net.Addr {
return s.grpcListener.Addr()
}
// Stop unblocks Run().
func (s *Server) Stop() {
s.handler.Stop()
}
// Shutdown the server, gracefully. Should be defered after New().
func (s *Server) Shutdown() {
ctx, cancel := context.WithTimeout(context.Background(), s.cfg.ServerGracefulShutdownTimeout)
defer cancel() // releases resources if httpServer.Shutdown completes before timeout elapses
s.HTTPServer.Shutdown(ctx)
s.GRPC.GracefulStop()
}