-
Notifications
You must be signed in to change notification settings - Fork 138
Fix worker logs not getting flushed in Core log level set to ERROR #1777
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 1 commit
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Heap } from 'heap-js'; | ||
| import { SdkComponent } from '@temporalio/common'; | ||
| import { native } from '@temporalio/core-bridge'; | ||
| import { DefaultLogger, LogEntry, Logger, LogTimestamp } from './logger'; | ||
| import { DefaultLogger, FlushableLogger, LogEntry, Logger, LogTimestamp } from './logger'; | ||
|
|
||
| /** | ||
| * A log collector that accepts log entries either through the TS `Logger` interface (e.g. used by | ||
|
|
@@ -25,10 +25,60 @@ export class NativeLogCollector { | |
|
|
||
| protected buffer = new Heap<LogEntry>((a, b) => Number(a.timestampNanos - b.timestampNanos)); | ||
|
|
||
| /** | ||
| * A timer that periodically flushes the buffer to the downstream logger. | ||
| */ | ||
| protected flushIntervalTimer: NodeJS.Timeout; | ||
|
|
||
| /** | ||
| * The minimum time an entry should be buffered before getting flushed. | ||
| * | ||
| * Increasing this value allows the buffer to do a better job of correctly reordering messages | ||
| * emitted from different sources (notably from Workflow executions through Sinks, and from Core) | ||
| * based on their absolute timestamps, but also increases latency of logs. | ||
| * | ||
| * The minimum buffer time requirement only applies as long as the buffer is not full. Once the | ||
| * buffer reaches its maximum size, older messages are unconditionally flushed, to prevent | ||
| * unbounded growth of the buffer. | ||
| * | ||
| * TODO(JWH): Is 100ms a reasonable compromise? That might seem a little high on latency, but to | ||
| * be useful, that value needs to exceed the time it typically takes to process | ||
| * Workflow Activations, let's say above the expected P90, but that's highly variable | ||
| * across our user base, and we don't really have field data anyway. | ||
| * We can revisit depending on user feedback. | ||
| */ | ||
| protected readonly minBufferTimeMs = 100; | ||
|
|
||
| /** | ||
| * Interval between flush passes checking for expired messages. | ||
| * | ||
| * This really is redundant, since Core itself is expected to flush its buffer every 10 ms, and | ||
| * we're checking for expired messages when it does. However, Core will only flush if it has | ||
| * accumulated at least one message; when Core's log level is set to WARN or higher, it may be | ||
| * many seconds, and even minutes, between Core's log messages, resulting in very rare flush | ||
| * from that end, which cause considerable delay on flushing log messages from other sources. | ||
| */ | ||
| protected readonly flushPassIntervalMs = 100; | ||
|
|
||
| /** | ||
| * The maximum number of log messages to buffer before flushing. | ||
| * | ||
| * When the buffer reaches this limit, older messages are unconditionally flushed (i.e. without | ||
| * regard to the minimum buffer time requirement), to prevent unbounded growth of the buffer. | ||
| */ | ||
| protected readonly maxBufferSize = 2000; | ||
|
|
||
| constructor(downstream: Logger) { | ||
| this.logger = new DefaultLogger('TRACE', (entry) => this.buffer.add(entry)); | ||
| this.logger = new DefaultLogger('TRACE', this.appendOne.bind(this)); | ||
| (this.logger as FlushableLogger).flush = this.flush.bind(this); | ||
| (this.logger as FlushableLogger).close = this.close.bind(this); | ||
|
|
||
| this.downstream = downstream; | ||
| this.receive = this.receive.bind(this); | ||
|
|
||
| // Flush the buffer every so often. | ||
| // Unref'ed so that it doesn't prevent the process from exiting. | ||
| this.flushIntervalTimer = setInterval(this.flushExpired.bind(this), this.flushPassIntervalMs).unref(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -44,14 +94,20 @@ export class NativeLogCollector { | |
| this.buffer.add(log); | ||
| } | ||
| } | ||
| this.flush(); | ||
| this.flushUnconditionally(); | ||
| this.flushExpired(); | ||
| } catch (_e) { | ||
| // We're not allowed to throw from here, and conversion errors have already been handled in | ||
| // convertFromNativeLogEntry(), so an error at this point almost certainly indicates a problem | ||
| // with the downstream logger. Just swallow it, there's really nothing else we can do. | ||
| } | ||
| } | ||
|
|
||
| private appendOne(entry: LogEntry): void { | ||
|
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. bit of a nit, but would prefer a better name here (i.e. |
||
| this.buffer.add(entry); | ||
| this.flushUnconditionally(); | ||
| } | ||
|
|
||
| private convertFromNativeLogEntry(entry: native.JsonString<native.LogEntry>): LogEntry | undefined { | ||
| try { | ||
| const log = JSON.parse(entry) as native.LogEntry; | ||
|
|
@@ -78,15 +134,49 @@ export class NativeLogCollector { | |
| } | ||
|
|
||
| /** | ||
| * Flush all buffered logs into the logger supplied to the constructor/ | ||
| * Flush messages that have exceeded their required minimal buffering time. | ||
| */ | ||
| flush(): void { | ||
| for (const entry of this.buffer) { | ||
| private flushExpired(): void { | ||
| const threadholdTimeNanos = BigInt(Date.now() - this.minBufferTimeMs) * 1_000_000n; | ||
| for (;;) { | ||
| const entry = this.buffer.peek(); | ||
| if (!entry || entry.timestampNanos > threadholdTimeNanos) break; | ||
| this.buffer.pop(); | ||
|
|
||
| this.downstream.log(entry.level, entry.message, { | ||
| [LogTimestamp]: entry.timestampNanos, | ||
| ...entry.meta, | ||
| }); | ||
| } | ||
| this.buffer.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Flush messages without regard to the time threshold, up to a given number of messages. | ||
| * | ||
| * If no limit is provided, flushes messages in excess to the maximum buffer size. | ||
| */ | ||
| private flushUnconditionally(maxFlushCount?: number): void { | ||
mjameswh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (maxFlushCount === undefined) { | ||
| maxFlushCount = this.buffer.size() - this.maxBufferSize; | ||
| } | ||
|
|
||
| while (maxFlushCount-- > 0) { | ||
| const entry = this.buffer.pop(); | ||
| if (!entry) break; | ||
|
|
||
| this.downstream.log(entry.level, entry.message, { | ||
| [LogTimestamp]: entry.timestampNanos, | ||
| ...entry.meta, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public flush(): void { | ||
| this.flushUnconditionally(Number.MAX_SAFE_INTEGER); | ||
| } | ||
|
|
||
| public close(): void { | ||
| this.flush(); | ||
| clearInterval(this.flushIntervalTimer); | ||
| } | ||
| } | ||
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
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.