Skip to content

Commit 2ae3be6

Browse files
committed
Avoid duplicates in the attributes table
1 parent 4359f65 commit 2ae3be6

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pdata/pprofile/attributes.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,26 @@ func PutAttribute(table AttributeTableSlice, record attributable, key string, va
7878
}
7979
}
8080

81-
if table.Len() >= math.MaxInt32 {
82-
return errTooManyTableEntries
83-
}
84-
8581
if record.AttributeIndices().Len() >= math.MaxInt32 {
8682
return errors.New("too many entries in AttributeIndices")
8783
}
8884

85+
for j := range table.Len() {
86+
a := table.At(j)
87+
if a.Key() == key && a.Value().Equal(value) {
88+
if j > math.MaxInt32 {
89+
return errTooManyTableEntries
90+
}
91+
// Add the index of the existing attribute to the indices.
92+
record.AttributeIndices().Append(int32(j)) //nolint:gosec // overflow checked
93+
return nil
94+
}
95+
}
96+
97+
if table.Len() >= math.MaxInt32 {
98+
return errTooManyTableEntries
99+
}
100+
89101
// Add the key/value pair as a new attribute to the table...
90102
entry := table.AppendEmpty()
91103
entry.SetKey(key)

pdata/pprofile/attributes_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ func testPutAttribute(t *testing.T, record attributable) {
5858
assert.Equal(t, 1, table.Len())
5959
assert.Equal(t, []int32{0}, record.AttributeIndices().AsRaw())
6060

61+
// Special case: removing and adding again should not change the table as
62+
// this can lead to multiple identical attributes in the table.
63+
record.AttributeIndices().FromRaw([]int32{})
64+
require.NoError(t, PutAttribute(table, record, "hello", pcommon.NewValueStr("world")))
65+
assert.Equal(t, 1, table.Len())
66+
assert.Equal(t, []int32{0}, record.AttributeIndices().AsRaw())
67+
6168
// Put an attribute, same key, different value.
6269
// This updates the index and adds to the table.
6370
fmt.Printf("test\n")

0 commit comments

Comments
 (0)