-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Introduce FromLocationIndices and PutLocation #13150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmathieu
wants to merge
18
commits into
open-telemetry:main
Choose a base branch
from
dmathieu:locations-read-write-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+513
−3
Open
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
63c1df7
introduce FromLocationIndices and PutLocation
dmathieu 45f395d
add changelog entry
dmathieu 92b2d97
this loop isn't needed in the context of locations
dmathieu 1215448
Update pdata/pprofile/locations.go
dmathieu 7da61c8
Update pdata/pprofile/locations_test.go
dmathieu f426fa8
Update pdata/pprofile/locations_test.go
dmathieu 627e4ed
add buildLine helper
dmathieu 12b54c0
setup buildLocation helper
dmathieu fd0fc28
use .All()
dmathieu adf2b2a
revert using b.Loop
dmathieu d16f3b2
Update pdata/pprofile/locations.go
dmathieu 5cb35c7
Update pdata/pprofile/locations.go
dmathieu e29fade
Update pdata/pprofile/locations.go
dmathieu 9b9e555
Update pdata/pprofile/locations_test.go
dmathieu d1f29ec
Update pdata/pprofile/locations_test.go
dmathieu fdaff28
Update pdata/pprofile/locations.go
dmathieu 9a7335f
locations are only ever on Profiles. Remove interface
dmathieu 7b19314
fix getting locIdx
dmathieu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: Add new helper methods `FromLocationIndices` and `PutLocation` to read and modify the content of locations. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [13150] | ||
|
||
# (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: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" | ||
|
||
// Equal checks equality with another LineSlice | ||
func (l LineSlice) Equal(val LineSlice) bool { | ||
if l.Len() != val.Len() { | ||
return false | ||
} | ||
|
||
for i := range l.Len() { | ||
if !l.At(i).Equal(val.At(i)) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Equal checks equality with another Line | ||
func (l Line) Equal(val Line) bool { | ||
return l.Column() == val.Column() && | ||
l.FunctionIndex() == val.FunctionIndex() && | ||
l.Line() == val.Line() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pprofile | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLineSliceEqual(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
orig LineSlice | ||
dest LineSlice | ||
want bool | ||
}{ | ||
{ | ||
name: "with empty slices", | ||
orig: NewLineSlice(), | ||
dest: NewLineSlice(), | ||
want: true, | ||
}, | ||
{ | ||
name: "with non-empty equal slices", | ||
orig: func() LineSlice { | ||
ls := NewLineSlice() | ||
ls.AppendEmpty().SetLine(1) | ||
return ls | ||
}(), | ||
dest: func() LineSlice { | ||
ls := NewLineSlice() | ||
ls.AppendEmpty().SetLine(1) | ||
return ls | ||
}(), | ||
want: true, | ||
}, | ||
{ | ||
name: "with different lengths", | ||
orig: func() LineSlice { | ||
ls := NewLineSlice() | ||
ls.AppendEmpty() | ||
return ls | ||
}(), | ||
dest: NewLineSlice(), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal slices", | ||
orig: func() LineSlice { | ||
ls := NewLineSlice() | ||
ls.AppendEmpty().SetLine(2) | ||
return ls | ||
}(), | ||
dest: func() LineSlice { | ||
ls := NewLineSlice() | ||
ls.AppendEmpty().SetLine(1) | ||
return ls | ||
}(), | ||
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)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLineEqual(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
orig Line | ||
dest Line | ||
want bool | ||
}{ | ||
{ | ||
name: "with empty lines", | ||
orig: NewLine(), | ||
dest: NewLine(), | ||
want: true, | ||
}, | ||
{ | ||
name: "with non-empty lines", | ||
orig: buildLine(1, 2, 3), | ||
dest: buildLine(1, 2, 3), | ||
want: true, | ||
}, | ||
{ | ||
name: "with non-equal column", | ||
orig: buildLine(1, 2, 3), | ||
dest: buildLine(2, 2, 3), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal function index", | ||
orig: buildLine(1, 2, 3), | ||
dest: buildLine(1, 3, 3), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal line", | ||
orig: buildLine(1, 2, 3), | ||
dest: buildLine(1, 2, 4), | ||
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)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func buildLine(col int64, funcIdx int32, line int64) Line { | ||
l := NewLine() | ||
l.SetColumn(col) | ||
l.SetFunctionIndex(funcIdx) | ||
l.SetLine(line) | ||
|
||
return l | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" | ||
|
||
// Equal checks equality with another Location | ||
func (l Location) Equal(val Location) bool { | ||
return l.MappingIndex() == val.MappingIndex() && | ||
l.Address() == val.Address() && | ||
l.AttributeIndices().Equal(val.AttributeIndices()) && | ||
l.IsFolded() == val.IsFolded() && | ||
l.Line().Equal(val.Line()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pprofile | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLocationEqual(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
orig Location | ||
dest Location | ||
want bool | ||
}{ | ||
{ | ||
name: "empty locations", | ||
orig: NewLocation(), | ||
dest: NewLocation(), | ||
want: true, | ||
}, | ||
{ | ||
name: "non-empty locations", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
dest: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
want: true, | ||
}, | ||
{ | ||
name: "with non-equal mapping index", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
dest: buildLocation(2, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal address", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
dest: buildLocation(1, 3, []int32{3}, true, buildLine(1, 2, 3)), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal attribute indices", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
dest: buildLocation(1, 2, []int32{5}, true, buildLine(1, 2, 3)), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal is folded", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
dest: buildLocation(1, 2, []int32{3}, false, buildLine(1, 2, 3)), | ||
want: false, | ||
}, | ||
{ | ||
name: "with non-equal lines", | ||
orig: buildLocation(1, 2, []int32{3}, true, buildLine(4, 5, 6)), | ||
dest: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), | ||
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)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func buildLocation(mapIdx int32, addr uint64, attrIdxs []int32, isFolded bool, line Line) Location { | ||
l := NewLocation() | ||
l.SetMappingIndex(mapIdx) | ||
l.SetAddress(addr) | ||
l.AttributeIndices().FromRaw(attrIdxs) | ||
l.SetIsFolded(isFolded) | ||
line.MoveTo(l.Line().AppendEmpty()) | ||
return l | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
type locatable interface { | ||
LocationIndices() pcommon.Int32Slice | ||
} | ||
|
||
// FromLocationIndices builds a slice containing all the locations of a record. | ||
// The record can be any struct that implements a `LocationIndices` method. | ||
// Updates made to the return map will not be applied back to the record. | ||
func FromLocationIndices(table LocationSlice, record locatable) LocationSlice { | ||
m := NewLocationSlice() | ||
m.EnsureCapacity(record.LocationIndices().Len()) | ||
|
||
for i := range record.LocationIndices().All() { | ||
l := table.At(int(record.LocationIndices().At(i))) | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
l.CopyTo(m.AppendEmpty()) | ||
} | ||
|
||
return m | ||
} | ||
|
||
var errTooManyLocationTableEntries = errors.New("too many entries in LocationTable") | ||
|
||
// PutLocation updates a LocationTable and a record's LocationIndices to | ||
// add or update a location. | ||
// The record can be any struct that implements a `LocationIndices` method. | ||
func PutLocation(table LocationSlice, record locatable, loc Location) error { | ||
for i := range record.LocationIndices().All() { | ||
idx := int(record.LocationIndices().At(i)) | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if idx < 0 || idx >= table.Len() { | ||
return fmt.Errorf("index value %d out of range in LocationIndices[%d]", idx, i) | ||
} | ||
attr := table.At(idx) | ||
if attr.Equal(loc) { | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Location already exists, nothing to do. | ||
return nil | ||
} | ||
} | ||
|
||
if record.LocationIndices().Len() >= math.MaxInt32 { | ||
return errors.New("too many entries in LocationIndices") | ||
} | ||
|
||
for j, a := range table.All() { | ||
if a.Equal(loc) { | ||
if j > math.MaxInt32 { | ||
return errTooManyLocationTableEntries | ||
} | ||
// Add the index of the existing location to the indices. | ||
record.LocationIndices().Append(int32(j)) //nolint:gosec // overflow checked | ||
return nil | ||
} | ||
} | ||
|
||
if table.Len() >= math.MaxInt32 { | ||
return errTooManyLocationTableEntries | ||
} | ||
|
||
loc.CopyTo(table.AppendEmpty()) | ||
record.LocationIndices().Append(int32(table.Len() - 1)) //nolint:gosec // overflow checked | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.