Skip to content

Commit 328d308

Browse files
committed
Introduce MergeFrom for profiles
Signed-off-by: Israel Blancas <[email protected]>
1 parent 73f090c commit 328d308

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
7+
component: pdata/pprofile
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Introduce `MergeTo` method
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14091]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

pdata/pprofile/profiles_merge.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
5+
6+
// MergeTo merges the current Profiles into dest, updating the destination
7+
// dictionary as needed and appending the resource profiles.
8+
// The source Profiles is consumed and marked read-only after this operation.
9+
func (ms Profiles) MergeTo(dest Profiles) error {
10+
ms.getState().AssertMutable()
11+
dest.getState().AssertMutable()
12+
if ms.getOrig() == dest.getOrig() {
13+
return nil
14+
}
15+
16+
if err := ms.switchDictionary(ms.Dictionary(), dest.Dictionary()); err != nil {
17+
return err
18+
}
19+
20+
ms.ResourceProfiles().MoveAndAppendTo(dest.ResourceProfiles())
21+
ms.MarkReadOnly()
22+
23+
return nil
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestProfilesMergeTo(t *testing.T) {
14+
src := NewProfiles()
15+
dest := NewProfiles()
16+
17+
src.ResourceProfiles().AppendEmpty()
18+
src.ResourceProfiles().AppendEmpty()
19+
dest.ResourceProfiles().AppendEmpty()
20+
21+
require.NoError(t, src.MergeTo(dest))
22+
23+
assert.Equal(t, 3, dest.ResourceProfiles().Len())
24+
assert.Equal(t, 0, src.ResourceProfiles().Len())
25+
assert.True(t, src.IsReadOnly())
26+
}
27+
28+
func TestProfilesMergeToSelf(t *testing.T) {
29+
profiles := NewProfiles()
30+
profiles.Dictionary().StringTable().Append("test")
31+
profiles.ResourceProfiles().AppendEmpty()
32+
33+
require.NoError(t, profiles.MergeTo(profiles))
34+
35+
assert.Equal(t, 1, profiles.Dictionary().StringTable().Len())
36+
assert.Equal(t, 1, profiles.ResourceProfiles().Len())
37+
}
38+
39+
func TestProfilesMergeToError(t *testing.T) {
40+
src := NewProfiles()
41+
dest := NewProfiles()
42+
43+
stackTable := src.Dictionary().StackTable()
44+
stackTable.AppendEmpty()
45+
stack := stackTable.AppendEmpty()
46+
stack.LocationIndices().Append(1)
47+
48+
locationTable := src.Dictionary().LocationTable()
49+
locationTable.AppendEmpty()
50+
locationTable.AppendEmpty().SetMappingIndex(1)
51+
52+
sample := src.ResourceProfiles().AppendEmpty().
53+
ScopeProfiles().AppendEmpty().
54+
Profiles().AppendEmpty().
55+
Samples().AppendEmpty()
56+
sample.SetStackIndex(1)
57+
58+
err := src.MergeTo(dest)
59+
require.Error(t, err)
60+
61+
assert.Equal(t, 0, dest.ResourceProfiles().Len())
62+
}

0 commit comments

Comments
 (0)