Skip to content

Commit aa403a7

Browse files
authored
Fixes merge of empty samples in pprof (#3031)
1 parent 11256b1 commit aa403a7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

pkg/pprof/merge.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ func (m *ProfileMerge) merge(p *profilev1.Profile, clone bool) error {
3737
return nil
3838
}
3939
ConvertIDsToIndices(p)
40+
var initial bool
4041
if m.profile == nil {
4142
m.init(p, clone)
43+
initial = true
4244
}
4345

4446
// We rewrite strings first in order to compare
4547
// sample types and period type.
4648
m.tmp = slices.GrowLen(m.tmp, len(p.StringTable))
4749
m.stringTable.Index(m.tmp, p.StringTable)
4850
RewriteStrings(p, m.tmp)
49-
if len(m.profile.StringTable) == 0 {
51+
if initial {
5052
// Right after initialisation we need to make
5153
// sure that the string identifiers are normalized
5254
// among profiles.

pkg/pprof/merge_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,60 @@ func Test_Merge_Sample(t *testing.T) {
368368

369369
testhelper.EqualProto(t, expected, m.Profile())
370370
}
371+
372+
func TestMergeEmpty(t *testing.T) {
373+
var m ProfileMerge
374+
375+
err := m.Merge(&profilev1.Profile{
376+
SampleType: []*profilev1.ValueType{
377+
{
378+
Type: 2,
379+
Unit: 1,
380+
},
381+
},
382+
PeriodType: &profilev1.ValueType{
383+
Type: 2,
384+
Unit: 1,
385+
},
386+
StringTable: []string{"", "nanoseconds", "cpu"},
387+
})
388+
require.NoError(t, err)
389+
err = m.Merge(&profilev1.Profile{
390+
Sample: []*profilev1.Sample{
391+
{
392+
LocationId: []uint64{1},
393+
Value: []int64{1},
394+
},
395+
},
396+
Location: []*profilev1.Location{
397+
{
398+
Id: 1,
399+
MappingId: 1,
400+
Line: []*profilev1.Line{{FunctionId: 1, Line: 1}},
401+
},
402+
},
403+
Function: []*profilev1.Function{
404+
{
405+
Id: 1,
406+
Name: 1,
407+
},
408+
},
409+
SampleType: []*profilev1.ValueType{
410+
{
411+
Type: 3,
412+
Unit: 2,
413+
},
414+
},
415+
PeriodType: &profilev1.ValueType{
416+
Type: 3,
417+
Unit: 2,
418+
},
419+
Mapping: []*profilev1.Mapping{
420+
{
421+
Id: 1,
422+
},
423+
},
424+
StringTable: []string{"", "bar", "nanoseconds", "cpu"},
425+
})
426+
require.NoError(t, err)
427+
}

0 commit comments

Comments
 (0)