Skip to content

Commit 35f5bfd

Browse files
committed
more review changes
1 parent e0a0548 commit 35f5bfd

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

pdata/pprofile/attributes.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,16 @@ type attributable interface {
1111
AttributeIndices() pcommon.Int32Slice
1212
}
1313

14-
// BuildAttributes builds a [pcommon.Map] containing the attributes of a record.
14+
// FromAttributeIndices builds a [pcommon.Map] containing the attributes of a
15+
// record.
1516
// The record can by any struct that implements an `AttributeIndices` method.
16-
func BuildAttributes(profile Profile, record attributable) pcommon.Map {
17+
func FromAttributeIndices(profile Profile, record attributable) pcommon.Map {
1718
m := pcommon.NewMap()
19+
m.EnsureCapacity(record.AttributeIndices().Len())
1820

1921
for i := 0; i < record.AttributeIndices().Len(); i++ {
20-
id := int(record.AttributeIndices().At(i))
21-
a := profile.AttributeTable().At(id)
22-
23-
key := a.Key()
24-
val := a.Value()
25-
26-
switch val.Type() {
27-
case pcommon.ValueTypeEmpty:
28-
m.PutStr(key, "")
29-
case pcommon.ValueTypeStr:
30-
m.PutStr(key, val.AsString())
31-
case pcommon.ValueTypeBool:
32-
m.PutBool(key, val.Bool())
33-
case pcommon.ValueTypeDouble:
34-
m.PutDouble(key, val.Double())
35-
case pcommon.ValueTypeInt:
36-
m.PutInt(key, val.Int())
37-
case pcommon.ValueTypeBytes:
38-
bs := m.PutEmptyBytes(key)
39-
val.Bytes().CopyTo(bs)
40-
case pcommon.ValueTypeMap:
41-
ma := m.PutEmptyMap(key)
42-
val.Map().CopyTo(ma)
43-
case pcommon.ValueTypeSlice:
44-
sl := m.PutEmptySlice(key)
45-
val.Slice().CopyTo(sl)
46-
}
22+
kv := profile.AttributeTable().At(int(record.AttributeIndices().At(i)))
23+
kv.Value().CopyTo(m.PutEmpty(kv.Key()))
4724
}
4825

4926
return m

pdata/pprofile/attributes_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/require"
1211

1312
"go.opentelemetry.io/collector/pdata/pcommon"
1413
)
1514

16-
func TestBuildAttributes(t *testing.T) {
15+
func TestFromAttributeIndices(t *testing.T) {
1716
profile := NewProfile()
1817
att := profile.AttributeTable().AppendEmpty()
1918
att.SetKey("hello")
@@ -22,31 +21,29 @@ func TestBuildAttributes(t *testing.T) {
2221
att2.SetKey("bonjour")
2322
att2.Value().SetStr("monde")
2423

25-
attrs := BuildAttributes(profile, profile)
24+
attrs := FromAttributeIndices(profile, profile)
2625
assert.Equal(t, attrs, pcommon.NewMap())
2726

2827
// A Location with a single attribute
2928
loc := NewLocation()
3029
loc.AttributeIndices().Append(0)
3130

32-
attrs = BuildAttributes(profile, loc)
31+
attrs = FromAttributeIndices(profile, loc)
3332

34-
m := pcommon.NewMap()
35-
require.NoError(t, m.FromRaw(map[string]any{"hello": "world"}))
36-
assert.Equal(t, attrs, m)
33+
m := map[string]any{"hello": "world"}
34+
assert.Equal(t, attrs.AsRaw(), m)
3735

3836
// A Mapping with two attributes
3937
mapp := NewLocation()
4038
mapp.AttributeIndices().Append(0, 1)
4139

42-
attrs = BuildAttributes(profile, mapp)
40+
attrs = FromAttributeIndices(profile, mapp)
4341

44-
m = pcommon.NewMap()
45-
require.NoError(t, m.FromRaw(map[string]any{"hello": "world", "bonjour": "monde"}))
46-
assert.Equal(t, attrs, m)
42+
m = map[string]any{"hello": "world", "bonjour": "monde"}
43+
assert.Equal(t, attrs.AsRaw(), m)
4744
}
4845

49-
func BenchmarkBuildAttributes(b *testing.B) {
46+
func BenchmarkFromAttributeIndices(b *testing.B) {
5047
profile := NewProfile()
5148

5249
for i := range 10 {
@@ -62,6 +59,6 @@ func BenchmarkBuildAttributes(b *testing.B) {
6259
b.ReportAllocs()
6360

6461
for n := 0; n < b.N; n++ {
65-
_ = BuildAttributes(profile, obj)
62+
_ = FromAttributeIndices(profile, obj)
6663
}
6764
}

0 commit comments

Comments
 (0)