Skip to content

Commit

Permalink
upgrade pprofiletest to proto v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Dec 17, 2024
1 parent 2839359 commit ae73072
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 42 deletions.
33 changes: 28 additions & 5 deletions pkg/pdatatest/pprofiletest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofi
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()
for l := 0; l < lr.AttributeTable().Len(); l++ {
a := lr.AttributeTable().At(l)
if a.Key() == opt.attributeName {
a.Value().SetEmptyBytes()
}
}
}
}
Expand Down Expand Up @@ -206,10 +208,31 @@ func sortProfileSlices(ls pprofile.Profiles) {
if !bytes.Equal(as[:], bs[:]) {
return bytes.Compare(as[:], bs[:]) < 0
}
aAttrs := pdatautil.MapHash(a.Attributes())
bAttrs := pdatautil.MapHash(b.Attributes())
am := pcommon.NewMap()
for _, i := range a.AttributeIndices().AsRaw() {

Check failure on line 212 in pkg/pdatatest/pprofiletest/options.go

View workflow job for this annotation

GitHub Actions / govulncheck (pkg)

a.AttributeIndices undefined (type pprofile.Profile has no field or method AttributeIndices)
v := a.AttributeTable().At(int(i))
am.PutStr(v.Key(), v.Value().AsString())
}
bm := pcommon.NewMap()
for _, i := range b.AttributeIndices().AsRaw() {

Check failure on line 217 in pkg/pdatatest/pprofiletest/options.go

View workflow job for this annotation

GitHub Actions / govulncheck (pkg)

b.AttributeIndices undefined (type pprofile.Profile has no field or method AttributeIndices)
v := b.AttributeTable().At(int(i))
bm.PutStr(v.Key(), v.Value().AsString())
}

aAttrs := pdatautil.MapHash(am)
bAttrs := pdatautil.MapHash(bm)
return bytes.Compare(aAttrs[:], bAttrs[:]) < 0
})
}
}
}

func profileAttributesToMap(p pprofile.Profile) map[string]string {
d := map[string]string{}
for _, i := range p.AttributeIndices().AsRaw() {

Check failure on line 232 in pkg/pdatatest/pprofiletest/options.go

View workflow job for this annotation

GitHub Actions / govulncheck (pkg)

p.AttributeIndices undefined (type pprofile.Profile has no field or method AttributeIndices)
v := p.AttributeTable().At(int(i))
d[v.Key()] = v.Value().AsString()
}

return d
}
27 changes: 21 additions & 6 deletions pkg/pdatatest/pprofiletest/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,32 +181,36 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error {
var outOfOrderErrs error
for e := 0; e < numProfiles; e++ {
elr := expected.Profiles().At(e)
em := profileAttributesToMap(elr)

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()) {
am := profileAttributesToMap(alr)

if reflect.DeepEqual(em, am) {
foundMatch = true
matchingProfiles[alr] = elr
if e != a {
outOfOrderErrs = multierr.Append(outOfOrderErrs,
fmt.Errorf(`profiles are out of order: profile "%v" expected at index %d, found at index %d`,
elr.Attributes().AsRaw(), e, a))
em, e, a))
}
break
}
}
if !foundMatch {
errs = multierr.Append(errs, fmt.Errorf("missing expected profile: %v", elr.Attributes().AsRaw()))
errs = multierr.Append(errs, fmt.Errorf("missing expected profile: %v", em))
}
}

for i := 0; i < numProfiles; i++ {
if _, ok := matchingProfiles[actual.Profiles().At(i)]; !ok {
errs = multierr.Append(errs, fmt.Errorf("unexpected profile: %v",
actual.Profiles().At(i).Attributes().AsRaw()))
profileAttributesToMap(actual.Profiles().At(i))))
}
}

Expand All @@ -218,15 +222,26 @@ func CompareScopeProfiles(expected, actual pprofile.ScopeProfiles) error {
}

for alr, elr := range matchingProfiles {
errPrefix := fmt.Sprintf(`profile "%v"`, elr.Attributes().AsRaw())
errPrefix := fmt.Sprintf(`profile "%v"`, profileAttributesToMap(elr))
errs = multierr.Append(errs, internal.AddErrPrefix(errPrefix, CompareProfile(elr, alr)))
}
return errs
}

func compareAttributes(a, b pprofile.Profile) error {
aa := profileAttributesToMap(a)
ba := profileAttributesToMap(b)

if !reflect.DeepEqual(aa, ba) {
return fmt.Errorf("attributes don't match expected: %v, actual: %v", aa, ba)
}

return nil
}

func CompareProfile(expected, actual pprofile.Profile) error {
errs := multierr.Combine(
internal.CompareAttributes(expected.Attributes(), actual.Attributes()),
compareAttributes(expected, actual),
internal.CompareDroppedAttributesCount(expected.DroppedAttributesCount(), actual.DroppedAttributesCount()),
)

Expand Down
Loading

0 comments on commit ae73072

Please sign in to comment.