Skip to content

Commit 1ba0f7d

Browse files
0xadam-brownOpenCode
andcommitted
feat(android): Add Buddy live feed
Show a passive Live Feed from the Buddy floating button so developers can inspect recent useful signals before or during recording. Track screens, manual steps, errors, failed HTTP breadcrumbs, and adverse spans in a bounded ring buffer. Surface unviewed adverse signals with an attention card and floating button badge/ring. Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 7a0753c commit 1ba0f7d

7 files changed

Lines changed: 721 additions & 22 deletions

File tree

‎sentry-android-buddy/api/sentry-android-buddy.api‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,12 @@ public final class io/sentry/android/buddy/ComposableSingletons$SentryBuddyUiKt
314314
public final fun getLambda$-1944467248$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function2;
315315
public final fun getLambda$-218216137$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
316316
public final fun getLambda$-682238859$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
317+
public final fun getLambda$-909325499$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
317318
public final fun getLambda$1040476556$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
318319
public final fun getLambda$1215034671$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
320+
public final fun getLambda$1525882558$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
319321
public final fun getLambda$1644532347$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
322+
public final fun getLambda$1802898184$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
320323
public final fun getLambda$1884831281$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
321324
public final fun getLambda$1892283088$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
322325
public final fun getLambda$356797945$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
@@ -609,6 +612,11 @@ public final class io/sentry/android/buddy/SentryBuddySessionState$Intro : io/se
609612
public static final field INSTANCE Lio/sentry/android/buddy/SentryBuddySessionState$Intro;
610613
}
611614

