Skip to content

feat(log): show custom attributes in log view#914

Merged
betegon merged 9 commits intomainfrom
feat/log-view-custom-attributes
May 5, 2026
Merged

feat(log): show custom attributes in log view#914
betegon merged 9 commits intomainfrom
feat/log-view-custom-attributes

Conversation

@betegon
Copy link
Copy Markdown
Member

@betegon betegon commented May 4, 2026

sentry log view showed a fixed set of fields. Custom attributes users attach to their logs — visible in the Sentry UI — were never fetched or displayed.

sentry log list already supported custom attributes via --fields before this work. No changes there.

How it works

The Sentry UI calls a trace-items detail endpoint when expanding a log row:

GET /projects/{org}/{project}/trace-items/{itemId}/?item_type=logs&trace_id={traceId}

It returns every attribute on the log without needing to enumerate field names. log view now does the same: after getLogs fetches standard fields (including trace), getLogItemDetail is called in parallel for each log. formatLogDetails renders all non-standard attributes in a Custom Attributes section automatically. --fields filters that section when you only want specific attributes.

The endpoint is EXPERIMENTAL in Sentry and not yet in @sentry/api (generated from getsentry/sentry-api-schema), so it uses apiRequestToRegion directly — same pattern as listTraceLogs. Attribute types mirror TraceItemResponseAttribute:
https://github.com/getsentry/sentry/blob/8a4f150b21b/static/app/views/explore/hooks/useTraceItemDetails.tsx#L85-L89

If the endpoint is unavailable (no trace ID on the log, or request fails), the formatter degrades gracefully and shows only standard fields.

Type consolidation

PR #623 defined TraceItemAttribute and TraceItemDetail as plain TS types in traces.ts. This PR moves them to src/types/sentry.ts with Zod schemas (.passthrough() so meta/links from the spans endpoint are preserved), exports them via the types barrel, and re-exports from traces.ts for existing callers. getSpanDetails also gets schema validation for parity with getLogItemDetail.

Usage

# Shows ALL custom attributes automatically — no config needed
sentry log view myorg/myproject <id>

# Limits the Custom Attributes section to specific fields
sentry log view myorg/myproject <id> --fields order.id,user.tier

# JSON output: unchanged
sentry log view myorg/myproject <id> --json

getLogs and getLogsBatch now accept extraFields, which are merged with
the default DETAILED_LOG_FIELDS before the API request — same pattern
as listLogs. formatLogDetails renders a Custom Attributes section for
any fields that were explicitly requested. The KNOWN_DETAIL_FIELDS
blocklist approach was avoided; instead extraFields flows through
LogViewData so the formatter only shows what was asked for.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-914/

Built to branch gh-pages at 2026-05-05 14:08 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Codecov Results 📊

6678 passed | Total: 6678 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +14
Passed Tests 📈 +14
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 87.06%. Project has 13508 uncovered lines.
✅ Project coverage is 76.69%. Comparing base (base) to head (head).

Files with missing lines (3)
File Patch % Lines
src/lib/api/logs.ts 48.00% ⚠️ 13 Missing
src/commands/log/view.ts 87.23% ⚠️ 6 Missing
src/lib/formatters/log.ts 95.31% ⚠️ 3 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    76.62%    76.69%    +0.07%
==========================================
  Files          303       303         —
  Lines        57821     57961      +140
  Branches         0         0         —
==========================================
+ Hits         44308     44453      +145
- Misses       13513     13508        -5
- Partials         0         0         —

Generated by Codecov Action

Replace the --fields-only approach with the trace-items detail endpoint
(/projects/{org}/{project}/trace-items/{itemId}/?item_type=logs) which
returns every attribute on the log without requiring field enumeration —
the same call the Sentry UI makes when expanding a log row.

After getLogs fetches standard fields (including trace), getLogItemDetail
is called in parallel for each log that has a trace ID. formatLogDetails
renders all non-standard attributes in a Custom Attributes section.
--fields acts as a filter on that section rather than an adder.

