-
Notifications
You must be signed in to change notification settings - Fork 107
/
forwarder.go
356 lines (310 loc) · 10.4 KB
/
forwarder.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package forwarder
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
trace "go.opentelemetry.io/otel/trace"
"github.com/prometheus/client_golang/prometheus"
clientmodel "github.com/prometheus/client_model/go"
"github.com/openshift/telemeter/pkg/authorize"
telemeterhttp "github.com/openshift/telemeter/pkg/http"
"github.com/openshift/telemeter/pkg/metricfamily"
"github.com/openshift/telemeter/pkg/metricsclient"
)
type RuleMatcher interface {
MatchRules() []string
}
var (
gaugeFederateSamples = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "federate_samples",
Help: "Tracks the number of samples per federation",
})
gaugeFederateFilteredSamples = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "federate_filtered_samples",
Help: "Tracks the number of samples filtered per federation",
})
federateErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "federate_requests_failed_total",
Help: "Number of times the client failed to forward metrics",
})
federateTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "federate_requests_total",
Help: "Number of times the client forwarded metrics",
})
)
func init() {
prometheus.MustRegister(
federateErrors, federateTotal, gaugeFederateSamples, gaugeFederateFilteredSamples,
)
}
// Config defines the parameters that can be used to configure a worker.
// The only required field is `From`.
type Config struct {
From *url.URL
ToAuthorize *url.URL
ToUpload *url.URL
FromToken string
ToToken string
FromTokenFile string
ToTokenFile string
FromCAFile string
TLSCertFile string
TLSKey string
AnonymizeLabels []string
AnonymizeSalt string
AnonymizeSaltFile string
Debug bool
Interval time.Duration
LimitBytes int64
Rules []string
RulesFile string
Transformer metricfamily.Transformer
Logger log.Logger
Tracer trace.TracerProvider
}
// Worker represents a metrics forwarding agent. It collects metrics from a source URL and forwards them to a sink.
// A Worker should be configured with a `Config` and instantiated with the `New` func.
// Workers are thread safe; all access to shared fields are synchronized.
type Worker struct {
fromClient *metricsclient.Client
toClient *metricsclient.Client
from *url.URL
to *url.URL
interval time.Duration
transformer metricfamily.Transformer
rules []string
lastMetrics []*clientmodel.MetricFamily
lock sync.Mutex
reconfigure chan struct{}
logger log.Logger
}
// New creates a new Worker based on the provided Config. If the Config contains invalid
// values, then an error is returned.
func New(cfg Config) (*Worker, error) {
if cfg.From == nil {
return nil, errors.New("a URL from which to scrape is required")
}
logger := log.With(cfg.Logger, "component", "forwarder")
w := Worker{
from: cfg.From,
interval: cfg.Interval,
reconfigure: make(chan struct{}),
to: cfg.ToUpload,
logger: log.With(cfg.Logger, "component", "forwarder/worker"),
}
if w.interval == 0 {
w.interval = 4*time.Minute + 30*time.Second
}
// Configure the anonymization.
anonymizeSalt := cfg.AnonymizeSalt
if len(cfg.AnonymizeSalt) == 0 && len(cfg.AnonymizeSaltFile) > 0 {
data, err := ioutil.ReadFile(cfg.AnonymizeSaltFile)
if err != nil {
return nil, fmt.Errorf("failed to read anonymize-salt-file: %v", err)
}
anonymizeSalt = strings.TrimSpace(string(data))
}
if len(cfg.AnonymizeLabels) != 0 && len(anonymizeSalt) == 0 {
return nil, fmt.Errorf("anonymize-salt must be specified if anonymize-labels is set")
}
if len(cfg.AnonymizeLabels) == 0 {
level.Warn(logger).Log("msg", "not anonymizing any labels")
}
// Configure a transformer.
var transformer metricfamily.MultiTransformer
if cfg.Transformer != nil {
transformer.With(cfg.Transformer)
}
if len(cfg.AnonymizeLabels) > 0 {
transformer.With(metricfamily.NewMetricsAnonymizer(anonymizeSalt, cfg.AnonymizeLabels, nil))
}
// Create the `fromClient`.
fromTransport := metricsclient.DefaultTransport()
fromClient := &http.Client{Transport: fromTransport}
if len(cfg.FromCAFile) > 0 {
level.Debug(logger).Log("msg", "TLS configuration", "ca_file", cfg.FromCAFile, "cert_file", cfg.TLSCertFile, "key_file", cfg.TLSKey)
var cert tls.Certificate
var err error
if fromTransport.TLSClientConfig == nil {
fromTransport.TLSClientConfig = &tls.Config{}
}
if cfg.TLSCertFile != "" && cfg.TLSKey != "" {
cert, err = tls.LoadX509KeyPair(*&cfg.TLSCertFile, *&cfg.TLSKey)
if err != nil {
return nil, fmt.Errorf("creating client x509 keypair from cert file %s and key file %s: %w", cfg.TLSCertFile, cfg.TLSKey, err)
}
}
pool, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to read system certificates: %v", err)
}
data, err := ioutil.ReadFile(cfg.FromCAFile)
if err != nil {
return nil, fmt.Errorf("failed to read from-ca-file: %v", err)
}
if !pool.AppendCertsFromPEM(data) {
level.Warn(logger).Log("msg", "no certs found in from-ca-file")
}
fromTransport.TLSClientConfig.Certificates = []tls.Certificate{cert}
fromTransport.TLSClientConfig.RootCAs = pool
}
if cfg.Debug {
level.Debug(logger).Log("msg", "enabling the debug round tripper for the fromClient transport")
fromClient.Transport = telemeterhttp.NewDebugRoundTripper(logger, fromClient.Transport)
}
if len(cfg.FromToken) == 0 && len(cfg.FromTokenFile) > 0 {
level.Debug(logger).Log("msg", "enabling the token file round tripper for the fromClient transport")
data, err := ioutil.ReadFile(cfg.FromTokenFile)
if err != nil {
return nil, fmt.Errorf("unable to read from-token-file: %v", err)
}
cfg.FromToken = strings.TrimSpace(string(data))
}
if len(cfg.FromToken) > 0 {
level.Debug(logger).Log("msg", "enabling the token round tripper for the fromClient transport")
fromClient.Transport = telemeterhttp.NewBearerRoundTripper(cfg.FromToken, fromClient.Transport)
}
w.fromClient = metricsclient.New(logger, fromClient, cfg.LimitBytes, w.interval, "federate_from")
// Create the `toClient`.
toTransport := metricsclient.DefaultTransport()
toTransport.Proxy = http.ProxyFromEnvironment
toClient := &http.Client{Transport: otelhttp.NewTransport(toTransport, otelhttp.WithTracerProvider(cfg.Tracer))}
if cfg.Debug {
toClient.Transport = telemeterhttp.NewDebugRoundTripper(logger, toClient.Transport)
}
if len(cfg.ToToken) == 0 && len(cfg.ToTokenFile) > 0 {
data, err := ioutil.ReadFile(cfg.ToTokenFile)
if err != nil {
return nil, fmt.Errorf("unable to read to-token-file: %v", err)
}
cfg.ToToken = strings.TrimSpace(string(data))
}
if (len(cfg.ToToken) > 0) != (cfg.ToAuthorize != nil) {
return nil, errors.New("an authorization URL and authorization token must both specified or empty")
}
if len(cfg.ToToken) > 0 {
// Exchange our token for a token from the authorize endpoint, which also gives us a
// set of expected labels we must include.
rt := authorize.NewServerRotatingRoundTripper(cfg.ToToken, cfg.ToAuthorize, toClient.Transport)
toClient.Transport = rt
transformer.With(metricfamily.NewLabel(nil, rt))
}
w.toClient = metricsclient.New(logger, toClient, cfg.LimitBytes, w.interval, "federate_to")
w.transformer = transformer
// Configure the matching rules.
rules := cfg.Rules
if len(cfg.RulesFile) > 0 {
data, err := ioutil.ReadFile(cfg.RulesFile)
if err != nil {
return nil, fmt.Errorf("unable to read match-file: %v", err)
}
rules = append(rules, strings.Split(string(data), "\n")...)
}
for i := 0; i < len(rules); {
s := strings.TrimSpace(rules[i])
if len(s) == 0 {
rules = append(rules[:i], rules[i+1:]...)
continue
}
rules[i] = s
i++
}
w.rules = rules
return &w, nil
}
// Reconfigure temporarily stops a worker and reconfigures is with the provided Config.
// Is thread safe and can run concurrently with `LastMetrics` and `Run`.
func (w *Worker) Reconfigure(cfg Config) error {
worker, err := New(cfg)
if err != nil {
return fmt.Errorf("failed to reconfigure: %v", err)
}
w.lock.Lock()
defer w.lock.Unlock()
w.fromClient = worker.fromClient
w.toClient = worker.toClient
w.interval = worker.interval
w.from = worker.from
w.to = worker.to
w.transformer = worker.transformer
w.rules = worker.rules
// Signal a restart to Run func.
// Do this in a goroutine since we do not care if restarting the Run loop is asynchronous.
go func() { w.reconfigure <- struct{}{} }()
return nil
}
func (w *Worker) LastMetrics() []*clientmodel.MetricFamily {
w.lock.Lock()
defer w.lock.Unlock()
return w.lastMetrics
}
func (w *Worker) Run(ctx context.Context) {
for {
// Ensure that the Worker does not access critical configuration during a reconfiguration.
w.lock.Lock()
wait := w.interval
// The critical section ends here.
w.lock.Unlock()
federateTotal.Inc()
if err := w.forward(ctx); err != nil {
federateErrors.Inc()
level.Error(w.logger).Log("msg", "unable to forward results", "err", err)
wait = time.Minute
}
select {
// If the context is cancelled, then we're done.
case <-ctx.Done():
return
case <-time.After(wait):
// We want to be able to interrupt a sleep to immediately apply a new configuration.
case <-w.reconfigure:
}
}
}
func (w *Worker) forward(ctx context.Context) error {
w.lock.Lock()
defer w.lock.Unlock()
// Load the match rules each time.
from := w.from
// reset query from last invocation, otherwise match rules will be appended
w.from.RawQuery = ""
v := from.Query()
for _, rule := range w.rules {
v.Add("match[]", rule)
}
from.RawQuery = v.Encode()
req := &http.Request{Method: "GET", URL: from}
families, err := w.fromClient.Retrieve(ctx, req)
if err != nil {
return err
}
before := metricfamily.MetricsCount(families)
if err := metricfamily.Filter(families, w.transformer); err != nil {
return err
}
families = metricfamily.Pack(families)
after := metricfamily.MetricsCount(families)
gaugeFederateSamples.Set(float64(before))
gaugeFederateFilteredSamples.Set(float64(before - after))
w.lastMetrics = families
if len(families) == 0 {
level.Warn(w.logger).Log("msg", "no metrics to send, doing nothing")
return nil
}
if w.to == nil {
return nil
}
req = &http.Request{Method: "POST", URL: w.to}
return w.toClient.Send(ctx, req, families)
}