-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
server.go
414 lines (361 loc) · 12.1 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
// Copyright (c) 2019,2020 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0
package app
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"github.com/soheilhy/cmux"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"github.com/jaegertracing/jaeger/cmd/query/app/apiv3"
"github.com/jaegertracing/jaeger/cmd/query/app/internal/api_v3"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/pkg/bearertoken"
"github.com/jaegertracing/jaeger/pkg/netutils"
"github.com/jaegertracing/jaeger/pkg/recoveryhandler"
"github.com/jaegertracing/jaeger/pkg/telemetery"
"github.com/jaegertracing/jaeger/pkg/tenancy"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
"github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
)
// Server runs HTTP, Mux and a grpc server
type Server struct {
querySvc *querysvc.QueryService
queryOptions *QueryOptions
conn net.Listener
grpcConn net.Listener
httpConn net.Listener
cmuxServer cmux.CMux
grpcServer *grpc.Server
httpServer *httpServer
separatePorts bool
bgFinished sync.WaitGroup
telemetery.Setting
}
// NewServer creates and initializes Server
func NewServer(
ctx context.Context,
querySvc *querysvc.QueryService,
metricsQuerySvc querysvc.MetricsQueryService,
options *QueryOptions,
tm *tenancy.Manager,
telset telemetery.Setting,
) (*Server, error) {
_, httpPort, err := net.SplitHostPort(options.HTTP.Endpoint)
if err != nil {
return nil, fmt.Errorf("invalid HTTP server host:port: %w", err)
}
_, grpcPort, err := net.SplitHostPort(options.GRPC.NetAddr.Endpoint)
if err != nil {
return nil, fmt.Errorf("invalid gRPC server host:port: %w", err)
}
separatePorts := grpcPort != httpPort || grpcPort == "0" || httpPort == "0"
if (options.HTTP.TLSSetting != nil || options.GRPC.TLSSetting != nil) && !separatePorts {
return nil, errors.New("server with TLS enabled can not use same host ports for gRPC and HTTP. Use dedicated HTTP and gRPC host ports instead")
}
grpcServer, err := createGRPCServer(ctx, options, tm, telset)
if err != nil {
return nil, err
}
registerGRPCHandlers(grpcServer, querySvc, metricsQuerySvc, telset)
httpServer, err := createHTTPServer(ctx, querySvc, metricsQuerySvc, options, tm, telset)
if err != nil {
return nil, err
}
return &Server{
querySvc: querySvc,
queryOptions: options,
grpcServer: grpcServer,
httpServer: httpServer,
separatePorts: separatePorts,
Setting: telset,
}, nil
}
func registerGRPCHandlers(
server *grpc.Server,
querySvc *querysvc.QueryService,
metricsQuerySvc querysvc.MetricsQueryService,
telset telemetery.Setting,
) {
reflection.Register(server)
handler := NewGRPCHandler(querySvc, metricsQuerySvc, GRPCHandlerOptions{
Logger: telset.Logger,
})
healthServer := health.NewServer()
api_v2.RegisterQueryServiceServer(server, handler)
metrics.RegisterMetricsQueryServiceServer(server, handler)
api_v3.RegisterQueryServiceServer(server, &apiv3.Handler{QueryService: querySvc})
healthServer.SetServingStatus("jaeger.api_v2.QueryService", grpc_health_v1.HealthCheckResponse_SERVING)
healthServer.SetServingStatus("jaeger.api_v2.metrics.MetricsQueryService", grpc_health_v1.HealthCheckResponse_SERVING)
healthServer.SetServingStatus("jaeger.api_v3.QueryService", grpc_health_v1.HealthCheckResponse_SERVING)
grpc_health_v1.RegisterHealthServer(server, healthServer)
}
func createGRPCServer(
ctx context.Context,
options *QueryOptions,
tm *tenancy.Manager,
telset telemetery.Setting,
) (*grpc.Server, error) {
var grpcOpts []configgrpc.ToServerOption
unaryInterceptors := []grpc.UnaryServerInterceptor{
bearertoken.NewUnaryServerInterceptor(),
}
streamInterceptors := []grpc.StreamServerInterceptor{
bearertoken.NewStreamServerInterceptor(),
}
//nolint:contextcheck
if tm.Enabled {
unaryInterceptors = append(unaryInterceptors, tenancy.NewGuardingUnaryInterceptor(tm))
streamInterceptors = append(streamInterceptors, tenancy.NewGuardingStreamInterceptor(tm))
}
grpcOpts = append(grpcOpts,
configgrpc.WithGrpcServerOption(grpc.ChainUnaryInterceptor(unaryInterceptors...)),
configgrpc.WithGrpcServerOption(grpc.ChainStreamInterceptor(streamInterceptors...)),
)
return options.GRPC.ToServer(
ctx,
telset.Host,
component.TelemetrySettings{
Logger: telset.Logger,
TracerProvider: telset.TracerProvider,
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider {
return noop.NewMeterProvider()
},
},
grpcOpts...)
}
type httpServer struct {
*http.Server
staticHandlerCloser io.Closer
}
var _ io.Closer = (*httpServer)(nil)
func initRouter(
querySvc *querysvc.QueryService,
metricsQuerySvc querysvc.MetricsQueryService,
queryOpts *QueryOptions,
tenancyMgr *tenancy.Manager,
telset telemetery.Setting,
) (http.Handler, io.Closer) {
apiHandlerOptions := []HandlerOption{
HandlerOptions.Logger(telset.Logger),
HandlerOptions.Tracer(telset.TracerProvider),
HandlerOptions.MetricsQueryService(metricsQuerySvc),
}
apiHandler := NewAPIHandler(
querySvc,
apiHandlerOptions...)
r := NewRouter()
if queryOpts.BasePath != "/" {
r = r.PathPrefix(queryOpts.BasePath).Subrouter()
}
(&apiv3.HTTPGateway{
QueryService: querySvc,
Logger: telset.Logger,
Tracer: telset.TracerProvider,
}).RegisterRoutes(r)
apiHandler.RegisterRoutes(r)
staticHandlerCloser := RegisterStaticHandler(r, telset.Logger, queryOpts, querySvc.GetCapabilities())
var handler http.Handler = r
if queryOpts.BearerTokenPropagation {
handler = bearertoken.PropagationHandler(telset.Logger, handler)
}
if tenancyMgr.Enabled {
handler = tenancy.ExtractTenantHTTPHandler(tenancyMgr, handler)
}
handler = traceResponseHandler(handler)
return handler, staticHandlerCloser
}
func createHTTPServer(
ctx context.Context,
querySvc *querysvc.QueryService,
metricsQuerySvc querysvc.MetricsQueryService,
queryOpts *QueryOptions,
tm *tenancy.Manager,
telset telemetery.Setting,
) (*httpServer, error) {
handler, staticHandlerCloser := initRouter(querySvc, metricsQuerySvc, queryOpts, tm, telset)
handler = recoveryhandler.NewRecoveryHandler(telset.Logger, true)(handler)
hs, err := queryOpts.HTTP.ToServer(
ctx,
telset.Host,
component.TelemetrySettings{
Logger: telset.Logger,
TracerProvider: telset.TracerProvider,
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider {
return noop.NewMeterProvider()
},
},
handler,
)
if err != nil {
return nil, errors.Join(err, staticHandlerCloser.Close())
}
server := &httpServer{
Server: hs,
staticHandlerCloser: staticHandlerCloser,
}
// TODO why doesn't OTEL helper do that already?
if queryOpts.HTTP.TLSSetting != nil {
tlsCfg, err := queryOpts.HTTP.TLSSetting.LoadTLSConfig(ctx) // This checks if the certificates are correctly provided
if err != nil {
return nil, errors.Join(err, staticHandlerCloser.Close())
}
server.TLSConfig = tlsCfg
}
return server, nil
}
func (hS httpServer) Close() error {
var errs []error
errs = append(errs, hS.Server.Close())
errs = append(errs, hS.staticHandlerCloser.Close())
return errors.Join(errs...)
}
// initListener initialises listeners of the server
func (s *Server) initListener(ctx context.Context) (cmux.CMux, error) {
if s.separatePorts { // use separate ports and listeners each for gRPC and HTTP requests
var err error
s.grpcConn, err = s.queryOptions.GRPC.NetAddr.Listen(ctx)
if err != nil {
return nil, err
}
s.httpConn, err = s.queryOptions.HTTP.ToListener(ctx)
if err != nil {
return nil, err
}
s.Logger.Info(
"Query server started",
zap.String("http_addr", s.HTTPAddr()),
zap.String("grpc_addr", s.GRPCAddr()),
)
return nil, nil
}
// old behavior using cmux
conn, err := net.Listen("tcp", s.queryOptions.HTTP.Endpoint)
if err != nil {
return nil, err
}
s.conn = conn
var tcpPort int
if port, err := netutils.GetPort(s.conn.Addr()); err == nil {
tcpPort = port
}
s.Logger.Info(
"Query server started",
zap.Int("port", tcpPort),
zap.String("addr", s.queryOptions.HTTP.Endpoint))
// cmux server acts as a reverse-proxy between HTTP and GRPC backends.
cmuxServer := cmux.New(s.conn)
s.grpcConn = cmuxServer.MatchWithWriters(
cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"),
cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc+proto"),
)
s.httpConn = cmuxServer.Match(cmux.Any())
return cmuxServer, nil
}
// Start http, GRPC and cmux servers concurrently
func (s *Server) Start(ctx context.Context) error {
cmuxServer, err := s.initListener(ctx)
if err != nil {
return fmt.Errorf("query server failed to initialize listener: %w", err)
}
s.cmuxServer = cmuxServer
var tcpPort int
if !s.separatePorts {
if port, err := netutils.GetPort(s.conn.Addr()); err == nil {
tcpPort = port
}
}
var httpPort int
if port, err := netutils.GetPort(s.httpConn.Addr()); err == nil {
httpPort = port
}
var grpcPort int
if port, err := netutils.GetPort(s.grpcConn.Addr()); err == nil {
grpcPort = port
}
s.bgFinished.Add(1)
go func() {
defer s.bgFinished.Done()
s.Logger.Info("Starting HTTP server", zap.Int("port", httpPort), zap.String("addr", s.queryOptions.HTTP.Endpoint))
var err error
if s.queryOptions.HTTP.TLSSetting != nil {
err = s.httpServer.ServeTLS(s.httpConn, "", "")
} else {
err = s.httpServer.Serve(s.httpConn)
}
if err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, cmux.ErrListenerClosed) && !errors.Is(err, cmux.ErrServerClosed) {
s.Logger.Error("Could not start HTTP server", zap.Error(err))
s.ReportStatus(componentstatus.NewFatalErrorEvent(err))
return
}
s.Logger.Info("HTTP server stopped", zap.Int("port", httpPort), zap.String("addr", s.queryOptions.HTTP.Endpoint))
}()
// Start GRPC server concurrently
s.bgFinished.Add(1)
go func() {
defer s.bgFinished.Done()
s.Logger.Info("Starting GRPC server", zap.Int("port", grpcPort), zap.String("addr", s.queryOptions.GRPC.NetAddr.Endpoint))
err := s.grpcServer.Serve(s.grpcConn)
if err != nil && !errors.Is(err, cmux.ErrListenerClosed) && !errors.Is(err, cmux.ErrServerClosed) {
s.Logger.Error("Could not start GRPC server", zap.Error(err))
s.ReportStatus(componentstatus.NewFatalErrorEvent(err))
return
}
s.Logger.Info("GRPC server stopped", zap.Int("port", grpcPort), zap.String("addr", s.queryOptions.GRPC.NetAddr.Endpoint))
}()
// Start cmux server concurrently.
if !s.separatePorts {
s.bgFinished.Add(1)
go func() {
defer s.bgFinished.Done()
s.Logger.Info("Starting CMUX server", zap.Int("port", tcpPort), zap.String("addr", s.queryOptions.HTTP.Endpoint))
err := cmuxServer.Serve()
// TODO: find a way to avoid string comparison. Even though cmux has ErrServerClosed, it's not returned here.
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
s.Logger.Error("Could not start multiplexed server", zap.Error(err))
s.ReportStatus(componentstatus.NewFatalErrorEvent(err))
return
}
s.Logger.Info("CMUX server stopped", zap.Int("port", tcpPort), zap.String("addr", s.queryOptions.HTTP.Endpoint))
}()
}
return nil
}
func (s *Server) HTTPAddr() string {
return s.httpConn.Addr().String()
}
func (s *Server) GRPCAddr() string {
return s.grpcConn.Addr().String()
}
// Close stops HTTP, GRPC servers and closes the port listener.
func (s *Server) Close() error {
var errs []error
s.Logger.Info("Closing HTTP server")
if err := s.httpServer.Close(); err != nil {
errs = append(errs, fmt.Errorf("failed to close HTTP server: %w", err))
}
s.Logger.Info("Stopping gRPC server")
s.grpcServer.Stop()
if !s.separatePorts {
s.Logger.Info("Closing CMux server")
s.cmuxServer.Close()
}
s.bgFinished.Wait()
s.Logger.Info("Server stopped")
return errors.Join(errs...)
}