Skip to content

Commit 94550e8

Browse files
committed
Remove AddAttribute()
1 parent cbac5e8 commit 94550e8

File tree

3 files changed

+1
-137
lines changed

3 files changed

+1
-137
lines changed

.chloggen/profiles-PutAttributes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ change_type: enhancement
77
component: pdata/profile
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: Introduce PutAttribute helper method to modify the content of attributable records.
10+
note: Replace AddAttribute with the PutAttribute helper method to modify the content of attributable records.
1111

1212
# One or more tracking issues or pull requests related to the change
1313
issues: [12798]

pdata/pprofile/attributes.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
55

66
import (
77
"errors"
8-
"fmt"
98
"math"
109

1110
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -31,42 +30,6 @@ func FromAttributeIndices(table AttributeTableSlice, record attributable) pcommo
3130
return m
3231
}
3332

34-
// AddAttribute updates an AttributeTable and a record's AttributeIndices to
35-
// add a new attribute.
36-
// The record can by any struct that implements an `AttributeIndices` method.
37-
func AddAttribute(table AttributeTableSlice, record attributable, key string, value pcommon.Value) error {
38-
for i := range table.Len() {
39-
a := table.At(i)
40-
41-
if a.Key() == key && a.Value().Equal(value) {
42-
if i >= math.MaxInt32 {
43-
return fmt.Errorf("Attribute %s=%#v has too high an index to be added to AttributeIndices", key, value)
44-
}
45-
46-
for j := range record.AttributeIndices().Len() {
47-
v := record.AttributeIndices().At(j)
48-
if v == int32(i) { //nolint:gosec // overflow checked
49-
return nil
50-
}
51-
}
52-
53-
record.AttributeIndices().Append(int32(i)) //nolint:gosec // overflow checked
54-
return nil
55-
}
56-
}
57-
58-
if table.Len() >= math.MaxInt32 {
59-
return errors.New("AttributeTable can't take more attributes")
60-
}
61-
table.EnsureCapacity(table.Len() + 1)
62-
entry := table.AppendEmpty()
63-
entry.SetKey(key)
64-
value.CopyTo(entry.Value())
65-
record.AttributeIndices().Append(int32(table.Len()) - 1) //nolint:gosec // overflow checked
66-
67-
return nil
68-
}
69-
7033
// PutAttribute updates an AttributeTable and a record's AttributeIndices to
7134
// add a new attribute.
7235
// The assumption is that attributes are a map as for other signals (metrics, logs, etc.), thus

pdata/pprofile/attributes_test.go

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,6 @@ func TestFromAttributeIndices(t *testing.T) {
4444
assert.Equal(t, attrs.AsRaw(), m)
4545
}
4646

47-
func TestAddAttribute(t *testing.T) {
48-
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-
58-
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)
65-
66-
assert.Equal(t, 2, table.Len())
67-
assert.Equal(t, []int32{0}, mapp.AttributeIndices().AsRaw())
68-
69-
// Add a duplicate attribute
70-
err = AddAttribute(table, mapp, "hello", pcommon.NewValueStr("world"))
71-
require.NoError(t, err)
72-
73-
assert.Equal(t, 2, table.Len())
74-
assert.Equal(t, []int32{0}, mapp.AttributeIndices().AsRaw())
75-
}
76-
7747
func TestPutAttribute(t *testing.T) {
7848
table := NewAttributeTableSlice()
7949
indices := NewProfile()
@@ -136,75 +106,6 @@ func BenchmarkFromAttributeIndices(b *testing.B) {
136106
}
137107
}
138108

139-
func BenchmarkAddAttribute(b *testing.B) {
140-
for _, bb := range []struct {
141-
name string
142-
key string
143-
value pcommon.Value
144-
145-
runBefore func(*testing.B, AttributeTableSlice, attributable)
146-
}{
147-
{
148-
name: "with a new string attribute",
149-
key: "attribute",
150-
value: pcommon.NewValueStr("test"),
151-
},
152-
{
153-
name: "with an existing attribute",
154-
key: "attribute",
155-
value: pcommon.NewValueStr("test"),
156-
157-
runBefore: func(_ *testing.B, table AttributeTableSlice, _ attributable) {
158-
entry := table.AppendEmpty()
159-
entry.SetKey("attribute")
160-
entry.Value().SetStr("test")
161-
},
162-
},
163-
{
164-
name: "with a duplicate attribute",
165-
key: "attribute",
166-
value: pcommon.NewValueStr("test"),
167-
168-
runBefore: func(_ *testing.B, table AttributeTableSlice, obj attributable) {
169-
require.NoError(b, AddAttribute(table, obj, "attribute", pcommon.NewValueStr("test")))
170-
},
171-
},
172-
{
173-
name: "with a hundred attributes to loop through",
174-
key: "attribute",
175-
value: pcommon.NewValueStr("test"),
176-
177-
runBefore: func(_ *testing.B, table AttributeTableSlice, _ attributable) {
178-
for i := range 100 {
179-
entry := table.AppendEmpty()
180-
entry.SetKey(fmt.Sprintf("attr_%d", i))
181-
entry.Value().SetStr("test")
182-
}
183-
184-
entry := table.AppendEmpty()
185-
entry.SetKey("attribute")
186-
entry.Value().SetStr("test")
187-
},
188-
},
189-
} {
190-
b.Run(bb.name, func(b *testing.B) {
191-
table := NewAttributeTableSlice()
192-
obj := NewLocation()
193-
194-
if bb.runBefore != nil {
195-
bb.runBefore(b, table, obj)
196-
}
197-
198-
b.ResetTimer()
199-
b.ReportAllocs()
200-
201-
for n := 0; n < b.N; n++ {
202-
_ = AddAttribute(table, obj, bb.key, bb.value)
203-
}
204-
})
205-
}
206-
}
207-
208109
func BenchmarkPutAttribute(b *testing.B) {
209110
for _, bb := range []struct {
210111
name string

0 commit comments

Comments
 (0)