-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add server timing to responses #164
base: main
Are you sure you want to change the base?
Conversation
with ipfs/service-worker-gateway#531, requesting http://non-ipns-site.blog.ipns.helia-sw-gateway.localhost/ results in: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self review of added test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty neat. Implementation looks good to me.
What do you think about making this optional per request? I can imagine that in node.js this isn't very useful and just makes the response object larger.
We need to work out exactly what events we need to time
At the very least we'd want:
- IPNS/DNSLink resolution (if it's an ipns url)
- content routing, i.e. finding providers.
We could look into making it an option and defaulting to true in sw-gateway and helia-http-gateway. FYI with helia-http-gateway, which runs on nodejs, it's still useful because it's returning the response directly from verified-fetch.
more granular IPNS/DNSLink resolution will require some deeper piping of the server timing. I will look into those, or see if I can finagle some determinism from onProgress events |
@2color added the option to include server timing headers, defaulting to false on construction but overridable per request. Also added docs and updated readme |
packages/verified-fetch/src/index.ts
Outdated
* @default false | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing | ||
*/ | ||
includeServerTiming?: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's typed as a boolean, any thoughts on making this a bit shorter?
includeServerTiming?: boolean | |
serverTiming?: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong preference, but I prefer a more explicit name since serverTiming
doesn't communicate its purpose. Also, verb-prefixed names for booleans are more idiomatic. What do you think about withServerTiming
instead?
Option names should communicate their intent and effect rather than aim for brevity. More explicit names lead to a better dev understanding, with and without reading the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough! I agree.
withServerTiming
works too and is shorter.
@@ -452,6 +460,26 @@ export class VerifiedFetch { | |||
[identity.code]: this.handleRaw | |||
} | |||
|
|||
private async handleServerTiming<T> (name: string, description: string, fn: () => Promise<T>): Promise<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check this.includeServerTiming
here to encapsulate most of the logic here and avoid the measuring and assigning of this.serverTimingHeaders
?
That would also allow simplifying responseWithServerTiming
and renaming to something a bit more generic, e.g. augmentResponse
or handleResponse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea let me peek at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, it just so happened that switching the server timing handling to handleServerTiming required me to pipe things through a lot more, so I'm moving forward with this and adding the "IPNS/DNSLink resolution (if it's an ipns url)" you previously mentioned.
I didn't originally like it in handleServerTiming
because it required plumbing the request-level "withServerTiming" into more places, but it makes more sense for multiple reasons.
it also prevents the extra work of wrapping the timed functions.. so it's a win. thanks for the push
Co-authored-by: Daniel Norman <[email protected]>
@2color all requested changes are in, but the code is a little bit sloppier now because of how parse-resource & parse-url-string are currently written. |
Title
feat: add server timing to responses
Description
This PR adds Server Timing headers to responses by wrapping all responses returned by
fetch
in aResponse
object that includes the server timing headers.The headers are generated by wrapping async functions with a
serverTiming
utility function that records the time taken to execute the function, and then adding those headers to an array on the VerifiedFetch class.Notes & open questions
serverTiming
utility function usage so that it's easier to add new timings without having to handle the error and success cases manually for each one.Example of current server timings for http://bafybeie4vcqkutumw7s26ob2bwqwqi44m6lrssjmiirlhrzhs2akdqmkw4.ipfs.helia-sw-gateway.localhost/:
Example of current server timings for http://blog-ipfs-tech.ipns.helia-sw-gateway.localhost/:
Change checklist