Skip to content

Commit

Permalink
Add explicit test for prometheus histogram buckets conversion (open-t…
Browse files Browse the repository at this point in the history
…elemetry#9739)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored and djaglowski committed May 10, 2022
1 parent 3e8f888 commit 9aec504
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions receiver/prometheusreceiver/internal/otlp_metricfamily_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ var mc = byLookupMetadataCache{

func TestMetricGroupData_toDistributionUnitTest(t *testing.T) {
type scrape struct {
at int64
value float64
metric string
at int64
value float64
metric string
extraLabel labels.Label
}
tests := []struct {
name string
Expand All @@ -106,21 +107,24 @@ func TestMetricGroupData_toDistributionUnitTest(t *testing.T) {
intervalStartTimeMs int64
}{
{
name: "histogram with startTimestamp of 11",
name: "histogram with startTimestamp",
metricName: "histogram",
intervalStartTimeMs: 11,
labels: labels.Labels{{Name: "a", Value: "A"}, {Name: "le", Value: "0.75"}, {Name: "b", Value: "B"}},
labels: labels.Labels{{Name: "a", Value: "A"}, {Name: "b", Value: "B"}},
scrapes: []*scrape{
{at: 11, value: 10, metric: "histogram_count"},
{at: 11, value: 66, metric: "histogram_count"},
{at: 11, value: 1004.78, metric: "histogram_sum"},
{at: 13, value: 33.7, metric: "value"},
{at: 13, value: 33, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "0.75"}},
{at: 13, value: 55, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "2.75"}},
{at: 13, value: 66, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "+Inf"}},
},
want: func() pmetric.HistogramDataPoint {
point := pmetric.NewHistogramDataPoint()
point.SetCount(10)
point.SetCount(66)
point.SetSum(1004.78)
point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
point.SetBucketCounts([]uint64{33})
point.SetExplicitBounds([]float64{0.75, 2.75})
point.SetBucketCounts([]uint64{33, 22, 11})
point.SetStartTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
attributes := point.Attributes()
attributes.InsertString("a", "A")
Expand All @@ -132,17 +136,20 @@ func TestMetricGroupData_toDistributionUnitTest(t *testing.T) {
name: "histogram that is stale",
metricName: "histogram_stale",
intervalStartTimeMs: 11,
labels: labels.Labels{{Name: "a", Value: "A"}, {Name: "le", Value: "0.75"}, {Name: "b", Value: "B"}},
labels: labels.Labels{{Name: "a", Value: "A"}, {Name: "b", Value: "B"}},
scrapes: []*scrape{
{at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_count"},
{at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_sum"},
{at: 13, value: math.Float64frombits(value.StaleNaN), metric: "value"},
{at: 13, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "0.75"}},
{at: 13, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "2.75"}},
{at: 13, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "+Inf"}},
},
want: func() pmetric.HistogramDataPoint {
point := pmetric.NewHistogramDataPoint()
point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
point.SetFlags(pdataStaleFlags)
point.SetBucketCounts([]uint64{0})
point.SetExplicitBounds([]float64{0.75, 2.75})
point.SetBucketCounts([]uint64{0, 0, 0})
point.SetStartTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
attributes := point.Attributes()
attributes.InsertString("a", "A")
Expand All @@ -157,7 +164,13 @@ func TestMetricGroupData_toDistributionUnitTest(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mp := newMetricFamilyPdata(tt.metricName, mc, zap.NewNop())
for _, tv := range tt.scrapes {
require.NoError(t, mp.Add(tv.metric, tt.labels.Copy(), tv.at, tv.value))
var lbls labels.Labels
if tv.extraLabel.Name != "" {
lbls = labels.NewBuilder(tt.labels).Set(tv.extraLabel.Name, tv.extraLabel.Value).Labels()
} else {
lbls = tt.labels.Copy()
}
require.NoError(t, mp.Add(tv.metric, lbls, tv.at, tv.value))
}

require.Equal(t, 1, len(mp.groups), "Expecting exactly 1 groupKey")
Expand Down

0 comments on commit 9aec504

Please sign in to comment.