Merge pull request #13914 from bboreham/scrape-reload-metric
Some checks are pending
buf.build / lint and publish (push) Waiting to run
CI / Go tests (push) Waiting to run
CI / More Go tests (push) Waiting to run
CI / Go tests with previous Go version (push) Waiting to run
CI / UI tests (push) Waiting to run
CI / Go tests on Windows (push) Waiting to run
CI / Mixins tests (push) Waiting to run
CI / Build Prometheus for common architectures (0) (push) Waiting to run
CI / Build Prometheus for common architectures (1) (push) Waiting to run
CI / Build Prometheus for common architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (0) (push) Waiting to run
CI / Build Prometheus for all architectures (1) (push) Waiting to run
CI / Build Prometheus for all architectures (10) (push) Waiting to run
CI / Build Prometheus for all architectures (11) (push) Waiting to run
CI / Build Prometheus for all architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (3) (push) Waiting to run
CI / Build Prometheus for all architectures (4) (push) Waiting to run
CI / Build Prometheus for all architectures (5) (push) Waiting to run
CI / Build Prometheus for all architectures (6) (push) Waiting to run
CI / Build Prometheus for all architectures (7) (push) Waiting to run
CI / Build Prometheus for all architectures (8) (push) Waiting to run
CI / Build Prometheus for all architectures (9) (push) Waiting to run
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions
CI / Check generated parser (push) Waiting to run
CI / golangci-lint (push) Waiting to run
CI / fuzzing (push) Waiting to run
CI / codeql (push) Waiting to run
CI / Publish main branch artifacts (push) Blocked by required conditions
CI / Publish release artefacts (push) Blocked by required conditions
CI / Publish UI on npm Registry (push) Blocked by required conditions
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

[REFACTOR] Scraping: rename variables for clarity
This commit is contained in:
Bryan Boreham 2024-12-02 17:09:25 +00:00 committed by GitHub
commit 48287b15d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 7 deletions

View file

@ -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,
})

View file

@ -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) {