diff --git a/core/src/main/java/io/opentelemetry/android/OpenTelemetryRum.java b/core/src/main/java/io/opentelemetry/android/OpenTelemetryRum.java index 8fd0cbafd..2810f8de5 100644 --- a/core/src/main/java/io/opentelemetry/android/OpenTelemetryRum.java +++ b/core/src/main/java/io/opentelemetry/android/OpenTelemetryRum.java @@ -7,6 +7,7 @@ import android.app.Application; import io.opentelemetry.android.config.OtelRumConfig; +import io.opentelemetry.android.internal.services.ServiceManager; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.logs.SdkLoggerProvider; @@ -65,8 +66,9 @@ static SdkPreconfiguredRumBuilder builder( Application application, OpenTelemetrySdk openTelemetrySdk, boolean discoverInstrumentations) { + ServiceManager.initialize(application); return new SdkPreconfiguredRumBuilder( - application, openTelemetrySdk, discoverInstrumentations); + application, openTelemetrySdk, discoverInstrumentations, ServiceManager.get()); } /** Returns a no-op implementation of {@link OpenTelemetryRum}. */ diff --git a/core/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/core/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index 715d51ca7..48203d563 100644 --- a/core/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/core/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -321,10 +321,9 @@ OpenTelemetryRum build(ServiceManager serviceManager) { application, sdk, sessionId, - serviceManager::getAppLifecycleService, - config.shouldDiscoverInstrumentations()); + config.shouldDiscoverInstrumentations(), + serviceManager); instrumentations.forEach(delegate::addInstrumentation); - serviceManager.start(); return delegate.build(); } diff --git a/core/src/main/java/io/opentelemetry/android/SdkPreconfiguredRumBuilder.kt b/core/src/main/java/io/opentelemetry/android/SdkPreconfiguredRumBuilder.kt index 9b1894313..35a380e0e 100644 --- a/core/src/main/java/io/opentelemetry/android/SdkPreconfiguredRumBuilder.kt +++ b/core/src/main/java/io/opentelemetry/android/SdkPreconfiguredRumBuilder.kt @@ -9,7 +9,6 @@ import android.app.Application import io.opentelemetry.android.instrumentation.AndroidInstrumentation import io.opentelemetry.android.instrumentation.AndroidInstrumentationLoader import io.opentelemetry.android.internal.services.ServiceManager -import io.opentelemetry.android.internal.services.applifecycle.AppLifecycleService import io.opentelemetry.sdk.OpenTelemetrySdk class SdkPreconfiguredRumBuilder @@ -21,13 +20,11 @@ class SdkPreconfiguredRumBuilder SessionId( SessionIdTimeoutHandler(), ), - appLifecycleServiceProvider: () -> AppLifecycleService = { - ServiceManager.get().getAppLifecycleService() - }, private val discoverInstrumentations: Boolean, + private val serviceManager: ServiceManager, ) { private val instrumentations = mutableListOf() - private val appLifecycleService by lazy { appLifecycleServiceProvider.invoke() } + private val appLifecycleService by lazy { serviceManager.getAppLifecycleService() } /** * Adds an instrumentation to be applied as a part of the [build] method call. @@ -49,6 +46,7 @@ class SdkPreconfiguredRumBuilder * @return A new [OpenTelemetryRum] instance. */ fun build(): OpenTelemetryRum { + serviceManager.start() // the app state listeners need to be run in the first ActivityLifecycleCallbacks since they // might turn off/on additional telemetry depending on whether the app is active or not appLifecycleService.registerListener(sessionId.timeoutHandler) diff --git a/core/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt b/core/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt index 89fadecc8..647cd4508 100644 --- a/core/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt +++ b/core/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt @@ -55,5 +55,10 @@ interface ServiceManager : Startable { checkNotNull(instance) { "Services haven't been initialized" } return instance!! } + + @JvmStatic + fun resetForTest() { + instance = null + } } } diff --git a/core/src/test/java/io/opentelemetry/android/OpenTelemetryRumBuilderTest.java b/core/src/test/java/io/opentelemetry/android/OpenTelemetryRumBuilderTest.java index 7ce472b6f..0c10aca6b 100644 --- a/core/src/test/java/io/opentelemetry/android/OpenTelemetryRumBuilderTest.java +++ b/core/src/test/java/io/opentelemetry/android/OpenTelemetryRumBuilderTest.java @@ -24,7 +24,6 @@ import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -import android.app.Activity; import android.app.Application; import android.os.Looper; import androidx.annotation.NonNull; @@ -82,7 +81,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; -import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -98,11 +96,8 @@ public class OpenTelemetryRumBuilderTest { @Mock Application application; @Mock Looper looper; @Mock android.content.Context applicationContext; - @Mock Activity activity; - @Mock ApplicationStateListener listener; @Mock InitializationEvents initializationEvents; - @Captor ArgumentCaptor activityCallbacksCaptor; private AutoCloseable mocks; @Before @@ -118,6 +113,7 @@ public void tearDown() throws Exception { SignalFromDiskExporter.resetForTesting(); InitializationEvents.resetForTest(); AndroidInstrumentationLoader.resetForTest(); + ServiceManager.resetForTest(); mocks.close(); } @@ -171,7 +167,6 @@ public void shouldBuildLogRecordProvider() { .build(); OpenTelemetrySdk sdk = (OpenTelemetrySdk) openTelemetryRum.getOpenTelemetry(); - String sessionId = openTelemetryRum.getRumSessionId(); EventLogger eventLogger = SdkEventLoggerProvider.create(sdk.getSdkLoggerProvider()) .get("otel.initialization.events"); @@ -444,6 +439,15 @@ public void verifyServicesAreStarted() { verify(serviceManager).start(); } + @Test + public void verifyPreconfiguredServicesInitialization() { + OpenTelemetrySdk openTelemetrySdk = mock(); + + OpenTelemetryRum.builder(application, openTelemetrySdk, true).build(); + + assertThat(ServiceManager.get()).isNotNull(); + } + /** * @noinspection KotlinInternalInJava */