Skip to content

Commit 87e99d0

Browse files
authored
introduce the Equal method on Stack (#13952)
This introduces `Stack.Equal()`, to help with profiles merging. See #13928
1 parent ba0f599 commit 87e99d0

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

.chloggen/stack-equal.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: 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: Introduce `Equal` method on the `Stack` type
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13952]
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/stack.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// Equal checks equality with another Stack
7+
func (ms Stack) Equal(val Stack) bool {
8+
if ms.LocationIndices().Len() != val.LocationIndices().Len() {
9+
return false
10+
}
11+
12+
for i := range ms.LocationIndices().Len() {
13+
if ms.LocationIndices().At(i) != val.LocationIndices().At(i) {
14+
return false
15+
}
16+
}
17+
18+
return true
19+
}

pdata/pprofile/stack_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestStackEqual(t *testing.T) {
13+
for _, tt := range []struct {
14+
name string
15+
orig Stack
16+
dest Stack
17+
want bool
18+
}{
19+
{
20+
name: "with empty stacks",
21+
orig: NewStack(),
22+
dest: NewStack(),
23+
want: true,
24+
},
25+
{
26+
name: "with non-empty equal stacks",
27+
orig: func() Stack {
28+
s := NewStack()
29+
s.LocationIndices().Append(1)
30+
return s
31+
}(),
32+
dest: func() Stack {
33+
s := NewStack()
34+
s.LocationIndices().Append(1)
35+
return s
36+
}(),
37+
want: true,
38+
},
39+
{
40+
name: "with different location indices lengths",
41+
orig: func() Stack {
42+
s := NewStack()
43+
s.LocationIndices().Append(1)
44+
return s
45+
}(),
46+
dest: NewStack(),
47+
want: false,
48+
},
49+
{
50+
name: "with non-equal location indices",
51+
orig: func() Stack {
52+
s := NewStack()
53+
s.LocationIndices().Append(2)
54+
return s
55+
}(),
56+
dest: func() Stack {
57+
s := NewStack()
58+
s.LocationIndices().Append(1)
59+
return s
60+
}(),
61+
want: false,
62+
},
63+
} {
64+
t.Run(tt.name, func(t *testing.T) {
65+
if tt.want {
66+
assert.True(t, tt.orig.Equal(tt.dest))
67+
} else {
68+
assert.False(t, tt.orig.Equal(tt.dest))
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)