Apollo3
To be able to capture transactions, you'll need to first set up performance monitoring.
Sentry's Apollo3 integration provides both the SentryApollo3Interceptor
and the SentryApollo3HttpInterceptor
, which create a span for each outgoing HTTP request executed with an Apollo Kotlin GraphQL client. The integration also provides extension functions on the ApolloClient.Builder
.
Install
To install Apollo3:
implementation 'io.sentry:sentry-apollo-3:6.29.0'
For other dependency managers, see the central Maven repository.
Configure With Extension
Add Interceptors
to ApolloClient.Builder
using ApolloBuilderExtensions
:
import com.apollographql.apollo3.ApolloClient;
import io.sentry.apollo3.SentryApolloBuilderExtensionsKt;
ApolloClient apollo = SentryApolloBuilderExtensionsKt
.sentryTracing(new ApolloClient.Builder())
.serverUrl("https://your-api-host/")
.build();
Manual Configuration
Because HttpInterceptors
need to be added to the NetworkTransport
, the SentryInterceptors
need to be added manually if you're using a custom NetworkTransport
:
import com.apollographql.apollo3.ApolloClient;
import com.apollographql.apollo3.network.http.HttpNetworkTransport;
import io.sentry.apollo3.SentryApollo3HttpInterceptor;
import io.sentry.apollo3.SentryApollo3Interceptor;
ApolloClient apollo = new ApolloClient.Builder()
.networkTransport(
new HttpNetworkTransport.Builder()
.serverUrl("https://your-api-host/")
.addInterceptor(new SentryApollo3HttpInterceptor())
.build())
.addInterceptor(new SentryApollo3Interceptor())
.build();
Modify or Drop Spans
Spans created around requests can be modified or dropped using SentryApollo3HttpInterceptor.BeforeSpanCallback
passed to SentryApollo3HttpInterceptor
or the sentryTracing
extension function:
import com.apollographql.apollo3.ApolloClient;
import io.sentry.apollo3.SentryApolloBuilderExtensionsKt;
ApolloClient apollo = SentryApolloBuilderExtensionsKt.sentryTracing(
new ApolloClient.Builder(),
(span, request, response) -> {
if ("LaunchDetails".equals(span.getOperation())) {
span.setTag("tag-name", "tag-value");
}
return span;
})
.serverUrl("https://your-api-host/")
.build();
GraphQL Client Errors
Once enabled, this feature will automatically capture GraphQL client errors such as bad operations and response codes, and report them to Sentry as error events. Each error event will contain the request
and response
data, including the url
, status_code
, and data
(stringified query
).
Sentry will group GraphQL client errors by the operationName
, operationType
, and statusCode
, so that you can easily see the number of errors that happened for each.
This is an opt-in feature and can be enabled by setting the captureFailedRequests
option to true
:
import com.apollographql.apollo3.ApolloClient
import io.sentry.apollo3.sentryTracing
val apollo = ApolloClient.builder()
.serverUrl("https://your-api-host/")
.sentryTracing(captureFailedRequests = true)
.build()
By default, only GraphQL client errors with a response that includes the errors array will be captured as error events.
GraphQL client errors from every target (.*
regular expression) are automatically captured, but you can change this behavior by setting the failedRequestTargets
option with either a regular expression or a plain String
. A plain string must contain at least one of the items from the list. Plain strings don't have to be full matches, meaning the URL of a request is matched when it contains a string provided through the option.
import com.apollographql.apollo3.ApolloClient
import io.sentry.apollo3.sentryTracing
val apollo = ApolloClient.builder()
.serverUrl("https://your-api-host/")
.sentryTracing(captureFailedRequests = true, failedRequestTargets = listOf("myapi.com"))
.build()
By default, error events won't contain Headers
or Cookies
, but you can change this behavior by setting the sendDefaultPii
option to true
:
AndroidManifest.xml
<application>
<meta-data android:name="io.sentry.send-default-pii" android:value="true" />
</application>
Error events will contain the raw bodies of GraphQL requests and responses, which may include sensitive data. To avoid this, parameterize your queries using the variables field. Relay will then run PII Data Scrubbing, automatically transforming values into [Filtered]
.
Alternatively, you can customize the event and scrub the data yourself.
Customize or Drop the Error Event
To customize or drop the error event, you'll need to do a manual initialization of the Sentry Android SDK. The captured error event can then be customized or dropped with a BeforeSendCallback
:
import io.sentry.android.core.SentryAndroid
import io.sentry.SentryOptions.BeforeSendCallback
import com.apollographql.apollo3.api.http.HttpRequest
import com.apollographql.apollo3.api.http.HttpResponse
import io.sentry.TypeCheckHint.APOLLO_REQUEST
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
SentryAndroid.init(this) { options ->
// Add a callback that will be used before the event is sent to Sentry.
// With this callback, you can modify the event or, when returning null, also discard the event.
options.beforeSend = BeforeSendCallback { event, hint ->
val request = hint.getAs(APOLLO_REQUEST, HttpRequest::class.java)
val response = hint.getAs(APOLLO_RESPONSE, HttpResponse::class.java)
// customize or drop the event
event
}
}
Automatically and Manually Captured GraphQL Client Errors
When captureFailedRequests
is enabled, some GraphQL client libraries will throw unchecked exceptions, such as the ApolloException
and its implementations. This means the error event will be captured by both the GraphQL client library and the Sentry Android SDK. To avoid this, we recommend identifying these errors and using the Sentry.captureException
method instead of capturing them manually:
import io.sentry.Sentry
import com.apollographql.apollo3.exception.ApolloException
try {
// If this API call returns the `errors` array, it will be captured as an error event by the `SentryApollo3HttpInterceptor`.
return apolloClient.query(LaunchDetailsQuery(launchId)).execute()
} catch (e: ApolloException) {
// Do not manually capture this exception to avoid duplicated error events.
// Sentry.captureException(e)
}
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 6.29.0
- Repository:
- https://github.com/getsentry/sentry-java