Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | 11x 11x 18x 18x 11x 11x 11x 11x 11x 11x 11x 1x 6x 3x 3x 27x 34x 34x 34x 4x 34x 34x 8x 8x 9x 9x 15x 9x 6x 6x 3x 3x 6x 6x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 8x 10x 10x 9x 10x 10x 10x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 9x 11x 18x 18x 18x 18x 18x 18x 9x 17x | /**
* sentry span list
*
* List spans from a Sentry project, or within a specific trace.
*
* Dual-mode command (like `log list`):
* - **Project mode** (no trace ID): lists spans across the entire project
* - **Trace mode** (trace ID provided): lists spans within a specific trace
*
* Disambiguation uses {@link isTraceId} to detect 32-char hex trace IDs.
*/
import type { SentryContext } from "../../context.js";
import type { SpanSortValue } from "../../lib/api/traces.js";
import { listSpans } from "../../lib/api-client.js";
import { validateLimit } from "../../lib/arg-parsing.js";
import {
advancePaginationState,
buildPaginationContextKey,
hasPreviousPage,
resolveCursor,
} from "../../lib/db/pagination.js";
import {
type FlatSpan,
formatSpanTable,
spanListItemToFlatSpan,
translateSpanQuery,
} from "../../lib/formatters/index.js";
import { filterFields } from "../../lib/formatters/json.js";
import { renderMarkdown } from "../../lib/formatters/markdown.js";
import { CommandOutput } from "../../lib/formatters/output.js";
import {
buildListCommand,
LIST_DEFAULT_LIMIT,
LIST_MAX_LIMIT,
LIST_PERIOD_FLAG,
PERIOD_ALIASES,
paginationHint,
TARGET_PATTERN_NOTE,
} from "../../lib/list-command.js";
import { withProgress } from "../../lib/polling.js";
import { resolveOrgProjectFromArg } from "../../lib/resolve-target.js";
import { sanitizeQuery } from "../../lib/search-query.js";
import {
appendPeriodHint,
serializeTimeRange,
type TimeRange,
timeRangeToApiParams,
} from "../../lib/time-range.js";
import {
type ParsedTraceTarget,
parseDualModeArgs,
resolveTraceOrgProject,
warnIfNormalized,
} from "../../lib/trace-target.js";
import { SpanListItemSchema } from "../../types/index.js";
type ListFlags = {
readonly limit: number;
readonly query?: string;
readonly sort: SpanSortValue;
readonly period: TimeRange;
readonly cursor?: string;
readonly json: boolean;
readonly fresh: boolean;
readonly fields?: string[];
};
/**
* All field names already covered by the default SPAN_FIELDS request —
* both the raw API names (e.g., `id`, `span.op`) and their output-side
* aliases (e.g., `span_id`, `op`) produced by `spanListItemToFlatSpan`.
* Any `--fields` value NOT in this set is treated as a custom attribute
* and forwarded to the API as an extra `field` parameter.
*/
const KNOWN_SPAN_FIELDS = new Set([
// API names (in SPAN_FIELDS)
"id",
"parent_span",
"span.op",
"description",
"span.duration",
"timestamp",
"project",
"transaction",
"trace",
// Output aliases (from spanListItemToFlatSpan)
"span_id",
"parent_span_id",
"op",
"duration_ms",
"start_timestamp",
"project_slug",
]);
/** Field group aliases that expand to curated field sets */
const FIELD_GROUP_ALIASES: Record<string, string[]> = {
gen_ai: [
"gen_ai.usage.input_tokens",
"gen_ai.usage.output_tokens",
"gen_ai.request.model",
"gen_ai.system",
],
};
/**
* Extract field names from --fields that need additional API requests.
*
* Expands group aliases (e.g., `gen_ai` → four OTEL attribute fields) and
* filters out names already covered by the default SPAN_FIELDS request.
*
* @param fields - Raw --fields values from the CLI
* @returns Deduplicated extra API field names, or undefined if none are needed
*/
function extractExtraApiFields(
fields: string[] | undefined
): string[] | undefined {
Eif (!fields?.length) {
return;
}
const expanded = new Set<string>();
for (const f of fields) {
const alias = FIELD_GROUP_ALIASES[f];
if (alias) {
for (const a of alias) {
expanded.add(a);
}
} else {
expanded.add(f);
}
}
// Remove anything already requested by SPAN_FIELDS or its output aliases
for (const known of KNOWN_SPAN_FIELDS) {
expanded.delete(known);
}
return expanded.size > 0 ? Array.from(expanded) : undefined;
}
/** Accepted values for the --sort flag (matches trace list) */
const VALID_SORT_VALUES: SpanSortValue[] = ["date", "duration"];
/** Default sort order for span results */
const DEFAULT_SORT: SpanSortValue = "date";
/** Default time period for span queries */
const DEFAULT_PERIOD = "7d";
/** Pagination storage key for trace-scoped span listing */
export const PAGINATION_KEY = "span-list";
/** Pagination storage key for project-scoped span listing */
export const PROJECT_PAGINATION_KEY = "span-search";
/** Command name used in resolver error messages (project mode) */
const COMMAND_NAME = "span list";
/** Usage hint for trace-mode ContextError messages */
const TRACE_USAGE_HINT = "sentry span list [<org>/<project>/]<trace-id>";
/**
* Parse --limit flag, delegating range validation to shared utility.
*/
function parseLimit(value: string): number {
return validateLimit(value, 1, LIST_MAX_LIMIT);
}
/**
* Parse and validate sort flag value.
*
* @throws Error if value is not "date" or "duration"
*/
export function parseSort(value: string): SpanSortValue {
if (!VALID_SORT_VALUES.includes(value as SpanSortValue)) {
throw new Error(
`Invalid sort value. Must be one of: ${VALID_SORT_VALUES.join(", ")}`
);
}
return value as SpanSortValue;
}
/**
* Disambiguate span list positional arguments.
*
* Thin wrapper around {@link parseDualModeArgs} that binds the
* trace-mode usage hint for span list.
*/
export function parseSpanListArgs(
args: string[]
): ReturnType<typeof parseDualModeArgs> {
return parseDualModeArgs(args, TRACE_USAGE_HINT);
}
// ---------------------------------------------------------------------------
// Next-page hints
// ---------------------------------------------------------------------------
/** Append active non-default flags to a base next-page command. */
function appendFlagHints(
base: string,
flags: Pick<ListFlags, "sort" | "query" | "period">
): string {
const parts: string[] = [];
Iif (flags.sort !== DEFAULT_SORT) {
parts.push(`--sort ${flags.sort}`);
}
if (flags.query) {
parts.push(`-q "${flags.query}"`);
}
appendPeriodHint(parts, flags.period, DEFAULT_PERIOD);
return parts.length > 0 ? `${base} ${parts.join(" ")}` : base;
}
/** Build the CLI hint for fetching the next page in trace mode. */
function traceNextPageHint(
org: string,
project: string,
traceId: string,
flags: Pick<ListFlags, "sort" | "query" | "period">
): string {
return appendFlagHints(
`sentry span list ${org}/${project}/${traceId} -c next`,
flags
);
}
/** Build the CLI hint for fetching the previous page in trace mode. */
function tracePrevPageHint(
org: string,
project: string,
traceId: string,
flags: Pick<ListFlags, "sort" | "query" | "period">
): string {
return appendFlagHints(
`sentry span list ${org}/${project}/${traceId} -c prev`,
flags
);
}
/** Build the CLI hint for fetching the next page in project mode. */
function projectNextPageHint(
org: string,
project: string,
flags: Pick<ListFlags, "sort" | "query" | "period">
): string {
return appendFlagHints(`sentry span list ${org}/${project} -c next`, flags);
}
/** Build the CLI hint for fetching the previous page in project mode. */
function projectPrevPageHint(
org: string,
project: string,
flags: Pick<ListFlags, "sort" | "query" | "period">
): string {
return appendFlagHints(`sentry span list ${org}/${project} -c prev`, flags);
}
// ---------------------------------------------------------------------------
// Output config types and formatters
// ---------------------------------------------------------------------------
/** Structured data returned by the command for both JSON and human output */
type SpanListData = {
/** Flattened span items for display */
flatSpans: FlatSpan[];
/** Whether more results are available beyond the limit */
hasMore: boolean;
/** Whether a previous page exists (for bidirectional hints) */
hasPrev?: boolean;
/** Opaque cursor for fetching the next page (null/undefined when no more) */
nextCursor?: string | null;
/** The trace ID being queried (only in trace mode) */
traceId?: string;
/** Org slug for project-mode header */
org?: string;
/** Project slug for project-mode header */
project?: string;
/** Extra attribute names from --fields for human table columns */
extraAttributes?: string[];
};
/**
* Format span list data for human-readable terminal output.
*
* Uses `renderMarkdown()` for the header and `formatSpanTable()` for the table,
* ensuring proper rendering in both TTY and plain output modes.
* When extra attributes are present (from --fields API expansion), they are
* appended as additional table columns.
*/
function formatSpanListHuman(data: SpanListData): string {
if (data.flatSpans.length === 0) {
return data.hasMore
? "No spans on this page."
: "No spans matched the query.";
}
const parts: string[] = [];
if (data.traceId) {
parts.push(renderMarkdown(`Spans in trace \`${data.traceId}\`:\n`));
} else {
parts.push(`Spans in ${data.org}/${data.project}:\n\n`);
}
parts.push(formatSpanTable(data.flatSpans, data.extraAttributes));
return parts.join("\n");
}
/**
* Transform span list data for JSON output.
*
* Produces a `{ data: [...], hasMore, nextCursor? }` envelope matching the
* standard paginated list format. Applies `--fields` filtering per element.
*/
function jsonTransformSpanList(data: SpanListData, fields?: string[]): unknown {
const items =
fields && fields.length > 0
? data.flatSpans.map((item) => filterFields(item, fields))
: data.flatSpans;
const envelope: Record<string, unknown> = {
data: items,
hasMore: data.hasMore,
hasPrev: !!data.hasPrev,
};
Eif (
data.nextCursor !== null &&
data.nextCursor !== undefined &&
data.nextCursor !== ""
) {
envelope.nextCursor = data.nextCursor;
}
return envelope;
}
// ---------------------------------------------------------------------------
// Mode handlers — extracted from func() to stay under biome complexity limit
// ---------------------------------------------------------------------------
/** Shared context passed to mode handlers from the Stricli command function. */
type ModeContext = {
cwd: string;
flags: ListFlags;
/** Parsed time range from the --period flag */
timeRange: TimeRange;
/** Extra API field names derived from --fields (undefined when none needed) */
extraApiFields?: string[];
};
/**
* Handle trace mode: list spans within a specific trace.
*
* Resolves the trace target, builds a query prefixed with `trace:{id}`,
* and returns the result with a trace-specific header and hints.
*/
async function handleTraceMode(
parsed: ParsedTraceTarget,
ctx: ModeContext
): Promise<{ output: SpanListData; hint?: string }> {
const { flags, cwd, extraApiFields, timeRange } = ctx;
warnIfNormalized(parsed, "span.list");
const { traceId, org, project } = await resolveTraceOrgProject(
parsed,
cwd,
TRACE_USAGE_HINT
);
const queryParts = [`trace:${traceId}`];
if (flags.query) {
queryParts.push(translateSpanQuery(flags.query));
}
const apiQuery = queryParts.join(" ");
const contextKey = buildPaginationContextKey(
"span",
`${org}/${project}/${traceId}`,
{ sort: flags.sort, q: flags.query, period: serializeTimeRange(timeRange) }
);
const { cursor, direction } = resolveCursor(
flags.cursor,
PAGINATION_KEY,
contextKey
);
const { data: spanItems, nextCursor } = await withProgress(
{ message: `Fetching spans (up to ${flags.limit})...`, json: flags.json },
() =>
listSpans(org, project, {
query: apiQuery,
sort: flags.sort,
limit: flags.limit,
cursor,
...timeRangeToApiParams(timeRange),
extraFields: extraApiFields,
allProjects: true,
})
);
// Update pagination state (handles both advance and truncation)
advancePaginationState(PAGINATION_KEY, contextKey, direction, nextCursor);
const hasPrev = hasPreviousPage(PAGINATION_KEY, contextKey);
const flatSpans = spanItems.map((item) =>
spanListItemToFlatSpan(item, extraApiFields)
);
const hasMore = !!nextCursor;
const nav = paginationHint({
hasPrev,
hasMore,
prevHint: tracePrevPageHint(org, project, traceId, flags),
nextHint: traceNextPageHint(org, project, traceId, flags),
});
let hint: string | undefined;
Iif (flatSpans.length === 0 && nav) {
hint = `No spans on this page. ${nav}`;
} else if (flatSpans.length > 0) {
const countText = `Showing ${flatSpans.length} span${flatSpans.length === 1 ? "" : "s"}.`;
hint = nav
? `${countText} ${nav}`
: `${countText} Use 'sentry span view ${traceId} <span-id>' to view span details.`;
}
return {
output: {
flatSpans,
hasMore,
hasPrev,
nextCursor,
traceId,
extraAttributes: extraApiFields,
},
hint,
};
}
/**
* Handle project mode: list spans across the entire project.
*
* Resolves the org/project target and queries the spans dataset
* without a trace ID filter.
*/
async function handleProjectMode(
target: string | undefined,
ctx: ModeContext
): Promise<{ output: SpanListData; hint?: string }> {
const { flags, cwd, extraApiFields, timeRange } = ctx;
const { org, project } = await resolveOrgProjectFromArg(
target,
cwd,
COMMAND_NAME
);
const apiQuery = flags.query ? translateSpanQuery(flags.query) : undefined;
const contextKey = buildPaginationContextKey(
"span-search",
`${org}/${project}`,
{ sort: flags.sort, q: flags.query, period: serializeTimeRange(timeRange) }
);
const { cursor, direction } = resolveCursor(
flags.cursor,
PROJECT_PAGINATION_KEY,
contextKey
);
const { data: spanItems, nextCursor } = await withProgress(
{ message: `Fetching spans (up to ${flags.limit})...`, json: flags.json },
() =>
listSpans(org, project, {
query: apiQuery,
sort: flags.sort,
limit: flags.limit,
cursor,
...timeRangeToApiParams(timeRange),
extraFields: extraApiFields,
})
);
// Update pagination state (handles both advance and truncation)
advancePaginationState(
PROJECT_PAGINATION_KEY,
contextKey,
direction,
nextCursor
);
const hasPrev = hasPreviousPage(PROJECT_PAGINATION_KEY, contextKey);
const flatSpans = spanItems.map((item) =>
spanListItemToFlatSpan(item, extraApiFields)
);
const hasMore = !!nextCursor;
const nav = paginationHint({
hasPrev,
hasMore,
prevHint: projectPrevPageHint(org, project, flags),
nextHint: projectNextPageHint(org, project, flags),
});
let hint: string | undefined;
Iif (flatSpans.length === 0 && nav) {
hint = `No spans on this page. ${nav}`;
} else if (flatSpans.length > 0) {
const countText = `Showing ${flatSpans.length} span${flatSpans.length === 1 ? "" : "s"}.`;
hint = nav
? `${countText} ${nav}`
: `${countText} Use 'sentry span view <trace-id> <span-id>' to view span details.`;
}
return {
output: {
flatSpans,
hasMore,
hasPrev,
nextCursor,
org,
project,
extraAttributes: extraApiFields,
},
hint,
};
}
// ---------------------------------------------------------------------------
// Command definition
// ---------------------------------------------------------------------------
export const listCommand = buildListCommand("span", {
docs: {
brief: "List spans in a project or trace",
fullDescription:
"List spans from a Sentry project, or within a specific trace.\n\n" +
"Project mode (no trace ID):\n" +
" sentry span list # auto-detect from DSN or config\n" +
" sentry span list <org>/<project> # explicit org and project\n" +
" sentry span list <project> # find project across all orgs\n\n" +
`${TARGET_PATTERN_NOTE}\n\n` +
"Trace mode (provide a 32-char trace ID):\n" +
" sentry span list <trace-id> # auto-detect org/project\n" +
" sentry span list <org>/<project>/<trace-id> # explicit\n" +
" sentry span list <project> <trace-id> # find project + trace\n\n" +
"Pagination:\n" +
" sentry span list -c next # fetch next page (project mode)\n" +
" sentry span list -c prev # fetch previous page\n" +
" sentry span list <trace-id> -c next # fetch next page (trace mode)\n\n" +
"Examples:\n" +
" sentry span list # List recent spans in project\n" +
' sentry span list -q "op:db" # Find all DB spans\n' +
' sentry span list -q "duration:>100ms" # Slow spans\n' +
" sentry span list --period 24h # Last 24 hours only\n" +
" sentry span list --sort duration # Sort by slowest first\n" +
" sentry span list <trace-id> # Spans in a specific trace\n" +
' sentry span list <trace-id> -q "op:db" # DB spans in a trace\n\n' +
"Alias: `sentry spans` → `sentry span list`",
},
output: {
human: formatSpanListHuman,
jsonTransform: jsonTransformSpanList,
schema: SpanListItemSchema,
},
parameters: {
positional: {
kind: "array",
parameter: {
placeholder: "org/project/trace-id",
brief: "[<org>/<project>] or [<org>/<project>/]<trace-id>",
parse: String,
},
},
flags: {
limit: {
kind: "parsed",
parse: parseLimit,
brief: `Number of spans (<=${LIST_MAX_LIMIT})`,
default: String(LIST_DEFAULT_LIMIT),
},
query: {
kind: "parsed",
parse: sanitizeQuery,
brief:
'Filter spans (e.g., "op:db", "project:backend", "project:[cli,api]")',
optional: true,
},
sort: {
kind: "parsed",
parse: parseSort,
brief: `Sort order: ${VALID_SORT_VALUES.join(", ")}`,
default: DEFAULT_SORT,
},
period: LIST_PERIOD_FLAG,
},
aliases: {
...PERIOD_ALIASES,
n: "limit",
q: "query",
s: "sort",
},
},
async *func(this: SentryContext, flags: ListFlags, ...args: string[]) {
const { cwd } = this;
const timeRange = flags.period;
const parsed = parseSpanListArgs(args);
const extraApiFields = extractExtraApiFields(flags.fields);
const modeCtx: ModeContext = {
cwd,
flags,
timeRange,
extraApiFields,
};
const { output, hint } =
parsed.mode === "trace"
? await handleTraceMode(parsed.parsed, modeCtx)
: await handleProjectMode(parsed.target, modeCtx);
yield new CommandOutput(output);
return { hint };
},
});
|