-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathhttp.go
83 lines (71 loc) · 2.53 KB
/
http.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
// Copyright (c) 2020 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0
package server
import (
"context"
"net"
"net/http"
"github.com/gorilla/mux"
"go.opentelemetry.io/collector/config/confighttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/jaegertracing/jaeger/cmd/collector/app/handler"
samplinghttp "github.com/jaegertracing/jaeger/internal/sampling/http"
"github.com/jaegertracing/jaeger/internal/sampling/samplingstrategy"
"github.com/jaegertracing/jaeger/pkg/healthcheck"
"github.com/jaegertracing/jaeger/pkg/httpmetrics"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/pkg/recoveryhandler"
"github.com/jaegertracing/jaeger/pkg/telemetry"
)
// HTTPServerParams to construct a new Jaeger Collector HTTP Server
type HTTPServerParams struct {
confighttp.ServerConfig
Handler handler.JaegerBatchesHandler
SamplingProvider samplingstrategy.Provider
MetricsFactory metrics.Factory
HealthCheck *healthcheck.HealthCheck
Logger *zap.Logger
}
// StartHTTPServer based on the given parameters
func StartHTTPServer(params *HTTPServerParams) (*http.Server, error) {
params.Logger.Info("Starting jaeger-collector HTTP server", zap.String("host-port", params.Endpoint))
listener, err := params.ToListener(context.Background())
if err != nil {
return nil, err
}
errorLog, _ := zap.NewStdLogAt(params.Logger, zapcore.ErrorLevel)
server, err := params.ToServer(context.Background(), nil, telemetry.NoopSettings().ToOtelComponent(),
nil)
server.ErrorLog = errorLog
if err != nil {
return nil, err
}
serveHTTP(server, listener, params)
return server, nil
}
func serveHTTP(server *http.Server, listener net.Listener, params *HTTPServerParams) {
r := mux.NewRouter()
apiHandler := handler.NewAPIHandler(params.Handler)
apiHandler.RegisterRoutes(r)
cfgHandler := samplinghttp.NewHandler(samplinghttp.HandlerParams{
ConfigManager: &samplinghttp.ConfigManager{
SamplingProvider: params.SamplingProvider,
},
MetricsFactory: params.MetricsFactory,
BasePath: "/api",
LegacySamplingEndpoint: false,
})
cfgHandler.RegisterRoutes(r)
recoveryHandler := recoveryhandler.NewRecoveryHandler(params.Logger, true)
server.Handler = httpmetrics.Wrap(recoveryHandler(r), params.MetricsFactory, params.Logger)
go func() {
err := server.Serve(listener)
if err != nil {
if err != http.ErrServerClosed {
params.Logger.Error("Could not start HTTP collector", zap.Error(err))
}
}
params.HealthCheck.Set(healthcheck.Unavailable)
}()
}