-
Notifications
You must be signed in to change notification settings - Fork 130
Added graceful shutdown timeout #1825
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
96a4404
Added graceful shutdown timeout
almostinf 8ffb1c6
Fix config tests
almostinf 536c8f0
Fix failed licenses pipeline
almostinf 03545a7
Returned vendor
almostinf 4f1e848
Fix vendor
almostinf 66ff5f1
Returned licenses file
almostinf ff3af79
fix review
almostinf 4f826f4
merge main branch and fix conflicts
almostinf 9bfd941
merge main and fix conflicts
almostinf 6ed5731
Merge branch 'main' into feature/force-stop
almostinf c3248cb
merge main
almostinf 2962b5b
add third_party_licenses
almostinf a8a2506
update third_party_licenses.csv
almostinf 7d82a00
update third_party_licenses.csv
almostinf 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,9 +4,11 @@ package appolly | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"sync" | ||
"time" | ||
|
||
"github.com/grafana/beyla/v2/pkg/beyla" | ||
"github.com/grafana/beyla/v2/pkg/internal/discover" | ||
|
@@ -16,54 +18,63 @@ import ( | |
"github.com/grafana/beyla/v2/pkg/pipe/msg" | ||
) | ||
|
||
var errShutdownTimeout = errors.New("graceful shutdown has timed out") | ||
|
||
func log() *slog.Logger { | ||
return slog.With("component", "beyla.Instrumenter") | ||
} | ||
|
||
// Instrumenter finds and instrument a service/process, and forwards the traces as | ||
// configured by the user | ||
type Instrumenter struct { | ||
config *beyla.Config | ||
ctxInfo *global.ContextInfo | ||
config *beyla.Config | ||
ctxInfo *global.ContextInfo | ||
tracersWg *sync.WaitGroup | ||
bp *pipe.Instrumenter | ||
|
||
// tracesInput is used to communicate the found traces between the ProcessFinder and | ||
// the ProcessTracer. | ||
tracesInput *msg.Queue[[]request.Span] | ||
} | ||
|
||
// New Instrumenter, given a Config | ||
func New(ctx context.Context, ctxInfo *global.ContextInfo, config *beyla.Config) *Instrumenter { | ||
10000 | func New(ctx context.Context, ctxInfo *global.ContextInfo, config *beyla.Config) (*Instrumenter, error) { | |
setupFeatureContextInfo(ctx, ctxInfo, config) | ||
|
||
tracesInput := msg.NewQueue[[]request.Span](msg.ChannelBufferLen(config.ChannelBufferLen)) | ||
|
||
bp, err := pipe.Build(ctx, config, ctxInfo, tracesInput) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't instantiate instrumentation pipeline: %w", err) | ||
} | ||
|
||
return &Instrumenter{ | ||
config: config, | ||
ctxInfo: ctxInfo, | ||
tracesInput: msg.NewQueue[[]request.Span](msg.ChannelBufferLen(config.ChannelBufferLen)), | ||
} | ||
tracersWg: &sync.WaitGroup{}, | ||
tracesInput: tracesInput, | ||
bp: bp, | ||
}, nil | ||
} | ||
|
||
// FindAndInstrument searches in background for any new executable matching the | ||
// selection criteria. | ||
// Returns a channel that is closed when the Instrumenter completed all its tasks. | ||
// This is: when the context is cancelled, it has unloaded all the eBPF probes. | ||
func (i *Instrumenter) FindAndInstrument(ctx context.Context) (<-chan struct{}, error) { | ||
func (i *Instrumenter) FindAndInstrument(ctx context.Context) error { | ||
finder := discover.NewProcessFinder(i.config, i.ctxInfo, i.tracesInput) | ||
processEvents, err := finder.Start(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't start Process Finder: %w", err) | ||
return fmt.Errorf("couldn't start Process Finder: %w", err) | ||
} | ||
done := make(chan struct{}) | ||
|
||
// In background, listen indefinitely for each new process and run its | ||
// associated ebpf.ProcessTracer once it is found. | ||
wg := sync.WaitGroup{} | ||
go func() { | ||
log := log() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
log.Debug("stopped searching for new processes to instrument. Waiting for the eBPF tracers to be unloaded") | ||
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'd anyway log the messages, for debugging purposes. 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. This log is also still there, just moved to Instrumenter.stop() :) |
||
wg.Wait() | ||
close(done) | ||
log.Debug("tracers unloaded, exiting FindAndInstrument") | ||
return | ||
case ev := <-processEvents: | ||
switch ev.Type { | ||
|
@@ -72,9 +83,9 @@ func (i *Instrumenter) FindAndInstrument(ctx context.Context) (<-chan struct{}, | |
log.Debug("running tracer for new process", | ||
"inode", pt.FileInfo.Ino, "pid", pt.FileInfo.Pid, "exec", pt.FileInfo.CmdExePath) | ||
if pt.Tracer != nil { | ||
wg.Add(1) | ||
i.tracersWg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
defer i.tracersWg.Done() | ||
pt.Tracer.Run(ctx, i.tracesInput) | ||
}() | ||
} | ||
|
@@ -91,8 +102,9 @@ func (i *Instrumenter) FindAndInstrument(ctx context.Context) (<-chan struct{}, | |
} | ||
} | ||
}() | ||
|
||
// TODO: wait until all the resources have been freed/unmounted | ||
return done, nil | ||
return nil | ||
} | ||
|
||
// ReadAndForward keeps listening for traces in the BPF map, then reads, | ||
|
@@ -101,20 +113,41 @@ func (i *Instrumenter) ReadAndForward(ctx context.Context) error { | |
log := log() | ||
log.Debug("creating instrumentation pipeline") | ||
|
||
bp, err := pipe.Build(ctx, i.config, i.ctxInfo, i.tracesInput) | ||
if err != nil { | ||
return fmt.Errorf("can't instantiate instrumentation pipeline: %w", err) | ||
} | ||
|
||
log.Info("Starting main node") | ||
|
||
bp.Run(ctx) | ||
i.bp.Run(ctx) | ||
|
||
<-ctx.Done() | ||
|
||
log.Info("exiting auto-instrumenter") | ||
|
||
err := i.stop() | ||
if err != nil { | ||
return fmt.Errorf("failed to stop auto-instrumenter: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Instrumenter) stop() error { | ||
log := log() | ||
|
||
stopped := make(chan struct{}) | ||
go func() { | ||
log.Debug("stopped searching for new processes to instrument. Waiting for the eBPF tracers to be unloaded") | ||
i.tracersWg.Wait() | ||
stopped <- struct{}{} | ||
log.Debug("tracers unloaded, exiting FindAndInstrument") | ||
}() | ||
|
||
select { | ||
case <-time.After(i.config.ShutdownTimeout): | ||
return errShutdownTimeout | ||
case <-stopped: | ||
return nil | ||
} | ||
} | ||
|
||
func setupFeatureContextInfo(ctx context.Context, ctxInfo *global.ContextInfo, config *beyla.Config) { | ||
ctxInfo.AppO11y.ReportRoutes = config.Routes != nil | ||
setupKubernetes(ctx, ctxInfo) | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
Nice!!! Didn't know this tool.