-
Notifications
You must be signed in to change notification settings - Fork 487
/
prom_sd_processor.go
304 lines (262 loc) · 8.59 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
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
package promsdprocessor
import (
"context"
"fmt"
"net"
"strings"
"sync"
util "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"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/client"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
semconv "go.opentelemetry.io/collector/semconv/v1.6.1"
)
type promServiceDiscoProcessor struct {
nextConsumer consumer.Traces
discoveryMgr *discovery.Manager
discoveryMgrStop context.CancelFunc
discoveryMgrCtx context.Context
relabelConfigs map[string][]*relabel.Config
hostLabels map[string]model.LabelSet
mtx sync.Mutex
operationType string
podAssociations []string
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 := discovery.NewManager(ctx, logger, discovery.Name("traces service disco"))
relabelConfigs := map[string][]*relabel.Config{}
managerConfig := map[string]discovery.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
}
switch operationType {
case OperationTypeUpsert, OperationTypeInsert, OperationTypeUpdate:
case "": // Use Upsert by default
operationType = OperationTypeUpsert
default:
cancel()
return nil, fmt.Errorf("unknown operation type %s", operationType)
}
for _, podAssociation := range podAssociations {
switch podAssociation {
case podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel, podAssociationHostnameLabel, podAssociationConnectionIP:
default:
cancel()
return nil, fmt.Errorf("unknown pod association %s", podAssociation)
}
}
if len(podAssociations) == 0 {
podAssociations = []string{podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel, podAssociationHostnameLabel, podAssociationConnectionIP}
}
if nextConsumer == nil {
cancel()
return nil, component.ErrNilNextConsumer
}
return &promServiceDiscoProcessor{
nextConsumer: nextConsumer,
discoveryMgr: mgr,
discoveryMgrStop: cancel,
discoveryMgrCtx: ctx,
relabelConfigs: relabelConfigs,
hostLabels: make(map[string]model.LabelSet),
logger: logger,
operationType: operationType,
podAssociations: podAssociations,
}, nil
}
func (p *promServiceDiscoProcessor) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
rs := rss.At(i)
p.processAttributes(ctx, rs.Resource().Attributes())
}
return p.nextConsumer.ConsumeTraces(ctx, td)
}
func stringAttributeFromMap(attrs pcommon.Map, key string) string {
if attr, ok := attrs.Get(key); ok {
if attr.Type() == pcommon.ValueTypeStr {
return attr.Str()
}
}
return ""
}
func (p *promServiceDiscoProcessor) getConnectionIP(ctx context.Context) string {
c := client.FromContext(ctx)
if c.Addr == nil {
return ""
}
host := c.Addr.String()
if strings.Contains(host, ":") {
var err error
splitHost, _, err := net.SplitHostPort(host)
if err != nil {
// It's normal for this to fail for IPv6 address strings that don't actually include a port.
level.Debug(p.logger).Log("msg", "unable to split connection host and port", "host", host, "err", err)
} else {
host = splitHost
}
}
return host
}
func (p *promServiceDiscoProcessor) getPodIP(ctx context.Context, attrs pcommon.Map) string {
for _, podAssociation := range p.podAssociations {
switch podAssociation {
case podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel:
ip := stringAttributeFromMap(attrs, podAssociation)
if ip != "" {
return ip
}
case podAssociationHostnameLabel:
hostname := stringAttributeFromMap(attrs, semconv.AttributeHostName)
if net.ParseIP(hostname) != nil {
return hostname
}
case podAssociationConnectionIP:
ip := p.getConnectionIP(ctx)
if ip != "" {
return ip
}
}
}
return ""
}
func (p *promServiceDiscoProcessor) processAttributes(ctx context.Context, attrs pcommon.Map) {
ip := p.getPodIP(ctx, attrs)
// have to have an ip for labels lookup
if ip == "" {
level.Debug(p.logger).Log("msg", "unable to find ip in span attributes, skipping attribute addition")
return
}
p.mtx.Lock()
defer p.mtx.Unlock()
labels, ok := p.hostLabels[ip]
if !ok {
level.Debug(p.logger).Log("msg", "unable to find matching hostLabels", "ip", ip)
return
}
for k, v := range labels {
switch p.operationType {
case OperationTypeUpsert:
attrs.PutStr(string(k), string(v))
case OperationTypeInsert:
if _, ok := attrs.Get(string(k)); !ok {
attrs.PutStr(string(k), string(v))
}
case OperationTypeUpdate:
if toVal, ok := attrs.Get(string(k)); ok {
toVal.SetStr(string(v))
}
}
}
}
func (p *promServiceDiscoProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
// 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]model.LabelSet)
level.Debug(p.logger).Log("msg", "syncing target groups", "count", len(targetGroups))
for jobName, groups := range targetGroups {
p.syncGroups(jobName, groups, hostLabels)
}
p.mtx.Lock()
p.hostLabels = hostLabels
p.mtx.Unlock()
case <-p.discoveryMgrCtx.Done():
return
}
}
}
func (p *promServiceDiscoProcessor) syncGroups(jobName string, groups []*targetgroup.Group, hostLabels map[string]model.LabelSet) {
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]model.LabelSet) {
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(model.LabelSet)
for k, v := range processedLabels.Map() {
labels[model.LabelName(k)] = model.LabelValue(v)
}
address, ok := labels[model.AddressLabel]
if !ok {
level.Warn(p.logger).Log("msg", "ignoring target, unable to find address", "labels", labels.String())
continue
}
host := string(address)
if strings.Contains(host, ":") {
var err error
host, _, err = net.SplitHostPort(host)
if err != nil {
level.Warn(p.logger).Log("msg", "unable to split host port", "address", address, "err", err)
continue
}
}
for k := range labels {
if strings.HasPrefix(string(k), "__") {
delete(labels, k)
}
}
level.Debug(p.logger).Log("msg", "adding host to hostLabels", "host", host)
hostLabels[host] = labels
}
}