Scrape: defer report
When I started wotking on target_limit, scrapeAndReport did not exist
yet. Then I simply rebased my work without thinking.
It appears that there is a lot that can be inline if I defer() the
report.
@@ -1033,11 +1033,13 @@ func (sl *scrapeLoop) scrapeAndReport(interval, timeout time.Duration, last time
}
b := sl.buffers.Get(sl.lastScrapeSize).([]byte)
+ defer sl.buffers.Put(b)
buf := bytes.NewBuffer(b)
- app := sl.appender(sl.ctx)
var total, added, seriesAdded int
var err, appErr, scrapeErr error
+
+ app := sl.appender(sl.ctx)
defer func() {
if err != nil {
app.Rollback()
@@ -1048,8 +1050,15 @@ func (sl *scrapeLoop) scrapeAndReport(interval, timeout time.Duration, last time
level.Error(sl.l).Log("msg", "Scrape commit failed", "err", err)
}
}()
+
+ defer func() {
+ if err = sl.report(app, start, time.Since(start), total, added, seriesAdded, scrapeErr); err != nil {
+ level.Warn(sl.l).Log("msg", "Appending scrape report failed", "err", err)
+ }
+ }()
+
if forcedErr := sl.getForcedError(); forcedErr != nil {
- appErr = forcedErr
+ scrapeErr = forcedErr
// Add stale markers.
if _, _, _, err := sl.append(app, []byte{}, "", start); err != nil {
app.Rollback()
@@ -1059,7 +1068,10 @@ func (sl *scrapeLoop) scrapeAndReport(interval, timeout time.Duration, last time
if errc != nil {
errc <- forcedErr
}
- } else {
+
+ return start
+ }
+
var contentType string
scrapeCtx, cancel := context.WithTimeout(sl.ctx, timeout)
contentType, scrapeErr = sl.scraper.scrape(scrapeCtx, buf)
@@ -1095,17 +1107,11 @@ func (sl *scrapeLoop) scrapeAndReport(interval, timeout time.Duration, last time
level.Warn(sl.l).Log("msg", "Append failed", "err", err)
}
}
- }
-
- sl.buffers.Put(b)
if scrapeErr == nil {
scrapeErr = appErr
}
- if err = sl.report(app, start, time.Since(start), total, added, seriesAdded, scrapeErr); err != nil {
- level.Warn(sl.l).Log("msg", "Appending scrape report failed", "err", err)
- }
return start
}
