Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/receiver/prometheus/internal/prom_to_otlp_test.go
5460 views
1
// Copyright The OpenTelemetry Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package internal
16
17
import (
18
"testing"
19
20
"github.com/prometheus/prometheus/model/labels"
21
"github.com/stretchr/testify/require"
22
"go.opentelemetry.io/collector/pdata/pcommon"
23
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
24
)
25
26
type jobInstanceDefinition struct {
27
job, instance, host, scheme, port string
28
}
29
30
type k8sResourceDefinition struct {
31
podName, podUID, container, node, rs, ds, ss, job, cronjob, ns string
32
}
33
34
func makeK8sResource(jobInstance *jobInstanceDefinition, def *k8sResourceDefinition) pcommon.Resource {
35
resource := makeResourceWithJobInstanceScheme(jobInstance, true)
36
attrs := resource.Attributes()
37
if def.podName != "" {
38
attrs.PutStr(conventions.AttributeK8SPodName, def.podName)
39
}
40
if def.podUID != "" {
41
attrs.PutStr(conventions.AttributeK8SPodUID, def.podUID)
42
}
43
if def.container != "" {
44
attrs.PutStr(conventions.AttributeK8SContainerName, def.container)
45
}
46
if def.node != "" {
47
attrs.PutStr(conventions.AttributeK8SNodeName, def.node)
48
}
49
if def.rs != "" {
50
attrs.PutStr(conventions.AttributeK8SReplicaSetName, def.rs)
51
}
52
if def.ds != "" {
53
attrs.PutStr(conventions.AttributeK8SDaemonSetName, def.ds)
54
}
55
if def.ss != "" {
56
attrs.PutStr(conventions.AttributeK8SStatefulSetName, def.ss)
57
}
58
if def.job != "" {
59
attrs.PutStr(conventions.AttributeK8SJobName, def.job)
60
}
61
if def.cronjob != "" {
62
attrs.PutStr(conventions.AttributeK8SCronJobName, def.cronjob)
63
}
64
if def.ns != "" {
65
attrs.PutStr(conventions.AttributeK8SNamespaceName, def.ns)
66
}
67
return resource
68
}
69
70
func makeResourceWithJobInstanceScheme(def *jobInstanceDefinition, hasHost bool) pcommon.Resource {
71
resource := pcommon.NewResource()
72
attrs := resource.Attributes()
73
// Using hardcoded values to assert on outward expectations so that
74
// when variables change, these tests will fail and we'll have reports.
75
attrs.PutStr("service.name", def.job)
76
if hasHost {
77
attrs.PutStr("net.host.name", def.host)
78
}
79
attrs.PutStr("service.instance.id", def.instance)
80
attrs.PutStr("net.host.port", def.port)
81
attrs.PutStr("http.scheme", def.scheme)
82
return resource
83
}
84
85
func TestCreateNodeAndResourcePromToOTLP(t *testing.T) {
86
tests := []struct {
87
name, job string
88
instance string
89
sdLabels labels.Labels
90
want pcommon.Resource
91
}{
92
{
93
name: "all attributes proper",
94
job: "job", instance: "hostname:8888", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: "http"}),
95
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
96
"job", "hostname:8888", "hostname", "http", "8888",
97
}, true),
98
},
99
{
100
name: "missing port",
101
job: "job", instance: "myinstance", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: "https"}),
102
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
103
"job", "myinstance", "myinstance", "https", "",
104
}, true),
105
},
106
{
107
name: "blank scheme",
108
job: "job", instance: "myinstance:443", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: ""}),
109
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
110
"job", "myinstance:443", "myinstance", "", "443",
111
}, true),
112
},
113
{
114
name: "blank instance, blank scheme",
115
job: "job", instance: "", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: ""}),
116
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
117
"job", "", "", "", "",
118
}, true),
119
},
120
{
121
name: "blank instance, non-blank scheme",
122
job: "job", instance: "", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: "http"}),
123
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
124
"job", "", "", "http", "",
125
}, true),
126
},
127
{
128
name: "0.0.0.0 address",
129
job: "job", instance: "0.0.0.0:8888", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: "http"}),
130
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
131
"job", "0.0.0.0:8888", "", "http", "8888",
132
}, false),
133
},
134
{
135
name: "localhost",
136
job: "job", instance: "localhost:8888", sdLabels: labels.New(labels.Label{Name: "__scheme__", Value: "http"}),
137
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
138
"job", "localhost:8888", "", "http", "8888",
139
}, false),
140
},
141
{
142
name: "kubernetes daemonset pod",
143
job: "job", instance: "hostname:8888", sdLabels: labels.New(
144
labels.Label{Name: "__scheme__", Value: "http"},
145
labels.Label{Name: "__meta_kubernetes_pod_name", Value: "my-pod-23491"},
146
labels.Label{Name: "__meta_kubernetes_pod_uid", Value: "84279wretgu89dg489q2"},
147
labels.Label{Name: "__meta_kubernetes_pod_container_name", Value: "my-container"},
148
labels.Label{Name: "__meta_kubernetes_pod_node_name", Value: "k8s-node-123"},
149
labels.Label{Name: "__meta_kubernetes_pod_controller_name", Value: "my-pod"},
150
labels.Label{Name: "__meta_kubernetes_pod_controller_kind", Value: "DaemonSet"},
151
labels.Label{Name: "__meta_kubernetes_namespace", Value: "kube-system"},
152
),
153
want: makeK8sResource(&jobInstanceDefinition{
154
"job", "hostname:8888", "hostname", "http", "8888",
155
}, &k8sResourceDefinition{
156
podName: "my-pod-23491",
157
podUID: "84279wretgu89dg489q2",
158
container: "my-container",
159
node: "k8s-node-123",
160
ds: "my-pod",
161
ns: "kube-system",
162
}),
163
},
164
{
165
name: "kubernetes replicaset pod",
166
job: "job", instance: "hostname:8888", sdLabels: labels.New(
167
labels.Label{Name: "__scheme__", Value: "http"},
168
labels.Label{Name: "__meta_kubernetes_pod_name", Value: "my-pod-23491"},
169
labels.Label{Name: "__meta_kubernetes_pod_uid", Value: "84279wretgu89dg489q2"},
170
labels.Label{Name: "__meta_kubernetes_pod_container_name", Value: "my-container"},
171
labels.Label{Name: "__meta_kubernetes_pod_node_name", Value: "k8s-node-123"},
172
labels.Label{Name: "__meta_kubernetes_pod_controller_name", Value: "my-pod"},
173
labels.Label{Name: "__meta_kubernetes_pod_controller_kind", Value: "ReplicaSet"},
174
labels.Label{Name: "__meta_kubernetes_namespace", Value: "kube-system"},
175
),
176
want: makeK8sResource(&jobInstanceDefinition{
177
"job", "hostname:8888", "hostname", "http", "8888",
178
}, &k8sResourceDefinition{
179
podName: "my-pod-23491",
180
podUID: "84279wretgu89dg489q2",
181
container: "my-container",
182
node: "k8s-node-123",
183
rs: "my-pod",
184
ns: "kube-system",
185
}),
186
},
187
{
188
name: "kubernetes statefulset pod",
189
job: "job", instance: "hostname:8888", sdLabels: labels.New(
190
labels.Label{Name: "__scheme__", Value: "http"},
191
labels.Label{Name: "__meta_kubernetes_pod_name", Value: "my-pod-23491"},
192
labels.Label{Name: "__meta_kubernetes_pod_uid", Value: "84279wretgu89dg489q2"},
193
labels.Label{Name: "__meta_kubernetes_pod_container_name", Value: "my-container"},
194
labels.Label{Name: "__meta_kubernetes_pod_node_name", Value: "k8s-node-123"},
195
labels.Label{Name: "__meta_kubernetes_pod_controller_name", Value: "my-pod"},
196
labels.Label{Name: "__meta_kubernetes_pod_controller_kind", Value: "StatefulSet"},
197
labels.Label{Name: "__meta_kubernetes_namespace", Value: "kube-system"},
198
),
199
want: makeK8sResource(&jobInstanceDefinition{
200
"job", "hostname:8888", "hostname", "http", "8888",
201
}, &k8sResourceDefinition{
202
podName: "my-pod-23491",
203
podUID: "84279wretgu89dg489q2",
204
container: "my-container",
205
node: "k8s-node-123",
206
ss: "my-pod",
207
ns: "kube-system",
208
}),
209
},
210
{
211
name: "kubernetes job pod",
212
job: "job", instance: "hostname:8888", sdLabels: labels.New(
213
labels.Label{Name: "__scheme__", Value: "http"},
214
labels.Label{Name: "__meta_kubernetes_pod_name", Value: "my-pod-23491"},
215
labels.Label{Name: "__meta_kubernetes_pod_uid", Value: "84279wretgu89dg489q2"},
216
labels.Label{Name: "__meta_kubernetes_pod_container_name", Value: "my-container"},
217
labels.Label{Name: "__meta_kubernetes_pod_node_name", Value: "k8s-node-123"},
218
labels.Label{Name: "__meta_kubernetes_pod_controller_name", Value: "my-pod"},
219
labels.Label{Name: "__meta_kubernetes_pod_controller_kind", Value: "Job"},
220
labels.Label{Name: "__meta_kubernetes_namespace", Value: "kube-system"},
221
),
222
want: makeK8sResource(&jobInstanceDefinition{
223
"job", "hostname:8888", "hostname", "http", "8888",
224
}, &k8sResourceDefinition{
225
podName: "my-pod-23491",
226
podUID: "84279wretgu89dg489q2",
227
container: "my-container",
228
node: "k8s-node-123",
229
job: "my-pod",
230
ns: "kube-system",
231
}),
232
},
233
{
234
name: "kubernetes cronjob pod",
235
job: "job", instance: "hostname:8888", sdLabels: labels.New(
236
labels.Label{Name: "__scheme__", Value: "http"},
237
labels.Label{Name: "__meta_kubernetes_pod_name", Value: "my-pod-23491"},
238
labels.Label{Name: "__meta_kubernetes_pod_uid", Value: "84279wretgu89dg489q2"},
239
labels.Label{Name: "__meta_kubernetes_pod_container_name", Value: "my-container"},
240
labels.Label{Name: "__meta_kubernetes_pod_node_name", Value: "k8s-node-123"},
241
labels.Label{Name: "__meta_kubernetes_pod_controller_name", Value: "my-pod"},
242
labels.Label{Name: "__meta_kubernetes_pod_controller_kind", Value: "CronJob"},
243
labels.Label{Name: "__meta_kubernetes_namespace", Value: "kube-system"},
244
),
245
want: makeK8sResource(&jobInstanceDefinition{
246
"job", "hostname:8888", "hostname", "http", "8888",
247
}, &k8sResourceDefinition{
248
podName: "my-pod-23491",
249
podUID: "84279wretgu89dg489q2",
250
container: "my-container",
251
node: "k8s-node-123",
252
cronjob: "my-pod",
253
ns: "kube-system",
254
}),
255
},
256
{
257
name: "kubernetes node (e.g. kubelet)",
258
job: "job", instance: "hostname:8888", sdLabels: labels.New(
259
labels.Label{Name: "__scheme__", Value: "http"},
260
labels.Label{Name: "__meta_kubernetes_node_name", Value: "k8s-node-123"},
261
),
262
want: makeK8sResource(&jobInstanceDefinition{
263
"job", "hostname:8888", "hostname", "http", "8888",
264
}, &k8sResourceDefinition{
265
node: "k8s-node-123",
266
}),
267
},
268
{
269
name: "kubernetes service endpoint",
270
job: "job", instance: "hostname:8888", sdLabels: labels.New(
271
labels.Label{Name: "__scheme__", Value: "http"},
272
labels.Label{Name: "__meta_kubernetes_endpoint_node_name", Value: "k8s-node-123"},
273
),
274
want: makeK8sResource(&jobInstanceDefinition{
275
"job", "hostname:8888", "hostname", "http", "8888",
276
}, &k8sResourceDefinition{
277
node: "k8s-node-123",
278
}),
279
},
280
}
281
282
for _, tt := range tests {
283
tt := tt
284
t.Run(tt.name, func(t *testing.T) {
285
got := CreateResource(tt.job, tt.instance, tt.sdLabels)
286
got.Attributes().Sort()
287
tt.want.Attributes().Sort()
288
require.Equal(t, tt.want, got)
289
})
290
}
291
}
292
293