Skip to content

Commit

Permalink
Add Prometheus Receiver test for time series with empty label values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafain117 authored Nov 24, 2021
1 parent 5e0e78e commit dfa941e
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
16 changes: 16 additions & 0 deletions receiver/prometheusreceiver/metrics_receiver_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ func compareSummaryAttributes(attributes map[string]string) summaryPointComparat
}
}

func compareHistogramAttributes(attributes map[string]string) histogramPointComparator {
return func(t *testing.T, histogramDataPoint *pdata.HistogramDataPoint) {
req := assert.Equal(t, len(attributes), histogramDataPoint.Attributes().Len(), "Histogram attributes length do not match")
if req {
for k, v := range attributes {
value, ok := histogramDataPoint.Attributes().Get(k)
if ok {
assert.Equal(t, v, value.AsString(), "Histogram attributes value do not match")
} else {
assert.Fail(t, "Histogram attributes key do not match")
}
}
}
}
}

func compareStartTimestamp(startTimeStamp pdata.Timestamp) numberPointComparator {
return func(t *testing.T, numberDataPoint *pdata.NumberDataPoint) {
assert.Equal(t, startTimeStamp.String(), numberDataPoint.StartTimestamp().String(), "Start-Timestamp does not match")
Expand Down
167 changes: 167 additions & 0 deletions receiver/prometheusreceiver/metrics_receiver_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,170 @@ func TestLabelNameLimitConfig(t *testing.T) {

testComponentCustomConfig(t, targets, mp, cfg)
}

//for all metric types, testLabel has empty value
const emptyLabelValuesTarget1 = `
# HELP test_gauge0 This is my gauge
# TYPE test_gauge0 gauge
test_gauge0{id="1",testLabel=""} 19
# HELP test_counter0 This is my counter
# TYPE test_counter0 counter
test_counter0{id="1",testLabel=""} 100
# HELP test_histogram0 This is my histogram
# TYPE test_histogram0 histogram
test_histogram0_bucket{id="1",testLabel="",le="0.1"} 1000
test_histogram0_bucket{id="1",testLabel="",le="0.5"} 1500
test_histogram0_bucket{id="1",testLabel="",le="1"} 2000
test_histogram0_bucket{id="1",testLabel="",le="+Inf"} 2500
test_histogram0_sum{id="1",testLabel=""} 5000
test_histogram0_count{id="1",testLabel=""} 2500
# HELP test_summary0 This is my summary
# TYPE test_summary0 summary
test_summary0{id="1",testLabel="",quantile="0.1"} 1
test_summary0{id="1",testLabel="",quantile="0.5"} 5
test_summary0{id="1",testLabel="",quantile="0.99"} 8
test_summary0_sum{id="1",testLabel=""} 5000
test_summary0_count{id="1",testLabel=""} 1000
`

func verifyEmptyLabelValuesTarget1(t *testing.T, td *testData, rms []*pdata.ResourceMetrics) {
require.Greater(t, len(rms), 0, "At least one resource metric should be present")

want := td.attributes
metrics1 := rms[0].InstrumentationLibraryMetrics().At(0).Metrics()
ts1 := metrics1.At(0).Gauge().DataPoints().At(0).Timestamp()

e1 := []testExpectation{
assertMetricPresent("test_gauge0",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(19),
compareAttributes(map[string]string{"id": "1"}),
},
},
}),
assertMetricPresent("test_counter0",
compareMetricType(pdata.MetricDataTypeSum),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(100),
compareAttributes(map[string]string{"id": "1"}),
},
},
}),
assertMetricPresent("test_histogram0",
compareMetricType(pdata.MetricDataTypeHistogram),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(ts1),
compareHistogramTimestamp(ts1),
compareHistogram(2500, 5000, []uint64{1000, 500, 500, 500}),
compareHistogramAttributes(map[string]string{"id": "1"}),
},
},
}),
assertMetricPresent("test_summary0",
compareMetricType(pdata.MetricDataTypeSummary),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(ts1),
compareSummaryTimestamp(ts1),
compareSummary(1000, 5000, [][]float64{{0.1, 1}, {0.5, 5}, {0.99, 8}}),
compareSummaryAttributes(map[string]string{"id": "1"}),
},
},
}),
}
doCompare(t, "scrape-empty-label-values-1", want, rms[0], e1)
}

// target has two time series for both gauge and counter, only one time series has a value for the label "testLabel"
const emptyLabelValuesTarget2 = `
# HELP test_gauge0 This is my gauge.
# TYPE test_gauge0 gauge
test_gauge0{id="1",testLabel=""} 19
test_gauge0{id="2",testLabel="foobar"} 2
# HELP test_counter0 This is my counter
# TYPE test_counter0 counter
test_counter0{id="1",testLabel=""} 100
test_counter0{id="2",testLabel="foobar"} 110
`

func verifyEmptyLabelValuesTarget2(t *testing.T, td *testData, rms []*pdata.ResourceMetrics) {
require.Greater(t, len(rms), 0, "At least one resource metric should be present")

want := td.attributes
metrics1 := rms[0].InstrumentationLibraryMetrics().At(0).Metrics()
ts1 := metrics1.At(0).Gauge().DataPoints().At(0).Timestamp()

e1 := []testExpectation{
assertMetricPresent("test_gauge0",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(19),
compareAttributes(map[string]string{"id": "1"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(2),
compareAttributes(map[string]string{"id": "2", "testLabel": "foobar"}),
},
},
}),
assertMetricPresent("test_counter0",
compareMetricType(pdata.MetricDataTypeSum),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(100),
compareAttributes(map[string]string{"id": "1"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(110),
compareAttributes(map[string]string{"id": "2", "testLabel": "foobar"}),
},
},
}),
}
doCompare(t, "scrape-empty-label-values-2", want, rms[0], e1)
}

func TestEmptyLabelValues(t *testing.T) {
targets := []*testData{
{
name: "target1",
pages: []mockPrometheusResponse{
{code: 200, data: emptyLabelValuesTarget1},
},
validateFunc: verifyEmptyLabelValuesTarget1,
},
{
name: "target2",
pages: []mockPrometheusResponse{
{code: 200, data: emptyLabelValuesTarget2},
},
validateFunc: verifyEmptyLabelValuesTarget2,
},
}
testComponent(t, targets, false, "")
}

0 comments on commit dfa941e

Please sign in to comment.