615+
public final class io/sentry/android/buddy/SentryBuddySessionState$LiveFeed : io/sentry/android/buddy/SentryBuddySessionState {
616+
public static final field $stable I
617+
public static final field INSTANCE Lio/sentry/android/buddy/SentryBuddySessionState$LiveFeed;
618+
}
619+
612620
public final class io/sentry/android/buddy/SentryBuddySessionState$Recording : io/sentry/android/buddy/SentryBuddySessionState {
613621
public static final field $stable I
614622
public fun <init> (Lio/sentry/android/buddy/BuddyFlowIntent;J)V
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package io.sentry.android.buddy
2+
3+
import java.util.Date
4+
5+
internal data class BuddyLiveFeed(
6+
val items: List<BuddyLiveFeedItem> = emptyList(),
7+
val unviewedAdverseCount: Int = 0,
8+
val latestAdverseItem: BuddyLiveFeedItem? = null,
9+
val latestUnviewedAdverseItem: BuddyLiveFeedItem? = null,
10+
)
11+
12+
internal data class BuddyLiveFeedItem(
13+
val id: Long,
14+
val timelineItem: BuddyTimelineItem,
15+
val category: Category,
16+
val severity: Severity = Severity.LOW,
17+
val adverse: Boolean = false,
18+
val viewed: Boolean = false,
19+
) {
20+
enum class Category(val label: String) {
21+
SCREEN("Screen"),
22+
STEP("Step"),
23+
ERROR("Error"),
24+
FAILED_HTTP("Failed HTTP"),
25+
SLOW_SPAN("Slow span"),
26+
FAILED_SPAN("Failed span"),
27+
}
28+
29+
val timestamp: Date
30+
get() = timelineItem.timestamp
31+
}
32+
33+
internal class BuddyLiveFeedBuffer(private val capacity: Int) {
34+
private val items = ArrayDeque<BuddyLiveFeedItem>()
35+
private var nextId = 0L
36+
37+
fun add(
38+
item: BuddyTimelineItem,
39+
category: BuddyLiveFeedItem.Category,
40+
severity: Severity,
41+
adverse: Boolean,
42+
): BuddyLiveFeed {
43+
nextId++
44+
items +=
45+
BuddyLiveFeedItem(
46+
id = nextId,
47+
timelineItem = item,
48+
category = category,
49+
severity = severity,
50+
adverse = adverse,
51+
)
52+
while (items.size > capacity) {
53+
items.removeFirst()
54+
}
55+
return snapshot()
56+
}
57+
58+
fun markAdverseViewed(): BuddyLiveFeed {
59+
for (index in items.indices) {
60+
val item = items[index]
61+
if (item.adverse) {
62+
items[index] = item.copy(viewed = true)
63+
}
64+
}
65+
return snapshot()
66+
}
67+
68+
fun snapshot(): BuddyLiveFeed {
69+
val newestFirst = items.reversed()
70+
val adverseItems = newestFirst.filter { it.adverse }
71+
val unviewedAdverseItems = adverseItems.filterNot { it.viewed }
72+
return BuddyLiveFeed(
73+
items = newestFirst,
74+
unviewedAdverseCount = unviewedAdverseItems.size,
75+
latestAdverseItem = adverseItems.maxWithOrNull(BuddyLiveFeedItemAttentionComparator),
76+
latestUnviewedAdverseItem =
77+
unviewedAdverseItems.maxWithOrNull(BuddyLiveFeedItemAttentionComparator),
78+
)
79+
}
80+
81+
private companion object {
82+
private val BuddyLiveFeedItemAttentionComparator =
83+
compareBy<BuddyLiveFeedItem>({ it.severity.rank }, { it.timestamp.time })
84+
85+
private val Severity.rank: Int
86+
get() =
87+
when (this) {
88+
Severity.HIGH -> 3
89+
Severity.MEDIUM -> 2
90+
Severity.LOW -> 1
91+
}
92+
}
93+
}

‎sentry-android-buddy/src/main/java/io/sentry/android/buddy/SentryBuddy.kt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public object SentryBuddy {
8888
return requireInstalled().stop()
8989
}
9090

91+
internal fun liveFeedSnapshot(): BuddyLiveFeed = requireInstalled().liveFeedSnapshot()
92+
93+
internal fun markLiveFeedSeen(): BuddyLiveFeed = requireInstalled().markLiveFeedSeen()
94+
95+
internal fun addLiveFeedListener(listener: (BuddyLiveFeed) -> Unit): () -> Unit =
96+
requireInstalled().addLiveFeedListener(listener)
97+
9198
private fun requireInstalled(): BuddyRecorder {
9299
return checkNotNull(recorder) { "SentryBuddy.install(application) must be called first." }
93100
}

‎sentry-android-buddy/src/main/java/io/sentry/android/buddy/SentryBuddySession.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public object RealSentryBuddyRecorderFacade : SentryBuddyRecorderFacade {
142142
public sealed class SentryBuddySessionState {
143143
public object Closed : SentryBuddySessionState()
144144

145+
public object LiveFeed : SentryBuddySessionState()
146+
145147
public object Intro : SentryBuddySessionState()
146148

147149
public data class Recording
@@ -192,6 +194,9 @@ public constructor(
192194
public var state: SentryBuddySessionState = SentryBuddySessionState.Closed
193195
private set
194196

197+
internal var liveFeed: BuddyLiveFeed = safeLiveFeed()
198+
private set
199+
195200
private val transientRecordingEventLock: Any = Any()
196201
private val transientRecordingEventListeners = mutableListOf<(TransientRecordingEvent) -> Unit>()
197202
private var transientRecordingEventId: Long = 0
@@ -200,6 +205,11 @@ public constructor(
200205
state = SentryBuddySessionState.Intro
201206
}
202207

208+
internal fun openLiveFeed() {
209+
liveFeed = safeMarkLiveFeedSeen()
210+
state = SentryBuddySessionState.LiveFeed
211+
}
212+
203213
public fun close() {
204214
if (state !is SentryBuddySessionState.Recording) {
205215
state = SentryBuddySessionState.Closed
@@ -354,6 +364,46 @@ public constructor(
354364
}
355365
}
356366

367+
internal fun addLiveFeedListener(listener: (BuddyLiveFeed) -> Unit): () -> Unit {
368+
val noOp: () -> Unit = {}
369+
return try {
370+
if (recorderFacade !== RealSentryBuddyRecorderFacade) {
371+
listener(BuddyLiveFeed())
372+
noOp
373+
} else {
374+
SentryBuddy.addLiveFeedListener { feed ->
375+
liveFeed = feed
376+
listener(feed)
377+
}
378+
}
379+
} catch (_: IllegalStateException) {
380+
listener(BuddyLiveFeed())
381+
noOp
382+
}
383+
}
384+
385+
private fun safeLiveFeed(): BuddyLiveFeed =
386+
try {
387+
if (recorderFacade === RealSentryBuddyRecorderFacade) {
388+
SentryBuddy.liveFeedSnapshot()
389+
} else {
390+
BuddyLiveFeed()
391+
}
392+
} catch (_: IllegalStateException) {
393+
BuddyLiveFeed()
394+
}
395+
396+
private fun safeMarkLiveFeedSeen(): BuddyLiveFeed =
397+
try {
398+
if (recorderFacade === RealSentryBuddyRecorderFacade) {
399+
SentryBuddy.markLiveFeedSeen()
400+
} else {
401+
BuddyLiveFeed()
402+
}
403+
} catch (_: IllegalStateException) {
404+
BuddyLiveFeed()
405+
}
406+
357407
private fun buildFlowAnalysisRequest(
358408
state: SentryBuddySessionState.Briefing
359409
): FlowAnalysisRequest {

0 commit comments

Comments
 (0)