Skip to content

Commit 702b969

Browse files
committed
upgrade elasticsearch exporter
1 parent 67163c9 commit 702b969

File tree

4 files changed

+140
-103
lines changed

4 files changed

+140
-103
lines changed

exporter/elasticsearchexporter/internal/serializer/otelserializer/profile_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func basicProfiles() pprofiletest.Profiles {
4444
Sample: []pprofiletest.Sample{
4545
{
4646
TimestampsUnixNano: []uint64{0},
47-
Value: []int64{1},
47+
Values: []int64{1},
4848
Locations: []pprofiletest.Location{
4949
{
5050
Mapping: &pprofiletest.Mapping{},
@@ -78,30 +78,38 @@ func TestSerializeProfile(t *testing.T) {
7878
dic.StringTable().Append("samples", "count", "cpu", "nanoseconds")
7979

8080
a := dic.AttributeTable().AppendEmpty()
81-
a.SetKey("process.executable.build_id.htlhash")
81+
a.SetKeyStrindex(4)
82+
dic.StringTable().Append("process.executable.build_id.htlhash")
8283
a.Value().SetStr("600DCAFE4A110000F2BF38C493F5FB92")
8384
a = dic.AttributeTable().AppendEmpty()
84-
a.SetKey("profile.frame.type")
85+
a.SetKeyStrindex(5)
86+
dic.StringTable().Append("profile.frame.type")
8587
a.Value().SetStr("native")
8688
a = dic.AttributeTable().AppendEmpty()
87-
a.SetKey("host.id")
89+
a.SetKeyStrindex(6)
90+
dic.StringTable().Append("host.id")
8891
a.Value().SetStr("localhost")
8992
a = dic.AttributeTable().AppendEmpty()
90-
a.SetKey("process.executable.name")
93+
a.SetKeyStrindex(7)
94+
dic.StringTable().Append("process.executable.name")
9195
a.Value().SetStr("libc.so.6")
9296

97+
dic.MappingTable().AppendEmpty()
9398
m := dic.MappingTable().AppendEmpty()
9499
m.AttributeIndices().Append(0)
95100

96101
l := dic.LocationTable().AppendEmpty()
97-
l.SetMappingIndex(0)
102+
l.SetMappingIndex(1)
98103
l.SetAddress(111)
99104
l.AttributeIndices().Append(1)
100105

106+
stack := dic.StackTable().AppendEmpty()
107+
stack.LocationIndices().Append(0)
108+
101109
return dic
102110
},
103111
profileCustomizer: func(_ pcommon.Resource, _ pcommon.InstrumentationScope, profile pprofile.Profile) {
104-
st := profile.SampleType().AppendEmpty()
112+
st := profile.SampleType()
105113
st.SetTypeStrindex(0)
106114
st.SetUnitStrindex(1)
107115
pt := profile.PeriodType()
@@ -110,12 +118,11 @@ func TestSerializeProfile(t *testing.T) {
110118
profile.SetPeriod(1e9 / 20)
111119

112120
profile.AttributeIndices().Append(2)
113-
profile.LocationIndices().Append(0)
114121

115122
sample := profile.Sample().AppendEmpty()
116123
sample.TimestampsUnixNano().Append(0)
117-
sample.SetLocationsLength(1)
118124
sample.AttributeIndices().Append(3)
125+
sample.SetStackIndex(0)
119126
},
120127
wantErr: false,
121128
expected: []map[string]any{

exporter/elasticsearchexporter/internal/serializer/otelserializer/serializeprofiles/benchmark_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ func BenchmarkTransform(b *testing.B) {
1919
name: "with a basic recorded sample",
2020
buildDictionary: func() pprofile.ProfilesDictionary {
2121
dic := pprofile.NewProfilesDictionary()
22+
dic.StringTable().Append("")
23+
2224
a := dic.AttributeTable().AppendEmpty()
23-
a.SetKey("profile.frame.type")
25+
dic.StringTable().Append("profile.frame.type")
26+
a.SetKeyStrindex(1)
2427
a.Value().SetStr("native")
2528
a = dic.AttributeTable().AppendEmpty()
26-
a.SetKey("process.executable.build_id.htlhash")
29+
a.SetKeyStrindex(2)
30+
dic.StringTable().Append("process.executable.build_id.htlhash")
2731
a.Value().SetStr(buildIDEncoded)
2832
a = dic.AttributeTable().AppendEmpty()
29-
a.SetKey("process.executable.build_id.htlhash")
33+
a.SetKeyStrindex(3)
34+
dic.StringTable().Append("process.executable.build_id.htlhash")
3035
a.Value().SetStr(buildID2Encoded)
3136

3237
dic.StringTable().Append("firefox", "libc.so", "samples", "count", "cpu", "nanoseconds")
@@ -55,7 +60,7 @@ func BenchmarkTransform(b *testing.B) {
5560
sp := rp.ScopeProfiles().AppendEmpty()
5661
p := sp.Profiles().AppendEmpty()
5762

58-
st := p.SampleType().AppendEmpty()
63+
st := p.SampleType()
5964
st.SetTypeStrindex(2)
6065
st.SetUnitStrindex(3)
6166
pt := p.PeriodType()
@@ -64,9 +69,7 @@ func BenchmarkTransform(b *testing.B) {
6469

6570
s := p.Sample().AppendEmpty()
6671
s.TimestampsUnixNano().Append(42)
67-
s.Value().Append(1)
68-
s.SetLocationsLength(2)
69-
s.SetLocationsStartIndex(0)
72+
s.Values().Append(1)
7073

7174
return rp
7275
},

exporter/elasticsearchexporter/internal/serializer/otelserializer/serializeprofiles/transform.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ func Transform(dic pprofile.ProfilesDictionary, resource pcommon.Resource, scope
4646
// and mixing profiles will make profiling information unusable.
4747
func checkProfileType(dic pprofile.ProfilesDictionary, profile pprofile.Profile) error {
4848
sampleType := profile.SampleType()
49-
if sampleType.Len() != 1 {
50-
return fmt.Errorf("expected 1 sample type but got %d", sampleType.Len())
51-
}
5249

53-
sType := getString(dic, int(sampleType.At(0).TypeStrindex()))
54-
sUnit := getString(dic, int(sampleType.At(0).UnitStrindex()))
50+
sType := getString(dic, int(sampleType.TypeStrindex()))
51+
sUnit := getString(dic, int(sampleType.UnitStrindex()))
5552

5653
// Make sure only on-CPU profiling data is accepted at the moment.
5754
// This needs to match with
@@ -94,7 +91,7 @@ func stackPayloads(dic pprofile.ProfilesDictionary, resource pcommon.Resource, s
9491
}
9592

9693
for _, sample := range profile.Sample().All() {
97-
frames, frameTypes, leafFrame, err := stackFrames(dic, profile, sample)
94+
frames, frameTypes, leafFrame, err := stackFrames(dic, sample)
9895
if err != nil {
9996
return nil, fmt.Errorf("failed to create stackframes: %w", err)
10097
}
@@ -141,8 +138,8 @@ func stackPayloads(dic pprofile.ProfilesDictionary, resource pcommon.Resource, s
141138
event.TimeStamp = newUnixTime64(t)
142139

143140
count := 1
144-
if j < sample.Value().Len() {
145-
count = int(sample.Value().At(j))
141+
if j < sample.Values().Len() {
142+
count = int(sample.Values().At(j))
146143
}
147144
for range count {
148145
stackPayload = append(stackPayload, StackPayload{
@@ -234,8 +231,9 @@ func stackTraceEvent(dic pprofile.ProfilesDictionary, traceID string, sample ppr
234231
continue
235232
}
236233
attr := dic.AttributeTable().At(int(idx))
234+
key := dic.StringTable().At(int(attr.KeyStrindex()))
237235

238-
switch attribute.Key(attr.Key()) {
236+
switch attribute.Key(key) {
239237
case semconv.ThreadNameKey:
240238
event.ThreadName = attr.Value().AsString()
241239
case semconv.ProcessExecutableNameKey:
@@ -270,10 +268,11 @@ func stackTrace(stackTraceID string, frames []StackFrame, frameTypes []libpf.Fra
270268
}
271269
}
272270

273-
func stackFrames(dic pprofile.ProfilesDictionary, profile pprofile.Profile, sample pprofile.Sample) ([]StackFrame, []libpf.FrameType, *frameID, error) {
274-
frames := make([]StackFrame, 0, sample.LocationsLength())
271+
func stackFrames(dic pprofile.ProfilesDictionary, sample pprofile.Sample) ([]StackFrame, []libpf.FrameType, *frameID, error) {
272+
stack := dic.StackTable().At(int(sample.StackIndex()))
273+
frames := make([]StackFrame, 0, stack.LocationIndices().Len())
275274

276-
locations := getLocations(dic, profile, sample)
275+
locations := getLocations(dic, stack)
277276
totalFrames := 0
278277
for _, location := range locations {
279278
totalFrames += location.Line().Len()
@@ -328,7 +327,8 @@ func stackFrames(dic pprofile.ProfilesDictionary, profile pprofile.Profile, samp
328327
func getFrameID(dic pprofile.ProfilesDictionary, location pprofile.Location) *frameID {
329328
// The MappingIndex is known to be valid.
330329
fileID := libpf.FileID{}
331-
if location.HasMappingIndex() {
330+
331+
if location.MappingIndex() > 0 {
332332
mapping := dic.MappingTable().At(int(location.MappingIndex()))
333333
fileID, _ = getBuildID(dic, mapping)
334334
}
@@ -377,7 +377,9 @@ func getStringFromAttribute(dic pprofile.ProfilesDictionary, record attributable
377377
return "", fmt.Errorf("requested attribute index (%d) "+
378378
"exceeds size of attribute table (%d)", idx, lenAttrTable)
379379
}
380-
if dic.AttributeTable().At(idx).Key() == attrKey {
380+
381+
key := dic.StringTable().At(int(dic.AttributeTable().At(idx).KeyStrindex()))
382+
if key == attrKey {
381383
return dic.AttributeTable().At(idx).Value().AsString(), nil
382384
}
383385
}
@@ -405,7 +407,11 @@ func executables(dic pprofile.ProfilesDictionary, mappings pprofile.MappingSlice
405407
metadata := make([]ExeMetadata, 0, mappings.Len())
406408
lastSeen := GetStartOfWeekFromTime(time.Now())
407409

408-
for _, mapping := range mappings.All() {
410+
for i, mapping := range mappings.All() {
411+
if i == 0 {
412+
continue
413+
}
414+
409415
filename := dic.StringTable().At(int(mapping.FilenameStrindex()))
410416
if filename == "" {
411417
// This is true for interpreted languages like Python.
@@ -460,18 +466,13 @@ func stackTraceID(frames []StackFrame) (string, error) {
460466
return traceHash.Base64(), nil
461467
}
462468

463-
func getLocations(dic pprofile.ProfilesDictionary, profile pprofile.Profile, sample pprofile.Sample) []pprofile.Location {
464-
locations := make([]pprofile.Location, 0, sample.LocationsLength())
469+
func getLocations(dic pprofile.ProfilesDictionary, stack pprofile.Stack) []pprofile.Location {
470+
locations := make([]pprofile.Location, 0, stack.LocationIndices().Len())
465471

466-
firstIndexPos := int(sample.LocationsStartIndex())
467-
lastIndexPos := int(sample.LocationsStartIndex() + sample.LocationsLength())
468-
lastIndexPos = min(lastIndexPos, profile.LocationIndices().Len())
469-
for i := firstIndexPos; i < lastIndexPos; i++ {
470-
locationIndex := int(profile.LocationIndices().At(i))
471-
if locationIndex < dic.LocationTable().Len() {
472-
locations = append(locations, dic.LocationTable().At(locationIndex))
473-
}
472+
for i := range stack.LocationIndices().All() {
473+
locations = append(locations, dic.LocationTable().At(i))
474474
}
475+
475476
return locations
476477
}
477478

@@ -498,7 +499,7 @@ func newHostMetadata(dic pprofile.ProfilesDictionary, resource pcommon.Resource,
498499

499500
addEventHostData(attrs, resource.Attributes())
500501
addEventHostData(attrs, scope.Attributes())
501-
addEventHostData(attrs, pprofile.FromAttributeIndices(dic.AttributeTable(), profile))
502+
addEventHostData(attrs, pprofile.FromAttributeIndices(dic.AttributeTable(), profile, dic))
502503

503504
if len(attrs) == 0 {
504505
return nil

0 commit comments

Comments
 (0)