Skip to content

Commit

Permalink
[receiver/scraperhelper] exit before scraping if the receiver was shu…
Browse files Browse the repository at this point in the history
…tdown (#11632)

#### Description
exit before scraping if the receiver was shutdown, do not wait the
initial delay.
  • Loading branch information
atoulme authored Nov 12, 2024
1 parent 45f1fba commit 6a276ba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/initial_delay_shutdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: scraperhelper

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: If the scraper shuts down, do not scrape first.

# One or more tracking issues or pull requests related to the change
issues: [11632]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
When the scraper is shutting down, it currently will scrape at least once.
With this change, upon receiving a shutdown order, the receiver's scraperhelper will exit immediately.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
7 changes: 6 additions & 1 deletion receiver/scraperhelper/scrapercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ func (sc *controller) Shutdown(ctx context.Context) error {
func (sc *controller) startScraping() {
go func() {
if sc.initialDelay > 0 {
<-time.After(sc.initialDelay)
select {
case <-time.After(sc.initialDelay):
case <-sc.done:
sc.terminated <- struct{}{}
return
}
}

if sc.tickerCh == nil {
Expand Down
34 changes: 34 additions & 0 deletions receiver/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,37 @@ func TestScrapeControllerInitialDelay(t *testing.T) {

assert.NoError(t, r.Shutdown(context.Background()), "Must not error closing down")
}

func TestShutdownBeforeScrapeCanStart(t *testing.T) {
cfg := ControllerConfig{
CollectionInterval: time.Second,
InitialDelay: 5 * time.Second,
}

scp, err := NewScraper(component.MustNewType("timed"), func(context.Context) (pmetric.Metrics, error) {
// make the scraper wait for long enough it would disrupt a shutdown.
time.Sleep(30 * time.Second)
return pmetric.NewMetrics(), nil
})
require.NoError(t, err, "Must not error when creating scraper")

r, err := NewScraperControllerReceiver(
&cfg,
receivertest.NewNopSettings(),
new(consumertest.MetricsSink),
AddScraper(scp),
)
require.NoError(t, err, "Must not error when creating receiver")
require.NoError(t, r.Start(context.Background(), componenttest.NewNopHost()))
shutdown := make(chan struct{}, 1)
go func() {
assert.NoError(t, r.Shutdown(context.Background()))
close(shutdown)
}()
timer := time.NewTicker(10 * time.Second)
select {
case <-timer.C:
require.Fail(t, "shutdown should not wait for scraping")
case <-shutdown:
}
}

0 comments on commit 6a276ba

Please sign in to comment.