MCPcopy
hub / github.com/prometheus/prometheus / testScrapeLoopAppendSampleLimit

Function testScrapeLoopAppendSampleLimit

scrape/scrape_test.go:3184–3252  ·  view source on GitHub ↗
(t *testing.T, appV2 bool)

Source from the content-addressed store, hash-verified

3182}
3183
3184func testScrapeLoopAppendSampleLimit(t *testing.T, appV2 bool) {
3185 appTest := teststorage.NewAppendable()
3186 sl, _ := newTestScrapeLoop(t, func(sl *scrapeLoop) {
3187 if appV2 {
3188 sl.appendableV2 = appendableV2Func(func(ctx context.Context) storage.AppenderV2 {
3189 // Chain appTest to verify what samples passed through.
3190 return &limitAppenderV2{AppenderV2: appTest.AppenderV2(ctx), limit: 1}
3191 })
3192 } else {
3193 sl.appendable = appendableFunc(func(ctx context.Context) storage.Appender {
3194 // Chain appTest to verify what samples passed through.
3195 return &limitAppender{Appender: appTest.Appender(ctx), limit: 1}
3196 })
3197 }
3198
3199 sl.sampleMutator = func(l labels.Labels) labels.Labels {
3200 if l.Has("deleteme") {
3201 return labels.EmptyLabels()
3202 }
3203 return l
3204 }
3205 sl.sampleLimit = 1 // Same as limitAppender.limit
3206 })
3207
3208 // Get the value of the Counter before performing append.
3209 beforeMetric := dto.Metric{}
3210 err := sl.metrics.targetScrapeSampleLimit.Write(&beforeMetric)
3211 require.NoError(t, err)
3212
3213 beforeMetricValue := beforeMetric.GetCounter().GetValue()
3214
3215 now := time.Now()
3216 app := sl.appender()
3217 total, added, seriesAdded, err := app.append([]byte("metric_a 1\nmetric_b 1\nmetric_c 1\n"), "text/plain", now)
3218 require.ErrorIs(t, err, errSampleLimit)
3219 require.NoError(t, app.Rollback())
3220 require.Equal(t, 3, total)
3221 require.Equal(t, 3, added)
3222 require.Equal(t, 1, seriesAdded)
3223
3224 // Check that the Counter has been incremented a single time for the scrape,
3225 // not multiple times for each sample.
3226 metric := dto.Metric{}
3227 err = sl.metrics.targetScrapeSampleLimit.Write(&metric)
3228 require.NoError(t, err)
3229
3230 v := metric.GetCounter().GetValue()
3231 change := v - beforeMetricValue
3232 require.Equal(t, 1.0, change, "Unexpected change of sample limit metric: %f", change)
3233
3234 // And verify that we got the samples that fit under the limit.
3235 want := []sample{
3236 {
3237 L: labels.FromStrings(model.MetricNameLabel, "metric_a"),
3238 T: timestamp.FromTime(now),
3239 V: 1,
3240 },
3241 }

Callers 1

Calls 15

AppenderV2Method · 0.95
AppenderMethod · 0.95
GetCounterMethod · 0.95
RolledbackSamplesMethod · 0.95
NewAppendableFunction · 0.92
EmptyLabelsFunction · 0.92
FromStringsFunction · 0.92
FromTimeFunction · 0.92
RequireEqualFunction · 0.92
newTestScrapeLoopFunction · 0.85
appendableV2FuncFuncType · 0.85
appendableFuncFuncType · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…