The endpoint is EXPERIMENTAL and not yet in @sentry/api (getsentry/
sentry-api-schema), so it uses apiRequestToRegion directly, matching
the listTraceLogs pattern. Types mirror TraceItemResponseAttribute:
https://github.com/getsentry/sentry/blob/8a4f150b21b/static/app/views/explore/hooks/useTraceItemDetails.tsx#L85-L89

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@betegon betegon changed the title feat(log): support custom attributes in log view via --fields feat(log): show custom attributes in log view May 5, 2026
TraceItemAttribute and TraceItemDetail were defined twice: as plain TS
types in traces.ts (PR #623) and as Zod schemas in sentry.ts (this
branch). Remove the plain types from traces.ts and import from the
types barrel instead, re-exporting for existing callers.

Also:
- Add .passthrough() to TraceItemDetailSchema so meta/links from the
  spans endpoint are preserved without failing validation
- Add schema validation to getSpanDetails, matching getLogItemDetail
- Rename SHOWN_IN_STANDARD_SECTIONS → REDUNDANT_LOG_DETAIL_ATTRS to
  mirror REDUNDANT_DETAIL_ATTRS naming in traces.ts

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@betegon betegon marked this pull request as ready for review May 5, 2026 11:14
- Wrap getLogsBatch config+extraFields into options object (useMaxParams)
- Use explicit return statement in catch to satisfy both noEmptyBlock and
  noUselessUndefined rules
- Switch TraceItem re-export in traces.ts to export-from syntax
  (noExportedImports), remove unused local import of TraceItemAttribute

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/lib/api/traces.ts Outdated
Comment thread src/commands/log/view.ts Outdated
Comment thread src/lib/api/logs.ts Outdated
- Add "array" variant to TraceItemAttributeSchema (Sentry backend gates
  this via organizations:trace-item-details-array-fields; without it the
  strict discriminated union throws and fetchMultiSpanDetails silently
  drops the span's attributes)
- Cap parallel log detail fetches with pLimit(15), matching
  SPAN_DETAIL_CONCURRENCY used by fetchMultiSpanDetails in traces.ts
- Document why getLogItemDetail uses apiRequestToRegion directly (endpoint
  is EXPERIMENTAL and not yet in @sentry/api / sentry-api-schema)

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/lib/api/logs.ts
Comment thread src/commands/log/view.ts Outdated
betegon and others added 2 commits May 5, 2026 15:18
- Use limit.map() instead of Promise.all + manual wrapping, matching
  the fetchMultiSpanDetails pattern in traces.ts
- Extract shared getTraceItemDetail helper in traces.ts; getSpanDetails
  and getLogItemDetail both delegate to it — only item_type differs
  (options object keeps param count at 4)

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…etSpanDetails

Covers the three uncovered areas:

- formatLogDetails: custom attributes rendering, REDUNDANT_LOG_DETAIL_ATTRS
  filtering, --fields filter, fallback path (extraFields without allAttributes),
  and empty allAttributes
- view.func: getLogItemDetail called for traced logs, skipped for traceless
  logs, graceful degradation on failure, custom attributes appear in output
- traces: getSpanDetails delegates to trace-items endpoint with item_type=spans

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d0dd84e. Configure here.

Comment thread src/commands/log/view.ts Outdated
Comment thread src/lib/formatters/log.ts Outdated
betegon and others added 2 commits May 5, 2026 16:02
…stringify

- Skip getLogItemDetail calls when --json is set — jsonTransform only
  uses data.logs, not data.details, so the extra round-trips are wasted.
  Mirrors the shouldFetchDetails ternary pattern in trace/view.ts.
- Use JSON.stringify for "array" type attributes in the Custom Attributes
  section instead of String(), which produces "[object Object]" for
  arrays containing objects.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@betegon betegon merged commit 7344933 into main May 5, 2026
26 checks passed
@betegon betegon deleted the feat/log-view-custom-attributes branch May 5, 2026 15:01
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.

2 participants