MCPcopy
hub / github.com/prometheus/prometheus / runScrapeLoopTest

Function runScrapeLoopTest

scrape/scrape_test.go:151–217  ·  view source on GitHub ↗
(t *testing.T, appV2 bool, s *teststorage.TestStorage, expectOutOfOrder bool)

Source from the content-addressed store, hash-verified

149}
150
151func runScrapeLoopTest(t *testing.T, appV2 bool, s *teststorage.TestStorage, expectOutOfOrder bool) {
152 sl, _ := newTestScrapeLoop(t, withAppendable(s, appV2))
153
154 // Current time for generating timestamps.
155 now := time.Now()
156
157 // Calculate timestamps for the samples based on the current time.
158 now = now.Truncate(time.Minute) // round down the now timestamp to the nearest minute
159 timestampInorder1 := now
160 timestampOutOfOrder := now.Add(-5 * time.Minute)
161 timestampInorder2 := now.Add(5 * time.Minute)
162
163 app := sl.appender()
164 _, _, _, err := app.append([]byte(`metric_total{a="1",b="1"} 1`), "text/plain", timestampInorder1)
165 require.NoError(t, err)
166
167 _, _, _, err = app.append([]byte(`metric_total{a="1",b="1"} 2`), "text/plain", timestampOutOfOrder)
168 require.NoError(t, err)
169
170 _, _, _, err = app.append([]byte(`metric_total{a="1",b="1"} 3`), "text/plain", timestampInorder2)
171 require.NoError(t, err)
172
173 require.NoError(t, app.Commit())
174
175 // Query the samples back from the storage.
176 q, err := s.Querier(time.Time{}.UnixNano(), time.Now().UnixNano())
177 require.NoError(t, err)
178 t.Cleanup(func() { _ = q.Close() })
179
180 // Use a matcher to filter the metric name.
181 series := q.Select(t.Context(), false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", "metric_total"))
182
183 var results []sample
184 for series.Next() {
185 it := series.At().Iterator(nil)
186 for it.Next() == chunkenc.ValFloat {
187 t, v := it.At()
188 results = append(results, sample{
189 L: series.At().Labels(),
190 T: t,
191 V: v,
192 })
193 }
194 require.NoError(t, it.Err())
195 }
196 require.NoError(t, series.Err())
197
198 // Define the expected results
199 want := []sample{
200 {
201 L: labels.FromStrings("__name__", "metric_total", "a", "1", "b", "1"),
202 T: timestamp.FromTime(timestampInorder1),
203 V: 1,
204 },
205 {
206 L: labels.FromStrings("__name__", "metric_total", "a", "1", "b", "1"),
207 T: timestamp.FromTime(timestampInorder2),
208 V: 3,

Calls 15

MustNewMatcherFunction · 0.92
FromStringsFunction · 0.92
FromTimeFunction · 0.92
RequireNotEqualFunction · 0.92
RequireEqualFunction · 0.92
newTestScrapeLoopFunction · 0.85
withAppendableFunction · 0.85
AddMethod · 0.65
appendMethod · 0.65
CommitMethod · 0.65
QuerierMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…