-
-
Notifications
You must be signed in to change notification settings - Fork 427
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
Changes from 7 commits
770b0c9
d2a6428
75f9086
2418613
e70c24a
a99e873
71dabc0
ea95a8c
e32dae4
b2112d7
683d374
6f2d6be
ab17056
700504e
6164c80
91a0b2d
d58842a
1eba47c
c750672
5f906ab
7d95756
65fdef8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,8 @@ object Config { | |
val graphQlJava = "com.graphql-java:graphql-java:17.3" | ||
|
||
val kotlinReflect = "org.jetbrains.kotlin:kotlin-reflect" | ||
|
||
val apollo3 = "com.apollographql.apollo3:apollo-runtime:3.3.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call
|
||
} | ||
|
||
object AnnotationProcessors { | ||
|
@@ -132,6 +134,7 @@ object Config { | |
val awaitility = "org.awaitility:awaitility-kotlin:4.1.1" | ||
val mockWebserver = "com.squareup.okhttp3:mockwebserver:${Libs.okHttpVersion}" | ||
val mockWebserver3 = "com.squareup.okhttp3:mockwebserver:3.14.9" | ||
val mockWebserver4 = "com.squareup.okhttp3:mockwebserver:4.9.3" | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val jsonUnit = "net.javacrumbs.json-unit:json-unit:2.32.0" | ||
val hsqldb = "org.hsqldb:hsqldb:2.6.1" | ||
val javaFaker = "com.github.javafaker:javafaker:1.0.2" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
public final class io/sentry/apollo3/ApolloRequestBodyContent { | ||
public fun <init> ()V | ||
public fun <init> (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)V | ||
public synthetic fun <init> (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public final fun component1 ()Ljava/lang/String; | ||
public final fun component2 ()Ljava/util/Map; | ||
public final fun component3 ()Ljava/lang/String; | ||
public final fun copy (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)Lio/sentry/apollo3/ApolloRequestBodyContent; | ||
public static synthetic fun copy$default (Lio/sentry/apollo3/ApolloRequestBodyContent;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;ILjava/lang/Object;)Lio/sentry/apollo3/ApolloRequestBodyContent; | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getOperationName ()Ljava/lang/String; | ||
public final fun getQuery ()Ljava/lang/String; | ||
public final fun getVariables ()Ljava/util/Map; | ||
public fun hashCode ()I | ||
public final fun setOperationName (Ljava/lang/String;)V | ||
public final fun setQuery (Ljava/lang/String;)V | ||
public final fun setVariables (Ljava/util/Map;)V | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
||
public final class io/sentry/apollo3/ApolloRequestBodyContent$Deserializer : io/sentry/JsonDeserializer { | ||
public static final field INSTANCE Lio/sentry/apollo3/ApolloRequestBodyContent$Deserializer; | ||
public fun deserialize (Lio/sentry/JsonObjectReader;Lio/sentry/ILogger;)Lio/sentry/apollo3/ApolloRequestBodyContent; | ||
public synthetic fun deserialize (Lio/sentry/JsonObjectReader;Lio/sentry/ILogger;)Ljava/lang/Object; | ||
} | ||
|
||
public final class io/sentry/apollo3/ApolloRequestBodyContent$JsonKeys { | ||
public static final field INSTANCE Lio/sentry/apollo3/ApolloRequestBodyContent$JsonKeys; | ||
public static final field OPERATION_NAME Ljava/lang/String; | ||
public static final field QUERY Ljava/lang/String; | ||
public static final field VARIABLES Ljava/lang/String; | ||
} | ||
|
||
public final class io/sentry/apollo3/SentryApollo3HttpInterceptor : com/apollographql/apollo3/network/http/HttpInterceptor { | ||
public static final field Companion Lio/sentry/apollo3/SentryApollo3HttpInterceptor$Companion; | ||
public fun <init> ()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 fun getKNOWN_OPERATIONS ()Ljava/util/List; | ||
} | ||
|
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.apollo3) | ||
|
||
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") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.sentry.apollo3 | ||
|
||
import io.sentry.ILogger | ||
import io.sentry.JsonDeserializer | ||
import io.sentry.JsonObjectReader | ||
import io.sentry.vendor.gson.stream.JsonToken | ||
|
||
data class ApolloRequestBodyContent( | ||
var query: String = "", | ||
var variables: Map<String, Any>? = null, | ||
var operationName: String? = null | ||
) { | ||
// JsonSerializable | ||
object JsonKeys { | ||
const val QUERY = "query" | ||
const val VARIABLES = "variables" | ||
const val OPERATION_NAME = "operationName" | ||
} | ||
|
||
object Deserializer : JsonDeserializer<ApolloRequestBodyContent> { | ||
override fun deserialize(reader: JsonObjectReader, logger: ILogger): ApolloRequestBodyContent { | ||
val content = ApolloRequestBodyContent() | ||
reader.beginObject() | ||
while (reader.peek() == JsonToken.NAME) { | ||
when (val nextName = reader.nextName()) { | ||
JsonKeys.VARIABLES -> content.variables = reader.nextObjectOrNull() as? Map<String, Any> | ||
JsonKeys.QUERY -> content.query = reader.nextString() | ||
JsonKeys.OPERATION_NAME -> content.operationName = reader.nextString() | ||
else -> { | ||
reader.nextUnknown(logger, mutableMapOf(), nextName) | ||
} | ||
} | ||
} | ||
reader.endObject() | ||
return content | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package io.sentry.apollo3 | ||
|
||
import com.apollographql.apollo3.api.http.HttpRequest | ||
import com.apollographql.apollo3.api.http.HttpResponse | ||
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 | ||
import okio.Buffer | ||
import java.io.InputStreamReader | ||
|
||
class SentryApollo3HttpInterceptor(private val hub: IHub = HubAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null) : | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 requestBuilder = request.newBuilder() | ||
|
||
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() | ||
|
||
return try { | ||
val httpResponse = chain.proceed(modifiedRequest) | ||
span.status = SpanStatus.fromHttpStatusCode(httpResponse.statusCode, SpanStatus.UNKNOWN) | ||
finish(span, modifiedRequest, httpResponse) | ||
|
||
httpResponse | ||
} catch (e: Exception) { | ||
when (e) { | ||
is ApolloNetworkException -> span.status = SpanStatus.INTERNAL_ERROR | ||
else -> SpanStatus.UNKNOWN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks wrong, every error would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will double check but, afaik we get a httpResponse for all Status Codes including 4xx and 5xx in the HttpInterceptor |
||
} | ||
finish(span, modifiedRequest) | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw e | ||
} | ||
} | ||
} | ||
|
||
private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { | ||
if (this != null && this != -1L) { | ||
fn.invoke(this) | ||
} | ||
} | ||
|
||
private fun startChild(request: HttpRequest, activeSpan: ISpan): ISpan { | ||
val buffer = Buffer() | ||
request.body?.writeTo(buffer) | ||
|
||
val reader = InputStreamReader(buffer.inputStream()) | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val content = try { | ||
hub.options.serializer.deserialize( | ||
reader, | ||
ApolloRequestBodyContent::class.java, | ||
ApolloRequestBodyContent.Deserializer | ||
) | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (e: Exception) { | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hub.options.logger.log(SentryLevel.ERROR, "Error deserializing Apollo Request Body.", e) | ||
} | ||
|
||
val span = when (content) { | ||
is ApolloRequestBodyContent -> createSpanFromBodyContent(activeSpan, request, content) | ||
else -> activeSpan.startChild("apollo.request.unknown") | ||
} | ||
|
||
val operationId = request.headers.firstOrNull { it.name == "X-APOLLO-OPERATION-ID" }?.value ?: "unknown" | ||
|
||
return span.apply { | ||
setData("operationId", operationId) | ||
} | ||
} | ||
|
||
private fun createSpanFromBodyContent(activeSpan: ISpan, request: HttpRequest, content: ApolloRequestBodyContent): ISpan { | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val operationType = parseOperationType(content) | ||
val operationName = content.operationName ?: request.headers.firstOrNull { it.name == "X-APOLLO-OPERATION-NAME" }?.value ?: "unknown" | ||
val description = "$operationType $operationName" | ||
|
||
return activeSpan.startChild(operationName, description).apply { | ||
setData("variables", content.variables.toString()) | ||
} | ||
} | ||
|
||
private fun finish(span: ISpan, request: HttpRequest, response: HttpResponse? = null) { | ||
var newSpan: ISpan = span | ||
if (beforeSpan != null) { | ||
try { | ||
newSpan = beforeSpan.execute(span, request, response) | ||
} catch (e: Exception) { | ||
hub.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) | ||
} | ||
} | ||
newSpan.finish() | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
response?.let { httpResponse -> | ||
val breadcrumb = | ||
Breadcrumb.http(request.url, request.method.name, httpResponse.statusCode) | ||
|
||
request.body?.contentLength.ifHasValidLength { contentLength -> | ||
breadcrumb.setData("request_body_size", contentLength) | ||
} | ||
|
||
httpResponse.body?.peek()?.readByteArray()?.size?.toLong().ifHasValidLength { contentLength -> | ||
breadcrumb.setData("response_body_size", contentLength) | ||
} | ||
|
||
val hint = Hint().also { | ||
it.set(TypeCheckHint.APOLLO_REQUEST, request) | ||
it.set(TypeCheckHint.APOLLO_RESPONSE, httpResponse) | ||
} | ||
|
||
hub.addBreadcrumb(breadcrumb, hint) | ||
} | ||
} | ||
|
||
private fun parseOperationType(content: ApolloRequestBodyContent?): String { | ||
val operationPart = content?.query?.takeWhile { !it.isWhitespace() } | ||
return KNOWN_OPERATIONS.firstOrNull { it == operationPart } ?: "unknown" | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
companion object { | ||
val KNOWN_OPERATIONS = listOf("query", "mutation", "subscription") | ||
} | ||
|
||
/** | ||
* 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 | ||
} | ||
} |
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.