Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumentation API part 4 #439

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion android-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ dependencies {
implementation(project(":instrumentation:activity"))
implementation(project(":instrumentation:anr"))
implementation(project(":instrumentation:crash"))
implementation(project(":instrumentation:slowrendering"))
implementation(libs.androidx.core)
implementation(libs.androidx.navigation.fragment)
implementation(libs.androidx.lifecycle.process)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.opentelemetry.android.instrumentation.common.InstrumentedApplication;
import io.opentelemetry.android.instrumentation.crash.CrashReporter;
import io.opentelemetry.android.instrumentation.crash.CrashReporterBuilder;
import io.opentelemetry.android.instrumentation.slowrendering.SlowRenderingDetector;
import io.opentelemetry.android.instrumentation.startup.InitializationEvents;
import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents;
import io.opentelemetry.android.internal.features.networkattrs.NetworkAttributesSpanAppender;
Expand Down Expand Up @@ -475,19 +474,6 @@ private void applyConfiguration() {
});
}

// Enable slow rendering detection if enabled
if (config.isSlowRenderingDetectionEnabled()) {
addInstrumentation(
instrumentedApplication -> {
SlowRenderingDetector.builder()
.setSlowRenderingDetectionPollInterval(
config.getSlowRenderingDetectionPollInterval())
.build()
.installOn(instrumentedApplication);
initializationEvents.slowRenderingDetectorInitialized();
});
}

