| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlptext |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 16 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 17 |
) |
| 18 |
|
| 19 |
func TestProfilesText(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
in pprofile.Profiles |
| 23 |
out string |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "empty_profiles", |
| 27 |
in: pprofile.NewProfiles(), |
| 28 |
out: "empty.out", |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "two_profiles", |
| 32 |
in: extendProfiles(testdata.GenerateProfiles(2)), |
| 33 |
out: "two_profiles.out", |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "profiles_with_entity_refs", |
| 37 |
in: generateProfilesWithEntityRefs(), |
| 38 |
out: "profiles_with_entity_refs.out", |
| 39 |
}, |
| 40 |
} |
| 41 |
for _, tt := range tests { |
| 42 |
t.Run(tt.name, func(t *testing.T) { |
| 43 |
got, err := NewTextProfilesMarshaler().MarshalProfiles(tt.in) |
| 44 |
require.NoError(t, err) |
| 45 |
out, err := os.ReadFile(filepath.Join("testdata", "profiles", tt.out)) |
| 46 |
require.NoError(t, err) |
| 47 |
expected := strings.ReplaceAll(string(out), "\r", "") |
| 48 |
assert.Equal(t, expected, string(got)) |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func extendProfiles(profiles pprofile.Profiles) pprofile.Profiles { |
| 55 |
dic := profiles.Dictionary() |
| 56 |
location := dic.LocationTable().AppendEmpty() |
| 57 |
location.SetMappingIndex(3) |
| 58 |
location.SetAddress(4) |
| 59 |
line := location.Lines().AppendEmpty() |
| 60 |
line.SetFunctionIndex(1) |
| 61 |
line.SetLine(2) |
| 62 |
line.SetColumn(3) |
| 63 |
location.AttributeIndices().FromRaw([]int32{6, 7}) |
| 64 |
dic.StringTable().Append("intValue") |
| 65 |
at := dic.AttributeTable() |
| 66 |
a := at.AppendEmpty() |
| 67 |
a.SetKeyStrindex(1) |
| 68 |
a.Value().SetInt(42) |
| 69 |
attributeUnits := dic.AttributeTable().AppendEmpty() |
| 70 |
attributeUnits.SetKeyStrindex(2) |
| 71 |
attributeUnits.SetUnitStrindex(5) |
| 72 |
dic.StringTable().Append("foobar") |
| 73 |
mapping := dic.MappingTable().AppendEmpty() |
| 74 |
mapping.SetMemoryStart(2) |
| 75 |
mapping.SetMemoryLimit(3) |
| 76 |
mapping.SetFileOffset(4) |
| 77 |
mapping.SetFilenameStrindex(5) |
| 78 |
mapping.AttributeIndices().FromRaw([]int32{7, 8}) |
| 79 |
function := dic.FunctionTable().AppendEmpty() |
| 80 |
function.SetNameStrindex(2) |
| 81 |
function.SetSystemNameStrindex(3) |
| 82 |
function.SetFilenameStrindex(4) |
| 83 |
function.SetStartLine(5) |
| 84 |
|
| 85 |
linkTable := dic.LinkTable().AppendEmpty() |
| 86 |
linkTable.SetTraceID([16]byte{0x03, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}) |
| 87 |
linkTable.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}) |
| 88 |
|
| 89 |
sc := profiles.ResourceProfiles().At(0).ScopeProfiles().At(0) |
| 90 |
profilesCount := profiles.ResourceProfiles().At(0).ScopeProfiles().At(0).Profiles().Len() |
| 91 |
for i := range profilesCount { |
| 92 |
if i%2 == 0 { |
| 93 |
profile := sc.Profiles().At(i) |
| 94 |
profile.AttributeIndices().FromRaw([]int32{1}) |
| 95 |
} |
| 96 |
} |
| 97 |
return profiles |
| 98 |
} |
| 99 |
|
| 100 |
func generateProfilesWithEntityRefs() pprofile.Profiles { |
| 101 |
pd := pprofile.NewProfiles() |
| 102 |
rp := pd.ResourceProfiles().AppendEmpty() |
| 103 |
|
| 104 |
setupResourceWithEntityRefs(rp.Resource()) |
| 105 |
|
| 106 |
sp := rp.ScopeProfiles().AppendEmpty() |
| 107 |
sp.Scope().SetName("test-scope") |
| 108 |
profile := sp.Profiles().AppendEmpty() |
| 109 |
|
| 110 |
sample := profile.Samples().AppendEmpty() |
| 111 |
sample.Values().Append(100) |
| 112 |
|
| 113 |
dic := pd.Dictionary() |
| 114 |
dic.StringTable().Append("") |
| 115 |
dic.StringTable().Append("cpu") |
| 116 |
dic.StringTable().Append("nanoseconds") |
| 117 |
|
| 118 |
sampleType := profile.SampleType() |
| 119 |
sampleType.SetTypeStrindex(1) |
| 120 |
sampleType.SetUnitStrindex(2) |
| 121 |
|
| 122 |
return pd |
| 123 |
} |