|  | 
|  | 1 | +package io.sentry.apollo | 
|  | 2 | + | 
|  | 3 | +import com.apollographql.apollo.api.Mutation | 
|  | 4 | +import com.apollographql.apollo.api.Query | 
|  | 5 | +import com.apollographql.apollo.api.Subscription | 
|  | 6 | +import com.apollographql.apollo.exception.ApolloException | 
|  | 7 | +import com.apollographql.apollo.exception.ApolloHttpException | 
|  | 8 | +import com.apollographql.apollo.interceptor.ApolloInterceptor | 
|  | 9 | +import com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack | 
|  | 10 | +import com.apollographql.apollo.interceptor.ApolloInterceptor.FetchSourceType | 
|  | 11 | +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest | 
|  | 12 | +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse | 
|  | 13 | +import com.apollographql.apollo.interceptor.ApolloInterceptorChain | 
|  | 14 | +import io.sentry.HubAdapter | 
|  | 15 | +import io.sentry.IHub | 
|  | 16 | +import io.sentry.ISpan | 
|  | 17 | +import io.sentry.SentryLevel | 
|  | 18 | +import io.sentry.SpanStatus | 
|  | 19 | +import java.util.concurrent.Executor | 
|  | 20 | + | 
|  | 21 | +class SentryApolloInterceptor( | 
|  | 22 | +    private val hub: IHub = HubAdapter.getInstance(), | 
|  | 23 | +    private val beforeSpan: BeforeSpanCallback? = null | 
|  | 24 | +) : ApolloInterceptor { | 
|  | 25 | + | 
|  | 26 | +    constructor(hub: IHub) : this(hub, null) | 
|  | 27 | +    constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) | 
|  | 28 | + | 
|  | 29 | +    override fun interceptAsync(request: InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: CallBack) { | 
|  | 30 | +        val activeSpan = hub.span | 
|  | 31 | +        if (activeSpan == null) { | 
|  | 32 | +            chain.proceedAsync(request, dispatcher, callBack) | 
|  | 33 | +        } else { | 
|  | 34 | +            val span = startChild(request, activeSpan) | 
|  | 35 | +            val sentryTraceHeader = span.toSentryTrace() | 
|  | 36 | + | 
|  | 37 | +            // we have no access to URI, no way to verify tracing origins | 
|  | 38 | +            val headers = request.requestHeaders.toBuilder().addHeader(sentryTraceHeader.name, sentryTraceHeader.value).build() | 
|  | 39 | +            val requestWithHeader = request.toBuilder().requestHeaders(headers).build() | 
|  | 40 | +            span.setData("operationId", requestWithHeader.operation.operationId()) | 
|  | 41 | +            span.setData("variables", requestWithHeader.operation.variables().valueMap().toString()) | 
|  | 42 | + | 
|  | 43 | +            chain.proceedAsync(requestWithHeader, dispatcher, object : CallBack { | 
|  | 44 | +                override fun onResponse(response: InterceptorResponse) { | 
|  | 45 | +                    // onResponse is called only for statuses 2xx | 
|  | 46 | +                    span.status = response.httpResponse.map { SpanStatus.fromHttpStatusCode(it.code(), SpanStatus.UNKNOWN) } | 
|  | 47 | +                        .or(SpanStatus.UNKNOWN) | 
|  | 48 | + | 
|  | 49 | +                    finish(span, requestWithHeader, response) | 
|  | 50 | +                    callBack.onResponse(response) | 
|  | 51 | +                } | 
|  | 52 | + | 
|  | 53 | +                override fun onFetch(sourceType: FetchSourceType) { | 
|  | 54 | +                    callBack.onFetch(sourceType) | 
|  | 55 | +                } | 
|  | 56 | + | 
|  | 57 | +                override fun onFailure(e: ApolloException) { | 
|  | 58 | +                    span.apply { | 
|  | 59 | +                        status = if (e is ApolloHttpException) SpanStatus.fromHttpStatusCode(e.code(), SpanStatus.INTERNAL_ERROR) else SpanStatus.INTERNAL_ERROR | 
|  | 60 | +                        throwable = e | 
|  | 61 | +                    } | 
|  | 62 | +                    finish(span, requestWithHeader) | 
|  | 63 | +                    callBack.onFailure(e) | 
|  | 64 | +                } | 
|  | 65 | + | 
|  | 66 | +                override fun onCompleted() { | 
|  | 67 | +                    callBack.onCompleted() | 
|  | 68 | +                } | 
|  | 69 | +            }) | 
|  | 70 | +        } | 
|  | 71 | +    } | 
|  | 72 | + | 
|  | 73 | +    override fun dispose() {} | 
|  | 74 | + | 
|  | 75 | +    private fun startChild(request: InterceptorRequest, activeSpan: ISpan): ISpan { | 
|  | 76 | +        val operation = request.operation.name().name() | 
|  | 77 | +        val operationType = when (request.operation) { | 
|  | 78 | +            is Query -> "query" | 
|  | 79 | +            is Mutation -> "mutation" | 
|  | 80 | +            is Subscription -> "subscription" | 
|  | 81 | +            else -> request.operation.javaClass.simpleName | 
|  | 82 | +        } | 
|  | 83 | +        val description = "$operationType $operation" | 
|  | 84 | +        return activeSpan.startChild(operation, description) | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    private fun finish(span: ISpan, request: InterceptorRequest, response: InterceptorResponse? = null) { | 
|  | 88 | +        var newSpan: ISpan = span | 
|  | 89 | +        if (beforeSpan != null) { | 
|  | 90 | +            try { | 
|  | 91 | +                newSpan = beforeSpan.execute(span, request, response) | 
|  | 92 | +            } catch (e: Exception) { | 
|  | 93 | +                hub.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) | 
|  | 94 | +            } | 
|  | 95 | +        } | 
|  | 96 | +        newSpan.finish() | 
|  | 97 | +    } | 
|  | 98 | + | 
|  | 99 | +    /** | 
|  | 100 | +     * The BeforeSpan callback | 
|  | 101 | +     */ | 
|  | 102 | +    interface BeforeSpanCallback { | 
|  | 103 | +        /** | 
|  | 104 | +         * Mutates span before being added. | 
|  | 105 | +         * | 
|  | 106 | +         * @param span the span to mutate or drop | 
|  | 107 | +         * @param request the HTTP request executed by okHttp | 
|  | 108 | +         * @param response the HTTP response received by okHttp | 
|  | 109 | +         */ | 
|  | 110 | +        fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan | 
|  | 111 | +    } | 
|  | 112 | +} | 
0 commit comments