-
Notifications
You must be signed in to change notification settings - Fork 43
feat(telemetry): add streaming JSON export writer #960
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
Open
EhabY
wants to merge
5
commits into
main
Choose a base branch
from
feat/issue-903-export-telemetry-json-writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1112f41
feat(telemetry): add JSON export writer
EhabY 7051857
refactor(util): extract fs helpers and stream JSON export
EhabY 5201c23
refactor(telemetry): nest writers in writers/ and prep shared helpers
EhabY 392217d
refactor(util): address review feedback on writeAtomically
EhabY d8a9b52
fix(test): stabilize ssh.process.replaced flake
EhabY 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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { createWriteStream } from "node:fs"; | ||
| import { Readable } from "node:stream"; | ||
| import { pipeline } from "node:stream/promises"; | ||
|
|
||
| import { writeAtomically } from "../../../util/fs"; | ||
| import { serializeTelemetryEvent } from "../../wireFormat"; | ||
|
|
||
| import type { TelemetryEvent } from "../../event"; | ||
|
|
||
| /** | ||
| * Streams `events` as a JSON array to `outputPath` via a temp file and | ||
| * atomic rename. Returns the number of events written. `onCleanupError` | ||
| * is invoked if removing the temp file after a failed write itself fails | ||
| * (typically a Windows lock); callers are expected to log it. | ||
| */ | ||
| export async function writeJsonArrayExport( | ||
| outputPath: string, | ||
| events: AsyncIterable<TelemetryEvent>, | ||
| onCleanupError: (err: unknown, tempPath: string) => void, | ||
| ): Promise<number> { | ||
| let count = 0; | ||
| async function* chunks(): AsyncGenerator<string> { | ||
| yield "["; | ||
| for await (const event of events) { | ||
| yield (count === 0 ? "\n" : ",\n") + | ||
| JSON.stringify(serializeTelemetryEvent(event)); | ||
| count += 1; | ||
| } | ||
| yield count === 0 ? "]\n" : "\n]\n"; | ||
| } | ||
| await writeAtomically( | ||
| outputPath, | ||
| async (tempPath) => { | ||
| await pipeline( | ||
| Readable.from(chunks()), | ||
| createWriteStream(tempPath, { encoding: "utf8" }), | ||
| ); | ||
| }, | ||
| onCleanupError, | ||
| ); | ||
| return count; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import * as fs from "node:fs/promises"; | ||
|
|
||
| const transientRenameCodes: ReadonlySet<string> = new Set([ | ||
| "EPERM", | ||
| "EACCES", | ||
| "EBUSY", | ||
| ]); | ||
|
|
||
| /** | ||
| * Rename with retry for transient Windows filesystem errors (EPERM, EACCES, | ||
| * EBUSY). On Windows, antivirus, Search Indexer, cloud sync, or concurrent | ||
| * processes can briefly lock files causing renames to fail. | ||
| * | ||
| * On non-Windows platforms, calls renameFn directly with no retry. | ||
| * | ||
| * Matches the strategy used by VS Code (pfs.ts) and graceful-fs: 60s | ||
| * wall-clock timeout with linear backoff (10ms increments) capped at 100ms. | ||
| */ | ||
| export async function renameWithRetry( | ||
| renameFn: (src: string, dest: string) => Promise<void>, | ||
| source: string, | ||
| destination: string, | ||
| timeoutMs = 60_000, | ||
| delayCapMs = 100, | ||
| ): Promise<void> { | ||
| if (process.platform !== "win32") { | ||
| return renameFn(source, destination); | ||
| } | ||
| const startTime = Date.now(); | ||
| for (let attempt = 1; ; attempt++) { | ||
| try { | ||
| return await renameFn(source, destination); | ||
| } catch (err) { | ||
| const code = (err as NodeJS.ErrnoException).code; | ||
| if ( | ||
| !code || | ||
| !transientRenameCodes.has(code) || | ||
| Date.now() - startTime >= timeoutMs | ||
| ) { | ||
| throw err; | ||
| } | ||
| const delay = Math.min(delayCapMs, attempt * 10); | ||
| await new Promise((resolve) => setTimeout(resolve, delay)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate a temporary file path by appending a suffix with a random component. | ||
| * The suffix describes the purpose of the temp file (e.g. "temp", "old"). | ||
| * Example: tempFilePath("/a/b", "temp") → "/a/b.temp-k7x3f9qw" | ||
| */ | ||
| export function tempFilePath(basePath: string, suffix: string): string { | ||
| return `${basePath}.${suffix}-${crypto.randomUUID().substring(0, 8)}`; | ||
| } | ||
|
|
||
| /** | ||
|
EhabY marked this conversation as resolved.
|
||
| * Atomically writes to `outputPath` via a sibling temp file and rename. | ||
| * The parent directory must already exist. On failure the destination is | ||
| * left untouched, the temp file is best-effort removed, and the writer | ||
| * error is always rethrown. `onCleanupError` receives any error from the | ||
| * cleanup attempt; its own throws are swallowed. | ||
| */ | ||
| export async function writeAtomically<T>( | ||
| outputPath: string, | ||
| write: (tempPath: string) => Promise<T>, | ||
| onCleanupError: (err: unknown, tempPath: string) => void, | ||
| ): Promise<T> { | ||
| const tempPath = tempFilePath(outputPath, "temp"); | ||
| try { | ||
| const result = await write(tempPath); | ||
| await renameWithRetry(fs.rename, tempPath, outputPath); | ||
| return result; | ||
| } catch (err) { | ||
| try { | ||
| await fs.rm(tempPath, { force: true }).catch((rmErr) => { | ||
| onCleanupError(rmErr, tempPath); | ||
| }); | ||
| } catch { | ||
| // onCleanupError threw; the writer error below takes precedence. | ||
| } | ||
| throw err; | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * Wraps a sync array as an `AsyncIterable` that yields one item per microtask, | ||
| * so consumers exercise the same async iteration path they would in production. | ||
| */ | ||
| export async function* asyncIterable<T>( | ||
| values: readonly T[], | ||
| ): AsyncGenerator<T> { | ||
| for (const value of values) { | ||
| await Promise.resolve(); | ||
| yield value; | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.