don't blindly convert series with the classic histogram name suffixes if they are not actually histograms based on metadata

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
pull/14978/head
Jeanette Tan 12 months ago committed by György Krajcsovits
parent 8b3ae15ad5
commit f35c6649e4
  1. 21
      model/textparse/nhcbparse.go
  2. 82
      scrape/scrape_test.go
  3. 3
      util/convertnhcb/convertnhcb.go

@ -44,6 +44,9 @@ type NhcbParser struct {
// For Metric. // For Metric.
lset labels.Labels lset labels.Labels
metricString string metricString string
// For Type.
bType []byte
typ model.MetricType
// Caches the entry itself if we are inserting a converted NHCB // Caches the entry itself if we are inserting a converted NHCB
// halfway through. // halfway through.
@ -96,7 +99,7 @@ func (p *NhcbParser) Help() ([]byte, []byte) {
} }
func (p *NhcbParser) Type() ([]byte, model.MetricType) { func (p *NhcbParser) Type() ([]byte, model.MetricType) {
return p.parser.Type() return p.bType, p.typ
} }
func (p *NhcbParser) Unit() ([]byte, []byte) { func (p *NhcbParser) Unit() ([]byte, []byte) {
@ -147,7 +150,7 @@ func (p *NhcbParser) Next() (Entry, error) {
case EntrySeries: case EntrySeries:
p.bytes, p.ts, p.value = p.parser.Series() p.bytes, p.ts, p.value = p.parser.Series()
p.metricString = p.parser.Metric(&p.lset) p.metricString = p.parser.Metric(&p.lset)
histBaseName := convertnhcb.GetHistogramMetricBaseName(p.lset) histBaseName := convertnhcb.GetHistogramMetricBaseName(p.lset.Get(labels.MetricName))
if histBaseName == p.lastNativeHistName { if histBaseName == p.lastNativeHistName {
break break
} }
@ -160,20 +163,18 @@ func (p *NhcbParser) Next() (Entry, error) {
if isNhcb := p.handleClassicHistogramSeries(p.lset); isNhcb && !p.keepClassicHistograms { if isNhcb := p.handleClassicHistogramSeries(p.lset); isNhcb && !p.keepClassicHistograms {
return p.Next() return p.Next()
} }
return et, err
case EntryHistogram: case EntryHistogram:
p.bytes, p.ts, p.h, p.fh = p.parser.Histogram() p.bytes, p.ts, p.h, p.fh = p.parser.Histogram()
p.metricString = p.parser.Metric(&p.lset) p.metricString = p.parser.Metric(&p.lset)
p.lastNativeHistName = p.lset.Get(labels.MetricName) p.lastNativeHistName = p.lset.Get(labels.MetricName)
if p.processNhcb() { case EntryType:
p.entry = et p.bType, p.typ = p.parser.Type()
return EntryHistogram, nil
} }
default:
if p.processNhcb() { if p.processNhcb() {
p.entry = et p.entry = et
return EntryHistogram, nil return EntryHistogram, nil
} }
}
return et, err return et, err
} }
@ -182,7 +183,13 @@ func (p *NhcbParser) Next() (Entry, error) {
// isn't already a native histogram with the same name (assuming it is always processed // isn't already a native histogram with the same name (assuming it is always processed
// right before the classic histograms) and returns true if the collation was done. // right before the classic histograms) and returns true if the collation was done.
func (p *NhcbParser) handleClassicHistogramSeries(lset labels.Labels) bool { func (p *NhcbParser) handleClassicHistogramSeries(lset labels.Labels) bool {
if p.typ != model.MetricTypeHistogram {
return false
}
mName := lset.Get(labels.MetricName) mName := lset.Get(labels.MetricName)
if convertnhcb.GetHistogramMetricBaseName(mName) != string(p.bType) {
return false
}
switch { switch {
case strings.HasSuffix(mName, "_bucket") && lset.Has(labels.BucketLabel): case strings.HasSuffix(mName, "_bucket") && lset.Has(labels.BucketLabel):
le, err := strconv.ParseFloat(lset.Get(labels.BucketLabel), 64) le, err := strconv.ParseFloat(lset.Get(labels.BucketLabel), 64)

@ -3377,7 +3377,7 @@ func TestConvertClassicHistograms(t *testing.T) {
return fmt.Sprintf(` return fmt.Sprintf(`
# HELP %s some help text # HELP %s some help text
# TYPE %s counter # TYPE %s counter
%s %d %s{address="0.0.0.0",port="5001"} %d
`, name, name, name, value) `, name, name, name, value)
} else { } else {
return fmt.Sprintf(` return fmt.Sprintf(`
@ -3420,6 +3420,14 @@ name: "%s"
help: "some help text" help: "some help text"
type: COUNTER type: COUNTER
metric: < metric: <
label: <
name: "address"
value: "0.0.0.0"
>
label: <
name: "port"
value: "5001"
>
counter: < counter: <
value: %d value: %d
> >
@ -3519,30 +3527,57 @@ metric: <
"text": { "text": {
text: []string{ text: []string{
genTestCounterText("test_metric_1", 1, true), genTestCounterText("test_metric_1", 1, true),
genTestCounterText("test_metric_1_count", 1, true),
genTestCounterText("test_metric_1_sum", 1, true),
genTestCounterText("test_metric_1_bucket", 1, true),
genTestHistText("test_histogram_1", true), genTestHistText("test_histogram_1", true),
genTestCounterText("test_metric_2", 1, true), genTestCounterText("test_metric_2", 1, true),
genTestCounterText("test_metric_2_count", 1, true),
genTestCounterText("test_metric_2_sum", 1, true),
genTestCounterText("test_metric_2_bucket", 1, true),
genTestHistText("test_histogram_2", true), genTestHistText("test_histogram_2", true),
genTestCounterText("test_metric_3", 1, true), genTestCounterText("test_metric_3", 1, true),
genTestCounterText("test_metric_3_count", 1, true),
genTestCounterText("test_metric_3_sum", 1, true),
genTestCounterText("test_metric_3_bucket", 1, true),
genTestHistText("test_histogram_3", true), genTestHistText("test_histogram_3", true),
}, },
}, },
"text, no metadata, in different order": { "text, in different order": {
text: []string{ text: []string{
genTestCounterText("test_metric_1", 1, false), genTestCounterText("test_metric_1", 1, true),
genTestHistText("test_histogram_1", false), genTestCounterText("test_metric_1_count", 1, true),
genTestCounterText("test_metric_2", 1, false), genTestCounterText("test_metric_1_sum", 1, true),
genTestHistText("test_histogram_2", false), genTestCounterText("test_metric_1_bucket", 1, true),
genTestHistText("test_histogram_3", false), genTestHistText("test_histogram_1", true),
genTestCounterText("test_metric_3", 1, false), genTestCounterText("test_metric_2", 1, true),
genTestCounterText("test_metric_2_count", 1, true),
genTestCounterText("test_metric_2_sum", 1, true),
genTestCounterText("test_metric_2_bucket", 1, true),
genTestHistText("test_histogram_2", true),
genTestHistText("test_histogram_3", true),
genTestCounterText("test_metric_3", 1, true),
genTestCounterText("test_metric_3_count", 1, true),
genTestCounterText("test_metric_3_sum", 1, true),
genTestCounterText("test_metric_3_bucket", 1, true),
}, },
}, },
"protobuf": { "protobuf": {
text: []string{ text: []string{
genTestCounterProto("test_metric_1", 1), genTestCounterProto("test_metric_1", 1),
genTestCounterProto("test_metric_1_count", 1),
genTestCounterProto("test_metric_1_sum", 1),
genTestCounterProto("test_metric_1_bucket", 1),
genTestHistProto("test_histogram_1", true, false), genTestHistProto("test_histogram_1", true, false),
genTestCounterProto("test_metric_2", 1), genTestCounterProto("test_metric_2", 1),
genTestCounterProto("test_metric_2_count", 1),
genTestCounterProto("test_metric_2_sum", 1),
genTestCounterProto("test_metric_2_bucket", 1),
genTestHistProto("test_histogram_2", true, false), genTestHistProto("test_histogram_2", true, false),
genTestCounterProto("test_metric_3", 1), genTestCounterProto("test_metric_3", 1),
genTestCounterProto("test_metric_3_count", 1),
genTestCounterProto("test_metric_3_sum", 1),
genTestCounterProto("test_metric_3_bucket", 1),
genTestHistProto("test_histogram_3", true, false), genTestHistProto("test_histogram_3", true, false),
}, },
contentType: "application/vnd.google.protobuf", contentType: "application/vnd.google.protobuf",
@ -3551,20 +3586,38 @@ metric: <
text: []string{ text: []string{
genTestHistProto("test_histogram_1", true, false), genTestHistProto("test_histogram_1", true, false),
genTestCounterProto("test_metric_1", 1), genTestCounterProto("test_metric_1", 1),
genTestCounterProto("test_metric_1_count", 1),
genTestCounterProto("test_metric_1_sum", 1),
genTestCounterProto("test_metric_1_bucket", 1),
genTestHistProto("test_histogram_2", true, false), genTestHistProto("test_histogram_2", true, false),
genTestCounterProto("test_metric_2", 1), genTestCounterProto("test_metric_2", 1),
genTestCounterProto("test_metric_2_count", 1),
genTestCounterProto("test_metric_2_sum", 1),
genTestCounterProto("test_metric_2_bucket", 1),
genTestHistProto("test_histogram_3", true, false), genTestHistProto("test_histogram_3", true, false),
genTestCounterProto("test_metric_3", 1), genTestCounterProto("test_metric_3", 1),
genTestCounterProto("test_metric_3_count", 1),
genTestCounterProto("test_metric_3_sum", 1),
genTestCounterProto("test_metric_3_bucket", 1),
}, },
contentType: "application/vnd.google.protobuf", contentType: "application/vnd.google.protobuf",
}, },
"protobuf, with native exponential histogram": { "protobuf, with native exponential histogram": {
text: []string{ text: []string{
genTestCounterProto("test_metric_1", 1), genTestCounterProto("test_metric_1", 1),
genTestCounterProto("test_metric_1_count", 1),
genTestCounterProto("test_metric_1_sum", 1),
genTestCounterProto("test_metric_1_bucket", 1),
genTestHistProto("test_histogram_1", true, true), genTestHistProto("test_histogram_1", true, true),
genTestCounterProto("test_metric_2", 1), genTestCounterProto("test_metric_2", 1),
genTestCounterProto("test_metric_2_count", 1),
genTestCounterProto("test_metric_2_sum", 1),
genTestCounterProto("test_metric_2_bucket", 1),
genTestHistProto("test_histogram_2", true, true), genTestHistProto("test_histogram_2", true, true),
genTestCounterProto("test_metric_3", 1), genTestCounterProto("test_metric_3", 1),
genTestCounterProto("test_metric_3_count", 1),
genTestCounterProto("test_metric_3_sum", 1),
genTestCounterProto("test_metric_3_bucket", 1),
genTestHistProto("test_histogram_3", true, true), genTestHistProto("test_histogram_3", true, true),
}, },
contentType: "application/vnd.google.protobuf", contentType: "application/vnd.google.protobuf",
@ -3767,12 +3820,21 @@ metric: <
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_metric_%d", i))) series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_metric_%d", i)))
checkFloatSeries(series, 1, 1.) checkFloatSeries(series, 1, 1.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_sum", i))) series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_metric_%d_count", i)))
checkFloatSeries(series, tc.expectedClassicHistCount, 10.) checkFloatSeries(series, 1, 1.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_metric_%d_sum", i)))
checkFloatSeries(series, 1, 1.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_metric_%d_bucket", i)))
checkFloatSeries(series, 1, 1.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_count", i))) series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_count", i)))
checkFloatSeries(series, tc.expectedClassicHistCount, 1.) checkFloatSeries(series, tc.expectedClassicHistCount, 1.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_sum", i)))
checkFloatSeries(series, tc.expectedClassicHistCount, 10.)
series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_bucket", i))) series = q.Select(ctx, false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", fmt.Sprintf("test_histogram_%d_bucket", i)))
checkBucketValues(tc.expectedClassicHistCount, metricsText.contentType, series) checkBucketValues(tc.expectedClassicHistCount, metricsText.contentType, series)

@ -164,8 +164,7 @@ func GetHistogramMetricBase(m labels.Labels, suffix string) labels.Labels {
Labels() Labels()
} }
func GetHistogramMetricBaseName(m labels.Labels) string { func GetHistogramMetricBaseName(s string) string {
s := m.Get(labels.MetricName)
for _, rep := range histogramNameSuffixReplacements { for _, rep := range histogramNameSuffixReplacements {
s = rep.pattern.ReplaceAllString(s, rep.repl) s = rep.pattern.ReplaceAllString(s, rep.repl)
} }

Loading…
Cancel
Save