Skip to content
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

write trace to file as it comes #635

Merged
merged 2 commits into from
Aug 21, 2024
Merged

write trace to file as it comes #635

merged 2 commits into from
Aug 21, 2024

Conversation

pelikhan
Copy link
Member

@pelikhan pelikhan commented Aug 20, 2024

The changes introduced in the GIT_DIFF are primarily related to the management of tracing in the codebase.

  • 📖 The function runScript appears to be handling loading scripts and preserving trace information.
  • 🚀 Added code to ensure the "out" and "outTrace" directories exist (ensureDir), and to clean up the “out” directory is if the removeOut flag is set (emptyDir).
  • ✨ Introduced a new event listener to TRACE_CHUNK event in the trace object. When this event fires, the trace chunk details are appended to the file specified by outTrace.
  • 👥 The same TRACE_CHUNK listener logic is copied for a new file "res.trace.md" set in the "out" directory, barring circumstances where the outTrace file is the same as "res.trace.md".
  • 🗑️ The subsequent code which wrote the full trace content to the outTrace file at the end of the function (writeText(outTrace, trace.content)) has now been removed to reflect the new approach.
  • 🔄 This indicates a shift from a full dump of tracing at the end of function execution towards a real-time tracing information write process. This would benefit programs where live trace information is needed during script running, or where large traces could benefit from being written incrementally.

generated by pr-describe

Copy link

The changes in the GIT_DIFF primarily revolve around how traces are handled and how output directories are managed.

Notably, changes include:

  • Importing dirname from node:path and writeFile, appendFile from fs/promises.
  • Adding an event listener to the trace object that responds to TRACE_CHUNK events. When these events occur, the trace chunk is appended to the output trace file.
  • Adding similar functionality for an output file named res.trace.md in the output directory.
  • Making sure the output directory is created and cleaned (if necessary) before the tracing functionality is started.
  • Removing the previously used writeText(outTrace, trace.content) that wrote the entire trace content at once.

Concerns:

  • The trace writing functionality is duplicated for outTrace and res.trace.md. It would be better to abstract this into a separate function that handles trace writing given a file name and a trace object, thus not repeating code.
  • The code checks if (out && trace) twice, once for outTrace and once for res.trace.md. Consolidating these might make the code easier to follow.

Suggested changes:

+ async function setupTraceWriting(trace: Trace, filename: string) {
+     await ensureDir(dirname(filename))
+     await writeFile(filename, "", { encoding: "utf-8" })
+     trace.addEventListener(
+         TRACE_CHUNK,
+         async (ev) => {
+             const tev = ev as TraceChunkEvent
+             await appendFile(filename, tev.chunk, { encoding: "utf-8" })
+         },
+         false
+     )
+ }

+ if (out) {
+     if (removeOut) await emptyDir(out)
+     await ensureDir(out)
+ }
+ if (out && trace) {
+     const ofn = join(out, "res.trace.md")
+     if (outTrace) {
+         setupTraceWriting(trace, outTrace);
+     }
+     if (ofn !== outTrace) {
+         setupTraceWriting(trace, ofn);
+     }
+ }

Overall, the changes look fine except for the noted concerns and suggested improvements. The change prioritizes writing trace chunks as they arrive instead of at the end, which could be beneficial for long-running processes. However, make sure to test these changes thoroughly to ensure that the trace writing functionality works as expected.

Concerns aside, LGTM 🚀

generated by pr-review

await appendFile(filename, tev.chunk, { encoding: "utf-8" })
},
false
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an async function as an event listener can lead to unhandled promise rejections if the promise is rejected. Consider wrapping the async function call within the event listener in a try-catch block to handle any potential errors. 🛠️

generated by pr-review-commit async_in_event_listener

await setupTraceWriting(trace, ofn)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup for trace writing is only done if 'out' and 'trace' are truthy. However, 'outTrace' is used later in the code without checking if it was set up correctly. This could potentially lead to errors if 'outTrace' was not set up because 'out' or 'trace' were falsy. Please ensure that 'outTrace' is always correctly set up. 🧐

generated by pr-review-commit conditional_trace_setup

@@ -234,7 +264,6 @@ export async function runScript(
if (result.status !== "success")
logVerbose(result.statusText ?? result.status)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line of code that writes the trace content to 'outTrace' has been removed. If 'outTrace' is expected to contain the trace content, this could lead to unexpected behavior. Please ensure that the trace content is written to 'outTrace' as expected. 🕵️‍♀️

generated by pr-review-commit removed_trace_write

@pelikhan pelikhan merged commit a53684e into main Aug 21, 2024
9 checks passed
@pelikhan pelikhan deleted the live-trace branch August 21, 2024 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant