-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathenrich.go
More file actions
242 lines (192 loc) · 6.46 KB
/
enrich.go
File metadata and controls
242 lines (192 loc) · 6.46 KB
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
package enrich
import (
"context"
"fmt"
"sync"
prometheus_client "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/metadata"
"github.com/prometheus/prometheus/storage"
"go.uber.org/atomic"
"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/component/prometheus"
"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/service/labelstore"
)
func init() {
component.Register(component.Registration{
Name: "prometheus.enrich",
Stability: featuregate.StabilityExperimental,
Args: Arguments{},
Exports: Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
},
})
}
// Arguments configures the prometheus.enrich component.
type Arguments struct {
// The targets to use for enrichment
Targets []discovery.Target `alloy:"targets,attr"`
// Which label from targets to use for matching (e.g. "hostname", "ip")
TargetMatchLabel string `alloy:"target_match_label,attr"`
// Which label from logs to match against (e.g. "hostname", "ip")
// If not specified, TargetMatchLabel will be used
MetricsMatchLabel string `alloy:"metrics_match_label,attr,optional"`
// List of labels to copy from discovered targets to logs. If empty, all labels will be copied.
LabelsToCopy []string `alloy:"labels_to_copy,attr,optional"`
ForwardTo []storage.Appendable `alloy:"forward_to,attr"`
}
// Exports holds values which are exported by the prometheus.enrich component.
type Exports struct {
Receiver storage.Appendable `alloy:"receiver,attr"`
}
type Component struct {
opts component.Options
args Arguments
mut sync.RWMutex
receiver *prometheus.Interceptor
fanout *prometheus.Fanout
exited atomic.Bool
targetsCache map[string]model.LabelSet
cacheMutex sync.RWMutex
cacheSize prometheus_client.Gauge
}
func New(opts component.Options, args Arguments) (*Component, error) {
service, err := opts.GetServiceData(labelstore.ServiceName)
if err != nil {
return nil, err
}
ls := service.(labelstore.LabelStore)
c := &Component{
opts: opts,
args: args,
}
c.cacheSize = prometheus_client.NewGauge(prometheus_client.GaugeOpts{
Name: "alloy_prometheus_target_cache_size",
Help: "Total size of targets cache",
})
for _, metric := range []prometheus_client.Collector{
c.cacheSize,
} {
err = opts.Registerer.Register(metric)
if err != nil {
return nil, err
}
}
c.fanout = prometheus.NewFanout(args.ForwardTo, opts.ID, opts.Registerer, ls)
c.receiver = prometheus.NewInterceptor(
c.fanout,
prometheus.WithComponentID(c.opts.ID),
prometheus.WithAppendHook(func(_ storage.SeriesRef, l labels.Labels, t int64, v float64, next storage.Appender) (storage.SeriesRef, error) {
if c.exited.Load() {
return 0, fmt.Errorf("%s has exited", opts.ID)
}
newLabels := c.enrich(l)
// Since SeriesRefs are tied to the labels, we send zero to indicate the seriesRef should be recalculated downstream.
return next.Append(0, newLabels, t, v)
}),
prometheus.WithExemplarHook(func(_ storage.SeriesRef, l labels.Labels, e exemplar.Exemplar, next storage.Appender) (storage.SeriesRef, error) {
if c.exited.Load() {
return 0, fmt.Errorf("%s has exited", opts.ID)
}
newLabels := c.enrich(l)
// Since SeriesRefs are tied to the labels, we send zero to indicate the seriesRef should be recalculated downstream.
return next.AppendExemplar(0, newLabels, e)
}),
prometheus.WithMetadataHook(func(_ storage.SeriesRef, l labels.Labels, m metadata.Metadata, next storage.Appender) (storage.SeriesRef, error) {
if c.exited.Load() {
return 0, fmt.Errorf("%s has exited", opts.ID)
}
newLabels := c.enrich(l)
// Since SeriesRefs are tied to the labels, we send zero to indicate the seriesRef should be recalculated downstream.
return next.UpdateMetadata(0, newLabels, m)
}),
prometheus.WithHistogramHook(func(_ storage.SeriesRef, l labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram, next storage.Appender) (storage.SeriesRef, error) {
if c.exited.Load() {
return 0, fmt.Errorf("%s has exited", opts.ID)
}
newLabels := c.enrich(l)
return next.AppendHistogram(0, newLabels, t, h, fh)
}),
)
if err = c.Update(args); err != nil {
return nil, err
}
return c, nil
}
// Run implements component.Component.
func (c *Component) Run(ctx context.Context) error {
defer c.exited.Store(true)
<-ctx.Done()
return nil
}
func (c *Component) Name() string {
return "prometheus.enrich"
}
func (c *Component) Ready() bool {
return true
}
// Update implements component.Component.
func (c *Component) Update(args component.Arguments) error {
c.mut.Lock()
defer c.mut.Unlock()
newArgs := args.(Arguments)
c.fanout.UpdateChildren(newArgs.ForwardTo)
c.refreshCacheFromTargets(newArgs.Targets)
c.opts.OnStateChange(Exports{Receiver: c.receiver})
return nil
}
func (c *Component) enrich(lbls labels.Labels) labels.Labels {
var targetSet model.LabelSet
var ok bool
matchLabel := c.args.MetricsMatchLabel
if matchLabel == "" {
matchLabel = c.args.TargetMatchLabel
}
mlv := lbls.Get(matchLabel)
if mlv == "" {
return lbls
}
c.cacheMutex.RLock()
targetSet, ok = c.targetsCache[mlv]
c.cacheMutex.RUnlock()
if !ok {
return lbls
}
newLabels := labels.NewBuilder(lbls.Copy())
if len(c.args.LabelsToCopy) == 0 {
for k, v := range targetSet {
newLabels.Set(string(k), string(v))
}
} else {
for _, label := range c.args.LabelsToCopy {
if value, ok := targetSet[model.LabelName(label)]; ok {
newLabels.Set(label, string(value))
}
}
}
return newLabels.Labels()
}
func (c *Component) refreshCacheFromTargets(targets []discovery.Target) {
newCache := make(map[string]model.LabelSet)
for _, target := range targets {
labelSet := make(model.LabelSet)
// Copy both own and group labels
target.ForEachLabel(func(k, v string) bool {
labelSet[model.LabelName(k)] = model.LabelValue(v)
return true
})
if matchValue := string(labelSet[model.LabelName(c.args.TargetMatchLabel)]); matchValue != "" {
newCache[matchValue] = labelSet
}
}
c.cacheMutex.Lock()
c.targetsCache = newCache
c.cacheMutex.Unlock()
c.cacheSize.Set(float64(len(c.targetsCache)))
}