-
Notifications
You must be signed in to change notification settings - Fork 486
/
prom_sd_processor.go
178 lines (150 loc) · 5.54 KB
/
prom_sd_processor.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
package promsdprocessor
import (
"context"
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/agent/component/discovery"
promsdconsumer "github.com/grafana/agent/pkg/traces/promsdprocessor/consumer"
util "github.com/grafana/agent/pkg/util/log"
"github.com/prometheus/prometheus/config"
promdiscovery "github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
)
type promServiceDiscoProcessor struct {
discoveryMgr *promdiscovery.Manager
discoveryMgrStop context.CancelFunc
discoveryMgrCtx context.Context
relabelConfigs map[string][]*relabel.Config
consumer *promsdconsumer.Consumer
logger log.Logger
}
func newTraceProcessor(nextConsumer consumer.Traces, operationType string, podAssociations []string, scrapeConfigs []*config.ScrapeConfig) (processor.Traces, error) {
ctx, cancel := context.WithCancel(context.Background())
logger := log.With(util.Logger, "component", "traces service disco")
mgr := promdiscovery.NewManager(ctx, logger, promdiscovery.Name("traces service disco"))
relabelConfigs := map[string][]*relabel.Config{}
managerConfig := map[string]promdiscovery.Configs{}
for _, v := range scrapeConfigs {
managerConfig[v.JobName] = v.ServiceDiscoveryConfigs
relabelConfigs[v.JobName] = v.RelabelConfigs
}
err := mgr.ApplyConfig(managerConfig)
if err != nil {
cancel()
return nil, err
}
if len(podAssociations) == 0 {
podAssociations = []string{
promsdconsumer.PodAssociationIPLabel,
promsdconsumer.PodAssociationOTelIPLabel,
promsdconsumer.PodAssociationk8sIPLabel,
promsdconsumer.PodAssociationHostnameLabel,
promsdconsumer.PodAssociationConnectionIP,
}
}
consumerOpts := promsdconsumer.Options{
// Don't bother setting up labels - this will be done by the UpdateOptionsHostLabels() function.
HostLabels: map[string]discovery.Target{},
OperationType: operationType,
PodAssociations: podAssociations,
NextConsumer: nextConsumer,
}
consumer, err := promsdconsumer.NewConsumer(consumerOpts, logger)
if err != nil {
cancel()
return nil, fmt.Errorf("cannot create a new consumer %w", err)
}
return &promServiceDiscoProcessor{
discoveryMgr: mgr,
discoveryMgrStop: cancel,
discoveryMgrCtx: ctx,
relabelConfigs: relabelConfigs,
logger: logger,
consumer: consumer,
}, nil
}
func (p *promServiceDiscoProcessor) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
return p.consumer.ConsumeTraces(ctx, td)
}
func (p *promServiceDiscoProcessor) Capabilities() consumer.Capabilities {
return p.consumer.Capabilities()
}
// Start is invoked during service startup.
func (p *promServiceDiscoProcessor) Start(_ context.Context, _ component.Host) error {
go p.watchServiceDiscovery()
go func() {
err := p.discoveryMgr.Run()
if err != nil && err != context.Canceled {
level.Error(p.logger).Log("msg", "failed to start prom svc disco. relabeling disabled", "err", err)
}
}()
return nil
}
// Shutdown is invoked during service shutdown.
func (p *promServiceDiscoProcessor) Shutdown(context.Context) error {
if p.discoveryMgrStop != nil {
p.discoveryMgrStop()
}
return nil
}
func (p *promServiceDiscoProcessor) watchServiceDiscovery() {
for {
// p.discoveryMgr.SyncCh() is never closed so we need to watch the context as well to properly exit this goroutine
select {
case targetGroups := <-p.discoveryMgr.SyncCh():
hostLabels := make(map[string]discovery.Target)
level.Debug(p.logger).Log("msg", "syncing target groups", "count", len(targetGroups))
for jobName, groups := range targetGroups {
p.syncGroups(jobName, groups, hostLabels)
}
p.consumer.UpdateOptionsHostLabels(hostLabels)
case <-p.discoveryMgrCtx.Done():
return
}
}
}
func (p *promServiceDiscoProcessor) syncGroups(jobName string, groups []*targetgroup.Group, hostLabels map[string]discovery.Target) {
level.Debug(p.logger).Log("msg", "syncing target group", "jobName", jobName)
for _, g := range groups {
p.syncTargets(jobName, g, hostLabels)
}
}
func (p *promServiceDiscoProcessor) syncTargets(jobName string, group *targetgroup.Group, hostLabels map[string]discovery.Target) {
level.Debug(p.logger).Log("msg", "syncing targets", "count", len(group.Targets))
relabelConfig := p.relabelConfigs[jobName]
if relabelConfig == nil {
level.Warn(p.logger).Log("msg", "relabel config not found for job. skipping labeling", "jobName", jobName)
return
}
for _, t := range group.Targets {
discoveredLabels := group.Labels.Merge(t)
level.Debug(p.logger).Log("discoveredLabels", discoveredLabels)
var labelMap = make(map[string]string)
for k, v := range discoveredLabels.Clone() {
labelMap[string(k)] = string(v)
}
processedLabels, keep := relabel.Process(labels.FromMap(labelMap), relabelConfig...)
level.Debug(p.logger).Log("processedLabels", processedLabels)
if !keep {
continue
}
var labels = make(discovery.Target)
for k, v := range processedLabels.Map() {
labels[k] = v
}
host, err := promsdconsumer.GetHostFromLabels(labels)
if err != nil {
level.Warn(p.logger).Log("msg", "ignoring target, unable to find address", "err", err)
continue
}
level.Debug(p.logger).Log("msg", "adding host to hostLabels", "host", host)
hostLabels[host] = promsdconsumer.NewTargetsWithNonInternalLabels(labels)
}
}