Koin Built-in Performance Monitoring with @Monitor
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.
Setup
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)
}
}
Basic Usage
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)
}
}
Generated Code
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:
- Method execution times
- Call frequency and patterns
- Error rates and types
- Performance bottlenecks
ViewModels Monitoring
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)
}
Kotzilla Platform Integration
The monitoring data is automatically sent to your Kotzilla Platform workspace, providing:
- Real-time Performance Dashboard: View method execution times and trends
- Error Tracking: Monitor exception rates and stack traces
- Usage Analytics: Understand which components are most heavily used
- Performance Alerts: Get notified of performance regressions
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"
}
]
}
Requirements
- Classes annotated with
@Monitor
must be open (automatically handled byallOpen
plugin) - Kotzilla SDK dependency must be available at runtime
- Valid Kotzilla Platform account and API key for data collection
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
.
For complete setup instructions and advanced configuration options, visit the Kotzilla Documentation.