-
Notifications
You must be signed in to change notification settings - Fork 461
fix(envd): bound the in-memory logs queue #2676
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
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,37 +6,39 @@ import ( | |
| "log" | ||
| "net/http" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/envd/internal/host" | ||
| ) | ||
|
|
||
| const ExporterTimeout = 10 * time.Second | ||
| const ( | ||
| ExporterTimeout = 10 * time.Second | ||
|
|
||
| // Under Loki's 256 KiB max_line_size default. | ||
| maxLogLineBytes = 192 << 10 | ||
| maxBufferedBytes = 8 << 20 | ||
| ) | ||
|
|
||
| type HTTPExporter struct { | ||
| client http.Client | ||
| logs [][]byte | ||
| mmdsOpts *host.MMDSOpts | ||
| client http.Client | ||
| logs [][]byte | ||
| bufferedBytes int | ||
| mmdsOpts atomic.Pointer[host.MMDSOpts] | ||
|
|
||
| // Concurrency coordination | ||
| triggers chan struct{} | ||
| logLock sync.RWMutex | ||
| mmdsLock sync.RWMutex | ||
| logLock sync.Mutex | ||
| startOnce sync.Once | ||
| } | ||
|
|
||
| func NewHTTPLogsExporter(ctx context.Context, mmdsChan <-chan *host.MMDSOpts) *HTTPExporter { | ||
| exporter := &HTTPExporter{ | ||
| client: http.Client{ | ||
| Timeout: ExporterTimeout, | ||
| }, | ||
| triggers: make(chan struct{}, 1), | ||
| startOnce: sync.Once{}, | ||
| mmdsOpts: &host.MMDSOpts{ | ||
| SandboxID: "unknown", | ||
| TemplateID: "unknown", | ||
| LogsCollectorAddress: "", | ||
| Timeout: ExporterTimeout, | ||
| Transport: &http.Transport{DisableKeepAlives: true}, | ||
| }, | ||
| triggers: make(chan struct{}, 1), | ||
| } | ||
|
|
||
| go exporter.listenForMMDSOptsAndStart(ctx, mmdsChan) | ||
|
|
@@ -75,9 +77,7 @@ func (w *HTTPExporter) listenForMMDSOptsAndStart(ctx context.Context, mmdsChan < | |
| return | ||
| } | ||
|
|
||
| w.mmdsLock.Lock() | ||
| w.mmdsOpts.Update(mmdsOpts.SandboxID, mmdsOpts.TemplateID, mmdsOpts.LogsCollectorAddress) | ||
| w.mmdsLock.Unlock() | ||
| w.mmdsOpts.Store(mmdsOpts) | ||
|
|
||
| w.startOnce.Do(func() { | ||
| go w.start(ctx) | ||
|
|
@@ -87,26 +87,39 @@ func (w *HTTPExporter) listenForMMDSOptsAndStart(ctx context.Context, mmdsChan < | |
| } | ||
|
|
||
| func (w *HTTPExporter) start(ctx context.Context) { | ||
| // Cap stderr noise: log each error kind at most once per logFloor so a | ||
| // fast-failing collector (e.g. TCP RST) can't flood journald. | ||
| const logFloor = time.Minute | ||
| var lastLoggedJSONErr, lastLoggedSendErr time.Time | ||
|
Contributor
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. Might be good to add a "398 error ignored since last log" or something similar |
||
|
|
||
| for range w.triggers { | ||
| logs := w.getAllLogs() | ||
|
|
||
| if len(logs) == 0 { | ||
| continue | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| opts := w.mmdsOpts.Load() | ||
| if opts == nil { | ||
| continue | ||
| } | ||
|
|
||
| for _, logLine := range logs { | ||
| w.mmdsLock.RLock() | ||
| logLineWithOpts, err := w.mmdsOpts.AddOptsToJSON(logLine) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| w.mmdsLock.RUnlock() | ||
| logLineWithOpts, err := opts.AddOptsToJSON(logLine) | ||
| if err != nil { | ||
| log.Printf("error adding instance logging options (%+v) to JSON (%+v) with logs : %v\n", w.mmdsOpts, logLine, err) | ||
| if time.Since(lastLoggedJSONErr) > logFloor { | ||
| log.Printf("error adding instance logging options to JSON: %v", err) | ||
| lastLoggedJSONErr = time.Now() | ||
| } | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| err = w.sendInstanceLogs(ctx, logLineWithOpts, w.mmdsOpts.LogsCollectorAddress) | ||
| if err != nil { | ||
| log.Printf("error sending instance logs: %+v", err) | ||
| if err := w.sendInstanceLogs(ctx, logLineWithOpts, opts.LogsCollectorAddress); err != nil { | ||
| if time.Since(lastLoggedSendErr) > logFloor { | ||
| log.Printf("error sending instance logs: %+v", err) | ||
| lastLoggedSendErr = time.Now() | ||
| } | ||
|
|
||
| continue | ||
|
ValentaTomas marked this conversation as resolved.
|
||
| } | ||
|
|
@@ -124,6 +137,11 @@ func (w *HTTPExporter) resumeProcessing() { | |
| } | ||
|
|
||
| func (w *HTTPExporter) Write(logs []byte) (int, error) { | ||
| // Drop oversized lines: Loki would reject them anyway. | ||
| if len(logs) > maxLogLineBytes { | ||
| return len(logs), nil | ||
| } | ||
|
ValentaTomas marked this conversation as resolved.
|
||
|
|
||
| logsCopy := make([]byte, len(logs)) | ||
| copy(logsCopy, logs) | ||
|
|
||
|
|
@@ -138,6 +156,7 @@ func (w *HTTPExporter) getAllLogs() [][]byte { | |
|
|
||
| logs := w.logs | ||
| w.logs = nil | ||
| w.bufferedBytes = 0 | ||
|
|
||
| return logs | ||
| } | ||
|
|
@@ -146,6 +165,16 @@ func (w *HTTPExporter) addLogs(logs []byte) { | |
| w.logLock.Lock() | ||
| defer w.logLock.Unlock() | ||
|
|
||
| // Drop the oldest entries to stay under maxBufferedBytes. Happens when | ||
| // the collector is unreachable or the producer outruns the send loop; | ||
| // keeping the queue bounded matters more than not losing old lines. | ||
| for w.bufferedBytes+len(logs) > maxBufferedBytes && len(w.logs) > 0 { | ||
| w.bufferedBytes -= len(w.logs[0]) | ||
| w.logs[0] = nil | ||
| w.logs = w.logs[1:] | ||
| } | ||
|
|
||
| w.bufferedBytes += len(logs) | ||
| w.logs = append(w.logs, logs) | ||
|
|
||
| w.resumeProcessing() | ||
|
|
||
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package pkg | ||
|
|
||
| const Version = "0.5.21" | ||
| const Version = "0.5.22" |
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.
Uh oh!
There was an error while loading. Please reload this page.