Skip to content

Commit 97cfbe7

Browse files
authored
feat(android): add Apollo 4 docs (#12729)
* feat(android): add Apollo 4 docs * replace back SentryApollo4BuilderExtensions with SentryApolloBuilderExtensions * improve
1 parent 1a17183 commit 97cfbe7

File tree

2 files changed

+446
-0
lines changed

2 files changed

+446
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
title: Apollo 4
3+
caseStyle: camelCase
4+
supportLevel: production
5+
sdk: sentry.java.apollo-4
6+
description: "Learn more about the Sentry Apollo 4 integration for the Android SDK."
7+
categories:
8+
- mobile
9+
---
10+
11+
<Alert>
12+
13+
To be able to capture transactions, you'll need to first <PlatformLink to="/tracing/">set up tracing</PlatformLink>.
14+
15+
</Alert>
16+
17+
Sentry's Apollo 4 integration provides both the `SentryApollo4Interceptor` and the `SentryApollo4HttpInterceptor`, which create a span for each outgoing HTTP request executed with an [Apollo Kotlin](https://github.com/apollographql/apollo-kotlin) GraphQL client. The integration also provides extension functions on the `ApolloClient.Builder`.
18+
19+
## Install
20+
21+
Install the Apollo 4 integration:
22+
23+
```groovy {tabTitle:Gradle}
24+
implementation 'io.sentry:sentry-apollo-4:{{@inject packages.version('sentry.java.apollo-4', '8.3.0') }}'
25+
```
26+
27+
For other dependency managers, see the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-apollo-4).
28+
29+
## Configure With Extension
30+
31+
Add `Interceptors` to `ApolloClient.Builder` using `SentryApolloBuilderExtensions`:
32+
33+
```java
34+
import com.apollographql.ApolloClient;
35+
import io.sentry.apollo4.SentryApolloBuilderExtensionsKt;
36+
37+
ApolloClient apollo = SentryApolloBuilderExtensionsKt
38+
.sentryTracing(new ApolloClient.Builder())
39+
.serverUrl("https://your-api-host/")
40+
.build();
41+
```
42+
43+
```kotlin
44+
import com.apollographql.ApolloClient
45+
import io.sentry.apollo4.sentryTracing
46+
47+
val apollo = ApolloClient.builder()
48+
.serverUrl("https://your-api-host/")
49+
.sentryTracing()
50+
.build()
51+
```
52+
53+
## Manual Configuration
54+
55+
Because `HttpInterceptors` need to be added to the `NetworkTransport`, the `SentryInterceptors` need to be added manually if you're using a custom `NetworkTransport`:
56+
57+
```java
58+
import com.apollographql.ApolloClient;
59+
import com.apollographql.network.http.HttpNetworkTransport;
60+
import io.sentry.apollo4.SentryApollo4HttpInterceptor;
61+
import io.sentry.apollo4.SentryApollo4Interceptor;
62+
63+
ApolloClient apollo = new ApolloClient.Builder()
64+
.networkTransport(
65+
new HttpNetworkTransport.Builder()
66+
.serverUrl("https://your-api-host/")
67+
.addInterceptor(new SentryApollo4HttpInterceptor())
68+
.build())
69+
.addInterceptor(new SentryApollo4Interceptor())
70+
.build();
71+
```
72+
73+
```kotlin
74+
import com.apollographql.ApolloClient
75+
import io.sentry.apollo4.SentryApollo4HttpInterceptor
76+
import io.sentry.apollo4.SentryApollo4Interceptor
77+
import com.apollographql.network.http.HttpNetworkTransport
78+
79+
val apollo = ApolloClient.builder()
80+
.networkTransport(
81+
HttpNetworkTransport.Builder()
82+
.serverUrl("https://apollo-fullstack-tutorial.herokuapp.com/graphql")
83+
.addInterceptor(SentryApollo4HttpInterceptor())
84+
.build()
85+
)
86+
.addInterceptor(SentryApollo4Interceptor())
87+
.build()
88+
```
89+
90+
## Modify or Drop Spans
91+
92+
Spans created around requests can be modified or dropped using `SentryApollo4HttpInterceptor.BeforeSpanCallback` passed to `SentryApollo4HttpInterceptor` or the `sentryTracing` extension function:
93+
94+
```java
95+
import com.apollographql.ApolloClient;
96+
import io.sentry.apollo4.SentryApolloBuilderExtensionsKt;
97+
98+
ApolloClient apollo = SentryApolloBuilderExtensionsKt.sentryTracing(
99+
new ApolloClient.Builder(),
100+
(span, request, response) -> {
101+
if ("LaunchDetails".equals(span.getOperation())) {
102+
span.setTag("tag-name", "tag-value");
103+
}
104+
return span;
105+
})
106+
.serverUrl("https://your-api-host/")
107+
.build();
108+
```
109+
110+
```kotlin
111+
import com.apollographql.ApolloClient
112+
import io.sentry.apollo4.sentryTracing
113+
114+
val apollo = ApolloClient.builder()
115+
.serverUrl("https://apollo-fullstack-tutorial.herokuapp.com/graphql")
116+
.sentryTracing { span, request, response ->
117+
if ("LaunchDetails" == span.operation) {
118+
span.setTag("tag-name", "tag-value")
119+
}
120+
span
121+
}
122+
.build()
123+
```
124+
125+
## GraphQL Client Errors
126+
127+
This feature automatically captures GraphQL client errors (like bad operations and response codes) as error events and reports them to Sentry. The error event will contain the `request` and `response` data, including the `url`, `status_code`, and `data` (stringified `query`).
128+
129+
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.
130+
131+
This feature is enabled by default and can be disabled by setting the `captureFailedRequests` option to `false`:
132+
133+
```kotlin
134+
import com.apollographql.ApolloClient
135+
import io.sentry.apollo4.sentryTracing
136+
137+
val apollo = ApolloClient.builder()
138+
.serverUrl("https://your-api-host/")
139+
.sentryTracing(captureFailedRequests = false)
140+
.build()
141+
```
142+
143+
By default, only GraphQL client errors with a response that includes the [errors](https://spec.graphql.org/October2021/#sec-Errors) array will be captured as error events.
144+
145+
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.
146+
147+
```kotlin
148+
import com.apollographql.ApolloClient
149+
import io.sentry.apollo4.sentryTracing
150+
151+
val apollo = ApolloClient.builder()
152+
.serverUrl("https://your-api-host/")
153+
.sentryTracing(captureFailedRequests = true, failedRequestTargets = listOf("myapi.com"))
154+
.build()
155+
```
156+
157+
By default, error events won't contain `Headers` or `Cookies`, but you can change this behavior by setting the `sendDefaultPii` option to `true`:
158+
159+
```kotlin
160+
Sentry.init { options ->
161+
options.isSendDefaultPii = true
162+
}
163+
```
164+
165+
```xml {filename:AndroidManifest.xml}
166+
<application>
167+
<meta-data android:name="io.sentry.send-default-pii" android:value="true" />
168+
</application>
169+
```
170+
171+
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](https://spec.graphql.org/October2021/#sec-Language.Variables) field. [Relay](/product/relay) will then run [PII Data Scrubbing](/product/relay/#pii-data-scrubbing), automatically transforming values into `[Filtered]`.
172+
173+
Alternatively, you can customize the event and scrub the data yourself.
174+
175+
### Customize or Drop the Error Event
176+
177+
The captured error event can be customized or dropped with a `BeforeSendCallback`:
178+
179+
```kotlin
180+
import io.sentry.Sentry
181+
import io.sentry.SentryOptions.BeforeSendCallback
182+
import com.apollographql.api.http.HttpRequest
183+
import com.apollographql.api.http.HttpResponse
184+
import io.sentry.TypeCheckHint.APOLLO_REQUEST
185+
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
186+
187+
Sentry.init { options ->
188+
// Add a callback that will be used before the event is sent to Sentry.
189+
// With this callback, you can modify the event or, when returning null, also discard the event.
190+
options.beforeSend = BeforeSendCallback { event, hint ->
191+
val request = hint.getAs(APOLLO_REQUEST, HttpRequest::class.java)
192+
val response = hint.getAs(APOLLO_RESPONSE, HttpResponse::class.java)
193+
194+
// customize or drop the event
195+
event
196+
}
197+
}
198+
```
199+
200+
### Automatically and Manually Captured GraphQL Client Errors
201+
202+
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:
203+
204+
```kotlin
205+
import io.sentry.Sentry
206+
import com.apollographql.exception.ApolloException
207+
208+
try {
209+
// If this API call returns the `errors` array, it will be captured as an error event by the `SentryApollo4HttpInterceptor`.
210+
return apolloClient.query(LaunchDetailsQuery(launchId)).execute()
211+
} catch (e: ApolloException) {
212+
// Do not manually capture this exception to avoid duplicated error events.
213+
// Sentry.captureException(e)
214+
}
215+
```

0 commit comments

Comments
 (0)