From e22ce458d9637cd8eff6552922d1fe3fb955de90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= <56847527+LikeTheSalad@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:49:12 +0200 Subject: [PATCH] Instrumentation API part 7 (#465) * Removing fragment lifecycle callbacks from fragment instrumentation * Adding FragmentLifecycleInstrumentation * Removing lifecycle module * Updating tests * Reusing lifecycle instrumentation scope --- .../fragments/VisibleFragmentTracker.kt | 1 - .../fragments/VisibleFragmentTrackerTest.kt | 51 ++++++++ .../ActivityLifecycleInstrumentation.kt | 5 +- .../instrumentation/common/Constants.kt | 10 ++ .../FragmentLifecycleInstrumentation.kt | 53 ++++++++ .../RumFragmentLifecycleCallbacks.java | 14 +-- .../RumFragmentLifecycleCallbacksTest.java | 49 +++----- instrumentation/lifecycle/build.gradle.kts | 27 ---- .../AndroidLifecycleInstrumentation.java | 119 ------------------ ...ndroidLifecycleInstrumentationBuilder.java | 54 -------- 10 files changed, 137 insertions(+), 246 deletions(-) create mode 100644 android-agent/src/test/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTrackerTest.kt create mode 100644 instrumentation/common-api/src/main/java/io/opentelemetry/android/instrumentation/common/Constants.kt create mode 100644 instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/FragmentLifecycleInstrumentation.kt delete mode 100644 instrumentation/lifecycle/build.gradle.kts delete mode 100644 instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentation.java delete mode 100644 instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentationBuilder.java diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTracker.kt b/android-agent/src/main/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTracker.kt index 84e12e531..21dc01407 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTracker.kt +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTracker.kt @@ -22,7 +22,6 @@ class VisibleFragmentTracker(private val visibleScreenService: VisibleScreenServ fm: FragmentManager, f: Fragment, ) { - super.onFragmentPaused(fm, f) visibleScreenService.fragmentPaused(f) } } diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTrackerTest.kt b/android-agent/src/test/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTrackerTest.kt new file mode 100644 index 000000000..bc81a8f74 --- /dev/null +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/visiblescreen/fragments/VisibleFragmentTrackerTest.kt @@ -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) + } + } +} diff --git a/instrumentation/activity/src/main/java/io/opentelemetry/android/instrumentation/activity/ActivityLifecycleInstrumentation.kt b/instrumentation/activity/src/main/java/io/opentelemetry/android/instrumentation/activity/ActivityLifecycleInstrumentation.kt index 9463bf00c..154927a04 100644 --- a/instrumentation/activity/src/main/java/io/opentelemetry/android/instrumentation/activity/ActivityLifecycleInstrumentation.kt +++ b/instrumentation/activity/src/main/java/io/opentelemetry/android/instrumentation/activity/ActivityLifecycleInstrumentation.kt @@ -10,6 +10,7 @@ import android.os.Build import io.opentelemetry.android.OpenTelemetryRum import io.opentelemetry.android.instrumentation.AndroidInstrumentation import io.opentelemetry.android.instrumentation.activity.startup.AppStartupTimer +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.activities.DefaultingActivityLifecycleCallbacks @@ -20,10 +21,6 @@ class ActivityLifecycleInstrumentation : AndroidInstrumentation { private var screenNameExtractor: ScreenNameExtractor = ScreenNameExtractor.DEFAULT private var tracerCustomizer: (Tracer) -> Tracer = { it } - companion object { - private const val INSTRUMENTATION_SCOPE: String = "io.opentelemetry.lifecycle" - } - fun setTracerCustomizer(customizer: (Tracer) -> Tracer) { tracerCustomizer = customizer } diff --git a/instrumentation/common-api/src/main/java/io/opentelemetry/android/instrumentation/common/Constants.kt b/instrumentation/common-api/src/main/java/io/opentelemetry/android/instrumentation/common/Constants.kt new file mode 100644 index 000000000..5caea08fd --- /dev/null +++ b/instrumentation/common-api/src/main/java/io/opentelemetry/android/instrumentation/common/Constants.kt @@ -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" +} diff --git a/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/FragmentLifecycleInstrumentation.kt b/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/FragmentLifecycleInstrumentation.kt new file mode 100644 index 000000000..2eeb0ad75 --- /dev/null +++ b/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/FragmentLifecycleInstrumentation.kt @@ -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) + } + } +} diff --git a/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacks.java b/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacks.java index 81d54d166..7d4b8e3bd 100644 --- a/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacks.java +++ b/instrumentation/fragment/src/main/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacks.java @@ -14,24 +14,24 @@ import androidx.fragment.app.FragmentManager; import io.opentelemetry.android.common.ActiveSpan; import io.opentelemetry.android.instrumentation.common.ScreenNameExtractor; -import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService; import io.opentelemetry.api.trace.Tracer; import java.util.HashMap; import java.util.Map; +import java.util.function.Supplier; public class RumFragmentLifecycleCallbacks extends FragmentManager.FragmentLifecycleCallbacks { private final Map tracersByFragmentClassName = new HashMap<>(); private final Tracer tracer; - private final VisibleScreenService visibleScreenService; + private final Supplier lastVisibleScreen; private final ScreenNameExtractor screenNameExtractor; public RumFragmentLifecycleCallbacks( Tracer tracer, - VisibleScreenService visibleScreenService, + Supplier lastVisibleScreen, ScreenNameExtractor screenNameExtractor) { this.tracer = tracer; - this.visibleScreenService = visibleScreenService; + this.lastVisibleScreen = lastVisibleScreen; this.screenNameExtractor = screenNameExtractor; } @@ -87,13 +87,11 @@ public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) .addEvent("fragmentResumed") .addPreviousScreenAttribute() .endActiveSpan(); - visibleScreenService.fragmentResumed(f); } @Override public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) { super.onFragmentPaused(fm, f); - visibleScreenService.fragmentPaused(f); getTracer(f).startSpanIfNoneInProgress("Paused").addEvent("fragmentPaused"); } @@ -152,9 +150,7 @@ private FragmentTracer getTracer(Fragment fragment) { FragmentTracer.builder(fragment) .setTracer(tracer) .setScreenName(screenNameExtractor.extract(fragment)) - .setActiveSpan( - new ActiveSpan( - visibleScreenService::getPreviouslyVisibleScreen)) + .setActiveSpan(new ActiveSpan(lastVisibleScreen)) .build(); tracersByFragmentClassName.put(fragment.getClass().getName(), activityTracer); } diff --git a/instrumentation/fragment/src/test/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacksTest.java b/instrumentation/fragment/src/test/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacksTest.java index c28bc68c2..ea2bd3605 100644 --- a/instrumentation/fragment/src/test/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacksTest.java +++ b/instrumentation/fragment/src/test/java/io/opentelemetry/android/instrumentation/fragment/RumFragmentLifecycleCallbacksTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import io.opentelemetry.android.instrumentation.common.ScreenNameExtractor; import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService; @@ -48,10 +49,7 @@ void setup() { @Test void fragmentCreation() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentCreationLifecycle(fragment); @@ -83,10 +81,7 @@ void fragmentCreation() { @Test void fragmentRestored() { when(visibleScreenService.getPreviouslyVisibleScreen()).thenReturn("previousScreen"); - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentRestoredLifecycle(fragment); @@ -113,10 +108,7 @@ void fragmentRestored() { @Test void fragmentResumed() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentResumedLifecycle(fragment); @@ -139,10 +131,7 @@ void fragmentResumed() { @Test void fragmentPaused() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentPausedLifecycle(fragment); @@ -168,10 +157,7 @@ void fragmentPaused() { @Test void fragmentDetachedFromActive() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentDetachedFromActiveLifecycle(fragment); @@ -224,10 +210,7 @@ void fragmentDetachedFromActive() { @Test void fragmentDestroyedFromStopped() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentViewDestroyedFromStoppedLifecycle(fragment); @@ -252,10 +235,7 @@ void fragmentDestroyedFromStopped() { @Test void fragmentDetachedFromStopped() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentDetachedFromStoppedLifecycle(fragment); @@ -294,10 +274,7 @@ void fragmentDetachedFromStopped() { @Test void fragmentDetached() { - FragmentCallbackTestHarness testHarness = - new FragmentCallbackTestHarness( - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenService, screenNameExtractor)); + FragmentCallbackTestHarness testHarness = getFragmentCallbackTestHarness(); Fragment fragment = mock(Fragment.class); testHarness.runFragmentDetachedLifecycle(fragment); @@ -326,4 +303,12 @@ private void checkEventExists(List events, String eventName) { events.stream().filter(e -> e.getName().equals(eventName)).findAny(); assertTrue(event.isPresent(), "Event with name " + eventName + " not found"); } + + private @NonNull FragmentCallbackTestHarness getFragmentCallbackTestHarness() { + return new FragmentCallbackTestHarness( + new RumFragmentLifecycleCallbacks( + tracer, + visibleScreenService::getPreviouslyVisibleScreen, + screenNameExtractor)); + } } diff --git a/instrumentation/lifecycle/build.gradle.kts b/instrumentation/lifecycle/build.gradle.kts deleted file mode 100644 index e9400d861..000000000 --- a/instrumentation/lifecycle/build.gradle.kts +++ /dev/null @@ -1,27 +0,0 @@ -plugins { - id("otel.android-library-conventions") - id("otel.publish-conventions") -} - -description = "OpenTelemetry Android ANR instrumentation" - -android { - namespace = "io.opentelemetry.android.instrumentation.lifecycle" - - defaultConfig { - consumerProguardFiles("consumer-rules.pro") - } -} - -dependencies { - api(platform(libs.opentelemetry.platform)) - api(project(":instrumentation:activity")) // TODO: Should lifecycle/fragment/activity share a root? - api(project(":instrumentation:common-api")) - api(project(":instrumentation:fragment")) // TODO: Should lifecycle/fragment/activity share a root? - api(libs.androidx.navigation.fragment) - api(libs.opentelemetry.api) - implementation(libs.androidx.core) - implementation(libs.opentelemetry.semconv) - implementation(libs.opentelemetry.sdk) - implementation(libs.opentelemetry.instrumentation.api) -} diff --git a/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentation.java b/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentation.java deleted file mode 100644 index bf41e4911..000000000 --- a/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentation.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.android.instrumentation.lifecycle; - -import android.app.Application; -import android.os.Build; -import androidx.annotation.NonNull; -import io.opentelemetry.android.instrumentation.activity.ActivityCallbacks; -import io.opentelemetry.android.instrumentation.activity.ActivityTracerCache; -import io.opentelemetry.android.instrumentation.activity.Pre29ActivityCallbacks; -import io.opentelemetry.android.instrumentation.activity.Pre29VisibleScreenLifecycleBinding; -import io.opentelemetry.android.instrumentation.activity.RumFragmentActivityRegisterer; -import io.opentelemetry.android.instrumentation.activity.VisibleScreenLifecycleBinding; -import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker; -import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; -import io.opentelemetry.android.instrumentation.common.ScreenNameExtractor; -import io.opentelemetry.android.instrumentation.fragment.RumFragmentLifecycleCallbacks; -import io.opentelemetry.android.instrumentation.activity.startup.AppStartupTimer; -import io.opentelemetry.api.trace.Tracer; -import java.util.function.Function; - -/** - * This is an umbrella instrumentation that covers several things: * startup timer callback is - * registered so that UI startup time can be measured - activity lifecycle callbacks are registered - * so that lifecycle events can be generated - activity lifecycle callback listener is registered to - * that will register a FragmentLifecycleCallbacks when appropriate - activity lifecycle callback - * listener is registered to dispatch events to the VisibleScreenTracker - */ -public class AndroidLifecycleInstrumentation { - - private static final String INSTRUMENTATION_SCOPE = "io.opentelemetry.lifecycle"; - private final AppStartupTimer startupTimer; - private final VisibleScreenTracker visibleScreenTracker; - - private final Function tracerCustomizer; - private final ScreenNameExtractor screenNameExtractor; - - AndroidLifecycleInstrumentation(AndroidLifecycleInstrumentationBuilder builder) { - this.startupTimer = builder.startupTimer; - this.visibleScreenTracker = builder.visibleScreenTracker; - this.tracerCustomizer = builder.tracerCustomizer; - this.screenNameExtractor = builder.screenNameExtractor; - } - - public static AndroidLifecycleInstrumentationBuilder builder() { - return new AndroidLifecycleInstrumentationBuilder(); - } - - public void installOn(InstrumentedApplication app) { - installStartupTimerInstrumentation(app); - installActivityLifecycleEventsInstrumentation(app); - installFragmentLifecycleInstrumentation(app); - installScreenTrackingInstrumentation(app); - } - - private void installStartupTimerInstrumentation(InstrumentedApplication app) { - app.getApplication() - .registerActivityLifecycleCallbacks(startupTimer.createLifecycleCallback()); - } - - private void installActivityLifecycleEventsInstrumentation(InstrumentedApplication app) { - Application.ActivityLifecycleCallbacks activityCallbacks = buildActivityEventsCallback(app); - app.getApplication().registerActivityLifecycleCallbacks(activityCallbacks); - } - - @NonNull - private Application.ActivityLifecycleCallbacks buildActivityEventsCallback( - InstrumentedApplication instrumentedApp) { - Tracer delegateTracer = - instrumentedApp.getOpenTelemetrySdk().getTracer(INSTRUMENTATION_SCOPE); - Tracer tracer = tracerCustomizer.apply(delegateTracer); - - ActivityTracerCache tracers = - new ActivityTracerCache( - tracer, visibleScreenTracker, startupTimer, screenNameExtractor); - if (Build.VERSION.SDK_INT < 29) { - return new Pre29ActivityCallbacks(tracers); - } - return new ActivityCallbacks(tracers); - } - - private void installFragmentLifecycleInstrumentation(InstrumentedApplication app) { - Application.ActivityLifecycleCallbacks fragmentRegisterer = buildFragmentRegisterer(app); - app.getApplication().registerActivityLifecycleCallbacks(fragmentRegisterer); - } - - @NonNull - private Application.ActivityLifecycleCallbacks buildFragmentRegisterer( - InstrumentedApplication app) { - - Tracer delegateTracer = app.getOpenTelemetrySdk().getTracer(INSTRUMENTATION_SCOPE); - Tracer tracer = tracerCustomizer.apply(delegateTracer); - RumFragmentLifecycleCallbacks fragmentLifecycle = - new RumFragmentLifecycleCallbacks( - tracer, visibleScreenTracker, screenNameExtractor); - if (Build.VERSION.SDK_INT < 29) { - return RumFragmentActivityRegisterer.createPre29(fragmentLifecycle); - } - return RumFragmentActivityRegisterer.create(fragmentLifecycle); - } - - private void installScreenTrackingInstrumentation(InstrumentedApplication app) { - Application.ActivityLifecycleCallbacks screenTrackingBinding = - buildScreenTrackingBinding(visibleScreenTracker); - app.getApplication().registerActivityLifecycleCallbacks(screenTrackingBinding); - } - - @NonNull - private Application.ActivityLifecycleCallbacks buildScreenTrackingBinding( - VisibleScreenTracker visibleScreenTracker) { - if (Build.VERSION.SDK_INT < 29) { - return new Pre29VisibleScreenLifecycleBinding(visibleScreenTracker); - } - return new VisibleScreenLifecycleBinding(visibleScreenTracker); - } -} diff --git a/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentationBuilder.java b/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentationBuilder.java deleted file mode 100644 index dd7382863..000000000 --- a/instrumentation/lifecycle/src/main/java/io/opentelemetry/android/instrumentation/lifecycle/AndroidLifecycleInstrumentationBuilder.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.android.instrumentation.lifecycle; - -import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker; -import io.opentelemetry.android.instrumentation.common.ScreenNameExtractor; -import io.opentelemetry.android.instrumentation.activity.startup.AppStartupTimer; -import io.opentelemetry.api.trace.Tracer; -import java.util.function.Function; - -public class AndroidLifecycleInstrumentationBuilder { - private static final VisibleScreenTracker INVALID_SCREEN_TRACKER = new VisibleScreenTracker(); - private static final AppStartupTimer INVALID_TIMER = new AppStartupTimer(); - ScreenNameExtractor screenNameExtractor = ScreenNameExtractor.DEFAULT; - AppStartupTimer startupTimer = INVALID_TIMER; - VisibleScreenTracker visibleScreenTracker = INVALID_SCREEN_TRACKER; - Function tracerCustomizer = Function.identity(); - - public AndroidLifecycleInstrumentationBuilder setStartupTimer(AppStartupTimer timer) { - this.startupTimer = timer; - return this; - } - - public AndroidLifecycleInstrumentationBuilder setVisibleScreenTracker( - VisibleScreenTracker tracker) { - this.visibleScreenTracker = tracker; - return this; - } - - public AndroidLifecycleInstrumentationBuilder setTracerCustomizer( - Function customizer) { - this.tracerCustomizer = customizer; - return this; - } - - public AndroidLifecycleInstrumentationBuilder setScreenNameExtractor( - ScreenNameExtractor screenNameExtractor) { - this.screenNameExtractor = screenNameExtractor; - return this; - } - - public AndroidLifecycleInstrumentation build() { - if (visibleScreenTracker == INVALID_SCREEN_TRACKER) { - throw new IllegalStateException("visibleScreenTracker must be configured."); - } - if (startupTimer == INVALID_TIMER) { - throw new IllegalStateException("startupTimer must be configured."); - } - return new AndroidLifecycleInstrumentation(this); - } -}