|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package pprofiletest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pprofiletest" |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "time" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 11 | + "go.opentelemetry.io/collector/pdata/pprofile" |
| 12 | + |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal" |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" |
| 15 | +) |
| 16 | + |
| 17 | +// CompareProfilesOption can be used to mutate expected and/or actual profiles before comparing. |
| 18 | +type CompareProfilesOption interface { |
| 19 | + applyOnProfiles(expected, actual pprofile.Profiles) |
| 20 | +} |
| 21 | + |
| 22 | +type compareProfilesOptionFunc func(expected, actual pprofile.Profiles) |
| 23 | + |
| 24 | +func (f compareProfilesOptionFunc) applyOnProfiles(expected, actual pprofile.Profiles) { |
| 25 | + f(expected, actual) |
| 26 | +} |
| 27 | + |
| 28 | +// IgnoreResourceAttributeValue is a CompareProfilesOption that removes a resource attribute |
| 29 | +// from all resources. |
| 30 | +func IgnoreResourceAttributeValue(attributeName string) CompareProfilesOption { |
| 31 | + return ignoreResourceAttributeValue{ |
| 32 | + attributeName: attributeName, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +type ignoreResourceAttributeValue struct { |
| 37 | + attributeName string |
| 38 | +} |
| 39 | + |
| 40 | +func (opt ignoreResourceAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { |
| 41 | + opt.maskProfilesResourceAttributeValue(expected) |
| 42 | + opt.maskProfilesResourceAttributeValue(actual) |
| 43 | +} |
| 44 | + |
| 45 | +func (opt ignoreResourceAttributeValue) maskProfilesResourceAttributeValue(profiles pprofile.Profiles) { |
| 46 | + rls := profiles.ResourceProfiles() |
| 47 | + for i := 0; i < rls.Len(); i++ { |
| 48 | + internal.MaskResourceAttributeValue(rls.At(i).Resource(), opt.attributeName) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// IgnoreResourceAttributeValue is a CompareProfilesOption that removes a resource attribute |
| 53 | +// from all resources. |
| 54 | +func IgnoreScopeAttributeValue(attributeName string) CompareProfilesOption { |
| 55 | + return ignoreScopeAttributeValue{ |
| 56 | + attributeName: attributeName, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type ignoreScopeAttributeValue struct { |
| 61 | + attributeName string |
| 62 | +} |
| 63 | + |
| 64 | +func (opt ignoreScopeAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { |
| 65 | + opt.maskProfilesScopeAttributeValue(expected) |
| 66 | + opt.maskProfilesScopeAttributeValue(actual) |
| 67 | +} |
| 68 | + |
| 69 | +func (opt ignoreScopeAttributeValue) maskProfilesScopeAttributeValue(profiles pprofile.Profiles) { |
| 70 | + rls := profiles.ResourceProfiles() |
| 71 | + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { |
| 72 | + sls := rls.At(i).ScopeProfiles() |
| 73 | + for j := 0; j < sls.Len(); j++ { |
| 74 | + lr := sls.At(j) |
| 75 | + val, exists := lr.Scope().Attributes().Get(opt.attributeName) |
| 76 | + if exists { |
| 77 | + val.SetEmptyBytes() |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// IgnoreProfileAttributeValue is a CompareProfilesOption that sets the value of an attribute |
| 84 | +// to empty bytes for every profile |
| 85 | +func IgnoreProfileAttributeValue(attributeName string) CompareProfilesOption { |
| 86 | + return ignoreProfileAttributeValue{ |
| 87 | + attributeName: attributeName, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +type ignoreProfileAttributeValue struct { |
| 92 | + attributeName string |
| 93 | +} |
| 94 | + |
| 95 | +func (opt ignoreProfileAttributeValue) applyOnProfiles(expected, actual pprofile.Profiles) { |
| 96 | + opt.maskProfileAttributeValue(expected) |
| 97 | + opt.maskProfileAttributeValue(actual) |
| 98 | +} |
| 99 | + |
| 100 | +func (opt ignoreProfileAttributeValue) maskProfileAttributeValue(profiles pprofile.Profiles) { |
| 101 | + rls := profiles.ResourceProfiles() |
| 102 | + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { |
| 103 | + sls := rls.At(i).ScopeProfiles() |
| 104 | + for j := 0; j < sls.Len(); j++ { |
| 105 | + lrs := sls.At(j).Profiles() |
| 106 | + for k := 0; k < lrs.Len(); k++ { |
| 107 | + lr := lrs.At(k) |
| 108 | + val, exists := lr.Attributes().Get(opt.attributeName) |
| 109 | + if exists { |
| 110 | + val.SetEmptyBytes() |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// IgnoreProfileTimestampValues is a CompareProfilesOption that sets the value of start timestamp |
| 118 | +// and duration to empty bytes for every profile |
| 119 | +func IgnoreProfileTimestampValues() CompareProfilesOption { |
| 120 | + return ignoreProfileTimestampValues{} |
| 121 | +} |
| 122 | + |
| 123 | +type ignoreProfileTimestampValues struct{} |
| 124 | + |
| 125 | +func (opt ignoreProfileTimestampValues) applyOnProfiles(expected, actual pprofile.Profiles) { |
| 126 | + opt.maskProfileTimestampValues(expected) |
| 127 | + opt.maskProfileTimestampValues(actual) |
| 128 | +} |
| 129 | + |
| 130 | +func (opt ignoreProfileTimestampValues) maskProfileTimestampValues(profiles pprofile.Profiles) { |
| 131 | + rls := profiles.ResourceProfiles() |
| 132 | + for i := 0; i < profiles.ResourceProfiles().Len(); i++ { |
| 133 | + sls := rls.At(i).ScopeProfiles() |
| 134 | + for j := 0; j < sls.Len(); j++ { |
| 135 | + lrs := sls.At(j).Profiles() |
| 136 | + for k := 0; k < lrs.Len(); k++ { |
| 137 | + lr := lrs.At(k) |
| 138 | + lr.SetStartTime(pcommon.NewTimestampFromTime(time.Time{})) |
| 139 | + lr.SetDuration(pcommon.NewTimestampFromTime(time.Time{})) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// IgnoreResourceProfilesOrder is a CompareProfilesOption that ignores the order of resource traces/metrics/profiles. |
| 146 | +func IgnoreResourceProfilesOrder() CompareProfilesOption { |
| 147 | + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { |
| 148 | + sortResourceProfilesSlice(expected.ResourceProfiles()) |
| 149 | + sortResourceProfilesSlice(actual.ResourceProfiles()) |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +func sortResourceProfilesSlice(rls pprofile.ResourceProfilesSlice) { |
| 154 | + rls.Sort(func(a, b pprofile.ResourceProfiles) bool { |
| 155 | + if a.SchemaUrl() != b.SchemaUrl() { |
| 156 | + return a.SchemaUrl() < b.SchemaUrl() |
| 157 | + } |
| 158 | + aAttrs := pdatautil.MapHash(a.Resource().Attributes()) |
| 159 | + bAttrs := pdatautil.MapHash(b.Resource().Attributes()) |
| 160 | + return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +// IgnoreScopeProfilesOrder is a CompareProfilesOption that ignores the order of instrumentation scope traces/metrics/profiles. |
| 165 | +func IgnoreScopeProfilesOrder() CompareProfilesOption { |
| 166 | + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { |
| 167 | + sortScopeProfilesSlices(expected) |
| 168 | + sortScopeProfilesSlices(actual) |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +func sortScopeProfilesSlices(ls pprofile.Profiles) { |
| 173 | + for i := 0; i < ls.ResourceProfiles().Len(); i++ { |
| 174 | + ls.ResourceProfiles().At(i).ScopeProfiles().Sort(func(a, b pprofile.ScopeProfiles) bool { |
| 175 | + if a.SchemaUrl() != b.SchemaUrl() { |
| 176 | + return a.SchemaUrl() < b.SchemaUrl() |
| 177 | + } |
| 178 | + if a.Scope().Name() != b.Scope().Name() { |
| 179 | + return a.Scope().Name() < b.Scope().Name() |
| 180 | + } |
| 181 | + return a.Scope().Version() < b.Scope().Version() |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// IgnoreProfilesOrder is a CompareProfilesOption that ignores the order of profile records. |
| 187 | +func IgnoreProfilesOrder() CompareProfilesOption { |
| 188 | + return compareProfilesOptionFunc(func(expected, actual pprofile.Profiles) { |
| 189 | + sortProfileSlices(expected) |
| 190 | + sortProfileSlices(actual) |
| 191 | + }) |
| 192 | +} |
| 193 | + |
| 194 | +func sortProfileSlices(ls pprofile.Profiles) { |
| 195 | + for i := 0; i < ls.ResourceProfiles().Len(); i++ { |
| 196 | + for j := 0; j < ls.ResourceProfiles().At(i).ScopeProfiles().Len(); j++ { |
| 197 | + ls.ResourceProfiles().At(i).ScopeProfiles().At(j).Profiles().Sort(func(a, b pprofile.Profile) bool { |
| 198 | + if a.StartTime() != b.StartTime() { |
| 199 | + return a.StartTime() < b.StartTime() |
| 200 | + } |
| 201 | + if a.Duration() != b.Duration() { |
| 202 | + return a.Duration() < b.Duration() |
| 203 | + } |
| 204 | + as := a.ProfileID() |
| 205 | + bs := b.ProfileID() |
| 206 | + if !bytes.Equal(as[:], bs[:]) { |
| 207 | + return bytes.Compare(as[:], bs[:]) < 0 |
| 208 | + } |
| 209 | + aAttrs := pdatautil.MapHash(a.Attributes()) |
| 210 | + bAttrs := pdatautil.MapHash(b.Attributes()) |
| 211 | + return bytes.Compare(aAttrs[:], bAttrs[:]) < 0 |
| 212 | + }) |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments