forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
153 lines (131 loc) · 4.67 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
// Copyright (c) 2019 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/gorilla/handlers"
"github.com/opentracing/opentracing-go"
"github.com/soheilhy/cmux"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/jaegertracing/jaeger/cmd/flags"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/pkg/healthcheck"
"github.com/jaegertracing/jaeger/pkg/recoveryhandler"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
)
// Server runs HTTP, Mux and a grpc server
type Server struct {
svc *flags.Service
querySvc *querysvc.QueryService
queryOptions *QueryOptions
tracer opentracing.Tracer // TODO make part of flags.Service
conn net.Listener
grpcServer *grpc.Server
httpServer *http.Server
}
// NewServer creates and initializes Server
func NewServer(svc *flags.Service, querySvc *querysvc.QueryService, options *QueryOptions, tracer opentracing.Tracer) *Server {
return &Server{
svc: svc,
querySvc: querySvc,
queryOptions: options,
tracer: tracer,
grpcServer: createGRPCServer(querySvc, svc.Logger, tracer),
httpServer: createHTTPServer(querySvc, options, tracer, svc.Logger),
}
}
func createGRPCServer(querySvc *querysvc.QueryService, logger *zap.Logger, tracer opentracing.Tracer) *grpc.Server {
srv := grpc.NewServer()
handler := NewGRPCHandler(querySvc, logger, tracer)
api_v2.RegisterQueryServiceServer(srv, handler)
return srv
}
func createHTTPServer(querySvc *querysvc.QueryService, queryOpts *QueryOptions, tracer opentracing.Tracer, logger *zap.Logger) *http.Server {
apiHandlerOptions := []HandlerOption{
HandlerOptions.Logger(logger),
HandlerOptions.Tracer(tracer),
}
apiHandler := NewAPIHandler(
querySvc,
apiHandlerOptions...)
r := NewRouter()
if queryOpts.BasePath != "/" {
r = r.PathPrefix(queryOpts.BasePath).Subrouter()
}
apiHandler.RegisterRoutes(r)
RegisterStaticHandler(r, logger, queryOpts)
var handler http.Handler = r
if queryOpts.BearerTokenPropagation {
handler = bearerTokenPropagationHandler(logger, r)
}
handler = handlers.CompressHandler(handler)
recoveryHandler := recoveryhandler.NewRecoveryHandler(logger, true)
return &http.Server{
Handler: recoveryHandler(handler),
}
}
// Start http, GRPC and cmux servers concurrently
func (s *Server) Start() error {
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", s.queryOptions.Port))
if err != nil {
return err
}
s.conn = conn
// cmux server acts as a reverse-proxy between HTTP and GRPC backends.
cmuxServer := cmux.New(s.conn)
grpcListener := cmuxServer.MatchWithWriters(
cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"),
cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc+proto"),
)
httpListener := cmuxServer.Match(cmux.Any())
go func() {
s.svc.Logger.Info("Starting HTTP server", zap.Int("port", s.queryOptions.Port))
switch err := s.httpServer.Serve(httpListener); err {
case nil, http.ErrServerClosed, cmux.ErrListenerClosed:
// normal exit, nothing to do
default:
s.svc.Logger.Error("Could not start HTTP server", zap.Error(err))
}
s.svc.SetHealthCheckStatus(healthcheck.Unavailable)
}()
// Start GRPC server concurrently
go func() {
s.svc.Logger.Info("Starting GRPC server", zap.Int("port", s.queryOptions.Port))
if err := s.grpcServer.Serve(grpcListener); err != nil {
s.svc.Logger.Error("Could not start GRPC server", zap.Error(err))
}
s.svc.SetHealthCheckStatus(healthcheck.Unavailable)
}()
// Start cmux server concurrently.
go func() {
s.svc.Logger.Info("Starting CMUX server", zap.Int("port", s.queryOptions.Port))
err := cmuxServer.Serve()
// TODO: Remove string comparison when https://github.com/soheilhy/cmux/pull/69 is merged
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
s.svc.Logger.Error("Could not start multiplexed server", zap.Error(err))
}
s.svc.SetHealthCheckStatus(healthcheck.Unavailable)
}()
return nil
}
// Close stops http, GRPC servers and closes the port listener.
func (s *Server) Close() {
s.grpcServer.Stop()
s.httpServer.Close()
s.conn.Close()
}