Skip to content

feat(dsn): add automatic DSN detection with caching#11

Merged
betegon merged 3 commits intomainfrom
feat/dsn-auto-detection
Jan 16, 2026
Merged

feat(dsn): add automatic DSN detection with caching#11
betegon merged 3 commits intomainfrom
feat/dsn-auto-detection

Conversation

@betegon
Copy link
Member

@betegon betegon commented Jan 16, 2026

Summary

Adds GitHub CLI-inspired DSN auto-detection to the Sentry CLI. When users run commands like sentry issue list from a project directory, the CLI now automatically finds the Sentry DSN and resolves org/project without requiring --org and --project flags.

Problem

Users had to manually specify --org and --project flags for every command, or configure defaults. This is tedious, especially when the DSN is already in their project's .env file or source code.

Solution

New DSN detection module that:

  1. Detects DSN from multiple sources (in priority order):

    • SENTRY_DSN environment variable
    • .env files (.env.local, .env, .env.development, etc.)
    • JavaScript/TypeScript source code (Sentry.init({ dsn: "..." }))
  2. Caches for speed:

    • Fast path (~5ms): verifies cached DSN source file hasn't changed
    • Slow path (~2-5s): full scan on cache miss
    • Stores resolved org/project info to skip API calls
  3. Handles conflicts: warns when multiple different DSNs are found

Changes

Area Description
src/lib/dsn/ New DSN detection module with parser, cache, detector, resolver
src/commands/issue/list.ts Shows available projects when none specified
src/commands/project/list.ts Scopes to detected org, shows detection hint
src/types/config.ts Added dsnCache schema for storing cached entries

Example Output

$ sentry issue list
Issues in my-org/my-project (showing 25):
...
Detected from .env.local
$ sentry project list
SLUG          PLATFORM     NAME
my-project    javascript   My Project
...
Detected from .env.local
Use --org to see projects from other organizations.

Testing

  • 20 new tests for DSN detection and caching
  • All 94 tests pass

Adds a new DSN detection module that automatically finds Sentry DSN
from environment variables, .env files, and JavaScript/TypeScript code.

Features:
- Fast path (~5ms): verifies cached DSN source file
- Slow path (~2-5s): full scan when cache miss
- Conflict detection: warns when multiple different DSNs exist
- Project resolution caching: stores org/project info to skip API calls

Detection priority:
1. SENTRY_DSN env var (instant)
2. .env files (.env.local, .env, .env.development, etc.)
3. Source code (Sentry.init patterns)

The module is structured for extensibility to support other languages
(Python, PHP, etc.) in the future.
Updates issue and project commands to use DSN auto-detection:

- issue list: shows available projects when none specified
- issue get: uses DSN to resolve org for short ID lookups
- project list: scopes to detected org, shows hint about --org flag

When no org/project is specified and DSN is detected, commands now
show 'Detected from .env.local' to indicate the source.
Adds comprehensive tests for:
- Cache read/write operations
- DSN detection with caching behavior
- Cache updates when DSN changes
- Conflict detection with multiple DSNs
- Env var priority over cached values
@betegon betegon marked this pull request as ready for review January 16, 2026 11:28
@betegon betegon merged commit 6188779 into main Jan 16, 2026
2 checks passed
@betegon betegon deleted the feat/dsn-auto-detection branch January 16, 2026 11:28
BYK added a commit that referenced this pull request Mar 4, 2026
…parallelize cleanup

Review Round 3 — 15 human review comments addressed:

#1: login.ts — Replace await .catch() with proper try/await/catch blocks
#2: whoami.ts + all commands — Export FRESH_ALIASES constant from list-command.ts
    to reduce boilerplate; update 15 command files to use it
#3: response-cache.ts — Bump immutable TTL from 1hr to 24hr (events/traces
    never change once created)
#4–6: response-cache.ts — Restructure URL_TIER_PATTERNS as Record<TtlTier, RegExp[]>,
    combine duplicate regex patterns into single alternations
#7: response-cache.ts — Replace localeCompare with simple < comparison for
    ASCII URL query param sorting
#8: response-cache.ts — Remove try-catch in normalizeUrl (URLs reaching the
    cache already came from a fetch, always valid)
#9: response-cache.ts — Link immutableMinTimeToLive to FALLBACK_TTL_MS.immutable
    instead of hardcoded magic number
#10: response-cache.ts — Use Object.fromEntries(headers.entries()) instead of
    manual forEach loop
#11: response-cache.ts — Remove unnecessary await on fire-and-forget unlink in
    catch block
#12: response-cache.ts — Add expiresAt field to CacheEntry for O(1) expiry
    checks during cleanup (no CachePolicy deserialization needed)
#13–15: response-cache.ts — Parallelize cache I/O (collectEntryMetadata,
    deleteExpiredEntries, evictExcessEntries) using p-limit-style concurrency
    limiter (max 8 concurrent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant