-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Java: document Apollo Android integration #4202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
01dc984
Java: document Apollo Android integration
maciejwalkowiak f1dcca5
Polish.
maciejwalkowiak f7a039b
Polish.
maciejwalkowiak 0e7bd27
Add Android variant
maciejwalkowiak cf7e01b
Update src/platforms/android/common/performance/instrumentation/apoll…
maciejwalkowiak f6bcc4e
Update src/platforms/android/common/performance/instrumentation/apoll…
maciejwalkowiak 2b288bd
Update src/platforms/android/common/performance/instrumentation/apoll…
maciejwalkowiak 5ea3f7e
Polish Java apollo.
maciejwalkowiak b8b649b
Remove Maven dependencies from Android docs.
maciejwalkowiak 1abcc8a
Remove coroutines from Android.
maciejwalkowiak b917c17
Apply suggestions from code review
maciejwalkowiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/platforms/android/common/performance/instrumentation/apollo.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: Apollo Integration | ||
sidebar_order: 20 | ||
description: "Learn how to capture the performance of Apollo GraphQL client." | ||
--- | ||
|
||
<Note> | ||
|
||
Capturing transactions requires that you first <PlatformLink to="/performance/">set up performance monitoring</PlatformLink> if you haven't already. | ||
|
||
</Note> | ||
|
||
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() | ||
``` |
138 changes: 138 additions & 0 deletions
138
src/platforms/java/performance/instrumentation/apollo.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
--- | ||
title: Apollo Integration | ||
sidebar_order: 20 | ||
description: "Learn how to capture the performance of Apollo GraphQL client." | ||
--- | ||
|
||
<Note> | ||
|
||
Capturing transactions requires that you first <PlatformLink to="/performance/">set up performance monitoring</PlatformLink> if you haven't already. | ||
|
||
</Note> | ||
|
||
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} | ||
<dependency> | ||
<groupId>io.sentry</groupId> | ||
<artifactId>sentry-apollo</artifactId> | ||
<version>{{ packages.version('sentry.java.apollo', '5.2.0') }}</version> | ||
</dependency> | ||
``` | ||
|
||
```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 <PlatformLink to="/enriching-events/scopes/#kotlin-coroutines">Sentry's coroutines support</PlatformLink>. | ||
|
||
## Using With Java | ||
|
||
Configure Global Hub Mode: | ||
|
||
```java | ||
import io.sentry.Sentry; | ||
|
||
Sentry.init(options -> { | ||
.. | ||
}, true) | ||
``` | ||
|
||
<Note> | ||
In Global Hub Mode, all threads use the same Hub. | ||
</Note> | ||
|
||
## 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() | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.