Skip to content

feat: capture per-phase resource timing for API journeys - #1164

Merged
shahzad31 merged 2 commits into
mainfrom
api-response-timing
Aug 4, 2026
Merged

shahzad31 merged 2 commits into
mainfrom
api-response-timing

Conversation

@shahzad31

Copy link
Copy Markdown
Contributor

Summary

Reuses Playwright's newly released APIResponse.timing() (shipped in 1.62.0) to give API journeys the same per-phase network timing breakdown that browser journeys already have.

  • Bump Playwright 1.61.1 → 1.62.0 (playwright, playwright-chromium, playwright-core).
  • New shared helper src/plugins/network-timings.ts — extracts the Resource Timing → phase-breakdown (getResourceTimings) and total calculation with wall-clock fallback (calcTotalTime). Since APIResponse.timing() returns the identical shape as Request.timing(), both NetworkManager (browser) and APINetworkManager (API) now share exactly the same math.
  • API journeys (api-network.ts): replaced the coarse wall-clock stopwatch — which left blocked/dns/ssl/connect at -1 and hard-coded receive: 0 — with a real breakdown from response.timing(). Phases that don't happen (reused keep-alive socket) or are unknown (HAR replay) still report -1, and calcTotalTime falls back to the wall-clock span in that case.
  • Browser journeys (network.ts): refactored to call the shared helper (removing the duplicated inline math and _calcTotalTime); behavior unchanged.

Before vs after (API journeys)

phase before after
blocked / dns / ssl / connect -1 derived from APIResponse.timing()
wait wall-clock fetch duration TTFB from Resource Timing
receive hard-coded 0 download time from Resource Timing

Test plan

  • network-timings.test.ts — deterministic unit tests: full HTTPS timing, reused-socket/cached-DNS (-1 phases), HAR-replay wall-clock fallback.
  • api-network.test.ts — asserts per-phase timings are populated from APIResponse.timing().
  • options.test.ts — updated the hard-coded device user-agent that shifted with the Playwright bump (Chrome/149… → 151…).
  • Full unit suite green (38 suites / 315 passed, 5 skipped), tsc --noEmit, ESLint, Prettier clean.
  • Heartbeat/integration smoke recommended, since 1.61 → 1.62 pulls in all of 1.62's browser/behavior changes beyond this feature.

Made with Cursor

Bump Playwright to 1.62.0 and surface the new APIResponse.timing() on
API journeys. Previously API monitors only recorded a coarse wall-clock
`wait`/`total` with all other phases at -1 and `receive` hard-coded to 0.

Since APIResponse.timing() returns the same Resource Timing shape as
Request.timing(), the phase-breakdown and total logic is extracted into a
shared `network-timings` helper reused by both the browser NetworkManager
and the APINetworkManager, so both journey types emit consistent
dns/connect/ssl/wait/receive timings. The wall-clock span remains as the
fallback for HAR replay / reused sockets where phases report -1.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread src/plugins/network.ts Outdated

// Gather all resource timing information up until the
// TTFB(Time to first byte) is received
// Resource timing up to TTFB; `receive` fills in on `requestfinished`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment is more robotic, lets reset to the previous version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, restored the original comment 👍

Comment thread src/network-timings.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This file not a plugin per se, more of a helper for timing breakdown, lets move it somewhere else not under plugins directory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it out to src/network-timings.ts (out of the plugins dir).

Comment thread src/plugins/network.ts Outdated

networkEntry.loadEndTime = epochTimeInSeconds();
// responseEnd is fired after the last byte of the response is received.
// Full timing (incl. `receive`) is available once the response completes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same I prefer the old comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted this one too.

Comment thread src/network-timings.ts
* between `requestSentTime` and `loadEndTime` when Resource Timing data is
* unavailable (`startTime <= 0`, e.g. HAR replay or aborted requests).
*/
export function calcTotalTime(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe we can change it to a pure func and return a separate object instead of overwiting the timing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — made it pure, it returns total now and the caller assigns it onto timings.total.

const request = this.driver.request;
this._originalFetch = request.fetch.bind(request);
(request as any).fetch = (urlOrRequest: RequestLike, options?: any) =>
this._interceptRequest(urlOrRequest, options);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didnt review the piece that added this, any reason why we didnt use the Events like ApiRequest.Request and RequestFinished ? Wasnt that possible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PW don't expose those unfortunately

- Move the timing-breakdown helper out of plugins/ (it is not a plugin)
  to src/network-timings.ts
- Make calcTotalTime a pure function that returns total instead of
  mutating timings; callers assign the result
- Restore the original TTFB / responseEnd comments in network.ts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@mcapell mcapell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, added a small comment

Comment thread src/network-timings.ts
? roundMilliSecs(rtiming.connectEnd - rtiming.connectStart)
: -1;
const ssl =
rtiming.secureConnectionStart !== -1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we check rtiming.connectEnd as well? something like:

Suggested change
rtiming.secureConnectionStart !== -1
rtiming.secureConnectionStart !== -1 && rtiming.connectEnd !== -1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@shahzad31
shahzad31 merged commit faab8b4 into main Aug 4, 2026
5 checks passed
@shahzad31
shahzad31 deleted the api-response-timing branch August 4, 2026 13:17
@elastic-observability-automation

Copy link
Copy Markdown

🎉 This PR is included in version 1.25.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants