-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(profile): add profile command to surface CPU profiling data #187
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
Open
BYK
wants to merge
35
commits into
main
Choose a base branch
from
feat/profile-command
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.
Open
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
cddef24
feat(profile): add profile command to surface CPU profiling data
BYK 4ac04d6
fix(profile): handle empty profiles and fix percentage calculation
BYK e757183
feat(profile): add transaction aliases for quick reference
BYK ce939e1
chore: regenerate SKILL.md for profile commands
BYK 86c2ab1
test: add unit and property tests for transaction aliases
BYK 0e78088
Merge main into feat/profile-command
BYK 456ebf5
chore: regenerate SKILL.md after merge
BYK 7068f49
fix(profile): ensure extractTransactionSegment never returns numeric …
BYK b14520f
fix(profile): address PR review comments
BYK f98c5de
chore: regenerate SKILL.md after adding --web flag to profile list
BYK cb96897
refactor(profile): replace --org/--project flags with positional args
BYK e6fada5
chore: trigger CI
BYK dd20ce9
Merge main into feat/profile-command
BYK f329ace
test(profile): add tests for analyzer, formatters, and URLs
BYK 56ff6ac
test(profile): add command-level tests for list and view
BYK e44e230
fix(test): use uniqueArray in alias prefix property test
BYK de41ae2
fix: address all 6 BugBot review comments on profile commands
BYK 44f682e
fix: deduplicate resolveListTarget and rename formatDuration to forma…
BYK c685648
fix: resolve bare transaction names and formatDurationMs boundary rou…
BYK ab7d707
fix: exclude current fingerprint in stale alias detection queries
BYK c83e27b
fix: guard against empty aliases from hyphen/underscore-only transact…
BYK 9ce148d
fix: prefix-based disambiguation to avoid alias overlap, and null-saf…
BYK 0d5c2b3
fix: use nullish() for ProfileFunctionRow fields to accept null from API
BYK c7d75ab
Merge remote-tracking branch 'origin/main' into feat/profile-command
BYK b615452
fix(profile): remove unused orgDisplay/projectDisplay from ResolvedPr…
BYK d45567e
fix(profile): improve output formatting
BYK ec75c70
feat(profile): add SAMPLES column and docs page
BYK d35a878
chore: regenerate SKILL.md
github-actions[bot] 2185365
fix(profile): address BugBot review comments
BYK 3817e60
fix(db): use TABLE_SCHEMAS for transaction_aliases and bump schema to v6
BYK 043a7af
fix(profile): align table header columns with data rows
BYK 9a362cc
fix(profile): fix alias pattern and schema repair robustness
BYK 47e63fd
fix(profile): fix invalid --org flag in error suggestion
BYK 16f269c
fix(profile): simplify explicit target resolution and add disambiguat…
BYK ff4195f
fix(profile): use exhaustive switch check in view.ts default case
BYK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { buildRouteMap } from "@stricli/core"; | ||
| import { listCommand } from "./list.js"; | ||
| import { viewCommand } from "./view.js"; | ||
|
|
||
| export const profileRoute = buildRouteMap({ | ||
| routes: { | ||
| list: listCommand, | ||
| view: viewCommand, | ||
| }, | ||
| docs: { | ||
| brief: "Analyze CPU profiling data", | ||
| fullDescription: | ||
| "View and analyze CPU profiling data from your Sentry projects.\n\n" + | ||
| "Commands:\n" + | ||
| " list List transactions with profiling data\n" + | ||
| " view View CPU profiling analysis for a transaction", | ||
| hideRoute: {}, | ||
| }, | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| /** | ||
| * sentry profile list | ||
| * | ||
| * List transactions with profiling data from Sentry. | ||
| * Uses the Explore Events API with the profile_functions dataset. | ||
| */ | ||
|
|
||
| import { buildCommand, numberParser } from "@stricli/core"; | ||
| import type { SentryContext } from "../../context.js"; | ||
| import { getProject, listProfiledTransactions } from "../../lib/api-client.js"; | ||
| import { parseOrgProjectArg } from "../../lib/arg-parsing.js"; | ||
| import { openInBrowser } from "../../lib/browser.js"; | ||
| import { | ||
| buildTransactionFingerprint, | ||
| setTransactionAliases, | ||
| } from "../../lib/db/transaction-aliases.js"; | ||
| import { ContextError } from "../../lib/errors.js"; | ||
| import { | ||
| divider, | ||
| formatProfileListFooter, | ||
| formatProfileListHeader, | ||
| formatProfileListRow, | ||
| formatProfileListTableHeader, | ||
| writeJson, | ||
| } from "../../lib/formatters/index.js"; | ||
| import { | ||
| resolveOrgAndProject, | ||
| resolveProjectBySlug, | ||
| } from "../../lib/resolve-target.js"; | ||
| import { buildProfilingSummaryUrl } from "../../lib/sentry-urls.js"; | ||
| import { buildTransactionAliases } from "../../lib/transaction-alias.js"; | ||
| import type { TransactionAliasEntry, Writer } from "../../types/index.js"; | ||
| import { parsePeriod } from "./shared.js"; | ||
|
|
||
| type ListFlags = { | ||
| readonly period: string; | ||
| readonly limit: number; | ||
| readonly json: boolean; | ||
| readonly web: boolean; | ||
| }; | ||
|
|
||
| /** Usage hint for ContextError messages */ | ||
| const USAGE_HINT = "sentry profile list <org>/<project>"; | ||
|
|
||
| /** Resolved org and project for profile list */ | ||
| type ResolvedListTarget = { | ||
| org: string; | ||
| project: string; | ||
| detectedFrom?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve org/project from parsed argument or auto-detection. | ||
| * | ||
| * @throws {ContextError} When target cannot be resolved | ||
| */ | ||
| async function resolveListTarget( | ||
| target: string | undefined, | ||
| cwd: string | ||
| ): Promise<ResolvedListTarget> { | ||
| const parsed = parseOrgProjectArg(target); | ||
|
|
||
| switch (parsed.type) { | ||
| case "org-all": | ||
| throw new ContextError( | ||
| "Project", | ||
| "Profile listing requires a specific project.\n\n" + | ||
| "Usage: sentry profile list <org>/<project>" | ||
| ); | ||
|
|
||
| case "explicit": { | ||
| const resolved = await resolveOrgAndProject({ | ||
| org: parsed.org, | ||
| project: parsed.project, | ||
| cwd, | ||
| usageHint: USAGE_HINT, | ||
| }); | ||
| if (!resolved) { | ||
| throw new ContextError("Organization and project", USAGE_HINT); | ||
| } | ||
| return resolved; | ||
| } | ||
|
|
||
| case "project-search": | ||
| return await resolveProjectBySlug(parsed.projectSlug, USAGE_HINT); | ||
BYK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| case "auto-detect": { | ||
| const resolved = await resolveOrgAndProject({ | ||
| cwd, | ||
| usageHint: USAGE_HINT, | ||
| }); | ||
| if (!resolved) { | ||
| throw new ContextError("Organization and project", USAGE_HINT); | ||
| } | ||
| return resolved; | ||
| } | ||
|
|
||
| default: { | ||
| const _exhaustiveCheck: never = parsed; | ||
| throw new ContextError( | ||
| `Unexpected target type: ${_exhaustiveCheck}`, | ||
| USAGE_HINT | ||
| ); | ||
| } | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Write empty state message when no profiles are found. | ||
| */ | ||
| function writeEmptyState(stdout: Writer, orgProject: string): void { | ||
| stdout.write(`No profiling data found for ${orgProject}.\n`); | ||
| stdout.write( | ||
| "\nMake sure profiling is enabled for your project and that profile data has been collected.\n" | ||
| ); | ||
| } | ||
|
|
||
| export const listCommand = buildCommand({ | ||
| docs: { | ||
| brief: "List transactions with profiling data", | ||
| fullDescription: | ||
| "List transactions that have CPU profiling data in Sentry.\n\n" + | ||
| "Target specification:\n" + | ||
| " sentry profile list # auto-detect from DSN or config\n" + | ||
| " sentry profile list <org>/<proj> # explicit org and project\n" + | ||
| " sentry profile list <project> # find project across all orgs\n\n" + | ||
| "The command shows transactions with profile counts and p75 timing data.", | ||
| }, | ||
| parameters: { | ||
| positional: { | ||
| kind: "tuple", | ||
| parameters: [ | ||
| { | ||
| placeholder: "target", | ||
| brief: "Target: <org>/<project> or <project>", | ||
| parse: String, | ||
| optional: true, | ||
| }, | ||
| ], | ||
| }, | ||
| flags: { | ||
| period: { | ||
| kind: "parsed", | ||
| parse: parsePeriod, | ||
| brief: "Time period: 1h, 24h, 7d, 14d, 30d", | ||
| default: "24h", | ||
| }, | ||
| limit: { | ||
| kind: "parsed", | ||
| parse: numberParser, | ||
| brief: "Maximum number of transactions to return", | ||
| default: "20", | ||
| }, | ||
| json: { | ||
| kind: "boolean", | ||
| brief: "Output as JSON", | ||
| default: false, | ||
| }, | ||
| web: { | ||
| kind: "boolean", | ||
| brief: "Open in browser", | ||
| default: false, | ||
| }, | ||
| }, | ||
| aliases: { n: "limit", w: "web" }, | ||
| }, | ||
| async func( | ||
| this: SentryContext, | ||
| flags: ListFlags, | ||
| target?: string | ||
| ): Promise<void> { | ||
| const { stdout, cwd, setContext } = this; | ||
|
|
||
| // Resolve org and project from positional arg or auto-detection | ||
| const resolvedTarget = await resolveListTarget(target, cwd); | ||
|
|
||
| // Set telemetry context | ||
| setContext([resolvedTarget.org], [resolvedTarget.project]); | ||
|
|
||
| // Get project to retrieve numeric ID (required for profile API and web URLs) | ||
| const project = await getProject( | ||
| resolvedTarget.org, | ||
| resolvedTarget.project | ||
| ); | ||
|
|
||
| // Open in browser if requested | ||
| if (flags.web) { | ||
| await openInBrowser( | ||
| stdout, | ||
| buildProfilingSummaryUrl(resolvedTarget.org, project.id), | ||
| "profiling" | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Fetch profiled transactions | ||
| const response = await listProfiledTransactions( | ||
| resolvedTarget.org, | ||
| project.id, | ||
| { | ||
| statsPeriod: flags.period, | ||
| limit: flags.limit, | ||
| } | ||
| ); | ||
|
|
||
| const orgProject = `${resolvedTarget.org}/${resolvedTarget.project}`; | ||
|
|
||
| // Build and store transaction aliases for later use with profile view | ||
| const transactionInputs = response.data | ||
| .filter((row) => row.transaction) | ||
| .map((row) => ({ | ||
| transaction: row.transaction as string, | ||
| orgSlug: resolvedTarget.org, | ||
| projectSlug: resolvedTarget.project, | ||
| })); | ||
|
|
||
| const aliases = buildTransactionAliases(transactionInputs); | ||
|
|
||
| // Store aliases with fingerprint for cache validation | ||
| const fingerprint = buildTransactionFingerprint( | ||
| resolvedTarget.org, | ||
| resolvedTarget.project, | ||
| flags.period | ||
| ); | ||
| setTransactionAliases(aliases, fingerprint); | ||
|
|
||
| // Build alias lookup map for formatting | ||
| const aliasMap = new Map<string, TransactionAliasEntry>(); | ||
| for (const alias of aliases) { | ||
| aliasMap.set(alias.transaction, alias); | ||
| } | ||
|
|
||
| // JSON output | ||
| if (flags.json) { | ||
| writeJson(stdout, response.data); | ||
| return; | ||
| } | ||
|
|
||
| // Empty state | ||
| if (response.data.length === 0) { | ||
| writeEmptyState(stdout, orgProject); | ||
| return; | ||
| } | ||
|
|
||
| // Human-readable output with aliases | ||
| const hasAliases = aliases.length > 0; | ||
| stdout.write(`${formatProfileListHeader(orgProject, flags.period)}\n\n`); | ||
| stdout.write(`${formatProfileListTableHeader(hasAliases)}\n`); | ||
| stdout.write(`${divider(82)}\n`); | ||
|
|
||
| for (const row of response.data) { | ||
| const alias = row.transaction ? aliasMap.get(row.transaction) : undefined; | ||
| stdout.write(`${formatProfileListRow(row, alias)}\n`); | ||
| } | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| stdout.write(formatProfileListFooter(hasAliases)); | ||
|
|
||
| if (resolvedTarget.detectedFrom) { | ||
| stdout.write(`\n\nDetected from ${resolvedTarget.detectedFrom}\n`); | ||
| } | ||
| }, | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Shared utilities for profile commands. | ||
| */ | ||
|
|
||
| /** Valid period values for profiling queries */ | ||
| export const VALID_PERIODS = ["1h", "24h", "7d", "14d", "30d"]; | ||
|
|
||
| /** | ||
| * Parse and validate a stats period string. | ||
| * | ||
| * @param value - Period string to validate | ||
| * @returns The validated period string | ||
| * @throws Error if the period is not in VALID_PERIODS | ||
| */ | ||
| export function parsePeriod(value: string): string { | ||
| if (!VALID_PERIODS.includes(value)) { | ||
| throw new Error( | ||
| `Invalid period. Must be one of: ${VALID_PERIODS.join(", ")}` | ||
| ); | ||
| } | ||
| return value; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.