-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathenrich_test.go
More file actions
155 lines (145 loc) · 3.93 KB
/
enrich_test.go
File metadata and controls
155 lines (145 loc) · 3.93 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
package enrich
import (
"fmt"
"testing"
"time"
"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/service/labelstore"
"github.com/grafana/alloy/internal/util"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/stretchr/testify/require"
)
func TestEnricher(t *testing.T) {
tests := []struct {
name string
args Arguments
inputLabels map[string]string
expectedLabels map[string]string
}{
{
name: "label enrichment with target_labels and metrics_match_label",
args: Arguments{
Targets: []discovery.Target{
discovery.NewTargetFromMap(map[string]string{
"service": "test-service",
"env": "prod",
"owner": "team-a",
"foo": "bar",
}),
},
TargetMatchLabel: "service",
MetricsMatchLabel: "service_name",
LabelsToCopy: []string{"env", "owner"},
},
inputLabels: map[string]string{
"service_name": "test-service",
},
expectedLabels: map[string]string{
"service_name": "test-service",
"env": "prod",
"owner": "team-a",
},
},
{
name: "mismatch metrics and targets",
args: Arguments{
Targets: []discovery.Target{
discovery.NewTargetFromMap(map[string]string{
"service": "unknown",
"env": "prod",
"owner": "team-a",
"foo": "bar",
}),
},
TargetMatchLabel: "service",
MetricsMatchLabel: "service_name",
LabelsToCopy: []string{"env", "owner"},
},
inputLabels: map[string]string{
"service_name": "test-service",
},
expectedLabels: map[string]string{
"service_name": "test-service",
},
},
{
name: "match using target_match_label when metrics_match_label is not specified",
args: Arguments{
Targets: []discovery.Target{
discovery.NewTargetFromMap(map[string]string{
"service": "test-service",
"env": "prod",
"owner": "team-a",
"foo": "bar",
}),
},
TargetMatchLabel: "service",
},
inputLabels: map[string]string{
"service": "test-service",
},
expectedLabels: map[string]string{
"service": "test-service",
"env": "prod",
"owner": "team-a",
"foo": "bar",
},
},
{
name: "copy as is, when targets is empty",
args: Arguments{
Targets: []discovery.Target{},
TargetMatchLabel: "service",
},
inputLabels: map[string]string{
"service": "test-service",
},
expectedLabels: map[string]string{
"service": "test-service",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fanout := prometheus.NewInterceptor(
nil,
prometheus.WithAppendHook(func(ref storage.SeriesRef, l labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {
for name, value := range tt.expectedLabels {
require.Equal(t, l.Get(name), value)
}
return ref, nil
}))
var entry storage.Appendable
tt.args.ForwardTo = []storage.Appendable{fanout}
_, err := New(component.Options{
ID: "1",
Logger: util.TestAlloyLogger(t),
OnStateChange: func(e component.Exports) {
newE := e.(Exports)
entry = newE.Receiver
},
Registerer: prom.NewRegistry(),
GetServiceData: getServiceData,
}, tt.args)
require.NoError(t, err)
lbls := labels.FromMap(tt.inputLabels)
app := entry.Appender(t.Context())
_, err = app.Append(0, lbls, time.Now().UnixMilli(), 0)
require.NoError(t, err)
err = app.Commit()
require.NoError(t, err)
})
}
}
func getServiceData(name string) (interface{}, error) {
switch name {
case labelstore.ServiceName:
return labelstore.New(nil, prom.DefaultRegisterer), nil
default:
return nil, fmt.Errorf("service not found %s", name)
}
}