Skip to content

Commit

Permalink
Handle empty result sets in merge logic
Browse files Browse the repository at this point in the history
Fixes #329
  • Loading branch information
jacksontj committed Jul 20, 2020
1 parent c8af25e commit 30fc828
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/promhttputil/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ func MergeSampleStream(antiAffinityBuffer model.Time, a, b *model.SampleStream)
return nil, fmt.Errorf("cannot merge mismatch fingerprints")
}

// if either set of values are empty, return the one with data
if len(a.Values) == 0 {
return b, nil
} else if len(b.Values) == 0 {
return a, nil
}

// TODO: really there should be a library method for this in prometheus IMO
// At this point we have 2 sorted lists of datapoints which we need to merge
newValues := make([]model.SamplePair, 0, len(a.Values))
Expand Down
58 changes: 58 additions & 0 deletions pkg/promhttputil/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,64 @@ func TestMergeValues(t *testing.T) {
}),
antiAffinity: model.Time(2),
},

{
name: "Matrix merge empty A",
a: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{},
},
}),
b: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{{
model.Time(100),
model.SampleValue(10),
}},
},
}),
r: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{{
model.Time(100),
model.SampleValue(10),
}},
},
}),
antiAffinity: model.Time(2),
},

{
name: "Matrix merge empty A",
a: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{{
model.Time(100),
model.SampleValue(10),
}},
},
}),
b: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{},
},
}),
r: model.Matrix([]*model.SampleStream{
{
model.Metric(model.LabelSet{model.MetricNameLabel: model.LabelValue("hosta")}),
[]model.SamplePair{{
model.Time(100),
model.SampleValue(10),
}},
},
}),
antiAffinity: model.Time(2),
},
}

for _, test := range tests {
Expand Down

0 comments on commit 30fc828

Please sign in to comment.