Profile the token endpoint by wall clock #137

Merged
CrowderSoup merged 1 commit from claude/token-wall-clock into main 2026-11-24 23:41:51 +00:00
Owner

The token endpoint takes 1–2 s on some requests:

App /o/token/ timing
vTodo p50 1.3 s
vPages about 2 s
vJournal p95 3.1 s

Its CPU profile shows only about 10 ms per request, so the time is spent waiting. CPU sampling can't see waits, and GlitchTip keeps no per-request spans.

This adds a wall-clock sampler that runs only during WALL_CLOCK_ENDPOINTS requests, which for now is just /o/token/:

  • A thread reads the request thread's stack every 10 ms. Each sample is weighted by the real time since the previous one.
  • When the request ends, the thread sends the folded stacks to Pyroscope's /ingest (through the same edge and credentials the agent uses) as <service>.wall{…}.
  • Pyroscope stores that as the wall profile type for the same service, with the agent's labels plus endpoint.
  • The Pyroscope agent stays on CPU. Its oncpu=False setting applies to the whole process and would turn every profile into wall time.

Cost: the sampler runs only while a token request is in flight (a few hundred a week). Push failures are logged and never affect the response.

To use it: in Pyroscope, choose the service, profile type wall, and endpoint="/o/token/".

Verified:

  • A local run on a 0.6 s blocking call captured token_view;wait_for_row_lock 595 ms plus 26 ms of compute.
  • Replaying that push to Pyroscope read back as 621 ms of wall time under the service.
  • New tests cover the sampler, endpoint filtering and the ingest request.
  • Full suite passes.

🤖 Generated with Claude Code

The token endpoint takes 1–2 s on some requests: | App | `/o/token/` timing | |---|---| | vTodo | p50 1.3 s | | vPages | about 2 s | | vJournal | p95 3.1 s | Its CPU profile shows only about 10 ms per request, so the time is spent waiting. CPU sampling can't see waits, and GlitchTip keeps no per-request spans. This adds a wall-clock sampler that runs only during `WALL_CLOCK_ENDPOINTS` requests, which for now is just `/o/token/`: - A thread reads the request thread's stack every 10 ms. Each sample is weighted by the real time since the previous one. - When the request ends, the thread sends the folded stacks to Pyroscope's `/ingest` (through the same edge and credentials the agent uses) as `<service>.wall{…}`. - Pyroscope stores that as the **wall** profile type for the same service, with the agent's labels plus `endpoint`. - The Pyroscope agent stays on CPU. Its `oncpu=False` setting applies to the whole process and would turn every profile into wall time. Cost: the sampler runs only while a token request is in flight (a few hundred a week). Push failures are logged and never affect the response. To use it: in Pyroscope, choose the service, profile type `wall`, and `endpoint="/o/token/"`. Verified: - A local run on a 0.6 s blocking call captured `token_view;wait_for_row_lock 595` ms plus 26 ms of compute. - Replaying that push to Pyroscope read back as 621 ms of wall time under the service. - New tests cover the sampler, endpoint filtering and the ingest request. - Full suite passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Profile the token endpoint by wall clock
All checks were successful
tests / pytest (push) Successful in 2m8s
1ff68f34d7
/o/token/ takes 1-2 s on some requests while its CPU profile shows about
10 ms per request: the time goes to waiting, which CPU sampling can't see.

While a request to one of WALL_CLOCK_ENDPOINTS (just /o/token/) runs, a
thread samples that request thread's stack every 10 ms, weighting each
sample by the time since the last, and when the request ends sends the
folded stacks to Pyroscope as this service's "wall" profile type, with
the agent's labels plus endpoint. The agent stays on CPU: its oncpu=False
is process-wide and would turn every profile into wall time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.