Skip to content

Commit 8de90e7

Browse files
authored
Add FromAttributeIndices helper to pprofile (#12176)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Profiles attributes are stored in a single `AttributeTable` attribute, and then referenced in each substruct as `AttributeIndices`. This means to read them as a `pcommon.Map`, a bit of pre-processing is required. This adds an helper method so each component manipulating profiles doesn't have to reimplement it. Benchmark: ``` go test -bench=BenchmarkFromAttributeIndices ./... goos: darwin goarch: arm64 pkg: go.opentelemetry.io/collector/pdata/pprofile cpu: Apple M1 Max BenchmarkFromAttributeIndices-10 9789714 123.2 ns/op 140 B/op 4 allocs/op PASS ok go.opentelemetry.io/collector/pdata/pprofile 1.952s PASS ok go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp 0.407s ```
1 parent 50b76b9 commit 8de90e7

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata/pprofile
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add new helper method `FromAttributeIndices` to build a `pcommon.Map` out of `AttributeIndices`.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12176]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

pdata/pprofile/attributes.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
5+
6+
import (
7+
"go.opentelemetry.io/collector/pdata/pcommon"
8+
)
9+
10+
type attributable interface {
11+
AttributeIndices() pcommon.Int32Slice
12+
}
13+
14+
// FromAttributeIndices builds a [pcommon.Map] containing the attributes of a
15+
// record.
16+
// The record can by any struct that implements an `AttributeIndices` method.
17+
// Updates made to the return map will not be applied back to the record.
18+
func FromAttributeIndices(table AttributeTableSlice, record attributable) pcommon.Map {
19+
m := pcommon.NewMap()
20+
m.EnsureCapacity(record.AttributeIndices().Len())
21+
22+
for i := 0; i < record.AttributeIndices().Len(); i++ {
23+
kv := table.At(int(record.AttributeIndices().At(i)))
24+
kv.Value().CopyTo(m.PutEmpty(kv.Key()))
25+
}
26+
27+
return m
28+
}

pdata/pprofile/attributes_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"go.opentelemetry.io/collector/pdata/pcommon"
13+
)
14+
15+
func TestFromAttributeIndices(t *testing.T) {
16+
table := NewAttributeTableSlice()
17+
att := table.AppendEmpty()
18+
att.SetKey("hello")
19+
att.Value().SetStr("world")
20+
att2 := table.AppendEmpty()
21+
att2.SetKey("bonjour")
22+
att2.Value().SetStr("monde")
23+
24+
attrs := FromAttributeIndices(table, NewProfile())
25+
assert.Equal(t, attrs, pcommon.NewMap())
26+
27+
// A Location with a single attribute
28+
loc := NewLocation()
29+
loc.AttributeIndices().Append(0)
30+
31+
attrs = FromAttributeIndices(table, loc)
32+
33+
m := map[string]any{"hello": "world"}
34+
assert.Equal(t, attrs.AsRaw(), m)
35+
36+
// A Mapping with two attributes
37+
mapp := NewLocation()
38+
mapp.AttributeIndices().Append(0, 1)
39+
40+
attrs = FromAttributeIndices(table, mapp)
41+
42+
m = map[string]any{"hello": "world", "bonjour": "monde"}
43+
assert.Equal(t, attrs.AsRaw(), m)
44+
}
45+
46+
func BenchmarkFromAttributeIndices(b *testing.B) {
47+
table := NewAttributeTableSlice()
48+
49+
for i := range 10 {
50+
att := table.AppendEmpty()
51+
att.SetKey(fmt.Sprintf("key_%d", i))
52+
att.Value().SetStr(fmt.Sprintf("value_%d", i))
53+
}
54+
55+
obj := NewLocation()
56+
obj.AttributeIndices().Append(1, 3, 7)
57+
58+
b.ResetTimer()
59+
b.ReportAllocs()
60+
61+
for n := 0; n < b.N; n++ {
62+
_ = FromAttributeIndices(table, obj)
63+
}
64+
}

0 commit comments

Comments
 (0)