// Enable crash reporting instrumentation
if (config.isCrashReportingEnabled()) {
addInstrumentation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@
*/
public class OtelRumConfig {

private static final Duration DEFAULT_SLOW_RENDERING_DETECTION_POLL_INTERVAL =
Duration.ofSeconds(1);

private Supplier<Attributes> globalAttributesSupplier = Attributes::empty;
private boolean includeNetworkAttributes = true;
private boolean generateSdkInitializationEvents = true;
private boolean includeScreenAttributes = true;
private DiskBufferingConfiguration diskBufferingConfiguration =
DiskBufferingConfiguration.builder().build();
private boolean anrDetectionEnabled = true;
private boolean slowRenderingDetectionEnabled = true;
private Duration slowRenderingDetectionPollInterval =
DEFAULT_SLOW_RENDERING_DETECTION_POLL_INTERVAL;
private boolean crashReportingEnabled = true;
private Duration sessionTimeout = Duration.ofMinutes(15);

Expand Down Expand Up @@ -135,36 +129,6 @@ public OtelRumConfig disableAnrDetection() {
return this;
}

/** Returns true if the slow rendering detection instrumentation is enabled. */
public boolean isSlowRenderingDetectionEnabled() {
return slowRenderingDetectionEnabled;
}

/**
* Call this method to disable the slow rendering detection instrumentation.
*
* @return this
*/
public OtelRumConfig disableSlowRenderingDetection() {
slowRenderingDetectionEnabled = false;
return this;
}

/** Returns the Duration at which slow renders are polled. Default = 1s. */
public Duration getSlowRenderingDetectionPollInterval() {
return slowRenderingDetectionPollInterval;
}

/**
* Call this to configure the duration for polling for slow renders.
*
* @return this
*/
public OtelRumConfig setSlowRenderingDetectionPollInterval(Duration duration) {
slowRenderingDetectionPollInterval = duration;
return this;
}

/** Returns true if crash reporting is enabled. */
public boolean isCrashReportingEnabled() {
return crashReportingEnabled;
Expand Down
1 change: 1 addition & 0 deletions instrumentation/slowrendering/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
dependencies {
api(platform(libs.opentelemetry.platform))
api(libs.opentelemetry.api)
api(project(":android-agent"))
api(project(":common"))
api(project(":instrumentation:common-api"))
implementation(libs.androidx.core)
Expand Down

This file was deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was renamed to SlowRenderingInstrumentation and then added the installOn method from SlowRenderingDetector.

This file was deleted.

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

package io.opentelemetry.android.instrumentation.slowrendering;

import android.app.Application;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.common.RumConstants;
import io.opentelemetry.android.instrumentation.AndroidInstrumentation;
import java.time.Duration;

/** Entrypoint for installing the slow rendering detection instrumentation. */
public final class SlowRenderingInstrumentation implements AndroidInstrumentation {

Duration slowRenderingDetectionPollInterval = Duration.ofSeconds(1);

/**
* Configures the rate at which frame render durations are polled.
*
* @param interval The period that should be used for polling.
* @return {@code this}
*/
public SlowRenderingInstrumentation setSlowRenderingDetectionPollInterval(Duration interval) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably the agent detects when this class is on the classpath, instantiates it, and installs it. How would we expect this method to ever get called? I think there are no usages outside of tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now, people will need to know what the instrumentation impl class is to configure a particular instrumentation by using AndroidInstrumentationRegistry, so I guess each instrumentation's README should mention the class name when explaining its configurable params. So for this case, they'd have to write the following to call this method:

AndroidInstrumentationRegistry.get().get(SlowRenderingInstrumentation::class.java).setSlowRenderingDetectionPollInterval(Duration.ofSeconds(2))

I guess we can probably build some helper tools around it in the future to make it less verbose, but that's essentially the way to do so now, though any ideas to improve it are welcome!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@breedx-splk I'm a bit worried about not having all the parts ready before the next release, since the agent won't have any auto instrumentation working until all parts are done, so I'll go ahead and merge this PR and we can continue this discussion on the SIG if you'd like as this way of configuring instrumentations is not exclusive to the slowrendering one anyway.

if (interval.toMillis() <= 0) {
Log.e(
RumConstants.OTEL_RUM_LOG_TAG,
"Invalid slowRenderingDetectionPollInterval: "
+ interval
+ "; must be positive");
return this;
}
this.slowRenderingDetectionPollInterval = interval;
return this;
}

@Override
public void install(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can add a @RequiresApi or @ChecksSdkIntAtLeast annotation here or to the class itself to flag attempted usage in a lower API level?1020.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think of it because this method (at least for now) is meant to be called by the agent and not directly by our users, although I guess it's not impossible that someone would want to call it directly, so I guess it's probably better to make sure. I'll add the changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the changes.

@NonNull Application application, @NonNull OpenTelemetryRum openTelemetryRum) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Log.w(
RumConstants.OTEL_RUM_LOG_TAG,
"Slow/frozen rendering detection is not supported on platforms older than Android N (SDK version 24).");
return;
}

SlowRenderListener detector =
new SlowRenderListener(
openTelemetryRum
.getOpenTelemetry()
.getTracer("io.opentelemetry.slow-rendering"),
slowRenderingDetectionPollInterval);

application.registerActivityLifecycleCallbacks(detector);
detector.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.instrumentation.slowrendering

import android.app.Application
import io.mockk.Called
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import io.opentelemetry.android.OpenTelemetryRum
import io.opentelemetry.api.OpenTelemetry
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.time.Duration

@RunWith(RobolectricTestRunner::class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use AndroidJUnit4 test runner here too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I was rushing this test earlier today and couldn't make AS find AndroidJUnit4, so I guess there's some missing dependency in this project, though I have some time now so let me take a look at what's missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the changes, also made the missing test dependency available to all android lib modules.

class SlowRenderingInstrumentationTest {
private lateinit var slowRenderingInstrumentation: SlowRenderingInstrumentation
private lateinit var application: Application
private lateinit var openTelemetryRum: OpenTelemetryRum
private lateinit var openTelemetry: OpenTelemetry

@Before
fun setUp() {
application = mockk()
openTelemetry = mockk()
openTelemetryRum = mockk()
every { openTelemetryRum.openTelemetry }.returns(openTelemetry)
slowRenderingInstrumentation = SlowRenderingInstrumentation()
}

@Test
fun `Verify default poll interval value`() {
assertThat(slowRenderingInstrumentation.slowRenderingDetectionPollInterval).isEqualTo(
Duration.ofSeconds(1),
)
}

@Test
fun `Changing poll interval value`() {
slowRenderingInstrumentation.setSlowRenderingDetectionPollInterval(Duration.ofSeconds(2))

assertThat(slowRenderingInstrumentation.slowRenderingDetectionPollInterval).isEqualTo(
Duration.ofSeconds(2),
)
}

@Test
fun `Not changing poll interval value when provided value is negative`() {
slowRenderingInstrumentation.setSlowRenderingDetectionPollInterval(Duration.ofSeconds(-2))

assertThat(slowRenderingInstrumentation.slowRenderingDetectionPollInterval).isEqualTo(
Duration.ofSeconds(1),
)
}

@Config(sdk = [23])
@Test
fun `Not installing instrumentation on devices with API level lower than 24`() {
slowRenderingInstrumentation.install(application, openTelemetryRum)

verify {
application wasNot Called
}
verify {
openTelemetryRum wasNot Called
}
}

@Config(sdk = [24, 25])
@Test
fun `Installing instrumentation on devices with API level equal or higher than 24`() {
val capturedListener = slot<SlowRenderListener>()
every { openTelemetry.getTracer(any()) }.returns(mockk())
every { application.registerActivityLifecycleCallbacks(any()) } just Runs

slowRenderingInstrumentation.install(application, openTelemetryRum)

verify { openTelemetry.getTracer("io.opentelemetry.slow-rendering") }
verify { application.registerActivityLifecycleCallbacks(capture(capturedListener)) }
}
}