Skip to content

Commit c79745f

Browse files
authored
Change SetLink signature to return the link's ID, not update (#14031)
Part of #14016
1 parent 0e6e67d commit c79745f

File tree

3 files changed

+55
-53
lines changed

3 files changed

+55
-53
lines changed

.chloggen/setlink-immutable.yaml

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: breaking
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: Update `SetLink` to return the link's ID rather than update the Sample
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14016, 14031]
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/links.go

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

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

1211
var errTooManyLinkTableEntries = errors.New("too many entries in LinkTable")
1312

14-
// SetLink updates a LinkTable and a Sample's LinkIndex to
15-
// add or update a link.
16-
func SetLink(table LinkSlice, record Sample, li Link) error {
17-
idx := int(record.LinkIndex())
18-
if idx > 0 {
19-
if idx >= table.Len() {
20-
return fmt.Errorf("index value %d out of range for LinkIndex", idx)
21-
}
22-
mapAt := table.At(idx)
23-
if mapAt.Equal(li) {
24-
// Link already exists, nothing to do.
25-
return nil
26-
}
27-
}
28-
13+
// SetLink updates a LinkTable, adding or providing a value and returns its
14+
// index.
15+
func SetLink(table LinkSlice, li Link) (int32, error) {
2916
for j, l := range table.All() {
3017
if l.Equal(li) {
3118
if j > math.MaxInt32 {
32-
return errTooManyLinkTableEntries
19+
return 0, errTooManyLinkTableEntries
3320
}
34-
// Add the index of the existing link to the indices.
35-
record.SetLinkIndex(int32(j)) //nolint:gosec // G115 overflow checked
36-
return nil
21+
return int32(j), nil //nolint:gosec // G115 overflow checked
3722
}
3823
}
3924

4025
if table.Len() >= math.MaxInt32 {
41-
return errTooManyLinkTableEntries
26+
return 0, errTooManyLinkTableEntries
4227
}
4328

4429
li.CopyTo(table.AppendEmpty())
45-
record.SetLinkIndex(int32(table.Len() - 1)) //nolint:gosec // G115 overflow checked
46-
return nil
30+
return int32(table.Len() - 1), nil //nolint:gosec // G115 overflow checked
4731
}

pdata/pprofile/links_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,45 @@ func TestSetLink(t *testing.T) {
1818
l.SetTraceID(pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))
1919
l2 := NewLink()
2020
l.SetTraceID(pcommon.TraceID([16]byte{2, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 2}))
21-
smpl := NewSample()
2221

2322
// Put a first link
24-
require.NoError(t, SetLink(table, smpl, l))
23+
idx, err := SetLink(table, l)
24+
require.NoError(t, err)
2525
assert.Equal(t, 1, table.Len())
26-
assert.Equal(t, int32(0), smpl.LinkIndex())
26+
assert.Equal(t, int32(0), idx)
2727

2828
// Put the same link
2929
// This should be a no-op.
30-
require.NoError(t, SetLink(table, smpl, l))
30+
idx, err = SetLink(table, l)
31+
require.NoError(t, err)
3132
assert.Equal(t, 1, table.Len())
32-
assert.Equal(t, int32(0), smpl.LinkIndex())
33+
assert.Equal(t, int32(0), idx)
3334

3435
// Set a new link
3536
// This sets the index and adds to the table.
36-
require.NoError(t, SetLink(table, smpl, l2))
37+
idx, err = SetLink(table, l2)
38+
require.NoError(t, err)
3739
assert.Equal(t, 2, table.Len())
38-
assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115
40+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
3941

4042
// Set an existing link
41-
require.NoError(t, SetLink(table, smpl, l))
43+
idx, err = SetLink(table, l)
44+
require.NoError(t, err)
4245
assert.Equal(t, 2, table.Len())
43-
assert.Equal(t, int32(0), smpl.LinkIndex())
46+
assert.Equal(t, int32(0), idx)
4447
// Set another existing link
45-
require.NoError(t, SetLink(table, smpl, l2))
48+
idx, err = SetLink(table, l2)
49+
require.NoError(t, err)
4650
assert.Equal(t, 2, table.Len())
47-
assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115
48-
}
49-
50-
func TestSetLinkCurrentTooHigh(t *testing.T) {
51-
table := NewLinkSlice()
52-
smpl := NewSample()
53-
smpl.SetLinkIndex(42)
54-
55-
err := SetLink(table, smpl, NewLink())
56-
require.Error(t, err)
57-
assert.Equal(t, 0, table.Len())
58-
assert.Equal(t, int32(42), smpl.LinkIndex())
51+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
5952
}
6053

6154
func BenchmarkSetLink(b *testing.B) {
6255
for _, bb := range []struct {
6356
name string
6457
link Link
6558

66-
runBefore func(*testing.B, LinkSlice, Sample)
59+
runBefore func(*testing.B, LinkSlice)
6760
}{
6861
{
6962
name: "with a new link",
@@ -77,7 +70,7 @@ func BenchmarkSetLink(b *testing.B) {
7770
return l
7871
}(),
7972

80-
runBefore: func(_ *testing.B, table LinkSlice, _ Sample) {
73+
runBefore: func(_ *testing.B, table LinkSlice) {
8174
l := table.AppendEmpty()
8275
l.SetTraceID(pcommon.NewTraceIDEmpty())
8376
},
@@ -86,8 +79,9 @@ func BenchmarkSetLink(b *testing.B) {
8679
name: "with a duplicate link",
8780
link: NewLink(),
8881

89-
runBefore: func(_ *testing.B, table LinkSlice, obj Sample) {
90-
require.NoError(b, SetLink(table, obj, NewLink()))
82+
runBefore: func(b *testing.B, table LinkSlice) {
83+
_, err := SetLink(table, NewLink())
84+
require.NoError(b, err)
9185
},
9286
},
9387
{
@@ -98,7 +92,7 @@ func BenchmarkSetLink(b *testing.B) {
9892
return l
9993
}(),
10094

101-
runBefore: func(_ *testing.B, table LinkSlice, _ Sample) {
95+
runBefore: func(_ *testing.B, table LinkSlice) {
10296
for range 100 {
10397
table.AppendEmpty()
10498
}
@@ -107,17 +101,16 @@ func BenchmarkSetLink(b *testing.B) {
107101
} {
108102
b.Run(bb.name, func(b *testing.B) {
109103
table := NewLinkSlice()
110-
obj := NewSample()
111104

112105
if bb.runBefore != nil {
113-
bb.runBefore(b, table, obj)
106+
bb.runBefore(b, table)
114107
}
115108

116109
b.ResetTimer()
117110
b.ReportAllocs()
118111

119112
for b.Loop() {
120-
_ = SetLink(table, obj, bb.link)
113+
_, _ = SetLink(table, bb.link)
121114
}
122115
})
123116
}

0 commit comments

Comments
 (0)