Amplitude-Kotlin
Amplitude-Kotlin copied to clipboard
Race condition when using multiple instances of Amplitude.
Expected Behavior
Events from two or more instances of Amplitude are sent to the respective endpoints.
Current Behavior
When 2 instances of Amplitude are used in the same project, a condition occurs when both instances use StorageProviders initialized to work in the same directory. Thus, a situation arises when events from one instance go to another.
Possible Solution
Override storageProvider in configuration to use different prefixes for each instance, but it doesn't seem to be a clear solution.
or
Change internal logic of library to use instanceName from config as part of prefix to initialize storageProvider.
Steps to Reproduce
- Initialize 2 or more instances of Amplitude with different configurations (but keep default
storageProvider). - Start sending events (send events to both instances at the same time).
- Watch as events are sent to the wrong endpoints.
Environment
- Device: Google Pixel 6 Pro
- Device OS and Version: Android 13
Hi @dmoklyakov, thanks for choosing amplitude. What version of the Kotlin SDKs are you using?
Hi @yuhao900914! I'm using version 1.7.1.
Is there an update on this, or any workarounds? I believe I am also hitting this issue
@kennedyjosh you can use my 1st suggested possible solution as workaround. You just need to pass storageProvider to Amplitude configuration like this:
storageProvider = object: StorageProvider {
override fun getStorage(
amplitude: Amplitude,
ignored: String?
): Storage {
return AndroidStorage(
context,
amplitude.configuration.apiKey,
amplitude.configuration.loggerProvider.getLogger(amplitude),
yourCustomPrefix // Should be a distinct string for each Amplitude instance.
)
}
}
@kennedyjosh you can use my 1st suggested possible solution as workaround
perhaps my version of the SDK (1.5.2) does not have the same API, as in this version it seems AndroidStorage only takes 2 parameters: context and apiKey.
Examining the source code for my version, it seems the prefixes are different (it uses the api key) so I don't think I am having the same issue as you, probably just a similar one. Examining each instance's StorageProvider instances using the debugger confirms they are accessing separate files in separate directories.