Skip to content

Commit

Permalink
AMP-94015 update the way for diagonostics
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen committed Feb 28, 2024
1 parent a0c6524 commit c267552
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import com.amplitude.android.Configuration
import com.amplitude.android.utilities.AndroidStorage

class ApiKeyStorageMigration(
private val amplitude: Amplitude
private val amplitude: Amplitude,
) {
suspend fun execute() {
val configuration = amplitude.configuration as Configuration
val logger = amplitude.logger

val storage = amplitude.storage as? AndroidStorage
if (storage != null) {
val apiKeyStorage = AndroidStorage(configuration.context, configuration.apiKey, logger, storage.prefix)
val apiKeyStorage = AndroidStorage(configuration.context, configuration.apiKey, logger, storage.prefix, amplitude.diagnostics)
StorageKeyMigration(apiKeyStorage, storage, logger).execute()
}

val identifyInterceptStorage = amplitude.identifyInterceptStorage as? AndroidStorage
if (identifyInterceptStorage != null) {
val apiKeyStorage = AndroidStorage(configuration.context, configuration.apiKey, logger, identifyInterceptStorage.prefix)
val apiKeyStorage =
AndroidStorage(configuration.context, configuration.apiKey, logger, identifyInterceptStorage.prefix, amplitude.diagnostics)
StorageKeyMigration(apiKeyStorage, identifyInterceptStorage, logger).execute()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.amplitude.core.Storage
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.EventPipeline
import com.amplitude.core.utilities.Diagnostics
import com.amplitude.core.utilities.EventsFileManager
import com.amplitude.core.utilities.EventsFileStorage
import com.amplitude.core.utilities.FileResponseHandler
Expand All @@ -25,6 +26,7 @@ class AndroidStorage(
val storageKey: String,
private val logger: Logger,
internal val prefix: String?,
private val diagnostics: Diagnostics,
) : Storage, EventsFileStorage {
companion object {
const val STORAGE_PREFIX = "amplitude-android"
Expand All @@ -34,7 +36,7 @@ class AndroidStorage(
context.getSharedPreferences("${getPrefix()}-$storageKey", Context.MODE_PRIVATE)
private val storageDirectory: File = context.getDir(getDir(), Context.MODE_PRIVATE)
private val eventsFile =
EventsFileManager(storageDirectory, storageKey, AndroidKVS(sharedPreferences), logger)
EventsFileManager(storageDirectory, storageKey, AndroidKVS(sharedPreferences), logger, diagnostics)
private val eventCallbacksMap = mutableMapOf<String, EventCallBack>()

override suspend fun writeEvent(event: BaseEvent) {
Expand Down Expand Up @@ -112,10 +114,6 @@ class AndroidStorage(
eventsFile.splitFile(filePath, events)
}

override suspend fun getDiagnostics(): String {
return eventsFile.getDiagnostics()
}

private fun getPrefix(): String {
return prefix ?: STORAGE_PREFIX
}
Expand All @@ -139,6 +137,7 @@ class AndroidStorageProvider : StorageProvider {
configuration.instanceName,
configuration.loggerProvider.getLogger(amplitude),
prefix,
amplitude.diagnostics,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.amplitude.android.utilities.AndroidStorage
import com.amplitude.common.jvm.ConsoleLogger
import com.amplitude.core.Storage
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.utilities.Diagnostics
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.jupiter.api.Assertions
Expand All @@ -16,13 +17,15 @@ import java.util.UUID

@RunWith(RobolectricTestRunner::class)
class StorageKeyMigrationTest {
private val testDiagnostics = Diagnostics()

@Test
fun `simple values should be migrated`() {
val context = ApplicationProvider.getApplicationContext<Context>()
val logger = ConsoleLogger()

val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)
val sourceFileIndexKey = "amplitude.events.file.index.${source.storageKey}"
val destinationFileIndexKey = "amplitude.events.file.index.${destination.storageKey}"

Expand Down Expand Up @@ -74,8 +77,8 @@ class StorageKeyMigrationTest {
val context = ApplicationProvider.getApplicationContext<Context>()
val logger = ConsoleLogger()

val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)

runBlocking {
source.writeEvent(createEvent(1))
Expand Down Expand Up @@ -117,8 +120,8 @@ class StorageKeyMigrationTest {
val context = ApplicationProvider.getApplicationContext<Context>()
val logger = ConsoleLogger()

val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)

var destinationPreviousSessionId = destination.read(Storage.Constants.PREVIOUS_SESSION_ID)
var destinationLastEventTime = destination.read(Storage.Constants.LAST_EVENT_TIME)
Expand Down Expand Up @@ -150,8 +153,8 @@ class StorageKeyMigrationTest {
val context = ApplicationProvider.getApplicationContext<Context>()
val logger = ConsoleLogger()

val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null)
val source = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)
val destination = AndroidStorage(context, UUID.randomUUID().toString(), logger, null, testDiagnostics)

runBlocking {
source.writeEvent(createEvent(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.amplitude.android.events.BaseEvent
import com.amplitude.common.jvm.ConsoleLogger
import com.amplitude.core.utilities.Diagnostics
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.json.JSONArray
Expand All @@ -16,12 +17,13 @@ import java.io.File
@RunWith(RobolectricTestRunner::class)
class AndroidStorageTest {
private val context = ApplicationProvider.getApplicationContext<Context>()
private val testDiagnostics = Diagnostics()

@Test
fun `test write event and read`() {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val storage = AndroidStorage(context, storageKey, logger, "test")
val storage = AndroidStorage(context, storageKey, logger, "test", testDiagnostics)

runBlocking {
storage.writeEvent(createEvent("test1"))
Expand Down Expand Up @@ -50,7 +52,7 @@ class AndroidStorageTest {
val storageKey = "storageKey"
val prefix = "test"
createEarlierVersionEventFiles(prefix)
val storage = AndroidStorage(context, storageKey, logger, prefix)
val storage = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)

runBlocking {
val eventsData = storage.readEventsContent()
Expand Down Expand Up @@ -81,7 +83,7 @@ class AndroidStorageTest {
val storageKey = "storageKey"
val prefix = "test"
createEarlierVersionEventFiles(prefix)
val storage = AndroidStorage(context, storageKey, logger, prefix)
val storage = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)

runBlocking {
storage.writeEvent(createEvent("test13"))
Expand Down Expand Up @@ -109,7 +111,8 @@ class AndroidStorageTest {
val storageDirectory = context.getDir("$prefix-disk-queue", Context.MODE_PRIVATE)
val file0 = File(storageDirectory, "storageKey-0")
file0.writeText("{\"eventType\":\"test1\"}\n{\"eventType\":\"test2\"}\n{\"eventType\":\"test3\"\n")
val storage = AndroidStorage(context, storageKey, logger, prefix)
val diagnostics = Diagnostics()
val storage = AndroidStorage(context, storageKey, logger, prefix, diagnostics)
runBlocking {
var eventsCount = 0
val eventsData = storage.readEventsContent()
Expand All @@ -119,16 +122,15 @@ class AndroidStorageTest {
eventsCount += events.length()
}
assertEquals(2, eventsCount)
val diagnostics = storage.getDiagnostics()
assertEquals("{\"malformed_events\":[\"{\\\"eventType\\\":\\\"test3\\\"\"]}", diagnostics)
assertEquals("{\"malformed_events\":[\"{\\\"eventType\\\":\\\"test3\\\"\"]}", diagnostics.extractDiagnostics())
}
}

@Test
fun `concurrent writes to the same instance`() {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val storage = AndroidStorage(context, storageKey, logger, "test")
val storage = AndroidStorage(context, storageKey, logger, "test", testDiagnostics)

runBlocking {
val job1 =
Expand Down Expand Up @@ -168,7 +170,7 @@ class AndroidStorageTest {
fun `concurrent writes from multiple threads`() {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val storage = AndroidStorage(context, storageKey, logger, "test")
val storage = AndroidStorage(context, storageKey, logger, "test", testDiagnostics)
for (i in 0..100) {
val currentThread =
Thread {
Expand Down Expand Up @@ -199,8 +201,8 @@ class AndroidStorageTest {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val prefix = "test"
val storage1 = AndroidStorage(context, storageKey, logger, prefix)
val storage2 = AndroidStorage(context, storageKey, logger, prefix)
val storage1 = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)
val storage2 = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)

runBlocking {
val job1 =
Expand Down Expand Up @@ -248,7 +250,7 @@ class AndroidStorageTest {
val storageKey = "storageKey"
val prefix = "test"
for (i in 0..100) {
val storage = AndroidStorage(context, storageKey, logger, prefix)
val storage = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)
val currentThread =
Thread {
runBlocking {
Expand All @@ -262,7 +264,7 @@ class AndroidStorageTest {
currentThread.join()
}
var eventsCount = 0
val storageForRead = AndroidStorage(context, storageKey, logger, prefix)
val storageForRead = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)
runBlocking {
val eventsData = storageForRead.readEventsContent()
eventsData.withIndex().forEach { (_, filePath) ->
Expand Down
Loading

0 comments on commit c267552

Please sign in to comment.