You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: add run subcommand and make diff results trustworthy - #43
Add sql-tap run -- <command> to measure the SQL traffic of a single command, and tighten sql-tap diff (#42) so that its result can be trusted as evidence for a change. With this, an AI agent (or a human) can measure before and after a fix with one command each and attach the diff output to the PR.
sql-tap run -out=before.json localhost:9091 -- go test ./...
# ...apply the change...
sql-tap run -out=after.json localhost:9091 -- go test ./...
sql-tap diff -format=markdown before.json after.json
Behavior changes to diff from #42: reports measured with different commands are now rejected (exit 2), and slow query increases only warn by default instead of failing.
Changes
Not missing events at the start: feat(server): send subscribed header once Watch subscription is registered
sql-tapd sends a sql-tap-subscribed response header right after registering the Watch subscription. Previously the client could not tell when the subscription was in place, so queries issued right after connecting could be missed (the existing tests sleep 50ms for this reason).
Existing clients (TUI, -ci) ignore the header, so nothing changes for them.
feat(ci): add Subscribe to wait for sql-tapd subscription confirmation
ci.Subscribe(ctx, addr, timeout) returns only after the header arrives. An older sql-tapd never sends it, so Subscribe fails after the timeout with a hint that sql-tapd may be older than sql-tap.
ci.Run (used by -ci) does not wait for the header, so new sql-tap keeps working with older sql-tapd in CI mode.
Not missing events at the end: feat(ci): drain in-flight events after stop in Watcher.Collect
After the command exits, events may still be on their way over gRPC. Watcher.Collect(stop, Drain{Idle, Max}) keeps receiving until no event arrives for Idle, bounded by Max.
Hitting Max returns the result together with ErrDrainLimit, since the numbers may be incomplete (typically another application is using the same sql-tapd).
feat(ci): record measured command in CI results
Result.Command / JSON command (omitted when empty). Adding an optional field keeps the schema at version: 1; older reports decode with no command.
fix(ci): bound the whole Subscribe by its timeout
The timeout only covered waiting for the header, not connection setup. It now cancels the whole Subscribe.
conn.Close() can block for ~5s after a failed dial to localhost (127.0.0.1 returns immediately), so failed connections are closed in the background.
feat: add run subcommand to measure SQL traffic of a command
sql-tap run [flags] <addr> -- <command> [args...] with -format, -out, -subscribe-timeout (5s), -drain (200ms), -drain-max (5s).
The command keeps stdin / stdout / stderr; the report goes to stderr so it does not mix with test output. SIGINT / SIGTERM are forwarded to the command.
Exit codes, in priority order:
the command's own exit code if it fails, so test failures are never hidden behind a clean SQL report
2 if no statements were captured (the app likely bypassed the proxy), the subscription was not confirmed, or draining hit -drain-max
1 if N+1 or slow queries were found, same as -ci
0 otherwise
The report and -out are written even when the command fails.
feat(diff): fail when reports were measured with different commands
Comparing go test ./... before with go test ./order/... after would look like a large improvement. When both reports record a command and they differ, diff exits 2. -allow-command-mismatch compares anyway with a warning.
When either report has no command (e.g. written by -ci), diff compares but warns that it cannot verify the scope.
Adds Comparison.Warnings, rendered in text, Markdown and JSON.
feat(diff): only warn on slow query increases by default
Slow flags depend on a time threshold, so a query at 99ms in one run and 101ms in the next would fail diff without any code change. Statement and N+1 counts are stable for the same code and stay strict.
Slow increases now go to Warnings; -fail-on-slow restores the previous behavior.
fix: stop HTML-escaping SQL in JSON reports
encoding/json escaped <, > and &, so WHERE total > ? came out as WHERE total > ? in -out, -format=json and diff -format=json. Both MarshalJSON and the outer encoders now disable HTML escaping.
fix(diff): mention command mismatch in exit code help
docs: document run and diff subcommands
README: usage, new "Measuring a command" and "Comparing reports" sections, -format / -out for CI mode, and the zero-statement warning.
Verification
go test -race ./... passes
golangci-lint run ./...: 0 issues
Each intermediate commit builds and passes go test -race ./ci/ . on its own
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Collection can end prematurely and report success while the command is still running, with additional unresolved issues in exit-status preservation, descendant signal forwarding, and warning rendering.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Incomplete reports can produce misleading clean improvements in Diff, while collection error handling can misreport successful completion or discard captured diagnostics.
A receive error such as gRPC Unavailable returns before either report writer runs, so even a failed command produces no report and can leave an older -out file untouched. Watcher.Collect also discards captured events on this path (ci/watcher.go:106). Preserve the partial aggregate and write it as an incomplete report before returning the collection error, while retaining the command's exit-code priority.
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #44 (67e2a74): run records why a measurement may be incomplete in the JSON incomplete field, and diff refuses marked reports with exit 2. The report itself is still written for diagnosis.
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #44 (c16e9cc): the end-of-stream decision now checks whether stop is actually closed (streamEndErr), with a unit test for the closed-stop case that fails against the previous logic.
Re: "Write partial reports when event collection fails" from the review summary: fixed in #44 (5dbb406). Watcher.Collect now returns the partial result along with a receive error, and run writes the report marked incomplete for any collection error, so a stale -out file is overwritten. The command's exit code still takes priority.
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
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
Add
sql-tap run -- <command>to measure the SQL traffic of a single command, and tightensql-tap diff(#42) so that its result can be trusted as evidence for a change. With this, an AI agent (or a human) can measure before and after a fix with one command each and attach thediffoutput to the PR.Behavior changes to
difffrom #42: reports measured with different commands are now rejected (exit 2), and slow query increases only warn by default instead of failing.Changes
Not missing events at the start:
feat(server): send subscribed header once Watch subscription is registeredsql-tap-subscribedresponse header right after registering the Watch subscription. Previously the client could not tell when the subscription was in place, so queries issued right after connecting could be missed (the existing tests sleep 50ms for this reason).-ci) ignore the header, so nothing changes for them.feat(ci): add Subscribe to wait for sql-tapd subscription confirmationci.Subscribe(ctx, addr, timeout)returns only after the header arrives. An older sql-tapd never sends it, soSubscribefails after the timeout with a hint that sql-tapd may be older than sql-tap.ci.Run(used by-ci) does not wait for the header, so new sql-tap keeps working with older sql-tapd in CI mode.Not missing events at the end:
feat(ci): drain in-flight events after stop in Watcher.CollectWatcher.Collect(stop, Drain{Idle, Max})keeps receiving until no event arrives forIdle, bounded byMax.Maxreturns the result together withErrDrainLimit, since the numbers may be incomplete (typically another application is using the same sql-tapd).feat(ci): record measured command in CI resultsResult.Command/ JSONcommand(omitted when empty). Adding an optional field keeps the schema atversion: 1; older reports decode with no command.fix(ci): bound the whole Subscribe by its timeoutSubscribe.conn.Close()can block for ~5s after a failed dial tolocalhost(127.0.0.1returns immediately), so failed connections are closed in the background.feat: add run subcommand to measure SQL traffic of a commandsql-tap run [flags] <addr> -- <command> [args...]with-format,-out,-subscribe-timeout(5s),-drain(200ms),-drain-max(5s).2if no statements were captured (the app likely bypassed the proxy), the subscription was not confirmed, or draining hit-drain-max1if N+1 or slow queries were found, same as-ci0otherwise-outare written even when the command fails.feat(diff): fail when reports were measured with different commandsgo test ./...before withgo test ./order/...after would look like a large improvement. When both reports record a command and they differ,diffexits 2.-allow-command-mismatchcompares anyway with a warning.-ci),diffcompares but warns that it cannot verify the scope.Comparison.Warnings, rendered in text, Markdown and JSON.feat(diff): only warn on slow query increases by defaultdiffwithout any code change. Statement and N+1 counts are stable for the same code and stay strict.Warnings;-fail-on-slowrestores the previous behavior.fix: stop HTML-escaping SQL in JSON reportsencoding/jsonescaped<,>and&, soWHERE total > ?came out asWHERE total > ?in-out,-format=jsonanddiff -format=json. BothMarshalJSONand the outer encoders now disable HTML escaping.fix(diff): mention command mismatch in exit code helpdocs: document run and diff subcommands-format/-outfor CI mode, and the zero-statement warning.Verification
go test -race ./...passesgolangci-lint run ./...: 0 issuesgo test -race ./ci/ .on its own