-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removing fragment lifecycle callbacks from fragment instrumentation * Adding FragmentLifecycleInstrumentation * Removing lifecycle module * Updating tests * Reusing lifecycle instrumentation scope
- Loading branch information
1 parent
be5d084
commit e22ce45
Showing
10 changed files
with
137 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...telemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTrackerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.internal.services.visiblescreen.fragments | ||
|
||
import androidx.fragment.app.Fragment | ||
import io.mockk.Runs | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class VisibleFragmentTrackerTest { | ||
private lateinit var service: VisibleScreenService | ||
private lateinit var fragment: Fragment | ||
private lateinit var visibleFragmentTracker: VisibleFragmentTracker | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
service = mockk() | ||
fragment = mockk() | ||
visibleFragmentTracker = VisibleFragmentTracker(service) | ||
} | ||
|
||
@Test | ||
fun `Track fragment resumed`() { | ||
every { service.fragmentResumed(any()) } just Runs | ||
|
||
visibleFragmentTracker.onFragmentResumed(mockk(), fragment) | ||
|
||
verify { | ||
service.fragmentResumed(fragment) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Track fragment paused`() { | ||
every { service.fragmentPaused(any()) } just Runs | ||
|
||
visibleFragmentTracker.onFragmentPaused(mockk(), fragment) | ||
|
||
verify { | ||
service.fragmentPaused(fragment) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ion/common-api/src/main/java/io/opentelemetry/android/instrumentation/common/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.instrumentation.common | ||
|
||
object Constants { | ||
const val INSTRUMENTATION_SCOPE = "io.opentelemetry.lifecycle" | ||
} |
53 changes: 53 additions & 0 deletions
53
...ava/io/opentelemetry/android/instrumentation/fragment/FragmentLifecycleInstrumentation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.instrumentation.fragment | ||
|
||
import android.app.Application | ||
import android.app.Application.ActivityLifecycleCallbacks | ||
import android.os.Build | ||
import io.opentelemetry.android.OpenTelemetryRum | ||
import io.opentelemetry.android.instrumentation.AndroidInstrumentation | ||
import io.opentelemetry.android.instrumentation.common.Constants.INSTRUMENTATION_SCOPE | ||
import io.opentelemetry.android.instrumentation.common.ScreenNameExtractor | ||
import io.opentelemetry.android.internal.services.ServiceManager | ||
import io.opentelemetry.android.internal.services.visiblescreen.fragments.RumFragmentActivityRegisterer | ||
import io.opentelemetry.api.trace.Tracer | ||
|
||
class FragmentLifecycleInstrumentation : AndroidInstrumentation { | ||
private var screenNameExtractor = ScreenNameExtractor.DEFAULT | ||
private var tracerCustomizer: (Tracer) -> Tracer = { it } | ||
|
||
fun setTracerCustomizer(customizer: (Tracer) -> Tracer) { | ||
tracerCustomizer = customizer | ||
} | ||
|
||
fun setScreenNameExtractor(screenNameExtractor: ScreenNameExtractor) { | ||
this.screenNameExtractor = screenNameExtractor | ||
} | ||
|
||
override fun install( | ||
application: Application, | ||
openTelemetryRum: OpenTelemetryRum, | ||
) { | ||
application.registerActivityLifecycleCallbacks(buildFragmentRegisterer(openTelemetryRum)) | ||
} | ||
|
||
private fun buildFragmentRegisterer(openTelemetryRum: OpenTelemetryRum): ActivityLifecycleCallbacks { | ||
val visibleScreenService = ServiceManager.get().getVisibleScreenService() | ||
val delegateTracer: Tracer = openTelemetryRum.openTelemetry.getTracer(INSTRUMENTATION_SCOPE) | ||
val fragmentLifecycle = | ||
RumFragmentLifecycleCallbacks( | ||
tracerCustomizer.invoke(delegateTracer), | ||
visibleScreenService::previouslyVisibleScreen, | ||
screenNameExtractor, | ||
) | ||
return if (Build.VERSION.SDK_INT < 29) { | ||
RumFragmentActivityRegisterer.createPre29(fragmentLifecycle) | ||
} else { | ||
RumFragmentActivityRegisterer.create(fragmentLifecycle) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.