Skip to content

Commit

Permalink
ignore metric attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Oct 10, 2023
1 parent 2e335f2 commit 4fc7fbc
Show file tree
Hide file tree
Showing 2 changed files with 4,450 additions and 2,810 deletions.
64 changes: 58 additions & 6 deletions functional_tests/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,44 @@ func testK8sClusterReceiverMetrics(t *testing.T) {
return value[(strings.LastIndex(value, "/") + 1):]
}

var selected pmetric.Metrics
for _, m := range metricsConsumer.AllMetrics() {
if m.ResourceMetrics().Len() == expectedMetrics.ResourceMetrics().Len() {
selected = m
var selected *pmetric.Metrics
for h := len(metricsConsumer.AllMetrics()) - 1; h >= 0; h-- {
m := metricsConsumer.AllMetrics()[h]
foundCorrectSet := false
OUTER:
for i := 0; i < m.ResourceMetrics().Len(); i++ {
for j := 0; j < m.ResourceMetrics().At(i).ScopeMetrics().Len(); j++ {
for k := 0; k < m.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().Len(); k++ {
metricToConsider := m.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().At(k)
if metricToConsider.Name() == "k8s.container.restarts" {
foundCorrectSet = true
break OUTER
}
}
}
}
if !foundCorrectSet {
continue
}
if m.ResourceMetrics().Len() == expectedMetrics.ResourceMetrics().Len() && m.MetricCount() == expectedMetrics.MetricCount() {
selected = &m
break
}
}

require.NotNil(t, selected)

attrNamesToIgnore := []string{"container.id", "k8s.daemonset.uid", "k8s.deployment.uid", "k8s.pod.uid", "k8s.pod.name", "k8s.replicaset.uid", "k8s.replicaset.name", "k8s.namespace.uid"}

ignoreMetricDataPointAttributes(t, *selected, attrNamesToIgnore...)
ignoreMetricDataPointAttributes(t, expectedMetrics, attrNamesToIgnore...)

require.NoError(t,
pmetrictest.CompareMetrics(expectedMetrics, selected,
pmetrictest.CompareMetrics(expectedMetrics, *selected,
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreMetricValues("k8s.deployment.desired", "k8s.deployment.available", "k8s.container.restarts", "k8s.container.cpu_request", "k8s.container.memory_request", "k8s.container.memory_limit"),
pmetrictest.IgnoreMetricAttributeValue("container.id", "k8s.daemonset.uid", "k8s.deployment.uid", "k8s.pod.uid", "k8s.pod.name", "k8s.replicaset.uid", "k8s.replicaset.name"),
pmetrictest.IgnoreMetricValues("k8s.namespace.phase", "k8s.pod.phase", "k8s.replicaset.desired", "k8s.replicaset.available", "k8s.daemonset.ready_nodes", "k8s.daemonset.misscheduled_nodes", "k8s.daemonset.desired_scheduled_nodes", "k8s.daemonset.current_scheduled_nodes", "k8s.container.ready", "k8s.container.memory_request", "k8s.container.memory_limit", "k8s.container.cpu_request", "k8s.container.cpu_limit", "k8s.deployment.desired", "k8s.deployment.available", "k8s.container.restarts", "k8s.container.cpu_request", "k8s.container.memory_request", "k8s.container.memory_limit"),
pmetrictest.ChangeResourceAttributeValue("k8s.deployment.name", shortenNames),
pmetrictest.ChangeResourceAttributeValue("k8s.pod.name", shortenNames),
pmetrictest.ChangeResourceAttributeValue("k8s.replicaset.name", shortenNames),
Expand All @@ -364,6 +389,7 @@ func testK8sClusterReceiverMetrics(t *testing.T) {
pmetrictest.ChangeResourceAttributeValue("k8s.daemonset.uid", replaceWithStar),
pmetrictest.ChangeResourceAttributeValue("container.image.name", containerImageShorten),
pmetrictest.ChangeResourceAttributeValue("container.id", replaceWithStar),
pmetrictest.ChangeResourceAttributeValue("host.name", replaceWithStar),
pmetrictest.IgnoreScopeVersion(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
Expand All @@ -373,6 +399,32 @@ func testK8sClusterReceiverMetrics(t *testing.T) {
)
}

func ignoreMetricDataPointAttributes(t *testing.T, metrics pmetric.Metrics, attributeNames ...string) {
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
for j := 0; j < metrics.ResourceMetrics().At(i).ScopeMetrics().Len(); j++ {
for k := 0; k < metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().Len(); k++ {
metricToChange := metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().At(k)
switch metricToChange.Type() {

case pmetric.MetricTypeGauge:
for l := 0; l < metricToChange.Gauge().DataPoints().Len(); l++ {
dp := metricToChange.Gauge().DataPoints().At(l)
dp.Attributes().Range(func(k string, v pcommon.Value) bool {
for _, attributeName := range attributeNames {
if k == attributeName {
v.SetStr("*")
break
}
}
return true
})
}
}
}
}
}
}

func testAgentLogs(t *testing.T, ) {
logsConsumer := setupOnce(t).logsConsumer
waitForLogs(t, 5, logsConsumer)
Expand Down
Loading

0 comments on commit 4fc7fbc

Please sign in to comment.