Skip to content

Commit

Permalink
Initializing services when creating a preconfigured rum instance (#528)
Browse files Browse the repository at this point in the history
* Clean up

* Initializing ServiceManager when building a preconfigured rum instance
  • Loading branch information
LikeTheSalad authored Aug 13, 2024
1 parent 76d461b commit 58c871e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AndroidInstrumentation>()
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.
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ interface ServiceManager : Startable {
checkNotNull(instance) { "Services haven't been initialized" }
return instance!!
}

@JvmStatic
fun resetForTest() {
instance = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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<Application.ActivityLifecycleCallbacks> activityCallbacksCaptor;
private AutoCloseable mocks;

@Before
Expand All @@ -118,6 +113,7 @@ public void tearDown() throws Exception {
SignalFromDiskExporter.resetForTesting();
InitializationEvents.resetForTest();
AndroidInstrumentationLoader.resetForTest();
ServiceManager.resetForTest();
mocks.close();
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 58c871e

Please sign in to comment.