Skip to content

Commit 82f87e4

Browse files
authored
feat(android): UI events transactions (#4985)
1 parent 1508bcc commit 82f87e4

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/includes/getting-started-primer/android.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The SDK builds a crash report that persists to disk and tries to send the report
2626
- [User Feedback](/platforms/android/enriching-events/user-feedback/) provides the ability to collect user information when an event occurs.
2727
- [Performance Monitoring](/product/performance/) creates transactions for:
2828
- Android activity transactions
29+
- User interaction transactions (view click, scroll, swipe, and so on)
2930
- Android fragment spans with [Fragment Integration](/platforms/android/configuration/integrations/fragment/)
3031
- [Cold and warm app start](/platforms/android/performance/instrumentation/automatic-instrumentation/#app-start-instrumentation)
3132
- [Slow and frozen frames](/platforms/android/performance/instrumentation/automatic-instrumentation/#slow-and-frozen-frames)

src/platforms/android/common/performance/instrumentation/automatic-instrumentation.mdx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,103 @@ The span finishes once the operation has been executed. The span `status` is set
223223
When the operation throws an `Exception`, Sentry's SDK associates this exception to the running span. If you haven't set the SDK to swallow the exception and capture it, the span and SentryEvent will be linked when viewing it on the **Issue Details** page in sentry.io.
224224

225225
For more information see our [file I/O integration](/platforms/android/configuration/integrations/file-io/).
226+
227+
### User Interaction Instrumentation
228+
229+
_(New in version 6.0.0)_
230+
231+
The UI instrumentation, once enabled, captures transactions for a set of different user interactions, which include clicks, scrolls and swipes. The SDK composes the transaction name out of the host `Activity` and the `id` of the view, which captured the user interaction; for example, `LoginActivity.login_button`. The transaction operation is set to `ui.action` plus the interaction type (one of `click`, `scroll` or `swipe`).
232+
233+
<Note>
234+
235+
If the view doesn't have the `id` assigned, the transaction won't be captured because it can't be uniquely identified otherwise.
236+
237+
</Note>
238+
239+
The UI instrumentation is disabled by default, but you can enable it by setting:
240+
241+
```xml {filename:AndroidManifest.xml}
242+
<manifest>
243+
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
244+
</manifest>
245+
```
246+
247+
The transaction finishes automatically after it reaches the specified [idleTimeout](/platforms/android/configuration/options/#idle-timeout) and all of its child spans are finished. The `idleTimeoout` defaults to `3000` milliseconds (three seconds). You can also disable the idle timeout by setting it to `null`, but the transaction must be finished manually in this case.
248+
249+
<Note>
250+
251+
If the UI transaction has idled, but didn't have any child spans added, it will be dropped.
252+
253+
</Note>
254+
255+
To finish the transaction manually, you can:
256+
257+
```java
258+
import io.sentry.Sentry;
259+
260+
ISpan span = Sentry.getSpan();
261+
if (span != null) {
262+
span.finish();
263+
}
264+
```
265+
266+
```kotlin
267+
import io.sentry.Sentry
268+
269+
val span = Sentry.getSpan()
270+
span?.finish()
271+
```
272+
273+
If the host `Activity` transitions into a non-interactive state (for example, `onPause`), the UI transaction will be scheduled to finish automatically (as soon as all of its child spans are finished).
274+
275+
Transactions are always bound to the `Scope` automatically if there's none set. Because of that, you can create spans using manual instrumentation, and those spans will be automatically associated with the running UI transaction.
276+
277+
```java
278+
import java.lang.Exception;
279+
280+
import io.sentry.ISpan;
281+
import io.sentry.Sentry;
282+
import io.sentry.SpanStatus;
283+
284+
void loadUserDataOnClick() {
285+
ISpan span = Sentry.getSpan();
286+
if (span != null) {
287+
ISpan innerSpan = span.startChild("loadUserData");
288+
try {
289+
// omitted code
290+
} catch (Exception e) {
291+
innerSpan.setThrowable(e);
292+
innerSpan.setStatus(SpanStatus.NOT_FOUND);
293+
throw e;
294+
} finally {
295+
innerSpan.finish();
296+
}
297+
}
298+
}
299+
```
300+
301+
```kotlin
302+
import java.lang.Exception
303+
304+
import io.sentry.Sentry
305+
import io.sentry.SpanStatus
306+
307+
fun loadUserDataOnClick() {
308+
val span = Sentry.getSpan()
309+
span?.let {
310+
val innerSpan = it.startChild("displayUloadUserDataserData")
311+
312+
try {
313+
// omitted code
314+
} catch (e: Exception) {
315+
innerSpan.throwable = e
316+
innerSpan.status = SpanStatus.NOT_FOUND
317+
throw e
318+
} finally {
319+
innerSpan.finish()
320+
}
321+
}
322+
}
323+
```
324+
325+
When the UI transaction is not finished yet, but the user makes a new interaction, the SDK automatically finishes the previous UI transaction. This is because only one transaction can be bound to the scope at a time. However, if the same view has been interacted with (for example, a `RecyclerView` was scrolled again within the `idleTimeout` window), the idle timer will be reset and the transaction duration will be extended with the `idleTimeout` value.

src/platforms/common/configuration/options.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ _(New in version 6.0.0)_
383383

384384
</ConfigKey>
385385

386+
<ConfigKey name="idle-timeout" supported={["android"]}>
387+
388+
The idle time, measured in ms, to wait until a transaction will be automatically finished. The transaction will use the end timestamp of the last finished span as the endtime for the transaction.
389+
390+
The default is `3000`.
391+
392+
_(New in version 6.0.0)_
393+
394+
<Note>
395+
396+
This only affects [user interaction transactions](/platforms/android/performance/instrumentation/automatic-instrumentation/#user-interaction-instrumentation).
397+
398+
</Note>
399+
400+
</ConfigKey>
401+
386402
<PlatformSection supported={["javascript.electron"]}>
387403

388404
<ConfigKey name="ipcMode" />

0 commit comments

Comments
 (0)