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][internal/comparetest]Make all exported Compare functions not mutated #17553

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 19 additions & 11 deletions internal/comparetest/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ func CompareLogs(expected, actual plog.Logs, options ...LogsCompareOption) error
// CompareResourceLogs compares each part of two given ResourceLogs and returns
// an error if they don't match. The error describes what didn't match.
func CompareResourceLogs(expected, actual plog.ResourceLogs) error {
eilms := expected.ScopeLogs()
ailms := actual.ScopeLogs()
exp, act := plog.NewResourceLogs(), plog.NewResourceLogs()
expected.CopyTo(exp)
actual.CopyTo(act)

eilms := exp.ScopeLogs()
ailms := act.ScopeLogs()

if eilms.Len() != ailms.Len() {
return fmt.Errorf("number of instrumentation libraries does not match expected: %d, actual: %d", eilms.Len(),
Expand Down Expand Up @@ -123,24 +127,28 @@ func CompareResourceLogs(expected, actual plog.ResourceLogs) error {
// CompareLogRecordSlices compares each part of two given LogRecordSlices and returns
// an error if they don't match. The error describes what didn't match.
func CompareLogRecordSlices(expected, actual plog.LogRecordSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of log records does not match expected: %d, actual: %d", expected.Len(), actual.Len())
exp, act := plog.NewLogRecordSlice(), plog.NewLogRecordSlice()
expected.CopyTo(exp)
actual.CopyTo(act)

if exp.Len() != act.Len() {
return fmt.Errorf("number of log records does not match expected: %d, actual: %d", exp.Len(), act.Len())
}

expected.Sort(sortLogRecordSlice)
actual.Sort(sortLogRecordSlice)
exp.Sort(sortLogRecordSlice)
act.Sort(sortLogRecordSlice)

numLogRecords := expected.Len()
numLogRecords := exp.Len()

// Keep track of matching records so that each record can only be matched once
matchingLogRecords := make(map[plog.LogRecord]plog.LogRecord, numLogRecords)

var errs error
for e := 0; e < numLogRecords; e++ {
elr := expected.At(e)
elr := exp.At(e)
var foundMatch bool
for a := 0; a < numLogRecords; a++ {
alr := actual.At(a)
alr := act.At(a)
if _, ok := matchingLogRecords[alr]; ok {
continue
}
Expand All @@ -156,8 +164,8 @@ func CompareLogRecordSlices(expected, actual plog.LogRecordSlice) error {
}

for i := 0; i < numLogRecords; i++ {
if _, ok := matchingLogRecords[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("log has extra record with attributes: %v", actual.At(i).Attributes().AsRaw()))
if _, ok := matchingLogRecords[act.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("log has extra record with attributes: %v", act.At(i).Attributes().AsRaw()))
}
}

Expand Down
26 changes: 17 additions & 9 deletions internal/comparetest/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ func CompareMetrics(expected, actual pmetric.Metrics, options ...MetricsCompareO
}

func CompareResourceMetrics(expected, actual pmetric.ResourceMetrics) error {
eilms := expected.ScopeMetrics()
ailms := actual.ScopeMetrics()
exp, act := pmetric.NewResourceMetrics(), pmetric.NewResourceMetrics()
expected.CopyTo(exp)
actual.CopyTo(act)

eilms := exp.ScopeMetrics()
ailms := act.ScopeMetrics()

if eilms.Len() != ailms.Len() {
return fmt.Errorf("number of instrumentation libraries does not match expected: %d, actual: %d", eilms.Len(),
Expand Down Expand Up @@ -120,15 +124,19 @@ func CompareResourceMetrics(expected, actual pmetric.ResourceMetrics) error {
// an error if they don't match. The error describes what didn't match. The
// expected and actual values are clones before options are applied.
func CompareMetricSlices(expected, actual pmetric.MetricSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of metrics does not match expected: %d, actual: %d", expected.Len(), actual.Len())
exp, act := pmetric.NewMetricSlice(), pmetric.NewMetricSlice()
expected.CopyTo(exp)
actual.CopyTo(act)

if exp.Len() != act.Len() {
return fmt.Errorf("number of metrics does not match expected: %d, actual: %d", exp.Len(), act.Len())
}

// Sort MetricSlices
expected.Sort(sortMetricSlice)
actual.Sort(sortMetricSlice)
exp.Sort(sortMetricSlice)
act.Sort(sortMetricSlice)

expectedByName, actualByName := metricsByName(expected), metricsByName(actual)
expectedByName, actualByName := metricsByName(exp), metricsByName(act)

var errs error
for name := range actualByName {
Expand All @@ -147,8 +155,8 @@ func CompareMetricSlices(expected, actual pmetric.MetricSlice) error {
return errs
}

for i := 0; i < actual.Len(); i++ {
actualMetric := actual.At(i)
for i := 0; i < act.Len(); i++ {
actualMetric := act.At(i)
expectedMetric := expectedByName[actualMetric.Name()]
if actualMetric.Description() != expectedMetric.Description() {
return fmt.Errorf("metric Description does not match expected: %s, actual: %s", expectedMetric.Description(), actualMetric.Description())
Expand Down
30 changes: 19 additions & 11 deletions internal/comparetest/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ func CompareTraces(expected, actual ptrace.Traces, options ...TracesCompareOptio
// CompareResourceSpans compares each part of two given ResourceSpans and returns
// an error if they don't match. The error describes what didn't match.
func CompareResourceSpans(expected, actual ptrace.ResourceSpans) error {
eilms := expected.ScopeSpans()
ailms := actual.ScopeSpans()
exp, act := ptrace.NewResourceSpans(), ptrace.NewResourceSpans()
expected.CopyTo(exp)
actual.CopyTo(act)

eilms := exp.ScopeSpans()
ailms := act.ScopeSpans()

if eilms.Len() != ailms.Len() {
return fmt.Errorf("number of instrumentation libraries does not match expected: %d, actual: %d", eilms.Len(),
Expand Down Expand Up @@ -123,24 +127,28 @@ func CompareResourceSpans(expected, actual ptrace.ResourceSpans) error {
// CompareSpanSlices compares each part of two given SpanSlices and returns
// an error if they don't match. The error describes what didn't match.
func CompareSpanSlices(expected, actual ptrace.SpanSlice) error {
if expected.Len() != actual.Len() {
return fmt.Errorf("number of spans does not match expected: %d, actual: %d", expected.Len(), actual.Len())
exp, act := ptrace.NewSpanSlice(), ptrace.NewSpanSlice()
expected.CopyTo(exp)
actual.CopyTo(act)

if exp.Len() != act.Len() {
return fmt.Errorf("number of spans does not match expected: %d, actual: %d", exp.Len(), act.Len())
}

expected.Sort(sortSpanSlice)
actual.Sort(sortSpanSlice)
exp.Sort(sortSpanSlice)
act.Sort(sortSpanSlice)

numSpans := expected.Len()
numSpans := exp.Len()

// Keep track of matching spans so that each span can only be matched once
matchingSpans := make(map[ptrace.Span]ptrace.Span, numSpans)

var errs error
for e := 0; e < numSpans; e++ {
elr := expected.At(e)
elr := exp.At(e)
var foundMatch bool
for a := 0; a < numSpans; a++ {
alr := actual.At(a)
alr := act.At(a)
if _, ok := matchingSpans[alr]; ok {
continue
}
Expand All @@ -156,8 +164,8 @@ func CompareSpanSlices(expected, actual ptrace.SpanSlice) error {
}

for i := 0; i < numSpans; i++ {
if _, ok := matchingSpans[actual.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("span has extra record with attributes: %v", actual.At(i).Attributes().AsRaw()))
if _, ok := matchingSpans[act.At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("span has extra record with attributes: %v", act.At(i).Attributes().AsRaw()))
}
}

Expand Down