KmpAppInsights
KmpAppInsights
KmpAppInsights is a Kotlin Multiplatform library that helps your product connect to Microsoft Azure Insights. Supports both iOS & Android
You can find the latest package here from MVN
How it works
KmpAppInsights automatically captures logs and stores it locally in an SQLite file on your app's internal storage. Once you configure your instrumentation key (which you can take from your Azure portal), the library will start uploading data to microsoft insights.
To get started, import the library into your project:
_1implementation("io.github.thearchitect123:appInsights:+") // latest package
Once you import your library and start your app, KmpAppInsights automatically starts tracking logs internally (depending on the functions you invoke below). To configure your instrumentation key, please write this inside your Application class:
_2 val delayBetweenUploads = 10 // number of seconds between each upload call _2 InsightsClient.configureInsightsClient("MY_SUPER_SECRET_INSIGHTS_KEY", delayBetweenUploads, allowBackgrounding = true)
KmpAppInsights relies on a timer that loops based on number of seconds that you pass to it. Every, seconds based on input, it will upload the logs captured on storage to Azure. It's optimal to keep this delay at around 30 seconds to a minute at most. Too many calls will exhaust your connection pool, and could cause slowdowns or crashes on your app.
By default the library only uploads the insights data in the foreground. However by setting allowBackgrounding to true, it configures your app to upload regardless if the device is locked or if the app is running in the background. Please note that to do this you must have backgrounding configured, and KmpEssentials installed.
Below are the functions available, and they each output a different log type on Azure.
_26 KmpAppInsights.writeCustomEvent(message: EventTypeMap, eventName: String)_26 _26 // best suited for navigation (when page appears/disappears for example)_26 KmpAppInsights.writePageView(_26 message: EventTypeMap,_26 eventName: String,_26 pageName: String,_26 sessionId: String = ""_26 )_26_26 // best suited for logging http requests_26 KmpAppInsights.writeRequest(_26 message: EventTypeMap,_26 eventName: String,_26 requestInfo: RequestStorageData_26 )_26_26 // best for debugging events_26 KmpAppInsights.writeTrace(_26 message: EventTypeMap,_26 eventName: String,_26 level: TraceSeverityLevel_26 )_26_26 // write all your errors using this function_26 KmpAppInsights.writeException(ex: Exception, message: EventTypeMap)
You also have the option to force flush all your logs manually without having to wait for the internal timer to expire. To do so, please use this:
_1 KmpAppInsights.forceFlushAllLogs()
Setup Requirements for Android
Regards if you are not using KmpEssentials or you are, you must invoke this function inside your Application class BEFORE invoking InsightsClient.configureInsightsClient. To do so please add this inside your App's Application class:
_12class CoreApplication : Application() {_12 override fun onCreate() {_12 super.onCreate()_12_12 ..my app code_12_12 ApplicationInsights.initialize(this)_12_12 val delayBetweenUploads = 10 // number of seconds between each upload call _12 InsightsClient.configureInsightsClient("MY_SUPER_SECRET_INSIGHTS_KEY", delayBetweenUploads, allowBackgrounding = true)_12 }_12}
Setup Requirements for iOS
None Required. Just import the library directly into your shared module, and it just works!