Skip to content

Commit

Permalink
fix bug with nil Sum (#5115)
Browse files Browse the repository at this point in the history
A bug was caught in the prometheus tests in the contrib repository where a histogram datapoint that contained a nil sum was copied into a new datapoint, resulting in the sum being initialized to 0.

Added a test to validate the issue is fixed. I will add another issue to add this test to the generated tests for future use.
  • Loading branch information
Alex Boten committed Mar 30, 2022
1 parent a73610a commit 0f07b0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion model/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ func (opv *optionalPrimitiveValue) generateSetWithTestValue(sb *strings.Builder)
}

func (opv *optionalPrimitiveValue) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("dest.Set" + opv.fieldName + "(ms." + opv.fieldName + "())\n")
sb.WriteString("if ms.Has" + opv.fieldName + "(){\n")
sb.WriteString("\tdest.Set" + opv.fieldName + "(ms." + opv.fieldName + "())\n")
sb.WriteString("}\n")
}

var _ baseField = (*optionalPrimitiveValue)(nil)
4 changes: 3 additions & 1 deletion model/internal/pdata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions model/internal/pdata/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,31 @@ func TestDataPointCountWithNilDataPoints(t *testing.T) {
assert.EqualValues(t, 0, metrics.DataPointCount())
}

func TestHistogramWithNilSum(t *testing.T) {
metrics := NewMetrics()
ilm := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
histo := ilm.Metrics().AppendEmpty()
histo.SetDataType(MetricDataTypeHistogram)
histogramDataPoints := histo.Histogram().DataPoints()
histogramDataPoints.AppendEmpty()
dest := ilm.Metrics().AppendEmpty()
histo.CopyTo(dest)
assert.EqualValues(t, histo, dest)
}

func TestHistogramWithValidSum(t *testing.T) {
metrics := NewMetrics()
ilm := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
histo := ilm.Metrics().AppendEmpty()
histo.SetDataType(MetricDataTypeHistogram)
histogramDataPoints := histo.Histogram().DataPoints()
histogramDataPoints.AppendEmpty()
histogramDataPoints.At(0).SetSum(10)
dest := ilm.Metrics().AppendEmpty()
histo.CopyTo(dest)
assert.EqualValues(t, histo, dest)
}

func TestMetricsMoveTo(t *testing.T) {
metrics := NewMetrics()
fillTestResourceMetricsSlice(metrics.ResourceMetrics())
Expand Down

0 comments on commit 0f07b0b

Please sign in to comment.