From f46a73dcdd36ec7770ce7bf8ffa26bd9199a7502 Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@gmail.com>
Date: Thu, 19 Aug 2021 14:18:02 +0300
Subject: [PATCH] lib/promscrape: use scrapeTimestamp when storing stale
 markers for failed scrape

This will make timestamps for stale markers more consistent for timestamps for other samples
---
 lib/promscrape/scrapework.go | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lib/promscrape/scrapework.go b/lib/promscrape/scrapework.go
index 87bc291a45..b000dee90e 100644
--- a/lib/promscrape/scrapework.go
+++ b/lib/promscrape/scrapework.go
@@ -239,7 +239,8 @@ func (sw *scrapeWork) run(stopCh <-chan struct{}) {
 		timestamp += scrapeInterval.Milliseconds()
 		select {
 		case <-stopCh:
-			sw.sendStaleMarkers(false)
+			t := time.Now().UnixNano() / 1e6
+			sw.sendStaleMarkers(t, false)
 			return
 		case tt := <-ticker.C:
 			t := tt.UnixNano() / 1e6
@@ -323,7 +324,7 @@ func (sw *scrapeWork) scrapeInternal(scrapeTimestamp, realTimestamp int64) error
 	sw.addAutoTimeseries(wc, "scrape_samples_post_metric_relabeling", float64(samplesPostRelabeling), scrapeTimestamp)
 	sw.addAutoTimeseries(wc, "scrape_series_added", float64(seriesAdded), scrapeTimestamp)
 	if up == 0 {
-		sw.sendStaleMarkers(true)
+		sw.sendStaleMarkers(scrapeTimestamp, true)
 	}
 	sw.updateActiveSeries(wc)
 	sw.pushData(&wc.writeRequest)
@@ -520,12 +521,12 @@ func (sw *scrapeWork) updateActiveSeries(wc *writeRequestCtx) {
 	sw.activeSeries = as
 }
 
-func (sw *scrapeWork) sendStaleMarkers(skipAutogenSeries bool) {
+func (sw *scrapeWork) sendStaleMarkers(timestamp int64, skipAutogenSeries bool) {
 	series := make([]prompbmarshal.TimeSeries, 0, len(sw.activeSeries))
 	staleMarkSamples := []prompbmarshal.Sample{
 		{
 			Value:     decimal.StaleNaN,
-			Timestamp: time.Now().UnixNano() / 1e6,
+			Timestamp: timestamp,
 		},
 	}
 	for _, b := range sw.activeSeries {