Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/common/net/server.go
4096 views
1
package net
2
3
import (
4
"fmt"
5
6
"github.com/go-kit/log"
7
"github.com/go-kit/log/level"
8
"github.com/gorilla/mux"
9
"github.com/prometheus/client_golang/prometheus"
10
"github.com/prometheus/common/model"
11
"github.com/weaveworks/common/logging"
12
weaveworks "github.com/weaveworks/common/server"
13
)
14
15
// TargetServer is wrapper around weaveworks.Server that handles some common configuration used in all flow components
16
// that expose a network server. It just handles configuration and initialization, the handlers implementation are left
17
// to the consumer.
18
type TargetServer struct {
19
logger log.Logger
20
config *weaveworks.Config
21
metricsNamespace string
22
server *weaveworks.Server
23
}
24
25
// NewTargetServer creates a new TargetServer, applying some defaults to the server configuration.
26
// If provided config is nil, a default configuration will be used instead.
27
func NewTargetServer(logger log.Logger, metricsNamespace string, reg prometheus.Registerer, config *ServerConfig) (*TargetServer, error) {
28
if !model.IsValidMetricName(model.LabelValue(metricsNamespace)) {
29
return nil, fmt.Errorf("metrics namespace is not prometheus compatiible: %s", metricsNamespace)
30
}
31
32
ts := &TargetServer{
33
logger: logger,
34
metricsNamespace: metricsNamespace,
35
}
36
37
if config == nil {
38
config = &ServerConfig{}
39
}
40
41
// convert from River into the weaveworks config
42
serverCfg := config.Convert()
43
// Set the config to the new combined config.
44
// Avoid logging entire received request on failures
45
serverCfg.ExcludeRequestInLog = true
46
// Configure dedicated metrics registerer
47
serverCfg.Registerer = reg
48
// Persist crafter config in server
49
ts.config = &serverCfg
50
// To prevent metric collisions because all metrics are going to be registered in the global Prometheus registry.
51
ts.config.MetricsNamespace = ts.metricsNamespace
52
// We don't want the /debug and /metrics endpoints running, since this is not the main Flow HTTP server.
53
// We want this target to expose the least surface area possible, hence disabling WeaveWorks HTTP server metrics
54
// and debugging functionality.
55
ts.config.RegisterInstrumentation = false
56
// Add logger to weaveworks
57
ts.config.Log = logging.GoKit(ts.logger)
58
59
return ts, nil
60
}
61
62
// MountAndRun mounts the handlers and starting the server.
63
func (ts *TargetServer) MountAndRun(mountRoute func(router *mux.Router)) error {
64
level.Info(ts.logger).Log("msg", "starting server")
65
srv, err := weaveworks.New(*ts.config)
66
if err != nil {
67
return err
68
}
69
70
ts.server = srv
71
mountRoute(ts.server.HTTP)
72
73
go func() {
74
err := srv.Run()
75
if err != nil {
76
level.Error(ts.logger).Log("msg", "server shutdown with error", "err", err)
77
}
78
}()
79
80
return nil
81
}
82
83
// HTTPListenAddr returns the listen address of the HTTP server.
84
func (ts *TargetServer) HTTPListenAddr() string {
85
return ts.server.HTTPListenAddr().String()
86
}
87
88
// GRPCListenAddr returns the listen address of the gRPC server.
89
func (ts *TargetServer) GRPCListenAddr() string {
90
return ts.server.GRPCListenAddr().String()
91
}
92
93
// StopAndShutdown stops and shuts down the underlying server.
94
func (ts *TargetServer) StopAndShutdown() {
95
ts.server.Stop()
96
ts.server.Shutdown()
97
}
98
99