feat(sql): add the traceroot sql command (api client, CSV/table/JSON/file output)#30
Draft
dark-sorceror wants to merge 4 commits into
Draft
feat(sql): add the traceroot sql command (api client, CSV/table/JSON/file output)#30dark-sorceror wants to merge 4 commits into
traceroot sql command (api client, CSV/table/JSON/file output)#30dark-sorceror wants to merge 4 commits into
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
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
Fixes #27
Adds the primary CLI surface for the SQL Query Gateway:
traceroot sql "<query>". Users send read-only SQL against the curated analytical export (spans/traces) and get results back as an aligned table (default), one-line JSON, or RFC-4180 CSV — optionally written straight to a file.It delivers the API-client methods, the CSV renderer, and the
sqlquery command. A follow-up PR (feat/sql-schema-command) addstraceroot sql schemaand the README docs.How it works
The backend resolves
project_idserver-side from the API key — the CLI never sends scope. The CLI only knows the two endpoint paths and the documented request/response shape; nothing else about the backend is assumed.What's included
API client (
src/api/client.ts)request<T>helper to take an optional{ method, body }while leaving every existing GET caller (whoami,listTraces,getTrace,exportTrace) byte-for-byte unchanged.sqlQuery(body)→POST /api/v1/public/sql;sqlSchema()→GET /api/v1/public/sql/schema(consumed by the follow-up schema PR). Both reuse the existing Bearer-auth headers, the per-request timeout signal, the{"detail": "..."}error-envelope parsing, and the api-key redaction on network failures.SqlColumn,SqlQueryRequest,SqlQueryResponse,SqlSchemaTable,SqlSchemaResponse) — the SQL endpoints are not inopenapi.jsonyet, so these stand in until codegen can derive them.CSV renderer (
src/render/csv.ts)renderCsv(headers, rows)— RFC-4180-ish, dependency-free: quotes a cell only when it contains,"\r\n; doubles inner quotes;null/undefined→ empty cell; objects/arrays →JSON.stringify; numbers/booleans stringified; always a trailing newline.sqlquery command (src/commands/sql.ts,src/commands/sql/query.ts)sqlcommand group with the query as its default action, registered insrc/commands/index.ts.-f/--file <path>,--csv,--output <file>,--max-rows <n>, plus the existing global--json.--file;--jsonand--csvare mutually exclusive;--max-rowsmust be a positive integer. All argument validation runs before auth resolution, so misuse fails fast without credentials.--outputwrites the selected format to a file.truncated=trueprints a stderr warning in table/CSV mode and is self-describing in JSON mode.CliError→ centralreportError, exit 1); a backend400surfaces itsdetailverbatim. No new exit-code taxonomy.--helpcarries the canonical examples (count spans 24h, p95 latency by model, cost by model, export to CSV, find error spans) using the canonical time columnspan_start_time.Test plan
sqlQueryPOSTs to the right URL with Bearer auth +content-type, serializingquery/parameters/max_rowsinto the body;sqlSchemaGETs with auth{"detail"}→CliError--outputwrites the selected format to a file-f/--filereads the query; positional +--filerejects; neither rejects; empty/whitespace query rejects; unreadable file rejects--json+--csvrejects;--max-rowsforwarded to the request body; non-positive--max-rowsrejectsrunCli)npm run build,npm run lint,npm run format:checkclean; full Vitest suite greentraceroot sql "SELECT count() FROM spans",... --csv) — not run here; no live SQL Gateway availableSummary by cubic
Adds the
traceroot sql "<query>"command to run read-only SQL against the analytical export and output results as a table (default), one-line JSON, or RFC‑4180 CSV. Includes new API client methods, a CSV renderer, and file output support.New Features
traceroot sql "<query>"with positional query or-f/--file;--jsonor--csvoutput;--outputto file;--max-rowslimit; validation runs before auth.sqlQueryPOST to/api/v1/public/sql;sqlSchemaGET/api/v1/public/sql/schema; Bearer auth, per-request timeout,{"detail"}error parsing, API key scrubbing on failures.renderCsv(headers, rows)with RFC‑4180 quoting (commas, quotes, CR/LF), JSON-stringify for objects, empty for null/undefined, trailing newline.--outputwrites bytes; warns on truncation in table/CSV; JSON is self-describing; backend 400detailsurfaces as-is.Refactors
request<T>supports{ method, body }; existing GET callers (whoami,listTraces,getTrace,exportTrace) unchanged.Written for commit b6d7e18. Summary will update on new commits.