Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions .chloggen/fix-pprofile-mergeto-dictionary-zero-value.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
component: pkg/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "`Profiles.MergeTo` no longer overwrites index 0 of an empty destination dictionary table with real data"

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

# (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: |
`MergeTo` did not reserve index 0 of the destination dictionary's tables for their zero value before
remapping entries into them. When the destination dictionary was empty, the first entry merged in would
land at index 0, the slot every unset reference (e.g. an unset Strindex) resolves to, producing a
dictionary that violates the `ProfilesDictionary` protocol. `MergeTo` now seeds index 0 of any table that
is still empty with that table's zero value before remapping.

# 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]
33 changes: 33 additions & 0 deletions pdata/pprofile/profiles_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func (ms Profiles) MergeTo(dest Profiles) error {
return nil
}

reserveDictionaryZeroValues(dest.Dictionary())

if err := ms.switchDictionary(ms.Dictionary(), dest.Dictionary()); err != nil {
return err
}
Expand All @@ -22,3 +24,34 @@ func (ms Profiles) MergeTo(dest Profiles) error {

return nil
}

// reserveDictionaryZeroValues seeds index 0 of each of dst's tables with that
// table's zero value, but only for tables that are still empty. Per the
// ProfilesDictionary contract, index 0 of every table MUST hold the zero
// value for that table's element type, since unset references (e.g. an
// unset Strindex) resolve to it. Tables that already hold entries are left
// untouched, since inserting at index 0 would invalidate every existing
// reference into them.
func reserveDictionaryZeroValues(dst ProfilesDictionary) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this functionality into dictionary_helpers.go and maybe rename it to something like ensureDictionarySentinels()?

if dst.StringTable().Len() == 0 {
dst.StringTable().Append("")
}
if dst.AttributeTable().Len() == 0 {
dst.AttributeTable().AppendEmpty()
}
if dst.FunctionTable().Len() == 0 {
dst.FunctionTable().AppendEmpty()
}
if dst.LinkTable().Len() == 0 {
dst.LinkTable().AppendEmpty()
}
if dst.LocationTable().Len() == 0 {
dst.LocationTable().AppendEmpty()
}
if dst.MappingTable().Len() == 0 {
dst.MappingTable().AppendEmpty()
}
if dst.StackTable().Len() == 0 {
dst.StackTable().AppendEmpty()
}
}
53 changes: 53 additions & 0 deletions pdata/pprofile/profiles_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,59 @@ func TestProfilesMergeTo(t *testing.T) {
}
}

// TestProfilesMergeTo_ReservesDictionaryZeroValues verifies that MergeTo
// reserves index 0 of every destination table for that table's zero value,
// even when the destination dictionary starts out completely empty (not
// pre-populated by the caller). See
// https://github.com/open-telemetry/opentelemetry-collector/issues/15661.
func TestProfilesMergeTo_ReservesDictionaryZeroValues(t *testing.T) {
src := NewProfiles()
dic := src.Dictionary()
dic.StringTable().Append("") // conformant source
dic.AttributeTable().AppendEmpty()
dic.StackTable().AppendEmpty()
dic.LocationTable().AppendEmpty()
dic.FunctionTable().AppendEmpty()
dic.MappingTable().AppendEmpty()
dic.LinkTable().AppendEmpty()
dic.StringTable().Append("inuse_space") // index 1

prof := src.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
prof.SampleType().SetTypeStrindex(1)

dest := NewProfiles()
require.NoError(t, src.MergeTo(dest))

assert.Equal(t, "", dest.Dictionary().StringTable().At(0))
assert.Equal(t, "inuse_space", dest.Dictionary().StringTable().At(1))

require.Equal(t, 1, dest.Dictionary().AttributeTable().Len())
attr := dest.Dictionary().AttributeTable().At(0)
assert.Zero(t, attr.KeyStrindex())
assert.Zero(t, attr.UnitStrindex())
assert.Equal(t, pcommon.ValueTypeEmpty, attr.Value().Type())

require.Equal(t, 1, dest.Dictionary().StackTable().Len())
assert.Zero(t, dest.Dictionary().StackTable().At(0).LocationIndices().Len())

require.Equal(t, 1, dest.Dictionary().LocationTable().Len())
loc := dest.Dictionary().LocationTable().At(0)
assert.Zero(t, loc.MappingIndex())
assert.Zero(t, loc.Address())
assert.Zero(t, loc.Lines().Len())

require.Equal(t, 1, dest.Dictionary().FunctionTable().Len())
fn := dest.Dictionary().FunctionTable().At(0)
assert.Zero(t, fn.NameStrindex())
assert.Zero(t, fn.SystemNameStrindex())
assert.Zero(t, fn.FilenameStrindex())

require.Equal(t, 1, dest.Dictionary().MappingTable().Len())
assert.Zero(t, dest.Dictionary().MappingTable().At(0).FilenameStrindex())

require.Equal(t, 1, dest.Dictionary().LinkTable().Len())
}

func TestProfilesMergeToSelf(t *testing.T) {
profiles := NewProfiles()
profiles.Dictionary().StringTable().Append("", "test")
Expand Down