sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

Support for performance tracking with Apollo on Android

Open alexmetcalfe opened this issue 5 years ago • 5 comments

We use Apollo android extensively through our app.

This uses OkHttp under the hood which means I assume each request will be traced in the performance dashboard once this is complete -> https://github.com/getsentry/sentry-java/pull/1330

However with GraphQL each request posts to the same URL. The body of the post contains the query which tells the backend what to do eg a query or mutation...

This I believe would make it difficult to understand what each network request is doing...

In the meantime we can do something similar to what the OkHttp interceptor is doing but it would be great if this came out of the box for us.

alexmetcalfe avatar Mar 17 '21 22:03 alexmetcalfe

thanks for reporting @alexmetcalfe

sounds doable, looks like the Apollo Android lib offers interceptors similar to OkHttp https://www.apollographql.com/docs/ios/initialization/#example-advanced-client-setup maybe with that we could gather more info and create enriched Spans OOTB (it's an iOS docs but pretty sure Android has something similar)

marandaneto avatar Mar 18 '21 14:03 marandaneto

Managed to hack together something today based of your OkHttp interceptor, seems to work well so far!

class SentryApolloInterceptor(
        private val hub: IHub = HubAdapter.getInstance()
) : ApolloInterceptor {
    override fun interceptAsync(request: ApolloInterceptor.InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: ApolloInterceptor.CallBack) {
        val operationName = request.operation.name().name()
        val operationType = if(request.operation is Query) "query" else "mutation"
        val operationDescription = "$operationType $operationName"

        // read transaction from the bound scope
        val span = hub.span?.startChild("apollo.http.client", operationDescription)

        var newRequest = request
        span?.toSentryTrace()?.let {
            newRequest = request.toBuilder().requestHeaders(
                    request.requestHeaders.toBuilder()
                            .addHeader(it.name, it.value)
                            .build()
            ).build()
        }

        span?.spanContext?.tags?.put("operationId", request.operation.operationId())
        span?.spanContext?.tags?.put("variables", request.operation.variables().valueMap().toString())

        chain.proceedAsync(newRequest, dispatcher, object : ApolloInterceptor.CallBack {
            override fun onResponse(response: ApolloInterceptor.InterceptorResponse) {
                val httpResponse = response.httpResponse.get()
                span?.finish(SpanStatus.fromHttpStatusCode(httpResponse.code, SpanStatus.INTERNAL_ERROR))

                val breadcrumb = Breadcrumb(operationDescription)
                httpResponse.request.body?.contentLength().ifHasValidLength {
                    breadcrumb.setData("requestBodySize", it)
                }
                httpResponse.body?.contentLength().ifHasValidLength {
                    breadcrumb.setData("responseBodySize", it)
                }
                hub.addBreadcrumb(breadcrumb)
                callBack.onResponse(response)
            }

            override fun onFetch(sourceType: ApolloInterceptor.FetchSourceType?) {
                callBack.onFetch(sourceType)
            }

            override fun onFailure(e: ApolloException) {
                callBack.onFailure(e)
            }

            override fun onCompleted() {
                callBack.onCompleted()
            }
        })
    }

    override fun dispose() {
        // no-op
    }

    private fun Long?.ifHasValidLength(fn: (Long) -> Unit) {
        if (this != null && this != -1L) {
            fn.invoke(this)
        }
    }
}

alexmetcalfe avatar Mar 19 '21 00:03 alexmetcalfe

Thanks awesome @alexmetcalfe! Thank you for sharing this

bruno-garcia avatar Mar 19 '21 03:03 bruno-garcia

nicely done, @alexmetcalfe

be aware that if it throws, I guess you won't be associating the error to the Span nor finishing the Span as errored, by the callback signatures, I'd guess you need to do something here -> onFailure(e: ApolloException)

similar to https://github.com/getsentry/sentry-java/blob/main/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt#L34-L38

marandaneto avatar Mar 19 '21 07:03 marandaneto

Yeah good point, had not covered the failure side of things yet 👍

alexmetcalfe avatar Mar 19 '21 16:03 alexmetcalfe