Apollo Integration

Sentry Apollo integration provides the SentryApolloInterceptor, which creates a span for each outgoing HTTP request executed with an Apollo Android GraphQL client.

Install

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

For other dependency managers, see the central Maven repository.

Configure

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.

Using With Java

Configure Global Hub Mode:

Copied
import io.sentry.Sentry;

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

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.

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

Modify or Drop Spans

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").