expand tests for classic and exponential native histograms

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
pull/14978/head
Jeanette Tan 2024-07-03 17:56:48 +08:00 committed by György Krajcsovits
parent e3899187da
commit 8b3ae15ad5
1 changed files with 90 additions and 55 deletions

View File

@ -3426,23 +3426,10 @@ metric: <
> >
`, name, value) `, name, value)
} }
genTestHistProto := func(name string) string { genTestHistProto := func(name string, hasClassic, hasExponential bool) string {
return fmt.Sprintf(` var classic string
name: "%s" if hasClassic {
help: "This is a histogram with default buckets" classic = `
type: HISTOGRAM
metric: <
label: <
name: "address"
value: "0.0.0.0"
>
label: <
name: "port"
value: "5001"
>
histogram: <
sample_count: 1
sample_sum: 10
bucket: < bucket: <
cumulative_count: 0 cumulative_count: 0
upper_bound: 0.005 upper_bound: 0.005
@ -3486,16 +3473,48 @@ metric: <
bucket: < bucket: <
cumulative_count: 1 cumulative_count: 1
upper_bound: 10 upper_bound: 10
>`
}
var expo string
if hasExponential {
expo = `
schema: 3
zero_threshold: 2.938735877055719e-39
zero_count: 0
positive_span: <
offset: 2
length: 1
> >
positive_delta: 1`
}
return fmt.Sprintf(`
name: "%s"
help: "This is a histogram with default buckets"
type: HISTOGRAM
metric: <
label: <
name: "address"
value: "0.0.0.0"
>
label: <
name: "port"
value: "5001"
>
histogram: <
sample_count: 1
sample_sum: 10
%s
%s
> >
timestamp_ms: 1234568 timestamp_ms: 1234568
> >
`, name) `, name, classic, expo)
} }
metricsTexts := map[string]struct { metricsTexts := map[string]struct {
text []string text []string
contentType string contentType string
hasExponential bool
}{ }{
"text": { "text": {
text: []string{ text: []string{
@ -3520,25 +3539,37 @@ metric: <
"protobuf": { "protobuf": {
text: []string{ text: []string{
genTestCounterProto("test_metric_1", 1), genTestCounterProto("test_metric_1", 1),
genTestHistProto("test_histogram_1"), genTestHistProto("test_histogram_1", true, false),
genTestCounterProto("test_metric_2", 1), genTestCounterProto("test_metric_2", 1),
genTestHistProto("test_histogram_2"), genTestHistProto("test_histogram_2", true, false),
genTestCounterProto("test_metric_3", 1), genTestCounterProto("test_metric_3", 1),
genTestHistProto("test_histogram_3"), genTestHistProto("test_histogram_3", true, false),
}, },
contentType: "application/vnd.google.protobuf", contentType: "application/vnd.google.protobuf",
}, },
"protobuf, in different order": { "protobuf, in different order": {
text: []string{ text: []string{
genTestHistProto("test_histogram_1"), genTestHistProto("test_histogram_1", true, false),
genTestCounterProto("test_metric_1", 1), genTestCounterProto("test_metric_1", 1),
genTestHistProto("test_histogram_2"), genTestHistProto("test_histogram_2", true, false),
genTestCounterProto("test_metric_2", 1), genTestCounterProto("test_metric_2", 1),
genTestHistProto("test_histogram_3"), genTestHistProto("test_histogram_3", true, false),
genTestCounterProto("test_metric_3", 1), genTestCounterProto("test_metric_3", 1),
}, },
contentType: "application/vnd.google.protobuf", contentType: "application/vnd.google.protobuf",
}, },
"protobuf, with native exponential histogram": {
text: []string{
genTestCounterProto("test_metric_1", 1),
genTestHistProto("test_histogram_1", true, true),
genTestCounterProto("test_metric_2", 1),
genTestHistProto("test_histogram_2", true, true),
genTestCounterProto("test_metric_3", 1),
genTestHistProto("test_histogram_3", true, true),
},
contentType: "application/vnd.google.protobuf",
hasExponential: true,
},
} }
checkBucketValues := func(expectedCount int, contentType string, series storage.SeriesSet) { checkBucketValues := func(expectedCount int, contentType string, series storage.SeriesSet) {
@ -3746,8 +3777,12 @@ metric: <
checkBucketValues(tc.expectedClassicHistCount, metricsText.contentType, series) checkBucketValues(tc.expectedClassicHistCount, metricsText.contentType, series)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d", i))) series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d", i)))
if metricsText.hasExponential {
checkHistSeries(series, 1, 3)
} else {
checkHistSeries(series, tc.expectedNhcbCount, histogram.CustomBucketsSchema) checkHistSeries(series, tc.expectedNhcbCount, histogram.CustomBucketsSchema)
} }
}
}) })
} }
} }