docs/reference/koin-annotations/monitor.md
The @Monitor annotation (available since Koin Annotations 2.2.0) enables automatic performance monitoring and tracing for your Koin components through the Kotzilla Platform, the official tooling platform for Koin.
Add the Kotzilla SDK dependency:
dependencies {
implementation "io.kotzilla:kotzilla-core:latest.version"
}
Check the latest version on the Kotzilla documentation.
Configure the allOpen plugin to make monitored classes extensible:
plugins {
id "org.jetbrains.kotlin.plugin.allopen"
}
allOpen {
annotation("org.koin.core.annotation.Monitor")
}
Initialize Kotzilla analytics in your Koin configuration:
import io.kotzilla.sdk.analytics.koin.analytics
fun initKoin() {
startKoin {
// Enable Kotzilla monitoring
analytics()
modules(appModule)
}
}
Simply annotate your Koin components with @Monitor:
@Monitor
@Single
class UserService(private val userRepository: UserRepository) {
fun findUser(id: String): User? = userRepository.findById(id)
suspend fun createUser(userData: UserData): User {
return userRepository.save(userData)
}
}
The compiler automatically generates a proxy class that wraps your component:
/**
* Generated by @Monitor - Koin proxy for 'UserService'
*/
class UserServiceProxy(userRepository: UserRepository) : UserService(userRepository) {
override fun findUser(id: String): User? {
return KotzillaCore.getDefaultInstance().trace("UserService.findUser") {
super.findUser(id)
}
}
override suspend fun createUser(userData: UserData): User {
return KotzillaCore.getDefaultInstance().suspendTrace("UserService.createUser") {
super.createUser(userData)
}
}
}
Koin automatically uses the proxy instead of the original class, transparently capturing:
Monitor your ViewModels to track UI performance:
@Monitor
@KoinViewModel
class DetailViewModel(private val repository: Repository) : ViewModel() {
fun loadData(id: String): StateFlow<Data> = repository.getData(id)
}
The monitoring data is automatically sent to your Kotzilla Platform workspace, providing:
Create your free Kotzilla account and configure the API key in your kotzilla.json file:
{
"sdkVersion": "latest.version",
"keys": [
{
"appId": "your-app-id",
"applicationPackageName": "com.example.app",
"keyId": "your-key-id",
"apiKey": "your-api-key"
}
]
}
@Monitor must be open (automatically handled by allOpen plugin):::info
The @Monitor annotation only tracks method calls on the monitored class itself. Dependencies injected into the monitored class are not automatically monitored unless they are also annotated with @Monitor.
:::
:::note For complete setup instructions and advanced configuration options, visit the Kotzilla Documentation. :::