Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/pprofile-set-stack.yaml
Original file line number Diff line number Diff line change
@@ -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 `SetStack` method

# One or more tracking issues or pull requests related to the change
issues: [14007]

# (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]
31 changes: 31 additions & 0 deletions pdata/pprofile/stacks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

import (
"errors"
"math"
)

var errTooManyStackTableEntries = errors.New("too many entries in StackTable")

// SetStack updates a StackTable, adding or providing a stack and returns its
// index.
func SetStack(table StackSlice, st Stack) (int32, error) {
for j, l := range table.All() {
if l.Equal(st) {
if j > math.MaxInt32 {
return 0, errTooManyStackTableEntries
}
return int32(j), nil //nolint:gosec // G115 overflow checked
}
}

if table.Len() >= math.MaxInt32 {
return 0, errTooManyStackTableEntries
}

st.CopyTo(table.AppendEmpty())
return int32(table.Len() - 1), nil //nolint:gosec // G115 overflow checked
}
115 changes: 115 additions & 0 deletions pdata/pprofile/stacks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSetStack(t *testing.T) {
table := NewStackSlice()
s := NewStack()
s.LocationIndices().Append(1)
s2 := NewStack()
s.LocationIndices().Append(2)

// Put a first stack
idx, err := SetStack(table, s)
require.NoError(t, err)
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), idx)

// Put the same stack
// This should be a no-op.
idx, err = SetStack(table, s)
require.NoError(t, err)
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), idx)

// Set a new stack
// This sets the index and adds to the table.
idx, err = SetStack(table, s2)
require.NoError(t, err)
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115

// Set an existing stack
idx, err = SetStack(table, s)
require.NoError(t, err)
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(0), idx)
// Set another existing stack
idx, err = SetStack(table, s2)
require.NoError(t, err)
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
}

func BenchmarkSetStack(b *testing.B) {
for _, bb := range []struct {
name string
stack Stack

runBefore func(*testing.B, StackSlice)
}{
{
name: "with a new stack",
stack: NewStack(),
},
{
name: "with an existing stack",
stack: func() Stack {
s := NewStack()
s.LocationIndices().Append(1)
return s
}(),

runBefore: func(_ *testing.B, table StackSlice) {
s := table.AppendEmpty()
s.LocationIndices().Append(1)
},
},
{
name: "with a duplicate stack",
stack: NewStack(),

runBefore: func(_ *testing.B, table StackSlice) {
_, err := SetStack(table, NewStack())
require.NoError(b, err)
},
},
{
name: "with a hundred stacks to loop through",
stack: func() Stack {
s := NewStack()
s.LocationIndices().Append(1)
return s
}(),

runBefore: func(_ *testing.B, table StackSlice) {
for range 100 {
table.AppendEmpty()
}
},
},
} {
b.Run(bb.name, func(b *testing.B) {
table := NewStackSlice()

if bb.runBefore != nil {
bb.runBefore(b, table)
}

b.ResetTimer()
b.ReportAllocs()

for b.Loop() {
_, _ = SetStack(table, bb.stack)
}
})
}
}