mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description check before accessing profiles dictionary indices and fail gracefully if they are out of bounds <!-- Issue number if applicable --> #### Link to tracking issue Fixes #14803 <!--Describe what testing was performed and which tests were added.--> #### Testing Added tests to prove missing dictionary indices fail and don't panic <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Andrzej Stencel <[email protected]>
108 lines
3.8 KiB
Go
108 lines
3.8 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package normal
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.opentelemetry.io/collector/pdata/pprofile"
|
|
)
|
|
|
|
func TestMarshalProfiles(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input pprofile.Profiles
|
|
expected string
|
|
}{
|
|
{
|
|
name: "empty profile",
|
|
input: pprofile.NewProfiles(),
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "one profile",
|
|
input: func() pprofile.Profiles {
|
|
profiles := pprofile.NewProfiles()
|
|
dic := profiles.Dictionary()
|
|
dic.StringTable().Append("")
|
|
dic.StringTable().Append("key1")
|
|
|
|
a := dic.AttributeTable().AppendEmpty()
|
|
a.SetKeyStrindex(1)
|
|
a.Value().SetStr("value1")
|
|
|
|
profile := profiles.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
|
|
profiles.ResourceProfiles().At(0).SetSchemaUrl("https://example.com/resource")
|
|
profiles.ResourceProfiles().At(0).Resource().Attributes().PutStr("resourceKey", "resourceValue")
|
|
profiles.ResourceProfiles().At(0).ScopeProfiles().At(0).SetSchemaUrl("https://example.com/scope")
|
|
profiles.ResourceProfiles().At(0).ScopeProfiles().At(0).Scope().SetName("scope-name")
|
|
profiles.ResourceProfiles().At(0).ScopeProfiles().At(0).Scope().SetVersion("1.2.3")
|
|
profiles.ResourceProfiles().At(0).ScopeProfiles().At(0).Scope().Attributes().PutStr("scopeKey", "scopeValue")
|
|
profile.SetProfileID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
|
|
profile.Samples().AppendEmpty()
|
|
profile.Samples().AppendEmpty()
|
|
profile.AttributeIndices().Append(0)
|
|
return profiles
|
|
}(),
|
|
expected: `ResourceProfiles #0 [https://example.com/resource] resourceKey=resourceValue
|
|
ScopeProfiles #0 [email protected] [https://example.com/scope] scopeKey=scopeValue
|
|
0102030405060708090a0b0c0d0e0f10 samples=2 key1=value1
|
|
`,
|
|
},
|
|
{
|
|
name: "invalid attribute index outputs placeholder",
|
|
input: func() pprofile.Profiles {
|
|
profiles := pprofile.NewProfiles()
|
|
dic := profiles.Dictionary()
|
|
dic.StringTable().Append("")
|
|
|
|
profile := profiles.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
|
|
profile.SetProfileID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
|
|
profile.Samples().AppendEmpty()
|
|
// Attribute index 99 is out of bounds (attribute table is empty)
|
|
profile.AttributeIndices().Append(99)
|
|
return profiles
|
|
}(),
|
|
expected: `ResourceProfiles #0
|
|
ScopeProfiles #0
|
|
0102030405060708090a0b0c0d0e0f10 samples=1 [Missing Dictionary AttributeTable Item #99]
|
|
`,
|
|
},
|
|
{
|
|
name: "invalid string index outputs placeholder",
|
|
input: func() pprofile.Profiles {
|
|
profiles := pprofile.NewProfiles()
|
|
dic := profiles.Dictionary()
|
|
// Only one string at index 0
|
|
dic.StringTable().Append("")
|
|
|
|
a := dic.AttributeTable().AppendEmpty()
|
|
// KeyStrindex 99 is out of bounds
|
|
a.SetKeyStrindex(99)
|
|
a.Value().SetStr("value1")
|
|
|
|
profile := profiles.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
|
|
profile.SetProfileID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
|
|
profile.Samples().AppendEmpty()
|
|
profile.AttributeIndices().Append(0)
|
|
return profiles
|
|
}(),
|
|
expected: `ResourceProfiles #0
|
|
ScopeProfiles #0
|
|
0102030405060708090a0b0c0d0e0f10 samples=1 [Missing Dictionary Key String Item #99]=value1
|
|
`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
output, err := NewNormalProfilesMarshaler().MarshalProfiles(tt.input)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.expected, string(output))
|
|
})
|
|
}
|
|
}
|