From f4917f02dd6cd2fa9a6cc3594dcc5ff611b94a34 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 7 Nov 2024 13:00:14 +0100 Subject: [PATCH 01/22] com Signed-off-by: odubajDT --- pkg/pdatatest/go.mod | 1 + pkg/pdatatest/go.sum | 2 + pkg/pdatatest/pprofiletest/options.go | 214 +++++++++++++ pkg/pdatatest/pprofiletest/package_test.go | 14 + pkg/pdatatest/pprofiletest/profiles.go | 342 +++++++++++++++++++++ 5 files changed, 573 insertions(+) create mode 100644 pkg/pdatatest/pprofiletest/options.go create mode 100644 pkg/pdatatest/pprofiletest/package_test.go create mode 100644 pkg/pdatatest/pprofiletest/profiles.go diff --git a/pkg/pdatatest/go.mod b/pkg/pdatatest/go.mod index 1fa2521ad123..f2ad6d218ef7 100644 --- a/pkg/pdatatest/go.mod +++ b/pkg/pdatatest/go.mod @@ -7,6 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.115.0 github.com/stretchr/testify v1.10.0 go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8 + go.opentelemetry.io/collector/pdata/pprofile v0.115.0 go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 ) diff --git a/pkg/pdatatest/go.sum b/pkg/pdatatest/go.sum index 6d68c4eeac41..1120d7f0ab2a 100644 --- a/pkg/pdatatest/go.sum +++ b/pkg/pdatatest/go.sum @@ -33,6 +33,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8 h1:PUaCJ1XIIomqXvFBF6hMFikhZIwoBc57UP7xlaRT//I= go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go new file mode 100644 index 000000000000..deb31673ba70 --- /dev/null +++ b/pkg/pdatatest/pprofiletest/options.go @@ -0,0 +1,214 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofiletest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pprofiletest" + +import ( + "bytes" + "time" + + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pprofile" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" +) + +// CompareProfilesOption can be used to mutate expected and/or actual profiles before comparing. +type CompareProfilesOption interface { + applyOnProfiles(expected, actual pprofile.Profiles) +} + +type compareProfilesOptionFunc func(expected, actual pprofile.Profiles) + +func (f compareProfilesOptionFunc) applyOnProfiles(expected, actual pprofile.Profiles) { + f(expected, actual) +} + +// IgnoreResourceAttributeValue is a CompareProfilesOption that removes a resource attribute +// from all resources. +func IgnoreResourceAttributeValue(attributeName string) CompareProfilesOption { + return ignoreResourceAttributeValue{ + attributeName: attributeName, + } +} + +type ignoreResourceAttributeValue struct { + attributeName string +} + +func (opt ignoreResourceAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfilesResourceAttributeValue(expected) + opt.maskProfilesResourceAttributeValue(actual) +} + +func (opt ignoreResourceAttributeValue) maskProfilesResourceAttributeValue(profiles pprofile.Profiles) { + rls := profiles.ResourceProfiles() + for i := 0; i < rls.Len(); i++ { + internal.MaskResourceAttributeValue(rls.At(i).Resource(), opt.attributeName) + } +} + +// IgnoreProfileRecordAttributeValue is a CompareProfilesOption that sets the value of an attribute +// to empty bytes for every profile record +func IgnoreProfileRecordAttributeValue(attributeName string) CompareProfilesOption { + return ignoreProfileRecordAttributeValue{ + attributeName: attributeName, + } +} + +type ignoreProfileRecordAttributeValue struct { + attributeName string +} + +func (opt ignoreProfileRecordAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfileRecordAttributeValue(expected) + opt.maskProfileRecordAttributeValue(actual) +} + +func (opt ignoreProfileRecordAttributeValue) maskProfileRecordAttributeValue(profiles pprofile.Profiles) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).ProfileRecords() + for k := 0; k < lrs.Len(); k++ { + lr := lrs.At(k) + val, exists := lr.Attributes().Get(opt.attributeName) + if exists { + val.SetEmptyBytes() + } + } + } + } +} + +func IgnoreTimestamp() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + now := pcommon.NewTimestampFromTime(time.Now()) + maskTimestamp(expected, now) + maskTimestamp(actual, now) + }) +} + +func maskTimestamp(profiles pprofile.Profiles, ts pcommon.Timestamp) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).ProfileRecords() + for k := 0; k < lrs.Len(); k++ { + lrs.At(k).SetTimestamp(ts) + } + } + } +} + +func IgnoreObservedTimestamp() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + now := pcommon.NewTimestampFromTime(time.Now()) + maskObservedTimestamp(expected, now) + maskObservedTimestamp(actual, now) + }) +} + +func maskObservedTimestamp(profiles pprofile.Profiles, ts pcommon.Timestamp) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).ProfileRecords() + for k := 0; k < lrs.Len(); k++ { + lrs.At(k).SetObservedTimestamp(ts) + } + } + } +} + +// IgnoreResourceProfilesOrder is a CompareProfilesOption that ignores the order of resource traces/metrics/profiles. +func IgnoreResourceProfilesOrder() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + sortResourceProfilesSlice(expected.ResourceProfiles()) + sortResourceProfilesSlice(actual.ResourceProfiles()) + }) +} + +func sortResourceProfilesSlice(rls pprofile.ResourceProfilesSlice) { + rls.Sort(func(a, b pprofile.ResourceProfiles) bool { + if a.SchemaUrl() != b.SchemaUrl() { + return a.SchemaUrl() < b.SchemaUrl() + } + aAttrs := pdatautil.MapHash(a.Resource().Attributes()) + bAttrs := pdatautil.MapHash(b.Resource().Attributes()) + return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 + }) +} + +// IgnoreScopeProfilesOrder is a CompareProfilesOption that ignores the order of instrumentation scope traces/metrics/profiles. +func IgnoreScopeProfilesOrder() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + sortScopeProfilesSlices(expected) + sortScopeProfilesSlices(actual) + }) +} + +func sortScopeProfilesSlices(ls pprofile.Profiles) { + for i := 0; i < ls.ResourceProfiles().Len(); i++ { + ls.ResourceProfiles().At(i).ScopeProfiles().Sort(func(a, b pprofile.ScopeProfiles) bool { + if a.SchemaUrl() != b.SchemaUrl() { + return a.SchemaUrl() < b.SchemaUrl() + } + if a.Scope().Name() != b.Scope().Name() { + return a.Scope().Name() < b.Scope().Name() + } + return a.Scope().Version() < b.Scope().Version() + }) + } +} + +// IgnoreProfileRecordsOrder is a CompareProfilesOption that ignores the order of profile records. +func IgnoreProfileRecordsOrder() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + sortProfileRecordSlices(expected) + sortProfileRecordSlices(actual) + }) +} + +func sortProfileRecordSlices(ls pprofile.Profiles) { + for i := 0; i < ls.ResourceProfiles().Len(); i++ { + for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { + ls.ResourceProfiles().At(i).ScopeProfiles().At(j).ProfileRecords().Sort(func(a, b pprofile.ProfileRecord) bool { + if a.ObservedTimestamp() != b.ObservedTimestamp() { + return a.ObservedTimestamp() < b.ObservedTimestamp() + } + if a.Timestamp() != b.Timestamp() { + return a.Timestamp() < b.Timestamp() + } + if a.SeverityNumber() != b.SeverityNumber() { + return a.SeverityNumber() < b.SeverityNumber() + } + if a.SeverityText() != b.SeverityText() { + return a.SeverityText() < b.SeverityText() + } + at := a.TraceID() + bt := b.TraceID() + if !bytes.Equal(at[:], bt[:]) { + return bytes.Compare(at[:], bt[:]) < 0 + } + as := a.SpanID() + bs := b.SpanID() + if !bytes.Equal(as[:], bs[:]) { + return bytes.Compare(as[:], bs[:]) < 0 + } + aAttrs := pdatautil.MapHash(a.Attributes()) + bAttrs := pdatautil.MapHash(b.Attributes()) + if !bytes.Equal(aAttrs[:], bAttrs[:]) { + return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 + } + ab := pdatautil.ValueHash(a.Body()) + bb := pdatautil.ValueHash(b.Body()) + return bytes.Compare(ab[:], bb[:]) < 0 + }) + } + } +} diff --git a/pkg/pdatatest/pprofiletest/package_test.go b/pkg/pdatatest/pprofiletest/package_test.go new file mode 100644 index 000000000000..453a8bc92eb6 --- /dev/null +++ b/pkg/pdatatest/pprofiletest/package_test.go @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofiletest + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go new file mode 100644 index 000000000000..b08767bca9a0 --- /dev/null +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -0,0 +1,342 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofiletest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pprofiletest" + +import ( + "fmt" + "reflect" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" + "go.opentelemetry.io/collector/pdata/pprofile" + "go.uber.org/multierr" +) + +// CompareProfiles compares each part of two given Profiles and returns +// an error if they don't match. The error describes what didn't match. +func CompareProfiles(expected, actual pprofile.Profiles, options ...CompareProfilesOption) error { + exp, act := pprofile.NewProfiles(), pprofile.NewProfiles() + expected.CopyTo(exp) + actual.CopyTo(act) + + // r := exp.ResourceProfiles() + // exp.IsReadOnly() + // exp.MarkReadOnly() + // exp.SampleCount() + + // r1 := r.At(0) + // r1.Resource().Attributes() + // r1.Resource().DroppedAttributesCount() + // s := r1.ScopeProfiles().At(0) + + // s.SchemaUrl() + // s.Scope() + + // p := s.Profiles().At(0) + + // p.Attributes() + // p.DroppedAttributesCount() + // p.EndTime() + // pp := p.Profile() + // p.ProfileID() + // p.StartTime() + + // pp.AttributeTable() + // pp.AttributeUnits() + // pp.Comment() + // pp.DefaultSampleType() + // pp.DropFrames() + // pp.Duration() + // pp.Function() + // pp.KeepFrames() + // pp.LinkTable() + // pp.Location() + // pp.LocationIndices() + + for _, option := range options { + option.applyOnProfiles(exp, act) + } + + if exp.IsReadOnly() != act.IsReadOnly() { + return fmt.Errorf("readOnly state differs from expected '%s'", exp.IsReadOnly()) + } + + if exp.SampleCount() != act.SampleCount() { + return fmt.Errorf("sample count state differs from expected '%d', actual '%d'", exp.SampleCount(), act.SampleCount()) + } + + expectedProfiles, actualProfiles := exp.ResourceProfiles(), act.ResourceProfiles() + if expectedProfiles.Len() != actualProfiles.Len() { + return fmt.Errorf("number of resources doesn't match expected: %d, actual: %d", + expectedProfiles.Len(), actualProfiles.Len()) + } + + numResources := expectedProfiles.Len() + + // Keep track of matching resources so that each can only be matched once + matchingResources := make(map[pprofile.ResourceProfiles]pprofile.ResourceProfiles, numResources) + + var errs error + var outOfOrderErrs error + for e := 0; e < numResources; e++ { + er := expectedProfiles.At(e) + var foundMatch bool + for a := 0; a < numResources; a++ { + ar := actualProfiles.At(a) + if _, ok := matchingResources[ar]; ok { + continue + } + if reflect.DeepEqual(er.Resource().Attributes().AsRaw(), ar.Resource().Attributes().AsRaw()) { + foundMatch = true + matchingResources[ar] = er + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`resources are out of order: resource "%v" expected at index %d, found at index %d`, + er.Resource().Attributes().AsRaw(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf("missing expected resource: %v", er.Resource().Attributes().AsRaw())) + } + } + + for i := 0; i < numResources; i++ { + if _, ok := matchingResources[actualProfiles.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf("unexpected resource: %v", actualProfiles.At(i).Resource().Attributes().AsRaw())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for ar, er := range matchingResources { + errPrefix := fmt.Sprintf(`resource "%v"`, er.Resource().Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareResourceProfiles(er, ar))) + } + + return errs +} + +// CompareResourceProfiles compares each part of two given ResourceProfiles and returns +// an error if they don't match. The error describes what didn't match. +func CompareResourceProfiles(expected, actual pprofile.ResourceProfiles) error { + errs := multierr.Combine( + internal.CompareResource(expected.Resource(), actual.Resource()), + internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()), + ) + + esls := expected.ScopeProfiles() + asls := actual.ScopeProfiles() + + if esls.Len() != asls.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of scopes doesn't match expected: %d, actual: %d", esls.Len(), + asls.Len())) + return errs + } + + numScopeProfiles := esls.Len() + + // Keep track of matching scope profiles so that each container can only be matched once + matchingScopeProfiles := make(map[pprofile.ScopeProfiles]pprofile.ScopeProfiles, numScopeProfiles) + + var outOfOrderErrs error + for e := 0; e < numScopeProfiles; e++ { + esl := expected.ScopeProfiles().At(e) + var foundMatch bool + for a := 0; a < numScopeProfiles; a++ { + asl := actual.ScopeProfiles().At(a) + if _, ok := matchingScopeProfiles[asl]; ok { + continue + } + if esl.Scope().Name() == asl.Scope().Name() { + foundMatch = true + matchingScopeProfiles[asl] = esl + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf("scopes are out of order: scope %s expected at index %d, found at index %d", + esl.Scope().Name(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf("missing expected scope: %s", esl.Scope().Name())) + } + } + + for i := 0; i < numScopeProfiles; i++ { + if _, ok := matchingScopeProfiles[actual.ScopeProfiles().At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf("unexpected scope: %s", actual.ScopeProfiles().At(i).Scope().Name())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for i := 0; i < esls.Len(); i++ { + errPrefix := fmt.Sprintf(`scope "%s"`, esls.At(i).Scope().Name()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareScopeProfiles(esls.At(i), asls.At(i)))) + } + + return errs +} + +// CompareScopeProfiles compares each part of two given ProfilesSlices and returns +// an error if they don't match. The error describes what didn't match. +func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error { + errs := multierr.Combine( + internal.CompareInstrumentationScope(expected.Scope(), actual.Scope()), + internal.CompareSchemaURL(expected.SchemaUrl(), actual.SchemaUrl()), + ) + + if expected.Profiles().Len() != actual.Profiles().Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile containers doesn't match expected: %d, actual: %d", + expected.Profiles().Len(), actual.Profiles().Len())) + return errs + } + + numProfiles := expected.Profiles().Len() + + // Keep track of matching containers so that each container can only be matched once + matchingProfiles := make(map[pprofile.ProfileContainer]pprofile.ProfileContainer, numProfiles) + + var outOfOrderErrs error + for e := 0; e < numProfiles; e++ { + elr := expected.Profiles().At(e) + var foundMatch bool + for a := 0; a < numProfiles; a++ { + alr := actual.Profiles().At(a) + if _, ok := matchingProfiles[alr]; ok { + continue + } + if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) { + foundMatch = true + matchingProfiles[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`profile containers are out of order: profile container "%v" expected at index %d, found at index %d`, + elr.Attributes().AsRaw(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf("missing expected profile container: %v", elr.Attributes().AsRaw())) + } + } + + for i := 0; i < numProfiles; i++ { + if _, ok := matchingProfiles[actual.Profiles().At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf("unexpected profile container: %v", + actual.Profiles().At(i).Attributes().AsRaw())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingProfiles { + errPrefix := fmt.Sprintf(`profile container "%v"`, elr.Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileContainer(elr, alr))) + } + return errs +} + +func CompareProfileContainer(expected, actual pprofile.ProfileContainer) error { + errs := multierr.Combine( + internal.CompareAttributes(expected.Attributes(), actual.Attributes()), + internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()), + ) + + if expected.ProfileID().String() != actual.ProfileID().String() { + errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%s', actual '%s'", expected.ProfileID().String(), actual.ProfileID().String())) + } + + if expected.StartTime() != actual.StartTime() { + errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, "+"actual: %d", expected.StartTime(), actual.StartTime())) + } + + if expected.EndTime() != actual.EndTime() { + errs = multierr.Append(errs, fmt.Errorf("end timestamp doesn't match expected: %d, "+"actual: %d", expected.EndTime(), actual.EndTime())) + } + + errPrefix := fmt.Sprintf(`profile "%v"`, expected.Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfile(expected.Profile(), actual.Profile()))) + + return errs +} + +func CompareProfile(expected, actual pprofile.Profile) error { + errs := multierr.Combine( + internal.CompareAttributes(expected.AttributeTable(), actual.AttributeTable()), + ) + + if !reflect.DeepEqual(expected.LocationIndices(), actual.LocationIndices()) { + errs = multierr.Append(errs, fmt.Errorf("locationIndicies do not match expected")) + } + + if !reflect.DeepEqual(expected.Comment(), actual.Comment()) { + errs = multierr.Append(errs, fmt.Errorf("comment does not match expected")) + } + + if !reflect.DeepEqual(expected.StringTable(), actual.StringTable()) { + errs = multierr.Append(errs, fmt.Errorf("stringTable does not match expected")) + } + + if expected.DropFrames() != actual.DropFrames() { + errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.DropFrames(), actual.DropFrames())) + } + + if expected.KeepFrames() != actual.KeepFrames() { + errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.KeepFrames(), actual.KeepFrames())) + } + + if expected.StartTime() != actual.StartTime() { + errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d", expected.StartTime(), actual.StartTime())) + } + + if expected.Duration() != actual.Duration() { + errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d", expected.Duration(), actual.Duration())) + } + + if expected.Period() != actual.Period() { + errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.Period(), actual.Period())) + } + + if expected.DefaultSampleType() != actual.DefaultSampleType() { + errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.DefaultSampleType(), actual.DefaultSampleType())) + } + + // sampletype + expected.SampleType() + // sample + expected.Sample() + // mapping + expected.Mapping() + //location + expected.Location() + //function + expected.Function() + //attributeunits + expected.AttributeUnits() + //linktable + expected.LinkTable() + //periodtype + expected.PeriodType() + + return errs +} From 72b82a5ba667af6fbc8c1353ea60f7cdc73ecc5b Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 11:54:43 +0100 Subject: [PATCH 02/22] finished implementation Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 712 +++++++++++++++++++++++-- 1 file changed, 662 insertions(+), 50 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index b08767bca9a0..a00f8baa87ea 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -19,40 +19,6 @@ func CompareProfiles(expected, actual pprofile.Profiles, options ...CompareProfi expected.CopyTo(exp) actual.CopyTo(act) - // r := exp.ResourceProfiles() - // exp.IsReadOnly() - // exp.MarkReadOnly() - // exp.SampleCount() - - // r1 := r.At(0) - // r1.Resource().Attributes() - // r1.Resource().DroppedAttributesCount() - // s := r1.ScopeProfiles().At(0) - - // s.SchemaUrl() - // s.Scope() - - // p := s.Profiles().At(0) - - // p.Attributes() - // p.DroppedAttributesCount() - // p.EndTime() - // pp := p.Profile() - // p.ProfileID() - // p.StartTime() - - // pp.AttributeTable() - // pp.AttributeUnits() - // pp.Comment() - // pp.DefaultSampleType() - // pp.DropFrames() - // pp.Duration() - // pp.Function() - // pp.KeepFrames() - // pp.LinkTable() - // pp.Location() - // pp.LocationIndices() - for _, option := range options { option.applyOnProfiles(exp, act) } @@ -321,22 +287,668 @@ func CompareProfile(expected, actual pprofile.Profile) error { errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.DefaultSampleType(), actual.DefaultSampleType())) } - // sampletype - expected.SampleType() - // sample - expected.Sample() - // mapping - expected.Mapping() - //location - expected.Location() - //function - expected.Function() - //attributeunits - expected.AttributeUnits() - //linktable - expected.LinkTable() - //periodtype - expected.PeriodType() + if expected.PeriodType().Type() != actual.PeriodType().Type() || + expected.PeriodType().Unit() != actual.PeriodType().Unit() || + expected.PeriodType().AggregationTemporality() != actual.PeriodType().AggregationTemporality() { + errs = multierr.Append(errs, fmt.Errorf("periodType does not match expected 'unit: %d, type: %d, aggregationTemporality: %d', actual 'unit: %d, type: %d,"+ + "aggregationTemporality: %d'", expected.PeriodType().Unit(), expected.PeriodType().Type(), expected.PeriodType().AggregationTemporality(), + actual.PeriodType().Unit(), actual.PeriodType().Type(), actual.PeriodType().AggregationTemporality())) + } + + errs = multierr.Append(errs, CompareProfileValueTypeSlice(expected.SampleType(), actual.SampleType())) + + errs = multierr.Append(errs, CompareProfileSampleSlice(expected.Sample(), actual.Sample())) + + errs = multierr.Append(errs, CompareProfileMappingSlice(expected.Mapping(), actual.Mapping())) + + errs = multierr.Append(errs, CompareProfileLocationSlice(expected.Location(), actual.Location())) + + errs = multierr.Append(errs, CompareProfileFunctionSlice(expected.Function(), actual.Function())) + + errs = multierr.Append(errs, CompareProfileAttributeUnitSlice(expected.AttributeUnits(), actual.AttributeUnits())) + + errs = multierr.Append(errs, CompareProfileLinkSlice(expected.LinkTable(), actual.LinkTable())) + + return errs +} + +func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numValueTypes := expected.Len() + + matchingValueTypes := make(map[pprofile.ValueType]pprofile.ValueType, numValueTypes) + + var outOfOrderErrs error + for e := 0; e < numValueTypes; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numValueTypes; a++ { + alr := actual.At(a) + if _, ok := matchingValueTypes[alr]; ok { + continue + } + if elr.Type() == alr.Type() && elr.Unit() == alr.Unit() { + foundMatch = true + matchingValueTypes[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`profile valueTypes are out of order: profile valueType "unit: %d, type: %d, aggregationTemporality: %d" expected at index %d, found at index %d`, + elr.Unit(), elr.Type(), elr.AggregationTemporality(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected profile valueType "unit: %d, type: %d, aggregationTemporality: %d`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) + } + } + + for i := 0; i < numValueTypes; i++ { + if _, ok := matchingValueTypes[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile valueType "unit: %d, type: %d, aggregationTemporality: %d`, + actual.At(i).Unit(), actual.At(i).Type(), actual.At(i).AggregationTemporality())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingValueTypes { + if !isValueTypeEqual(elr, alr) { + errs = multierr.Append(errs, fmt.Errorf(`expected profile valueType "unit: %d, type: %d, aggregationTemporality: %d",`+ + `got "unit: %d, type: %d, aggregationTemporality: %d"`, elr.Unit(), elr.Type(), elr.AggregationTemporality(), + alr.Unit(), alr.Type(), alr.AggregationTemporality())) + } + } + + return errs +} + +func isValueTypeEqual(expected, actual pprofile.ValueType) bool { + return expected.Type() == actual.Type() && + expected.Unit() == actual.Unit() && + expected.AggregationTemporality() == actual.AggregationTemporality() +} + +func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile sample doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numSlice := expected.Len() + + matchingItems := make(map[pprofile.Sample]pprofile.Sample, numSlice) + + var outOfOrderErrs error + for e := 0; e < numSlice; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numSlice; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`samples are out of order: sample "%v" expected at index %d, found at index %d`, + elr.Attributes().AsRaw(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected profile sample: %v`, elr.Attributes().AsRaw())) + } + } + + for i := 0; i < numSlice; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf("unexpected profile sample: %v", + actual.At(i).Attributes().AsRaw())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingItems { + errPrefix := fmt.Sprintf(`profile sample "%v"`, elr.Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileSample(elr, alr))) + } + + return errs +} + +func CompareProfileSample(expected, actual pprofile.Sample) error { + var errs error + if expected.LocationsStartIndex() != actual.LocationsStartIndex() { + errs = multierr.Append(errs, fmt.Errorf("expected locationStartIndex '%d', got '%d'", expected.LocationsStartIndex(), actual.LocationsStartIndex())) + } + + if expected.LocationsLength() != actual.LocationsLength() { + errs = multierr.Append(errs, fmt.Errorf("expected locationLenght '%d', got '%d'", expected.LocationsLength(), actual.LocationsLength())) + } + + if expected.StacktraceIdIndex() != actual.StacktraceIdIndex() { + errs = multierr.Append(errs, fmt.Errorf("expected stacktraceIdIndex '%d', got '%d'", expected.StacktraceIdIndex(), actual.StacktraceIdIndex())) + } + + if expected.Link() != actual.Link() { + errs = multierr.Append(errs, fmt.Errorf("expected link '%d', got '%d'", expected.Link(), actual.Link())) + } + + if !reflect.DeepEqual(expected.LocationIndex().AsRaw(), actual.LocationIndex().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected locationIndex '%v', got '%v'", expected.LocationIndex().AsRaw(), actual.LocationIndex().AsRaw())) + } + + if !reflect.DeepEqual(expected.Value().AsRaw(), actual.Value().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected value '%v', got '%v'", expected.Value().AsRaw(), actual.Value().AsRaw())) + } + + if !reflect.DeepEqual(expected.TimestampsUnixNano().AsRaw(), actual.TimestampsUnixNano().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected timestampUnixNano '%v', got '%v'", expected.TimestampsUnixNano().AsRaw(), actual.TimestampsUnixNano().AsRaw())) + } + + if !reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) + } + + errs = multierr.Append(errs, CompareProfileLabelSlice(expected.Label(), actual.Label())) + + return errs +} + +func CompareProfileLabelSlice(expected, actual pprofile.LabelSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numLabels := expected.Len() + + matchingLabels := make(map[pprofile.Label]pprofile.Label, numLabels) + + var outOfOrderErrs error + for e := 0; e < numLabels; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numLabels; a++ { + alr := actual.At(a) + if _, ok := matchingLabels[alr]; ok { + continue + } + if elr.Key() == alr.Key() { + foundMatch = true + matchingLabels[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`labels are out of order: label "key: %d" expected at index %d, found at index %d`, + elr.Key(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected label "key: %d`, elr.Key())) + } + } + + for i := 0; i < numLabels; i++ { + if _, ok := matchingLabels[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected label "key: %d`, + actual.At(i).Key())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingLabels { + if !isLabelEqual(elr, alr) { + errs = multierr.Append(errs, fmt.Errorf(`label with "key: %d" does not match expected,`, alr.Key())) + } + } + + return errs +} + +func isLabelEqual(expected, actual pprofile.Label) bool { + return expected.Key() == actual.Key() && + expected.Str() == actual.Str() && + expected.Num() == actual.Num() && + expected.NumUnit() == actual.NumUnit() +} + +func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.Mapping]pprofile.Mapping, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) && elr.ID() == alr.ID() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`mapping are out of order: mapping "attributes: %v, id: %d" expected at index %d, found at index %d`, + elr.Attributes().AsRaw(), elr.ID(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected mapping "attributes: %v, id: %d"`, elr.Attributes().AsRaw(), elr.ID())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile mapping "attributes: %v, id: %d"`, + actual.At(i).Attributes().AsRaw(), actual.At(i).ID())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingItems { + if !isMappingEqual(elr, alr) { + errs = multierr.Append(errs, fmt.Errorf(`mapping with "attributes: %v, id: %d", does not match expected`, + alr.Attributes().AsRaw(), alr.ID())) + } + } + + return errs +} + +func isMappingEqual(expected, actual pprofile.Mapping) bool { + return expected.ID() == actual.ID() && + expected.MemoryStart() == actual.MemoryStart() && + expected.MemoryLimit() == actual.MemoryLimit() && + expected.FileOffset() == actual.FileOffset() && + expected.Filename() == actual.Filename() && + expected.BuildID() == actual.BuildID() && + expected.BuildIDKind() == actual.BuildIDKind() && + reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) && + expected.HasFunctions() == actual.HasFunctions() && + expected.HasFilenames() == actual.HasFilenames() && + expected.HasLineNumbers() == actual.HasLineNumbers() && + expected.HasInlineFrames() == actual.HasInlineFrames() +} + +func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.Function]pprofile.Function, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if elr.Name() == alr.Name() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`function are out of order: function "name: %d" expected at index %d, found at index %d`, + elr.Name(), elr.ID(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected function "name: %d"`, elr.Name())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile function "name: %d"`, + actual.At(i).Name())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingItems { + if !isFunctionEqual(elr, alr) { + errs = multierr.Append(errs, fmt.Errorf(`function with "name: %d" does not match expected`, alr.Name())) + } + } + + return errs +} + +func isFunctionEqual(expected, actual pprofile.Function) bool { + return expected.ID() == actual.ID() && + expected.Name() == actual.Name() && + expected.SystemName() == actual.SystemName() && + expected.StartLine() == actual.StartLine() && + expected.Filename() == actual.Filename() +} + +func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.Location]pprofile.Location, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) && elr.ID() == alr.ID() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`location is out of order: location "attributes: %v, id: %d" expected at index %d, found at index %d`, + elr.Attributes().AsRaw(), elr.ID(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected location "attributes: %v, id: %d"`, elr.Attributes().AsRaw(), elr.ID())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected location "attributes: %v, id: %d"`, + actual.At(i).Attributes().AsRaw(), actual.At(i).ID())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingItems { + errPrefix := fmt.Sprintf(`profile location "id: %d"`, elr.ID()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLocation(elr, alr))) + } + + return errs +} + +func CompareProfileLocation(expected, actual pprofile.Location) error { + var errs error + if expected.ID() != actual.ID() { + return fmt.Errorf("expected id '%d', got '%d'", expected.ID(), actual.ID()) + } + + if expected.MappingIndex() != actual.MappingIndex() { + return fmt.Errorf("expected mappingIndex '%d', got '%d'", expected.MappingIndex(), actual.MappingIndex()) + } + + if expected.Address() != actual.Address() { + return fmt.Errorf("expected address '%d', got '%d'", expected.Address(), actual.Address()) + } + + if expected.IsFolded() != actual.IsFolded() { + return fmt.Errorf("expected isFolded '%v', got '%v'", expected.IsFolded(), actual.IsFolded()) + } + + if expected.TypeIndex() != actual.TypeIndex() { + return fmt.Errorf("expected typeIndex '%d', got '%d'", expected.TypeIndex(), actual.TypeIndex()) + } + + if !reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) + } + + errs = multierr.Append(errs, CompareProfileLineSlice(expected.Line(), actual.Line())) + + return errs +} + +func CompareProfileLineSlice(expected, actual pprofile.LineSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.Line]pprofile.Line, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if elr.FunctionIndex() == alr.FunctionIndex() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`lines are out of order: line "functionIndex: %d" expected at index %d, found at index %d`, + elr.FunctionIndex(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected line "functionIndex: %d"`, elr.FunctionIndex())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile line "functionIndex: %d"`, + actual.At(i).FunctionIndex())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + for alr, elr := range matchingItems { + if !isLineEqual(elr, alr) { + errs = multierr.Append(errs, fmt.Errorf(`line with "functionIndex: %d" does not match expected`, alr.FunctionIndex())) + } + } + + return errs +} + +func isLineEqual(expected, actual pprofile.Line) bool { + return expected.FunctionIndex() == actual.FunctionIndex() && + expected.Line() == actual.Line() && + expected.Column() == actual.Column() +} + +func CompareProfileAttributeUnitSlice(expected, actual pprofile.AttributeUnitSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.AttributeUnit]pprofile.AttributeUnit, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if elr.AttributeKey() == alr.AttributeKey() && elr.Unit() == alr.Unit() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`attributeUnit are out of order: attributeUnit "attributeKey: %d" expected at index %d, found at index %d`, + elr.AttributeKey(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected attributeUnit "attributeKey: %d"`, elr.AttributeKey())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile attributeUnit "attributeKey: %d"`, + actual.At(i).AttributeKey())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } + + return errs +} + +func CompareProfileLinkSlice(expected, actual pprofile.LinkSlice) error { + var errs error + if expected.Len() != actual.Len() { + errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + expected.Len(), actual.Len())) + return errs + } + + numItems := expected.Len() + + matchingItems := make(map[pprofile.Link]pprofile.Link, numItems) + + var outOfOrderErrs error + for e := 0; e < numItems; e++ { + elr := expected.At(e) + var foundMatch bool + for a := 0; a < numItems; a++ { + alr := actual.At(a) + if _, ok := matchingItems[alr]; ok { + continue + } + if elr.TraceID().String() == alr.TraceID().String() && elr.SpanID().String() == alr.SpanID().String() { + foundMatch = true + matchingItems[alr] = elr + if e != a { + outOfOrderErrs = multierr.Append(outOfOrderErrs, + fmt.Errorf(`links are out of order: link "spanId: %s, traceId: %s" expected at index %d, found at index %d`, + elr.SpanID(), elr.TraceID(), e, a)) + } + break + } + } + if !foundMatch { + errs = multierr.Append(errs, fmt.Errorf(`missing expected link "spanId: %s, traceId: %s"`, elr.SpanID(), elr.TraceID())) + } + } + + for i := 0; i < numItems; i++ { + if _, ok := matchingItems[actual.At(i)]; !ok { + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile link "spanId: %s, traceId: %s"`, + actual.At(i).SpanID(), actual.At(i).TraceID())) + } + } + + if errs != nil { + return errs + } + if outOfOrderErrs != nil { + return outOfOrderErrs + } return errs } From 52aa6195a47b6ed4031b2140eb61dbb5ab01f64d Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 13:29:00 +0100 Subject: [PATCH 03/22] polish options Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 119 ++++++++------------------ 1 file changed, 38 insertions(+), 81 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index deb31673ba70..60e9fb176f22 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -5,9 +5,7 @@ package pprofiletest // import "github.com/open-telemetry/opentelemetry-collecto import ( "bytes" - "time" - "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pprofile" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" @@ -49,77 +47,67 @@ func (opt ignoreResourceAttributeValue) maskProfilesResourceAttributeValue(profi } } -// IgnoreProfileRecordAttributeValue is a CompareProfilesOption that sets the value of an attribute -// to empty bytes for every profile record -func IgnoreProfileRecordAttributeValue(attributeName string) CompareProfilesOption { - return ignoreProfileRecordAttributeValue{ +// IgnoreResourceAttributeValue is a CompareProfilesOption that removes a resource attribute +// from all resources. +func IgnoreScopeAttributeValue(attributeName string) CompareProfilesOption { + return ignoreScopeAttributeValue{ attributeName: attributeName, } } -type ignoreProfileRecordAttributeValue struct { +type ignoreScopeAttributeValue struct { attributeName string } -func (opt ignoreProfileRecordAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { - opt.maskProfileRecordAttributeValue(expected) - opt.maskProfileRecordAttributeValue(actual) +func (opt ignoreScopeAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfilesScopeAttributeValue(expected) + opt.maskProfilesScopeAttributeValue(actual) } -func (opt ignoreProfileRecordAttributeValue) maskProfileRecordAttributeValue(profiles pprofile.Profiles) { +func (opt ignoreScopeAttributeValue) maskProfilesScopeAttributeValue(profiles pprofile.Profiles) { rls := profiles.ResourceProfiles() for i := 0; i < profiles.ResourceProfiles().Len(); i++ { sls := rls.At(i).ScopeProfiles() for j := 0; j < sls.Len(); j++ { - lrs := sls.At(j).ProfileRecords() - for k := 0; k < lrs.Len(); k++ { - lr := lrs.At(k) - val, exists := lr.Attributes().Get(opt.attributeName) - if exists { - val.SetEmptyBytes() - } + lr := sls.At(j) + val, exists := lr.Scope().Attributes().Get(opt.attributeName) + if exists { + val.SetEmptyBytes() } + } } } -func IgnoreTimestamp() CompareProfilesOption { - return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - now := pcommon.NewTimestampFromTime(time.Now()) - maskTimestamp(expected, now) - maskTimestamp(actual, now) - }) +// IgnoreProfileContainerAttributeValue is a CompareProfilesOption that sets the value of an attribute +// to empty bytes for every profile +func IgnoreProfileContainerAttributeValue(attributeName string) CompareProfilesOption { + return ignoreProfileContainerAttributeValue{ + attributeName: attributeName, + } } -func maskTimestamp(profiles pprofile.Profiles, ts pcommon.Timestamp) { - rls := profiles.ResourceProfiles() - for i := 0; i < profiles.ResourceProfiles().Len(); i++ { - sls := rls.At(i).ScopeProfiles() - for j := 0; j < sls.Len(); j++ { - lrs := sls.At(j).ProfileRecords() - for k := 0; k < lrs.Len(); k++ { - lrs.At(k).SetTimestamp(ts) - } - } - } +type ignoreProfileContainerAttributeValue struct { + attributeName string } -func IgnoreObservedTimestamp() CompareProfilesOption { - return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - now := pcommon.NewTimestampFromTime(time.Now()) - maskObservedTimestamp(expected, now) - maskObservedTimestamp(actual, now) - }) +func (opt ignoreProfileContainerAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfileContainerAttributeValue(expected) + opt.maskProfileContainerAttributeValue(actual) } -func maskObservedTimestamp(profiles pprofile.Profiles, ts pcommon.Timestamp) { +func (opt ignoreProfileContainerAttributeValue) maskProfileContainerAttributeValue(profiles pprofile.Profiles) { rls := profiles.ResourceProfiles() for i := 0; i < profiles.ResourceProfiles().Len(); i++ { sls := rls.At(i).ScopeProfiles() for j := 0; j < sls.Len(); j++ { - lrs := sls.At(j).ProfileRecords() + lrs := sls.At(j).Profiles() for k := 0; k < lrs.Len(); k++ { - lrs.At(k).SetObservedTimestamp(ts) + lr := lrs.At(k) + val, exists := lr.Attributes().Get(opt.attributeName) + if exists { + val.SetEmptyBytes() + } } } } @@ -166,49 +154,18 @@ func sortScopeProfilesSlices(ls pprofile.Profiles) { } } -// IgnoreProfileRecordsOrder is a CompareProfilesOption that ignores the order of profile records. -func IgnoreProfileRecordsOrder() CompareProfilesOption { +// IgnoreProfilesOrder is a CompareProfilesOption that ignores the order of profile records. +func IgnoreProfileContainersOrder() CompareProfilesOption { return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - sortProfileRecordSlices(expected) - sortProfileRecordSlices(actual) + sortProfileContainerSlices(expected) + sortProfileContainerSlices(actual) }) } -func sortProfileRecordSlices(ls pprofile.Profiles) { +func sortProfileContainerSlices(ls pprofile.Profiles) { for i := 0; i < ls.ResourceProfiles().Len(); i++ { for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { - ls.ResourceProfiles().At(i).ScopeProfiles().At(j).ProfileRecords().Sort(func(a, b pprofile.ProfileRecord) bool { - if a.ObservedTimestamp() != b.ObservedTimestamp() { - return a.ObservedTimestamp() < b.ObservedTimestamp() - } - if a.Timestamp() != b.Timestamp() { - return a.Timestamp() < b.Timestamp() - } - if a.SeverityNumber() != b.SeverityNumber() { - return a.SeverityNumber() < b.SeverityNumber() - } - if a.SeverityText() != b.SeverityText() { - return a.SeverityText() < b.SeverityText() - } - at := a.TraceID() - bt := b.TraceID() - if !bytes.Equal(at[:], bt[:]) { - return bytes.Compare(at[:], bt[:]) < 0 - } - as := a.SpanID() - bs := b.SpanID() - if !bytes.Equal(as[:], bs[:]) { - return bytes.Compare(as[:], bs[:]) < 0 - } - aAttrs := pdatautil.MapHash(a.Attributes()) - bAttrs := pdatautil.MapHash(b.Attributes()) - if !bytes.Equal(aAttrs[:], bAttrs[:]) { - return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 - } - ab := pdatautil.ValueHash(a.Body()) - bb := pdatautil.ValueHash(b.Body()) - return bytes.Compare(ab[:], bb[:]) < 0 - }) + ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { return true }) } } } From 486ad3380cf451f04ef201f7ec6161268ec14e79 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 13:35:31 +0100 Subject: [PATCH 04/22] fix errors Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index a00f8baa87ea..8e34fe60af31 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -24,7 +24,7 @@ func CompareProfiles(expected, actual pprofile.Profiles, options ...CompareProfi } if exp.IsReadOnly() != act.IsReadOnly() { - return fmt.Errorf("readOnly state differs from expected '%s'", exp.IsReadOnly()) + return fmt.Errorf("readOnly state differs from expected '%t'", exp.IsReadOnly()) } if exp.SampleCount() != act.SampleCount() { @@ -648,7 +648,7 @@ func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`function are out of order: function "name: %d" expected at index %d, found at index %d`, - elr.Name(), elr.ID(), e, a)) + elr.Name(), e, a)) } break } From 09ffbcaed4d2f6c749ae4c5de3d1023323c845a3 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 13:47:58 +0100 Subject: [PATCH 05/22] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 60e9fb176f22..1788556afa75 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -165,7 +165,7 @@ func IgnoreProfileContainersOrder() CompareProfilesOption { func sortProfileContainerSlices(ls pprofile.Profiles) { for i := 0; i < ls.ResourceProfiles().Len(); i++ { for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { - ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { return true }) + //ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { return true }) } } } From 5cca324bcf7e2f6fe7aee079e7caab6b6f065e0b Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 13:53:05 +0100 Subject: [PATCH 06/22] make generate Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index 8e34fe60af31..2492f4e72b80 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -7,9 +7,10 @@ import ( "fmt" "reflect" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" "go.opentelemetry.io/collector/pdata/pprofile" "go.uber.org/multierr" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" ) // CompareProfiles compares each part of two given Profiles and returns From 62339a48d55fa0cef9da06fc63a2dadf9056beba Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 13:55:25 +0100 Subject: [PATCH 07/22] add chlog Signed-off-by: odubajDT --- .chloggen/profiles-pdata.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/profiles-pdata.yaml diff --git a/.chloggen/profiles-pdata.yaml b/.chloggen/profiles-pdata.yaml new file mode 100644 index 000000000000..631f5e603a5a --- /dev/null +++ b/.chloggen/profiles-pdata.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/pdatatest + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add support for Profiles signal comparison." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36232] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From b2f9742be31104f9437445ac096d99085d95b2a3 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 8 Nov 2024 14:08:46 +0100 Subject: [PATCH 08/22] sort profileContainer slices Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 1788556afa75..8218ce772aca 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -165,7 +165,22 @@ func IgnoreProfileContainersOrder() CompareProfilesOption { func sortProfileContainerSlices(ls pprofile.Profiles) { for i := 0; i < ls.ResourceProfiles().Len(); i++ { for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { - //ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { return true }) + ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { + if a.StartTime() != b.StartTime() { + return a.StartTime() < b.StartTime() + } + if a.EndTime() != b.EndTime() { + return a.EndTime() < b.EndTime() + } + as := a.ProfileID() + bs := b.ProfileID() + if !bytes.Equal(as[:], bs[:]) { + return bytes.Compare(as[:], bs[:]) < 0 + } + aAttrs := pdatautil.MapHash(a.Attributes()) + bAttrs := pdatautil.MapHash(b.Attributes()) + return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 + }) } } } From 390ec27b652812572d7b0793036e7c1171f3ec14 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 11 Nov 2024 08:20:03 +0100 Subject: [PATCH 09/22] add options for profile container and profile Signed-off-by: odubajDT --- pkg/pdatatest/internal/util.go | 20 ++++ pkg/pdatatest/pmetrictest/options.go | 30 +----- pkg/pdatatest/pprofiletest/options.go | 142 ++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 25 deletions(-) diff --git a/pkg/pdatatest/internal/util.go b/pkg/pdatatest/internal/util.go index f9b96dfc7b6a..9213ab8dbbff 100644 --- a/pkg/pdatatest/internal/util.go +++ b/pkg/pdatatest/internal/util.go @@ -7,6 +7,7 @@ import ( "fmt" "reflect" "regexp" + "sort" "go.opentelemetry.io/collector/pdata/pcommon" "go.uber.org/multierr" @@ -88,3 +89,22 @@ func CompareDroppedAttributesCount(expected, actual uint32) error { } 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 +} diff --git a/pkg/pdatatest/pmetrictest/options.go b/pkg/pdatatest/pmetrictest/options.go index 883e8fa2b3b1..3a6ce4cb9651 100644 --- a/pkg/pdatatest/pmetrictest/options.go +++ b/pkg/pdatatest/pmetrictest/options.go @@ -8,7 +8,6 @@ import ( "fmt" "math" "regexp" - "sort" "time" "go.opentelemetry.io/collector/pdata/pcommon" @@ -275,27 +274,27 @@ func orderDatapointAttributes(metrics pmetric.Metrics) { 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()) + rawOrdered := internal.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()) + rawOrdered := internal.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()) + rawOrdered := internal.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()) + rawOrdered := internal.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()) + rawOrdered := internal.OrderMapByKey(msl.At(g).Summary().DataPoints().At(k).Attributes().AsRaw()) _ = msl.At(g).Summary().DataPoints().At(k).Attributes().FromRaw(rawOrdered) } case pmetric.MetricTypeEmpty: @@ -305,25 +304,6 @@ func orderDatapointAttributes(metrics pmetric.Metrics) { } } -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++ { diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 8218ce772aca..4321da86f3be 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -5,7 +5,9 @@ package pprofiletest // import "github.com/open-telemetry/opentelemetry-collecto import ( "bytes" + "time" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pprofile" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" @@ -113,6 +115,146 @@ func (opt ignoreProfileContainerAttributeValue) maskProfileContainerAttributeVal } } +// IgnoreProfileAttributeValue is a CompareProfilesOption that sets the value of an attribute +// to empty bytes for every profile +func IgnoreProfileAttributeValue(attributeName string) CompareProfilesOption { + return ignoreProfileAttributeValue{ + attributeName: attributeName, + } +} + +type ignoreProfileAttributeValue struct { + attributeName string +} + +func (opt ignoreProfileAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfileAttributeValue(expected) + opt.maskProfileAttributeValue(actual) +} + +func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofile.Profiles) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).Profiles() + for k := 0; k < lrs.Len(); k++ { + lr := lrs.At(k).Profile() + val, exists := lr.AttributeTable().Get(opt.attributeName) + if exists { + val.SetEmptyBytes() + } + } + } + } +} + +// IgnoreProfileContainerTimestampValues is a CompareProfilesOption that sets the value of start and end timestamp +// to empty bytes for every profile +func IgnoreProfileContainerTimestampValues(attributeName string) CompareProfilesOption { + return ignoreProfileContainerTimestampValues{} +} + +type ignoreProfileContainerTimestampValues struct { +} + +func (opt ignoreProfileContainerTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfileContainerTimestampValues(expected) + opt.maskProfileContainerTimestampValues(actual) +} + +func (opt ignoreProfileContainerTimestampValues) maskProfileContainerTimestampValues(profiles pprofile.Profiles) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).Profiles() + for k := 0; k < lrs.Len(); k++ { + lr := lrs.At(k) + lr.SetStartTime(pcommon.NewTimestampFromTime(time.Time{})) + lr.SetEndTime(pcommon.NewTimestampFromTime(time.Time{})) + } + } + } +} + +// IgnoreProfileTimestampValues is a CompareProfilesOption that sets the value of start timestamp +// and duration to empty bytes for every profile +func IgnoreProfileTimestampValues(attributeName string) CompareProfilesOption { + return ignoreProfileTimestampValues{} +} + +type ignoreProfileTimestampValues struct { +} + +func (opt ignoreProfileTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { + opt.maskProfileTimestampValues(expected) + opt.maskProfileTimestampValues(actual) +} + +func (opt ignoreProfileTimestampValues) maskProfileTimestampValues(profiles pprofile.Profiles) { + rls := profiles.ResourceProfiles() + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { + sls := rls.At(i).ScopeProfiles() + for j := 0; j < sls.Len(); j++ { + lrs := sls.At(j).Profiles() + for k := 0; k < lrs.Len(); k++ { + lr := lrs.At(k).Profile() + lr.SetStartTime(pcommon.NewTimestampFromTime(time.Time{})) + lr.SetDuration(pcommon.NewTimestampFromTime(time.Time{})) + } + } + } +} + +// IgnoreProfileContainerAttributesOrder is a CompareprofilesOption that ignores the order of profile container attributes. +func IgnoreProfileContainerAttributesOrder() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + orderProfileContainerAttributes(expected) + orderProfileContainerAttributes(actual) + }) +} + +func orderProfileContainerAttributes(metrics pprofile.Profiles) { + rms := metrics.ResourceProfiles() + for i := 0; i < rms.Len(); i++ { + ilms := rms.At(i).ScopeProfiles() + for j := 0; j < ilms.Len(); j++ { + msl := ilms.At(j).Profiles() + for g := 0; g < msl.Len(); g++ { + for k := 0; k < msl.At(g).Attributes().Len(); k++ { + rawOrdered := internal.OrderMapByKey(msl.At(g).Attributes().AsRaw()) + _ = msl.At(g).Attributes().FromRaw(rawOrdered) + } + } + } + } +} + +// IgnoreProfileAttributesOrder is a CompareprofilesOption that ignores the order of profile attributes. +func IgnoreProfileAttributesOrder() CompareProfilesOption { + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { + orderProfileAttributes(expected) + orderProfileAttributes(actual) + }) +} + +func orderProfileAttributes(metrics pprofile.Profiles) { + rms := metrics.ResourceProfiles() + for i := 0; i < rms.Len(); i++ { + ilms := rms.At(i).ScopeProfiles() + for j := 0; j < ilms.Len(); j++ { + msl := ilms.At(j).Profiles() + for g := 0; g < msl.Len(); g++ { + for k := 0; k < msl.At(g).Profile().AttributeTable().Len(); k++ { + rawOrdered := internal.OrderMapByKey(msl.At(g).Profile().AttributeTable().AsRaw()) + _ = msl.At(g).Profile().AttributeTable().FromRaw(rawOrdered) + } + } + } + } +} + // IgnoreResourceProfilesOrder is a CompareProfilesOption that ignores the order of resource traces/metrics/profiles. func IgnoreResourceProfilesOrder() CompareProfilesOption { return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { From 3a0a8c805732bb88566e307c0185dbac22b20be0 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 11 Nov 2024 09:05:19 +0100 Subject: [PATCH 10/22] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 4 ++-- pkg/pdatatest/pprofiletest/profiles_test.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 pkg/pdatatest/pprofiletest/profiles_test.go diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 4321da86f3be..dce661e5e812 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -151,7 +151,7 @@ func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofi // IgnoreProfileContainerTimestampValues is a CompareProfilesOption that sets the value of start and end timestamp // to empty bytes for every profile -func IgnoreProfileContainerTimestampValues(attributeName string) CompareProfilesOption { +func IgnoreProfileContainerTimestampValues() CompareProfilesOption { return ignoreProfileContainerTimestampValues{} } @@ -180,7 +180,7 @@ func (opt ignoreProfileContainerTimestampValues) maskProfileContainerTimestampVa // IgnoreProfileTimestampValues is a CompareProfilesOption that sets the value of start timestamp // and duration to empty bytes for every profile -func IgnoreProfileTimestampValues(attributeName string) CompareProfilesOption { +func IgnoreProfileTimestampValues() CompareProfilesOption { return ignoreProfileTimestampValues{} } diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go new file mode 100644 index 000000000000..0b5329877c31 --- /dev/null +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -0,0 +1,4 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofiletest From a014edbe5e1c2e186eae74f8988f19bd5c8f1b19 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 12 Nov 2024 13:34:48 +0100 Subject: [PATCH 11/22] polish error messages + add test skeleton Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 70 ++--- pkg/pdatatest/pprofiletest/profiles_test.go | 313 ++++++++++++++++++++ 2 files changed, 349 insertions(+), 34 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index 2492f4e72b80..8f78e140c343 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -269,23 +269,23 @@ func CompareProfile(expected, actual pprofile.Profile) error { } if expected.KeepFrames() != actual.KeepFrames() { - errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.KeepFrames(), actual.KeepFrames())) + errs = multierr.Append(errs, fmt.Errorf("keepFrames does not match expected '%d', actual '%d'", expected.KeepFrames(), actual.KeepFrames())) } if expected.StartTime() != actual.StartTime() { - errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d", expected.StartTime(), actual.StartTime())) + errs = multierr.Append(errs, fmt.Errorf("startTime doesn't match expected: %d, actual: %d", expected.StartTime(), actual.StartTime())) } if expected.Duration() != actual.Duration() { - errs = multierr.Append(errs, fmt.Errorf("timestamp doesn't match expected: %d, actual: %d", expected.Duration(), actual.Duration())) + errs = multierr.Append(errs, fmt.Errorf("duration doesn't match expected: %d, actual: %d", expected.Duration(), actual.Duration())) } if expected.Period() != actual.Period() { - errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.Period(), actual.Period())) + errs = multierr.Append(errs, fmt.Errorf("period does not match expected '%d', actual '%d'", expected.Period(), actual.Period())) } if expected.DefaultSampleType() != actual.DefaultSampleType() { - errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.DefaultSampleType(), actual.DefaultSampleType())) + errs = multierr.Append(errs, fmt.Errorf("defaultSampleType does not match expected '%d', actual '%d'", expected.DefaultSampleType(), actual.DefaultSampleType())) } if expected.PeriodType().Type() != actual.PeriodType().Type() || @@ -296,19 +296,19 @@ func CompareProfile(expected, actual pprofile.Profile) error { actual.PeriodType().Unit(), actual.PeriodType().Type(), actual.PeriodType().AggregationTemporality())) } - errs = multierr.Append(errs, CompareProfileValueTypeSlice(expected.SampleType(), actual.SampleType())) + errs = multierr.Append(errs, internal.AddErrPrefix("sampleType", CompareProfileValueTypeSlice(expected.SampleType(), actual.SampleType()))) - errs = multierr.Append(errs, CompareProfileSampleSlice(expected.Sample(), actual.Sample())) + errs = multierr.Append(errs, internal.AddErrPrefix("sample", CompareProfileSampleSlice(expected.Sample(), actual.Sample()))) - errs = multierr.Append(errs, CompareProfileMappingSlice(expected.Mapping(), actual.Mapping())) + errs = multierr.Append(errs, internal.AddErrPrefix("mapping", CompareProfileMappingSlice(expected.Mapping(), actual.Mapping()))) - errs = multierr.Append(errs, CompareProfileLocationSlice(expected.Location(), actual.Location())) + errs = multierr.Append(errs, internal.AddErrPrefix("location", CompareProfileLocationSlice(expected.Location(), actual.Location()))) - errs = multierr.Append(errs, CompareProfileFunctionSlice(expected.Function(), actual.Function())) + errs = multierr.Append(errs, internal.AddErrPrefix("function", CompareProfileFunctionSlice(expected.Function(), actual.Function()))) - errs = multierr.Append(errs, CompareProfileAttributeUnitSlice(expected.AttributeUnits(), actual.AttributeUnits())) + errs = multierr.Append(errs, internal.AddErrPrefix("attributeUnits", CompareProfileAttributeUnitSlice(expected.AttributeUnits(), actual.AttributeUnits()))) - errs = multierr.Append(errs, CompareProfileLinkSlice(expected.LinkTable(), actual.LinkTable())) + errs = multierr.Append(errs, internal.AddErrPrefix("linkTable", CompareProfileLinkSlice(expected.LinkTable(), actual.LinkTable()))) return errs } @@ -316,7 +316,7 @@ func CompareProfile(expected, actual pprofile.Profile) error { func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of valueTypes doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -339,20 +339,20 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro matchingValueTypes[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`profile valueTypes are out of order: profile valueType "unit: %d, type: %d, aggregationTemporality: %d" expected at index %d, found at index %d`, + fmt.Errorf(`valueTypes are out of order: valueType "unit: %d, type: %d, aggregationTemporality: %d" expected at index %d, found at index %d`, elr.Unit(), elr.Type(), elr.AggregationTemporality(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected profile valueType "unit: %d, type: %d, aggregationTemporality: %d`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected valueType "unit: %d, type: %d, aggregationTemporality: %d`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) } } for i := 0; i < numValueTypes; i++ { if _, ok := matchingValueTypes[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected profile valueType "unit: %d, type: %d, aggregationTemporality: %d`, + errs = multierr.Append(errs, fmt.Errorf(`unexpected valueType "unit: %d, type: %d, aggregationTemporality: %d`, actual.At(i).Unit(), actual.At(i).Type(), actual.At(i).AggregationTemporality())) } } @@ -366,7 +366,7 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro for alr, elr := range matchingValueTypes { if !isValueTypeEqual(elr, alr) { - errs = multierr.Append(errs, fmt.Errorf(`expected profile valueType "unit: %d, type: %d, aggregationTemporality: %d",`+ + errs = multierr.Append(errs, fmt.Errorf(`expected valueType "unit: %d, type: %d, aggregationTemporality: %d",`+ `got "unit: %d, type: %d, aggregationTemporality: %d"`, elr.Unit(), elr.Type(), elr.AggregationTemporality(), alr.Unit(), alr.Type(), alr.AggregationTemporality())) } @@ -384,7 +384,7 @@ func isValueTypeEqual(expected, actual pprofile.ValueType) bool { func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile sample doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of samples doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -414,13 +414,13 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected profile sample: %v`, elr.Attributes().AsRaw())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected sample: %v`, elr.Attributes().AsRaw())) } } for i := 0; i < numSlice; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf("unexpected profile sample: %v", + errs = multierr.Append(errs, fmt.Errorf("unexpected sample: %v", actual.At(i).Attributes().AsRaw())) } } @@ -433,7 +433,7 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { } for alr, elr := range matchingItems { - errPrefix := fmt.Sprintf(`profile sample "%v"`, elr.Attributes().AsRaw()) + errPrefix := fmt.Sprintf(`sample "%v"`, elr.Attributes().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileSample(elr, alr))) } @@ -474,7 +474,8 @@ func CompareProfileSample(expected, actual pprofile.Sample) error { errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) } - errs = multierr.Append(errs, CompareProfileLabelSlice(expected.Label(), actual.Label())) + errPrefix := fmt.Sprintf(`labelSlice "%v"`, expected.Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLabelSlice(expected.Label(), actual.Label()))) return errs } @@ -482,7 +483,7 @@ func CompareProfileSample(expected, actual pprofile.Sample) error { func CompareProfileLabelSlice(expected, actual pprofile.LabelSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of labels doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -549,7 +550,7 @@ func isLabelEqual(expected, actual pprofile.Label) bool { func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of mappings doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -572,7 +573,7 @@ func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`mapping are out of order: mapping "attributes: %v, id: %d" expected at index %d, found at index %d`, + fmt.Errorf(`mappings are out of order: mapping "attributes: %v, id: %d" expected at index %d, found at index %d`, elr.Attributes().AsRaw(), elr.ID(), e, a)) } break @@ -625,7 +626,7 @@ func isMappingEqual(expected, actual pprofile.Mapping) bool { func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of functions doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -648,7 +649,7 @@ func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`function are out of order: function "name: %d" expected at index %d, found at index %d`, + fmt.Errorf(`functions are out of order: function "name: %d" expected at index %d, found at index %d`, elr.Name(), e, a)) } break @@ -693,7 +694,7 @@ func isFunctionEqual(expected, actual pprofile.Function) bool { func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of locations doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -742,7 +743,7 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error } for alr, elr := range matchingItems { - errPrefix := fmt.Sprintf(`profile location "id: %d"`, elr.ID()) + errPrefix := fmt.Sprintf(`location "id: %d"`, elr.ID()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLocation(elr, alr))) } @@ -775,7 +776,8 @@ func CompareProfileLocation(expected, actual pprofile.Location) error { errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) } - errs = multierr.Append(errs, CompareProfileLineSlice(expected.Line(), actual.Line())) + errPrefix := fmt.Sprintf(`line with location "id: %d"`, expected.ID()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLineSlice(expected.Line(), actual.Line()))) return errs } @@ -783,7 +785,7 @@ func CompareProfileLocation(expected, actual pprofile.Location) error { func CompareProfileLineSlice(expected, actual pprofile.LineSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of lines doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -849,7 +851,7 @@ func isLineEqual(expected, actual pprofile.Line) bool { func CompareProfileAttributeUnitSlice(expected, actual pprofile.AttributeUnitSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of attributeUnits doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } @@ -872,7 +874,7 @@ func CompareProfileAttributeUnitSlice(expected, actual pprofile.AttributeUnitSli matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`attributeUnit are out of order: attributeUnit "attributeKey: %d" expected at index %d, found at index %d`, + fmt.Errorf(`attributeUnits are out of order: attributeUnit "attributeKey: %d" expected at index %d, found at index %d`, elr.AttributeKey(), e, a)) } break @@ -903,7 +905,7 @@ func CompareProfileAttributeUnitSlice(expected, actual pprofile.AttributeUnitSli func CompareProfileLinkSlice(expected, actual pprofile.LinkSlice) error { var errs error if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile value types doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of links doesn't match expected: %d, actual: %d", expected.Len(), actual.Len())) return errs } diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 0b5329877c31..d1373a751461 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -2,3 +2,316 @@ // SPDX-License-Identifier: Apache-2.0 package pprofiletest + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pprofile" +) + +func TestCompareProfiles(t *testing.T) { + tcs := []struct { + name string + expected pprofile.Profiles + actual pprofile.Profiles + compareOptions []CompareProfilesOption + withoutOptions error + withOptions error + }{} + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + err := CompareProfiles(tc.expected, tc.actual) + if tc.withoutOptions == nil { + assert.NoError(t, err) + } else { + assert.EqualError(t, tc.withoutOptions, err.Error()) + } + + if tc.compareOptions == nil { + return + } + + err = CompareProfiles(tc.expected, tc.actual, tc.compareOptions...) + if tc.withOptions == nil { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.withOptions.Error()) + } + }) + } +} + +func TestCompareResourceProfiles(t *testing.T) { + tests := []struct { + name string + expected pprofile.ResourceProfiles + actual pprofile.ResourceProfiles + err error + }{ + { + name: "equal", + expected: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + return rl + }(), + actual: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + return rl + }(), + }, + { + name: "resource-attributes-mismatch", + expected: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.Resource().Attributes().PutStr("key1", "value1") + rl.Resource().Attributes().PutStr("key2", "value2") + return rl + }(), + actual: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.Resource().Attributes().PutStr("key1", "value1") + return rl + }(), + err: errors.New("attributes don't match expected: map[key1:value1 key2:value2], actual: map[key1:value1]"), + }, + { + name: "resource-schema-url-mismatch", + expected: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.SetSchemaUrl("schema-url") + return rl + }(), + actual: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.SetSchemaUrl("schema-url-2") + return rl + }(), + err: errors.New("schema url doesn't match expected: schema-url, actual: schema-url-2"), + }, + { + name: "scope-profiles-number-mismatch", + expected: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.ScopeProfiles().AppendEmpty() + rl.ScopeProfiles().AppendEmpty() + return rl + }(), + actual: func() pprofile.ResourceProfiles { + rl := pprofile.NewResourceProfiles() + rl.ScopeProfiles().AppendEmpty() + return rl + }(), + err: errors.New("number of scopes doesn't match expected: 2, actual: 1"), + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareResourceProfiles(test.expected, test.actual)) + }) + } +} + +func TestCompareScopeProfiles(t *testing.T) { + tests := []struct { + name string + expected pprofile.ScopeProfiles + actual pprofile.ScopeProfiles + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareScopeProfiles(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileContainer(t *testing.T) { + tests := []struct { + name string + expected pprofile.ProfileContainer + actual pprofile.ProfileContainer + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileContainer(test.expected, test.actual)) + }) + } +} + +func TestCompareProfile(t *testing.T) { + tests := []struct { + name string + expected pprofile.Profile + actual pprofile.Profile + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfile(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileValueTypeSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.ValueTypeSlice + actual pprofile.ValueTypeSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileValueTypeSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileSampleSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.SampleSlice + actual pprofile.SampleSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileSampleSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileSample(t *testing.T) { + tests := []struct { + name string + expected pprofile.Sample + actual pprofile.Sample + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileSample(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileLabelSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.LabelSlice + actual pprofile.LabelSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileLabelSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileMappingSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.MappingSlice + actual pprofile.MappingSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileMappingSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileFunctionSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.FunctionSlice + actual pprofile.FunctionSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileFunctionSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileLocationSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.LocationSlice + actual pprofile.LocationSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileLocationSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileLocation(t *testing.T) { + tests := []struct { + name string + expected pprofile.Location + actual pprofile.Location + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileLocation(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileLineSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.LineSlice + actual pprofile.LineSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileLineSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileAttributeUnitSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.AttributeUnitSlice + actual pprofile.AttributeUnitSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileAttributeUnitSlice(test.expected, test.actual)) + }) + } +} + +func TestCompareProfileLinkSlice(t *testing.T) { + tests := []struct { + name string + expected pprofile.LinkSlice + actual pprofile.LinkSlice + err error + }{} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.err, CompareProfileLinkSlice(test.expected, test.actual)) + }) + } +} From 4aed58052982ef81391401cf10822eafaa343294 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 12 Nov 2024 15:26:50 +0100 Subject: [PATCH 12/22] more polishing + unit tests Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 20 +- pkg/pdatatest/pprofiletest/profiles_test.go | 390 +++++++++++++++++++- 2 files changed, 397 insertions(+), 13 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index 8f78e140c343..b62ae42fb321 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -474,7 +474,7 @@ func CompareProfileSample(expected, actual pprofile.Sample) error { errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) } - errPrefix := fmt.Sprintf(`labelSlice "%v"`, expected.Attributes().AsRaw()) + errPrefix := fmt.Sprintf(`labelSlice of sample with attributes "%v"`, expected.Attributes().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLabelSlice(expected.Label(), actual.Label()))) return errs @@ -753,30 +753,30 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error func CompareProfileLocation(expected, actual pprofile.Location) error { var errs error if expected.ID() != actual.ID() { - return fmt.Errorf("expected id '%d', got '%d'", expected.ID(), actual.ID()) + errs = multierr.Append(errs, fmt.Errorf("expected id '%d', got '%d'", expected.ID(), actual.ID())) } if expected.MappingIndex() != actual.MappingIndex() { - return fmt.Errorf("expected mappingIndex '%d', got '%d'", expected.MappingIndex(), actual.MappingIndex()) + errs = multierr.Append(errs, fmt.Errorf("expected mappingIndex '%d', got '%d'", expected.MappingIndex(), actual.MappingIndex())) } if expected.Address() != actual.Address() { - return fmt.Errorf("expected address '%d', got '%d'", expected.Address(), actual.Address()) + errs = multierr.Append(errs, fmt.Errorf("expected address '%d', got '%d'", expected.Address(), actual.Address())) } if expected.IsFolded() != actual.IsFolded() { - return fmt.Errorf("expected isFolded '%v', got '%v'", expected.IsFolded(), actual.IsFolded()) + errs = multierr.Append(errs, fmt.Errorf("expected isFolded '%v', got '%v'", expected.IsFolded(), actual.IsFolded())) } if expected.TypeIndex() != actual.TypeIndex() { - return fmt.Errorf("expected typeIndex '%d', got '%d'", expected.TypeIndex(), actual.TypeIndex()) + errs = multierr.Append(errs, fmt.Errorf("expected typeIndex '%d', got '%d'", expected.TypeIndex(), actual.TypeIndex())) } if !reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) { errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) } - errPrefix := fmt.Sprintf(`line with location "id: %d"`, expected.ID()) + errPrefix := fmt.Sprintf(`line od location with "id: %d"`, expected.ID()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLineSlice(expected.Line(), actual.Line()))) return errs @@ -929,20 +929,20 @@ func CompareProfileLinkSlice(expected, actual pprofile.LinkSlice) error { if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`links are out of order: link "spanId: %s, traceId: %s" expected at index %d, found at index %d`, - elr.SpanID(), elr.TraceID(), e, a)) + elr.SpanID().String(), elr.TraceID().String(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected link "spanId: %s, traceId: %s"`, elr.SpanID(), elr.TraceID())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected link "spanId: %s, traceId: %s"`, elr.SpanID().String(), elr.TraceID().String())) } } for i := 0; i < numItems; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { errs = multierr.Append(errs, fmt.Errorf(`unexpected profile link "spanId: %s, traceId: %s"`, - actual.At(i).SpanID(), actual.At(i).TraceID())) + actual.At(i).SpanID().String(), actual.At(i).TraceID().String())) } } diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index d1373a751461..8b8bf2d034fa 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -8,7 +8,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pprofile" + "go.uber.org/multierr" ) func TestCompareProfiles(t *testing.T) { @@ -280,7 +282,165 @@ func TestCompareProfileLineSlice(t *testing.T) { expected pprofile.LineSlice actual pprofile.LineSlice err error - }{} + }{ + + { + name: "empty", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + return l + }(), + err: multierr.Combine( + errors.New(`lines are out of order: line "functionIndex: 1" expected at index 0, found at index 1`), + errors.New(`lines are out of order: line "functionIndex: 2" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + err: multierr.Combine( + errors.New(`number of lines doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(5) + i2.SetColumn(5) + return l + }(), + err: multierr.Combine( + errors.New(`line with "functionIndex: 2" does not match expected`), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(2) + i2.SetLine(4) + i2.SetColumn(4) + return l + }(), + actual: func() pprofile.LineSlice { + l := pprofile.NewLineSlice() + i1 := l.AppendEmpty() + i1.SetFunctionIndex(1) + i1.SetLine(3) + i1.SetColumn(3) + i2 := l.AppendEmpty() + i2.SetFunctionIndex(3) + i2.SetLine(5) + i2.SetColumn(5) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected line "functionIndex: 2"`), + errors.New(`unexpected profile line "functionIndex: 3"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { assert.Equal(t, test.err, CompareProfileLineSlice(test.expected, test.actual)) @@ -294,7 +454,119 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { expected pprofile.AttributeUnitSlice actual pprofile.AttributeUnitSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + return l + }(), + actual: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + return l + }(), + actual: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + return l + }(), + actual: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + return l + }(), + err: multierr.Combine( + errors.New(`attributeUnits are out of order: attributeUnit "attributeKey: 2" expected at index 0, found at index 1`), + errors.New(`attributeUnits are out of order: attributeUnit "attributeKey: 4" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + return l + }(), + actual: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + return l + }(), + err: multierr.Combine( + errors.New(`number of attributeUnits doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal", + expected: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(4) + i2.SetUnit(5) + return l + }(), + actual: func() pprofile.AttributeUnitSlice { + l := pprofile.NewAttributeUnitSlice() + i1 := l.AppendEmpty() + i1.SetAttributeKey(2) + i1.SetUnit(3) + i2 := l.AppendEmpty() + i2.SetAttributeKey(6) + i2.SetUnit(7) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected attributeUnit "attributeKey: 4"`), + errors.New(`unexpected profile attributeUnit "attributeKey: 6"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { assert.Equal(t, test.err, CompareProfileAttributeUnitSlice(test.expected, test.actual)) @@ -308,7 +580,119 @@ func TestCompareProfileLinkSlice(t *testing.T) { expected pprofile.LinkSlice actual pprofile.LinkSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + return l + }(), + actual: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + return l + }(), + actual: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + return l + }(), + actual: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + return l + }(), + err: multierr.Combine( + errors.New(`links are out of order: link "spanId: 7370616e69646e6e, traceId: 747261636569646e6e6e6e6e6e6e6e6e" expected at index 0, found at index 1`), + errors.New(`links are out of order: link "spanId: 7370616e69646e32, traceId: 74726163656964326e6e6e6e6e6e6e6e" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + return l + }(), + actual: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + return l + }(), + err: multierr.Combine( + errors.New(`number of links doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal", + expected: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn3"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid3nnnnnnnn"))) + return l + }(), + actual: func() pprofile.LinkSlice { + l := pprofile.NewLinkSlice() + i2 := l.AppendEmpty() + i2.SetSpanID(pcommon.SpanID([]byte("spanidn2"))) + i2.SetTraceID(pcommon.TraceID([]byte("traceid2nnnnnnnn"))) + i1 := l.AppendEmpty() + i1.SetSpanID(pcommon.SpanID([]byte("spanidnn"))) + i1.SetTraceID(pcommon.TraceID([]byte("traceidnnnnnnnnn"))) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected link "spanId: 7370616e69646e33, traceId: 74726163656964336e6e6e6e6e6e6e6e"`), + errors.New(`unexpected profile link "spanId: 7370616e69646e32, traceId: 74726163656964326e6e6e6e6e6e6e6e"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { assert.Equal(t, test.err, CompareProfileLinkSlice(test.expected, test.actual)) From 170eadbc8d13b1c3e6c9226ffbdbdffa57a3866b Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 13 Nov 2024 09:17:38 +0100 Subject: [PATCH 13/22] more unit tests Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 6 +- pkg/pdatatest/pprofiletest/profiles_test.go | 725 +++++++++++++++++++- 2 files changed, 706 insertions(+), 25 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index b62ae42fb321..263d70bc86aa 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -513,13 +513,13 @@ func CompareProfileLabelSlice(expected, actual pprofile.LabelSlice) error { } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected label "key: %d`, elr.Key())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected label "key: %d"`, elr.Key())) } } for i := 0; i < numLabels; i++ { if _, ok := matchingLabels[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected label "key: %d`, + errs = multierr.Append(errs, fmt.Errorf(`unexpected label "key: %d"`, actual.At(i).Key())) } } @@ -533,7 +533,7 @@ func CompareProfileLabelSlice(expected, actual pprofile.LabelSlice) error { for alr, elr := range matchingLabels { if !isLabelEqual(elr, alr) { - errs = multierr.Append(errs, fmt.Errorf(`label with "key: %d" does not match expected,`, alr.Key())) + errs = multierr.Append(errs, fmt.Errorf(`label with "key: %d" does not match expected`, alr.Key())) } } diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 8b8bf2d034fa..7fc8e51aa6d5 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -5,9 +5,11 @@ package pprofiletest import ( "errors" + "fmt" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pprofile" "go.uber.org/multierr" @@ -29,7 +31,7 @@ func TestCompareProfiles(t *testing.T) { if tc.withoutOptions == nil { assert.NoError(t, err) } else { - assert.EqualError(t, tc.withoutOptions, err.Error()) + require.EqualError(t, tc.withoutOptions, err.Error()) } if tc.compareOptions == nil { @@ -40,7 +42,7 @@ func TestCompareProfiles(t *testing.T) { if tc.withOptions == nil { assert.NoError(t, err) } else { - assert.EqualError(t, err, tc.withOptions.Error()) + require.EqualError(t, err, tc.withOptions.Error()) } }) } @@ -117,7 +119,7 @@ func TestCompareResourceProfiles(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareResourceProfiles(test.expected, test.actual)) + require.Equal(t, test.err, CompareResourceProfiles(test.expected, test.actual)) }) } } @@ -131,7 +133,7 @@ func TestCompareScopeProfiles(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareScopeProfiles(test.expected, test.actual)) + require.Equal(t, test.err, CompareScopeProfiles(test.expected, test.actual)) }) } } @@ -145,7 +147,7 @@ func TestCompareProfileContainer(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileContainer(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileContainer(test.expected, test.actual)) }) } } @@ -159,7 +161,7 @@ func TestCompareProfile(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfile(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfile(test.expected, test.actual)) }) } } @@ -173,7 +175,7 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileValueTypeSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileValueTypeSlice(test.expected, test.actual)) }) } } @@ -187,7 +189,7 @@ func TestCompareProfileSampleSlice(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileSampleSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileSampleSlice(test.expected, test.actual)) }) } } @@ -201,7 +203,7 @@ func TestCompareProfileSample(t *testing.T) { }{} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileSample(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileSample(test.expected, test.actual)) }) } } @@ -212,10 +214,148 @@ func TestCompareProfileLabelSlice(t *testing.T) { expected pprofile.LabelSlice actual pprofile.LabelSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + return l + }(), + err: multierr.Combine( + errors.New(`labels are out of order: label "key: 1" expected at index 0, found at index 1`), + errors.New(`labels are out of order: label "key: 2" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + err: multierr.Combine( + errors.New(`number of labels doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(5) + return l + }(), + err: multierr.Combine( + errors.New(`label with "key: 2" does not match expected`), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(2) + i2.SetNum(4) + return l + }(), + actual: func() pprofile.LabelSlice { + l := pprofile.NewLabelSlice() + i1 := l.AppendEmpty() + i1.SetKey(1) + i1.SetNum(3) + i2 := l.AppendEmpty() + i2.SetKey(3) + i2.SetNum(6) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected label "key: 2"`), + errors.New(`unexpected label "key: 3"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileLabelSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileLabelSlice(test.expected, test.actual)) }) } } @@ -226,10 +366,166 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected pprofile.MappingSlice actual pprofile.MappingSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + return l + }(), + err: multierr.Combine( + errors.New(`mappings are out of order: mapping "attributes: [1], id: 1" expected at index 0, found at index 1`), + errors.New(`mappings are out of order: mapping "attributes: [1], id: 2" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + err: multierr.Combine( + errors.New(`number of mappings doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(3) + return l + }(), + err: multierr.Combine( + errors.New(`mapping with "attributes: [1], id: 2", does not match expected`), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.MappingSlice { + l := pprofile.NewMappingSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(3) + i2.Attributes().Append(1, 2) + i2.SetFilename(2) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected mapping "attributes: [1], id: 2"`), + errors.New(`unexpected profile mapping "attributes: [1 2], id: 3"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileMappingSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileMappingSlice(test.expected, test.actual)) }) } } @@ -240,10 +536,167 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected pprofile.FunctionSlice actual pprofile.FunctionSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + return l + }(), + err: multierr.Combine( + errors.New(`functions are out of order: function "name: 1" expected at index 0, found at index 1`), + errors.New(`functions are out of order: function "name: 2" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + err: multierr.Combine( + errors.New(`number of functions doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(3) + return l + }(), + err: multierr.Combine( + errors.New(`function with "name: 2" does not match expected`), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.SetName(2) + i2.SetFilename(2) + return l + }(), + actual: func() pprofile.FunctionSlice { + l := pprofile.NewFunctionSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.SetName(1) + i1.SetFilename(1) + i2 := l.AppendEmpty() + i2.SetID(3) + i2.SetName(3) + i2.SetFilename(3) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected function "name: 2"`), + errors.New(`unexpected profile function "name: 3"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileFunctionSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileFunctionSlice(test.expected, test.actual)) }) } } @@ -254,10 +707,167 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected pprofile.LocationSlice actual pprofile.LocationSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + return l + }(), + err: multierr.Combine( + errors.New(`location is out of order: location "attributes: [1 2], id: 1" expected at index 0, found at index 1`), + errors.New(`location is out of order: location "attributes: [1 2 3], id: 2" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + return l + }(), + err: multierr.Combine( + errors.New(`number of locations doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(3) + return l + }(), + err: multierr.Combine( + fmt.Errorf(`location "id: 2": %w`, fmt.Errorf(`expected mappingIndex '2', got '3'`)), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(2) + i2.Attributes().Append(1, 2, 3) + i2.SetMappingIndex(2) + return l + }(), + actual: func() pprofile.LocationSlice { + l := pprofile.NewLocationSlice() + i1 := l.AppendEmpty() + i1.SetID(1) + i1.Attributes().Append(1, 2) + i1.SetMappingIndex(1) + i2 := l.AppendEmpty() + i2.SetID(3) + i2.Attributes().Append(1, 2, 3, 5) + i2.SetMappingIndex(2) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected location "attributes: [1 2 3], id: 2"`), + errors.New(`unexpected location "attributes: [1 2 3 5], id: 3"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileLocationSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileLocationSlice(test.expected, test.actual)) }) } } @@ -268,10 +878,81 @@ func TestCompareProfileLocation(t *testing.T) { expected pprofile.Location actual pprofile.Location err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.Location { + l := pprofile.NewLocation() + return l + }(), + actual: func() pprofile.Location { + l := pprofile.NewLocation() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.Location { + l := pprofile.NewLocation() + l.SetID(1) + l.SetAddress(2) + l.SetIsFolded(true) + l.SetMappingIndex(4) + l.SetTypeIndex(2) + l.Attributes().Append(1, 2, 3) + l.Line().AppendEmpty().Line() + return l + }(), + actual: func() pprofile.Location { + l := pprofile.NewLocation() + l.SetID(1) + l.SetAddress(2) + l.SetIsFolded(true) + l.SetMappingIndex(4) + l.SetTypeIndex(2) + l.Attributes().Append(1, 2, 3) + l.Line().AppendEmpty() + return l + }(), + }, + { + name: "not equal", + expected: func() pprofile.Location { + l := pprofile.NewLocation() + l.SetID(1) + l.SetAddress(3) + l.SetIsFolded(false) + l.SetMappingIndex(2) + l.SetTypeIndex(3) + l.Attributes().Append(1, 2, 3, 4) + l.Line().AppendEmpty().SetFunctionIndex(3) + return l + }(), + actual: func() pprofile.Location { + l := pprofile.NewLocation() + l.SetID(1) + l.SetAddress(2) + l.SetIsFolded(true) + l.SetMappingIndex(4) + l.SetTypeIndex(2) + l.Attributes().Append(1, 2, 3) + l.Line().AppendEmpty().Line() + return l + }(), + err: multierr.Combine( + errors.New(`expected mappingIndex '2', got '4'`), + errors.New(`expected address '3', got '2'`), + errors.New(`expected isFolded 'false', got 'true'`), + errors.New(`expected typeIndex '3', got '2'`), + errors.New(`expected attributes '[1 2 3 4]', got '[1 2 3]'`), + fmt.Errorf(`line od location with "id: 1": %w`, fmt.Errorf(`missing expected line "functionIndex: 3"`)), + fmt.Errorf(`line od location with "id: 1": %w`, fmt.Errorf(`unexpected profile line "functionIndex: 0"`)), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileLocation(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileLocation(test.expected, test.actual)) }) } } @@ -443,7 +1124,7 @@ func TestCompareProfileLineSlice(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileLineSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileLineSlice(test.expected, test.actual)) }) } } @@ -569,7 +1250,7 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileAttributeUnitSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileAttributeUnitSlice(test.expected, test.actual)) }) } } @@ -695,7 +1376,7 @@ func TestCompareProfileLinkSlice(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.err, CompareProfileLinkSlice(test.expected, test.actual)) + require.Equal(t, test.err, CompareProfileLinkSlice(test.expected, test.actual)) }) } } From 9534f93c05dd3e1ff97ea0f364ebdfd848ab41c8 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 13 Nov 2024 11:01:52 +0100 Subject: [PATCH 14/22] more unit tests Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 14 +- pkg/pdatatest/pprofiletest/profiles_test.go | 519 +++++++++++++++++++- 2 files changed, 520 insertions(+), 13 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index 263d70bc86aa..d758adae1eaa 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -346,13 +346,13 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected valueType "unit: %d, type: %d, aggregationTemporality: %d`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected valueType "unit: %d, type: %d, aggregationTemporality: %d"`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) } } for i := 0; i < numValueTypes; i++ { if _, ok := matchingValueTypes[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected valueType "unit: %d, type: %d, aggregationTemporality: %d`, + errs = multierr.Append(errs, fmt.Errorf(`unexpected valueType "unit: %d, type: %d, aggregationTemporality: %d"`, actual.At(i).Unit(), actual.At(i).Type(), actual.At(i).AggregationTemporality())) } } @@ -407,20 +407,20 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`samples are out of order: sample "%v" expected at index %d, found at index %d`, + fmt.Errorf(`samples are out of order: sample "attributes: %v" expected at index %d, found at index %d`, elr.Attributes().AsRaw(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected sample: %v`, elr.Attributes().AsRaw())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected sample "attributes: %v"`, elr.Attributes().AsRaw())) } } for i := 0; i < numSlice; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf("unexpected sample: %v", + errs = multierr.Append(errs, fmt.Errorf(`unexpected sample "attributes: %v"`, actual.At(i).Attributes().AsRaw())) } } @@ -433,7 +433,7 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { } for alr, elr := range matchingItems { - errPrefix := fmt.Sprintf(`sample "%v"`, elr.Attributes().AsRaw()) + errPrefix := fmt.Sprintf(`sample "attributes: %v"`, elr.Attributes().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileSample(elr, alr))) } @@ -717,7 +717,7 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`location is out of order: location "attributes: %v, id: %d" expected at index %d, found at index %d`, + fmt.Errorf(`locations are out of order: location "attributes: %v, id: %d" expected at index %d, found at index %d`, elr.Attributes().AsRaw(), elr.ID(), e, a)) } break diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 7fc8e51aa6d5..2f96cb23b5e8 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -62,6 +62,7 @@ func TestCompareResourceProfiles(t *testing.T) { rl.Resource().Attributes().PutStr("key1", "value1") l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) return rl }(), actual: func() pprofile.ResourceProfiles { @@ -69,6 +70,7 @@ func TestCompareResourceProfiles(t *testing.T) { rl.Resource().Attributes().PutStr("key1", "value1") l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) return rl }(), }, @@ -130,7 +132,131 @@ func TestCompareScopeProfiles(t *testing.T) { expected pprofile.ScopeProfiles actual pprofile.ScopeProfiles err error - }{} + }{ + { + name: "equal", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetName("scope-name") + l := sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetName("scope-name") + l := sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + return sl + }(), + }, + { + name: "scope-name-mismatch", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetName("scope-name") + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetName("scope-name-2") + return sl + }(), + err: errors.New("name doesn't match expected: scope-name, actual: scope-name-2"), + }, + { + name: "scope-version-mismatch", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetVersion("scope-version") + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().SetVersion("scope-version-2") + return sl + }(), + err: errors.New("version doesn't match expected: scope-version, actual: scope-version-2"), + }, + { + name: "scope-attributes-mismatch", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().Attributes().PutStr("scope-attr1", "value1") + sl.Scope().Attributes().PutStr("scope-attr2", "value2") + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Scope().Attributes().PutStr("scope-attr1", "value1") + sl.Scope().SetDroppedAttributesCount(1) + return sl + }(), + err: multierr.Combine( + errors.New("attributes don't match expected: map[scope-attr1:value1 scope-attr2:value2], "+ + "actual: map[scope-attr1:value1]"), + errors.New("dropped attributes count doesn't match expected: 0, actual: 1"), + ), + }, + { + name: "scope-schema-url-mismatch", + expected: func() pprofile.ScopeProfiles { + rl := pprofile.NewScopeProfiles() + rl.SetSchemaUrl("schema-url") + return rl + }(), + actual: func() pprofile.ScopeProfiles { + rl := pprofile.NewScopeProfiles() + rl.SetSchemaUrl("schema-url-2") + return rl + }(), + err: errors.New("schema url doesn't match expected: schema-url, actual: schema-url-2"), + }, + { + name: "profiles-number-mismatch", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Profiles().AppendEmpty() + sl.Profiles().AppendEmpty() + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + sl.Profiles().AppendEmpty() + return sl + }(), + err: errors.New("number of profile containers doesn't match expected: 2, actual: 1"), + }, + { + name: "profile-records-order-mismatch", + expected: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + l := sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l = sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr2", "value2") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + return sl + }(), + actual: func() pprofile.ScopeProfiles { + sl := pprofile.NewScopeProfiles() + l := sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr2", "value2") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l = sl.Profiles().AppendEmpty() + l.Attributes().PutStr("profile-attr1", "value1") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111113"))) + return sl + }(), + err: multierr.Combine( + errors.New(`profile containers are out of order: profile container "map[profile-attr1:value1]" expected at index 0, found at index 1`), + errors.New(`profile containers are out of order: profile container "map[profile-attr2:value2]" expected at index 1, found at index 0`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareScopeProfiles(test.expected, test.actual)) @@ -172,7 +298,164 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected pprofile.ValueTypeSlice actual pprofile.ValueTypeSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + return l + }(), + err: multierr.Combine( + errors.New(`valueTypes are out of order: valueType "unit: 1, type: 1, aggregationTemporality: 1" expected at index 0, found at index 1`), + errors.New(`valueTypes are out of order: valueType "unit: 2, type: 2, aggregationTemporality: 1" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + err: multierr.Combine( + errors.New(`number of valueTypes doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(2) + return l + }(), + err: multierr.Combine( + errors.New(`expected valueType "unit: 2, type: 2, aggregationTemporality: 1",got "unit: 2, type: 2, aggregationTemporality: 2"`), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(2) + i2.SetUnit(2) + i2.SetAggregationTemporality(1) + return l + }(), + actual: func() pprofile.ValueTypeSlice { + l := pprofile.NewValueTypeSlice() + i1 := l.AppendEmpty() + i1.SetType(1) + i1.SetUnit(1) + i1.SetAggregationTemporality(1) + i2 := l.AppendEmpty() + i2.SetType(3) + i2.SetUnit(3) + i2.SetAggregationTemporality(1) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected valueType "unit: 2, type: 2, aggregationTemporality: 1"`), + errors.New(`unexpected valueType "unit: 3, type: 3, aggregationTemporality: 1"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareProfileValueTypeSlice(test.expected, test.actual)) @@ -186,7 +469,165 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected pprofile.SampleSlice actual pprofile.SampleSlice err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + }, + { + name: "equal wrong order", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + return l + }(), + err: multierr.Combine( + errors.New(`samples are out of order: sample "attributes: [1 2]" expected at index 0, found at index 1`), + errors.New(`samples are out of order: sample "attributes: [1 2 3]" expected at index 1, found at index 0`), + ), + }, + { + name: "wrong length", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + err: multierr.Combine( + errors.New(`number of samples doesn't match expected: 1, actual: 2`), + ), + }, + { + name: "not equal - does not match expected", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(3) + i2.SetLocationsLength(3) + return l + }(), + err: multierr.Combine( + fmt.Errorf(`sample "attributes: [1 2 3]": %w`, fmt.Errorf(`expected locationLenght '2', got '3'`)), + fmt.Errorf(`sample "attributes: [1 2 3]": %w`, fmt.Errorf(`expected link '2', got '3'`)), + ), + }, + { + name: "not equal - missing", + expected: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3) + i2.SetLink(2) + i2.SetLocationsLength(2) + return l + }(), + actual: func() pprofile.SampleSlice { + l := pprofile.NewSampleSlice() + i1 := l.AppendEmpty() + i1.SetLink(1) + i1.SetLocationsLength(1) + i1.Attributes().Append(1, 2) + i2 := l.AppendEmpty() + i2.Attributes().Append(1, 2, 3, 5) + i2.SetLink(3) + i2.SetLocationsLength(3) + return l + }(), + err: multierr.Combine( + errors.New(`missing expected sample "attributes: [1 2 3]"`), + errors.New(`unexpected sample "attributes: [1 2 3 5]"`), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareProfileSampleSlice(test.expected, test.actual)) @@ -200,7 +641,73 @@ func TestCompareProfileSample(t *testing.T) { expected pprofile.Sample actual pprofile.Sample err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.Sample { + l := pprofile.NewSample() + return l + }(), + actual: func() pprofile.Sample { + l := pprofile.NewSample() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.Sample { + l := pprofile.NewSample() + l.SetLocationsStartIndex(1) + l.SetLocationsLength(1) + l.SetStacktraceIdIndex(1) + l.LocationIndex().Append(1, 2) + l.Attributes().Append(1, 2) + l.Label().AppendEmpty().SetKey(1) + return l + }(), + actual: func() pprofile.Sample { + l := pprofile.NewSample() + l.SetLocationsStartIndex(1) + l.SetLocationsLength(1) + l.SetStacktraceIdIndex(1) + l.LocationIndex().Append(1, 2) + l.Attributes().Append(1, 2) + l.Label().AppendEmpty().SetKey(1) + return l + }(), + }, + { + name: "not equal", + expected: func() pprofile.Sample { + l := pprofile.NewSample() + l.SetLocationsStartIndex(1) + l.SetLocationsLength(1) + l.SetStacktraceIdIndex(1) + l.LocationIndex().Append(1, 2) + l.Attributes().Append(1, 2) + l.Label().AppendEmpty().SetKey(1) + return l + }(), + actual: func() pprofile.Sample { + l := pprofile.NewSample() + l.SetLocationsStartIndex(2) + l.SetLocationsLength(3) + l.SetStacktraceIdIndex(3) + l.LocationIndex().Append(1, 2) + l.Attributes().Append(1, 2, 3) + l.Label().AppendEmpty().SetKey(2) + return l + }(), + err: multierr.Combine( + errors.New(`expected locationStartIndex '1', got '2'`), + errors.New(`expected locationLenght '1', got '3'`), + errors.New(`expected stacktraceIdIndex '1', got '3'`), + errors.New(`expected attributes '[1 2]', got '[1 2 3]'`), + fmt.Errorf(`labelSlice of sample with attributes "[1 2]": %w`, fmt.Errorf(`missing expected label "key: 1"`)), + fmt.Errorf(`labelSlice of sample with attributes "[1 2]": %w`, fmt.Errorf(`unexpected label "key: 2"`)), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareProfileSample(test.expected, test.actual)) @@ -773,8 +1280,8 @@ func TestCompareProfileLocationSlice(t *testing.T) { return l }(), err: multierr.Combine( - errors.New(`location is out of order: location "attributes: [1 2], id: 1" expected at index 0, found at index 1`), - errors.New(`location is out of order: location "attributes: [1 2 3], id: 2" expected at index 1, found at index 0`), + errors.New(`locations are out of order: location "attributes: [1 2], id: 1" expected at index 0, found at index 1`), + errors.New(`locations are out of order: location "attributes: [1 2 3], id: 2" expected at index 1, found at index 0`), ), }, { From a6e5936f2dbbce559dc1f75a31f980fc53ea1d26 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 13 Nov 2024 11:26:56 +0100 Subject: [PATCH 15/22] more unit tests Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles_test.go | 138 +++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 2f96cb23b5e8..8b4aac4f25c8 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -270,7 +270,62 @@ func TestCompareProfileContainer(t *testing.T) { expected pprofile.ProfileContainer actual pprofile.ProfileContainer err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + return l + }(), + actual: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + p := l.Profile() + p.SetKeepFrames(1) + return l + }(), + actual: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + p := l.Profile() + p.SetKeepFrames(1) + return l + }(), + }, + { + name: "not equal", + expected: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + p := l.Profile() + p.SetKeepFrames(1) + return l + }(), + actual: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + l.SetDroppedAttributesCount(3) + p := l.Profile() + p.SetKeepFrames(3) + return l + }(), + err: multierr.Combine( + errors.New(`dropped attributes count doesn't match expected: 2, actual: 3`), + errors.New(`profileID does not match expected '70726f66696c65696431313131313131', actual '70726f66696c65696431313131313132'`), + fmt.Errorf(`profile "map[]": %w`, fmt.Errorf(`keepFrames does not match expected '1', actual '3'`)), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareProfileContainer(test.expected, test.actual)) @@ -284,7 +339,86 @@ func TestCompareProfile(t *testing.T) { expected pprofile.Profile actual pprofile.Profile err error - }{} + }{ + { + name: "empty", + expected: func() pprofile.Profile { + l := pprofile.NewProfile() + return l + }(), + actual: func() pprofile.Profile { + l := pprofile.NewProfile() + return l + }(), + }, + { + name: "equal", + expected: func() pprofile.Profile { + l := pprofile.NewProfile() + l.SetKeepFrames(1) + l.AttributeTable().PutStr("key", "val") + l.SetPeriod(1) + s := l.SampleType().AppendEmpty() + s.SetType(1) + s.SetUnit(1) + a := l.AttributeUnits().AppendEmpty() + a.SetAttributeKey(1) + a.SetUnit(1) + return l + }(), + actual: func() pprofile.Profile { + l := pprofile.NewProfile() + l.SetKeepFrames(1) + l.AttributeTable().PutStr("key", "val") + l.SetPeriod(1) + s := l.SampleType().AppendEmpty() + s.SetType(1) + s.SetUnit(1) + a := l.AttributeUnits().AppendEmpty() + a.SetAttributeKey(1) + a.SetUnit(1) + return l + }(), + }, + { + name: "not equal", + expected: func() pprofile.Profile { + l := pprofile.NewProfile() + l.SetKeepFrames(1) + l.AttributeTable().PutStr("key", "val") + l.SetPeriod(1) + s := l.SampleType().AppendEmpty() + s.SetType(1) + s.SetUnit(1) + a := l.AttributeUnits().AppendEmpty() + a.SetAttributeKey(1) + a.SetUnit(1) + return l + }(), + actual: func() pprofile.Profile { + l := pprofile.NewProfile() + l.SetKeepFrames(2) + l.AttributeTable().PutStr("key1", "val1") + l.SetPeriod(2) + s := l.SampleType().AppendEmpty() + s.SetType(2) + s.SetUnit(2) + a := l.AttributeUnits().AppendEmpty() + a.SetAttributeKey(2) + a.SetUnit(2) + return l + }(), + err: multierr.Combine( + errors.New(`attributes don't match expected: map[key:val], actual: map[key1:val1]`), + errors.New(`keepFrames does not match expected '1', actual '2'`), + errors.New(`period does not match expected '1', actual '2'`), + fmt.Errorf(`sampleType: %w`, fmt.Errorf(`missing expected valueType "unit: 1, type: 1, aggregationTemporality: 0"`)), + fmt.Errorf(`sampleType: %w`, fmt.Errorf(`unexpected valueType "unit: 2, type: 2, aggregationTemporality: 0"`)), + fmt.Errorf(`attributeUnits: %w`, fmt.Errorf(`missing expected attributeUnit "attributeKey: 1"`)), + fmt.Errorf(`attributeUnits: %w`, fmt.Errorf(`unexpected profile attributeUnit "attributeKey: 2"`)), + ), + }, + } for _, test := range tests { t.Run(test.name, func(t *testing.T) { require.Equal(t, test.err, CompareProfile(test.expected, test.actual)) From 8e11334c1de02dd3bee8bfeaab2d28be68ca1bed Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 13 Nov 2024 16:31:20 +0100 Subject: [PATCH 16/22] more unit tests with options Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles_test.go | 190 ++++++++++++++++++-- 1 file changed, 178 insertions(+), 12 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 8b4aac4f25c8..d4487dbb2552 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -23,7 +23,173 @@ func TestCompareProfiles(t *testing.T) { compareOptions []CompareProfilesOption withoutOptions error withOptions error - }{} + }{ + { + name: "empty", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + return p + }(), + }, + { + name: "equal", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("scope-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("scope-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + return p + }(), + }, + { + name: "resource order", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("scope-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + rl2 := p.ResourceProfiles().AppendEmpty() + rl2.Resource().Attributes().PutStr("key2", "value2") + l2 := rl2.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l2.Attributes().PutStr("scope-attr2", "value2") + l2.SetProfileID(pprofile.NewProfileIDEmpty()) + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl2 := p.ResourceProfiles().AppendEmpty() + rl2.Resource().Attributes().PutStr("key2", "value2") + l2 := rl2.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l2.Attributes().PutStr("scope-attr2", "value2") + l2.SetProfileID(pprofile.NewProfileIDEmpty()) + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + l.Attributes().PutStr("scope-attr1", "value1") + l.SetProfileID(pprofile.NewProfileIDEmpty()) + return p + }(), + withoutOptions: multierr.Combine( + errors.New(`resources are out of order: resource "map[key1:value1]" expected at index 0, found at index 1`), + errors.New(`resources are out of order: resource "map[key2:value2]" expected at index 1, found at index 0`), + ), + compareOptions: []CompareProfilesOption{ + IgnoreResourceProfilesOrder()}, + }, + { + name: "resource masked attribute", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value2") + return p + }(), + withoutOptions: multierr.Combine( + errors.New(`missing expected resource: map[key1:value1]`), + errors.New(`unexpected resource: map[key1:value2]`), + ), + compareOptions: []CompareProfilesOption{ + IgnoreResourceAttributeValue("key1")}, + }, + { + name: "resource scope order", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Scope() + l.SetName("scope1") + l.Attributes().PutStr("scope-attr1", "value1") + l2 := rl.ScopeProfiles().AppendEmpty().Scope() + l2.Attributes().PutStr("scope-attr2", "value2") + l2.SetName("scope2") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l2 := rl.ScopeProfiles().AppendEmpty().Scope() + l2.Attributes().PutStr("scope-attr2", "value2") + l2.SetName("scope2") + l := rl.ScopeProfiles().AppendEmpty().Scope() + l.Attributes().PutStr("scope-attr1", "value1") + l.SetName("scope1") + return p + }(), + withoutOptions: errors.New(`resource "map[key1:value1]": scopes are out of order: scope scope1 expected at index 0, found at index 1; resource "map[key1:value1]": scopes are out of order: scope scope2 expected at index 1, found at index 0`), + compareOptions: []CompareProfilesOption{ + IgnoreScopeProfilesOrder()}, + }, + { + name: "mask scope attribute", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Scope() + l.SetName("scope1") + l.Attributes().PutStr("scope-attr1", "value1") + l2 := rl.ScopeProfiles().AppendEmpty().Scope() + l2.Attributes().PutStr("scope-attr2", "value2") + l2.SetName("scope2") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty().Scope() + l.Attributes().PutStr("scope-attr1", "value12") + l.SetName("scope1") + l2 := rl.ScopeProfiles().AppendEmpty().Scope() + l2.Attributes().PutStr("scope-attr2", "value22") + l2.SetName("scope2") + return p + }(), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": attributes don't match expected: map[scope-attr1:value1], actual: map[scope-attr1:value12]; resource "map[key1:value1]": scope "scope2": attributes don't match expected: map[scope-attr2:value2], actual: map[scope-attr2:value22]`), + compareOptions: []CompareProfilesOption{ + IgnoreScopeAttributeValue("scope-attr2"), + IgnoreScopeAttributeValue("scope-attr1"), + }, + }, + + // TODO + // ignore scope attribute value + // ignore container order + + // ignore profilecontainer attribute value + // ignore profilecontainer attribute order + // ignore profilecontainer timestamp value + + // ignore profile attribute value + // ignore profile attribute order + // ignore profile timestamp value + + } for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { @@ -31,7 +197,7 @@ func TestCompareProfiles(t *testing.T) { if tc.withoutOptions == nil { assert.NoError(t, err) } else { - require.EqualError(t, tc.withoutOptions, err.Error()) + require.EqualError(t, err, tc.withoutOptions.Error()) } if tc.compareOptions == nil { @@ -61,7 +227,7 @@ func TestCompareResourceProfiles(t *testing.T) { rl := pprofile.NewResourceProfiles() rl.Resource().Attributes().PutStr("key1", "value1") l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.NewProfileIDEmpty()) return rl }(), @@ -69,7 +235,7 @@ func TestCompareResourceProfiles(t *testing.T) { rl := pprofile.NewResourceProfiles() rl.Resource().Attributes().PutStr("key1", "value1") l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.NewProfileIDEmpty()) return rl }(), @@ -139,7 +305,7 @@ func TestCompareScopeProfiles(t *testing.T) { sl := pprofile.NewScopeProfiles() sl.Scope().SetName("scope-name") l := sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.NewProfileIDEmpty()) return sl }(), @@ -147,7 +313,7 @@ func TestCompareScopeProfiles(t *testing.T) { sl := pprofile.NewScopeProfiles() sl.Scope().SetName("scope-name") l := sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.NewProfileIDEmpty()) return sl }(), @@ -234,26 +400,26 @@ func TestCompareScopeProfiles(t *testing.T) { expected: func() pprofile.ScopeProfiles { sl := pprofile.NewScopeProfiles() l := sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) l = sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr2", "value2") + l.Attributes().PutStr("scope-attr2", "value2") l.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) return sl }(), actual: func() pprofile.ScopeProfiles { sl := pprofile.NewScopeProfiles() l := sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr2", "value2") + l.Attributes().PutStr("scope-attr2", "value2") l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) l = sl.Profiles().AppendEmpty() - l.Attributes().PutStr("profile-attr1", "value1") + l.Attributes().PutStr("scope-attr1", "value1") l.SetProfileID(pprofile.ProfileID([]byte("profileid1111113"))) return sl }(), err: multierr.Combine( - errors.New(`profile containers are out of order: profile container "map[profile-attr1:value1]" expected at index 0, found at index 1`), - errors.New(`profile containers are out of order: profile container "map[profile-attr2:value2]" expected at index 1, found at index 0`), + errors.New(`profile containers are out of order: profile container "map[scope-attr1:value1]" expected at index 0, found at index 1`), + errors.New(`profile containers are out of order: profile container "map[scope-attr2:value2]" expected at index 1, found at index 0`), ), }, } From a9b94d436919eded04d27605fc7dc4985361b497 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 14 Nov 2024 08:51:17 +0100 Subject: [PATCH 17/22] unit tests with options Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 48 ---- pkg/pdatatest/pprofiletest/profiles_test.go | 254 +++++++++++++++++++- 2 files changed, 241 insertions(+), 61 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index dce661e5e812..7faadb56a38e 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -207,54 +207,6 @@ func (opt ignoreProfileTimestampValues) maskProfileTimestampValues(profiles ppro } } -// IgnoreProfileContainerAttributesOrder is a CompareprofilesOption that ignores the order of profile container attributes. -func IgnoreProfileContainerAttributesOrder() CompareProfilesOption { - return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - orderProfileContainerAttributes(expected) - orderProfileContainerAttributes(actual) - }) -} - -func orderProfileContainerAttributes(metrics pprofile.Profiles) { - rms := metrics.ResourceProfiles() - for i := 0; i < rms.Len(); i++ { - ilms := rms.At(i).ScopeProfiles() - for j := 0; j < ilms.Len(); j++ { - msl := ilms.At(j).Profiles() - for g := 0; g < msl.Len(); g++ { - for k := 0; k < msl.At(g).Attributes().Len(); k++ { - rawOrdered := internal.OrderMapByKey(msl.At(g).Attributes().AsRaw()) - _ = msl.At(g).Attributes().FromRaw(rawOrdered) - } - } - } - } -} - -// IgnoreProfileAttributesOrder is a CompareprofilesOption that ignores the order of profile attributes. -func IgnoreProfileAttributesOrder() CompareProfilesOption { - return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - orderProfileAttributes(expected) - orderProfileAttributes(actual) - }) -} - -func orderProfileAttributes(metrics pprofile.Profiles) { - rms := metrics.ResourceProfiles() - for i := 0; i < rms.Len(); i++ { - ilms := rms.At(i).ScopeProfiles() - for j := 0; j < ilms.Len(); j++ { - msl := ilms.At(j).Profiles() - for g := 0; g < msl.Len(); g++ { - for k := 0; k < msl.At(g).Profile().AttributeTable().Len(); k++ { - rawOrdered := internal.OrderMapByKey(msl.At(g).Profile().AttributeTable().AsRaw()) - _ = msl.At(g).Profile().AttributeTable().FromRaw(rawOrdered) - } - } - } - } -} - // IgnoreResourceProfilesOrder is a CompareProfilesOption that ignores the order of resource traces/metrics/profiles. func IgnoreResourceProfilesOrder() CompareProfilesOption { return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index d4487dbb2552..84c8f5342239 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -16,6 +17,8 @@ import ( ) func TestCompareProfiles(t *testing.T) { + timestamp1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) + timestamp2 := timestamp1.Add(5 * time.Second) tcs := []struct { name string expected pprofile.Profiles @@ -176,19 +179,220 @@ func TestCompareProfiles(t *testing.T) { IgnoreScopeAttributeValue("scope-attr1"), }, }, - - // TODO - // ignore scope attribute value - // ignore container order - - // ignore profilecontainer attribute value - // ignore profilecontainer attribute order - // ignore profilecontainer timestamp value - - // ignore profile attribute value - // ignore profile attribute order - // ignore profile timestamp value - + { + name: "ignore profile container order", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("continer-attr1", "value1") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pc2.Attributes().PutStr("continer-attr2", "value2") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pc2.Attributes().PutStr("continer-attr2", "value2") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("continer-attr1", "value1") + return p + }(), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile containers are out of order: profile container "map[continer-attr1:value1]" expected at index 0, found at index 1; resource "map[key1:value1]": scope "scope1": profile containers are out of order: profile container "map[continer-attr2:value2]" expected at index 1, found at index 0`), + compareOptions: []CompareProfilesOption{ + IgnoreProfileContainersOrder(), + }, + }, + { + name: "ignore profile container attribute value", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pc2.Attributes().PutStr("container-attr2", "value2") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value3") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pc2.Attributes().PutStr("container-attr2", "value4") + return p + }(), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": missing expected profile container: map[container-attr1:value1]; resource "map[key1:value1]": scope "scope1": missing expected profile container: map[container-attr2:value2]; resource "map[key1:value1]": scope "scope1": unexpected profile container: map[container-attr1:value3]; resource "map[key1:value1]": scope "scope1": unexpected profile container: map[container-attr2:value4]`), + compareOptions: []CompareProfilesOption{ + IgnoreProfileContainerAttributeValue("container-attr2"), + IgnoreProfileContainerAttributeValue("container-attr1"), + }, + }, + { + name: "ignore profile container timestamp values", + expected: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pc.SetEndTime(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp1), + actual: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pc.SetEndTime(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp2), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": start timestamp doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": end timestamp doesn't match expected: 1577836805000000000, actual: 1577836810000000000`), + compareOptions: []CompareProfilesOption{ + IgnoreProfileContainerTimestampValues(), + }, + }, + { + name: "ignore profile attribute value", + expected: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value1") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pp2 := pc.Profile() + pp2.AttributeTable().PutStr("attr2", "value2") + return p + }(), + actual: func() pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value1") + pc2 := l.Profiles().AppendEmpty() + pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) + pp2 := pc.Profile() + pp2.AttributeTable().PutStr("attr2", "value3") + return p + }(), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[]": profile "map[]": attributes don't match expected: map[attr1:value1 attr2:value2], actual: map[attr1:value1 attr2:value3]`), + compareOptions: []CompareProfilesOption{ + IgnoreProfileAttributeValue("attr2"), + }, + }, + { + name: "ignore profile timestamp values", + expected: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value1") + pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp1), + actual: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value1") + pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp2), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": profile "map[container-attr1:value1]": startTime doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": profile "map[container-attr1:value1]": duration doesn't match expected: 1577836805000000000, actual: 1577836810000000000`), + compareOptions: []CompareProfilesOption{ + IgnoreProfileTimestampValues(), + }, + }, + { + name: "not equal without options", + expected: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value1") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value1") + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value1") + pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp1), + actual: func(timestamp time.Time) pprofile.Profiles { + p := pprofile.NewProfiles() + rl := p.ResourceProfiles().AppendEmpty() + rl.Resource().Attributes().PutStr("key1", "value2") + l := rl.ScopeProfiles().AppendEmpty() + l.Scope().SetName("scope1") + pc := l.Profiles().AppendEmpty() + pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + pc.Attributes().PutStr("container-attr1", "value2") + pp := pc.Profile() + pp.AttributeTable().PutStr("attr1", "value2") + pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + return p + }(timestamp2), + withoutOptions: errors.New(`missing expected resource: map[key1:value1]; unexpected resource: map[key1:value2]`), + }, } for _, tc := range tcs { @@ -491,6 +695,30 @@ func TestCompareProfileContainer(t *testing.T) { fmt.Errorf(`profile "map[]": %w`, fmt.Errorf(`keepFrames does not match expected '1', actual '3'`)), ), }, + { + name: "not equal attributes", + expected: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.Attributes().PutStr("attr1", "va2") + p := l.Profile() + p.SetKeepFrames(1) + return l + }(), + actual: func() pprofile.ProfileContainer { + l := pprofile.NewProfileContainer() + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.Attributes().PutStr("attr1", "va1") + p := l.Profile() + p.SetKeepFrames(1) + return l + }(), + err: multierr.Combine( + errors.New(`attributes don't match expected: map[attr1:va2], actual: map[attr1:va1]`), + ), + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { From f72f9525d7f175b127527a8f4f0ae5a3d2be32fd Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 14 Nov 2024 11:16:51 +0100 Subject: [PATCH 18/22] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 7faadb56a38e..097ee8ddc92b 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -76,7 +76,6 @@ func (opt ignoreScopeAttributeValue) maskProfilesScopeAttributeValue(profiles pp if exists { val.SetEmptyBytes() } - } } } From 3bdafb65b4c73843896915ea9980e816b6de2600 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 26 Nov 2024 13:40:01 +0100 Subject: [PATCH 19/22] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 6 ++---- pkg/pdatatest/pprofiletest/profiles_test.go | 10 ++++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 097ee8ddc92b..98230ccd6dcc 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -154,8 +154,7 @@ func IgnoreProfileContainerTimestampValues() CompareProfilesOption { return ignoreProfileContainerTimestampValues{} } -type ignoreProfileContainerTimestampValues struct { -} +type ignoreProfileContainerTimestampValues struct{} func (opt ignoreProfileContainerTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { opt.maskProfileContainerTimestampValues(expected) @@ -183,8 +182,7 @@ func IgnoreProfileTimestampValues() CompareProfilesOption { return ignoreProfileTimestampValues{} } -type ignoreProfileTimestampValues struct { -} +type ignoreProfileTimestampValues struct{} func (opt ignoreProfileTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { opt.maskProfileTimestampValues(expected) diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 84c8f5342239..8c7b20130172 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -94,7 +94,8 @@ func TestCompareProfiles(t *testing.T) { errors.New(`resources are out of order: resource "map[key2:value2]" expected at index 1, found at index 0`), ), compareOptions: []CompareProfilesOption{ - IgnoreResourceProfilesOrder()}, + IgnoreResourceProfilesOrder(), + }, }, { name: "resource masked attribute", @@ -115,7 +116,8 @@ func TestCompareProfiles(t *testing.T) { errors.New(`unexpected resource: map[key1:value2]`), ), compareOptions: []CompareProfilesOption{ - IgnoreResourceAttributeValue("key1")}, + IgnoreResourceAttributeValue("key1"), + }, }, { name: "resource scope order", @@ -145,7 +147,8 @@ func TestCompareProfiles(t *testing.T) { }(), withoutOptions: errors.New(`resource "map[key1:value1]": scopes are out of order: scope scope1 expected at index 0, found at index 1; resource "map[key1:value1]": scopes are out of order: scope scope2 expected at index 1, found at index 0`), compareOptions: []CompareProfilesOption{ - IgnoreScopeProfilesOrder()}, + IgnoreScopeProfilesOrder(), + }, }, { name: "mask scope attribute", @@ -1999,7 +2002,6 @@ func TestCompareProfileLineSlice(t *testing.T) { actual pprofile.LineSlice err error }{ - { name: "empty", expected: func() pprofile.LineSlice { From 3aae6d5f5827ba6255181bae1a97e8b32152de07 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 4 Dec 2024 14:28:46 +0100 Subject: [PATCH 20/22] fix after rebase Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/options.go | 82 +- pkg/pdatatest/pprofiletest/profiles.go | 253 ++---- pkg/pdatatest/pprofiletest/profiles_test.go | 921 ++++++-------------- 3 files changed, 338 insertions(+), 918 deletions(-) diff --git a/pkg/pdatatest/pprofiletest/options.go b/pkg/pdatatest/pprofiletest/options.go index 98230ccd6dcc..17545953fa48 100644 --- a/pkg/pdatatest/pprofiletest/options.go +++ b/pkg/pdatatest/pprofiletest/options.go @@ -80,40 +80,6 @@ func (opt ignoreScopeAttributeValue) maskProfilesScopeAttributeValue(profiles pp } } -// IgnoreProfileContainerAttributeValue is a CompareProfilesOption that sets the value of an attribute -// to empty bytes for every profile -func IgnoreProfileContainerAttributeValue(attributeName string) CompareProfilesOption { - return ignoreProfileContainerAttributeValue{ - attributeName: attributeName, - } -} - -type ignoreProfileContainerAttributeValue struct { - attributeName string -} - -func (opt ignoreProfileContainerAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { - opt.maskProfileContainerAttributeValue(expected) - opt.maskProfileContainerAttributeValue(actual) -} - -func (opt ignoreProfileContainerAttributeValue) maskProfileContainerAttributeValue(profiles pprofile.Profiles) { - rls := profiles.ResourceProfiles() - for i := 0; i < profiles.ResourceProfiles().Len(); i++ { - sls := rls.At(i).ScopeProfiles() - for j := 0; j < sls.Len(); j++ { - lrs := sls.At(j).Profiles() - for k := 0; k < lrs.Len(); k++ { - lr := lrs.At(k) - val, exists := lr.Attributes().Get(opt.attributeName) - if exists { - val.SetEmptyBytes() - } - } - } - } -} - // IgnoreProfileAttributeValue is a CompareProfilesOption that sets the value of an attribute // to empty bytes for every profile func IgnoreProfileAttributeValue(attributeName string) CompareProfilesOption { @@ -138,8 +104,8 @@ func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofi for j := 0; j < sls.Len(); j++ { lrs := sls.At(j).Profiles() for k := 0; k < lrs.Len(); k++ { - lr := lrs.At(k).Profile() - val, exists := lr.AttributeTable().Get(opt.attributeName) + lr := lrs.At(k) + val, exists := lr.Attributes().Get(opt.attributeName) if exists { val.SetEmptyBytes() } @@ -148,34 +114,6 @@ func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofi } } -// IgnoreProfileContainerTimestampValues is a CompareProfilesOption that sets the value of start and end timestamp -// to empty bytes for every profile -func IgnoreProfileContainerTimestampValues() CompareProfilesOption { - return ignoreProfileContainerTimestampValues{} -} - -type ignoreProfileContainerTimestampValues struct{} - -func (opt ignoreProfileContainerTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { - opt.maskProfileContainerTimestampValues(expected) - opt.maskProfileContainerTimestampValues(actual) -} - -func (opt ignoreProfileContainerTimestampValues) maskProfileContainerTimestampValues(profiles pprofile.Profiles) { - rls := profiles.ResourceProfiles() - for i := 0; i < profiles.ResourceProfiles().Len(); i++ { - sls := rls.At(i).ScopeProfiles() - for j := 0; j < sls.Len(); j++ { - lrs := sls.At(j).Profiles() - for k := 0; k < lrs.Len(); k++ { - lr := lrs.At(k) - lr.SetStartTime(pcommon.NewTimestampFromTime(time.Time{})) - lr.SetEndTime(pcommon.NewTimestampFromTime(time.Time{})) - } - } - } -} - // IgnoreProfileTimestampValues is a CompareProfilesOption that sets the value of start timestamp // and duration to empty bytes for every profile func IgnoreProfileTimestampValues() CompareProfilesOption { @@ -196,7 +134,7 @@ func (opt ignoreProfileTimestampValues) maskProfileTimestampValues(profiles ppro for j := 0; j < sls.Len(); j++ { lrs := sls.At(j).Profiles() for k := 0; k < lrs.Len(); k++ { - lr := lrs.At(k).Profile() + lr := lrs.At(k) lr.SetStartTime(pcommon.NewTimestampFromTime(time.Time{})) lr.SetDuration(pcommon.NewTimestampFromTime(time.Time{})) } @@ -246,22 +184,22 @@ func sortScopeProfilesSlices(ls pprofile.Profiles) { } // IgnoreProfilesOrder is a CompareProfilesOption that ignores the order of profile records. -func IgnoreProfileContainersOrder() CompareProfilesOption { +func IgnoreProfilesOrder() CompareProfilesOption { return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { - sortProfileContainerSlices(expected) - sortProfileContainerSlices(actual) + sortProfileSlices(expected) + sortProfileSlices(actual) }) } -func sortProfileContainerSlices(ls pprofile.Profiles) { +func sortProfileSlices(ls pprofile.Profiles) { for i := 0; i < ls.ResourceProfiles().Len(); i++ { for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { - ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.ProfileContainer) bool { + ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.Profile) bool { if a.StartTime() != b.StartTime() { return a.StartTime() < b.StartTime() } - if a.EndTime() != b.EndTime() { - return a.EndTime() < b.EndTime() + if a.Duration() != b.Duration() { + return a.Duration() < b.Duration() } as := a.ProfileID() bs := b.ProfileID() diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index d758adae1eaa..ae082f29b544 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -4,6 +4,7 @@ package pprofiletest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pprofiletest" import ( + "bytes" "fmt" "reflect" @@ -167,7 +168,7 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error { ) if expected.Profiles().Len() != actual.Profiles().Len() { - errs = multierr.Append(errs, fmt.Errorf("number of profile containers doesn't match expected: %d, actual: %d", + errs = multierr.Append(errs, fmt.Errorf("number of profiles doesn't match expected: %d, actual: %d", expected.Profiles().Len(), actual.Profiles().Len())) return errs } @@ -175,7 +176,7 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error { numProfiles := expected.Profiles().Len() // Keep track of matching containers so that each container can only be matched once - matchingProfiles := make(map[pprofile.ProfileContainer]pprofile.ProfileContainer, numProfiles) + matchingProfiles := make(map[pprofile.Profile]pprofile.Profile, numProfiles) var outOfOrderErrs error for e := 0; e < numProfiles; e++ { @@ -191,20 +192,20 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error { matchingProfiles[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`profile containers are out of order: profile container "%v" expected at index %d, found at index %d`, + fmt.Errorf(`profiles are out of order: profile "%v" expected at index %d, found at index %d`, elr.Attributes().AsRaw(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf("missing expected profile container: %v", elr.Attributes().AsRaw())) + errs = multierr.Append(errs, fmt.Errorf("missing expected profile: %v", elr.Attributes().AsRaw())) } } for i := 0; i < numProfiles; i++ { if _, ok := matchingProfiles[actual.Profiles().At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf("unexpected profile container: %v", + errs = multierr.Append(errs, fmt.Errorf("unexpected profile: %v", actual.Profiles().At(i).Attributes().AsRaw())) } } @@ -217,13 +218,13 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error { } for alr, elr := range matchingProfiles { - errPrefix := fmt.Sprintf(`profile container "%v"`, elr.Attributes().AsRaw()) - errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileContainer(elr, alr))) + errPrefix := fmt.Sprintf(`profile "%v"`, elr.Attributes().AsRaw()) + errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfile(elr, alr))) } return errs } -func CompareProfileContainer(expected, actual pprofile.ProfileContainer) error { +func CompareProfile(expected, actual pprofile.Profile) error { errs := multierr.Combine( internal.CompareAttributes(expected.Attributes(), actual.Attributes()), internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()), @@ -237,39 +238,28 @@ func CompareProfileContainer(expected, actual pprofile.ProfileContainer) error { errs = multierr.Append(errs, fmt.Errorf("start timestamp doesn't match expected: %d, "+"actual: %d", expected.StartTime(), actual.StartTime())) } - if expected.EndTime() != actual.EndTime() { - errs = multierr.Append(errs, fmt.Errorf("end timestamp doesn't match expected: %d, "+"actual: %d", expected.EndTime(), actual.EndTime())) - } - - errPrefix := fmt.Sprintf(`profile "%v"`, expected.Attributes().AsRaw()) - errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfile(expected.Profile(), actual.Profile()))) - - return errs -} - -func CompareProfile(expected, actual pprofile.Profile) error { - errs := multierr.Combine( - internal.CompareAttributes(expected.AttributeTable(), actual.AttributeTable()), - ) - if !reflect.DeepEqual(expected.LocationIndices(), actual.LocationIndices()) { errs = multierr.Append(errs, fmt.Errorf("locationIndicies do not match expected")) } - if !reflect.DeepEqual(expected.Comment(), actual.Comment()) { + if !reflect.DeepEqual(expected.CommentStrindices(), actual.CommentStrindices()) { errs = multierr.Append(errs, fmt.Errorf("comment does not match expected")) } + if expected.Time() != actual.Time() { + errs = multierr.Append(errs, fmt.Errorf("time doesn't match expected: %d, actual: %d", expected.Time(), actual.Time())) + } + if !reflect.DeepEqual(expected.StringTable(), actual.StringTable()) { errs = multierr.Append(errs, fmt.Errorf("stringTable does not match expected")) } - if expected.DropFrames() != actual.DropFrames() { - errs = multierr.Append(errs, fmt.Errorf("profileID does not match expected '%d', actual '%d'", expected.DropFrames(), actual.DropFrames())) + if expected.OriginalPayloadFormat() != actual.OriginalPayloadFormat() { + errs = multierr.Append(errs, fmt.Errorf("originalPayloadFormat does not match expected '%s', actual '%s'", expected.OriginalPayloadFormat(), actual.OriginalPayloadFormat())) } - if expected.KeepFrames() != actual.KeepFrames() { - errs = multierr.Append(errs, fmt.Errorf("keepFrames does not match expected '%d', actual '%d'", expected.KeepFrames(), actual.KeepFrames())) + if bytes.Compare(expected.OriginalPayload().AsRaw(), actual.OriginalPayload().AsRaw()) != 0 { + errs = multierr.Append(errs, fmt.Errorf("keepFrames does not match expected '%s', actual '%s'", expected.OriginalPayload().AsRaw(), actual.OriginalPayload().AsRaw())) } if expected.StartTime() != actual.StartTime() { @@ -284,27 +274,27 @@ func CompareProfile(expected, actual pprofile.Profile) error { errs = multierr.Append(errs, fmt.Errorf("period does not match expected '%d', actual '%d'", expected.Period(), actual.Period())) } - if expected.DefaultSampleType() != actual.DefaultSampleType() { - errs = multierr.Append(errs, fmt.Errorf("defaultSampleType does not match expected '%d', actual '%d'", expected.DefaultSampleType(), actual.DefaultSampleType())) + if expected.DefaultSampleTypeStrindex() != actual.DefaultSampleTypeStrindex() { + errs = multierr.Append(errs, fmt.Errorf("defaultSampleType does not match expected '%d', actual '%d'", expected.DefaultSampleTypeStrindex(), actual.DefaultSampleTypeStrindex())) } - if expected.PeriodType().Type() != actual.PeriodType().Type() || - expected.PeriodType().Unit() != actual.PeriodType().Unit() || + if expected.PeriodType().TypeStrindex() != actual.PeriodType().TypeStrindex() || + expected.PeriodType().UnitStrindex() != actual.PeriodType().UnitStrindex() || expected.PeriodType().AggregationTemporality() != actual.PeriodType().AggregationTemporality() { errs = multierr.Append(errs, fmt.Errorf("periodType does not match expected 'unit: %d, type: %d, aggregationTemporality: %d', actual 'unit: %d, type: %d,"+ - "aggregationTemporality: %d'", expected.PeriodType().Unit(), expected.PeriodType().Type(), expected.PeriodType().AggregationTemporality(), - actual.PeriodType().Unit(), actual.PeriodType().Type(), actual.PeriodType().AggregationTemporality())) + "aggregationTemporality: %d'", expected.PeriodType().UnitStrindex(), expected.PeriodType().TypeStrindex(), expected.PeriodType().AggregationTemporality(), + actual.PeriodType().UnitStrindex(), actual.PeriodType().TypeStrindex(), actual.PeriodType().AggregationTemporality())) } errs = multierr.Append(errs, internal.AddErrPrefix("sampleType", CompareProfileValueTypeSlice(expected.SampleType(), actual.SampleType()))) errs = multierr.Append(errs, internal.AddErrPrefix("sample", CompareProfileSampleSlice(expected.Sample(), actual.Sample()))) - errs = multierr.Append(errs, internal.AddErrPrefix("mapping", CompareProfileMappingSlice(expected.Mapping(), actual.Mapping()))) + errs = multierr.Append(errs, internal.AddErrPrefix("mapping", CompareProfileMappingSlice(expected.MappingTable(), actual.MappingTable()))) - errs = multierr.Append(errs, internal.AddErrPrefix("location", CompareProfileLocationSlice(expected.Location(), actual.Location()))) + errs = multierr.Append(errs, internal.AddErrPrefix("location", CompareProfileLocationSlice(expected.LocationTable(), actual.LocationTable()))) - errs = multierr.Append(errs, internal.AddErrPrefix("function", CompareProfileFunctionSlice(expected.Function(), actual.Function()))) + errs = multierr.Append(errs, internal.AddErrPrefix("function", CompareProfileFunctionSlice(expected.FunctionTable(), actual.FunctionTable()))) errs = multierr.Append(errs, internal.AddErrPrefix("attributeUnits", CompareProfileAttributeUnitSlice(expected.AttributeUnits(), actual.AttributeUnits()))) @@ -334,26 +324,26 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro if _, ok := matchingValueTypes[alr]; ok { continue } - if elr.Type() == alr.Type() && elr.Unit() == alr.Unit() { + if elr.TypeStrindex() == alr.TypeStrindex() && elr.UnitStrindex() == alr.UnitStrindex() { foundMatch = true matchingValueTypes[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`valueTypes are out of order: valueType "unit: %d, type: %d, aggregationTemporality: %d" expected at index %d, found at index %d`, - elr.Unit(), elr.Type(), elr.AggregationTemporality(), e, a)) + elr.UnitStrindex(), elr.TypeStrindex(), elr.AggregationTemporality(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected valueType "unit: %d, type: %d, aggregationTemporality: %d"`, elr.Unit(), elr.Type(), elr.AggregationTemporality())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected valueType "unit: %d, type: %d, aggregationTemporality: %d"`, elr.UnitStrindex(), elr.TypeStrindex(), elr.AggregationTemporality())) } } for i := 0; i < numValueTypes; i++ { if _, ok := matchingValueTypes[actual.At(i)]; !ok { errs = multierr.Append(errs, fmt.Errorf(`unexpected valueType "unit: %d, type: %d, aggregationTemporality: %d"`, - actual.At(i).Unit(), actual.At(i).Type(), actual.At(i).AggregationTemporality())) + actual.At(i).UnitStrindex(), actual.At(i).TypeStrindex(), actual.At(i).AggregationTemporality())) } } @@ -367,8 +357,8 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro for alr, elr := range matchingValueTypes { if !isValueTypeEqual(elr, alr) { errs = multierr.Append(errs, fmt.Errorf(`expected valueType "unit: %d, type: %d, aggregationTemporality: %d",`+ - `got "unit: %d, type: %d, aggregationTemporality: %d"`, elr.Unit(), elr.Type(), elr.AggregationTemporality(), - alr.Unit(), alr.Type(), alr.AggregationTemporality())) + `got "unit: %d, type: %d, aggregationTemporality: %d"`, elr.UnitStrindex(), elr.TypeStrindex(), elr.AggregationTemporality(), + alr.UnitStrindex(), alr.TypeStrindex(), alr.AggregationTemporality())) } } @@ -376,8 +366,8 @@ func CompareProfileValueTypeSlice(expected, actual pprofile.ValueTypeSlice) erro } func isValueTypeEqual(expected, actual pprofile.ValueType) bool { - return expected.Type() == actual.Type() && - expected.Unit() == actual.Unit() && + return expected.TypeStrindex() == actual.TypeStrindex() && + expected.UnitStrindex() == actual.UnitStrindex() && expected.AggregationTemporality() == actual.AggregationTemporality() } @@ -402,26 +392,26 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { if _, ok := matchingItems[alr]; ok { continue } - if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) { + if reflect.DeepEqual(elr.AttributeIndices().AsRaw(), alr.AttributeIndices().AsRaw()) { foundMatch = true matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`samples are out of order: sample "attributes: %v" expected at index %d, found at index %d`, - elr.Attributes().AsRaw(), e, a)) + elr.AttributeIndices().AsRaw(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected sample "attributes: %v"`, elr.Attributes().AsRaw())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected sample "attributes: %v"`, elr.AttributeIndices().AsRaw())) } } for i := 0; i < numSlice; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { errs = multierr.Append(errs, fmt.Errorf(`unexpected sample "attributes: %v"`, - actual.At(i).Attributes().AsRaw())) + actual.At(i).AttributeIndices().AsRaw())) } } @@ -433,7 +423,7 @@ func CompareProfileSampleSlice(expected, actual pprofile.SampleSlice) error { } for alr, elr := range matchingItems { - errPrefix := fmt.Sprintf(`sample "attributes: %v"`, elr.Attributes().AsRaw()) + errPrefix := fmt.Sprintf(`sample "attributes: %v"`, elr.AttributeIndices().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileSample(elr, alr))) } @@ -450,16 +440,8 @@ func CompareProfileSample(expected, actual pprofile.Sample) error { errs = multierr.Append(errs, fmt.Errorf("expected locationLenght '%d', got '%d'", expected.LocationsLength(), actual.LocationsLength())) } - if expected.StacktraceIdIndex() != actual.StacktraceIdIndex() { - errs = multierr.Append(errs, fmt.Errorf("expected stacktraceIdIndex '%d', got '%d'", expected.StacktraceIdIndex(), actual.StacktraceIdIndex())) - } - - if expected.Link() != actual.Link() { - errs = multierr.Append(errs, fmt.Errorf("expected link '%d', got '%d'", expected.Link(), actual.Link())) - } - - if !reflect.DeepEqual(expected.LocationIndex().AsRaw(), actual.LocationIndex().AsRaw()) { - errs = multierr.Append(errs, fmt.Errorf("expected locationIndex '%v', got '%v'", expected.LocationIndex().AsRaw(), actual.LocationIndex().AsRaw())) + if !reflect.DeepEqual(expected.TimestampsUnixNano().AsRaw(), actual.TimestampsUnixNano().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected timestampUnixNano '%v', got '%v'", expected.TimestampsUnixNano().AsRaw(), actual.TimestampsUnixNano().AsRaw())) } if !reflect.DeepEqual(expected.Value().AsRaw(), actual.Value().AsRaw()) { @@ -470,83 +452,13 @@ func CompareProfileSample(expected, actual pprofile.Sample) error { errs = multierr.Append(errs, fmt.Errorf("expected timestampUnixNano '%v', got '%v'", expected.TimestampsUnixNano().AsRaw(), actual.TimestampsUnixNano().AsRaw())) } - if !reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) { - errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) - } - - errPrefix := fmt.Sprintf(`labelSlice of sample with attributes "%v"`, expected.Attributes().AsRaw()) - errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLabelSlice(expected.Label(), actual.Label()))) - - return errs -} - -func CompareProfileLabelSlice(expected, actual pprofile.LabelSlice) error { - var errs error - if expected.Len() != actual.Len() { - errs = multierr.Append(errs, fmt.Errorf("number of labels doesn't match expected: %d, actual: %d", - expected.Len(), actual.Len())) - return errs - } - - numLabels := expected.Len() - - matchingLabels := make(map[pprofile.Label]pprofile.Label, numLabels) - - var outOfOrderErrs error - for e := 0; e < numLabels; e++ { - elr := expected.At(e) - var foundMatch bool - for a := 0; a < numLabels; a++ { - alr := actual.At(a) - if _, ok := matchingLabels[alr]; ok { - continue - } - if elr.Key() == alr.Key() { - foundMatch = true - matchingLabels[alr] = elr - if e != a { - outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`labels are out of order: label "key: %d" expected at index %d, found at index %d`, - elr.Key(), e, a)) - } - break - } - } - if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected label "key: %d"`, elr.Key())) - } - } - - for i := 0; i < numLabels; i++ { - if _, ok := matchingLabels[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected label "key: %d"`, - actual.At(i).Key())) - } - } - - if errs != nil { - return errs - } - if outOfOrderErrs != nil { - return outOfOrderErrs - } - - for alr, elr := range matchingLabels { - if !isLabelEqual(elr, alr) { - errs = multierr.Append(errs, fmt.Errorf(`label with "key: %d" does not match expected`, alr.Key())) - } + if !reflect.DeepEqual(expected.AttributeIndices().AsRaw(), actual.AttributeIndices().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.AttributeIndices().AsRaw(), actual.AttributeIndices().AsRaw())) } return errs } -func isLabelEqual(expected, actual pprofile.Label) bool { - return expected.Key() == actual.Key() && - expected.Str() == actual.Str() && - expected.Num() == actual.Num() && - expected.NumUnit() == actual.NumUnit() -} - func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { var errs error if expected.Len() != actual.Len() { @@ -568,26 +480,26 @@ func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { if _, ok := matchingItems[alr]; ok { continue } - if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) && elr.ID() == alr.ID() { + if reflect.DeepEqual(elr.AttributeIndices().AsRaw(), alr.AttributeIndices().AsRaw()) { foundMatch = true matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`mappings are out of order: mapping "attributes: %v, id: %d" expected at index %d, found at index %d`, - elr.Attributes().AsRaw(), elr.ID(), e, a)) + fmt.Errorf(`mappings are out of order: mapping "attributes: %v" expected at index %d, found at index %d`, + elr.AttributeIndices().AsRaw(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected mapping "attributes: %v, id: %d"`, elr.Attributes().AsRaw(), elr.ID())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected mapping "attributes: %v"`, elr.AttributeIndices().AsRaw())) } } for i := 0; i < numItems; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected profile mapping "attributes: %v, id: %d"`, - actual.At(i).Attributes().AsRaw(), actual.At(i).ID())) + errs = multierr.Append(errs, fmt.Errorf(`unexpected profile mapping "attributes: %v"`, + actual.At(i).AttributeIndices().AsRaw())) } } @@ -600,8 +512,8 @@ func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { for alr, elr := range matchingItems { if !isMappingEqual(elr, alr) { - errs = multierr.Append(errs, fmt.Errorf(`mapping with "attributes: %v, id: %d", does not match expected`, - alr.Attributes().AsRaw(), alr.ID())) + errs = multierr.Append(errs, fmt.Errorf(`mapping with "attributes: %v", does not match expected`, + alr.AttributeIndices().AsRaw())) } } @@ -609,14 +521,11 @@ func CompareProfileMappingSlice(expected, actual pprofile.MappingSlice) error { } func isMappingEqual(expected, actual pprofile.Mapping) bool { - return expected.ID() == actual.ID() && - expected.MemoryStart() == actual.MemoryStart() && + return expected.MemoryStart() == actual.MemoryStart() && expected.MemoryLimit() == actual.MemoryLimit() && expected.FileOffset() == actual.FileOffset() && - expected.Filename() == actual.Filename() && - expected.BuildID() == actual.BuildID() && - expected.BuildIDKind() == actual.BuildIDKind() && - reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) && + expected.FilenameStrindex() == actual.FilenameStrindex() && + reflect.DeepEqual(expected.AttributeIndices().AsRaw(), actual.AttributeIndices().AsRaw()) && expected.HasFunctions() == actual.HasFunctions() && expected.HasFilenames() == actual.HasFilenames() && expected.HasLineNumbers() == actual.HasLineNumbers() && @@ -644,26 +553,26 @@ func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error if _, ok := matchingItems[alr]; ok { continue } - if elr.Name() == alr.Name() { + if elr.NameStrindex() == alr.NameStrindex() { foundMatch = true matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`functions are out of order: function "name: %d" expected at index %d, found at index %d`, - elr.Name(), e, a)) + elr.NameStrindex(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected function "name: %d"`, elr.Name())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected function "name: %d"`, elr.NameStrindex())) } } for i := 0; i < numItems; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { errs = multierr.Append(errs, fmt.Errorf(`unexpected profile function "name: %d"`, - actual.At(i).Name())) + actual.At(i).NameStrindex())) } } @@ -676,7 +585,7 @@ func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error for alr, elr := range matchingItems { if !isFunctionEqual(elr, alr) { - errs = multierr.Append(errs, fmt.Errorf(`function with "name: %d" does not match expected`, alr.Name())) + errs = multierr.Append(errs, fmt.Errorf(`function with "name: %d" does not match expected`, alr.NameStrindex())) } } @@ -684,11 +593,10 @@ func CompareProfileFunctionSlice(expected, actual pprofile.FunctionSlice) error } func isFunctionEqual(expected, actual pprofile.Function) bool { - return expected.ID() == actual.ID() && - expected.Name() == actual.Name() && - expected.SystemName() == actual.SystemName() && + return expected.NameStrindex() == actual.NameStrindex() && + expected.SystemNameStrindex() == actual.SystemNameStrindex() && expected.StartLine() == actual.StartLine() && - expected.Filename() == actual.Filename() + expected.FilenameStrindex() == actual.FilenameStrindex() } func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error { @@ -712,26 +620,26 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error if _, ok := matchingItems[alr]; ok { continue } - if reflect.DeepEqual(elr.Attributes().AsRaw(), alr.Attributes().AsRaw()) && elr.ID() == alr.ID() { + if reflect.DeepEqual(elr.AttributeIndices().AsRaw(), alr.AttributeIndices().AsRaw()) { foundMatch = true matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, - fmt.Errorf(`locations are out of order: location "attributes: %v, id: %d" expected at index %d, found at index %d`, - elr.Attributes().AsRaw(), elr.ID(), e, a)) + fmt.Errorf(`locations are out of order: location "attributes: %v" expected at index %d, found at index %d`, + elr.AttributeIndices().AsRaw(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected location "attributes: %v, id: %d"`, elr.Attributes().AsRaw(), elr.ID())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected location "attributes: %v"`, elr.AttributeIndices().AsRaw())) } } for i := 0; i < numItems; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { - errs = multierr.Append(errs, fmt.Errorf(`unexpected location "attributes: %v, id: %d"`, - actual.At(i).Attributes().AsRaw(), actual.At(i).ID())) + errs = multierr.Append(errs, fmt.Errorf(`unexpected location "attributes: %v"`, + actual.At(i).AttributeIndices().AsRaw())) } } @@ -743,7 +651,7 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error } for alr, elr := range matchingItems { - errPrefix := fmt.Sprintf(`location "id: %d"`, elr.ID()) + errPrefix := fmt.Sprintf(`location "attributes: %v"`, elr.AttributeIndices().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLocation(elr, alr))) } @@ -752,9 +660,6 @@ func CompareProfileLocationSlice(expected, actual pprofile.LocationSlice) error func CompareProfileLocation(expected, actual pprofile.Location) error { var errs error - if expected.ID() != actual.ID() { - errs = multierr.Append(errs, fmt.Errorf("expected id '%d', got '%d'", expected.ID(), actual.ID())) - } if expected.MappingIndex() != actual.MappingIndex() { errs = multierr.Append(errs, fmt.Errorf("expected mappingIndex '%d', got '%d'", expected.MappingIndex(), actual.MappingIndex())) @@ -768,15 +673,11 @@ func CompareProfileLocation(expected, actual pprofile.Location) error { errs = multierr.Append(errs, fmt.Errorf("expected isFolded '%v', got '%v'", expected.IsFolded(), actual.IsFolded())) } - if expected.TypeIndex() != actual.TypeIndex() { - errs = multierr.Append(errs, fmt.Errorf("expected typeIndex '%d', got '%d'", expected.TypeIndex(), actual.TypeIndex())) - } - - if !reflect.DeepEqual(expected.Attributes().AsRaw(), actual.Attributes().AsRaw()) { - errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.Attributes().AsRaw(), actual.Attributes().AsRaw())) + if !reflect.DeepEqual(expected.AttributeIndices().AsRaw(), actual.AttributeIndices().AsRaw()) { + errs = multierr.Append(errs, fmt.Errorf("expected attributes '%v', got '%v'", expected.AttributeIndices().AsRaw(), actual.AttributeIndices().AsRaw())) } - errPrefix := fmt.Sprintf(`line od location with "id: %d"`, expected.ID()) + errPrefix := fmt.Sprintf(`line of location with "attributes: %v"`, expected.AttributeIndices().AsRaw()) errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfileLineSlice(expected.Line(), actual.Line()))) return errs @@ -869,26 +770,26 @@ func CompareProfileAttributeUnitSlice(expected, actual pprofile.AttributeUnitSli if _, ok := matchingItems[alr]; ok { continue } - if elr.AttributeKey() == alr.AttributeKey() && elr.Unit() == alr.Unit() { + if elr.AttributeKeyStrindex() == alr.AttributeKeyStrindex() && elr.UnitStrindex() == alr.UnitStrindex() { foundMatch = true matchingItems[alr] = elr if e != a { outOfOrderErrs = multierr.Append(outOfOrderErrs, fmt.Errorf(`attributeUnits are out of order: attributeUnit "attributeKey: %d" expected at index %d, found at index %d`, - elr.AttributeKey(), e, a)) + elr.AttributeKeyStrindex(), e, a)) } break } } if !foundMatch { - errs = multierr.Append(errs, fmt.Errorf(`missing expected attributeUnit "attributeKey: %d"`, elr.AttributeKey())) + errs = multierr.Append(errs, fmt.Errorf(`missing expected attributeUnit "attributeKey: %d"`, elr.AttributeKeyStrindex())) } } for i := 0; i < numItems; i++ { if _, ok := matchingItems[actual.At(i)]; !ok { errs = multierr.Append(errs, fmt.Errorf(`unexpected profile attributeUnit "attributeKey: %d"`, - actual.At(i).AttributeKey())) + actual.At(i).AttributeKeyStrindex())) } } diff --git a/pkg/pdatatest/pprofiletest/profiles_test.go b/pkg/pdatatest/pprofiletest/profiles_test.go index 8c7b20130172..509434b41f6b 100644 --- a/pkg/pdatatest/pprofiletest/profiles_test.go +++ b/pkg/pdatatest/pprofiletest/profiles_test.go @@ -183,7 +183,7 @@ func TestCompareProfiles(t *testing.T) { }, }, { - name: "ignore profile container order", + name: "ignore profile order", expected: func() pprofile.Profiles { p := pprofile.NewProfiles() rl := p.ResourceProfiles().AppendEmpty() @@ -212,13 +212,13 @@ func TestCompareProfiles(t *testing.T) { pc.Attributes().PutStr("continer-attr1", "value1") return p }(), - withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile containers are out of order: profile container "map[continer-attr1:value1]" expected at index 0, found at index 1; resource "map[key1:value1]": scope "scope1": profile containers are out of order: profile container "map[continer-attr2:value2]" expected at index 1, found at index 0`), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profiles are out of order: profile "map[continer-attr1:value1]" expected at index 0, found at index 1; resource "map[key1:value1]": scope "scope1": profiles are out of order: profile "map[continer-attr2:value2]" expected at index 1, found at index 0`), compareOptions: []CompareProfilesOption{ - IgnoreProfileContainersOrder(), + IgnoreProfilesOrder(), }, }, { - name: "ignore profile container attribute value", + name: "ignore profile attribute value", expected: func() pprofile.Profiles { p := pprofile.NewProfiles() rl := p.ResourceProfiles().AppendEmpty() @@ -247,14 +247,14 @@ func TestCompareProfiles(t *testing.T) { pc2.Attributes().PutStr("container-attr2", "value4") return p }(), - withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": missing expected profile container: map[container-attr1:value1]; resource "map[key1:value1]": scope "scope1": missing expected profile container: map[container-attr2:value2]; resource "map[key1:value1]": scope "scope1": unexpected profile container: map[container-attr1:value3]; resource "map[key1:value1]": scope "scope1": unexpected profile container: map[container-attr2:value4]`), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": missing expected profile: map[container-attr1:value1]; resource "map[key1:value1]": scope "scope1": missing expected profile: map[container-attr2:value2]; resource "map[key1:value1]": scope "scope1": unexpected profile: map[container-attr1:value3]; resource "map[key1:value1]": scope "scope1": unexpected profile: map[container-attr2:value4]`), compareOptions: []CompareProfilesOption{ - IgnoreProfileContainerAttributeValue("container-attr2"), - IgnoreProfileContainerAttributeValue("container-attr1"), + IgnoreProfileAttributeValue("container-attr2"), + IgnoreProfileAttributeValue("container-attr1"), }, }, { - name: "ignore profile container timestamp values", + name: "ignore profile timestamp values", expected: func(timestamp time.Time) pprofile.Profiles { p := pprofile.NewProfiles() rl := p.ResourceProfiles().AppendEmpty() @@ -265,7 +265,7 @@ func TestCompareProfiles(t *testing.T) { pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) pc.Attributes().PutStr("container-attr1", "value1") pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pc.SetEndTime(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + pc.SetDuration(pcommon.NewTimestampFromTime(timestamp2)) return p }(timestamp1), actual: func(timestamp time.Time) pprofile.Profiles { @@ -278,86 +278,10 @@ func TestCompareProfiles(t *testing.T) { pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) pc.Attributes().PutStr("container-attr1", "value1") pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pc.SetEndTime(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + pc.SetDuration(pcommon.NewTimestampFromTime(timestamp2)) return p }(timestamp2), - withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": start timestamp doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": end timestamp doesn't match expected: 1577836805000000000, actual: 1577836810000000000`), - compareOptions: []CompareProfilesOption{ - IgnoreProfileContainerTimestampValues(), - }, - }, - { - name: "ignore profile attribute value", - expected: func() pprofile.Profiles { - p := pprofile.NewProfiles() - rl := p.ResourceProfiles().AppendEmpty() - rl.Resource().Attributes().PutStr("key1", "value1") - l := rl.ScopeProfiles().AppendEmpty() - l.Scope().SetName("scope1") - pc := l.Profiles().AppendEmpty() - pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value1") - pc2 := l.Profiles().AppendEmpty() - pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) - pp2 := pc.Profile() - pp2.AttributeTable().PutStr("attr2", "value2") - return p - }(), - actual: func() pprofile.Profiles { - p := pprofile.NewProfiles() - rl := p.ResourceProfiles().AppendEmpty() - rl.Resource().Attributes().PutStr("key1", "value1") - l := rl.ScopeProfiles().AppendEmpty() - l.Scope().SetName("scope1") - pc := l.Profiles().AppendEmpty() - pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value1") - pc2 := l.Profiles().AppendEmpty() - pc2.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) - pp2 := pc.Profile() - pp2.AttributeTable().PutStr("attr2", "value3") - return p - }(), - withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[]": profile "map[]": attributes don't match expected: map[attr1:value1 attr2:value2], actual: map[attr1:value1 attr2:value3]`), - compareOptions: []CompareProfilesOption{ - IgnoreProfileAttributeValue("attr2"), - }, - }, - { - name: "ignore profile timestamp values", - expected: func(timestamp time.Time) pprofile.Profiles { - p := pprofile.NewProfiles() - rl := p.ResourceProfiles().AppendEmpty() - rl.Resource().Attributes().PutStr("key1", "value1") - l := rl.ScopeProfiles().AppendEmpty() - l.Scope().SetName("scope1") - pc := l.Profiles().AppendEmpty() - pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - pc.Attributes().PutStr("container-attr1", "value1") - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value1") - pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) - return p - }(timestamp1), - actual: func(timestamp time.Time) pprofile.Profiles { - p := pprofile.NewProfiles() - rl := p.ResourceProfiles().AppendEmpty() - rl.Resource().Attributes().PutStr("key1", "value1") - l := rl.ScopeProfiles().AppendEmpty() - l.Scope().SetName("scope1") - pc := l.Profiles().AppendEmpty() - pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - pc.Attributes().PutStr("container-attr1", "value1") - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value1") - pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) - return p - }(timestamp2), - withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": profile "map[container-attr1:value1]": startTime doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile container "map[container-attr1:value1]": profile "map[container-attr1:value1]": duration doesn't match expected: 1577836805000000000, actual: 1577836810000000000`), + withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": profile "map[container-attr1:value1]": start timestamp doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile "map[container-attr1:value1]": time doesn't match expected: 1577836800000000000, actual: 1577836805000000000; resource "map[key1:value1]": scope "scope1": profile "map[container-attr1:value1]": startTime doesn't match expected: 1577836800000000000, actual: 1577836805000000000`), compareOptions: []CompareProfilesOption{ IgnoreProfileTimestampValues(), }, @@ -373,10 +297,8 @@ func TestCompareProfiles(t *testing.T) { pc := l.Profiles().AppendEmpty() pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) pc.Attributes().PutStr("container-attr1", "value1") - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value1") - pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pc.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) return p }(timestamp1), actual: func(timestamp time.Time) pprofile.Profiles { @@ -388,10 +310,8 @@ func TestCompareProfiles(t *testing.T) { pc := l.Profiles().AppendEmpty() pc.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) pc.Attributes().PutStr("container-attr1", "value2") - pp := pc.Profile() - pp.AttributeTable().PutStr("attr1", "value2") - pp.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) - pp.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) + pc.SetStartTime(pcommon.NewTimestampFromTime(timestamp)) + pc.SetDuration(pcommon.NewTimestampFromTime(timestamp.Add(5 * time.Second))) return p }(timestamp2), withoutOptions: errors.New(`missing expected resource: map[key1:value1]; unexpected resource: map[key1:value2]`), @@ -600,7 +520,7 @@ func TestCompareScopeProfiles(t *testing.T) { sl.Profiles().AppendEmpty() return sl }(), - err: errors.New("number of profile containers doesn't match expected: 2, actual: 1"), + err: errors.New("number of profiles doesn't match expected: 2, actual: 1"), }, { name: "profile-records-order-mismatch", @@ -625,8 +545,8 @@ func TestCompareScopeProfiles(t *testing.T) { return sl }(), err: multierr.Combine( - errors.New(`profile containers are out of order: profile container "map[scope-attr1:value1]" expected at index 0, found at index 1`), - errors.New(`profile containers are out of order: profile container "map[scope-attr2:value2]" expected at index 1, found at index 0`), + errors.New(`profiles are out of order: profile "map[scope-attr1:value1]" expected at index 0, found at index 1`), + errors.New(`profiles are out of order: profile "map[scope-attr2:value2]" expected at index 1, found at index 0`), ), }, } @@ -637,99 +557,6 @@ func TestCompareScopeProfiles(t *testing.T) { } } -func TestCompareProfileContainer(t *testing.T) { - tests := []struct { - name string - expected pprofile.ProfileContainer - actual pprofile.ProfileContainer - err error - }{ - { - name: "empty", - expected: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - return l - }(), - actual: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - return l - }(), - }, - { - name: "equal", - expected: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - l.SetDroppedAttributesCount(2) - p := l.Profile() - p.SetKeepFrames(1) - return l - }(), - actual: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - l.SetDroppedAttributesCount(2) - p := l.Profile() - p.SetKeepFrames(1) - return l - }(), - }, - { - name: "not equal", - expected: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - l.SetDroppedAttributesCount(2) - p := l.Profile() - p.SetKeepFrames(1) - return l - }(), - actual: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111112"))) - l.SetDroppedAttributesCount(3) - p := l.Profile() - p.SetKeepFrames(3) - return l - }(), - err: multierr.Combine( - errors.New(`dropped attributes count doesn't match expected: 2, actual: 3`), - errors.New(`profileID does not match expected '70726f66696c65696431313131313131', actual '70726f66696c65696431313131313132'`), - fmt.Errorf(`profile "map[]": %w`, fmt.Errorf(`keepFrames does not match expected '1', actual '3'`)), - ), - }, - { - name: "not equal attributes", - expected: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - l.SetDroppedAttributesCount(2) - l.Attributes().PutStr("attr1", "va2") - p := l.Profile() - p.SetKeepFrames(1) - return l - }(), - actual: func() pprofile.ProfileContainer { - l := pprofile.NewProfileContainer() - l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) - l.SetDroppedAttributesCount(2) - l.Attributes().PutStr("attr1", "va1") - p := l.Profile() - p.SetKeepFrames(1) - return l - }(), - err: multierr.Combine( - errors.New(`attributes don't match expected: map[attr1:va2], actual: map[attr1:va1]`), - ), - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - require.Equal(t, test.err, CompareProfileContainer(test.expected, test.actual)) - }) - } -} - func TestCompareProfile(t *testing.T) { tests := []struct { name string @@ -752,28 +579,32 @@ func TestCompareProfile(t *testing.T) { name: "equal", expected: func() pprofile.Profile { l := pprofile.NewProfile() - l.SetKeepFrames(1) - l.AttributeTable().PutStr("key", "val") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.SetDefaultSampleTypeStrindex(1) + l.Attributes().PutStr("key", "val") l.SetPeriod(1) s := l.SampleType().AppendEmpty() - s.SetType(1) - s.SetUnit(1) + s.SetTypeStrindex(1) + s.SetUnitStrindex(1) a := l.AttributeUnits().AppendEmpty() - a.SetAttributeKey(1) - a.SetUnit(1) + a.SetAttributeKeyStrindex(1) + a.SetUnitStrindex(1) return l }(), actual: func() pprofile.Profile { l := pprofile.NewProfile() - l.SetKeepFrames(1) - l.AttributeTable().PutStr("key", "val") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.SetDefaultSampleTypeStrindex(1) + l.Attributes().PutStr("key", "val") l.SetPeriod(1) s := l.SampleType().AppendEmpty() - s.SetType(1) - s.SetUnit(1) + s.SetTypeStrindex(1) + s.SetUnitStrindex(1) a := l.AttributeUnits().AppendEmpty() - a.SetAttributeKey(1) - a.SetUnit(1) + a.SetAttributeKeyStrindex(1) + a.SetUnitStrindex(1) return l }(), }, @@ -781,33 +612,36 @@ func TestCompareProfile(t *testing.T) { name: "not equal", expected: func() pprofile.Profile { l := pprofile.NewProfile() - l.SetKeepFrames(1) - l.AttributeTable().PutStr("key", "val") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.SetDefaultSampleTypeStrindex(1) + l.Attributes().PutStr("key", "val") l.SetPeriod(1) s := l.SampleType().AppendEmpty() - s.SetType(1) - s.SetUnit(1) + s.SetTypeStrindex(1) + s.SetUnitStrindex(1) a := l.AttributeUnits().AppendEmpty() - a.SetAttributeKey(1) - a.SetUnit(1) + a.SetAttributeKeyStrindex(1) + a.SetUnitStrindex(1) return l }(), actual: func() pprofile.Profile { l := pprofile.NewProfile() - l.SetKeepFrames(2) - l.AttributeTable().PutStr("key1", "val1") + l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111"))) + l.SetDroppedAttributesCount(2) + l.SetDefaultSampleTypeStrindex(1) + l.Attributes().PutStr("key1", "val1") l.SetPeriod(2) s := l.SampleType().AppendEmpty() - s.SetType(2) - s.SetUnit(2) + s.SetTypeStrindex(2) + s.SetUnitStrindex(2) a := l.AttributeUnits().AppendEmpty() - a.SetAttributeKey(2) - a.SetUnit(2) + a.SetAttributeKeyStrindex(2) + a.SetUnitStrindex(2) return l }(), err: multierr.Combine( errors.New(`attributes don't match expected: map[key:val], actual: map[key1:val1]`), - errors.New(`keepFrames does not match expected '1', actual '2'`), errors.New(`period does not match expected '1', actual '2'`), fmt.Errorf(`sampleType: %w`, fmt.Errorf(`missing expected valueType "unit: 1, type: 1, aggregationTemporality: 0"`)), fmt.Errorf(`sampleType: %w`, fmt.Errorf(`unexpected valueType "unit: 2, type: 2, aggregationTemporality: 0"`)), @@ -846,24 +680,24 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), actual: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), @@ -873,24 +707,24 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), actual: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) return l }(), @@ -904,20 +738,20 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) return l }(), actual: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), @@ -930,24 +764,24 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), actual: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(2) return l }(), @@ -960,24 +794,24 @@ func TestCompareProfileValueTypeSlice(t *testing.T) { expected: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(2) - i2.SetUnit(2) + i2.SetTypeStrindex(2) + i2.SetUnitStrindex(2) i2.SetAggregationTemporality(1) return l }(), actual: func() pprofile.ValueTypeSlice { l := pprofile.NewValueTypeSlice() i1 := l.AppendEmpty() - i1.SetType(1) - i1.SetUnit(1) + i1.SetTypeStrindex(1) + i1.SetUnitStrindex(1) i1.SetAggregationTemporality(1) i2 := l.AppendEmpty() - i2.SetType(3) - i2.SetUnit(3) + i2.SetTypeStrindex(3) + i2.SetUnitStrindex(3) i2.SetAggregationTemporality(1) return l }(), @@ -1017,24 +851,20 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), actual: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), @@ -1044,25 +874,21 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), actual: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) return l }(), err: multierr.Combine( @@ -1075,20 +901,17 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) return l }(), actual: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), @@ -1101,30 +924,25 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), actual: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(3) return l }(), err: multierr.Combine( fmt.Errorf(`sample "attributes: [1 2 3]": %w`, fmt.Errorf(`expected locationLenght '2', got '3'`)), - fmt.Errorf(`sample "attributes: [1 2 3]": %w`, fmt.Errorf(`expected link '2', got '3'`)), ), }, { @@ -1132,24 +950,20 @@ func TestCompareProfileSampleSlice(t *testing.T) { expected: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3) - i2.SetLink(2) + i2.AttributeIndices().Append(1, 2, 3) i2.SetLocationsLength(2) return l }(), actual: func() pprofile.SampleSlice { l := pprofile.NewSampleSlice() i1 := l.AppendEmpty() - i1.SetLink(1) i1.SetLocationsLength(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i2 := l.AppendEmpty() - i2.Attributes().Append(1, 2, 3, 5) - i2.SetLink(3) + i2.AttributeIndices().Append(1, 2, 3, 5) i2.SetLocationsLength(3) return l }(), @@ -1190,20 +1004,14 @@ func TestCompareProfileSample(t *testing.T) { l := pprofile.NewSample() l.SetLocationsStartIndex(1) l.SetLocationsLength(1) - l.SetStacktraceIdIndex(1) - l.LocationIndex().Append(1, 2) - l.Attributes().Append(1, 2) - l.Label().AppendEmpty().SetKey(1) + l.AttributeIndices().Append(1, 2) return l }(), actual: func() pprofile.Sample { l := pprofile.NewSample() l.SetLocationsStartIndex(1) l.SetLocationsLength(1) - l.SetStacktraceIdIndex(1) - l.LocationIndex().Append(1, 2) - l.Attributes().Append(1, 2) - l.Label().AppendEmpty().SetKey(1) + l.AttributeIndices().Append(1, 2) return l }(), }, @@ -1213,29 +1021,20 @@ func TestCompareProfileSample(t *testing.T) { l := pprofile.NewSample() l.SetLocationsStartIndex(1) l.SetLocationsLength(1) - l.SetStacktraceIdIndex(1) - l.LocationIndex().Append(1, 2) - l.Attributes().Append(1, 2) - l.Label().AppendEmpty().SetKey(1) + l.AttributeIndices().Append(1, 2) return l }(), actual: func() pprofile.Sample { l := pprofile.NewSample() l.SetLocationsStartIndex(2) l.SetLocationsLength(3) - l.SetStacktraceIdIndex(3) - l.LocationIndex().Append(1, 2) - l.Attributes().Append(1, 2, 3) - l.Label().AppendEmpty().SetKey(2) + l.AttributeIndices().Append(1, 2, 3) return l }(), err: multierr.Combine( errors.New(`expected locationStartIndex '1', got '2'`), errors.New(`expected locationLenght '1', got '3'`), - errors.New(`expected stacktraceIdIndex '1', got '3'`), errors.New(`expected attributes '[1 2]', got '[1 2 3]'`), - fmt.Errorf(`labelSlice of sample with attributes "[1 2]": %w`, fmt.Errorf(`missing expected label "key: 1"`)), - fmt.Errorf(`labelSlice of sample with attributes "[1 2]": %w`, fmt.Errorf(`unexpected label "key: 2"`)), ), }, } @@ -1246,158 +1045,6 @@ func TestCompareProfileSample(t *testing.T) { } } -func TestCompareProfileLabelSlice(t *testing.T) { - tests := []struct { - name string - expected pprofile.LabelSlice - actual pprofile.LabelSlice - err error - }{ - { - name: "empty", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - return l - }(), - }, - { - name: "equal", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - }, - { - name: "equal wrong order", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - return l - }(), - err: multierr.Combine( - errors.New(`labels are out of order: label "key: 1" expected at index 0, found at index 1`), - errors.New(`labels are out of order: label "key: 2" expected at index 1, found at index 0`), - ), - }, - { - name: "wrong length", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - err: multierr.Combine( - errors.New(`number of labels doesn't match expected: 1, actual: 2`), - ), - }, - { - name: "not equal - does not match expected", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(5) - return l - }(), - err: multierr.Combine( - errors.New(`label with "key: 2" does not match expected`), - ), - }, - { - name: "not equal - missing", - expected: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(2) - i2.SetNum(4) - return l - }(), - actual: func() pprofile.LabelSlice { - l := pprofile.NewLabelSlice() - i1 := l.AppendEmpty() - i1.SetKey(1) - i1.SetNum(3) - i2 := l.AppendEmpty() - i2.SetKey(3) - i2.SetNum(6) - return l - }(), - err: multierr.Combine( - errors.New(`missing expected label "key: 2"`), - errors.New(`unexpected label "key: 3"`), - ), - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - require.Equal(t, test.err, CompareProfileLabelSlice(test.expected, test.actual)) - }) - } -} - func TestCompareProfileMappingSlice(t *testing.T) { tests := []struct { name string @@ -1421,25 +1068,21 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), }, @@ -1448,30 +1091,26 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1, 2) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1, 2) + i1.SetFilenameStrindex(1) return l }(), err: multierr.Combine( - errors.New(`mappings are out of order: mapping "attributes: [1], id: 1" expected at index 0, found at index 1`), - errors.New(`mappings are out of order: mapping "attributes: [1], id: 2" expected at index 1, found at index 0`), + errors.New(`mappings are out of order: mapping "attributes: [1 2]" expected at index 0, found at index 1`), + errors.New(`mappings are out of order: mapping "attributes: [1]" expected at index 1, found at index 0`), ), }, { @@ -1479,20 +1118,17 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetFilename(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), err: multierr.Combine( @@ -1504,29 +1140,25 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(3) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(3) return l }(), err: multierr.Combine( - errors.New(`mapping with "attributes: [1], id: 2", does not match expected`), + errors.New(`mapping with "attributes: [1]", does not match expected`), ), }, { @@ -1534,30 +1166,26 @@ func TestCompareProfileMappingSlice(t *testing.T) { expected: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1) - i2.SetFilename(2) + i2.AttributeIndices().Append(1) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.MappingSlice { l := pprofile.NewMappingSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1) - i1.SetFilename(1) + i1.AttributeIndices().Append(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(3) - i2.Attributes().Append(1, 2) - i2.SetFilename(2) + i2.AttributeIndices().Append(1, 2) + i2.SetFilenameStrindex(2) return l }(), err: multierr.Combine( - errors.New(`missing expected mapping "attributes: [1], id: 2"`), - errors.New(`unexpected profile mapping "attributes: [1 2], id: 3"`), + errors.New(`missing expected mapping "attributes: [1]"`), + errors.New(`unexpected profile mapping "attributes: [1 2]"`), ), }, } @@ -1591,25 +1219,21 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), }, @@ -1618,25 +1242,21 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) return l }(), err: multierr.Combine( @@ -1649,21 +1269,18 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), err: multierr.Combine( @@ -1675,25 +1292,21 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(3) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(3) return l }(), err: multierr.Combine( @@ -1705,25 +1318,21 @@ func TestCompareProfileFunctionSlice(t *testing.T) { expected: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.SetName(2) - i2.SetFilename(2) + i2.SetNameStrindex(2) + i2.SetFilenameStrindex(2) return l }(), actual: func() pprofile.FunctionSlice { l := pprofile.NewFunctionSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.SetName(1) - i1.SetFilename(1) + i1.SetNameStrindex(1) + i1.SetFilenameStrindex(1) i2 := l.AppendEmpty() - i2.SetID(3) - i2.SetName(3) - i2.SetFilename(3) + i2.SetNameStrindex(3) + i2.SetFilenameStrindex(3) return l }(), err: multierr.Combine( @@ -1762,24 +1371,20 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), actual: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), @@ -1789,30 +1394,26 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), actual: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) return l }(), err: multierr.Combine( - errors.New(`locations are out of order: location "attributes: [1 2], id: 1" expected at index 0, found at index 1`), - errors.New(`locations are out of order: location "attributes: [1 2 3], id: 2" expected at index 1, found at index 0`), + errors.New(`locations are out of order: location "attributes: [1 2]" expected at index 0, found at index 1`), + errors.New(`locations are out of order: location "attributes: [1 2 3]" expected at index 1, found at index 0`), ), }, { @@ -1820,20 +1421,17 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), actual: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) return l }(), @@ -1846,29 +1444,25 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), actual: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(3) return l }(), err: multierr.Combine( - fmt.Errorf(`location "id: 2": %w`, fmt.Errorf(`expected mappingIndex '2', got '3'`)), + fmt.Errorf(`location "attributes: [1 2 3]": %w`, fmt.Errorf(`expected mappingIndex '2', got '3'`)), ), }, { @@ -1876,30 +1470,26 @@ func TestCompareProfileLocationSlice(t *testing.T) { expected: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(2) - i2.Attributes().Append(1, 2, 3) + i2.AttributeIndices().Append(1, 2, 3) i2.SetMappingIndex(2) return l }(), actual: func() pprofile.LocationSlice { l := pprofile.NewLocationSlice() i1 := l.AppendEmpty() - i1.SetID(1) - i1.Attributes().Append(1, 2) + i1.AttributeIndices().Append(1, 2) i1.SetMappingIndex(1) i2 := l.AppendEmpty() - i2.SetID(3) - i2.Attributes().Append(1, 2, 3, 5) + i2.AttributeIndices().Append(1, 2, 3, 5) i2.SetMappingIndex(2) return l }(), err: multierr.Combine( - errors.New(`missing expected location "attributes: [1 2 3], id: 2"`), - errors.New(`unexpected location "attributes: [1 2 3 5], id: 3"`), + errors.New(`missing expected location "attributes: [1 2 3]"`), + errors.New(`unexpected location "attributes: [1 2 3 5]"`), ), }, } @@ -1932,23 +1522,19 @@ func TestCompareProfileLocation(t *testing.T) { name: "equal", expected: func() pprofile.Location { l := pprofile.NewLocation() - l.SetID(1) l.SetAddress(2) l.SetIsFolded(true) l.SetMappingIndex(4) - l.SetTypeIndex(2) - l.Attributes().Append(1, 2, 3) + l.AttributeIndices().Append(1, 2, 3) l.Line().AppendEmpty().Line() return l }(), actual: func() pprofile.Location { l := pprofile.NewLocation() - l.SetID(1) l.SetAddress(2) l.SetIsFolded(true) l.SetMappingIndex(4) - l.SetTypeIndex(2) - l.Attributes().Append(1, 2, 3) + l.AttributeIndices().Append(1, 2, 3) l.Line().AppendEmpty() return l }(), @@ -1957,23 +1543,19 @@ func TestCompareProfileLocation(t *testing.T) { name: "not equal", expected: func() pprofile.Location { l := pprofile.NewLocation() - l.SetID(1) l.SetAddress(3) l.SetIsFolded(false) l.SetMappingIndex(2) - l.SetTypeIndex(3) - l.Attributes().Append(1, 2, 3, 4) + l.AttributeIndices().Append(1, 2, 3, 4) l.Line().AppendEmpty().SetFunctionIndex(3) return l }(), actual: func() pprofile.Location { l := pprofile.NewLocation() - l.SetID(1) l.SetAddress(2) l.SetIsFolded(true) l.SetMappingIndex(4) - l.SetTypeIndex(2) - l.Attributes().Append(1, 2, 3) + l.AttributeIndices().Append(1, 2, 3) l.Line().AppendEmpty().Line() return l }(), @@ -1981,10 +1563,9 @@ func TestCompareProfileLocation(t *testing.T) { errors.New(`expected mappingIndex '2', got '4'`), errors.New(`expected address '3', got '2'`), errors.New(`expected isFolded 'false', got 'true'`), - errors.New(`expected typeIndex '3', got '2'`), errors.New(`expected attributes '[1 2 3 4]', got '[1 2 3]'`), - fmt.Errorf(`line od location with "id: 1": %w`, fmt.Errorf(`missing expected line "functionIndex: 3"`)), - fmt.Errorf(`line od location with "id: 1": %w`, fmt.Errorf(`unexpected profile line "functionIndex: 0"`)), + fmt.Errorf(`line of location with "attributes: [1 2 3 4]": %w`, fmt.Errorf(`missing expected line "functionIndex: 3"`)), + fmt.Errorf(`line of location with "attributes: [1 2 3 4]": %w`, fmt.Errorf(`unexpected profile line "functionIndex: 0"`)), ), }, } @@ -2189,21 +1770,21 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { expected: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) return l }(), actual: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) return l }(), }, @@ -2212,21 +1793,21 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { expected: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) return l }(), actual: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) return l }(), err: multierr.Combine( @@ -2239,18 +1820,18 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { expected: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) return l }(), actual: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) return l }(), err: multierr.Combine( @@ -2262,21 +1843,21 @@ func TestCompareProfileAttributeUnitSlice(t *testing.T) { expected: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(4) - i2.SetUnit(5) + i2.SetAttributeKeyStrindex(4) + i2.SetUnitStrindex(5) return l }(), actual: func() pprofile.AttributeUnitSlice { l := pprofile.NewAttributeUnitSlice() i1 := l.AppendEmpty() - i1.SetAttributeKey(2) - i1.SetUnit(3) + i1.SetAttributeKeyStrindex(2) + i1.SetUnitStrindex(3) i2 := l.AppendEmpty() - i2.SetAttributeKey(6) - i2.SetUnit(7) + i2.SetAttributeKeyStrindex(6) + i2.SetUnitStrindex(7) return l }(), err: multierr.Combine( From 9c939c58b8c8c652d95ffb47f7f28c661a636e31 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 4 Dec 2024 14:50:30 +0100 Subject: [PATCH 21/22] fix lint Signed-off-by: odubajDT --- pkg/pdatatest/pprofiletest/profiles.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/pdatatest/pprofiletest/profiles.go b/pkg/pdatatest/pprofiletest/profiles.go index ae082f29b544..dc1222b8e4d0 100644 --- a/pkg/pdatatest/pprofiletest/profiles.go +++ b/pkg/pdatatest/pprofiletest/profiles.go @@ -258,7 +258,7 @@ func CompareProfile(expected, actual pprofile.Profile) error { errs = multierr.Append(errs, fmt.Errorf("originalPayloadFormat does not match expected '%s', actual '%s'", expected.OriginalPayloadFormat(), actual.OriginalPayloadFormat())) } - if bytes.Compare(expected.OriginalPayload().AsRaw(), actual.OriginalPayload().AsRaw()) != 0 { + if !bytes.Equal(expected.OriginalPayload().AsRaw(), actual.OriginalPayload().AsRaw()) { errs = multierr.Append(errs, fmt.Errorf("keepFrames does not match expected '%s', actual '%s'", expected.OriginalPayload().AsRaw(), actual.OriginalPayload().AsRaw())) } From 3032a03136f416600924ac2c65ce0740e1ae66e9 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 10 Dec 2024 13:23:16 +0100 Subject: [PATCH 22/22] go mod tidy Signed-off-by: odubajDT --- pkg/pdatatest/go.mod | 2 +- pkg/pdatatest/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/pdatatest/go.mod b/pkg/pdatatest/go.mod index f2ad6d218ef7..47f5a5b66f9b 100644 --- a/pkg/pdatatest/go.mod +++ b/pkg/pdatatest/go.mod @@ -7,7 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.115.0 github.com/stretchr/testify v1.10.0 go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8 - go.opentelemetry.io/collector/pdata/pprofile v0.115.0 + go.opentelemetry.io/collector/pdata/pprofile v0.115.1-0.20241206185113-3f3e208e71b8 go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 ) diff --git a/pkg/pdatatest/go.sum b/pkg/pdatatest/go.sum index 1120d7f0ab2a..292df9eba42a 100644 --- a/pkg/pdatatest/go.sum +++ b/pkg/pdatatest/go.sum @@ -33,8 +33,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8 h1:PUaCJ1XIIomqXvFBF6hMFikhZIwoBc57UP7xlaRT//I= go.opentelemetry.io/collector/pdata v1.21.1-0.20241206185113-3f3e208e71b8/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= -go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= -go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= +go.opentelemetry.io/collector/pdata/pprofile v0.115.1-0.20241206185113-3f3e208e71b8 h1:+RGyM6ZhtNHRaiNbIiJ82Ik6TFmk3BCOgLvhw509Hc4= +go.opentelemetry.io/collector/pdata/pprofile v0.115.1-0.20241206185113-3f3e208e71b8/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=