Skip to content

Commit

Permalink
Activity Open trace logging and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Aug 21, 2024
1 parent 832cf5a commit 5f29880
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 14 deletions.
19 changes: 19 additions & 0 deletions embrace-android-api/api/embrace-android-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ public abstract interface class io/embrace/android/embracesdk/internal/api/UserA
public abstract fun setUsername (Ljava/lang/String;)V
}

public abstract interface class io/embrace/android/embracesdk/internal/capture/activity/OpenEvents {
public abstract fun create (ILjava/lang/String;J)V
public abstract fun createEnd (IJ)V
public abstract fun render (ILjava/lang/String;J)V
public abstract fun renderEnd (IJ)V
public abstract fun resume (ILjava/lang/String;J)V
public abstract fun resumeEnd (IJ)V
public abstract fun start (ILjava/lang/String;J)V
public abstract fun startEnd (IJ)V
}

public final class io/embrace/android/embracesdk/internal/capture/activity/OpenEvents$OpenType : java/lang/Enum {
public static final field COLD Lio/embrace/android/embracesdk/internal/capture/activity/OpenEvents$OpenType;
public static final field HOT Lio/embrace/android/embracesdk/internal/capture/activity/OpenEvents$OpenType;
public final fun getTypeName ()Ljava/lang/String;
public static fun valueOf (Ljava/lang/String;)Lio/embrace/android/embracesdk/internal/capture/activity/OpenEvents$OpenType;
public static fun values ()[Lio/embrace/android/embracesdk/internal/capture/activity/OpenEvents$OpenType;
}

public final class io/embrace/android/embracesdk/internal/network/http/NetworkCaptureData {
public fun <init> (Ljava/util/Map;Ljava/lang/String;[BLjava/util/Map;[BLjava/lang/String;)V
public synthetic fun <init> (Ljava/util/Map;Ljava/lang/String;[BLjava/util/Map;[BLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.embrace.android.embracesdk.internal.capture.activity

public interface OpenEvents {

public fun create(instanceId: Int, activityName: String, timestampMs: Long)

public fun createEnd(instanceId: Int, timestampMs: Long)

public fun start(instanceId: Int, activityName: String, timestampMs: Long)

public fun startEnd(instanceId: Int, timestampMs: Long)

public fun resume(instanceId: Int, activityName: String, timestampMs: Long)

public fun resumeEnd(instanceId: Int, timestampMs: Long)

public fun render(instanceId: Int, activityName: String, timestampMs: Long)

public fun renderEnd(instanceId: Int, timestampMs: Long)

public enum class OpenType(public val typeName: String) {
COLD("cold"), HOT("hot")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ActivityLifecycleTracker(
/**
* List of listeners that subscribe to activity events.
*/
public val listeners: CopyOnWriteArrayList<ActivityLifecycleListener> =
public val activityListeners: CopyOnWriteArrayList<ActivityLifecycleListener> =
CopyOnWriteArrayList<ActivityLifecycleListener>()

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ public class ActivityLifecycleTracker(

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
updateStateWithActivity(activity)
stream(listeners) { listener: ActivityLifecycleListener ->
stream(activityListeners) { listener: ActivityLifecycleListener ->
try {
listener.onActivityCreated(activity, bundle)
} catch (ex: Exception) {
Expand All @@ -76,7 +76,7 @@ public class ActivityLifecycleTracker(

override fun onActivityStarted(activity: Activity) {
updateStateWithActivity(activity)
stream(listeners) { listener: ActivityLifecycleListener ->
stream(activityListeners) { listener: ActivityLifecycleListener ->
try {
listener.onActivityStarted(activity)
} catch (ex: Exception) {
Expand All @@ -103,7 +103,7 @@ public class ActivityLifecycleTracker(

override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {
stream(listeners) { listener: ActivityLifecycleListener ->
stream(activityListeners) { listener: ActivityLifecycleListener ->
try {
listener.onActivityStopped(activity)
} catch (ex: Exception) {
Expand All @@ -118,8 +118,8 @@ public class ActivityLifecycleTracker(
override fun onActivityDestroyed(activity: Activity) {}

override fun addListener(listener: ActivityLifecycleListener) {
if (!listeners.contains(listener)) {
listeners.addIfAbsent(listener)
if (!activityListeners.contains(listener)) {
activityListeners.addIfAbsent(listener)
}
}

Expand All @@ -133,7 +133,7 @@ public class ActivityLifecycleTracker(
try {
logger.logDebug("Shutting down ActivityLifecycleTracker")
application.unregisterActivityLifecycleCallbacks(this)
listeners.clear()
activityListeners.clear()
startupListeners.clear()
} catch (ex: Exception) {
logger.logWarning("Error when closing ActivityLifecycleTracker", ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ internal class ActivityLifecycleTrackerTest {
@Test
fun `verify a listener is added`() {
// assert empty list first
assertEquals(0, activityLifecycleTracker.listeners.size)
assertEquals(0, activityLifecycleTracker.activityListeners.size)

val mockActivityLifecycleListener = mockk<ActivityLifecycleListener>()
activityLifecycleTracker.addListener(mockActivityLifecycleListener)

assertEquals(1, activityLifecycleTracker.listeners.size)
assertEquals(1, activityLifecycleTracker.activityListeners.size)
}

@Test
Expand All @@ -209,7 +209,7 @@ internal class ActivityLifecycleTrackerTest {
// add it for a 2nd time
activityLifecycleTracker.addListener(mockActivityLifecycleListener)

assertEquals(1, activityLifecycleTracker.listeners.size)
assertEquals(1, activityLifecycleTracker.activityListeners.size)
}

@Test
Expand All @@ -220,8 +220,8 @@ internal class ActivityLifecycleTrackerTest {

activityLifecycleTracker.addListener(mockActivityLifecycleListener2)

assertEquals(2, activityLifecycleTracker.listeners.size)
assertEquals(mockActivityLifecycleListener2, activityLifecycleTracker.listeners[1])
assertEquals(2, activityLifecycleTracker.activityListeners.size)
assertEquals(mockActivityLifecycleListener2, activityLifecycleTracker.activityListeners[1])
}

@Test
Expand All @@ -237,7 +237,7 @@ internal class ActivityLifecycleTrackerTest {
activityLifecycleTracker.close()

verify { application.unregisterActivityLifecycleCallbacks(activityLifecycleTracker) }
assertTrue(activityLifecycleTracker.listeners.isEmpty())
assertTrue(activityLifecycleTracker.activityListeners.isEmpty())
assertTrue(activityLifecycleTracker.startupListeners.isEmpty())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.embrace.android.embracesdk.internal.capture.activity

import android.app.Activity
import android.os.Build
import android.os.Bundle
import io.embrace.android.embracesdk.internal.clock.nanosToMillis
import io.embrace.android.embracesdk.internal.session.lifecycle.ActivityLifecycleListener
import io.embrace.android.embracesdk.internal.utils.VersionChecker
import io.opentelemetry.sdk.common.Clock

internal class OpenEventEmitter(
private val openEvents: OpenEvents,
private val clock: Clock,
private val versionChecker: VersionChecker,

Check warning on line 14 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L11-L14

Added lines #L11 - L14 were not covered by tests
) : ActivityLifecycleListener {
override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
create(activity)

Check warning on line 17 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L17

Added line #L17 was not covered by tests
}

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
if (!versionChecker.firePrePostEvents()) {
create(activity)

Check warning on line 22 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L22

Added line #L22 was not covered by tests
}
}

override fun onActivityPostCreated(activity: Activity, savedInstanceState: Bundle?) {
createEnd(activity)

Check warning on line 27 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L27

Added line #L27 was not covered by tests
}

override fun onActivityPreStarted(activity: Activity) {
start(activity)

Check warning on line 31 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L31

Added line #L31 was not covered by tests
}

override fun onActivityStarted(activity: Activity) {
if (!versionChecker.firePrePostEvents()) {
createEnd(activity)
start(activity)

Check warning on line 37 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L36-L37

Added lines #L36 - L37 were not covered by tests
}
}

override fun onActivityPostStarted(activity: Activity) {
startEnd(activity)

Check warning on line 42 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L42

Added line #L42 was not covered by tests
}

override fun onActivityPreResumed(activity: Activity) {
resume(activity)

Check warning on line 46 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L46

Added line #L46 was not covered by tests
}

override fun onActivityResumed(activity: Activity) {
if (!versionChecker.firePrePostEvents()) {
startEnd(activity)
resumeEnd(activity)

Check warning on line 52 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L51-L52

Added lines #L51 - L52 were not covered by tests
}
}

override fun onActivityPostResumed(activity: Activity) {
resumeEnd(activity)

Check warning on line 57 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L57

Added line #L57 was not covered by tests
}

private fun create(activity: Activity) {
openEvents.create(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()

Check warning on line 64 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L61-L64

Added lines #L61 - L64 were not covered by tests
)
}

private fun createEnd(activity: Activity) {
openEvents.createEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()

Check warning on line 71 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L69-L71

Added lines #L69 - L71 were not covered by tests
)
}

private fun start(activity: Activity) {
openEvents.start(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()

Check warning on line 79 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L76-L79

Added lines #L76 - L79 were not covered by tests
)
}

private fun startEnd(activity: Activity) {
openEvents.startEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()

Check warning on line 86 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L84-L86

Added lines #L84 - L86 were not covered by tests
)
}

private fun resume(activity: Activity) {
openEvents.resume(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()

Check warning on line 94 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L91-L94

Added lines #L91 - L94 were not covered by tests
)
}

private fun resumeEnd(activity: Activity) {
openEvents.resumeEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()

Check warning on line 101 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L99-L101

Added lines #L99 - L101 were not covered by tests
)
}

private fun VersionChecker.firePrePostEvents(): Boolean = isAtLeast(Build.VERSION_CODES.Q)

Check warning on line 105 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L105

Added line #L105 was not covered by tests

private fun traceInstanceId(activity: Activity): Int = activity.hashCode()

Check warning on line 107 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L107

Added line #L107 was not covered by tests

private fun nowMs(): Long = clock.now().nanosToMillis()

Check warning on line 109 in embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-features/src/main/kotlin/io/embrace/android/embracesdk/internal/capture/activity/OpenEventEmitter.kt#L109

Added line #L109 was not covered by tests
}
Loading

0 comments on commit 5f29880

Please sign in to comment.