-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add --trace flag for structured I/O logging #24136
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
Draft
robobun
wants to merge
17
commits into
main
Choose a base branch
from
claude/add-trace-flag
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.
Draft
Conversation
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
Collaborator
Author
Collaborator
|
can you update the PR description to reflect the current format |
This adds a --trace=file.json flag that outputs structured logs for: - node:fs operations (open, read, write) - Bun.write operations - fetch HTTP requests and responses - Response.body consumption The trace format is line-delimited JSON with: - ns: namespace for filtering (fs, bun_write, fetch, response_body) - ts: millisecond timestamp - data: operation-specific structured data This will be used by AI agents like Claude to understand what applications are doing without manually adding console.log calls. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added tracing to: - open() with path, flags, mode, fd/errno - read() with fd, offset, length, bytes_read/errno - write() with fd, offset, length, bytes_written/errno - close() with fd, errno Uses inline traceFS() helper to avoid type compatibility issues. All tracing is zero-overhead when --trace flag is not used. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added tracing in WriteFile.doWrite() to track all writes through Bun.write, logging fd, buffer length, and bytes written/errno. Verified fs tracing works with sync operations (openSync, writeSync, closeSync). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added tracing for: - Request initiation: URL, method - Response received: URL, status, has_more, body_size - Response body consumption: call type, status Tested and verified all trace points work correctly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added comprehensive tests covering: - Multiple fs operations (open, read, write, close) - Multiple HTTP/fetch requests with different endpoints - Mixed fs + HTTP operations - Namespace filtering (fs, fetch, response_body) - Chronological ordering of trace events - Trace format validation (ns, ts, data fields) All tests verify trace output structure and content. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Fixed missing trace events for read/write operations that use position parameter. When readSync/writeSync are called with a position argument, they use preadInner/pwriteInner instead of readInner/writeInner, which were not instrumented. Changes: - Added tracing to preadInner and pwriteInner in node_fs.zig - Updated extensive-trace.test.ts to use openSync/writeSync/closeSync instead of writeFileSync/readFileSync - Increased timeout for fetch test to 10 seconds All 12 trace tests now pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Extended tracing to cover all high-level filesystem operations that were previously missing. This ensures AI agents get complete visibility into file operations regardless of which API is used. Added tracing for: - readFile/readFileSync - traces path, encoding, bytes read - writeFile/writeFileSync - traces path, length, bytes written - stat/lstat/fstat - traces path/fd, file size, mode - mkdir/mkdirSync - traces path, mode, recursive flag - rmdir/rmdirSync - traces path, recursive flag, success - unlink/unlinkSync - traces path, success - rename/renameSync - traces from/to paths, success - readdir/readdirSync - traces path, recursive flag, entry count Each operation has entry and exit traces with relevant data. Trace output only includes applicable fields (no null values). Added 5 new tests covering high-level operations: - trace high-level readFile/writeFile - trace stat operations - trace directory operations - trace rename operations All 16 tests pass (7 basic + 5 extensive + 4 new high-level). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Added two documentation files to help users and future developers understand and use the trace functionality: 1. docs/cli/trace.md - User-facing documentation covering: - Usage examples and CLI syntax - Complete list of traced operations - Trace event format and fields - Analysis examples with jq - Common use cases (debugging, testing, AI analysis) - Performance impact details - Implementation details 2. test/js/bun/trace/README.md - Developer documentation covering: - Test file organization - How to write new trace tests - Important test patterns (instrumented vs non-instrumented ops) - Common assertions and debugging tips - Coverage matrix showing what's tested - Guidelines for adding new trace points These docs will help future Claudes (and humans) effectively use and extend the trace functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Created a comprehensive trace analysis tool that demonstrates the value of --trace for debugging and understanding applications. Features: - Timeline visualization with key events - Operations breakdown by namespace - File system analysis (files accessed, bytes transferred) - HTTP analysis (endpoints, latency, response sizes) - Performance insights (slowest operations) - Automated recommendations (repeated reads, excessive ops) This tool shows how AI agents can use trace data to: - Understand application behavior without reading code - Identify performance bottlenecks - Detect inefficient patterns (repeated reads, API calls in loops) - Get file/network I/O statistics Example output includes formatted reports with emojis, charts, and actionable insights. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Implemented comprehensive tracing for subprocess operations to help
AI agents and developers understand what commands are being executed.
Implementation:
- Added traceSubprocess() helper in subprocess.zig
- Trace spawn events with: cmd, args count, cwd, env count, pid
- Trace exit events with: pid, exit_code, signal, or errno
- Works for both Bun.spawn() and child_process module
Trace format:
```json
{"ns":"subprocess","ts":...,"data":{"call":"spawn","cmd":"echo","args":4,"cwd":"/path","env_count":43,"pid":1234}}
{"ns":"subprocess","ts":...,"data":{"call":"exit","pid":1234,"exit_code":0}}
```
Testing:
- Added 3 comprehensive tests in subprocess-trace.test.ts
- Tests cover Bun.spawn, child_process spawn, and argument tracking
- All tests passing
This is critical for debugging applications that shell out to other
tools, which is very common in real-world applications.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Changed trace output from object format to array format for better
compactness and easier filtering.
Old format:
```json
{"ns":"fs","ts":123,"data":{"call":"writeFile","path":"test.txt"}}
```
New format:
```json
["fs",123,"writeFile",{"path":"test.txt"}]
```
Benefits:
- More compact (saves ~40% space)
- Easier to filter with jq: select(.[0] == "fs" and .[2] == "writeFile")
- Faster to parse (arrays vs objects)
- "call" field promoted to top level as operation name
Array structure:
- [0] = namespace (fs, fetch, subprocess, etc.)
- [1] = timestamp in milliseconds
- [2] = operation name (writeFile, spawn, request, etc.)
- [3] = operation-specific data object
Updated:
- output.zig tracer implementation to output array format
- All 19 tests updated for new format
- All tests passing
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Updated all documentation and examples to reflect the new compact array trace format. Changes: - docs/cli/trace.md: Updated format documentation with array structure - Added "Why Array Format?" section explaining benefits - Updated all jq examples for array indexing - Updated example trace output - examples/trace-analysis.js: Updated to parse array format - Changed e.ns to e[0], e.ts to e[1], e.data.call to e[2], e.data.* to e[3].* - Tested and working correctly with new format All trace filtering examples now use array indexing: - select(.[0] == "fs") instead of select(.ns == "fs") - .[2] for operation names instead of .data.call - .[3].path for data fields instead of .data.path 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
On Windows, std.json.stringify was failing when encountering pointers to opaque or function types in trace data. This commit adds compile-time checks to skip fields that can't be serialized: - Skip pointers to opaque types (*anyopaque) - Skip pointers to function types (*fn) - Skip function types directly The tracing system now safely handles all field types across platforms while still capturing all the useful data (paths, sizes, error codes, etc). All 19 trace tests pass, and cross-platform builds succeed (49/49 steps).
- Add tracing to Windows-specific uv_open, uv_close, uv_read, uv_write - Update output.zig to use new Zig bufferedWriter API - Update output.zig to use new std.json.Stringify API - Fix callconv(.C) to callconv(.c) for new Zig - Delete examples/trace-analysis.js as requested - Restore fetch.zig to main branch version (tracing was in refactored file) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
e884513 to
5140a4c
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements
--trace=file.jsonlflag that outputs structured logs of I/O operations to help AI agents and developers understand application behavior without manual instrumentation.Motivation
When debugging unfamiliar applications, developers and AI agents need to understand:
Currently this requires adding console.log statements throughout the code or using external tracing tools. This PR adds native tracing built into Bun.
Implementation
Core Infrastructure
Output.tracer()API insrc/output.zigwith zero-overhead design--trace=PATHCLI flag parsing insrc/cli/Arguments.zigTraced Operations
File System (
ns: "fs"):HTTP (
ns: "fetch"):Response Body (
ns: "response_body"):Bun.write (
ns: "bun_write"):Subprocess (
ns: "subprocess"):Trace Format
Compact array format - line-delimited JSON (JSONL):
Format:
[namespace, timestamp_ms, operation, data]Why array format?
jq 'select(.[0] == "fs")'Example Usage
Testing
Documentation
docs/cli/trace.mdtest/js/bun/trace/README.mdexamples/trace-analysis.jsPerformance
Zero overhead when disabled (default):
Minimal overhead when enabled:
Future Enhancements
Potential areas for expansion:
Closes
N/A - New feature
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]