Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ UI show, and the tools read it, with `--mcp` alone too. Its limit,
[`TRAP_TELEMETRY_MAX_SPANS`](web-ui.md#sentry-pages), applies: above it the least recently updated traces are
dropped. Clearing the Sentry pages of the web UI drops their traces for the tools too.

| Tool | Arguments | Result |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `list_traces` | `since` (cursor), `limit`, `project`, `source`, `errors_only`, `from`, `to`, `service`, `name`, `min_duration_ms`, `search`, `regex` | `traces`: `id`, `name`, `service`, `services`, `sources`, `projects`, `spans`, `duration_ms`, `error`, `updated` each; `cursor`; `has_more` |
| `get_trace` | `id`, `max_length` | The trace as plain text: a header line, then the tree of all its spans as the console prints OpenTelemetry traces |
| Tool | Arguments | Result |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `list_traces` | `since` (cursor), `limit`, `project`, `source`, `errors_only`, `from`, `to`, `service`, `name`, `min_duration_ms`, `search`, `regex` | `traces`: `id`, `name`, `service`, `services`, `sources`, `projects`, `spans`, `duration_ms`, `error`, `findings`, `updated` each; `cursor`; `has_more` |
| `get_trace` | `id`, `max_length` | The trace as plain text: a header line, the findings, then the tree of all its spans as the console prints OpenTelemetry traces |

- `list_traces` returns the traces updated after `since`, least recently updated first, at most `limit` of them
(20 by default, 100 at most). A trace is updated by every span or transaction that arrives for it, and an updated
Expand All @@ -317,6 +317,8 @@ dropped. Clearing the Sentry pages of the web UI drops their traces for the tool
list those of every part of the trace: a distributed trace may cross several.
- `spans` counts the spans and the transactions. `error` is true when one of them failed: an OpenTelemetry span of
the error status, a Sentry one of `internal_error`, `unknown_error` or `error`.
- `findings` counts the problems `get_trace` lists for the trace, of the listed projects: so far
[repeated queries (N+1)](web-ui.md#repeated-queries-n1).
- `project` is one project or a list, as in [the event filter](#filters): only the part of each trace sent to those
projects is listed and summed up. `source` is `sentry` or `otel`: only the traces with a part from it.
- `errors_only` lists only the traces with a failed span or transaction.
Expand All @@ -333,6 +335,17 @@ dropped. Clearing the Sentry pages of the web UI drops their traces for the tool
line holds the number of spans, the services, sources and projects, whether a span failed, and the time of the
last update. A Sentry span shows its op and description as its name, a kind read from its op (`http.server` is
`SERVER`, `http.client` `CLIENT`, `queue.publish` `PRODUCER`, `queue.process` `CONSUMER`, others `INTERNAL`), its `data` as attributes and a failure status as `ERROR`.
- `get_trace` lists the findings between the header line and the tree, the heaviest (the longest summed duration)
first, and leaves the section out when there are none. A finding is a line with its rule, the span count, the
summed duration, the transaction, the place in the code when the spans tell it and the ID of the first span,
then the query with its values as `?`. The rule `n_plus_one` is a [repeated query](web-ui.md#repeated-queries-n1):

```text
findings, the heaviest first:
- n_plus_one: 6 spans, 12.00 ms in total, in transaction "POST /checkout", from /app/src/Checkout/PriceCalculator.php:31, first id:3a9d5f1b7c2e4801
SELECT * FROM products WHERE id = ?
```

- `get_trace` cuts a text longer than `max_length` characters, 50 000 by default, and says so.

### User feedback
Expand Down
3 changes: 2 additions & 1 deletion docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ The Phar, the standalone binary and the Docker image do not include them, so the
- Traces join Sentry traces: a trace may mix both. The entry span of each service (a span without a parent,
with a remote parent, or a server or consumer span whose parent is in another service) is its transaction;
the service name is the `service.name` resource attribute. The UI shows span kinds, statuses, events, links,
the resource and the scope as `otel.*` attributes.
the resource and the scope as `otel.*` attributes. The query of a database span, for the
[repeated query check](web-ui.md#repeated-queries-n1), is its `db.query.text` or `db.statement` attribute.
- Log levels come from the severity number, else from the severity text.
- Each metric data point is one value: a monotonic sum is a `counter`, a gauge or a non-monotonic sum is a `gauge`,
and a histogram, exponential histogram or summary is a `distribution` whose value is the mean (`sum / count`),
Expand Down
42 changes: 41 additions & 1 deletion docs/web-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ The Sentry section has a page for each kind of data:
- Exceptions: the [Sentry](sentry.md) error events, one by one or grouped by type and message.
- Traces: the transactions and spans of [Sentry SDKs](sentry.md#what-an-envelope-gives) and
[OpenTelemetry](opentelemetry.md#how-the-ui-shows-otlp-data) exporters, one trace even when it crosses both,
with the waterfall, the call sequence between services, the related errors and logs.
with the waterfall, the call sequence between services, the related errors and logs. The request view of a
trace marks [repeated queries](#repeated-queries-n1) with an N+1 hint and groups their spans.
- Service map: the services and what they call (other services, databases, caches, queues) over the period,
with requests, errors and durations per service and per call, and the traces through each call. A call is a span
with a peer: a database or cache address, an HTTP host, a queue; it leads to the service that served it when
Expand Down Expand Up @@ -131,3 +132,42 @@ Memory is bounded by these limits; values below 1 fall back to the defaults:
Feedback has a fixed limit of 1 000 entries, cron jobs one of 200 jobs with 100 runs each and 8 MB of ping output
in all, the oldest runs losing theirs first.
These limits are apart from the [OpenTelemetry limits](opentelemetry.md#memory-limits) of the console.

### Repeated queries (N+1)

A trace reports an SQL query that one transaction runs 5 times or more, up to its values: the N+1 pattern of a loop
that loads a relation row by row. The trace page and the MCP tool [`get_trace`](mcp.md#traces) show the same
findings:

- Only SQL spans count: a Sentry span of op `db` or `db.*`, an OpenTelemetry span with `db.system` or
`db.system.name`, but not a Sentry `db.redis` span nor a span whose `db.system` is a store that takes no SQL:
Redis, Memcached, MongoDB, Elasticsearch, OpenSearch, Cassandra, Couchbase, CouchDB, DynamoDB, Cosmos DB, Neo4j,
HBase, Geode, Coherence. The query is the `db.query.text` attribute, else `db.statement`, else the span
description, where Sentry SDKs put it (an OpenTelemetry span name is a short summary such as
`SELECT shop.products`).
- A span belongs to the transaction it came in, else to the one up its parents: OpenTelemetry and Sentry span
streams send no transaction ID. Spans whose transaction has not arrived count together under the parent missing
from the trace, as the server span an OpenTelemetry batch exports after its children, else under their topmost
span.
- Two queries are the same when they differ only in values: string literals, numbers, `TRUE`, `FALSE` and the
placeholders `?`, `$1`, `:name` become `?`; an `IN (…)` list, the rows of `VALUES (…), (…)` or an `ARRAY[…]` of
any length become one `(?)` or `[?]`; comments go; keywords are upper-cased and spacing is made uniform. Names
stay as written, with their digits (`table2`), and so do `"quoted"` names and `::type` casts. A `?` or a quote
inside a string literal stays in the literal.
- The database is the `db.system` or `db.system.name` of the span, in any case. A backslash escapes a quote in a
literal of `mysql`, `mariadb`, `clickhouse`, `hive`, `spanner`, `gcp.spanner`, `bigquery` and `snowflake`, and
in an `E'…'` literal. Only a doubled quote does in `postgresql`, `sqlite`, `mssql`, `microsoft.sql_server`,
`oracle`, `oracle.db`, `db2`, `ibm.db2`, `h2`, `h2database`, `derby`, `hsqldb`, `cockroachdb`, `trino`,
`firebird`, `firebirdsql` and `teradata`. For any other database, and when the span names none, a backslash
escapes unless a literal of the query would then never end: then no backslash escapes in that query, so
`'C:\' AND b = 'y'` holds two literals. A literal cut off by the 32 KB limit below does not count as never
ending, so in a longer query of such a database a literal that really ends in a backslash takes the rest of the
part that is read.
- Trap reads the first 32 KB of a query: a longer one is shaped by that part and ends with `…`, and a bulk
`VALUES` list cut there still collapses. Queries that differ only after their first 32 KB count as one.
- Each finding has the query with its values as `?`, the span count, their summed duration, and the place in the
code, `code.filepath:code.lineno` (or `code.file.path:code.line.number`) of the first span that has it.
sentry-laravel sends it only for queries slower than `SENTRY_TRACE_SQL_ORIGIN_THRESHOLD_MS`, 100 ms by default.
- With a project chosen, only the spans of that project count, as if the others had not come.
- Trap finds them when a trace is read and keeps them until the trace changes, so a trace that grows as its spans
arrive is checked again.
6 changes: 6 additions & 0 deletions resources/payloads/sentry-checkout.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
"request": {"url": "http://shop.local/checkout", "method": "POST"},
"spans": [
{"trace_id": "{{hex32 trace}}", "span_id": "2e7b9d4a1c6f3058", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM carts WHERE user_id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.960}}", "timestamp": "{{ts -2.945}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4801", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9445}}", "timestamp": "{{ts -2.9425}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306, "code.filepath": "/app/src/Checkout/PriceCalculator.php", "code.lineno": 31, "code.function": "App\\Checkout\\PriceCalculator::price"}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4802", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9420}}", "timestamp": "{{ts -2.9400}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4803", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9395}}", "timestamp": "{{ts -2.9375}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4804", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9370}}", "timestamp": "{{ts -2.9350}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4805", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9345}}", "timestamp": "{{ts -2.9325}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "3a9d5f1b7c2e4806", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "SELECT * FROM products WHERE id = ?", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.9320}}", "timestamp": "{{ts -2.9300}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "7d3f1a9c5e2b8064", "parent_span_id": "9a1f3c5e7b2d4086", "op": "db.sql.query", "description": "INSERT INTO orders (user_id, total, status) VALUES (?, ?, ?)", "status": "ok", "origin": "auto.db.doctrine", "start_timestamp": "{{ts -2.930}}", "timestamp": "{{ts -2.900}}", "data": {"db.system": "mysql", "db.name": "shop", "server.address": "mysql", "server.port": 3306}},
{"trace_id": "{{hex32 trace}}", "span_id": "5b8e2c7f4a1d9036", "parent_span_id": "9a1f3c5e7b2d4086", "op": "cache.get", "description": "cart:1007", "status": "ok", "origin": "auto.cache", "start_timestamp": "{{ts -2.895}}", "timestamp": "{{ts -2.893}}", "data": {"cache.hit": true, "server.address": "redis"}},
{"trace_id": "{{hex32 trace}}", "span_id": "4c8a2d1f6b3e9075", "parent_span_id": "9a1f3c5e7b2d4086", "op": "http.client", "description": "POST https://payments.example.com/v1/charges", "status": "internal_error", "origin": "auto.http.client", "start_timestamp": "{{ts -2.880}}", "timestamp": "{{ts -2.410}}", "data": {"http.request.method": "POST", "url": "https://payments.example.com/v1/charges", "http.response.status_code": 502}}
Expand Down
2 changes: 1 addition & 1 deletion skills/trap/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ the agent reads back through Trap's MCP tools.

| Reference | Read when |
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [mcp.md](references/mcp.md) | Reading events, traces of Sentry and OpenTelemetry, cron jobs: the tools, filters, the script for agents without MCP |
| [mcp.md](references/mcp.md) | Reading events, traces of Sentry and OpenTelemetry, N+1 queries, cron jobs: the tools, filters, the script for agents without MCP |
| [dumps-and-logs.md](references/dumps-and-logs.md) | Seeing values or the code path at runtime: `trap()`, `tr()`, `td()`, `dump()`, `trap()::logger()` |
| [profiling.md](references/profiling.md) | Finding why code is slow or takes memory: `trap()::profile()`, the profilers, reading a profile; Xdebug function traces |
| [xdebug.md](references/xdebug.md) | Stopping at a line or an exception and stepping through PHP code: `--xdebug`, breakpoints, variables, `xdebug_*` tools |
Expand Down
11 changes: 10 additions & 1 deletion skills/trap/references/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The argument schema of each tool is in its `inputSchema` (`mcp.php tools <name>`
| `delete_event` | Deletes events by `id` or `ids` |
| `clear_events` | Deletes the events matching a filter, or all; pinned ones stay unless `include_pinned: true` |
| `list_traces` | Sentry and OpenTelemetry traces; filters by project, source, service, name, duration, errors |
| `get_trace` | One trace as a tree of its spans and transactions |
| `get_trace` | One trace as a tree of its spans and transactions, under its findings such as N+1 queries |
| `list_queue_jobs` | Queue jobs from `queue.process` attempts: retries, failures, errors, time in the queue |
| `list_feedback` | Sentry user feedback, newest first; by `project` or `event_id` of the error it is about |
| `list_crons` | Cron jobs of Sentry Crons check-ins and ping URLs with their newest runs, see [Cron jobs](#cron-jobs) |
Expand Down Expand Up @@ -113,6 +113,15 @@ error, in its `trace` context, opens the trace of the failed request. Slow trace
transactions pass 10 000 (`TRAP_TELEMETRY_MAX_SPANS`), and clearing the Sentry pages of the web UI drops them too.
OpenTelemetry logs and metrics have no MCP tool.

`findings` in `list_traces` counts the problems `get_trace` lists above the tree, heaviest first. `n_plus_one` is
one SQL query (Redis and other NoSQL stores don't count), values shown as `?`, run 5 times or more in one
transaction: the line gives the span count, the summed ms, the transaction, `from file:line` when the spans carry
`code.filepath` (sentry-laravel sends it for slow queries only) and the first span `id:` to find in the tree. Fix
it in that code: load the rows in one query (eager loading, `WHERE id IN (…)`), then rerun and check that the
finding is gone. The query is `db.query.text`, `db.statement` or the span description; spans without a transaction
count under the one up their parents, else under the parent span that has not arrived. With `project`, only the
spans of those projects count.

## Queue jobs

`list_queue_jobs` answers why a job failed or was retried: it groups the attempts (transactions and spans with op
Expand Down
Loading
Loading