amplitude / Amplitude-Kotlin

Amplitude Kotlin SDK
MIT License
27 stars 10 forks source link

Race condition when using multiple instances of Amplitude. #110

Open d-mokliakov opened 1 year ago

d-mokliakov commented 1 year ago

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

  1. Initialize 2 or more instances of Amplitude with different configurations (but keep default storageProvider).
  2. Start sending events (send events to both instances at the same time).
  3. Watch as events are sent to the wrong endpoints.

Environment

yuhao900914 commented 1 year ago

Hi @dmoklyakov, thanks for choosing amplitude. What version of the Kotlin SDKs are you using?

d-mokliakov commented 1 year ago

Hi @yuhao900914! I'm using version 1.7.1.

kennedyjosh commented 1 year ago

Is there an update on this, or any workarounds? I believe I am also hitting this issue

d-mokliakov commented 1 year ago

@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 commented 1 year ago

@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.