Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ fun AndroidComponentsExtension<*, *, *>.configure(
tmpDir.mkdirs()

onVariants { variant ->
if (extension.instrumentation.enabled.get()) {
if (extension.instrumentation.automaticOkHttpInstrumentation.get()) {
variant.configureInstrumentation(
SpanAddingClassVisitorFactory::class.java,
InstrumentationScope.ALL,
FramesComputationMode.COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS,
) { params ->
params.tmpDir.set(tmpDir)
params.debug.set(extension.instrumentation.debug)
params.proxyOkHttpEventListener.set(extension.instrumentation.proxyOkHttpEventListener)
params.okHttpInstrumentationType.set(extension.instrumentation.okHttpInstrumentationType)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ package io.bitdrift.capture.extension
import org.gradle.api.Project
import javax.inject.Inject

abstract class BitdriftPluginExtension @Inject constructor(project: Project) {
private val objects = project.objects

val instrumentation: InstrumentationExtension = objects.newInstance(InstrumentationExtension::class.java)
}
open class BitdriftPluginExtension @Inject constructor(project: Project) {
val instrumentation = project.extensions.create("instrumentation", InstrumentationExtension::class.java, project)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@
package io.bitdrift.capture.extension

import javax.inject.Inject
import org.gradle.api.model.ObjectFactory
import org.gradle.api.Project
import org.gradle.api.provider.Property

open class InstrumentationExtension @Inject constructor(objects: ObjectFactory) {
open class InstrumentationExtension @Inject constructor(project: Project) {
private val objects = project.objects

val enabled: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)
val automaticOkHttpInstrumentation: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)

val debug: Property<Boolean> = objects.property(Boolean::class.java).convention(
false
)

val proxyOkHttpEventListener: Property<Boolean> = objects.property(Boolean::class.java).convention(false)
}
val okHttpInstrumentationType: Property<OkHttpInstrumentationType> = objects.property(OkHttpInstrumentationType::class.java).convention(OkHttpInstrumentationType.OVERWRITE)

enum class OkHttpInstrumentationType {
PROXY,
OVERWRITE,
}

// Helpers so that these values can be used directly in the DSL
val PROXY = OkHttpInstrumentationType.PROXY
val OVERWRITE = OkHttpInstrumentationType.OVERWRITE
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.android.build.api.instrumentation.ClassContext
import com.android.build.api.instrumentation.ClassData
import com.android.build.api.instrumentation.InstrumentationParameters
import io.bitdrift.capture.CapturePlugin
import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType
import io.bitdrift.capture.instrumentation.okhttp.OkHttpEventListener
import io.bitdrift.capture.instrumentation.util.findClassReader
import io.bitdrift.capture.instrumentation.util.findClassWriter
Expand All @@ -55,7 +56,7 @@ abstract class SpanAddingClassVisitorFactory : AsmClassVisitorFactory<SpanAdding
val debug: Property<Boolean>

@get:Input
val proxyOkHttpEventListener: Property<Boolean>
val okHttpInstrumentationType: Property<OkHttpInstrumentationType>

@get:Internal
val tmpDir: Property<File>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class OkHttpEventListenerMethodInstrumentable(
apiVersion = apiVersion,
originalVisitor = originalVisitor,
instrumentableContext = instrumentableContext,
proxyEventListener = parameters.proxyOkHttpEventListener.get()
okHttpInstrumentationType = parameters.okHttpInstrumentationType.get()
)

override fun isInstrumentable(data: MethodContext): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

package io.bitdrift.capture.instrumentation.okhttp.visitor

import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType
import io.bitdrift.capture.instrumentation.MethodContext
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
Expand All @@ -42,7 +43,7 @@ class OkHttpEventListenerMethodVisitor(
apiVersion: Int,
originalVisitor: MethodVisitor,
instrumentableContext: MethodContext,
val proxyEventListener: Boolean
val okHttpInstrumentationType: OkHttpInstrumentationType
) : AdviceAdapter(
apiVersion,
originalVisitor,
Expand All @@ -57,10 +58,9 @@ class OkHttpEventListenerMethodVisitor(
override fun onMethodEnter() {
super.onMethodEnter()

if (proxyEventListener) {
addProxyingEventListener()
} else {
addOverwritingEventListener()
when (okHttpInstrumentationType) {
OkHttpInstrumentationType.PROXY -> addProxyingEventListener()
OkHttpInstrumentationType.OVERWRITE -> addOverwritingEventListener()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

package io.bitdrift.capture.instrumentation.fakes

import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType
import io.bitdrift.capture.instrumentation.ClassInstrumentable
import io.bitdrift.capture.instrumentation.SpanAddingClassVisitorFactory
import java.io.File
Expand All @@ -47,9 +48,9 @@ class TestSpanAddingParameters(
override val debug: Property<Boolean>
get() = DefaultProperty(PropertyHost.NO_OP, Boolean::class.javaObjectType)
.convention(debugOutput)
override val proxyOkHttpEventListener: Property<Boolean>
get() = DefaultProperty(PropertyHost.NO_OP, Boolean::class.javaObjectType)
.convention(true)
override val okHttpInstrumentationType: Property<OkHttpInstrumentationType>
get() = DefaultProperty(PropertyHost.NO_OP, OkHttpInstrumentationType::class.javaObjectType)
.convention(OkHttpInstrumentationType.PROXY)

override val tmpDir: Property<File>
get() = DefaultProperty<File>(PropertyHost.NO_OP, File::class.java).convention(inMemoryDir)
Expand Down
Loading