apollographql / apollo-link

:link: Interface for fetching and modifying control flow of GraphQL requests
https://www.apollographql.com/docs/link/
MIT License
1.44k stars 344 forks source link

[Feature Request] Allow logging fetch request time #1077

Open naddeoa opened 5 years ago

naddeoa commented 5 years ago

I've written my own link to try and log the total request time for each query. This is roughly the code below. I've also tried apollo-link-logger with the same results.

const metricsLink = new ApolloLink((operation, forward) => {

    const { operationName } = operation
    const startTime = new Date().getTime()
    const observable = forward(operation)
    observable.subscribe({
        complete: () => {
            const elapsed = new Date().getTime() - startTime
            console.warn(`[METRICS][${operationName}] (${elapsed}) complete`)
        }
    })

    return observable
})

// All links I'm using
ApolloLink.from([metricsLink, httpLink]),

The numbers that I get seem to account for a lot more than the raw request time though. I'm seeing differences on the order of seconds (for large queries) between what the Android profiler gives me for network requests and what this code gives me. As a test, I added a timer directly to the http link code and those numbers are much closer to the actual network request time.

It would be great if we could actually get at those numbers somehow. Without those, we don't know how long our requests take separate from all of the work that apollo is doing under the hood it seems and I couldn't find anything online about logging request times any differently. Apologies in advance if I missed something.

maapteh commented 5 years ago

You can add appollo engine to your app and you will get all details about each resolvers timing in general and specific. Based on that information you can make decisions if batching would make sence etc etc.

Also when you turn on tracing you will get this data and push it to your own services, its not advised to do it on your production app since it will deliver more data to the client. Hope this helps.

See https://imgur.com/a/7rdxIVG