diff --git a/src/platforms/android/common/performance/instrumentation/apollo.mdx b/src/platforms/android/common/performance/instrumentation/apollo.mdx new file mode 100644 index 0000000000000..a07ac048b0031 --- /dev/null +++ b/src/platforms/android/common/performance/instrumentation/apollo.mdx @@ -0,0 +1,89 @@ +--- +title: Apollo Integration +sidebar_order: 20 +description: "Learn how to capture the performance of Apollo GraphQL client." +--- + + + +Capturing transactions requires that you first set up performance monitoring if you haven't already. + + + +Sentry Apollo integration provides the `SentryApolloInterceptor`, which creates a span for each outgoing HTTP request executed with an [Apollo Android](https://github.com/apollographql/apollo-android) GraphQL client. + +## Install + +```groovy {tabTitle:Gradle} +implementation 'io.sentry:sentry-apollo:{{ packages.version('sentry.java.apollo', '5.2.0') }}' +``` + +For other dependency managers, see the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-apollo). + +## Configure + +Add `SentryApolloInterceptor` to Apollo builder: + +```java +import com.apollographql.apollo.ApolloClient; +import io.sentry.apollo.SentryApolloInterceptor; + +ApolloClient apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(new SentryApolloInterceptor()) + .build(); +``` + +```kotlin +import com.apollographql.apollo.ApolloClient +import io.sentry.apollo.SentryApolloInterceptor + +val apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(SentryApolloInterceptor()) + .build() +``` + +## Modify or Drop Spans + +Spans created around requests can be modified or dropped using `SentryApolloInterceptor.BeforeSpanCallback` passed to `SentryApolloInterceptor`: + +```java +import com.apollographql.apollo.ApolloClient; +import io.sentry.apollo.SentryApolloInterceptor; + +ApolloClient apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(new SentryApolloInterceptor( + (span, request, response) -> { + if ("aQuery".equals(request.operation.name().name())) { + span.setTag("tag-name", "tag-value"); + } + return span; + } + )) + .build(); +``` + +```kotlin +import com.apollographql.apollo.ApolloClient +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse +import io.sentry.ISpan +import io.sentry.apollo.SentryApolloInterceptor +import io.sentry.apollo.SentryApolloInterceptor.BeforeSpanCallback + +val apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor( + SentryApolloInterceptor(object : BeforeSpanCallback { + override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan { + if ("aQuery" == request.operation.name().name()) { + span.setTag("tag-key", "tag-value") + } + return span + } + }) + ) + .build() +``` diff --git a/src/platforms/java/performance/instrumentation/apollo.mdx b/src/platforms/java/performance/instrumentation/apollo.mdx new file mode 100644 index 0000000000000..ecf659e9fcc5f --- /dev/null +++ b/src/platforms/java/performance/instrumentation/apollo.mdx @@ -0,0 +1,138 @@ +--- +title: Apollo Integration +sidebar_order: 20 +description: "Learn how to capture the performance of Apollo GraphQL client." +--- + + + +Capturing transactions requires that you first set up performance monitoring if you haven't already. + + + +Sentry Apollo integration provides the `SentryApolloInterceptor`, which creates a span for each outgoing HTTP request executed with an [Apollo Android](https://github.com/apollographql/apollo-android) GraphQL client. + +## Install + +```xml {tabTitle:Maven} + + io.sentry + sentry-apollo + {{ packages.version('sentry.java.apollo', '5.2.0') }} + +``` + +```groovy {tabTitle:Gradle} +implementation 'io.sentry:sentry-apollo:{{ packages.version('sentry.java.apollo', '5.2.0') }}' +``` + +```scala {tabTitle: SBT} +libraryDependencies += "io.sentry" % "sentry-apollo" % "{{ packages.version('sentry.java.apollo', '5.2.0') }}" +``` + +For other dependency managers, see the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-apollo). + +## Configure + +Add `SentryApolloInterceptor` to Apollo builder: + +```java +import com.apollographql.apollo.ApolloClient; +import io.sentry.apollo.SentryApolloInterceptor; + +ApolloClient apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(new SentryApolloInterceptor()) + .build(); +``` + +```kotlin +import com.apollographql.apollo.ApolloClient +import io.sentry.apollo.SentryApolloInterceptor + +val apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(SentryApolloInterceptor()) + .build() +``` + +Apollo Android is built with Kotlin coroutines. This means that `SentryApolloInterceptor` can be used with Java using only Global Hub Mode (single Hub used by all threads), with Kotlin using single Hub mode, or with Sentry's coroutines support. + +## Using With Java + +Configure Global Hub Mode: + +```java +import io.sentry.Sentry; + +Sentry.init(options -> { + .. +}, true) +``` + + +In Global Hub Mode, all threads use the same Hub. + + +## Using With Kotlin Coroutines + +To make sure that a coroutine has access to the correct Sentry context, an instance of `SentryContext` must be provided when launching a coroutine. + +```kotlin +import io.sentry.kotlin.SentryContext +import com.apollographql.apollo.exception.ApolloException +import kotlinx.coroutines.launch + +launch(SentryContext()) { + val response = try { + apollo.query(..).toDeferred().await() + } catch (e: ApolloException) { + // handle protocol errors + return@launch + } +} +``` + +## Modify or Drop Spans + +Spans created around requests can be modified or dropped using `SentryApolloInterceptor.BeforeSpanCallback` passed to `SentryApolloInterceptor`: + +```java +import com.apollographql.apollo.ApolloClient; +import io.sentry.apollo.SentryApolloInterceptor; + +ApolloClient apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor(new SentryApolloInterceptor( + (span, request, response) -> { + if ("aQuery".equals(request.operation.name().name())) { + span.setTag("tag-name", "tag-value"); + } + return span; + } + )) + .build(); +``` + +```kotlin +import com.apollographql.apollo.ApolloClient +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest +import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse +import io.sentry.ISpan +import io.sentry.apollo.SentryApolloInterceptor +import io.sentry.apollo.SentryApolloInterceptor.BeforeSpanCallback + +val apollo = ApolloClient.builder() + .serverUrl("https://your-api-host/") + .addApplicationInterceptor( + SentryApolloInterceptor(object : BeforeSpanCallback { + override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan { + if ("aQuery" == request.operation.name().name()) { + span.setTag("tag-key", "tag-value") + } + return span + } + }) + ) + .build() +```