-
-
Notifications
You must be signed in to change notification settings - Fork 435
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
Add integration for Apollo-Kotlin 3 #2109
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
770b0c9
add Interceptors for apollo3, add issue templates, adapt original tes…
lbloder d2a6428
Merge branch 'main' into feat/apollo-v3
lbloder 75f9086
Merge branch 'main' into feat/apollo-v3
lbloder 2418613
add http interceptor for apollo3, extend ISerializer to allow for des…
lbloder e70c24a
fix how response body size is calculated, refactor and simplify inter…
lbloder a99e873
Merge branch 'main' into feat/apollo-v3
lbloder 71dabc0
update changelog
lbloder ea95a8c
use throwable, add constructors for java compatibility, use new apoll…
lbloder e32dae4
Merge branch 'main' into feat/apollo-v3
lbloder b2112d7
Merge branch 'main' into feat/apollo-v3
lbloder 683d374
add custom request composer to capture more info about the apollo call
lbloder 6f2d6be
remove unnecessary testcases in SentryApollo3InterceptorWithComposeTe…
lbloder ab17056
fix changelog after merge
lbloder 700504e
Merge branch 'main' into feat/apollo-v3
lbloder 6164c80
rollback changes to deserializer, move away from custom request compo…
lbloder 91a0b2d
Merge branch 'main' into feat/apollo-v3
lbloder d58842a
update tests to use TraceSamplingDecision
lbloder 1eba47c
add secondary extension for java compatibility, add apollo to craft.yml
lbloder c750672
Merge branch 'main' into feat/apollo-v3
lbloder 5f906ab
update api
lbloder 7d95756
Merge branch 'main' into feat/apollo-v3
lbloder 65fdef8
Merge branch 'main' into feat/apollo-v3
adinauer 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,34 @@ | ||
public final class io/sentry/apollo3/SentryApollo3HttpInterceptor : com/apollographql/apollo3/network/http/HttpInterceptor { | ||
public static final field Companion Lio/sentry/apollo3/SentryApollo3HttpInterceptor$Companion; | ||
public static final field SENTRY_APOLLO_3_OPERATION_NAME Ljava/lang/String; | ||
public static final field SENTRY_APOLLO_3_OPERATION_TYPE Ljava/lang/String; | ||
public static final field SENTRY_APOLLO_3_VARIABLES Ljava/lang/String; | ||
public fun <init> ()V | ||
public fun <init> (Lio/sentry/IHub;)V | ||
public fun <init> (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)V | ||
public synthetic fun <init> (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public fun dispose ()V | ||
public fun intercept (Lcom/apollographql/apollo3/api/http/HttpRequest;Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; | ||
} | ||
|
||
public abstract interface class io/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback { | ||
public abstract fun execute (Lio/sentry/ISpan;Lcom/apollographql/apollo3/api/http/HttpRequest;Lcom/apollographql/apollo3/api/http/HttpResponse;)Lio/sentry/ISpan; | ||
} | ||
|
||
public final class io/sentry/apollo3/SentryApollo3HttpInterceptor$Companion { | ||
} | ||
|
||
public final class io/sentry/apollo3/SentryApollo3Interceptor : com/apollographql/apollo3/interceptor/ApolloInterceptor { | ||
public fun <init> ()V | ||
public fun intercept (Lcom/apollographql/apollo3/api/ApolloRequest;Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;)Lkotlinx/coroutines/flow/Flow; | ||
} | ||
|
||
public final class io/sentry/apollo3/SentryApolloBuilderExtensionsKt { | ||
public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; | ||
} | ||
|
This file contains 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,79 @@ | ||
import net.ltgt.gradle.errorprone.errorprone | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
`java-library` | ||
kotlin("jvm") | ||
jacoco | ||
id(Config.QualityPlugins.errorProne) | ||
id(Config.QualityPlugins.gradleVersions) | ||
id(Config.BuildPlugins.buildConfig) version Config.BuildPlugins.buildConfigVersion | ||
} | ||
|
||
configure<JavaPluginExtension> { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
tasks.withType<KotlinCompile>().configureEach { | ||
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
kotlinOptions.languageVersion = Config.kotlinCompatibleLanguageVersion | ||
} | ||
|
||
dependencies { | ||
api(projects.sentry) | ||
api(projects.sentryKotlinExtensions) | ||
|
||
implementation(Config.Libs.apolloKotlin) | ||
|
||
compileOnly(Config.CompileOnly.nopen) | ||
errorprone(Config.CompileOnly.nopenChecker) | ||
errorprone(Config.CompileOnly.errorprone) | ||
errorprone(Config.CompileOnly.errorProneNullAway) | ||
compileOnly(Config.CompileOnly.jetbrainsAnnotations) | ||
|
||
// tests | ||
testImplementation(projects.sentryTestSupport) | ||
testImplementation(Config.Libs.coroutinesCore) | ||
testImplementation(kotlin(Config.kotlinStdLib)) | ||
testImplementation(Config.TestLibs.kotlinTestJunit) | ||
testImplementation(Config.TestLibs.mockitoKotlin) | ||
testImplementation(Config.TestLibs.mockitoInline) | ||
testImplementation(Config.TestLibs.mockWebserver4) | ||
} | ||
|
||
configure<SourceSetContainer> { | ||
test { | ||
java.srcDir("src/test/java") | ||
} | ||
} | ||
|
||
jacoco { | ||
toolVersion = Config.QualityPlugins.Jacoco.version | ||
} | ||
|
||
tasks.jacocoTestReport { | ||
reports { | ||
xml.required.set(true) | ||
html.required.set(false) | ||
} | ||
} | ||
|
||
tasks { | ||
jacocoTestCoverageVerification { | ||
violationRules { | ||
rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } } | ||
} | ||
} | ||
check { | ||
dependsOn(jacocoTestCoverageVerification) | ||
dependsOn(jacocoTestReport) | ||
} | ||
} | ||
|
||
tasks.withType<JavaCompile>().configureEach { | ||
options.errorprone { | ||
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR) | ||
option("NullAway:AnnotatedPackages", "io.sentry") | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt
This file contains 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,181 @@ | ||
package io.sentry.apollo3 | ||
|
||
import com.apollographql.apollo3.api.http.HttpHeader | ||
import com.apollographql.apollo3.api.http.HttpRequest | ||
import com.apollographql.apollo3.api.http.HttpResponse | ||
import com.apollographql.apollo3.exception.ApolloHttpException | ||
import com.apollographql.apollo3.exception.ApolloNetworkException | ||
import com.apollographql.apollo3.network.http.HttpInterceptor | ||
import com.apollographql.apollo3.network.http.HttpInterceptorChain | ||
import io.sentry.Breadcrumb | ||
import io.sentry.Hint | ||
import io.sentry.HubAdapter | ||
import io.sentry.IHub | ||
import io.sentry.ISpan | ||
import io.sentry.SentryLevel | ||
import io.sentry.SpanStatus | ||
import io.sentry.TracingOrigins | ||
import io.sentry.TypeCheckHint | ||
|
||
class SentryApollo3HttpInterceptor @JvmOverloads constructor(private val hub: IHub = HubAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null) : | ||
HttpInterceptor { | ||
|
||
override suspend fun intercept( | ||
request: HttpRequest, | ||
chain: HttpInterceptorChain | ||
): HttpResponse { | ||
val activeSpan = hub.span | ||
return if (activeSpan == null) { | ||
chain.proceed(request) | ||
} else { | ||
val span = startChild(request, activeSpan) | ||
|
||
val cleanedHeaders = removeSentryInternalHeaders(request.headers) | ||
|
||
val requestBuilder = request.newBuilder().apply { | ||
headers(cleanedHeaders) | ||
} | ||
|
||
if (TracingOrigins.contain(hub.options.tracingOrigins, request.url)) { | ||
val sentryTraceHeader = span.toSentryTrace() | ||
val baggageHeader = span.toBaggageHeader() | ||
requestBuilder.addHeader(sentryTraceHeader.name, sentryTraceHeader.value) | ||
|
||
baggageHeader?.let { | ||
requestBuilder.addHeader(it.name, it.value) | ||
} | ||
} | ||
|
||
val modifiedRequest = requestBuilder.build() | ||
var httpResponse: HttpResponse? = null | ||
var statusCode: Int? = null | ||
|
||
try { | ||
httpResponse = chain.proceed(modifiedRequest) | ||
statusCode = httpResponse.statusCode | ||
span.status = SpanStatus.fromHttpStatusCode(statusCode, SpanStatus.UNKNOWN) | ||
return httpResponse | ||
} catch (e: Throwable) { | ||
when (e) { | ||
is ApolloHttpException -> { | ||
statusCode = e.statusCode | ||
span.status = SpanStatus.fromHttpStatusCode(statusCode, SpanStatus.INTERNAL_ERROR) | ||
} | ||
is ApolloNetworkException -> span.status = SpanStatus.INTERNAL_ERROR | ||
else -> SpanStatus.INTERNAL_ERROR | ||
} | ||
span.throwable = e | ||
throw e | ||
} finally { | ||
finish(span, modifiedRequest, httpResponse, statusCode) | ||
} | ||
} | ||
} | ||
|
||
private fun removeSentryInternalHeaders(headers: List<HttpHeader>): List<HttpHeader> { | ||
return headers.filterNot { it.name == SENTRY_APOLLO_3_VARIABLES || it.name == SENTRY_APOLLO_3_OPERATION_NAME || it.name == SENTRY_APOLLO_3_OPERATION_TYPE } | ||
} | ||
|
||
private fun startChild(request: HttpRequest, activeSpan: ISpan): ISpan { | ||
val url = request.url | ||
val method = request.method | ||
|
||
val operationName = operationNameFromHeaders(request) | ||
val operation = operationName ?: "apollo.client" | ||
val operationType = request.valueForHeader(SENTRY_APOLLO_3_OPERATION_TYPE) ?: method | ||
val operationId = request.valueForHeader("X-APOLLO-OPERATION-ID") | ||
val variables = request.valueForHeader(SENTRY_APOLLO_3_VARIABLES) | ||
val description = "$operationType ${operationName ?: url}" | ||
|
||
return activeSpan.startChild(operation, description).apply { | ||
operationId?.let { | ||
setData("operationId", it) | ||
} | ||
|
||
variables?.let { | ||
setData("variables", it) | ||
} | ||
} | ||
} | ||
|
||
private fun operationNameFromHeaders(request: HttpRequest): String? { | ||
return request.valueForHeader(SENTRY_APOLLO_3_OPERATION_NAME) ?: request.valueForHeader("X-APOLLO-OPERATION-NAME") | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private fun HttpRequest.valueForHeader(key: String) = headers.firstOrNull { it.name == key }?.value | ||
|
||
private fun finish(span: ISpan, request: HttpRequest, response: HttpResponse? = null, statusCode: Int?) { | ||
if (beforeSpan != null) { | ||
try { | ||
val result = beforeSpan.execute(span, request, response) | ||
if (result == null) { | ||
// Span is dropped | ||
span.spanContext.sampled = false | ||
} | ||
} catch (e: Throwable) { | ||
hub.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) | ||
} | ||
} | ||
span.finish() | ||
|
||
val breadcrumb = | ||
Breadcrumb.http(request.url, request.method.name, statusCode) | ||
|
||
request.body?.contentLength.ifHasValidLength { contentLength -> | ||
breadcrumb.setData("request_body_size", contentLength) | ||
} | ||
|
||
val hint = Hint().also { | ||
it.set(TypeCheckHint.APOLLO_REQUEST, request) | ||
} | ||
|
||
response?.let { httpResponse -> | ||
// Content-Length header is not present on batched operations | ||
httpResponse.headersContentLength().ifHasValidLength { contentLength -> | ||
breadcrumb.setData("response_body_size", contentLength) | ||
} | ||
|
||
if (!breadcrumb.data.containsKey("response_body_size")) { | ||
httpResponse.body?.buffer?.size?.ifHasValidLength { contentLength -> | ||
breadcrumb.setData("response_body_size", contentLength) | ||
} | ||
} | ||
|
||
hint.set(TypeCheckHint.APOLLO_RESPONSE, httpResponse) | ||
} | ||
|
||
hub.addBreadcrumb(breadcrumb, hint) | ||
} | ||
|
||
// Extensions | ||
|
||
private fun HttpResponse.headersContentLength(): Long { | ||
return headers.firstOrNull { it.name == "Content-Length" }?.value?.toLongOrNull() ?: -1L | ||
} | ||
|
||
private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { | ||
if (this != null && this != -1L) { | ||
fn.invoke(this) | ||
} | ||
} | ||
|
||
/** | ||
* The BeforeSpan callback | ||
*/ | ||
fun interface BeforeSpanCallback { | ||
/** | ||
* Mutates span before being added. | ||
* | ||
* @param span the span to mutate or drop | ||
* @param request the Apollo request object | ||
* @param response the Apollo response object | ||
*/ | ||
fun execute(span: ISpan, request: HttpRequest, response: HttpResponse?): ISpan? | ||
} | ||
|
||
companion object { | ||
const val SENTRY_APOLLO_3_VARIABLES = "SENTRY-APOLLO-3-VARIABLES" | ||
const val SENTRY_APOLLO_3_OPERATION_NAME = "SENTRY-APOLLO-3-OPERATION-NAME" | ||
const val SENTRY_APOLLO_3_OPERATION_TYPE = "SENTRY-APOLLO-3-OPERATION-TYPE" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3Interceptor.kt
This file contains 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,40 @@ | ||
package io.sentry.apollo3 | ||
|
||
import com.apollographql.apollo3.api.ApolloRequest | ||
import com.apollographql.apollo3.api.ApolloResponse | ||
import com.apollographql.apollo3.api.CustomScalarAdapters | ||
import com.apollographql.apollo3.api.Mutation | ||
import com.apollographql.apollo3.api.Operation | ||
import com.apollographql.apollo3.api.Query | ||
import com.apollographql.apollo3.api.Subscription | ||
import com.apollographql.apollo3.api.variables | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptor | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class SentryApollo3Interceptor : ApolloInterceptor { | ||
|
||
override fun <D : Operation.Data> intercept( | ||
request: ApolloRequest<D>, | ||
chain: ApolloInterceptorChain | ||
): Flow<ApolloResponse<D>> { | ||
val builder = request.newBuilder() | ||
.addHttpHeader(SentryApollo3HttpInterceptor.SENTRY_APOLLO_3_OPERATION_TYPE, operationType(request)) | ||
.addHttpHeader(SentryApollo3HttpInterceptor.SENTRY_APOLLO_3_OPERATION_NAME, request.operation.name()) | ||
|
||
request.scalarAdapters?.let { | ||
builder.addHttpHeader(SentryApollo3HttpInterceptor.SENTRY_APOLLO_3_VARIABLES, request.operation.variables(it).valueMap.toString()) | ||
} | ||
return chain.proceed(builder.build()) | ||
} | ||
} | ||
|
||
private fun <D : Operation.Data> operationType(apolloRequest: ApolloRequest<D>) = when (apolloRequest.operation) { | ||
is Query -> "query" | ||
is Mutation -> "mutation" | ||
is Subscription -> "subscription" | ||
else -> apolloRequest.operation.javaClass.simpleName | ||
} | ||
|
||
private val <D : Operation.Data> ApolloRequest<D>.scalarAdapters | ||
get() = executionContext[CustomScalarAdapters] |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires adding the package in here https://github.com/getsentry/sentry-release-registry (for GA at least)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened getsentry/sentry-release-registry#78
Needs changes to craft.yml still
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marandaneto should I wait with adapting the
craft.yml
until the release registry PR is approved?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd comment it out, for now, getsentry/sentry-release-registry#78 isn't done yet and 6.2.0 is already outdated.