fix(agent): Reject negative collection_offset#18574
fix(agent): Reject negative collection_offset#18574skartikey merged 6 commits intoinfluxdata:masterfrom
Conversation
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>
Hipska
left a comment
There was a problem hiding this comment.
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.
| 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) | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done — validation is now in buildInput (per-input) and runAgent (agent-wide), not in NewTicker. The NewTicker signature remains *Ticker with no changes.
There was a problem hiding this comment.
Done — validation is now in buildInput (per-input) and runAgent (agent-wide), not in NewTicker. The NewTicker signature remains *Ticker with no changes.
| if offset >= interval { | ||
| return nil, fmt.Errorf("offset %s must be less than interval %s", offset, interval) | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed — moved the checks out of NewTicker in the previous commit. No validation logic remains in NewTicker.
There was a problem hiding this comment.
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>
Hipska
left a comment
There was a problem hiding this comment.
Just a suggestion to keep error messages short and clean.
| return fmt.Errorf( | ||
| "agent collection_offset must not be negative, found %v; use a positive offset instead (e.g. interval - offset)", | ||
| c.Agent.CollectionOffset, | ||
| ) |
There was a problem hiding this comment.
This should be enough
| 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) |
There was a problem hiding this comment.
Done — simplified to return fmt.Errorf("agent collection_offset must not be negative, found %v", c.Agent.CollectionOffset) as suggested.
There was a problem hiding this comment.
Done — simplified to return fmt.Errorf("agent collection_offset must not be negative, found %v", c.Agent.CollectionOffset) as suggested.
| 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) |
There was a problem hiding this comment.
| 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) |
There was a problem hiding this comment.
Done — updated to use %q and shortened the message as suggested.
There was a problem hiding this comment.
Done — updated to use %q and shortened the message as suggested.
Signed-off-by: majiayu000 <1835304752@qq.com>
|
@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! |
srebhan
left a comment
There was a problem hiding this comment.
Thanks for your contribution @majiayu000! I have two comments in the code...
- 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>
Signed-off-by: majiayu000 <1835304752@qq.com>
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>
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 👍 This pull request doesn't change the Telegraf binary size 📦 Click here to get additional PR build artifactsArtifact URLs |
|
@majiayu000 Thanks for the contribution! |
Signed-off-by: majiayu000 <1835304752@qq.com> (cherry picked from commit ca72998)
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:
runAgentbuildInputbuildInputThis avoids the ambiguity of silently normalizing negative offsets, which could mean different things to different users.
Changes
collection_offsetcheck inrunAgent(cmd/telegraf/telegraf.go), alongside existing interval/flush_interval checksintervalandcollection_offsetchecks inbuildInput(config/config.go)NewTickersignature or behaviorChecklist
Related issues
supersedes #18178
resolves #18175