Skip to content

Commit a03c3a1

Browse files
committed
[debugexporter] Fix profile attribute formatting
Assisted-by: ChatGPT 5.6
1 parent 52e6bf4 commit a03c3a1

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

exporter/debugexporter/internal/otlptext/databuffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (b *dataBuffer) logProfileSamples(ss pprofile.SampleSlice, dic pprofile.Pro
361361
if keyIdx < dic.StringTable().Len() {
362362
key = dic.StringTable().At(keyIdx)
363363
}
364-
b.logEntry(" -> %s: %s", key, attr.Value().AsRaw())
364+
b.logEntry(" -> %s: %v", key, attr.Value().AsRaw())
365365
}
366366
}
367367
}

exporter/debugexporter/internal/otlptext/profiles_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package otlptext
55

66
import (
7+
"fmt"
78
"os"
89
"path/filepath"
910
"strings"
@@ -12,6 +13,7 @@ import (
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415

16+
"go.opentelemetry.io/collector/pdata/pcommon"
1517
"go.opentelemetry.io/collector/pdata/pprofile"
1618
"go.opentelemetry.io/collector/pdata/testdata"
1719
)
@@ -50,6 +52,55 @@ func TestProfilesText(t *testing.T) {
5052
}
5153
}
5254

55+
func TestProfilesTextSampleAttributeValues(t *testing.T) {
56+
tests := []struct {
57+
name string
58+
setValue func(pcommon.Value)
59+
expected string
60+
}{
61+
{name: "empty", setValue: func(v pcommon.Value) {}, expected: "<nil>"},
62+
{name: "string", setValue: func(v pcommon.Value) { v.SetStr("value") }, expected: "value"},
63+
{name: "integer", setValue: func(v pcommon.Value) { v.SetInt(42) }, expected: "42"},
64+
{name: "double", setValue: func(v pcommon.Value) { v.SetDouble(3.14) }, expected: "3.14"},
65+
{name: "boolean", setValue: func(v pcommon.Value) { v.SetBool(true) }, expected: "true"},
66+
{
67+
name: "bytes",
68+
setValue: func(v pcommon.Value) { v.SetEmptyBytes().FromRaw([]byte{1, 2, 3}) },
69+
expected: "[1 2 3]",
70+
},
71+
{
72+
name: "map",
73+
setValue: func(v pcommon.Value) { _ = v.SetEmptyMap().FromRaw(map[string]any{"nested": "value"}) },
74+
expected: "map[nested:value]",
75+
},
76+
{
77+
name: "slice",
78+
setValue: func(v pcommon.Value) { _ = v.SetEmptySlice().FromRaw([]any{"value", true}) },
79+
expected: "[value true]",
80+
},
81+
}
82+
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
profiles := pprofile.NewProfiles()
86+
dic := profiles.Dictionary()
87+
dic.StringTable().Append("")
88+
dic.StringTable().Append("key")
89+
attribute := dic.AttributeTable().AppendEmpty()
90+
attribute.SetKeyStrindex(1)
91+
tt.setValue(attribute.Value())
92+
93+
sample := profiles.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty().Samples().AppendEmpty()
94+
sample.Values().Append(100)
95+
sample.AttributeIndices().Append(0)
96+
97+
output, err := NewTextProfilesMarshaler().MarshalProfiles(profiles)
98+
require.NoError(t, err)
99+
assert.Contains(t, string(output), fmt.Sprintf("-> key: %s", tt.expected))
100+
})
101+
}
102+
}
103+
53104
func TestProfilesTextInvalidDictionaryIndex(t *testing.T) {
54105
tests := []struct {
55106
name string

0 commit comments

Comments
 (0)