Skip to content

test(test-utils): Add fetchSpanAttributes to read span attributes via the sentry CLI - #24514

Open
JPeer264 wants to merge 1 commit into
developfrom
jp/test-utils-fetch-span-attributes
Open

JPeer264 wants to merge 1 commit into
developfrom
jp/test-utils-fetch-span-attributes

Conversation

@JPeer264

@JPeer264 JPeer264 commented Sep 18, 2026

Copy link
Copy Markdown
Member

This is helpful for the stacked PR where we add a test for workers-ai

Adds fetchSpanAttributes(traceId, spanId) to @sentry-internal/test-utils/cli, so send-to-sentry E2E tests can assert span attributes and not only the span op.

sentry trace view --json looks like it returns span attributes, but it silently drops all of them for most spans: the trace-items endpoint sends int attribute values as strings, the CLI's schema (0.44.1 and 0.45.0) expects numbers, and a failed schema check only logs Could not fetch details for span. The helper calls the same endpoint through sentry api, which does no schema check. The spawn and credential handling moved into a shared runner so both helpers use the E2E token the same way.

🤖 Generated with Claude Code

… the sentry CLI

`sentry trace view --json` drops all attributes of a span when the
trace-items endpoint returns an `int` attribute, because the endpoint sends
those values as strings and the CLI's schema rejects them. Read the
attributes through `sentry api` instead, and share the CLI runner with
fetchTrace.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@JPeer264
JPeer264 added this pull request to stack #24516 September 18, 2026 18:47
@JPeer264 JPeer264 self-assigned this Sep 18, 2026
@JPeer264
JPeer264 marked this pull request as ready for review September 18, 2026 18:49
Comment on lines +102 to +103
const { attributes } = JSON.parse(result.stdout) as { attributes: { name: string; value: unknown }[] };
return Object.fromEntries(attributes.map(({ name, value }) => [name, value]));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The fetchSpanAttributes function may crash with a TypeError because it calls .map() on the attributes property from an API response without verifying its existence or type.
Severity: MEDIUM

Suggested Fix

Add a guard to check that the attributes property exists and is an array before attempting to call .map() on it. If attributes is missing or not an array, handle it gracefully, for example by treating it as an empty array or throwing a more informative error.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: dev-packages/test-utils/src/cli.ts#L102-L103

Potential issue: The `fetchSpanAttributes` function parses a JSON response from the
Sentry API and uses a TypeScript type assertion to assume the presence of an
`attributes` key. It then immediately calls `.map()` on the destructured `attributes`
property. If the API returns a successful response but the JSON body lacks the
`attributes` key, or its value is `null`, the code will throw a `TypeError` when
attempting to call `.map()` on a non-array value. This lack of a defensive check makes
the code vulnerable to crashes from unexpected but valid API response structures, such
as a future schema change.

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.09 kB - -
@sentry/browser - with treeshaking flags 27.35 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.26 kB - -
@sentry/browser (incl. Tracing) 50.56 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 50.58 kB - -
@sentry/browser (incl. Tracing, Profiling) 53.56 kB - -
@sentry/browser (incl. Tracing, Replay) 90.11 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 79.21 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 94.81 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 107.78 kB - -
@sentry/browser (incl. Feedback) 46.62 kB - -
@sentry/browser (incl. sendFeedback) 34.15 kB - -
@sentry/browser (incl. FeedbackAsync) 39.26 kB - -
@sentry/browser (incl. Metrics) 30.1 kB - -
@sentry/browser (incl. Logs) 30.35 kB - -
@sentry/browser (incl. Metrics & Logs) 31.02 kB - -
@sentry/react 30.84 kB - -
@sentry/react (incl. Tracing) 52.88 kB - -
@sentry/vue 36.34 kB - -
@sentry/vue (incl. Tracing) 52.86 kB - -
@sentry/svelte 29.11 kB - -
CDN Bundle 30.8 kB - -
CDN Bundle (incl. Tracing) 51.1 kB - -
CDN Bundle (incl. Logs, Metrics) 33.06 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 53.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73.75 kB - -
CDN Bundle (incl. Tracing, Replay) 88.66 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 90.58 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 94.7 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 96.71 kB - -
CDN Bundle - uncompressed 91.16 kB - -
CDN Bundle (incl. Tracing) - uncompressed 152.53 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 97.73 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 158.49 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 227.14 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 272.1 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 278.04 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 285.81 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 291.74 kB - -
@sentry/nextjs (client) 55.21 kB - -
@sentry/sveltekit (client) 50.99 kB - -
@sentry/core/server 39.98 kB - -
@sentry/core/browser 13.66 kB - -
@sentry/node 133.34 kB +0.02% +26 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.17 kB - -
@sentry/node - without tracing 89.93 kB +0.03% +21 B 🔺
@sentry/node - without channel injection 112.15 kB +0.02% +15 B 🔺
@sentry/aws-serverless 98.19 kB +0.03% +21 B 🔺
@sentry/cloudflare (withSentry) - minified 204.69 kB - -
@sentry/cloudflare (withSentry) 509.25 kB - -

View base workflow run

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