From 536a487af456abc66b90a4307388619d26209034 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 13 Jul 2023 14:27:51 +0200 Subject: [PATCH] scrape: Refactor names of float samples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Continue to remove confusion that histogram samples are also samples and histogram values are also values etc. by renaming float values and float samples using the same schema as for histograms. Concretely: - result → resultFloats (corresponding to resultHistograms) - pendingResult → pendingFloats (corresponding to pendingHistograms) - rolledbackResult → rolledbackFloats (corresponding to rolledbackHistograms) - sample → floatSample (corresponding to histogramSample) This also order the fields in `collectResultAppender` more consistently. Signed-off-by: beorn7 --- scrape/helpers_test.go | 40 ++++++------ scrape/scrape_test.go | 134 ++++++++++++++++++++--------------------- 2 files changed, 87 insertions(+), 87 deletions(-) diff --git a/scrape/helpers_test.go b/scrape/helpers_test.go index 4862886e5..c580a5051 100644 --- a/scrape/helpers_test.go +++ b/scrape/helpers_test.go @@ -53,10 +53,10 @@ func (a nopAppender) UpdateMetadata(storage.SeriesRef, labels.Labels, metadata.M func (a nopAppender) Commit() error { return nil } func (a nopAppender) Rollback() error { return nil } -type sample struct { +type floatSample struct { metric labels.Labels t int64 - v float64 + f float64 } type histogramSample struct { @@ -69,23 +69,23 @@ type histogramSample struct { // It can be used as its zero value or be backed by another appender it writes samples through. type collectResultAppender struct { next storage.Appender - result []sample - pendingResult []sample - rolledbackResult []sample - pendingExemplars []exemplar.Exemplar - resultExemplars []exemplar.Exemplar + resultFloats []floatSample + pendingFloats []floatSample + rolledbackFloats []floatSample resultHistograms []histogramSample pendingHistograms []histogramSample rolledbackHistograms []histogramSample - pendingMetadata []metadata.Metadata + resultExemplars []exemplar.Exemplar + pendingExemplars []exemplar.Exemplar resultMetadata []metadata.Metadata + pendingMetadata []metadata.Metadata } func (a *collectResultAppender) Append(ref storage.SeriesRef, lset labels.Labels, t int64, v float64) (storage.SeriesRef, error) { - a.pendingResult = append(a.pendingResult, sample{ + a.pendingFloats = append(a.pendingFloats, floatSample{ metric: lset, t: t, - v: v, + f: v, }) if ref == 0 { @@ -133,11 +133,11 @@ func (a *collectResultAppender) UpdateMetadata(ref storage.SeriesRef, l labels.L } func (a *collectResultAppender) Commit() error { - a.result = append(a.result, a.pendingResult...) + a.resultFloats = append(a.resultFloats, a.pendingFloats...) a.resultExemplars = append(a.resultExemplars, a.pendingExemplars...) a.resultHistograms = append(a.resultHistograms, a.pendingHistograms...) a.resultMetadata = append(a.resultMetadata, a.pendingMetadata...) - a.pendingResult = nil + a.pendingFloats = nil a.pendingExemplars = nil a.pendingHistograms = nil a.pendingMetadata = nil @@ -148,9 +148,9 @@ func (a *collectResultAppender) Commit() error { } func (a *collectResultAppender) Rollback() error { - a.rolledbackResult = a.pendingResult + a.rolledbackFloats = a.pendingFloats a.rolledbackHistograms = a.pendingHistograms - a.pendingResult = nil + a.pendingFloats = nil a.pendingHistograms = nil if a.next == nil { return nil @@ -160,14 +160,14 @@ func (a *collectResultAppender) Rollback() error { func (a *collectResultAppender) String() string { var sb strings.Builder - for _, s := range a.result { - sb.WriteString(fmt.Sprintf("committed: %s %f %d\n", s.metric, s.v, s.t)) + for _, s := range a.resultFloats { + sb.WriteString(fmt.Sprintf("committed: %s %f %d\n", s.metric, s.f, s.t)) } - for _, s := range a.pendingResult { - sb.WriteString(fmt.Sprintf("pending: %s %f %d\n", s.metric, s.v, s.t)) + for _, s := range a.pendingFloats { + sb.WriteString(fmt.Sprintf("pending: %s %f %d\n", s.metric, s.f, s.t)) } - for _, s := range a.rolledbackResult { - sb.WriteString(fmt.Sprintf("rolledback: %s %f %d\n", s.metric, s.v, s.t)) + for _, s := range a.rolledbackFloats { + sb.WriteString(fmt.Sprintf("rolledback: %s %f %d\n", s.metric, s.f, s.t)) } return sb.String() } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 72e22fd0d..3f119b94d 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -740,12 +740,12 @@ func TestScrapeLoopStop(t *testing.T) { // We expected 1 actual sample for each scrape plus 5 for report samples. // At least 2 scrapes were made, plus the final stale markers. - if len(appender.result) < 6*3 || len(appender.result)%6 != 0 { - t.Fatalf("Expected at least 3 scrapes with 6 samples each, got %d samples", len(appender.result)) + if len(appender.resultFloats) < 6*3 || len(appender.resultFloats)%6 != 0 { + t.Fatalf("Expected at least 3 scrapes with 6 samples each, got %d samples", len(appender.resultFloats)) } // All samples in a scrape must have the same timestamp. var ts int64 - for i, s := range appender.result { + for i, s := range appender.resultFloats { switch { case i%6 == 0: ts = s.t @@ -754,9 +754,9 @@ func TestScrapeLoopStop(t *testing.T) { } } // All samples from the last scrape must be stale markers. - for _, s := range appender.result[len(appender.result)-5:] { - if !value.IsStaleNaN(s.v) { - t.Fatalf("Appended last sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(s.v)) + for _, s := range appender.resultFloats[len(appender.resultFloats)-5:] { + if !value.IsStaleNaN(s.f) { + t.Fatalf("Appended last sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(s.f)) } } } @@ -1192,10 +1192,10 @@ func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 27, len(appender.result), "Appended samples not as expected:\n%s", appender) - require.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") - require.True(t, value.IsStaleNaN(appender.result[6].v), - "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) + require.Equal(t, 27, len(appender.resultFloats), "Appended samples not as expected:\n%s", appender) + require.Equal(t, 42.0, appender.resultFloats[0].f, "Appended first sample not as expected") + require.True(t, value.IsStaleNaN(appender.resultFloats[6].f), + "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.resultFloats[6].f)) } func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) { @@ -1257,10 +1257,10 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 17, len(appender.result), "Appended samples not as expected:\n%s", appender) - require.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") - require.True(t, value.IsStaleNaN(appender.result[6].v), - "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) + require.Equal(t, 17, len(appender.resultFloats), "Appended samples not as expected:\n%s", appender) + require.Equal(t, 42.0, appender.resultFloats[0].f, "Appended first sample not as expected") + require.True(t, value.IsStaleNaN(appender.resultFloats[6].f), + "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.resultFloats[6].f)) } func TestScrapeLoopCache(t *testing.T) { @@ -1342,7 +1342,7 @@ func TestScrapeLoopCache(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 26, len(appender.result), "Appended samples not as expected:\n%s", appender) + require.Equal(t, 26, len(appender.resultFloats), "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) { @@ -1501,11 +1501,11 @@ func TestScrapeLoopAppend(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - expected := []sample{ + expected := []floatSample{ { metric: test.expLset, t: timestamp.FromTime(now), - v: test.expValue, + f: test.expValue, }, } @@ -1513,11 +1513,11 @@ func TestScrapeLoopAppend(t *testing.T) { // DeepEqual will report NaNs as being different, // so replace it with the expected one. if test.expValue == float64(value.NormalNaN) { - app.result[0].v = expected[0].v + app.resultFloats[0].f = expected[0].f } t.Logf("Test:%s", test.title) - require.Equal(t, expected, app.result) + require.Equal(t, expected, app.resultFloats) } } @@ -1587,13 +1587,13 @@ func TestScrapeLoopAppendForConflictingPrefixedLabels(t *testing.T) { require.NoError(t, slApp.Commit()) - require.Equal(t, []sample{ + require.Equal(t, []floatSample{ { metric: labels.FromStrings(tc.expected...), t: timestamp.FromTime(time.Date(2000, 1, 1, 1, 0, 0, 0, time.UTC)), - v: 0, + f: 0, }, - }, app.result) + }, app.resultFloats) }) } } @@ -1641,15 +1641,15 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - expected := []sample{ + expected := []floatSample{ { metric: lset, t: timestamp.FromTime(now), - v: expValue, + f: expValue, }, } - require.Equal(t, expected, app.result) + require.Equal(t, expected, app.resultFloats) } func TestScrapeLoopAppendSampleLimit(t *testing.T) { @@ -1709,14 +1709,14 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { require.Equal(t, 1.0, change, "Unexpected change of sample limit metric: %f", change) // And verify that we got the samples that fit under the limit. - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), t: timestamp.FromTime(now), - v: 1, + f: 1, }, } - require.Equal(t, want, resApp.rolledbackResult, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, resApp.rolledbackFloats, "Appended samples not as expected:\n%s", appender) now = time.Now() slApp = sl.appender(context.Background()) @@ -1869,19 +1869,19 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) { require.NoError(t, slApp.Commit()) // DeepEqual will report NaNs as being different, so replace with a different value. - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"), t: timestamp.FromTime(now), - v: 1, + f: 1, }, { metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"), t: timestamp.FromTime(now.Add(time.Minute)), - v: 2, + f: 2, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, capp.resultFloats, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendStaleness(t *testing.T) { @@ -1917,24 +1917,24 @@ func TestScrapeLoopAppendStaleness(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - ingestedNaN := math.Float64bits(app.result[1].v) + ingestedNaN := math.Float64bits(app.resultFloats[1].f) require.Equal(t, value.StaleNaN, ingestedNaN, "Appended stale sample wasn't as expected") // DeepEqual will report NaNs as being different, so replace with a different value. - app.result[1].v = 42 - want := []sample{ + app.resultFloats[1].f = 42 + want := []floatSample{ { metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), t: timestamp.FromTime(now), - v: 1, + f: 1, }, { metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), t: timestamp.FromTime(now.Add(time.Second)), - v: 42, + f: 42, }, } - require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { @@ -1969,14 +1969,14 @@ func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), t: 1000, - v: 1, + f: 1, }, } - require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendExemplar(t *testing.T) { @@ -1985,7 +1985,7 @@ func TestScrapeLoopAppendExemplar(t *testing.T) { scrapeText string contentType string discoveryLabels []string - floats []sample + floats []floatSample histograms []histogramSample exemplars []exemplar.Exemplar }{ @@ -1994,9 +1994,9 @@ func TestScrapeLoopAppendExemplar(t *testing.T) { scrapeText: "metric_total{n=\"1\"} 0\n# EOF", contentType: "application/openmetrics-text", discoveryLabels: []string{"n", "2"}, - floats: []sample{{ + floats: []floatSample{{ metric: labels.FromStrings("__name__", "metric_total", "exported_n", "1", "n", "2"), - v: 0, + f: 0, }}, }, { @@ -2004,9 +2004,9 @@ func TestScrapeLoopAppendExemplar(t *testing.T) { scrapeText: "metric_total{n=\"1\"} 0 # {a=\"abc\"} 1.0\n# EOF", contentType: "application/openmetrics-text", discoveryLabels: []string{"n", "2"}, - floats: []sample{{ + floats: []floatSample{{ metric: labels.FromStrings("__name__", "metric_total", "exported_n", "1", "n", "2"), - v: 0, + f: 0, }}, exemplars: []exemplar.Exemplar{ {Labels: labels.FromStrings("a", "abc"), Value: 1}, @@ -2017,9 +2017,9 @@ func TestScrapeLoopAppendExemplar(t *testing.T) { scrapeText: "metric_total{n=\"1\"} 0 # {a=\"abc\"} 1.0 10000\n# EOF", contentType: "application/openmetrics-text", discoveryLabels: []string{"n", "2"}, - floats: []sample{{ + floats: []floatSample{{ metric: labels.FromStrings("__name__", "metric_total", "exported_n", "1", "n", "2"), - v: 0, + f: 0, }}, exemplars: []exemplar.Exemplar{ {Labels: labels.FromStrings("a", "abc"), Value: 1, Ts: 10000000, HasTs: true}, @@ -2031,12 +2031,12 @@ func TestScrapeLoopAppendExemplar(t *testing.T) { metric_total{n="2"} 2 # {t="2"} 2.0 20000 # EOF`, contentType: "application/openmetrics-text", - floats: []sample{{ + floats: []floatSample{{ metric: labels.FromStrings("__name__", "metric_total", "n", "1"), - v: 1, + f: 1, }, { metric: labels.FromStrings("__name__", "metric_total", "n", "2"), - v: 2, + f: 2, }}, exemplars: []exemplar.Exemplar{ {Labels: labels.FromStrings("t", "1"), Value: 1, Ts: 10000000, HasTs: true}, @@ -2208,7 +2208,7 @@ metric: < _, _, _, err := sl.append(app, buf.Bytes(), test.contentType, now) require.NoError(t, err) require.NoError(t, app.Commit()) - require.Equal(t, test.floats, app.result) + require.Equal(t, test.floats, app.resultFloats) require.Equal(t, test.histograms, app.resultHistograms) require.Equal(t, test.exemplars, app.resultExemplars) }) @@ -2219,12 +2219,12 @@ func TestScrapeLoopAppendExemplarSeries(t *testing.T) { scrapeText := []string{`metric_total{n="1"} 1 # {t="1"} 1.0 10000 # EOF`, `metric_total{n="1"} 2 # {t="2"} 2.0 20000 # EOF`} - samples := []sample{{ + samples := []floatSample{{ metric: labels.FromStrings("__name__", "metric_total", "n", "1"), - v: 1, + f: 1, }, { metric: labels.FromStrings("__name__", "metric_total", "n", "1"), - v: 2, + f: 2, }} exemplars := []exemplar.Exemplar{ {Labels: labels.FromStrings("t", "1"), Value: 1, Ts: 10000000, HasTs: true}, @@ -2280,7 +2280,7 @@ func TestScrapeLoopAppendExemplarSeries(t *testing.T) { require.NoError(t, app.Commit()) } - require.Equal(t, samples, app.result) + require.Equal(t, samples, app.resultFloats) require.Equal(t, exemplars, app.resultExemplars) } @@ -2318,7 +2318,7 @@ func TestScrapeLoopRunReportsTargetDownOnScrapeError(t *testing.T) { } sl.run(nil) - require.Equal(t, 0.0, appender.result[0].v, "bad 'up' value") + require.Equal(t, 0.0, appender.resultFloats[0].f, "bad 'up' value") } func TestScrapeLoopRunReportsTargetDownOnInvalidUTF8(t *testing.T) { @@ -2356,7 +2356,7 @@ func TestScrapeLoopRunReportsTargetDownOnInvalidUTF8(t *testing.T) { } sl.run(nil) - require.Equal(t, 0.0, appender.result[0].v, "bad 'up' value") + require.Equal(t, 0.0, appender.resultFloats[0].f, "bad 'up' value") } type errorAppender struct { @@ -2405,14 +2405,14 @@ func TestScrapeLoopAppendGracefullyIfAmendOrOutOfOrderOrOutOfBounds(t *testing.T require.NoError(t, err) require.NoError(t, slApp.Commit()) - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings(model.MetricNameLabel, "normal"), t: timestamp.FromTime(now), - v: 1, + f: 1, }, } - require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender) require.Equal(t, 4, total) require.Equal(t, 4, added) require.Equal(t, 1, seriesAdded) @@ -2724,14 +2724,14 @@ func TestScrapeLoop_RespectTimestamps(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"), t: 0, - v: 1, + f: 1, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, capp.resultFloats, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoop_DiscardTimestamps(t *testing.T) { @@ -2766,14 +2766,14 @@ func TestScrapeLoop_DiscardTimestamps(t *testing.T) { require.NoError(t, err) require.NoError(t, slApp.Commit()) - want := []sample{ + want := []floatSample{ { metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"), t: timestamp.FromTime(now), - v: 1, + f: 1, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) + require.Equal(t, want, capp.resultFloats, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopDiscardDuplicateLabels(t *testing.T) {