Skip to content

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

@georgecrawford

Description

@georgecrawford

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions