/
filetargetmanager.go
512 lines (440 loc) · 14.6 KB
/
filetargetmanager.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package file
import (
"context"
"fmt"
"os"
"strings"
"sync"
"github.com/bmatcuk/doublestar"
"github.com/fsnotify/fsnotify"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/kubernetes"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/grafana/loki/v3/clients/pkg/logentry/stages"
"github.com/grafana/loki/v3/clients/pkg/promtail/api"
"github.com/grafana/loki/v3/clients/pkg/promtail/positions"
"github.com/grafana/loki/v3/clients/pkg/promtail/scrapeconfig"
"github.com/grafana/loki/v3/clients/pkg/promtail/targets/target"
"github.com/grafana/loki/v3/pkg/util"
)
const (
pathLabel = "__path__"
pathExcludeLabel = "__path_exclude__"
hostLabel = "__host__"
kubernetesPodNodeField = "spec.nodeName"
)
// FileTargetManager manages a set of targets.
// nolint:revive
type FileTargetManager struct {
log log.Logger
quit context.CancelFunc
syncers map[string]*targetSyncer
manager *discovery.Manager
watcher *fsnotify.Watcher
targetEventHandler chan fileTargetEvent
wg sync.WaitGroup
}
// NewFileTargetManager creates a new TargetManager.
func NewFileTargetManager(
metrics *Metrics,
logger log.Logger,
positions positions.Positions,
client api.EntryHandler,
scrapeConfigs []scrapeconfig.Config,
targetConfig *Config,
watchConfig WatchConfig,
) (*FileTargetManager, error) {
reg := metrics.reg
if reg == nil {
reg = prometheus.DefaultRegisterer
}
noopRegistry := util.NoopRegistry{}
noopSdMetrics, err := discovery.CreateAndRegisterSDMetrics(noopRegistry)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
ctx, quit := context.WithCancel(context.Background())
tm := &FileTargetManager{
log: logger,
quit: quit,
watcher: watcher,
targetEventHandler: make(chan fileTargetEvent),
syncers: map[string]*targetSyncer{},
manager: discovery.NewManager(
ctx,
log.With(logger, "component", "discovery"),
noopRegistry,
noopSdMetrics,
),
}
hostname, err := hostname()
if err != nil {
return nil, err
}
configs := map[string]discovery.Configs{}
for _, cfg := range scrapeConfigs {
if !cfg.HasServiceDiscoveryConfig() {
continue
}
pipeline, err := stages.NewPipeline(log.With(logger, "component", "file_pipeline"), cfg.PipelineStages, &cfg.JobName, reg)
if err != nil {
return nil, err
}
// Add Source value to the static config target groups for unique identification
// within scrape pool. Also, default target label to localhost if target is not
// defined in promtail config.
// Just to make sure prometheus target group sync works fine.
for i, tg := range cfg.ServiceDiscoveryConfig.StaticConfigs {
tg.Source = fmt.Sprintf("%d", i)
if len(tg.Targets) == 0 {
tg.Targets = []model.LabelSet{
{model.AddressLabel: "localhost"},
}
}
}
// Add an additional api-level node filtering, so we only fetch pod metadata for
// all the pods from the current node. Without this filtering we will have to
// download metadata for all pods running on a cluster, which may be a long operation.
for _, kube := range cfg.ServiceDiscoveryConfig.KubernetesSDConfigs {
if kube.Role == kubernetes.RolePod {
kube.Selectors = tm.fulfillKubePodSelector(kube.Selectors, hostname)
}
}
s := &targetSyncer{
metrics: metrics,
log: logger,
positions: positions,
relabelConfig: cfg.RelabelConfigs,
targets: map[string]*FileTarget{},
droppedTargets: []target.Target{},
hostname: hostname,
entryHandler: pipeline.Wrap(client),
targetConfig: targetConfig,
watchConfig: watchConfig,
fileEventWatchers: map[string]chan fsnotify.Event{},
encoding: cfg.Encoding,
decompressCfg: cfg.DecompressionCfg,
}
tm.syncers[cfg.JobName] = s
configs[cfg.JobName] = cfg.ServiceDiscoveryConfig.Configs()
}
tm.wg.Add(3)
go tm.run(ctx)
go tm.watchTargetEvents(ctx)
go tm.watchFsEvents(ctx)
go util.LogError("running target manager", tm.manager.Run)
return tm, tm.manager.ApplyConfig(configs)
}
func (tm *FileTargetManager) watchTargetEvents(ctx context.Context) {
defer tm.wg.Done()
for {
select {
case event := <-tm.targetEventHandler:
switch event.eventType {
case fileTargetEventWatchStart:
if err := tm.watcher.Add(event.path); err != nil {
level.Error(tm.log).Log("msg", "error adding directory to watcher", "error", err)
}
case fileTargetEventWatchStop:
if err := tm.watcher.Remove(event.path); err != nil {
if !errors.Is(err, fsnotify.ErrNonExistentWatch) {
level.Error(tm.log).Log("msg", " failed to remove directory from watcher", "error", err)
}
}
}
case <-ctx.Done():
return
}
}
}
func (tm *FileTargetManager) watchFsEvents(ctx context.Context) {
defer tm.wg.Done()
for {
select {
case event := <-tm.watcher.Events:
// we only care about Create events
if event.Op == fsnotify.Create {
level.Info(tm.log).Log("msg", "received file watcher event", "name", event.Name, "op", event.Op.String())
for _, s := range tm.syncers {
s.sendFileCreateEvent(event)
}
}
case err := <-tm.watcher.Errors:
level.Error(tm.log).Log("msg", "error from fswatch", "error", err)
case <-ctx.Done():
return
}
}
}
func (tm *FileTargetManager) run(ctx context.Context) {
defer tm.wg.Done()
for {
select {
case targetGroups := <-tm.manager.SyncCh():
for jobName, groups := range targetGroups {
tm.syncers[jobName].sync(groups, tm.targetEventHandler)
}
case <-ctx.Done():
return
}
}
}
// Ready if there's at least one file target
func (tm *FileTargetManager) Ready() bool {
for _, s := range tm.syncers {
if s.ready() {
return true
}
}
return false
}
// Stop the TargetManager.
func (tm *FileTargetManager) Stop() {
tm.quit()
tm.wg.Wait()
for _, s := range tm.syncers {
s.stop()
}
util.LogError("closing watcher", tm.watcher.Close)
close(tm.targetEventHandler)
}
// ActiveTargets returns the active targets currently being scraped.
func (tm *FileTargetManager) ActiveTargets() map[string][]target.Target {
result := map[string][]target.Target{}
for jobName, syncer := range tm.syncers {
result[jobName] = append(result[jobName], syncer.ActiveTargets()...)
}
return result
}
// AllTargets returns all targets, active and dropped.
func (tm *FileTargetManager) AllTargets() map[string][]target.Target {
result := map[string][]target.Target{}
for jobName, syncer := range tm.syncers {
result[jobName] = append(result[jobName], syncer.ActiveTargets()...)
result[jobName] = append(result[jobName], syncer.DroppedTargets()...)
}
return result
}
func (tm *FileTargetManager) fulfillKubePodSelector(selectors []kubernetes.SelectorConfig, host string) []kubernetes.SelectorConfig {
nodeSelector := fmt.Sprintf("%s=%s", kubernetesPodNodeField, host)
if len(selectors) == 0 {
return []kubernetes.SelectorConfig{{Role: kubernetes.RolePod, Field: nodeSelector}}
}
for _, selector := range selectors {
if selector.Field == "" {
selector.Field = nodeSelector
} else if !strings.Contains(selector.Field, nodeSelector) {
selector.Field += "," + nodeSelector
}
selector.Role = kubernetes.RolePod
}
return selectors
}
// targetSyncer sync targets based on service discovery changes.
type targetSyncer struct {
metrics *Metrics
log log.Logger
positions positions.Positions
entryHandler api.EntryHandler
hostname string
fileEventWatchers map[string]chan fsnotify.Event
droppedTargets []target.Target
targets map[string]*FileTarget
mtx sync.Mutex
relabelConfig []*relabel.Config
targetConfig *Config
watchConfig WatchConfig
decompressCfg *scrapeconfig.DecompressionConfig
encoding string
}
// sync synchronize target based on received target groups received by service discovery
func (s *targetSyncer) sync(groups []*targetgroup.Group, targetEventHandler chan fileTargetEvent) {
s.mtx.Lock()
defer s.mtx.Unlock()
targetsSeen := map[string]struct{}{}
dropped := []target.Target{}
for _, group := range groups {
for _, t := range group.Targets {
level.Debug(s.log).Log("msg", "new target", "labels", t)
discoveredLabels := group.Labels.Merge(t)
var labelMap = make(map[string]string)
for k, v := range discoveredLabels.Clone() {
labelMap[string(k)] = string(v)
}
processedLabels, keep := relabel.Process(labels.FromMap(labelMap), s.relabelConfig...)
var labels = make(model.LabelSet)
for k, v := range processedLabels.Map() {
labels[model.LabelName(k)] = model.LabelValue(v)
}
// Drop targets if instructed to drop in relabeling.
if !keep {
dropped = append(dropped, target.NewDroppedTarget("dropped target", discoveredLabels))
level.Debug(s.log).Log("msg", "dropped target")
s.metrics.failedTargets.WithLabelValues("dropped").Inc()
continue
}
host, ok := labels[hostLabel]
if ok && string(host) != s.hostname {
dropped = append(dropped, target.NewDroppedTarget(fmt.Sprintf("ignoring target, wrong host (labels:%s hostname:%s)", labels.String(), s.hostname), discoveredLabels))
level.Debug(s.log).Log("msg", "ignoring target, wrong host", "labels", labels.String(), "hostname", s.hostname)
s.metrics.failedTargets.WithLabelValues("wrong_host").Inc()
continue
}
path, ok := labels[pathLabel]
if !ok {
dropped = append(dropped, target.NewDroppedTarget("no path for target", discoveredLabels))
level.Info(s.log).Log("msg", "no path for target", "labels", labels.String())
s.metrics.failedTargets.WithLabelValues("no_path").Inc()
continue
}
pathExclude := labels[pathExcludeLabel]
for k := range labels {
if strings.HasPrefix(string(k), "__") {
delete(labels, k)
}
}
key := fmt.Sprintf("%s:%s", path, labels.String())
if pathExclude != "" {
key = fmt.Sprintf("%s:%s", key, pathExclude)
}
targetsSeen[key] = struct{}{}
if _, ok := s.targets[key]; ok {
dropped = append(dropped, target.NewDroppedTarget("ignoring target, already exists", discoveredLabels))
level.Debug(s.log).Log("msg", "ignoring target, already exists", "labels", labels.String())
s.metrics.failedTargets.WithLabelValues("exists").Inc()
continue
}
level.Info(s.log).Log("msg", "Adding target", "key", key)
wkey := string(path)
watcher, ok := s.fileEventWatchers[wkey]
if !ok {
watcher = make(chan fsnotify.Event)
s.fileEventWatchers[wkey] = watcher
}
t, err := s.newTarget(wkey, string(pathExclude), labels, discoveredLabels, watcher, targetEventHandler)
if err != nil {
dropped = append(dropped, target.NewDroppedTarget(fmt.Sprintf("Failed to create target: %s", err.Error()), discoveredLabels))
level.Error(s.log).Log("msg", "Failed to create target", "key", key, "error", err)
s.metrics.failedTargets.WithLabelValues("error").Inc()
continue
}
s.metrics.targetsActive.Add(1.)
s.targets[key] = t
}
}
s.droppedTargets = dropped
// keep track of how many targets are using a fileEventWatcher
watcherUseCount := make(map[string]int, len(s.fileEventWatchers))
for _, target := range s.targets {
if _, ok := watcherUseCount[target.path]; !ok {
watcherUseCount[target.path] = 1
} else {
watcherUseCount[target.path]++
}
}
// remove existing targets not seen in groups arg; cleanup unused fileEventWatchers
for key, target := range s.targets {
if _, ok := targetsSeen[key]; !ok {
level.Info(s.log).Log("msg", "Removing target", "key", key)
target.Stop()
s.metrics.targetsActive.Add(-1.)
delete(s.targets, key)
// close related file event watcher if no other targets are using
k := target.path
_, ok := watcherUseCount[k]
if !ok {
level.Warn(s.log).Log("msg", "failed to find file event watcher", "path", k)
continue
} else { //nolint:revive
if watcherUseCount[k]--; watcherUseCount[k] > 0 {
// Multiple targets are using this file watcher, leave it alone
continue
}
}
if _, ok := s.fileEventWatchers[k]; ok {
close(s.fileEventWatchers[k])
delete(s.fileEventWatchers, k)
} else {
level.Warn(s.log).Log("msg", "failed to remove file event watcher", "path", k)
}
}
}
}
// sendFileCreateEvent sends file creation events to only the targets with matched path.
func (s *targetSyncer) sendFileCreateEvent(event fsnotify.Event) {
// Lock the mutex because other threads are manipulating s.fileEventWatchers which can lead to a deadlock
// where we send events to channels where nobody is listening anymore
s.mtx.Lock()
defer s.mtx.Unlock()
for path, watcher := range s.fileEventWatchers {
matched, err := doublestar.Match(path, event.Name)
if err != nil {
level.Error(s.log).Log("msg", "failed to match file", "error", err, "filename", event.Name)
continue
}
if !matched {
level.Debug(s.log).Log("msg", "new file does not match glob", "filename", event.Name)
continue
}
watcher <- event
}
}
func (s *targetSyncer) newTarget(path, pathExclude string, labels model.LabelSet, discoveredLabels model.LabelSet, fileEventWatcher chan fsnotify.Event, targetEventHandler chan fileTargetEvent) (*FileTarget, error) {
return NewFileTarget(s.metrics, s.log, s.entryHandler, s.positions, path, pathExclude, labels, discoveredLabels, s.targetConfig, s.watchConfig, fileEventWatcher, targetEventHandler, s.encoding, s.decompressCfg)
}
func (s *targetSyncer) DroppedTargets() []target.Target {
s.mtx.Lock()
defer s.mtx.Unlock()
return append([]target.Target(nil), s.droppedTargets...)
}
func (s *targetSyncer) ActiveTargets() []target.Target {
s.mtx.Lock()
defer s.mtx.Unlock()
actives := []target.Target{}
for _, t := range s.targets {
actives = append(actives, t)
}
return actives
}
func (s *targetSyncer) ready() bool {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, target := range s.targets {
if target.Ready() {
return true
}
}
return false
}
func (s *targetSyncer) stop() {
s.mtx.Lock()
defer s.mtx.Unlock()
for key, target := range s.targets {
level.Info(s.log).Log("msg", "Removing target", "key", key)
target.Stop()
delete(s.targets, key)
}
for key, watcher := range s.fileEventWatchers {
close(watcher)
delete(s.fileEventWatchers, key)
}
s.entryHandler.Stop()
}
func hostname() (string, error) {
hostname := os.Getenv("HOSTNAME")
if hostname != "" {
return hostname, nil
}
return os.Hostname()
}