generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 23
/
metrics_service_name_test.go
175 lines (138 loc) · 6.97 KB
/
metrics_service_name_test.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
//go:build e2e
package e2e
import (
"fmt"
"io"
"net/http"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kyma-project/telemetry-manager/internal/testutils"
"github.com/kyma-project/telemetry-manager/test/testkit/assert"
kitk8s "github.com/kyma-project/telemetry-manager/test/testkit/k8s"
kitkyma "github.com/kyma-project/telemetry-manager/test/testkit/kyma"
. "github.com/kyma-project/telemetry-manager/test/testkit/matchers/metric"
"github.com/kyma-project/telemetry-manager/test/testkit/mocks/backend"
"github.com/kyma-project/telemetry-manager/test/testkit/mocks/servicenamebundle"
"github.com/kyma-project/telemetry-manager/test/testkit/mocks/telemetrygen"
"github.com/kyma-project/telemetry-manager/test/testkit/periodic"
"github.com/kyma-project/telemetry-manager/test/testkit/suite"
)
var _ = Describe(suite.ID(), Label(suite.LabelMetrics), Ordered, func() {
var (
mockNs = suite.ID()
pipelineName = suite.ID()
backendExportURL string
)
makeResources := func() []client.Object {
var objs []client.Object
objs = append(objs, kitk8s.NewNamespace(mockNs).K8sObject())
backend := backend.New(mockNs, backend.SignalTypeMetrics)
objs = append(objs, backend.K8sObjects()...)
backendExportURL = backend.ExportURL(proxyClient)
metricPipeline := testutils.NewMetricPipelineBuilder().
WithName(pipelineName).
WithRuntimeInput(true, testutils.IncludeNamespaces(kitkyma.SystemNamespaceName)).
WithOTLPOutput(testutils.OTLPEndpoint(backend.Endpoint())).
Build()
objs = append(objs, &metricPipeline)
objs = append(objs, servicenamebundle.K8sObjects(mockNs, telemetrygen.SignalTypeMetrics)...)
return objs
}
Context("When a MetricPipeline exists", Ordered, func() {
BeforeAll(func() {
k8sObjects := makeResources()
DeferCleanup(func() {
Expect(kitk8s.DeleteObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed())
})
Expect(kitk8s.CreateObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed())
})
It("Should have a running metric gateway deployment", func() {
assert.DeploymentReady(ctx, k8sClient, kitkyma.MetricGatewayName)
})
It("Ensures the metric agent daemonset is ready", func() {
assert.DaemonSetReady(ctx, k8sClient, kitkyma.MetricAgentName)
})
It("Should have a metrics backend running", func() {
assert.DeploymentReady(ctx, k8sClient, types.NamespacedName{Name: backend.DefaultName, Namespace: mockNs})
})
It("Should have a running pipeline", func() {
assert.MetricPipelineHealthy(ctx, k8sClient, pipelineName)
})
verifyServiceNameAttr := func(givenPodPrefix, expectedServiceName string) {
Eventually(func(g Gomega) {
resp, err := proxyClient.Get(backendExportURL)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp).To(HaveHTTPStatus(http.StatusOK))
bodyContent, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
g.Expect(err).NotTo(HaveOccurred())
g.Expect(bodyContent).To(HaveFlatMetrics(
ContainElement(SatisfyAll(
HaveResourceAttributes(HaveKeyWithValue("service.name", expectedServiceName)),
HaveResourceAttributes(HaveKeyWithValue("k8s.pod.name", ContainSubstring(givenPodPrefix))),
)),
))
}, periodic.TelemetryEventuallyTimeout, periodic.TelemetryInterval).Should(Succeed(), fmt.Sprintf("could not find metrics matching service.name: %s, k8s.pod.name: %s.*", expectedServiceName, givenPodPrefix))
}
It("Should set undefined service.name attribute to app.kubernetes.io/name label value", func() {
verifyServiceNameAttr(servicenamebundle.PodWithBothLabelsName, servicenamebundle.KubeAppLabelValue)
})
It("Should set undefined service.name attribute to app label value", func() {
verifyServiceNameAttr(servicenamebundle.PodWithAppLabelName, servicenamebundle.AppLabelValue)
})
It("Should set undefined service.name attribute to Deployment name", func() {
verifyServiceNameAttr(servicenamebundle.DeploymentName, servicenamebundle.DeploymentName)
})
It("Should set undefined service.name attribute to StatefulSet name", func() {
verifyServiceNameAttr(servicenamebundle.StatefulSetName, servicenamebundle.StatefulSetName)
})
It("Should set undefined service.name attribute to DaemonSet name", func() {
verifyServiceNameAttr(servicenamebundle.DaemonSetName, servicenamebundle.DaemonSetName)
})
It("Should set undefined service.name attribute to Job name", func() {
verifyServiceNameAttr(servicenamebundle.JobName, servicenamebundle.JobName)
})
It("Should set undefined service.name attribute to Pod name", func() {
verifyServiceNameAttr(servicenamebundle.PodWithNoLabelsName, servicenamebundle.PodWithNoLabelsName)
})
It("Should enrich service.name attribute when its value is unknown_service", func() {
verifyServiceNameAttr(servicenamebundle.PodWithUnknownServiceName, servicenamebundle.PodWithUnknownServiceName)
})
It("Should enrich service.name attribute when its value is following the unknown_service:<process.executable.name> pattern", func() {
verifyServiceNameAttr(servicenamebundle.PodWithUnknownServicePatternName, servicenamebundle.PodWithUnknownServicePatternName)
})
It("Should NOT enrich service.name attribute when its value is not following the unknown_service:<process.executable.name> pattern", func() {
verifyServiceNameAttr(servicenamebundle.PodWithInvalidStartForUnknownServicePatternName, servicenamebundle.AttrWithInvalidStartForUnknownServicePattern)
verifyServiceNameAttr(servicenamebundle.PodWithInvalidEndForUnknownServicePatternName, servicenamebundle.AttrWithInvalidEndForUnknownServicePattern)
verifyServiceNameAttr(servicenamebundle.PodWithMissingProcessForUnknownServicePatternName, servicenamebundle.AttrWithMissingProcessForUnknownServicePattern)
})
It("Should have metrics with service.name set to telemetry-metric-gateway", func() {
Eventually(func(g Gomega) {
resp, err := proxyClient.Get(backendExportURL)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp).To(HaveHTTPStatus(http.StatusOK))
bodyContent, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
g.Expect(err).NotTo(HaveOccurred())
g.Expect(bodyContent).To(HaveFlatMetrics(
ContainElement(HaveResourceAttributes(HaveKeyWithValue("service.name", kitkyma.MetricGatewayBaseName))),
))
}, periodic.EventuallyTimeout, periodic.TelemetryInterval).Should(Succeed())
})
It("Should have metrics with service.name set to telemetry-metric-agent", func() {
Eventually(func(g Gomega) {
resp, err := proxyClient.Get(backendExportURL)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp).To(HaveHTTPStatus(http.StatusOK))
bodyContent, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
g.Expect(err).NotTo(HaveOccurred())
g.Expect(bodyContent).To(HaveFlatMetrics(
ContainElement(HaveResourceAttributes(HaveKeyWithValue("service.name", kitkyma.MetricAgentBaseName))),
))
}, periodic.EventuallyTimeout, periodic.TelemetryInterval).Should(Succeed())
})
})
})