diff --git a/receiver/clickhousesystemtablesreceiver/receiver.go b/receiver/clickhousesystemtablesreceiver/receiver.go index 3a650284..fc014025 100644 --- a/receiver/clickhousesystemtablesreceiver/receiver.go +++ b/receiver/clickhousesystemtablesreceiver/receiver.go @@ -156,7 +156,12 @@ func (r *systemTablesReceiver) scrapeQueryLogIfReady(ctx context.Context) ( // For example, this can happen if this was the first successful scrape // after several failed attempts and subsequent waits for r.ScrapeIntervalSeconds nextScrapeMinServerTs := r.nextScrapeIntervalStartTs + r.scrapeIntervalSeconds + r.scrapeDelaySeconds - nextWaitSeconds := max(0, nextScrapeMinServerTs-serverTsNow) + + nextWaitSeconds := uint32(0) + if nextScrapeMinServerTs > serverTsNow { + // Do the subtraction only if it will not lead to an overflow/wrap around + nextWaitSeconds = nextScrapeMinServerTs - serverTsNow + } return nextWaitSeconds, nil } diff --git a/receiver/clickhousesystemtablesreceiver/receiver_test.go b/receiver/clickhousesystemtablesreceiver/receiver_test.go index b5ee6206..ea0c679d 100644 --- a/receiver/clickhousesystemtablesreceiver/receiver_test.go +++ b/receiver/clickhousesystemtablesreceiver/receiver_test.go @@ -127,4 +127,14 @@ func TestReceiver(t *testing.T) { et, exists := lr.Attributes().Get("event_time") require.True(exists) require.Equal(et.Str(), testQlEventTime.Format(time.RFC3339)) + + // should scrape again immediately if scrape is too far behind the server ts + // for example: this can happen if clickhouse goes down for some time + mockQuerrier.tsNow += 10 * testScrapeIntervalSeconds + testQl4 := makeTestQueryLog("host-4", time.Now(), "test query 4") + mockQuerrier.nextScrapeResult = []QueryLog{testQl4} + + waitSeconds, err = testReceiver.scrapeQueryLogIfReady(context.Background()) + require.Nil(err) + require.Equal(uint32(0), waitSeconds) }