diff --git a/.chloggen/14091-add-mergeto-profiles.yaml b/.chloggen/14091-add-mergeto-profiles.yaml new file mode 100644 index 00000000000..194e33830e8 --- /dev/null +++ b/.chloggen/14091-add-mergeto-profiles.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. receiver/otlp) +component: pdata/pprofile + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Introduce `MergeTo` method + +# One or more tracking issues or pull requests related to the change +issues: [14091] + +# (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/profiles_merge.go b/pdata/pprofile/profiles_merge.go new file mode 100644 index 00000000000..851bf739d99 --- /dev/null +++ b/pdata/pprofile/profiles_merge.go @@ -0,0 +1,24 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" + +// MergeTo merges the current Profiles into dest, updating the destination +// dictionary as needed and appending the resource profiles. +// The source Profiles is consumed and marked read-only after this operation. +func (ms Profiles) MergeTo(dest Profiles) error { + ms.getState().AssertMutable() + dest.getState().AssertMutable() + if ms.getOrig() == dest.getOrig() { + return nil + } + + if err := ms.switchDictionary(ms.Dictionary(), dest.Dictionary()); err != nil { + return err + } + + ms.ResourceProfiles().MoveAndAppendTo(dest.ResourceProfiles()) + ms.MarkReadOnly() + + return nil +} diff --git a/pdata/pprofile/profiles_merge_test.go b/pdata/pprofile/profiles_merge_test.go new file mode 100644 index 00000000000..03d451e1abe --- /dev/null +++ b/pdata/pprofile/profiles_merge_test.go @@ -0,0 +1,62 @@ +// 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 TestProfilesMergeTo(t *testing.T) { + src := NewProfiles() + dest := NewProfiles() + + src.ResourceProfiles().AppendEmpty() + src.ResourceProfiles().AppendEmpty() + dest.ResourceProfiles().AppendEmpty() + + require.NoError(t, src.MergeTo(dest)) + + assert.Equal(t, 3, dest.ResourceProfiles().Len()) + assert.Equal(t, 0, src.ResourceProfiles().Len()) + assert.True(t, src.IsReadOnly()) +} + +func TestProfilesMergeToSelf(t *testing.T) { + profiles := NewProfiles() + profiles.Dictionary().StringTable().Append("", "test") + profiles.ResourceProfiles().AppendEmpty() + + require.NoError(t, profiles.MergeTo(profiles)) + + assert.Equal(t, 2, profiles.Dictionary().StringTable().Len()) + assert.Equal(t, 1, profiles.ResourceProfiles().Len()) +} + +func TestProfilesMergeToError(t *testing.T) { + src := NewProfiles() + dest := NewProfiles() + + stackTable := src.Dictionary().StackTable() + stackTable.AppendEmpty() + stack := stackTable.AppendEmpty() + stack.LocationIndices().Append(1) + + locationTable := src.Dictionary().LocationTable() + locationTable.AppendEmpty() + locationTable.AppendEmpty().SetMappingIndex(1) + + sample := src.ResourceProfiles().AppendEmpty(). + ScopeProfiles().AppendEmpty(). + Profiles().AppendEmpty(). + Samples().AppendEmpty() + sample.SetStackIndex(1) + + err := src.MergeTo(dest) + require.Error(t, err) + + assert.Equal(t, 0, dest.ResourceProfiles().Len()) +}