--- name: perf-investigation description: >- Find which web or desktop app flows are slow, for whom, and why, using the Datadog MCP over the app's perf traces. Use when asked why the app, a view, boot, navigation or an interaction is slow, to rank slow flows, to compare platforms, OSes or versions, to check a performance change after a deploy, or to map a slow span to its code. --- # Perf investigation The web app (`service:web-app`, browser and Tauri desktop) exports `perf`-class traces: one root per boot, navigation, view or interaction, a child per phase, and HTTP spans that join backend services. Span names and required attributes are the [span model](../../../docs/agent-guide/observability.md#span-model); read it first. The dashboard is "Web app performance" (`infra/stacks/monitoring/dashboards/web-performance.ts`). ## How web-app spans look in Datadog Verified against production web-app spans: - The span name is `resource_name`. `operation_name` is the span kind (`Internal`, `http.client.request`); never group by it. HTTP client spans (`http ` in Tempo) are `operation_name:http.client.request` with the method as `resource_name`; group them by `@http.url` (no query string) and time them with `@http.response_headers_ms`. Their duration ends at the headers; `@http.body_ms` (and `@graphql.body_ms` on `graphql.transport`) is the time from there until the body was read, filtered by `@http.body_outcome:complete` (the span model lists the outcomes). - Span attributes are `@` (`@route.id`, `@flow.kind`, `@ui.work_to_ready_ms`). Resource attributes are facets too: `@app.platform` (`web`, `desktop`, `ios`, `android`), `@app.runtime`, `@os.type`, `@version` (group by `@version`; bare `version` returns nothing), and `env` (`prod`, `development`, `dev`). `@version` is the web bundle, which over-the-air updates bring to old binaries; the native binary is `@macro.native.build` (Android versionCode, iOS CFBundleVersion, desktop release build; numeric, so `@macro.native.build:>=28` works). Spans whose web bundle predates it have none. - Numeric attributes are measures (`pc75` over `@ui.elapsed_ms` works). Booleans arrive as strings: `@agent.session.load.cache_hit:true`. - `@duration` is nanoseconds; `ui.*_ms` fields are milliseconds; trace metrics (`trace.Internal`) are seconds. - Grouping by a facet a span lacks drops that span from the result. Pass `missing: "unknown"` in the SDK `groupBy` (the `aggregate_spans` tool cannot). Spans from builds before `os.type` shipped in browsers have it only on desktop; `@otel.user_agent:*Macintosh*` / `*Windows*` / `*Linux*` splits those. - Span search and aggregation see indexed spans only. The perf retention filter keeps every exported perf trace once deployed; without it, counts are a sample (diversity plus 1% flat). Use the `macro.web.*` span-based metrics or `trace.Internal{service:web-app} by {resource_name,version}` (roots only, 100% of ingested spans) for volume and percentiles. - DSS spans are `service:cloud-storage-service` in Datadog. ## Pitfalls - Production exports perf traces for 5% of users (a cohort that rotates weekly), and only where the `enable-browser-otel` PostHog flag is on. Weight counts by `1 / @macro.sampling.rate` to estimate all users. Small routes have few samples: report counts with every percentile. - Perf spans exist only from the release that shipped them; check `@version` before concluding a flow is fast or missing. - Exclude non-success outcomes from load percentiles: roots `@ui.outcome:success`, phases `@flow.outcome:success`. Views are roots (`@flow.kind:view`) or phases of `navigation`/`app.boot`; filter by `resource_name:view.*` to get both. - Phases overlap; never sum them. `.waited:false` means it was already loaded, not necessarily a cache hit. - `ui.long_task_*` and `ui.long_animation_frame_*` overlap each other and are not CPU time. ## Workflow Load `datadog/traces` once, then run each step as one `execute_code` script. `agg` is reused below: ```javascript import { client, v2 } from "@datadog/datadog-api-client"; const spans = new v2.SpansApi(client.createConfiguration()); const agg = (filter, compute, groupBy = []) => spans.aggregateSpans({ body: { data: { type: "aggregate_request", attributes: { filter: { from: "now-7d", to: "now", ...filter }, compute, groupBy } } } }) .then((r) => r.data.map((b) => ({ by: b.attributes.by, ...b.attributes.compute }))); ``` **1. Rank flows** by p75/p95 and volume: ```javascript return agg( { query: "service:web-app env:prod @macro.sampling.class:perf @flow.kind:* @ui.outcome:success" }, [ { aggregation: "count", type: "total" }, { aggregation: "pc75", metric: "@ui.elapsed_ms", type: "total" }, { aggregation: "pc95", metric: "@ui.elapsed_ms", type: "total" } ], [ { facet: "resource_name", limit: 30, sort: { type: "measure", aggregation: "pc95", metric: "@ui.elapsed_ms", order: "desc" } } ]); ``` Views (roots and phases): query `resource_name:view.* -@flow.outcome:cancelled -@ui.outcome:(cancelled OR hidden OR timeout)`, metric `@duration`, group by `@route.id`. **2. Split the slow flow** by who sees it: add `groupBy` entries for `@app.platform`, `@os.type`, `@app.runtime` and `@version`, each with `missing: "unknown"`, filtered to its `resource_name`. For trends, use `type: "timeseries", interval: "12h"` on every compute. **3. Break it into phases.** Phases carry the root's name, so aggregate them without trace queries: ```javascript return agg( { query: "service:web-app env:prod @flow.root:navigation @route.id:view-mail @flow.outcome:success" }, [ { aggregation: "count", type: "total" }, { aggregation: "pc75", metric: "@duration", type: "total" } ], [ { facet: "resource_name", limit: 30, sort: { type: "measure", aggregation: "pc75", metric: "@duration", order: "desc" } } ]); ``` Then fetch exemplars at the slow tail and read their trees: ```javascript const list = await spans.listSpansGet({ filterQuery: "service:web-app env:prod resource_name:view.view-mail @duration:>2000000000", filterFrom: "now-1d", filterTo: "now", sort: "-timestamp", pageLimit: 5 }); return list.data.map((s) => ({ trace: s.attributes.traceId, ns: s.attributes.custom?.duration })); ``` `get_datadog_trace` with each `trace` id gives every span's start and duration; compute each phase's offset from the root's start and look for the longest serial gap, not the longest span. Compare the root's `flow.ready_phase_count` and the `.waited` fields between fast and slow exemplars to separate cold loads from slow ones. **4. Correlate with the backend.** The same trace holds the `http ` and `graphql.transport` client spans and the server spans under them (`only_service_entry_spans: true` to summarise). Compare the client span with its server child: a large gap is network, queueing or auth refresh (`boot.auth.refresh`, `http POST /auth/jwt/refresh`), not the handler. To rank the endpoints perf traces wait on, aggregate `service:web-app @macro.sampling.class:perf operation_name:http.client.request` by `@http.url` over `@http.response_headers_ms`. A request is not done at its headers: on native the body crosses IPC in chunks and can take seconds longer. Compare body time by platform before blaming the server: ```javascript return agg( { query: "service:web-app env:prod @macro.sampling.class:perf operation_name:http.client.request @http.body_outcome:complete" }, [ { aggregation: "pc75", metric: "@http.body_ms", type: "total" }, { aggregation: "pc75", metric: "@http.response_headers_ms", type: "total" }, { aggregation: "count", type: "total" } ], [ { facet: "@app.platform", limit: 10 } ]); ``` A native fix ships in a binary, not the bundle: split by native build to see it. Spans from bundles older than the attribute fall in `unknown`: ```javascript return agg( { query: "service:web-app env:prod @app.platform:android operation_name:http.client.request @http.body_outcome:complete" }, [ { aggregation: "pc75", metric: "@http.body_ms", type: "total" }, { aggregation: "pc95", metric: "@http.body_ms", type: "total" }, { aggregation: "count", type: "total" } ], [ { facet: "@macro.native.build", limit: 20, missing: "unknown" } ]); ``` For `graphql.transport` use `resource_name:graphql.transport @graphql.body_outcome:complete` over `@graphql.body_ms`. The `macro.web.http.body_ms` and `macro.web.graphql.body_ms` span-based metrics cover every ingested perf span (by `app.platform`, `os.type`, `resource_name`). **5. Main thread and resources.** On roots, compare `@ui.work_to_ready_ms` with `@ui.long_animation_frame_blocking_ms` and `@ui.long_task_overlap_ms`: blocking close to the elapsed time means the main thread, not the network. `resource_name:app.runtime.sample` carries `@macro.memory.js_heap.used_bytes`, `@runtime.long_animation_frame.blocking_ms` and `@runtime.session_age_ms`; on the macOS desktop app it also carries process memory by role (`@macro.memory.{native,web_content,gpu,utility}.footprint_bytes`, check `@macro.memory.frontend.status`); bucket by session age with a histogram group: ```javascript return agg( { query: "service:web-app env:prod resource_name:app.runtime.sample" }, [ { aggregation: "pc75", metric: "@macro.memory.js_heap.used_bytes", type: "total" }, { aggregation: "count", type: "total" } ], [ { facet: "@runtime.session_age_ms", histogram: { interval: 600000, min: 0, max: 7200000 }, limit: 20 } ]); ``` **Rank what blocks the main thread.** Roots carry the up to three longest scripts of their long animation frames (fields in the [span model](../../../docs/agent-guide/observability.md#reading-phases)). Sum each slot's script time by invoker and source, then merge the slots in code. Strip the chunk hash so builds merge, and keep `source_char_position` from an exemplar to map back through that build's `.js.map`: ```javascript const slot = (n) => agg( { query: `service:web-app env:prod @flow.kind:* @ui.long_animation_frame_script_${n}.duration_ms:*` }, [ { aggregation: "count", type: "total" }, { aggregation: "sum", metric: `@ui.long_animation_frame_script_${n}.duration_ms`, type: "total" }, { aggregation: "sum", metric: `@ui.long_animation_frame_script_${n}.forced_style_layout_ms`, type: "total" } ], [ { facet: `@ui.long_animation_frame_script_${n}.invoker`, limit: 30, sort: { type: "measure", aggregation: "sum", metric: `@ui.long_animation_frame_script_${n}.duration_ms`, order: "desc" } }, { facet: `@ui.long_animation_frame_script_${n}.source_url`, limit: 30, sort: { type: "measure", aggregation: "sum", metric: `@ui.long_animation_frame_script_${n}.duration_ms`, order: "desc" } } ]); const norm = (url) => (url ?? "").replace(/-[A-Za-z0-9_-]{8}\.js$/, ".js"); const ranked = new Map(); const slots = await Promise.all([1, 2, 3].map(slot)); slots.forEach((rows, index) => { const field = (name) => `@ui.long_animation_frame_script_${index + 1}.${name}`; for (const { by, c0, c1, c2 } of rows) { const key = `${by[field("invoker")]} | ${norm(by[field("source_url")])}`; const total = ranked.get(key) ?? { scripts: 0, ms: 0, forcedLayoutMs: 0 }; ranked.set(key, { scripts: total.scripts + c0, ms: total.ms + c1, forcedLayoutMs: total.forcedLayoutMs + c2 }); } }); return [...ranked].sort((a, b) => b[1].ms - a[1].ms).slice(0, 20); ``` Add `@ui.operation`, `@route.id` or `@app.platform` to the query to rank one flow; add `@ui.long_animation_frame_script_1.source_function` as a group to split a chunk. Script time sums the longest scripts of overlapping frames, so it ranks what most often holds the thread during flows, not total CPU time; the same frame can count on two overlapping roots. Typing latency: `resource_name:input.typing_burst`, measure `@typing.event_duration_p95_ms`, group by `@typing.surface`. **6. Map the span to code.** Search the span name: `rg "'channel.messages'" apps/web/src` finds the `usePending`/`TraceFlow`/`beginInteraction` owner; `view.` comes from `lib/split-router/solid/outlet.tsx` with the route defined in the router; `http ` is the request path. The instrumentation skill explains each primitive. ## Verify locally first For Android, measure on an emulator with the benchmark harness ([README](../../../apps/web/scripts/android-benchmark/README.md)): it reads these same spans from a release build, so its numbers compare with the field. Run it with `--cpu-throttle 4` for main-thread-bound flows such as view switches: the emulator's WebView CPU is several times faster than an Android phone's. Before relying on a new or renamed span in Datadog, run the flow on a local stack and find it in Tempo (`live-debug` skill): `{ span.flow.kind != nil }` lists roots, `{ span.flow.root = "navigation" }` its phases. Local traces are 100% sampled. Perf data reaches Datadog only after the release ships. Report: the flows ranked (with counts), who is affected, the dominant phase or blocking work with exemplar trace links, the owning code, and what the sample size cannot tell.