Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prometheus Receiver] Adding testcase to validate normalNaNs passthrough #6710

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions receiver/prometheusreceiver/metrics_receiver_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ func compareDoubleValue(doubleVal float64) numberPointComparator {
}
}

func assertNormalNan() numberPointComparator {
return func(t *testing.T, numberDataPoint *pdata.NumberDataPoint) {
assert.True(t, math.Float64bits(numberDataPoint.DoubleVal()) == value.NormalNaN,
"Metric double value is not normalNaN as expected")
}
}

func compareHistogram(count uint64, sum float64, buckets []uint64) histogramPointComparator {
return func(t *testing.T, histogramDataPoint *pdata.HistogramDataPoint) {
assert.Equal(t, count, histogramDataPoint.Count(), "Histogram count value does not match")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math"
"testing"

"github.com/prometheus/prometheus/pkg/value"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/model/pdata"
)
Expand Down Expand Up @@ -231,6 +232,89 @@ func verifyStaleNanPage1FirstFailedScrape(t *testing.T, td *testData, resourceMe
doCompare(t, fmt.Sprintf("failedScrape-scrape-%d", iteration), wantAttributes, resourceMetric, e1)
}

// Prometheus gauge metric can be set to NaN, a use case could be when value 0 is not representable
// Prometheus summary metric quantiles can have NaN after getting expired
var normalNaNsPage1 = `
# HELP go_threads Number of OS threads created
# TYPE go_threads gauge
go_threads NaN

# HELP redis_connected_clients Redis connected clients
redis_connected_clients{name="rough-snowflake-web",port="6380"} NaN

# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} NaN
rpc_duration_seconds{quantile="0.9"} NaN
rpc_duration_seconds{quantile="0.99"} NaN
rpc_duration_seconds_sum 5000
rpc_duration_seconds_count 1000
`

// TestNormalNaNs validates the output of receiver when testdata contains NaN values
func TestNormalNaNs(t *testing.T) {
// 1. setup input data
targets := []*testData{
{
name: "target1",
pages: []mockPrometheusResponse{
{code: 200, data: normalNaNsPage1},
},
validateFunc: verifyNormalNaNs,
},
}
testComponent(t, targets, false, "")
}

func verifyNormalNaNs(t *testing.T, td *testData, resourceMetrics []*pdata.ResourceMetrics) {
verifyNumValidScrapeResults(t, td, resourceMetrics)
m1 := resourceMetrics[0]

// m1 has 3 metrics + 5 internal scraper metrics
assert.Equal(t, 8, metricsCount(m1))

wantAttributes := td.attributes

metrics1 := m1.InstrumentationLibraryMetrics().At(0).Metrics()
ts1 := getTS(metrics1)
e1 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
assertNormalNan(),
},
},
}),
assertMetricPresent("redis_connected_clients",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareAttributes(map[string]string{"name": "rough-snowflake-web", "port": "6380"}),
assertNormalNan(),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pdata.MetricDataTypeSummary),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(ts1),
compareSummaryTimestamp(ts1),
compareSummary(1000, 5000, [][]float64{{0.01, math.Float64frombits(value.NormalNaN)},
{0.9, math.Float64frombits(value.NormalNaN)}, {0.99, math.Float64frombits(value.NormalNaN)}}),
},
},
}),
}
doCompare(t, "scrape-NormalNaN-1", wantAttributes, m1, e1)
}

var infPage1 = `
# HELP go_threads Number of OS threads created
# TYPE go_threads gauge
Expand Down