Skip to content

feat(sentry): N+1 hint in the trace, trace facet for mails and HTTP dumps - #492

Merged
butschster merged 1 commit into
masterfrom
feat/trace-n-plus-one
Nov 22, 2026
Merged

butschster merged 1 commit into
masterfrom
feat/trace-n-plus-one

Conversation

@butschster

Copy link
Copy Markdown
Member

Server part of the request timeline (concept 3 of the logs / queues / request-timeline set, option B: Sentry data plus logs, mails and HTTP calls linked by trace id). The Monolog trace facet came with #491.

Change

  • N+1 hint. GET /api/sentry/traces/{traceId} gets n_plus_one: database spans (peer_type = db) of one transaction whose query, normalized, runs 5 or more times. Most repeated first. Each group has transaction_id, query, count, total_ms, origin, span_ids.
    • Normalizing: string and number literals, $N placeholders → ?; IN (…) and VALUES (…), (…) of any length → one ?; whitespace collapsed.
    • total_ms is summed from span start/end times (two decimals): repeated queries usually take under 1 ms, and duration_ms is whole milliseconds (it showed 0 ms live).
    • origin is code.filepath:code.lineno of the first span in the group that has one.
  • trace facet for mails and HTTP dumps, so GET /api/events?trace=<id> returns them for the timeline. Shared parser event.TraceIDFromHeaders: sentry-trace, then traceparent, then X-Trace-Id; 32 hex, not all zero, lower-cased.
    • SMTP: headers of the raw message. The app has to add one (mailers send none).
    • HTTP dump: Sentry and OTel SDKs add sentry-trace/traceparent to outgoing calls themselves, so Laravel Http:: calls land on the timeline with no app setup.
    • Old events are not re-indexed (the facet backfill already ran once).
  • Review notes from feat(logs): full-text search, per-minute histogram and trace facet #491: ?bucket= above 366 days → 400 (was a 500 on overflow); NUL bytes dropped from ?q= (was a 500); OpenAPI says case folding for words under 3 characters is ASCII only.
  • OpenAPI: n_plus_one, SentryNPlusOne, bucket maximum.

Facts for docs and UI

  • sentry-laravel sends the query's place in code only for queries slower than SENTRY_TRACE_SQL_ORIGIN_THRESHOLD_MS (100 ms by default). Live with 0: direct queries got routes/web.php:256, but lazy-loaded relations ($post->comments()->count(), $post->user) got none: the SDK looks only 20 stack frames deep. So the typical N+1 usually comes without file and line; the hint still shows the query, count and time. Don't promise file:line.
  • Mail: add X-Trace-Id (or sentry-trace) with the current trace id, e.g. in Laravel $message->getHeaders()->addTextHeader('X-Trace-Id', (string) \Sentry\SentrySdk::getCurrentHub()->getSpan()?->getTraceId()).
  • Monolog: put trace_id into extra or context (from feat(logs): full-text search, per-minute histogram and trace facet #491).
  • Ray / VarDumper: no trace field, not linked.

Tests

  • TestNormalizeSQL (MySQL backticks, Postgres $N, quoted strings with '', IN/VALUES lists, identifiers with digits), TestDetectNPlusOne (threshold, per transaction, origin with numeric and string line).
  • TestTraceDetail_NPlusOne_Laravel: a real request from sentry-laravel 4.31.0 (fixture testdata/sdk/laravel-n1, recorded from the buggregator/examples app): two groups of 30, total_ms > 0, no origin.
  • TestTraceIDFromHeaders, SMTP and HTTP-dump facet tests, histogram/q edge cases in internal/server/http.
  • go vet ./... clean; go test -count=1 ./...: 29 packages ok.

Live check

Built binary, buggregator/examples (Laravel 12 + sentry-laravel 4.31.0) in Docker, SENTRY_TRACE_SQL_ORIGIN_THRESHOLD_MS=0. Page /n1 loops over 28 posts (author + comment count), logs through Monolog with trace_id, sends a mail with X-Trace-Id, calls an http-dump URL with Http::post.

## GET /n1 trace 6c63c11c784645bea611bb2850ca0f9d
transaction: /n1 | spans: 62
n_plus_one: 28 x 0.62 ms | select * from "users" where "users"."id" = ? limit ? | origin: None
n_plus_one: 28 x 0.62 ms | select count(*) as aggregate from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null | origin: None
GET /api/events?trace=6c63c11c784645bea611bb2850ca0f9d
  http-dump
  smtp
  monolog
## GET /n2 trace a63ed52c8366415fb3a4644d9e19f09c (direct queries + a 28x loop)
n_plus_one: 28 x 0.96 ms | select count(*) as aggregate from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null | origin: None
origin on single query: select 1 -> /routes/web.php:256
origin on single query: select * from "posts" limit 1 -> /routes/web.php:257
origin on single query: select * from "posts" -> /routes/web.php:258

Not done here: an MCP tool for the trace (none exists yet); the timeline UI is the frontend item.

Made with harnsy

…umps

- GET /api/sentry/traces/{traceId} gets n_plus_one: database spans of one
  transaction whose normalized query (literals, $N, IN/VALUES lists -> ?)
  runs 5+ times; count, total ms from precise span times, origin, span ids.
- trace facet for SMTP (X-Trace-Id, sentry-trace, traceparent headers) and
  HTTP dumps (sentry-trace/traceparent that SDKs add to outgoing calls), so
  GET /api/events?trace=<id> returns them for the request timeline.
- Review notes from #491: ?bucket= above 366 days is a 400, NUL bytes are
  dropped from ?q=, OpenAPI notes ASCII-only case folding for short words.
- Fixture: real sentry-laravel 4.31.0 transaction of an N+1 page.
@butschster butschster added the type: feature Net-new functionality label Nov 22, 2026
@butschster
butschster merged commit 9e15d43 into master Nov 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Net-new functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant