Skip to content

fix(agent): Reject negative collection_offset#18574

Merged
skartikey merged 6 commits intoinfluxdata:masterfrom
majiayu000:fix/18175-reject-negative-offset
Mar 26, 2026
Merged

fix(agent): Reject negative collection_offset#18574
skartikey merged 6 commits intoinfluxdata:masterfrom
majiayu000:fix/18175-reject-negative-offset

Conversation

@majiayu000
Copy link
Copy Markdown
Contributor

@majiayu000 majiayu000 commented Mar 20, 2026

Summary

Based on the review feedback from @srebhan on #18178, this PR takes the simpler approach of rejecting invalid offset values at startup rather than normalizing them:

  • Negative agent-level collection_offset → error at startup in runAgent
  • Negative per-input collection_offset → error during config load in buildInput
  • Negative per-input interval → error during config load in buildInput

This avoids the ambiguity of silently normalizing negative offsets, which could mean different things to different users.

Changes

  • Add agent-wide negative collection_offset check in runAgent (cmd/telegraf/telegraf.go), alongside existing interval/flush_interval checks
  • Add per-input negative interval and collection_offset checks in buildInput (config/config.go)
  • No changes to NewTicker signature or behavior

Checklist

Related issues

supersedes #18178
resolves #18175

Validate interval and offset parameters in clock.NewTicker to prevent
misconfiguration. Negative offsets, offsets >= interval, and non-positive
intervals now return an error with a helpful suggestion instead of
causing unexpected behavior like rapid-fire ticks.

Fixes influxdata#18175

Signed-off-by: majiayu000 <1835304752@qq.com>
@telegraf-tiger telegraf-tiger Bot added the fix pr to fix corresponding bug label Mar 20, 2026
Copy link
Copy Markdown
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not in favour of changing the NewTicker footprint, while the checks could be done in other places where it has more context to provide more relevant log messages of where the misconfiguration actually exists.

Comment thread internal/clock/ticker.go Outdated
Comment on lines +28 to +33
if interval <= 0 {
return nil, fmt.Errorf("interval must be positive, got %s", interval)
}
if offset < 0 {
return nil, fmt.Errorf("negative offset %s is not allowed; use a positive offset instead, e.g. interval-offset = %s", offset, interval+offset)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks should be moved to func (c *Config) addInput or func (c *Config) buildInput.

And agent-wide check for interval already exists at func (t *Telegraf) runAgent, so the agent-wide check for negative offset should also be there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — validation is now in buildInput (per-input) and runAgent (agent-wide), not in NewTicker. The NewTicker signature remains *Ticker with no changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — validation is now in buildInput (per-input) and runAgent (agent-wide), not in NewTicker. The NewTicker signature remains *Ticker with no changes.

Comment thread internal/clock/ticker.go Outdated
Comment on lines +34 to +36
if offset >= interval {
return nil, fmt.Errorf("offset %s must be less than interval %s", offset, interval)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel like this is something to be checked in NewTicker, but already before calling this in func (a *Agent) runInputs where you can know the input ID/name and its logger.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — moved the checks out of NewTicker in the previous commit. No validation logic remains in NewTicker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — moved the checks out of NewTicker. Per-input validation is in buildInput where the input name is available for error messages.

…xdata#18574

Move offset/interval checks from NewTicker to where they have more
context: agent-wide collection_offset check in runAgent (alongside
existing interval check), per-input check in buildInput. Revert
NewTicker to its original signature returning *Ticker.

Signed-off-by: majiayu000 <1835304752@qq.com>
Copy link
Copy Markdown
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion to keep error messages short and clean.

Comment thread cmd/telegraf/telegraf.go Outdated
Comment on lines +460 to +463
return fmt.Errorf(
"agent collection_offset must not be negative, found %v; use a positive offset instead (e.g. interval - offset)",
c.Agent.CollectionOffset,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be enough

Suggested change
return fmt.Errorf(
"agent collection_offset must not be negative, found %v; use a positive offset instead (e.g. interval - offset)",
c.Agent.CollectionOffset,
)
return fmt.Errorf("agent collection_offset must not be negative, found %v", c.Agent.CollectionOffset)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — simplified to return fmt.Errorf("agent collection_offset must not be negative, found %v", c.Agent.CollectionOffset) as suggested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — simplified to return fmt.Errorf("agent collection_offset must not be negative, found %v", c.Agent.CollectionOffset) as suggested.

Comment thread config/config.go Outdated
cp.CollectionJitter, _ = c.getFieldDuration(tbl, "collection_jitter")
cp.CollectionOffset, _ = c.getFieldDuration(tbl, "collection_offset")
if cp.CollectionOffset < 0 {
return nil, fmt.Errorf("negative collection_offset %s is not allowed for input %s; use a positive offset instead (e.g. interval - offset)", cp.CollectionOffset, name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("negative collection_offset %s is not allowed for input %s; use a positive offset instead (e.g. interval - offset)", cp.CollectionOffset, name)
return nil, fmt.Errorf("negative collection_offset %q is not allowed for input %s", cp.CollectionOffset, name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — updated to use %q and shortened the message as suggested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — updated to use %q and shortened the message as suggested.

Comment thread config/config.go
@srebhan
Copy link
Copy Markdown
Member

srebhan commented Mar 20, 2026

@majiayu000 please restore the PR description template, especially the AI checkboxes and the "Related issues" section! The template is there for a reason namely to query relevant information we need to process the PR and a common structure so we can machine-read things.

Thank you!

Copy link
Copy Markdown
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @majiayu000! I have two comments in the code...

Comment thread cmd/telegraf/telegraf.go Outdated
Comment thread config/config.go Outdated
- Move agent-level collection_offset validation from runAgent to
  LoadConfigData where other agent table checks belong
- Remove per-input interval check (to be submitted as separate PR)

Signed-off-by: majiayu000 <1835304752@qq.com>
Comment thread config/config.go Outdated
Signed-off-by: majiayu000 <1835304752@qq.com>
Comment thread config/config.go Outdated
The error from buildInput is already prefixed with the input name
by addInput, so including it in the message is redundant.

Signed-off-by: majiayu000 <1835304752@qq.com>
@telegraf-tiger
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @majiayu000!

@srebhan srebhan changed the title fix(agent): reject negative collection_offset with helpful error fix(agent): Reject negative collection_offset Mar 26, 2026
@srebhan srebhan added area/configuration area/agent ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. labels Mar 26, 2026
@skartikey
Copy link
Copy Markdown
Contributor

@majiayu000 Thanks for the contribution!

@skartikey skartikey merged commit ca72998 into influxdata:master Mar 26, 2026
29 checks passed
@github-actions github-actions Bot added this to the v1.38.2 milestone Mar 26, 2026
srebhan pushed a commit that referenced this pull request Mar 30, 2026
Signed-off-by: majiayu000 <1835304752@qq.com>
(cherry picked from commit ca72998)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent area/configuration fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative collection_offset Causes Multiple Executions

5 participants