Apollo Integration

Learn how to capture tracing information of the Apollo GraphQL client.

Capturing transactions requires that you first set up tracing 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 GraphQL client.

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-apollo</artifactId>
    <version
  >7.10.0</version>
</dependency>

For other dependency managers, see the central Maven repository.

Add SentryApolloInterceptor to Apollo builder:

Copied
import com.apollographql.apollo.ApolloClient;
import io.sentry.apollo.SentryApolloInterceptor;

ApolloClient apollo = ApolloClient.builder()
    .serverUrl("https://your-api-host/")
    .addApplicationInterceptor(new 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.

Configure Global Hub Mode:

Copied
import io.sentry.Sentry;

Sentry.init(options -> {
  ..
}, true)

In Global Hub Mode, all threads use the same Hub.

To make sure that a coroutine has access to the correct Sentry context, an instance of SentryContext must be provided when launching a coroutine.

Copied
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
  }
}

Spans created around requests can be modified or dropped using SentryApolloInterceptor.BeforeSpanCallback passed to SentryApolloInterceptor:

Copied
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();
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").