Skip to content
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

Implement post-response hook for logging and monitoring, including matched route and params #3840

Closed
georgecrawford opened this issue Feb 11, 2022 · 3 comments

Comments

@georgecrawford
Copy link
Contributor

Describe the problem

I've split this out from #1194 (comment), which is a slightly different issue and request.

I'm keen to implement good logging and monitoring for my SvelteKit app. For monitoring, in particular, it's extremely useful to know which route, and which params, produced the final response. I use Prometheus, and with my old Express.js app I'd typically label each finished request/response with details like {method: 'GET', route: '/users/[id]/details', status: 200}. This generic route parameter means that I can aggregate all similar requests to the same route and understand how long the responses took to generate on average (or stddev, etc). At present, since I don't know which route rendered the response in handle(), the best I can do is to use url.pathname, which means I can't aggregate across similar request to the same route.

Describe the proposed solution

Perhaps we could consider a handleResponse hook, with a signature something like this (and apologies, my TypeScript knowledge is almost nil, so this is pseudocode-typing!):

/* @returns <void> */
hooks.handleResponse = function({
    event: RequestEvent, 
    response: Response, 
    responseData: {
        duration: IntMilliseconds, 
        route: SomeKindOfRouteObject, 
        params: Record<string, string>,
    }
}) {
    ...
}

I could make use of details from the route object (ideally, I'd get the path of the .svelte or .js file which finally served the response, or something else akin to the Express route (e.g. '/users/[id]/details'). I'd also log the response duration, and I might make use of params in my logging output.

Alternatives considered

At the moment, I don't believe it's possible to determine which route/params combo produced the response which resolve() from hooks.handle() returns, so I don't think I can get this data without a code change.

Importance

i cannot use SvelteKit without it

Additional Information

Something along these lines will be vital to people running SvelteKit apps in production, IMO.

@georgecrawford
Copy link
Contributor Author

Thinking about it more, I don't think we need responseData.duration, as we can derive that by using the sequence method, and wrapping the call to resolve() with a timer.

@georgecrawford
Copy link
Contributor Author

See related discussion of param validators here: #4291

Rich-Harris added a commit that referenced this issue Mar 16, 2022
Rich-Harris added a commit that referenced this issue Mar 16, 2022
* expose event.routeKey - closes #3840

* change routeKey to routeId

* rename routeKey to routeId, expose page.routeId

* rename route.key -> route.id everywhere

* adapter-node sure picked a weird time to stop typechecking

* oops

* Update .changeset/mean-crews-unite.md
@georgecrawford
Copy link
Contributor Author

Thanks @Rich-Harris 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant