undici icon indicating copy to clipboard operation
undici copied to clipboard

Missing fields on Fetch PerformanceResourceTiming instance

Open Ethan-Arrowood opened this issue 1 year ago • 6 comments

Does anyone know why the PerformanceResourceTiming instance is missing information regarding DNS and what not?

Here is an example:

PerformanceResourceTiming {
    name: 'https://example.com/',
    entryType: 'resource',
    startTime: 257.69891691207886,
    duration: 98.86862516403198,
    initiatorType: 'fetch',
    nextHopProtocol: undefined,
    workerStart: 0,
    redirectStart: 0,
    redirectEnd: 0,
    fetchStart: 257.69891691207886,
    domainLookupStart: undefined,
    domainLookupEnd: undefined,
    connectStart: undefined,
    connectEnd: undefined,
    secureConnectionStart: undefined,
    requestStart: 0,
    responseStart: 0,
    responseEnd: 356.56754207611084,
    transferSize: 300,
    encodedBodySize: 0,
    decodedBodySize: 0
  }

That you get from a simple script like:

const url = new URL('https://example.com')
const res1 = await fetch(url);

await res1.text();

const fetchEntry = perfHooks.performance.getEntriesByName(url);
console.log(fetchEntry);

I tracked down the places in the source code where this gets created:

  1. https://github.com/nodejs/undici/blob/8422aa988243fb4c6c37b78519954d7337cc240b/lib/fetch/index.js#L311-L325
  2. https://github.com/nodejs/node/blob/main/lib/internal/perf/resource_timing.js#L205-L228

But it's still not immediately clear to me how to fix this. Any ideas?

Ethan-Arrowood avatar Dec 08 '23 22:12 Ethan-Arrowood

I think I found it: https://github.com/nodejs/undici/blob/main/lib/fetch/util.js#L274-L288

These are all 0 - we need to figure out how to populate these values correctly.

Ethan-Arrowood avatar Dec 08 '23 22:12 Ethan-Arrowood

And the relevant part of the spec that would be setting some of these values (such as domainLookupStartTime (https://fetch.spec.whatwg.org/#ref-for-connection-timing-info-domain-lookup-start-time)

Ethan-Arrowood avatar Dec 08 '23 22:12 Ethan-Arrowood

Found it for real: https://github.com/nodejs/undici/blob/c228fdd8ec65699e82f28b76369527d765ce6df0/lib/fetch/index.js#L1770 its just not implemented.

Someone is welcome to take this on if they'd like. Otherwise I may be able to next week.

Ethan-Arrowood avatar Dec 08 '23 22:12 Ethan-Arrowood

Some relevant places to completely implement performanceresourcetiming:

  • https://github.com/nodejs/undici/blob/c228fdd8ec65699e82f28b76369527d765ce6df0/lib/fetch/index.js#L1770
  • https://github.com/nodejs/undici/blob/c228fdd8ec65699e82f28b76369527d765ce6df0/lib/fetch/index.js#L2107

Ethan-Arrowood avatar Jan 03 '24 19:01 Ethan-Arrowood

@ronag where / how would I get access to the underlying connection and dns processes for fetch? I'm trying to add those timings to the PerformanceResourceTiming instance and I can't seem to find it.

Ethan-Arrowood avatar Jan 05 '24 19:01 Ethan-Arrowood

I think is not doable without using Symbols to access the Socket from the dispatcher (either through init or getting the global one).

For the DNS is even trickier, I don't think there's a way to intercept that without messing directly with the lookup option while connecting 🤔

metcoder95 avatar Jan 10 '24 09:01 metcoder95