@@ -13,6 +13,36 @@ import (
13
13
"go.opentelemetry.io/collector/pdata/pcommon"
14
14
)
15
15
16
+ func TestAddAttribute (t * testing.T ) {
17
+ table := NewAttributeTableSlice ()
18
+ att := table .AppendEmpty ()
19
+ att .SetKey ("hello" )
20
+ att .Value ().SetStr ("world" )
21
+
22
+ // Add a brand new attribute
23
+ loc := NewLocation ()
24
+ err := AddAttribute (table , loc , "bonjour" , pcommon .NewValueStr ("monde" ))
25
+ require .NoError (t , err )
26
+
27
+ assert .Equal (t , 2 , table .Len ())
28
+ assert .Equal (t , []int32 {1 }, loc .AttributeIndices ().AsRaw ())
29
+
30
+ // Add an already existing attribute
31
+ mapp := NewMapping ()
32
+ err = AddAttribute (table , mapp , "hello" , pcommon .NewValueStr ("world" ))
33
+ require .NoError (t , err )
34
+
35
+ assert .Equal (t , 2 , table .Len ())
36
+ assert .Equal (t , []int32 {0 }, mapp .AttributeIndices ().AsRaw ())
37
+
38
+ // Add a duplicate attribute
39
+ err = AddAttribute (table , mapp , "hello" , pcommon .NewValueStr ("world" ))
40
+ require .NoError (t , err )
41
+
42
+ assert .Equal (t , 2 , table .Len ())
43
+ assert .Equal (t , []int32 {0 }, mapp .AttributeIndices ().AsRaw ())
44
+ }
45
+
16
46
func TestFromAttributeIndices (t * testing.T ) {
17
47
table := NewAttributeTableSlice ()
18
48
att := table .AppendEmpty ()
@@ -44,34 +74,85 @@ func TestFromAttributeIndices(t *testing.T) {
44
74
assert .Equal (t , attrs .AsRaw (), m )
45
75
}
46
76
47
- func TestAddAttribute (t * testing.T ) {
77
+ func testPutAttribute (t * testing.T , record attributable ) {
48
78
table := NewAttributeTableSlice ()
49
- att := table .AppendEmpty ()
50
- att .SetKey ("hello" )
51
- att .Value ().SetStr ("world" )
52
-
53
- // Add a brand new attribute
54
- loc := NewLocation ()
55
- err := AddAttribute (table , loc , "bonjour" , pcommon .NewValueStr ("monde" ))
56
- require .NoError (t , err )
57
79
80
+ // Put a first attribute.
81
+ require .NoError (t , PutAttribute (table , record , "hello" , pcommon .NewValueStr ("world" )))
82
+ assert .Equal (t , 1 , table .Len ())
83
+ assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
84
+
85
+ // Put an attribute, same key, same value.
86
+ // This should be a no-op.
87
+ require .NoError (t , PutAttribute (table , record , "hello" , pcommon .NewValueStr ("world" )))
88
+ assert .Equal (t , 1 , table .Len ())
89
+ assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
90
+
91
+ // Special case: removing and adding again should not change the table as
92
+ // this can lead to multiple identical attributes in the table.
93
+ record .AttributeIndices ().FromRaw ([]int32 {})
94
+ require .NoError (t , PutAttribute (table , record , "hello" , pcommon .NewValueStr ("world" )))
95
+ assert .Equal (t , 1 , table .Len ())
96
+ assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
97
+
98
+ // Put an attribute, same key, different value.
99
+ // This updates the index and adds to the table.
100
+ fmt .Printf ("test\n " )
101
+ require .NoError (t , PutAttribute (table , record , "hello" , pcommon .NewValueStr ("world2" )))
58
102
assert .Equal (t , 2 , table .Len ())
59
- assert .Equal (t , []int32 {1 }, loc .AttributeIndices ().AsRaw ())
60
-
61
- // Add an already existing attribute
62
- mapp := NewMapping ()
63
- err = AddAttribute (table , mapp , "hello" , pcommon .NewValueStr ("world" ))
64
- require .NoError (t , err )
103
+ assert .Equal (t , []int32 {1 }, record .AttributeIndices ().AsRaw ())
65
104
105
+ // Put an attribute that already exists in the table.
106
+ // This updates the index and does not add to the table.
107
+ require .NoError (t , PutAttribute (table , record , "hello" , pcommon .NewValueStr ("world" )))
66
108
assert .Equal (t , 2 , table .Len ())
67
- assert .Equal (t , []int32 {0 }, mapp .AttributeIndices ().AsRaw ())
109
+ assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
110
+
111
+ // Put a new attribute.
112
+ // This adds an index and adds to the table.
113
+ require .NoError (t , PutAttribute (table , record , "good" , pcommon .NewValueStr ("day" )))
114
+ assert .Equal (t , 3 , table .Len ())
115
+ assert .Equal (t , []int32 {0 , 2 }, record .AttributeIndices ().AsRaw ())
116
+
117
+ // Add multiple distinct attributes.
118
+ for i := range 100 {
119
+ require .NoError (t , PutAttribute (table , record , fmt .Sprintf ("key_%d" , i ), pcommon .NewValueStr ("day" )))
120
+ assert .Equal (t , i + 4 , table .Len ())
121
+ assert .Equal (t , i + 3 , record .AttributeIndices ().Len ())
122
+ }
68
123
69
- // Add a duplicate attribute
70
- err = AddAttribute (table , mapp , "hello" , pcommon .NewValueStr ("world" ))
71
- require .NoError (t , err )
124
+ // Add a negative index to the record.
125
+ record .AttributeIndices ().Append (- 1 )
126
+ tableLen := table .Len ()
127
+ indicesLen := record .AttributeIndices ().Len ()
128
+ // Try putting a new attribute, make sure it fails, and that table/indices didn't change.
129
+ require .Error (t , PutAttribute (table , record , "newKey" , pcommon .NewValueStr ("value" )))
130
+ require .Equal (t , tableLen , table .Len ())
131
+ require .Equal (t , indicesLen , record .AttributeIndices ().Len ())
132
+
133
+ // Set the last index to the table length, which is out of range.
134
+ record .AttributeIndices ().SetAt (indicesLen - 1 , int32 (tableLen )) //nolint:gosec
135
+ // Try putting a new attribute, make sure it fails, and that table/indices didn't change.
136
+ require .Error (t , PutAttribute (table , record , "newKey" , pcommon .NewValueStr ("value" )))
137
+ require .Equal (t , tableLen , table .Len ())
138
+ require .Equal (t , indicesLen , record .AttributeIndices ().Len ())
139
+ }
72
140
73
- assert .Equal (t , 2 , table .Len ())
74
- assert .Equal (t , []int32 {0 }, mapp .AttributeIndices ().AsRaw ())
141
+ func TestPutAttribute (t * testing.T ) {
142
+ // Test every existing record type.
143
+ for _ , tt := range []struct {
144
+ name string
145
+ attr attributable
146
+ }{
147
+ {"Profile" , NewProfile ()},
148
+ {"Sample" , NewSample ()},
149
+ {"Mapping" , NewMapping ()},
150
+ {"Location" , NewLocation ()},
151
+ } {
152
+ t .Run (tt .name , func (t * testing.T ) {
153
+ testPutAttribute (t , tt .attr )
154
+ })
155
+ }
75
156
}
76
157
77
158
func BenchmarkFromAttributeIndices (b * testing.B ) {
@@ -94,7 +175,7 @@ func BenchmarkFromAttributeIndices(b *testing.B) {
94
175
}
95
176
}
96
177
97
- func BenchmarkAddAttribute (b * testing.B ) {
178
+ func BenchmarkPutAttribute (b * testing.B ) {
98
179
for _ , bb := range []struct {
99
180
name string
100
181
key string
@@ -124,7 +205,7 @@ func BenchmarkAddAttribute(b *testing.B) {
124
205
value : pcommon .NewValueStr ("test" ),
125
206
126
207
runBefore : func (_ * testing.B , table AttributeTableSlice , obj attributable ) {
127
- require .NoError (b , AddAttribute (table , obj , "attribute" , pcommon .NewValueStr ("test" )))
208
+ require .NoError (b , PutAttribute (table , obj , "attribute" , pcommon .NewValueStr ("test" )))
128
209
},
129
210
},
130
211
{
@@ -157,7 +238,7 @@ func BenchmarkAddAttribute(b *testing.B) {
157
238
b .ReportAllocs ()
158
239
159
240
for n := 0 ; n < b .N ; n ++ {
160
- _ = AddAttribute (table , obj , bb .key , bb .value )
241
+ _ = PutAttribute (table , obj , bb .key , bb .value )
161
242
}
162
243
})
163
244
}
0 commit comments