Skip to content

Commit d36dbab

Browse files
authored
[pkg/testdata] Fix type of Attribute.Value for profiles (#39006)
The value type is `string`, but it should be `any`.
1 parent 4079d3f commit d36dbab

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/pdatatest
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Change type of attribute values from string to any.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39006]
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+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

pkg/pdatatest/pprofiletest/profiles_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ func TestCompareProfiles(t *testing.T) {
5959
expected: basicProfiles().Transform(),
6060
actual: basicProfiles().Transform(),
6161
},
62+
{
63+
name: "attribute value string is not bool",
64+
expected: func() pprofile.Profiles {
65+
p := basicProfiles()
66+
p.ResourceProfiles[0].Resource.Attributes[0] = Attribute{"key1", "true"}
67+
return p.Transform()
68+
}(),
69+
actual: func() pprofile.Profiles {
70+
p := basicProfiles()
71+
p.ResourceProfiles[0].Resource.Attributes[0] = Attribute{"key1", true}
72+
return p.Transform()
73+
}(),
74+
withoutOptions: errors.New(`missing expected resource: map[key1:true]; unexpected resource: map[key1:true]`),
75+
},
6276
{
6377
name: "resource order",
6478
expected: func() pprofile.Profiles {

pkg/pdatatest/pprofiletest/types.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package pprofiletest // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pprofiletest"
55
import (
6+
"fmt"
7+
68
"go.opentelemetry.io/collector/pdata/pcommon"
79
"go.opentelemetry.io/collector/pdata/pprofile"
810
)
@@ -30,7 +32,10 @@ func (rp ResourceProfile) Transform(pp pprofile.Profiles) pprofile.ResourceProfi
3032
sp.Transform(prp)
3133
}
3234
for _, a := range rp.Resource.Attributes {
33-
prp.Resource().Attributes().PutStr(a.Key, a.Value)
35+
if prp.Resource().Attributes().PutEmpty(a.Key).FromRaw(a.Value) != nil {
36+
panic(fmt.Sprintf("unsupported resource attribute value: {%s: %v (type %T)}",
37+
a.Key, a.Value, a.Value))
38+
}
3439
}
3540
return prp
3641
}
@@ -66,7 +71,10 @@ type Scope struct {
6671
func (sc Scope) Transform(psp pprofile.ScopeProfiles) pcommon.InstrumentationScope {
6772
psc := psp.Scope()
6873
for _, a := range sc.Attributes {
69-
psc.Attributes().PutStr(a.Key, a.Value)
74+
if psc.Attributes().PutEmpty(a.Key).FromRaw(a.Value) != nil {
75+
panic(fmt.Sprintf("unsupported scope attribute value: {%s: %v (type %T)}",
76+
a.Key, a.Value, a.Value))
77+
}
7078
}
7179
psc.SetName(sc.Name)
7280
psc.SetVersion(sc.Version)
@@ -274,13 +282,16 @@ func (m *Mapping) Transform(pp pprofile.Profile) {
274282

275283
type Attribute struct {
276284
Key string
277-
Value string
285+
Value any
278286
}
279287

280288
func (a *Attribute) Transform(pp pprofile.Profile) int32 {
281289
pa := pp.AttributeTable().AppendEmpty()
282290
pa.SetKey(a.Key)
283-
pa.Value().SetStr(a.Value)
291+
if pa.Value().FromRaw(a.Value) != nil {
292+
panic(fmt.Sprintf("unsupported attribute value: {%s: %v (type %T)}",
293+
a.Key, a.Value, a.Value))
294+
}
284295
return int32(pp.AttributeTable().Len() - 1)
285296
}
286297

0 commit comments

Comments
 (0)