Skip to content

Commit

Permalink
Instrumentation API part 3 (#422)
Browse files Browse the repository at this point in the history
* Instrumentation API part 2 (#408)

* Moving network provider tools to services

* Moving network service related tests

* Moving network attr init feature to the agent

* Making network instrumentation depend on the agent

* Initializing CurrentNetworkProvider as service

* Clean up config

* Created NetworkChangeInstrumentation

* Updated network monitor tests

* Clean up

* Renaming installOn by start

* Adding internal class docs

* Fixing visible for tests comment

* Rename .java to .kt

* Removing Carrier.Builder

* Transforming lifecycle tools to a service

* Updating tests

* Registering AppLifecycleService

* Rename .java to .kt

* Updating OpenTelemetryRumBuilderTest

* Updating network change instrumentation

* Adding internal comments

* Reverting back to instantiating OpenTelemetryRumBuilder using the static OpenTelemetryRum.builder function

* Updating tests

* Using AndroidJUnit4 instead of robolectric runner
  • Loading branch information
LikeTheSalad authored Jun 17, 2024
1 parent ec6a22a commit 53896cf
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 226 deletions.
2 changes: 1 addition & 1 deletion android-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ dependencies {
implementation(project(":common"))
implementation(project(":instrumentation:activity"))
implementation(project(":instrumentation:anr"))
implementation(project(":instrumentation:common-api"))
implementation(project(":instrumentation:crash"))
implementation(project(":instrumentation:slowrendering"))
implementation(libs.androidx.core)
implementation(libs.androidx.navigation.fragment)
implementation(libs.androidx.lifecycle.process)

api(platform(libs.opentelemetry.platform))
api(libs.opentelemetry.api)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ OpenTelemetryRum build(ServiceManager serviceManager) {
scheduleDiskTelemetryReader(signalFromDiskExporter, diskBufferingConfiguration);

SdkPreconfiguredRumBuilder delegate =
new SdkPreconfiguredRumBuilder(application, sdk, sessionId);
new SdkPreconfiguredRumBuilder(
application, sdk, sessionId, serviceManager::getAppLifecycleService);
instrumentationInstallers.forEach(delegate::addInstrumentation);
serviceManager.start();
return delegate.build();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android

import android.app.Application
import io.opentelemetry.android.instrumentation.common.InstrumentedApplication
import io.opentelemetry.android.internal.services.ServiceManager
import io.opentelemetry.android.internal.services.applifecycle.AppLifecycleService
import io.opentelemetry.sdk.OpenTelemetrySdk
import java.util.function.Consumer

class SdkPreconfiguredRumBuilder
@JvmOverloads
internal constructor(
private val application: Application,
private val sdk: OpenTelemetrySdk,
private val sessionId: SessionId =
SessionId(
SessionIdTimeoutHandler(),
),
appLifecycleServiceProvider: () -> AppLifecycleService = {
ServiceManager.get().getAppLifecycleService()
},
) {
private val instrumentationInstallers: MutableList<Consumer<InstrumentedApplication>> =
ArrayList()
private val appLifecycleService by lazy { appLifecycleServiceProvider.invoke() }

/**
* Adds an instrumentation installer function that will be run on an [ ] instance as a part of the [.build] method call.
*
* @return `this`
*/
fun addInstrumentation(instrumentationInstaller: Consumer<InstrumentedApplication>): SdkPreconfiguredRumBuilder {
instrumentationInstallers.add(instrumentationInstaller)
return this
}

/**
* Creates a new instance of [OpenTelemetryRum] with the settings of this [ ].
*
*
* This method uses a preconfigured OpenTelemetry SDK and install built-in system
* instrumentations in the passed Android [Application].
*
* @return A new [OpenTelemetryRum] instance.
*/
fun build(): OpenTelemetryRum {
// 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)

val tracer = sdk.getTracer(OpenTelemetryRum::class.java.simpleName)
sessionId.setSessionIdChangeListener(SessionIdChangeTracer(tracer))

// InstrumentedApplication instrumentedApplication =
// new InstrumentedApplicationImpl(application, sdk,
// applicationStateWatcher);
// for (Consumer<InstrumentedApplication> installer : instrumentationInstallers) {
// installer.accept(instrumentedApplication); TODO to be replaced by calls to
// AndroidInstrumentation.install
// }
return OpenTelemetryRumImpl(sdk, sessionId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.android;

import io.opentelemetry.android.instrumentation.common.ApplicationStateListener;
import io.opentelemetry.android.internal.services.applifecycle.ApplicationStateListener;
import io.opentelemetry.sdk.common.Clock;
import java.time.Duration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.android.internal.services

import android.app.Application
import io.opentelemetry.android.internal.services.applifecycle.AppLifecycleService
import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider
import io.opentelemetry.android.internal.services.periodicwork.PeriodicWorkService

Expand All @@ -21,6 +22,8 @@ interface ServiceManager : Startable {

fun getCurrentNetworkProvider(): CurrentNetworkProvider

fun getAppLifecycleService(): AppLifecycleService

companion object {
private var instance: ServiceManager? = null

Expand All @@ -38,6 +41,7 @@ interface ServiceManager : Startable {
),
PeriodicWorkService(),
CurrentNetworkProvider.create(application),
AppLifecycleService.create(),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.android.internal.services

import io.opentelemetry.android.internal.services.applifecycle.AppLifecycleService
import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider
import io.opentelemetry.android.internal.services.periodicwork.PeriodicWorkService
import java.util.Collections
Expand Down Expand Up @@ -36,6 +37,10 @@ internal class ServiceManagerImpl(services: List<Any>) : ServiceManager {
return getService(CurrentNetworkProvider::class.java)
}

override fun getAppLifecycleService(): AppLifecycleService {
return getService(AppLifecycleService::class.java)
}

override fun start() {
for (service in services.values) {
if (service is Startable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.internal.services.applifecycle

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import io.opentelemetry.android.internal.services.Startable

/**
* This class is internal and not for public use. Its APIs are unstable and can change at any time.
*/
class AppLifecycleService internal constructor(
private val applicationStateWatcher: ApplicationStateWatcher,
private val appLifecycle: Lifecycle,
) : Startable {
companion object {
@JvmStatic
fun create(): AppLifecycleService {
return AppLifecycleService(
ApplicationStateWatcher(),
ProcessLifecycleOwner.get().lifecycle,
)
}
}

fun registerListener(listener: ApplicationStateListener) {
applicationStateWatcher.registerListener(listener)
}

override fun start() {
appLifecycle.addObserver(applicationStateWatcher)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.internal.services.applifecycle;

/**
* Listener interface that is called whenever the instrumented application is brought to foreground
* from the background, or vice versa.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public interface ApplicationStateListener {

/**
* Called whenever the application is brought to the foreground (i.e. first activity starts).
*/
void onApplicationForegrounded();

/** Called whenever the application is brought to the background (i.e. last activity stops). */
void onApplicationBackgrounded();
}
Loading

0 comments on commit 53896cf

Please sign in to comment.