Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][pkg/pdataset/pmetrictest] introduce IgnoreDatapointAttributesOrder option to CompareMetricsOption #34076

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions connector/servicegraphconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func TestConnectorConsume(t *testing.T) {
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreDatapointAttributesOrder(),
)
require.NoError(t, err)
})
Expand Down
85 changes: 85 additions & 0 deletions pkg/pdatatest/pmetrictest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"regexp"
"sort"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -179,6 +180,90 @@ 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) error {
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())
err := msl.At(g).Gauge().DataPoints().At(k).Attributes().FromRaw(rawOrdered)
odubajDT marked this conversation as resolved.
Show resolved Hide resolved
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())
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())
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())
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())
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 {
// 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++ {
Expand Down
Loading