-
Notifications
You must be signed in to change notification settings - Fork 76
feat(instrumentation): Add FirstDraw spans to activity instrumentation #1281
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
Open
Doohl
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
Doohl:first-draw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,18 @@ This instrumentation produces the following telemetry: | |
| * `screen.name`: name of screen | ||
| * `last.screen.name`: name of screen, only when span contains the `activityPostResumed` event. | ||
|
|
||
| ### First Draw | ||
|
|
||
| * Type: Span | ||
| * Name: `FirstDraw` | ||
| * Description: On activity PreCreated or Created (pre API 29) callback, a span will be created | ||
| to represent the time that the UI pipeline took to render that Activity's first frame. The span ends | ||
| at the first draw of the window | ||
| [DecorView](https://developer.android.com/reference/android/view/Window#getDecorView()) | ||
| * Attributes: | ||
| * `activity.name`: name of activity | ||
| * `screen.name`: name of screen | ||
|
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. suggestion: We've recently aligned on using |
||
|
|
||
| ## Installation | ||
|
|
||
| This instrumentation comes with the [android agent](../../android-agent) out of the box, so | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...src/main/java/io/opentelemetry/android/instrumentation/activity/draw/FirstDrawListener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.instrumentation.activity.draw | ||
|
|
||
| import android.app.Activity | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.view.View | ||
| import android.view.ViewTreeObserver | ||
|
|
||
| internal object FirstDrawListener { | ||
| fun registerFirstDraw( | ||
| activity: Activity, | ||
| drawDoneCallback: () -> Unit, | ||
| ) { | ||
| val window = activity.window | ||
|
|
||
| // Wait until the decorView is created until we actually use it | ||
| window.onDecorViewReady { | ||
| val decorView = window.peekDecorView() | ||
| val versionsBeforeBugFix = Build.VERSION.SDK_INT < Build.VERSION_CODES.O | ||
| val decorViewAttached = decorView.viewTreeObserver.isAlive && decorView.isAttachedToWindow | ||
|
|
||
| // Before API version 26, draw listeners were not merged back into the real view tree observer | ||
| // Workaround is to wait until the view is attached before registering draw listeners | ||
| // Source: https://android.googlesource.com/platform/frameworks/base/+/9f8ec54244a5e0343b9748db3329733f259604f3 | ||
| if (versionsBeforeBugFix && !decorViewAttached) { | ||
| decorView.addOnAttachStateChangeListener( | ||
| object : View.OnAttachStateChangeListener { | ||
| override fun onViewAttachedToWindow(v: View) { | ||
| decorView.viewTreeObserver.addOnDrawListener(NextDrawListener(decorView, drawDoneCallback)) | ||
| decorView.removeOnAttachStateChangeListener(this) | ||
| } | ||
|
|
||
| override fun onViewDetachedFromWindow(v: View) = Unit | ||
| }, | ||
| ) | ||
| } else { | ||
| decorView.viewTreeObserver.addOnDrawListener(NextDrawListener(decorView, drawDoneCallback)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ViewTreeObserver.removeOnDrawListener() cannot be called from the onDraw() callback, | ||
| * so remove it in next draw. | ||
| */ | ||
| internal class NextDrawListener( | ||
| val view: View, | ||
| val drawDoneCallback: () -> Unit, | ||
| val handler: Handler = Handler(Looper.getMainLooper()), | ||
| ) : ViewTreeObserver.OnDrawListener { | ||
| var invoked = false | ||
|
|
||
| override fun onDraw() { | ||
| if (!invoked) { | ||
| invoked = true | ||
| drawDoneCallback() | ||
| handler.post { | ||
| if (view.viewTreeObserver.isAlive) { | ||
| view.viewTreeObserver.removeOnDrawListener(this) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
...ivity/src/main/java/io/opentelemetry/android/instrumentation/activity/draw/WindowUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.instrumentation.activity.draw | ||
|
|
||
| import android.os.Build | ||
| import android.view.ActionMode | ||
| import android.view.SearchEvent | ||
| import android.view.Window | ||
| import androidx.annotation.RequiresApi | ||
|
|
||
| internal fun Window.onDecorViewReady(callback: () -> Unit) { | ||
| if (peekDecorView() == null) { | ||
| onContentChanged { | ||
| callback() | ||
| return@onContentChanged false | ||
| } | ||
| } else { | ||
| callback() | ||
| } | ||
| } | ||
|
|
||
| internal fun Window.onContentChanged(callbackInvocation: () -> Boolean) { | ||
| val currentCallback = callback | ||
| val callback = | ||
| if (currentCallback is WindowDelegateCallback) { | ||
| currentCallback | ||
| } else { | ||
| val newCallback = WindowDelegateCallback(currentCallback) | ||
| callback = newCallback | ||
| newCallback | ||
| } | ||
| callback.onContentChangedCallbacks += callbackInvocation | ||
| } | ||
|
|
||
| internal class WindowDelegateCallback( | ||
| private val delegate: Window.Callback, | ||
| ) : Window.Callback by delegate { | ||
| val onContentChangedCallbacks = mutableListOf<() -> Boolean>() | ||
|
|
||
| override fun onContentChanged() { | ||
| onContentChangedCallbacks.removeAll { callback -> | ||
| !callback() | ||
| } | ||
| delegate.onContentChanged() | ||
| } | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.M) | ||
| override fun onSearchRequested(searchEvent: SearchEvent): Boolean = delegate.onSearchRequested(searchEvent) | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.M) | ||
| override fun onWindowStartingActionMode( | ||
| callback: ActionMode.Callback, | ||
| type: Int, | ||
| ): ActionMode? = delegate.onWindowStartingActionMode(callback, type) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
suggestion: I'm currently proposing
app.screen.time_to_first_drawin open-telemetry/semantic-conventions#2831. PR is still open so it may change but I don't think we should be using Pascal case regardless. I know that existing spans e.g.AppStartare in Pascal case but I recall both Jason and Hanson saying that this is something that needs to be fixed in the OTel Android SDK since it doesn't align with the usual casing convention of OTel.