-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: RunOnce / Serverless Telegraf #7408
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,95 @@ func (a *Agent) Run(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
// Run starts and runs only once | ||
func (a *Agent) RunOnce(ctx context.Context) error { | ||
log.Printf("I! [agent] Config: Interval:%s, Quiet:%#v, Hostname:%#v, "+ | ||
"Flush Interval:%s", | ||
a.Config.Agent.Interval.Duration, a.Config.Agent.Quiet, | ||
a.Config.Agent.Hostname, a.Config.Agent.FlushInterval.Duration) | ||
|
||
if ctx.Err() != nil { | ||
return ctx.Err() | ||
} | ||
|
||
log.Printf("D! [agent] Initializing plugins") | ||
err := a.initPlugins() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("D! [agent] Connecting outputs") | ||
err = a.connectOutputs(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
inputC := make(chan telegraf.Metric, 100) | ||
procC := make(chan telegraf.Metric, 100) | ||
outputC := make(chan telegraf.Metric, 100) | ||
|
||
startTime := time.Now() | ||
|
||
log.Printf("D! [agent] Starting service inputs") | ||
err = a.startServiceInputs(ctx, inputC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
src := inputC | ||
dst := inputC | ||
|
||
err = a.runInputs(ctx, startTime, dst) | ||
if err != nil { | ||
log.Printf("E! [agent] Error running inputs: %v", err) | ||
} | ||
|
||
log.Printf("D! [agent] Stopping service inputs") | ||
a.stopServiceInputs() | ||
|
||
close(dst) | ||
log.Printf("D! [agent] Input channel closed") | ||
|
||
src = dst | ||
|
||
if len(a.Config.Processors) > 0 { | ||
dst = procC | ||
|
||
err = a.runProcessors(src, dst) | ||
if err != nil { | ||
log.Printf("E! [agent] Error running processors: %v", err) | ||
} | ||
close(dst) | ||
log.Printf("D! [agent] Processor channel closed") | ||
|
||
src = dst | ||
} | ||
|
||
if len(a.Config.Aggregators) > 0 { | ||
dst = outputC | ||
|
||
err = a.runAggregators(startTime, src, dst) | ||
if err != nil { | ||
log.Printf("E! [agent] Error running aggregators: %v", err) | ||
} | ||
close(dst) | ||
log.Printf("D! [agent] Output channel closed") | ||
|
||
src = dst | ||
} | ||
|
||
err = a.runOutputs(startTime, src) | ||
if err != nil { | ||
log.Printf("E! [agent] Error running outputs: %v", err) | ||
} | ||
|
||
log.Printf("D! [agent] Closing outputs") | ||
a.closeOutputs() | ||
|
||
log.Printf("D! [agent] Stopped Successfully") | ||
return nil | ||
} | ||
|
||
// Test runs the inputs once and prints the output to stdout in line protocol. | ||
func (a *Agent) Test(ctx context.Context, waitDuration time.Duration) error { | ||
var wg sync.WaitGroup | ||
|
@@ -272,6 +361,14 @@ func (a *Agent) runInputs( | |
go func(input *models.RunningInput) { | ||
defer wg.Done() | ||
|
||
if a.Config.Agent.RunOnce { | ||
err := a.gatherOnce(acc, input, interval) | ||
if err != nil { | ||
acc.AddError(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might move error handling inside of gatherOnce. It won't affect existing use of it and it'll clean up both callers. |
||
} | ||
return | ||
} | ||
|
||
if a.Config.Agent.RoundInterval { | ||
err := internal.SleepContext( | ||
ctx, internal.AlignDuration(startTime, interval)) | ||
|
@@ -517,6 +614,23 @@ func (a *Agent) runOutputs( | |
wg.Add(1) | ||
go func(output *models.RunningOutput) { | ||
defer wg.Done() | ||
|
||
if a.Config.Agent.RunOnce { | ||
err := a.flushOnce(output, interval, output.Write) | ||
if err != nil { | ||
fmt.Println("Dead") | ||
} | ||
return | ||
} | ||
|
||
if !a.Config.Agent.RoundInterval { | ||
err := internal.SleepContext( | ||
ctx, internal.AlignDuration(startTime, interval)) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
a.flushLoop(ctx, startTime, output, interval, jitter) | ||
}(output) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want massive buffers here, and you're going to have a hard limit on how many metrics you can process this way. eg: a file input with 2 million lines is not going to work; it's going to fill this channel up before the rest of the chain can run. Since you're running the pieces one at a time. the
n
th + 1 write overn
buffer size is going to block forever.