From 3f6a133f2636a7ccb36efac50102dd7bd5f287f5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 10 Apr 2024 10:38:15 +0100 Subject: [PATCH 1/2] [Test] Scraping: check reload metrics Need to extend `newTestScrapeMetrics`` to get at the Registry. `gatherLabels` function could go upstream to `client_golang`. Signed-off-by: Bryan Boreham --- scrape/scrape_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 02a31b762..2aaf5e8e4 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -65,10 +65,15 @@ func TestMain(m *testing.M) { testutil.TolerantVerifyLeak(m) } -func newTestScrapeMetrics(t testing.TB) *scrapeMetrics { +func newTestRegistryAndScrapeMetrics(t testing.TB) (*prometheus.Registry, *scrapeMetrics) { reg := prometheus.NewRegistry() metrics, err := newScrapeMetrics(reg) require.NoError(t, err) + return reg, metrics +} + +func newTestScrapeMetrics(t testing.TB) *scrapeMetrics { + _, metrics := newTestRegistryAndScrapeMetrics(t) return metrics } @@ -370,6 +375,7 @@ func TestScrapePoolReload(t *testing.T) { return l } + reg, metrics := newTestRegistryAndScrapeMetrics(t) sp := &scrapePool{ appendable: &nopAppendable{}, activeTargets: map[uint64]*Target{}, @@ -377,7 +383,7 @@ func TestScrapePoolReload(t *testing.T) { newLoop: newLoop, logger: nil, client: http.DefaultClient, - metrics: newTestScrapeMetrics(t), + metrics: metrics, symbolTable: labels.NewSymbolTable(), } @@ -432,6 +438,12 @@ func TestScrapePoolReload(t *testing.T) { require.Equal(t, sp.activeTargets, beforeTargets, "Reloading affected target states unexpectedly") require.Len(t, sp.loops, numTargets, "Unexpected number of stopped loops after reload") + + got, err := gatherLabels(reg, "prometheus_target_reload_length_seconds") + require.NoError(t, err) + expectedName, expectedValue := "interval", "3s" + require.Equal(t, [][]*dto.LabelPair{{{Name: &expectedName, Value: &expectedValue}}}, got) + require.Equal(t, 1.0, prom_testutil.ToFloat64(sp.metrics.targetScrapePoolReloads)) } func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) { @@ -447,6 +459,7 @@ func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) { } return l } + reg, metrics := newTestRegistryAndScrapeMetrics(t) sp := &scrapePool{ appendable: &nopAppendable{}, activeTargets: map[uint64]*Target{ @@ -460,7 +473,7 @@ func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) { newLoop: newLoop, logger: nil, client: http.DefaultClient, - metrics: newTestScrapeMetrics(t), + metrics: metrics, symbolTable: labels.NewSymbolTable(), } @@ -468,6 +481,30 @@ func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) { if err != nil { t.Fatalf("unable to reload configuration: %s", err) } + // Check that the reload metric is labeled with the pool interval, not the overridden interval. + got, err := gatherLabels(reg, "prometheus_target_reload_length_seconds") + require.NoError(t, err) + expectedName, expectedValue := "interval", "3s" + require.Equal(t, [][]*dto.LabelPair{{{Name: &expectedName, Value: &expectedValue}}}, got) +} + +// Gather metrics from the provided Gatherer with specified familyName, +// and return all sets of name/value pairs. +func gatherLabels(g prometheus.Gatherer, familyName string) ([][]*dto.LabelPair, error) { + families, err := g.Gather() + if err != nil { + return nil, err + } + ret := make([][]*dto.LabelPair, 0) + for _, f := range families { + if f.GetName() == familyName { + for _, m := range f.GetMetric() { + ret = append(ret, m.GetLabel()) + } + break + } + } + return ret, nil } func TestScrapePoolTargetLimit(t *testing.T) { From 6380229c712aec0a17b38c60e7c79982f5bdc3d6 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 10 Apr 2024 15:39:17 +0100 Subject: [PATCH 2/2] [REFACTOR] Scraping: rename variables for clarity Instead of shadowing the outer variables, use different names. Signed-off-by: Bryan Boreham --- scrape/scrape.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scrape/scrape.go b/scrape/scrape.go index c5e344982..d1b389a77 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -350,12 +350,12 @@ func (sp *scrapePool) restartLoops(reuseCache bool) { } t := sp.activeTargets[fp] - interval, timeout, err := t.intervalAndTimeout(interval, timeout) + targetInterval, targetTimeout, err := t.intervalAndTimeout(interval, timeout) var ( s = &targetScraper{ Target: t, client: sp.client, - timeout: timeout, + timeout: targetTimeout, bodySizeLimit: bodySizeLimit, acceptHeader: acceptHeader(sp.config.ScrapeProtocols, validationScheme), acceptEncodingHeader: acceptEncodingHeader(enableCompression), @@ -373,8 +373,8 @@ func (sp *scrapePool) restartLoops(reuseCache bool) { trackTimestampsStaleness: trackTimestampsStaleness, mrc: mrc, cache: cache, - interval: interval, - timeout: timeout, + interval: targetInterval, + timeout: targetTimeout, validationScheme: validationScheme, fallbackScrapeProtocol: fallbackScrapeProtocol, })