Skip to content

Commit fb7a052

Browse files
committed
introduce switch dictionary for value type
1 parent c4bc99e commit fb7a052

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

pdata/pprofile/profile.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,14 @@ func (ms Profile) switchDictionary(src, dst ProfilesDictionary) error {
4444
}
4545
}
4646

47+
err := ms.PeriodType().switchDictionary(src, dst)
48+
if err != nil {
49+
return fmt.Errorf("error switching dictionary for period type: %w", err)
50+
}
51+
err = ms.SampleType().switchDictionary(src, dst)
52+
if err != nil {
53+
return fmt.Errorf("error switching dictionary for sample type: %w", err)
54+
}
55+
4756
return nil
4857
}

pdata/pprofile/profile_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,66 @@ func TestProfileSwitchDictionary(t *testing.T) {
183183
return d
184184
}(),
185185
},
186+
{
187+
name: "with a profile that has a period type",
188+
profile: func() Profile {
189+
p := NewProfile()
190+
p.PeriodType().SetTypeStrindex(1)
191+
return p
192+
}(),
193+
194+
src: func() ProfilesDictionary {
195+
d := NewProfilesDictionary()
196+
d.StringTable().Append("", "test")
197+
return d
198+
}(),
199+
dst: func() ProfilesDictionary {
200+
d := NewProfilesDictionary()
201+
d.StringTable().Append("", "foo")
202+
return d
203+
}(),
204+
205+
wantProfile: func() Profile {
206+
p := NewProfile()
207+
p.PeriodType().SetTypeStrindex(2)
208+
return p
209+
}(),
210+
wantDictionary: func() ProfilesDictionary {
211+
d := NewProfilesDictionary()
212+
d.StringTable().Append("", "foo", "test")
213+
return d
214+
}(),
215+
},
216+
{
217+
name: "with a profile that has a sample type",
218+
profile: func() Profile {
219+
p := NewProfile()
220+
p.SampleType().SetTypeStrindex(1)
221+
return p
222+
}(),
223+
224+
src: func() ProfilesDictionary {
225+
d := NewProfilesDictionary()
226+
d.StringTable().Append("", "test")
227+
return d
228+
}(),
229+
dst: func() ProfilesDictionary {
230+
d := NewProfilesDictionary()
231+
d.StringTable().Append("", "foo")
232+
return d
233+
}(),
234+
235+
wantProfile: func() Profile {
236+
p := NewProfile()
237+
p.SampleType().SetTypeStrindex(2)
238+
return p
239+
}(),
240+
wantDictionary: func() ProfilesDictionary {
241+
d := NewProfilesDictionary()
242+
d.StringTable().Append("", "foo", "test")
243+
return d
244+
}(),
245+
},
186246
} {
187247
t.Run(tt.name, func(t *testing.T) {
188248
profile := tt.profile

pdata/pprofile/valuetype.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
import "fmt"
7+
8+
// switchDictionary updates the ValueType, switching its indices from one
9+
// dictionary to another.
10+
func (ms ValueType) switchDictionary(src, dst ProfilesDictionary) error {
11+
if ms.TypeStrindex() > 0 {
12+
if src.StringTable().Len() < int(ms.TypeStrindex()) {
13+
return fmt.Errorf("invalid type index %d", ms.TypeStrindex())
14+
}
15+
16+
idx, err := SetString(dst.StringTable(), src.StringTable().At(int(ms.TypeStrindex())))
17+
if err != nil {
18+
return fmt.Errorf("couldn't set type: %w", err)
19+
}
20+
ms.SetTypeStrindex(idx)
21+
}
22+
23+
if ms.UnitStrindex() > 0 {
24+
if src.StringTable().Len() < int(ms.UnitStrindex()) {
25+
return fmt.Errorf("invalid unit index %d", ms.UnitStrindex())
26+
}
27+
28+
idx, err := SetString(dst.StringTable(), src.StringTable().At(int(ms.UnitStrindex())))
29+
if err != nil {
30+
return fmt.Errorf("couldn't set unit: %w", err)
31+
}
32+
ms.SetUnitStrindex(idx)
33+
}
34+
35+
return nil
36+
}

pdata/pprofile/valuetype_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pprofile
5+
6+
import (
7+
"errors"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestValueTypeSwitchDictionary(t *testing.T) {
15+
for _, tt := range []struct {
16+
name string
17+
valueType ValueType
18+
19+
src ProfilesDictionary
20+
dst ProfilesDictionary
21+
22+
wantValueType ValueType
23+
wantDictionary ProfilesDictionary
24+
wantErr error
25+
}{
26+
{
27+
name: "with an empty value type",
28+
valueType: NewValueType(),
29+
30+
src: NewProfilesDictionary(),
31+
dst: NewProfilesDictionary(),
32+
33+
wantValueType: NewValueType(),
34+
wantDictionary: NewProfilesDictionary(),
35+
},
36+
{
37+
name: "with an existing type",
38+
valueType: func() ValueType {
39+
vt := NewValueType()
40+
vt.SetTypeStrindex(1)
41+
return vt
42+
}(),
43+
44+
src: func() ProfilesDictionary {
45+
d := NewProfilesDictionary()
46+
d.StringTable().Append("", "test")
47+
return d
48+
}(),
49+
dst: func() ProfilesDictionary {
50+
d := NewProfilesDictionary()
51+
d.StringTable().Append("", "foo")
52+
return d
53+
}(),
54+
55+
wantValueType: func() ValueType {
56+
vt := NewValueType()
57+
vt.SetTypeStrindex(2)
58+
return vt
59+
}(),
60+
wantDictionary: func() ProfilesDictionary {
61+
d := NewProfilesDictionary()
62+
d.StringTable().Append("", "foo", "test")
63+
return d
64+
}(),
65+
},
66+
{
67+
name: "with a type index that does not match anything",
68+
valueType: func() ValueType {
69+
vt := NewValueType()
70+
vt.SetTypeStrindex(1)
71+
return vt
72+
}(),
73+
74+
src: NewProfilesDictionary(),
75+
dst: NewProfilesDictionary(),
76+
77+
wantValueType: func() ValueType {
78+
vt := NewValueType()
79+
vt.SetTypeStrindex(1)
80+
return vt
81+
}(),
82+
wantDictionary: NewProfilesDictionary(),
83+
wantErr: errors.New("invalid type index 1"),
84+
},
85+
{
86+
name: "with an existing unit",
87+
valueType: func() ValueType {
88+
vt := NewValueType()
89+
vt.SetUnitStrindex(1)
90+
return vt
91+
}(),
92+
93+
src: func() ProfilesDictionary {
94+
d := NewProfilesDictionary()
95+
d.StringTable().Append("", "test")
96+
return d
97+
}(),
98+
dst: func() ProfilesDictionary {
99+
d := NewProfilesDictionary()
100+
d.StringTable().Append("", "foo")
101+
return d
102+
}(),
103+
104+
wantValueType: func() ValueType {
105+
vt := NewValueType()
106+
vt.SetUnitStrindex(2)
107+
return vt
108+
}(),
109+
wantDictionary: func() ProfilesDictionary {
110+
d := NewProfilesDictionary()
111+
d.StringTable().Append("", "foo", "test")
112+
return d
113+
}(),
114+
},
115+
{
116+
name: "with a unit index that does not match anything",
117+
valueType: func() ValueType {
118+
vt := NewValueType()
119+
vt.SetUnitStrindex(1)
120+
return vt
121+
}(),
122+
123+
src: NewProfilesDictionary(),
124+
dst: NewProfilesDictionary(),
125+
126+
wantValueType: func() ValueType {
127+
vt := NewValueType()
128+
vt.SetUnitStrindex(1)
129+
return vt
130+
}(),
131+
wantDictionary: NewProfilesDictionary(),
132+
wantErr: errors.New("invalid unit index 1"),
133+
},
134+
} {
135+
t.Run(tt.name, func(t *testing.T) {
136+
vt := tt.valueType
137+
dst := tt.dst
138+
err := vt.switchDictionary(tt.src, dst)
139+
140+
if tt.wantErr == nil {
141+
require.NoError(t, err)
142+
} else {
143+
require.Equal(t, tt.wantErr, err)
144+
}
145+
146+
assert.Equal(t, tt.wantValueType, vt)
147+
assert.Equal(t, tt.wantDictionary, dst)
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)