-
Notifications
You must be signed in to change notification settings - Fork 44
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
Changes from 3 commits
34df196
565529e
a6cb258
e7b8c8b
8eb8610
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we can add a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) } | ||
} | ||
} |
There was a problem hiding this comment.
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 theinstallOn
method fromSlowRenderingDetector
.