Skip to content

Commit

Permalink
Record internal error when startup trace fails to be recorded
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Oct 28, 2024
1 parent 5444f9a commit 0ea9dbd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build.VERSION_CODES
import android.os.Process
import io.embrace.android.embracesdk.internal.clock.nanosToMillis
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.InternalErrorType
import io.embrace.android.embracesdk.internal.spans.PersistableEmbraceSpan
import io.embrace.android.embracesdk.internal.spans.SpanService
import io.embrace.android.embracesdk.internal.utils.Provider
Expand Down Expand Up @@ -97,6 +98,7 @@ internal class AppStartupTraceEmitter(
private var sdkInitEndedInForeground: Boolean? = null

private val startupRecorded = AtomicBoolean(false)
private val dataCollectionComplete = AtomicBoolean(false)
private val endWithFrameDraw: Boolean = versionChecker.isAtLeast(VERSION_CODES.Q)

override fun applicationInitStart(timestampMs: Long?) {
Expand Down Expand Up @@ -161,16 +163,20 @@ internal class AppStartupTraceEmitter(
* Called when app startup is considered complete, i.e. the data can be used and any additional updates can be ignored
*/
private fun dataCollectionComplete(callback: () -> Unit) {
if (!startupRecorded.get()) {
synchronized(startupRecorded) {
if (!startupRecorded.get()) {
if (!dataCollectionComplete.get()) {
synchronized(dataCollectionComplete) {
if (!dataCollectionComplete.get()) {
backgroundWorker.submit {
recordStartup()
if (!startupRecorded.get()) {
logger.logWarning("App startup trace recording attempted but did not succeed")
logger.trackInternalError(
type = InternalErrorType.APP_STARTUP_TRACE_NOT_RECORDED,
throwable = IllegalStateException("App startup trace not recorded after terminal app startup event")
)
}
}
callback()
dataCollectionComplete.set(true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class StartupTracker(
private var isFirstDraw = false
private var nullWindowCallbackErrorLogged = false
private var startupActivityId: Int? = null
private var startupDataCollectionComplete = false

override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
if (activity.useAsStartupActivity()) {
Expand All @@ -53,6 +54,7 @@ class StartupTracker(
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
if (activity.useAsStartupActivity()) {
val activityName = activity.localClassName
val application = activity.application
appStartupDataCollector.startupActivityInitStart()
if (versionChecker.isAtLeast(Build.VERSION_CODES.Q)) {
if (!isFirstDraw) {
Expand All @@ -66,7 +68,7 @@ class StartupTracker(
val callback = {
appStartupDataCollector.firstFrameRendered(
activityName = activityName,
collectionCompleteCallback = {}
collectionCompleteCallback = { startupComplete(application) }
)
}
decorView.viewTreeObserver.registerFrameCommitCallback(callback)
Expand Down Expand Up @@ -96,9 +98,10 @@ class StartupTracker(

override fun onActivityResumed(activity: Activity) {
if (activity.observeForStartup()) {
val application = activity.application
appStartupDataCollector.startupActivityResumed(
activityName = activity.localClassName,
collectionCompleteCallback = { }
collectionCompleteCallback = { startupComplete(application) }
)
}
}
Expand All @@ -111,6 +114,13 @@ class StartupTracker(

override fun onActivityDestroyed(activity: Activity) {}

private fun startupComplete(application: Application) {
if (!startupDataCollectionComplete) {
application.unregisterActivityLifecycleCallbacks(this)
startupDataCollectionComplete = true
}
}

/**
* Returns true if the Activity instance is being used as the startup Activity. It will return false if [useAsStartupActivity] has
* not been called previously to setup the Activity instance to be used as the startup Activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum class InternalErrorType {
PROCESS_STATE_SUMMARY_FAIL,
ANR_HEARTBEAT_STOP_FAIL,
SDK_START_FAIL,
APP_STARTUP_TRACE_NOT_RECORDED,
DELIVERY_SCHEDULING_FAIL,
PAYLOAD_DELIVERY_FAIL,
PAYLOAD_RESURRECTION_FAIL,
Expand Down

0 comments on commit 0ea9dbd

Please sign in to comment.