Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SentryOkHttpInterceptor instrumentation (#288)
- Loading branch information
Showing
20 changed files
with
265 additions
and
76 deletions.
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
23 changes: 23 additions & 0 deletions
23
...umentation-sample/src/main/java/io/sentry/samples/instrumentation/network/TrackService.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,23 @@ | ||
package io.sentry.samples.instrumentation.network | ||
|
||
import io.sentry.samples.instrumentation.data.Track | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.create | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
|
||
interface TrackService { | ||
|
||
@GET("v3/{uuid}") | ||
suspend fun tracks(@Path("uuid") uuid: String): List<Track> | ||
|
||
companion object { | ||
private val retrofit = Retrofit.Builder() | ||
.baseUrl("https://run.mocky.io/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
val instance = retrofit.create<TrackService>() | ||
} | ||
} |
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
14 changes: 0 additions & 14 deletions
14
...oid-instrumentation-sample/src/main/java/io/sentry/samples/instrumentation/util/Lyrics.kt
This file was deleted.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
plugin-build/src/main/kotlin/io/sentry/android/gradle/instrumentation/okhttp/OkHttp.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,49 @@ | ||
package io.sentry.android.gradle.instrumentation.okhttp | ||
|
||
import com.android.build.api.instrumentation.ClassContext | ||
import io.sentry.android.gradle.instrumentation.ClassInstrumentable | ||
import io.sentry.android.gradle.instrumentation.CommonClassVisitor | ||
import io.sentry.android.gradle.instrumentation.MethodContext | ||
import io.sentry.android.gradle.instrumentation.MethodInstrumentable | ||
import io.sentry.android.gradle.instrumentation.SpanAddingClassVisitorFactory | ||
import io.sentry.android.gradle.instrumentation.okhttp.visitor.ResponseWithInterceptorChainMethodVisitor | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
|
||
class OkHttp : ClassInstrumentable { | ||
override val fqName: String get() = "okhttp3.internal.connection.RealCall" | ||
|
||
override fun getVisitor( | ||
instrumentableContext: ClassContext, | ||
apiVersion: Int, | ||
originalVisitor: ClassVisitor, | ||
parameters: SpanAddingClassVisitorFactory.SpanAddingParameters | ||
): ClassVisitor = CommonClassVisitor( | ||
apiVersion = apiVersion, | ||
classVisitor = originalVisitor, | ||
className = fqName.substringAfterLast('.'), | ||
methodInstrumentables = listOf(ResponseWithInterceptorChain()), | ||
parameters = parameters | ||
) | ||
} | ||
|
||
class ResponseWithInterceptorChain : MethodInstrumentable { | ||
override val fqName: String get() = "getResponseWithInterceptorChain" | ||
|
||
override fun getVisitor( | ||
instrumentableContext: MethodContext, | ||
apiVersion: Int, | ||
originalVisitor: MethodVisitor, | ||
parameters: SpanAddingClassVisitorFactory.SpanAddingParameters | ||
): MethodVisitor = ResponseWithInterceptorChainMethodVisitor( | ||
api = apiVersion, | ||
originalVisitor = originalVisitor, | ||
access = instrumentableContext.access, | ||
name = instrumentableContext.name, | ||
descriptor = instrumentableContext.descriptor | ||
) | ||
|
||
override fun isInstrumentable(data: MethodContext): Boolean { | ||
return data.name?.startsWith(fqName) == true | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...ndroid/gradle/instrumentation/okhttp/visitor/ResponseWithInterceptorChainMethodVisitor.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,95 @@ | ||
package io.sentry.android.gradle.instrumentation.okhttp.visitor | ||
|
||
import io.sentry.android.gradle.instrumentation.util.Types | ||
import org.objectweb.asm.Label | ||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.commons.GeneratorAdapter | ||
import org.objectweb.asm.commons.Method | ||
|
||
class ResponseWithInterceptorChainMethodVisitor( | ||
api: Int, | ||
private val originalVisitor: MethodVisitor, | ||
access: Int, | ||
name: String?, | ||
descriptor: String? | ||
) : GeneratorAdapter(api, originalVisitor, access, name, descriptor) { | ||
|
||
private var shouldInstrument = false | ||
|
||
override fun visitMethodInsn( | ||
opcode: Int, | ||
owner: String?, | ||
name: String?, | ||
descriptor: String?, | ||
isInterface: Boolean | ||
) { | ||
if (opcode == Opcodes.INVOKEVIRTUAL && | ||
owner == "okhttp3/OkHttpClient" && | ||
name == "interceptors" | ||
) { | ||
shouldInstrument = true | ||
} | ||
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) | ||
} | ||
|
||
override fun visitInsn(opcode: Int) { | ||
super.visitInsn(opcode) | ||
if (opcode == Opcodes.POP && shouldInstrument) { | ||
visitAddSentryInterceptor() | ||
shouldInstrument = false | ||
} | ||
} | ||
|
||
/* | ||
Roughly constructing this, but in Java: | ||
if (interceptors.find { it is SentryOkHttpInterceptor } != null) { | ||
interceptors += SentryOkHttpInterceptor() | ||
} | ||
*/ | ||
private fun MethodVisitor.visitAddSentryInterceptor() { | ||
originalVisitor.visitVarInsn(Opcodes.ALOAD, 1) // interceptors list | ||
|
||
checkCast(Types.ITERABLE) | ||
invokeInterface(Types.ITERABLE, Method.getMethod("java.util.Iterator iterator ()")) | ||
val iteratorIndex = newLocal(Types.ITERATOR) | ||
storeLocal(iteratorIndex) | ||
|
||
val whileLabel = Label() | ||
val endWhileLabel = Label() | ||
visitLabel(whileLabel) | ||
loadLocal(iteratorIndex) | ||
invokeInterface(Types.ITERATOR, Method.getMethod("boolean hasNext ()")) | ||
ifZCmp(EQ, endWhileLabel) | ||
loadLocal(iteratorIndex) | ||
invokeInterface(Types.ITERATOR, Method.getMethod("Object next ()")) | ||
|
||
val interceptorIndex = newLocal(Types.OBJECT) | ||
storeLocal(interceptorIndex) | ||
loadLocal(interceptorIndex) | ||
checkCast(Types.OKHTTP_INTERCEPTOR) | ||
instanceOf(Types.SENTRY_OKHTTP_INTERCEPTOR) | ||
ifZCmp(EQ, whileLabel) | ||
loadLocal(interceptorIndex) | ||
val ifLabel = Label() | ||
goTo(ifLabel) | ||
|
||
visitLabel(endWhileLabel) | ||
originalVisitor.visitInsn(Opcodes.ACONST_NULL) | ||
visitLabel(ifLabel) | ||
val originalMethodLabel = Label() | ||
ifNonNull(originalMethodLabel) | ||
|
||
originalVisitor.visitVarInsn(Opcodes.ALOAD, 1) | ||
checkCast(Types.COLLECTION) | ||
newInstance(Types.SENTRY_OKHTTP_INTERCEPTOR) | ||
dup() | ||
val sentryOkHttpCtor = Method.getMethod("void <init> ()") | ||
invokeConstructor(Types.SENTRY_OKHTTP_INTERCEPTOR, sentryOkHttpCtor) | ||
val addInterceptor = Method.getMethod("boolean add (Object)") | ||
invokeInterface(Types.COLLECTION, addInterceptor) | ||
pop() | ||
visitLabel(originalMethodLabel) | ||
} | ||
} |
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
Oops, something went wrong.