/
system_metrics_agent.go
159 lines (128 loc) · 3.56 KB
/
system_metrics_agent.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
package app
import (
"context"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"sync"
"time"
"code.cloudfoundry.org/system-metrics-release/src/pkg/collector"
"code.cloudfoundry.org/system-metrics-release/src/pkg/egress/stats"
"code.cloudfoundry.org/tlsconfig"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const statOrigin = "system_metrics_agent"
type SystemMetricsAgent struct {
cfg Config
log *log.Logger
debugLis net.Listener
metricsLis net.Listener
metricsServer http.Server
mu sync.Mutex
inputFunc collector.InputFunc
}
func NewSystemMetricsAgent(i collector.InputFunc, cfg Config, log *log.Logger) *SystemMetricsAgent {
return &SystemMetricsAgent{
cfg: cfg,
log: log,
inputFunc: i,
}
}
func (a *SystemMetricsAgent) Run() {
a.startDebugServer()
metricsURL := fmt.Sprintf(":%d", a.cfg.MetricPort)
a.startMetricsServer(metricsURL)
}
func (a *SystemMetricsAgent) MetricsAddr() string {
a.mu.Lock()
defer a.mu.Unlock()
if a.metricsLis == nil {
return ""
}
return a.metricsLis.Addr().String()
}
func (a *SystemMetricsAgent) DebugAddr() string {
a.mu.Lock()
defer a.mu.Unlock()
if a.debugLis == nil {
return ""
}
return a.debugLis.Addr().String()
}
func (a *SystemMetricsAgent) Shutdown(ctx context.Context) {
a.mu.Lock()
defer a.mu.Unlock()
if a.debugLis != nil {
a.debugLis.Close()
}
err := a.metricsServer.Shutdown(ctx)
if err != nil {
a.log.Printf("failed to shutdown: %s\n", err)
}
}
func (a *SystemMetricsAgent) startDebugServer() {
a.mu.Lock()
defer a.mu.Unlock()
var err error
a.debugLis, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", a.cfg.DebugPort))
if err != nil {
a.log.Panicf("failed to start debug listener: %s\n", err)
}
go func() {
err := http.Serve(a.debugLis, nil)
if err != nil {
a.log.Printf("failed to serve: %s\n", err)
}
}()
}
func (a *SystemMetricsAgent) startMetricsServer(addr string) {
labels := map[string]string{
"source_id": statOrigin,
"deployment": a.cfg.Deployment,
"job": a.cfg.Job,
"index": a.cfg.Index,
"ip": a.cfg.IP,
}
promRegisterer := prometheus.NewRegistry()
promRegistry := stats.NewPromRegistry(promRegisterer)
promSender := stats.NewPromSender(promRegistry, statOrigin, a.cfg.LimitedMetrics, labels)
router := http.NewServeMux()
router.Handle("/metrics", promhttp.HandlerFor(promRegisterer, promhttp.HandlerOpts{}))
a.setup(addr, router)
go collector.NewProcessor(
a.inputFunc,
[]collector.StatsSender{promSender},
a.cfg.SampleInterval,
a.log,
).Run()
log.Printf("Metrics server closing: %s", a.metricsServer.ServeTLS(a.metricsLis, "", ""))
}
func (a *SystemMetricsAgent) setup(addr string, router *http.ServeMux) {
a.mu.Lock()
defer a.mu.Unlock()
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(a.cfg.CertPath, a.cfg.KeyPath),
).Server(
tlsconfig.WithClientAuthenticationFromFile(a.cfg.CACertPath),
)
if err != nil {
log.Fatalf("Unable to setup tls for metrics endpoint (%s): %s", addr, err)
}
a.metricsServer = http.Server{
Addr: addr,
ReadTimeout: 5 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
Handler: router,
TLSConfig: tlsConfig,
}
a.metricsLis, err = net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Unable to setup metrics endpoint (%s): %s", addr, err)
}
log.Printf("Metrics endpoint is listening on %s", a.metricsLis.Addr().String())
}