-
Notifications
You must be signed in to change notification settings - Fork 111
/
server.go
98 lines (83 loc) · 3.04 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
package net
import (
"fmt"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"github.com/grafana/alloy/internal/alloy/logging/level"
dskit "github.com/grafana/dskit/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
)
// TargetServer is wrapper around dskit.Server that handles some common
// configuration used in all components that expose a network server. It just
// handles configuration and initialization, the handlers implementation are
// left to the consumer.
type TargetServer struct {
logger log.Logger
config *dskit.Config
metricsNamespace string
server *dskit.Server
}
// NewTargetServer creates a new TargetServer, applying some defaults to the server configuration.
// If provided config is nil, a default configuration will be used instead.
func NewTargetServer(logger log.Logger, metricsNamespace string, reg prometheus.Registerer, config *ServerConfig) (*TargetServer, error) {
if !model.IsValidMetricName(model.LabelValue(metricsNamespace)) {
return nil, fmt.Errorf("metrics namespace is not prometheus compatible: %s", metricsNamespace)
}
ts := &TargetServer{
logger: logger,
metricsNamespace: metricsNamespace,
}
if config == nil {
config = DefaultServerConfig()
}
// convert from Alloy into the dskit config
serverCfg := config.convert()
// Set the config to the new combined config.
// Avoid logging entire received request on failures
serverCfg.ExcludeRequestInLog = true
// Configure dedicated metrics registerer
serverCfg.Registerer = reg
// Persist crafter config in server
ts.config = &serverCfg
// To prevent metric collisions because all metrics are going to be registered in the global Prometheus registry.
ts.config.MetricsNamespace = ts.metricsNamespace
// We don't want the /debug and /metrics endpoints running, since this is not
// the main HTTP server. We want this target to expose the least surface area
// possible, hence disabling dskit HTTP server metrics and debugging
// functionality.
ts.config.RegisterInstrumentation = false
// Add logger to dskit
ts.config.Log = ts.logger
return ts, nil
}
// MountAndRun mounts the handlers and starting the server.
func (ts *TargetServer) MountAndRun(mountRoute func(router *mux.Router)) error {
level.Info(ts.logger).Log("msg", "starting server")
srv, err := dskit.New(*ts.config)
if err != nil {
return err
}
ts.server = srv
mountRoute(ts.server.HTTP)
go func() {
err := srv.Run()
if err != nil {
level.Error(ts.logger).Log("msg", "server shutdown with error", "err", err)
}
}()
return nil
}
// HTTPListenAddr returns the listen address of the HTTP server.
func (ts *TargetServer) HTTPListenAddr() string {
return ts.server.HTTPListenAddr().String()
}
// GRPCListenAddr returns the listen address of the gRPC server.
func (ts *TargetServer) GRPCListenAddr() string {
return ts.server.GRPCListenAddr().String()
}
// StopAndShutdown stops and shuts down the underlying server.
func (ts *TargetServer) StopAndShutdown() {
ts.server.Stop()
ts.server.Shutdown()
}