-
Notifications
You must be signed in to change notification settings - Fork 450
/
main.go
262 lines (247 loc) · 8.12 KB
/
main.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
gokitlog "github.com/go-kit/log"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/discovery"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/collector"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/prehook"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/server"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
allocatorWatcher "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/watcher"
)
var (
setupLog = ctrl.Log.WithName("setup")
eventsMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "opentelemetry_allocator_events",
Help: "Number of events in the channel.",
}, []string{"source"})
)
func main() {
var (
// allocatorPrehook will be nil if filterStrategy is not set or
// unrecognized. No filtering will be used in this case.
allocatorPrehook prehook.Hook
allocator allocation.Allocator
discoveryManager *discovery.Manager
collectorWatcher *collector.Watcher
promWatcher allocatorWatcher.Watcher
targetDiscoverer *target.Discoverer
discoveryCancel context.CancelFunc
runGroup run.Group
eventChan = make(chan allocatorWatcher.Event)
eventCloser = make(chan bool, 1)
interrupts = make(chan os.Signal, 1)
errChan = make(chan error)
)
cfg, err := config.Load()
if err != nil {
fmt.Printf("Failed to load config: %v", err)
os.Exit(1)
}
ctrl.SetLogger(cfg.RootLogger)
if validationErr := config.ValidateConfig(cfg); validationErr != nil {
setupLog.Error(validationErr, "Invalid configuration")
os.Exit(1)
}
cfg.RootLogger.Info("Starting the Target Allocator")
ctx := context.Background()
log := ctrl.Log.WithName("allocator")
allocatorPrehook = prehook.New(cfg.FilterStrategy, log)
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocation.WithFilter(allocatorPrehook))
if err != nil {
setupLog.Error(err, "Unable to initialize allocation strategy")
os.Exit(1)
}
httpOptions := []server.Option{}
if cfg.HTTPS.Enabled {
tlsConfig, confErr := cfg.HTTPS.NewTLSConfig()
if confErr != nil {
setupLog.Error(confErr, "Unable to initialize TLS configuration")
os.Exit(1)
}
httpOptions = append(httpOptions, server.WithTLSConfig(tlsConfig, cfg.HTTPS.ListenAddr))
}
srv := server.NewServer(log, allocator, cfg.ListenAddr, httpOptions...)
discoveryCtx, discoveryCancel := context.WithCancel(ctx)
sdMetrics, err := discovery.CreateAndRegisterSDMetrics(prometheus.DefaultRegisterer)
if err != nil {
setupLog.Error(err, "Unable to register metrics for Prometheus service discovery")
os.Exit(1)
}
discoveryManager = discovery.NewManager(discoveryCtx, gokitlog.NewNopLogger(), prometheus.DefaultRegisterer, sdMetrics)
targetDiscoverer = target.NewDiscoverer(log, discoveryManager, allocatorPrehook, srv)
collectorWatcher, collectorWatcherErr := collector.NewCollectorWatcher(log, cfg.ClusterConfig)
if collectorWatcherErr != nil {
setupLog.Error(collectorWatcherErr, "Unable to initialize collector watcher")
os.Exit(1)
}
signal.Notify(interrupts, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer close(interrupts)
if cfg.PrometheusCR.Enabled {
promWatcher, err = allocatorWatcher.NewPrometheusCRWatcher(ctx, setupLog.WithName("prometheus-cr-watcher"), *cfg)
if err != nil {
setupLog.Error(err, "Can't start the prometheus watcher")
os.Exit(1)
}
// apply the initial configuration
promConfig, loadErr := promWatcher.LoadConfig(ctx)
if loadErr != nil {
setupLog.Error(err, "Can't load initial Prometheus configuration from Prometheus CRs")
os.Exit(1)
}
loadErr = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, promConfig.ScrapeConfigs)
if loadErr != nil {
setupLog.Error(err, "Can't load initial scrape targets from Prometheus CRs")
os.Exit(1)
}
runGroup.Add(
func() error {
promWatcherErr := promWatcher.Watch(eventChan, errChan)
setupLog.Info("Prometheus watcher exited")
return promWatcherErr
},
func(_ error) {
setupLog.Info("Closing prometheus watcher")
promWatcherErr := promWatcher.Close()
if promWatcherErr != nil {
setupLog.Error(promWatcherErr, "prometheus watcher failed to close")
}
})
}
runGroup.Add(
func() error {
discoveryManagerErr := discoveryManager.Run()
setupLog.Info("Discovery manager exited")
return discoveryManagerErr
},
func(_ error) {
setupLog.Info("Closing discovery manager")
discoveryCancel()
})
runGroup.Add(
func() error {
// Initial loading of the config file's scrape config
if cfg.PromConfig != nil && len(cfg.PromConfig.ScrapeConfigs) > 0 {
err = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourceConfigMap, cfg.PromConfig.ScrapeConfigs)
if err != nil {
setupLog.Error(err, "Unable to apply initial configuration")
return err
}
} else {
setupLog.Info("Prometheus config empty, skipping initial discovery configuration")
}
err := targetDiscoverer.Watch(allocator.SetTargets)
setupLog.Info("Target discoverer exited")
return err
},
func(_ error) {
setupLog.Info("Closing target discoverer")
targetDiscoverer.Close()
})
runGroup.Add(
func() error {
err := collectorWatcher.Watch(cfg.CollectorSelector, allocator.SetCollectors)
setupLog.Info("Collector watcher exited")
return err
},
func(_ error) {
setupLog.Info("Closing collector watcher")
collectorWatcher.Close()
})
runGroup.Add(
func() error {
err := srv.Start()
setupLog.Info("Server failed to start")
return err
},
func(_ error) {
setupLog.Info("Closing server")
if shutdownErr := srv.Shutdown(ctx); shutdownErr != nil {
setupLog.Error(shutdownErr, "Error on server shutdown")
}
})
if cfg.HTTPS.Enabled {
runGroup.Add(
func() error {
err := srv.StartHTTPS()
setupLog.Info("HTTPS Server failed to start")
return err
},
func(_ error) {
setupLog.Info("Closing HTTPS server")
if shutdownErr := srv.ShutdownHTTPS(ctx); shutdownErr != nil {
setupLog.Error(shutdownErr, "Error on HTTPS server shutdown")
}
})
}
runGroup.Add(
func() error {
for {
select {
case event := <-eventChan:
eventsMetric.WithLabelValues(event.Source.String()).Inc()
loadConfig, err := event.Watcher.LoadConfig(ctx)
if err != nil {
setupLog.Error(err, "Unable to load configuration")
continue
}
err = targetDiscoverer.ApplyConfig(event.Source, loadConfig.ScrapeConfigs)
if err != nil {
setupLog.Error(err, "Unable to apply configuration")
continue
}
case err := <-errChan:
setupLog.Error(err, "Watcher error")
case <-eventCloser:
return nil
}
}
},
func(_ error) {
setupLog.Info("Closing watcher loop")
close(eventCloser)
})
runGroup.Add(
func() error {
for {
select {
case <-interrupts:
setupLog.Info("Received interrupt")
return nil
case <-eventCloser:
return nil
}
}
},
func(_ error) {
setupLog.Info("Closing interrupt loop")
})
if runErr := runGroup.Run(); runErr != nil {
setupLog.Error(runErr, "run group exited")
}
setupLog.Info("Target allocator exited.")
}