feat(telemetry): add withTracing helper to reduce Sentry span boilerplate#172
feat(telemetry): add withTracing helper to reduce Sentry span boilerplate#172
Conversation
…late Add generic tracing helpers to reduce boilerplate when wrapping operations with Sentry spans: - withTracing(name, op, fn, attrs?) - Simple async wrapper with auto status - withTracingSpan(name, op, fn, attrs?) - Passes span to callback for dynamic attributes; won't override manually-set status - withFsSpan(operation, fn) - File system operations wrapper Refactored existing code to use new helpers: - code-scanner.ts: scanDirectory uses withTracingSpan - project-root.ts: findProjectRoot uses withTracingSpan, removed duplicate withFsSpan definition (now imports from telemetry) - withHttpSpan now uses withTracing internally Closes #165
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ Patch coverage is 99.13%. Project has 2183 uncovered lines. Files with missing lines (34)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 70.49% 70.69% +0.2%
==========================================
Files 54 54 —
Lines 7400 7448 +48
Branches 0 0 —
==========================================
+ Hits 5216 5265 +49
- Misses 2184 2183 -1
- Partials 0 0 —Generated by Codecov Action |
…Queries - Add optional query parameter to withDbSpan for SQL query tracking - When query is provided, use it as span name and set db.statement attribute - Update all auth.ts call sites to include parameterized SQL queries - This enables Sentry's Queries feature to show query sources
Replace manual query parameter in withDbSpan with automatic tracing: - Add createTracedDatabase() to wrap Database with Proxy - Intercepts query() and wraps returned Statements - Traces get/run/all/values calls with SQL as span name and db.statement - Update getDatabase() to return traced database - Simplify withDbSpan to just group logical operations (op: db.operation) - Revert auth.ts to use simple 2-argument withDbSpan calls This ensures db.statement is always accurate since it's captured directly from db.query(sql) - no manual duplication that can diverge.
|
Regarding the Bugbot comment about error handling in This catch block behavior is pre-existing from PR #159 (commit 67bc20f) that added DSN discovery. The design intentionally returns an empty result rather than throwing because:
This PR only refactored from |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
…ontext Address Bugbot feedback: createTracedStatement was inconsistent with createTracedDatabase - non-traced methods need to be bound to the target to preserve 'this' context for native methods with private fields. Without this, methods like finalize() or toString() would fail when called on traced statements.
Summary
Adds generic tracing helpers and automatic database query instrumentation to reduce boilerplate and improve observability.
Part 1: Generic Tracing Helpers
New helpers in
src/lib/telemetry.ts:withTracing(name, op, fn, attrs?)- Simple async wrapper with automatic status settingwithTracingSpan(name, op, fn, attrs?)- Passes span to callback for dynamic attributes; won't override manually-set statuswithFsSpan(operation, fn)- File system operations wrapperRefactored:
code-scanner.ts:scanDirectorynow useswithTracingSpanproject-root.ts: Removed duplicatewithFsSpan, uses shared helperwithHttpSpannow useswithTracinginternallyPart 2: Automatic Database Query Tracing
Problem: The previous
withDbSpanrequired manually passing SQL query strings, which could diverge from actual queries and created code bloat.Solution: Wrap the SQLite Database with a Proxy that automatically traces all queries:
createTracedDatabase(db)- Wraps Database, interceptsquery()methodcreateTracedStatement(stmt, sql)- Wraps Statement, tracesget/run/all/valuescallsdb.query(sql)- single source of truth, can't divergedb.statementattribute for Sentry Queries featureChanges:
db/index.ts:getDatabase()now returns a traced databasewithDbSpansimplified to just group logical operations (op:db.operation)auth.tsBefore (manual, error-prone)
After (automatic)
Span Hierarchy
Closes #165
Cursor Bugbot reviewed your changes and found no issues for commit bad9778