From 3649715dedfd711d480587588238d524db3ddaa7 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 15 Jul 2024 15:20:21 +0200 Subject: [PATCH 1/3] [chore]: avoid flakyness in tests Signed-off-by: odubajDT --- .../servicegraphconnector/connector_test.go | 1 + pkg/pdatatest/pmetrictest/options.go | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/connector/servicegraphconnector/connector_test.go b/connector/servicegraphconnector/connector_test.go index e57c3781836c..2eb9749207c9 100644 --- a/connector/servicegraphconnector/connector_test.go +++ b/connector/servicegraphconnector/connector_test.go @@ -125,6 +125,7 @@ func TestConnectorConsume(t *testing.T) { pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp(), + pmetrictest.IgnoreDatapointAttributesOrder(), ) require.NoError(t, err) }) diff --git a/pkg/pdatatest/pmetrictest/options.go b/pkg/pdatatest/pmetrictest/options.go index 14a3326a28a5..4c7323a45aa8 100644 --- a/pkg/pdatatest/pmetrictest/options.go +++ b/pkg/pdatatest/pmetrictest/options.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "regexp" + "sort" "time" "go.opentelemetry.io/collector/pdata/pcommon" @@ -179,6 +180,74 @@ func IgnoreMetricAttributeValue(attributeName string, metricNames ...string) Com }) } +// IgnoreMetricAttributeValue is a CompareMetricsOption that clears value of the metric attribute. +func IgnoreDatapointAttributesOrder() CompareMetricsOption { + return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) { + orderDatapointAttributes(expected) + orderDatapointAttributes(actual) + }) +} + +func orderDatapointAttributes(metrics pmetric.Metrics) { + rms := metrics.ResourceMetrics() + for i := 0; i < rms.Len(); i++ { + ilms := rms.At(i).ScopeMetrics() + for j := 0; j < ilms.Len(); j++ { + msl := ilms.At(j).Metrics() + for g := 0; g < msl.Len(); g++ { + msl.At(g) + switch msl.At(g).Type() { + case pmetric.MetricTypeGauge: + for k := 0; k < msl.At(g).Gauge().DataPoints().Len(); k++ { + rawOrdered := orderMapByKey(msl.At(g).Gauge().DataPoints().At(k).Attributes().AsRaw()) + msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + } + case pmetric.MetricTypeSum: + for k := 0; k < msl.At(g).Sum().DataPoints().Len(); k++ { + rawOrdered := orderMapByKey(msl.At(g).Sum().DataPoints().At(k).Attributes().AsRaw()) + msl.At(g).Sum().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + } + case pmetric.MetricTypeHistogram: + for k := 0; k < msl.At(g).Histogram().DataPoints().Len(); k++ { + rawOrdered := orderMapByKey(msl.At(g).Histogram().DataPoints().At(k).Attributes().AsRaw()) + msl.At(g).Histogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + } + case pmetric.MetricTypeExponentialHistogram: + for k := 0; k < msl.At(g).ExponentialHistogram().DataPoints().Len(); k++ { + rawOrdered := orderMapByKey(msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().AsRaw()) + msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + } + case pmetric.MetricTypeSummary: + for k := 0; k < msl.At(g).Summary().DataPoints().Len(); k++ { + rawOrdered := orderMapByKey(msl.At(g).Summary().DataPoints().At(k).Attributes().AsRaw()) + msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + } + case pmetric.MetricTypeEmpty: + } + } + } + } +} + +func orderMapByKey(input map[string]any) map[string]any { + // Create a slice to hold the keys + keys := make([]string, 0, len(input)) + for k := range input { + keys = append(keys, k) + } + + // Sort the keys + sort.Strings(keys) + + // Create a new map to hold the sorted key-value pairs + orderedMap := make(map[string]any, len(input)) + for _, k := range keys { + orderedMap[k] = input[k] + } + + return orderedMap +} + func maskMetricAttributeValue(metrics pmetric.Metrics, attributeName string, metricNames []string) { rms := metrics.ResourceMetrics() for i := 0; i < rms.Len(); i++ { From ab7ff87e29a2e1648a1230968bc666b230ac226d Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 15 Jul 2024 15:31:22 +0200 Subject: [PATCH 2/3] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pmetrictest/options.go | 32 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/pdatatest/pmetrictest/options.go b/pkg/pdatatest/pmetrictest/options.go index 4c7323a45aa8..a9cde74c977a 100644 --- a/pkg/pdatatest/pmetrictest/options.go +++ b/pkg/pdatatest/pmetrictest/options.go @@ -183,12 +183,12 @@ func IgnoreMetricAttributeValue(attributeName string, metricNames ...string) Com // IgnoreMetricAttributeValue is a CompareMetricsOption that clears value of the metric attribute. func IgnoreDatapointAttributesOrder() CompareMetricsOption { return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) { - orderDatapointAttributes(expected) - orderDatapointAttributes(actual) + _ = orderDatapointAttributes(expected) + _ = orderDatapointAttributes(actual) }) } -func orderDatapointAttributes(metrics pmetric.Metrics) { +func orderDatapointAttributes(metrics pmetric.Metrics) error { rms := metrics.ResourceMetrics() for i := 0; i < rms.Len(); i++ { ilms := rms.At(i).ScopeMetrics() @@ -200,33 +200,49 @@ func orderDatapointAttributes(metrics pmetric.Metrics) { case pmetric.MetricTypeGauge: for k := 0; k < msl.At(g).Gauge().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Gauge().DataPoints().At(k).Attributes().AsRaw()) - msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + err := msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + if err != nil { + return err + } } case pmetric.MetricTypeSum: for k := 0; k < msl.At(g).Sum().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Sum().DataPoints().At(k).Attributes().AsRaw()) - msl.At(g).Sum().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + err := msl.At(g).Sum().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + if err != nil { + return err + } } case pmetric.MetricTypeHistogram: for k := 0; k < msl.At(g).Histogram().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Histogram().DataPoints().At(k).Attributes().AsRaw()) - msl.At(g).Histogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + err := msl.At(g).Histogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + if err != nil { + return err + } } case pmetric.MetricTypeExponentialHistogram: for k := 0; k < msl.At(g).ExponentialHistogram().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().AsRaw()) - msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + err := msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + if err != nil { + return err + } } case pmetric.MetricTypeSummary: for k := 0; k < msl.At(g).Summary().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Summary().DataPoints().At(k).Attributes().AsRaw()) - msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + err := msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) + if err != nil { + return err + } } case pmetric.MetricTypeEmpty: } } } } + return nil } func orderMapByKey(input map[string]any) map[string]any { From d1f9d15a29a765f277cbe653e588506839b885e3 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 16 Jul 2024 07:36:44 +0200 Subject: [PATCH 3/3] polish Signed-off-by: odubajDT --- pkg/pdatatest/pmetrictest/options.go | 32 +++++++--------------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/pkg/pdatatest/pmetrictest/options.go b/pkg/pdatatest/pmetrictest/options.go index a9cde74c977a..89b7af8fc888 100644 --- a/pkg/pdatatest/pmetrictest/options.go +++ b/pkg/pdatatest/pmetrictest/options.go @@ -183,12 +183,12 @@ func IgnoreMetricAttributeValue(attributeName string, metricNames ...string) Com // IgnoreMetricAttributeValue is a CompareMetricsOption that clears value of the metric attribute. func IgnoreDatapointAttributesOrder() CompareMetricsOption { return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) { - _ = orderDatapointAttributes(expected) - _ = orderDatapointAttributes(actual) + orderDatapointAttributes(expected) + orderDatapointAttributes(actual) }) } -func orderDatapointAttributes(metrics pmetric.Metrics) error { +func orderDatapointAttributes(metrics pmetric.Metrics) { rms := metrics.ResourceMetrics() for i := 0; i < rms.Len(); i++ { ilms := rms.At(i).ScopeMetrics() @@ -200,49 +200,33 @@ func orderDatapointAttributes(metrics pmetric.Metrics) error { case pmetric.MetricTypeGauge: for k := 0; k < msl.At(g).Gauge().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Gauge().DataPoints().At(k).Attributes().AsRaw()) - err := msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered) - if err != nil { - return err - } + _ = msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeSum: for k := 0; k < msl.At(g).Sum().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Sum().DataPoints().At(k).Attributes().AsRaw()) - err := msl.At(g).Sum().DataPoints().At(k).Attributes().FromRaw(rawOrdered) - if err != nil { - return err - } + _ = msl.At(g).Sum().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeHistogram: for k := 0; k < msl.At(g).Histogram().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Histogram().DataPoints().At(k).Attributes().AsRaw()) - err := msl.At(g).Histogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) - if err != nil { - return err - } + _ = msl.At(g).Histogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeExponentialHistogram: for k := 0; k < msl.At(g).ExponentialHistogram().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().AsRaw()) - err := msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) - if err != nil { - return err - } + _ = msl.At(g).ExponentialHistogram().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeSummary: for k := 0; k < msl.At(g).Summary().DataPoints().Len(); k++ { rawOrdered := orderMapByKey(msl.At(g).Summary().DataPoints().At(k).Attributes().AsRaw()) - err := msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) - if err != nil { - return err - } + _ = msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeEmpty: } } } } - return nil } func orderMapByKey(input map[string]any) map[string]any {