Skip to content

Commit

Permalink
Fix count calculation and add asserts on input and output counts
Browse files Browse the repository at this point in the history
Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama committed Jul 14, 2023
1 parent d6499e6 commit 36ace23
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 15 additions & 1 deletion pkg/translator/prometheusremotewrite/histograms.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func exponentialToNativeHistogram(p pmetric.ExponentialHistogramDataPoint) (prom
if p.HasSum() {
h.Sum = p.Sum()
}
h.Count = &prompb.Histogram_CountInt{CountInt: p.Count()}
if scaleDown > 0 {
h.Count = &prompb.Histogram_CountInt{CountInt: nativeHistogramBucketCount(&h)}
} else {
h.Count = &prompb.Histogram_CountInt{CountInt: p.Count()}
}
}
return h, nil
}
Expand Down Expand Up @@ -194,3 +198,13 @@ func convertBucketsLayout(buckets pmetric.ExponentialHistogramDataPointBuckets,

return spans, deltas
}

func nativeHistogramBucketCount(h *prompb.Histogram) (count uint64) {
for _, span := range h.PositiveSpans {
count += uint64(span.Length)
}
for _, span := range h.NegativeSpans {
count += uint64(span.Length)
}
return
}
22 changes: 21 additions & 1 deletion pkg/translator/prometheusremotewrite/histograms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func TestExponentialToNativeHistogram(t *testing.T) {
},
wantNativeHist: func() prompb.Histogram {
return prompb.Histogram{
Count: &prompb.Histogram_CountInt{CountInt: 6},
Count: &prompb.Histogram_CountInt{CountInt: 4},
Sum: 10.1,
Schema: 8,
ZeroThreshold: defaultZeroThreshold,
Expand All @@ -552,6 +552,7 @@ func TestExponentialToNativeHistogram(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
validateHistogramCount(t, tt.exponentialHist()) // Sanity check.
got, err := exponentialToNativeHistogram(tt.exponentialHist())
if tt.wantErrMessage != "" {
assert.ErrorContains(t, err, tt.wantErrMessage)
Expand All @@ -560,10 +561,29 @@ func TestExponentialToNativeHistogram(t *testing.T) {

require.NoError(t, err)
assert.Equal(t, tt.wantNativeHist(), got)
validateNativeHistogramCount(t, got)
})
}
}

func validateHistogramCount(t *testing.T, h pmetric.ExponentialHistogramDataPoint) {
require.Equal(t, h.Count(), uint64(h.Positive().BucketCounts().Len())+uint64(h.Negative().BucketCounts().Len()))
}

func validateNativeHistogramCount(t *testing.T, h prompb.Histogram) {
require.NotNil(t, h.Count)
require.IsType(t, &prompb.Histogram_CountInt{}, h.Count)
want := h.Count.(*prompb.Histogram_CountInt).CountInt
actualCount := uint64(0)
for _, span := range h.PositiveSpans {
actualCount += uint64(span.Length)
}
for _, span := range h.NegativeSpans {
actualCount += uint64(span.Length)
}
assert.Equal(t, want, actualCount)
}

func TestAddSingleExponentialHistogramDataPoint(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 36ace23

Please sign in to comment.