diff --git a/.chloggen/stack-equal.yaml b/.chloggen/stack-equal.yaml new file mode 100644 index 00000000000..30104b00b29 --- /dev/null +++ b/.chloggen/stack-equal.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: pdata/pprofile + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Introduce `Equal` method on the `Stack` type + +# One or more tracking issues or pull requests related to the change +issues: [13952] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/pdata/pprofile/stack.go b/pdata/pprofile/stack.go new file mode 100644 index 00000000000..3629b0ee995 --- /dev/null +++ b/pdata/pprofile/stack.go @@ -0,0 +1,19 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" + +// Equal checks equality with another Stack +func (ms Stack) Equal(val Stack) bool { + if ms.LocationIndices().Len() != val.LocationIndices().Len() { + return false + } + + for i := range ms.LocationIndices().Len() { + if ms.LocationIndices().At(i) != val.LocationIndices().At(i) { + return false + } + } + + return true +} diff --git a/pdata/pprofile/stack_test.go b/pdata/pprofile/stack_test.go new file mode 100644 index 00000000000..887638b8cdc --- /dev/null +++ b/pdata/pprofile/stack_test.go @@ -0,0 +1,72 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStackEqual(t *testing.T) { + for _, tt := range []struct { + name string + orig Stack + dest Stack + want bool + }{ + { + name: "with empty stacks", + orig: NewStack(), + dest: NewStack(), + want: true, + }, + { + name: "with non-empty equal stacks", + orig: func() Stack { + s := NewStack() + s.LocationIndices().Append(1) + return s + }(), + dest: func() Stack { + s := NewStack() + s.LocationIndices().Append(1) + return s + }(), + want: true, + }, + { + name: "with different location indices lengths", + orig: func() Stack { + s := NewStack() + s.LocationIndices().Append(1) + return s + }(), + dest: NewStack(), + want: false, + }, + { + name: "with non-equal location indices", + orig: func() Stack { + s := NewStack() + s.LocationIndices().Append(2) + return s + }(), + dest: func() Stack { + s := NewStack() + s.LocationIndices().Append(1) + return s + }(), + want: false, + }, + } { + t.Run(tt.name, func(t *testing.T) { + if tt.want { + assert.True(t, tt.orig.Equal(tt.dest)) + } else { + assert.False(t, tt.orig.Equal(tt.dest)) + } + }) + } +}