forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetargetmanager.go
331 lines (284 loc) · 8.98 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
package targets
import (
"context"
"fmt"
"os"
"strings"
"sync"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/grafana/loki/pkg/helpers"
"github.com/grafana/loki/pkg/logentry/stages"
"github.com/grafana/loki/pkg/promtail/api"
"github.com/grafana/loki/pkg/promtail/positions"
"github.com/grafana/loki/pkg/promtail/scrape"
)
const (
pathLabel = "__path__"
hostLabel = "__host__"
)
var (
failedTargets = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "promtail",
Name: "targets_failed_total",
Help: "Number of failed targets.",
}, []string{"reason"})
targetsActive = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "promtail",
Name: "targets_active_total",
Help: "Number of active total.",
})
)
// FileTargetManager manages a set of targets.
type FileTargetManager struct {
log log.Logger
quit context.CancelFunc
syncers map[string]*targetSyncer
manager *discovery.Manager
}
// NewFileTargetManager creates a new TargetManager.
func NewFileTargetManager(
logger log.Logger,
positions *positions.Positions,
client api.EntryHandler,
scrapeConfigs []scrape.Config,
targetConfig *Config,
) (*FileTargetManager, error) {
ctx, quit := context.WithCancel(context.Background())
tm := &FileTargetManager{
log: logger,
quit: quit,
syncers: map[string]*targetSyncer{},
manager: discovery.NewManager(ctx, log.With(logger, "component", "discovery")),
}
hostname, err := hostname()
if err != nil {
return nil, err
}
config := map[string]sd_config.ServiceDiscoveryConfig{}
for _, cfg := range scrapeConfigs {
if !cfg.HasServiceDiscoveryConfig() {
continue
}
registerer := prometheus.DefaultRegisterer
pipeline, err := stages.NewPipeline(log.With(logger, "component", "file_pipeline"), cfg.PipelineStages, &cfg.JobName, registerer)
if err != nil {
return nil, err
}
// Backwards compatibility with old EntryParser config
if pipeline.Size() == 0 {
switch cfg.EntryParser {
case api.CRI:
level.Warn(logger).Log("msg", "WARNING!!! entry_parser config is deprecated, please change to pipeline_stages")
cri, err := stages.NewCRI(logger, registerer)
if err != nil {
return nil, err
}
pipeline.AddStage(cri)
case api.Docker:
level.Warn(logger).Log("msg", "WARNING!!! entry_parser config is deprecated, please change to pipeline_stages")
docker, err := stages.NewDocker(logger, registerer)
if err != nil {
return nil, err
}
pipeline.AddStage(docker)
case api.Raw:
level.Warn(logger).Log("msg", "WARNING!!! entry_parser config is deprecated, please change to pipeline_stages")
default:
}
}
s := &targetSyncer{
log: logger,
positions: positions,
relabelConfig: cfg.RelabelConfigs,
targets: map[string]*FileTarget{},
droppedTargets: []Target{},
hostname: hostname,
entryHandler: pipeline.Wrap(client),
targetConfig: targetConfig,
}
tm.syncers[cfg.JobName] = s
config[cfg.JobName] = cfg.ServiceDiscoveryConfig
}
go tm.run()
go helpers.LogError("running target manager", tm.manager.Run)
return tm, tm.manager.ApplyConfig(config)
}
func (tm *FileTargetManager) run() {
for targetGoups := range tm.manager.SyncCh() {
for jobName, groups := range targetGoups {
tm.syncers[jobName].sync(groups)
}
}
}
// 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()
for _, s := range tm.syncers {
s.stop()
}
}
// ActiveTargets returns the active targets currently being scraped.
func (tm *FileTargetManager) ActiveTargets() map[string][]Target {
result := map[string][]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 {
result := map[string][]Target{}
for jobName, syncer := range tm.syncers {
result[jobName] = append(result[jobName], syncer.ActiveTargets()...)
result[jobName] = append(result[jobName], syncer.DroppedTargets()...)
}
return result
}
// targetSyncer sync targets based on service discovery changes.
type targetSyncer struct {
log log.Logger
positions *positions.Positions
entryHandler api.EntryHandler
hostname string
droppedTargets []Target
targets map[string]*FileTarget
mtx sync.Mutex
relabelConfig []*relabel.Config
targetConfig *Config
}
// sync synchronize target based on received target groups received by service discovery
func (s *targetSyncer) sync(groups []*targetgroup.Group) {
s.mtx.Lock()
defer s.mtx.Unlock()
targets := map[string]struct{}{}
dropped := []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 := 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 empty targets (drop in relabeling).
if processedLabels == nil {
dropped = append(dropped, newDroppedTarget("dropping target, no labels", discoveredLabels))
level.Debug(s.log).Log("msg", "dropping target, no labels")
failedTargets.WithLabelValues("empty_labels").Inc()
continue
}
host, ok := labels[hostLabel]
if ok && string(host) != s.hostname {
dropped = append(dropped, 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)
failedTargets.WithLabelValues("wrong_host").Inc()
continue
}
path, ok := labels[pathLabel]
if !ok {
dropped = append(dropped, newDroppedTarget("no path for target", discoveredLabels))
level.Info(s.log).Log("msg", "no path for target", "labels", labels.String())
failedTargets.WithLabelValues("no_path").Inc()
continue
}
for k := range labels {
if strings.HasPrefix(string(k), "__") {
delete(labels, k)
}
}
key := labels.String()
targets[key] = struct{}{}
if _, ok := s.targets[key]; ok {
dropped = append(dropped, newDroppedTarget("ignoring target, already exists", discoveredLabels))
level.Debug(s.log).Log("msg", "ignoring target, already exists", "labels", labels.String())
failedTargets.WithLabelValues("exists").Inc()
continue
}
level.Info(s.log).Log("msg", "Adding target", "key", key)
t, err := s.newTarget(string(path), labels, discoveredLabels)
if err != nil {
dropped = append(dropped, 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)
failedTargets.WithLabelValues("error").Inc()
continue
}
targetsActive.Add(1.)
s.targets[key] = t
}
}
for key, target := range s.targets {
if _, ok := targets[key]; !ok {
level.Info(s.log).Log("msg", "Removing target", "key", key)
target.Stop()
targetsActive.Add(-1.)
delete(s.targets, key)
}
}
s.droppedTargets = dropped
}
func (s *targetSyncer) newTarget(path string, labels model.LabelSet, discoveredLabels model.LabelSet) (*FileTarget, error) {
return NewFileTarget(s.log, s.entryHandler, s.positions, path, labels, discoveredLabels, s.targetConfig)
}
func (s *targetSyncer) DroppedTargets() []Target {
s.mtx.Lock()
defer s.mtx.Unlock()
return append([]Target(nil), s.droppedTargets...)
}
func (s *targetSyncer) ActiveTargets() []Target {
s.mtx.Lock()
defer s.mtx.Unlock()
actives := []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)
}
}
func hostname() (string, error) {
hostname := os.Getenv("HOSTNAME")
if hostname != "" {
return hostname, nil
}
return os.Hostname()
}