Skip to content

Commit 32e2021

Browse files
0xadam-brownOpenCode
andcommitted
feat(android): Link Buddy feed items to Sentry
Allow Buddy live feed items with event or trace metadata to open the matching Sentry UI view. Add explicit link configuration and wire the Android sample to the README project. Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 1ba0f7d commit 32e2021

9 files changed

Lines changed: 224 additions & 17 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +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;
317+
public final fun getLambda$-90711693$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
318318
public final fun getLambda$1040476556$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
319319
public final fun getLambda$1215034671$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
320+
public final fun getLambda$1352873802$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
320321
public final fun getLambda$1525882558$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
321322
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;
323323
public final fun getLambda$1884831281$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
324324
public final fun getLambda$1892283088$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
325325
public final fun getLambda$356797945$sentry_android_buddy_release ()Lkotlin/jvm/functions/Function3;
@@ -489,12 +489,21 @@ public final class io/sentry/android/buddy/SentryBuddyOptions {
489489
public fun <init> (Z)V
490490
public fun <init> (ZZ)V
491491
public fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;)V
492-
public synthetic fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
492+
public fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;Ljava/lang/String;)V
493+
public fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;Ljava/lang/String;Ljava/lang/String;)V
494+
public fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
495+
public synthetic fun <init> (ZZLio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
493496
public final fun getEnabled ()Z
494497
public final fun getFlowAnalysesApi ()Lio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;
498+
public final fun getSentryUiBaseUrl ()Ljava/lang/String;
499+
public final fun getSentryUiOrganizationSlug ()Ljava/lang/String;
500+
public final fun getSentryUiProjectId ()Ljava/lang/String;
495501
public final fun getShowOverlay ()Z
496502
public final fun setEnabled (Z)V
497503
public final fun setFlowAnalysesApi (Lio/sentry/android/buddy/SentryBuddyFlowAnalysesApi;)V
504+
public final fun setSentryUiBaseUrl (Ljava/lang/String;)V
505+
public final fun setSentryUiOrganizationSlug (Ljava/lang/String;)V
506+
public final fun setSentryUiProjectId (Ljava/lang/String;)V
498507
public final fun setShowOverlay (Z)V
499508
}
500509

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.sentry.android.buddy
2+
3+
import java.net.URLEncoder
4+
5+
internal data class BuddySentryUiLinks(
6+
val baseUrl: String? = null,
7+
val organizationSlug: String? = null,
8+
val projectId: String? = null,
9+
) {
10+
fun linkFor(item: BuddyLiveFeedItem): String? {
11+
val baseUrl = resolvedBaseUrl() ?: return null
12+
val projectId = projectId?.takeIf { it.isNotBlank() } ?: return null
13+
return when (item.category) {
14+
BuddyLiveFeedItem.Category.ERROR ->
15+
item.timelineItem.data.stringValue(DATA_EVENT_ID)?.let { eventId ->
16+
"$baseUrl/issues/?project=${projectId.urlEncode()}&query=${"event.id:$eventId".urlEncode()}"
17+
}
18+
BuddyLiveFeedItem.Category.SLOW_SPAN,
19+
BuddyLiveFeedItem.Category.FAILED_SPAN -> traceLink(baseUrl, projectId, item)
20+
BuddyLiveFeedItem.Category.FAILED_HTTP -> traceLink(baseUrl, projectId, item)
21+
BuddyLiveFeedItem.Category.SCREEN,
22+
BuddyLiveFeedItem.Category.STEP -> null
23+
}
24+
}
25+
26+
private fun traceLink(baseUrl: String, projectId: String, item: BuddyLiveFeedItem): String? {
27+
val traceId = item.timelineItem.data.stringValue(DATA_TRACE_ID) ?: return null
28+
val spanId = item.timelineItem.data.stringValue(DATA_SPAN_ID)
29+
return buildString {
30+
append(baseUrl)
31+
append("/performance/trace/")
32+
append(traceId.urlEncode())
33+
append("/?project=")
34+
append(projectId.urlEncode())
35+
if (!spanId.isNullOrBlank()) {
36+
append("&span=")
37+
append(spanId.urlEncode())
38+
}
39+
}
40+
}
41+
42+
private fun resolvedBaseUrl(): String? {
43+
val explicitBaseUrl = baseUrl?.trimEnd('/')?.takeIf { it.isNotBlank() }
44+
if (explicitBaseUrl != null) {
45+
return explicitBaseUrl
46+
}
47+
val organization = organizationSlug?.takeIf { it.isNotBlank() } ?: return null
48+
return "https://$organization.sentry.io"
49+
}
50+
51+
private fun String.urlEncode(): String = URLEncoder.encode(this, "UTF-8")
52+
53+
private companion object {
54+
private const val DATA_EVENT_ID = "event_id"
55+
private const val DATA_TRACE_ID = "trace_id"
56+
private const val DATA_SPAN_ID = "span_id"
57+
}
58+
}
59+
60+
internal fun SentryBuddyOptions.sentryUiLinks(): BuddySentryUiLinks =
61+
BuddySentryUiLinks(
62+
baseUrl = sentryUiBaseUrl,
63+
organizationSlug = sentryUiOrganizationSlug,
64+
projectId = sentryUiProjectId,
65+
)
66+
67+
private fun Map<String, Any?>.stringValue(key: String): String? = this[key]?.toString()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ public object SentryBuddy {
194194
return null
195195
}
196196
return BuddyOverlayManager(
197-
SentryBuddySessionController(flowAnalysesApi = options.flowAnalysesApi)
198-
)
197+
SentryBuddySessionController(flowAnalysesApi = options.flowAnalysesApi)
198+
)
199+
.also { it.updateOptions(options) }
199200
}
200201

201202
@TestOnly

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public constructor(
99
public var enabled: Boolean = true,
1010
public var showOverlay: Boolean = true,
1111
public var flowAnalysesApi: SentryBuddyFlowAnalysesApi = DummySentryBuddyFlowAnalysesApi,
12+
public var sentryUiBaseUrl: String? = null,
13+
public var sentryUiOrganizationSlug: String? = null,
14+
public var sentryUiProjectId: String? = null,
1215
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public constructor(
197197
internal var liveFeed: BuddyLiveFeed = safeLiveFeed()
198198
private set
199199

200+
internal var sentryUiLinks: BuddySentryUiLinks = BuddySentryUiLinks()
201+
200202
private val transientRecordingEventLock: Any = Any()
201203
private val transientRecordingEventListeners = mutableListOf<(TransientRecordingEvent) -> Unit>()
202204
private var transientRecordingEventId: Long = 0

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

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package io.sentry.android.buddy
22

3+
import android.content.ActivityNotFoundException
4+
import android.content.Context
5+
import android.content.Intent
36
import android.graphics.Rect
7+
import android.net.Uri
48
import androidx.compose.animation.AnimatedVisibility
59
import androidx.compose.animation.core.Animatable
610
import androidx.compose.animation.core.tween
@@ -9,6 +13,7 @@ import androidx.compose.animation.fadeOut
913
import androidx.compose.foundation.ExperimentalFoundationApi
1014
import androidx.compose.foundation.background
1115
import androidx.compose.foundation.border
16+
import androidx.compose.foundation.clickable
1217
import androidx.compose.foundation.combinedClickable
1318
import androidx.compose.foundation.gestures.detectDragGestures
1419
import androidx.compose.foundation.layout.Arrangement
@@ -61,6 +66,7 @@ import androidx.compose.ui.input.pointer.pointerInput
6166
import androidx.compose.ui.layout.onGloballyPositioned
6267
import androidx.compose.ui.layout.positionInRoot
6368
import androidx.compose.ui.platform.LocalClipboardManager
69+
import androidx.compose.ui.platform.LocalContext
6470
import androidx.compose.ui.platform.LocalDensity
6571
import androidx.compose.ui.res.painterResource
6672
import androidx.compose.ui.text.AnnotatedString
@@ -240,6 +246,7 @@ private fun SentryBuddyOverlayContent(
240246
BuddySheet(
241247
state = state,
242248
liveFeed = liveFeed,
249+
sentryUiLinks = controller.sentryUiLinks,
243250
nowMs = nowMs,
244251
onDispatch = { dispatch(it) },
245252
onAnalyze = { dispatchAnalysis { analyze() } },
@@ -472,6 +479,7 @@ internal class BuddyOverlayHitBounds {
472479
private fun BuddySheet(
473480
state: SentryBuddySessionState,
474481
liveFeed: BuddyLiveFeed,
482+
sentryUiLinks: BuddySentryUiLinks,
475483
nowMs: Long,
476484
onDispatch: (SentryBuddySessionController.() -> Unit) -> Unit,
477485
onAnalyze: () -> Unit,
@@ -491,7 +499,8 @@ private fun BuddySheet(
491499
verticalArrangement = Arrangement.spacedBy(16.dp),
492500
) {
493501
when (state) {
494-
SentryBuddySessionState.LiveFeed -> LiveFeedSheet(liveFeed, nowMs, onDispatch)
502+
SentryBuddySessionState.LiveFeed ->
503+
LiveFeedSheet(liveFeed, sentryUiLinks, nowMs, onDispatch)
495504
SentryBuddySessionState.Intro -> IntroSheet(onDispatch)
496505
is SentryBuddySessionState.StoppedSummary -> StoppedSummarySheet(state, onDispatch)
497506
is SentryBuddySessionState.Briefing -> BriefingSheet(state, onDispatch, onAnalyze)
@@ -556,11 +565,12 @@ private fun IntroSheet(onDispatch: (SentryBuddySessionController.() -> Unit) ->
556565
@Composable
557566
private fun LiveFeedSheet(
558567
liveFeed: BuddyLiveFeed,
568+
sentryUiLinks: BuddySentryUiLinks,
559569
nowMs: Long,
560570
onDispatch: (SentryBuddySessionController.() -> Unit) -> Unit,
561571
) {
562572
SheetTitle("Sentry Buddy", "Live Feed")
563-
AttentionCard(liveFeed, nowMs)
573+
AttentionCard(liveFeed, sentryUiLinks, nowMs)
564574
Button(
565575
modifier = Modifier.fillMaxWidth().height(56.dp),
566576
colors = ButtonDefaults.buttonColors(containerColor = BuddyPurple),
@@ -577,12 +587,12 @@ private fun LiveFeedSheet(
577587
if (liveFeed.items.isEmpty()) {
578588
EmptyLiveFeedCard()
579589
} else {
580-
LiveFeedRows(liveFeed.items, nowMs)
590+
LiveFeedRows(liveFeed.items, sentryUiLinks, nowMs)
581591
}
582592
}
583593

584594
@Composable
585-
private fun AttentionCard(liveFeed: BuddyLiveFeed, nowMs: Long) {
595+
private fun AttentionCard(liveFeed: BuddyLiveFeed, sentryUiLinks: BuddySentryUiLinks, nowMs: Long) {
586596
val item = liveFeed.latestAdverseItem
587597
if (item == null) {
588598
Card(border = CardDefaults.outlinedCardBorder()) {
@@ -602,7 +612,11 @@ private fun AttentionCard(liveFeed: BuddyLiveFeed, nowMs: Long) {
602612
}
603613

604614
val color = severityColor(item.severity)
615+
val context = LocalContext.current
616+
val link = sentryUiLinks.linkFor(item)
605617
Card(
618+
modifier =
619+
Modifier.clickable(enabled = link != null) { link?.let { openSentryLink(context, it) } },
606620
colors = CardDefaults.cardColors(containerColor = color.copy(alpha = 0.10f)),
607621
border = CardDefaults.outlinedCardBorder(),
608622
) {
@@ -625,6 +639,14 @@ private fun AttentionCard(liveFeed: BuddyLiveFeed, nowMs: Long) {
625639
fontWeight = FontWeight.Bold,
626640
)
627641
item.subtitle()?.let { Text(it, color = BuddyMuted) }
642+
if (link != null) {
643+
Text(
644+
"Open in Sentry",
645+
color = BuddyPurple,
646+
style = MaterialTheme.typography.labelMedium,
647+
fontWeight = FontWeight.Bold,
648+
)
649+
}
628650
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
629651
adverseCountChips(liveFeed).forEach { chip ->
630652
Surface(
@@ -658,12 +680,22 @@ private fun EmptyLiveFeedCard() {
658680
}
659681

660682
@Composable
661-
private fun LiveFeedRows(items: List<BuddyLiveFeedItem>, nowMs: Long) {
683+
private fun LiveFeedRows(
684+
items: List<BuddyLiveFeedItem>,
685+
sentryUiLinks: BuddySentryUiLinks,
686+
nowMs: Long,
687+
) {
688+
val context = LocalContext.current
662689
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
663690
items.forEach { item ->
664691
val color =
665692
if (item.adverse) severityColor(item.severity) else timelineColor(item.timelineItem)
666-
Card(border = CardDefaults.outlinedCardBorder()) {
693+
val link = sentryUiLinks.linkFor(item)
694+
Card(
695+
modifier =
696+
Modifier.clickable(enabled = link != null) { link?.let { openSentryLink(context, it) } },
697+
border = CardDefaults.outlinedCardBorder(),
698+
) {
667699
Row(
668700
modifier = Modifier.fillMaxWidth().padding(14.dp),
669701
horizontalArrangement = Arrangement.spacedBy(12.dp),
@@ -686,12 +718,22 @@ private fun LiveFeedRows(items: List<BuddyLiveFeedItem>, nowMs: Long) {
686718
item.subtitle()?.let {
687719
Text(it, color = BuddyMuted, style = MaterialTheme.typography.bodySmall)
688720
}
689-
Text(
690-
item.category.label,
691-
color = color,
692-
style = MaterialTheme.typography.labelMedium,
693-
fontWeight = FontWeight.Bold,
694-
)
721+
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
722+
Text(
723+
item.category.label,
724+
color = color,
725+
style = MaterialTheme.typography.labelMedium,
726+
fontWeight = FontWeight.Bold,
727+
)
728+
if (link != null) {
729+
Text(
730+
"Open in Sentry",
731+
color = BuddyPurple,
732+
style = MaterialTheme.typography.labelMedium,
733+
fontWeight = FontWeight.Bold,
734+
)
735+
}
736+
}
695737
}
696738
}
697739
}
@@ -1017,6 +1059,14 @@ private fun relativeTime(timestampMs: Long, nowMs: Long): String {
10171059
return "${ageSeconds / 60}m ago"
10181060
}
10191061

1062+
private fun openSentryLink(context: Context, link: String) {
1063+
try {
1064+
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
1065+
} catch (_: ActivityNotFoundException) {
1066+
// A debug overlay should not crash the app when no browser can handle the link.
1067+
}
1068+
}
1069+
10201070
private fun Map<String, Any?>.mapValue(key: String): Map<*, *> =
10211071
this[key] as? Map<*, *> ?: emptyMap<Any, Any>()
10221072

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ internal class BuddyActivityLifecycleCallbacks(
569569
overlayManager =
570570
BuddyOverlayManager(SentryBuddySessionController(flowAnalysesApi = options.flowAnalysesApi))
571571
}
572+
overlayManager?.updateOptions(options)
572573
}
573574

574575
fun detachAll() {
@@ -579,6 +580,10 @@ internal class BuddyActivityLifecycleCallbacks(
579580
internal class BuddyOverlayManager(private val controller: SentryBuddySessionController) {
580581
private val overlays = WeakHashMap<Activity, View>()
581582

583+
fun updateOptions(options: SentryBuddyOptions) {
584+
controller.sentryUiLinks = options.sentryUiLinks()
585+
}
586+
582587
fun attach(activity: Activity) {
583588
if (overlays.containsKey(activity)) {
584589
return
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.sentry.android.buddy
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import java.util.Date
5+
import kotlin.test.Test
6+
7+
class BuddySentryUiLinksTest {
8+
@Test
9+
fun `error item links to event id search`() {
10+
val links =
11+
BuddySentryUiLinks(baseUrl = "https://sentry-sdks.sentry.io/", projectId = "5428559")
12+
val item = liveFeedItem(BuddyLiveFeedItem.Category.ERROR, mapOf("event_id" to "abc123"))
13+
14+
assertThat(links.linkFor(item))
15+
.isEqualTo("https://sentry-sdks.sentry.io/issues/?project=5428559&query=event.id%3Aabc123")
16+
}
17+
18+
@Test
19+
fun `span item links to trace view`() {
20+
val links = BuddySentryUiLinks(baseUrl = "https://sentry-sdks.sentry.io", projectId = "5428559")
21+
val item =
22+
liveFeedItem(
23+
BuddyLiveFeedItem.Category.SLOW_SPAN,
24+
mapOf("trace_id" to "trace-id", "span_id" to "span-id"),
25+
)
26+
27+
assertThat(links.linkFor(item))
28+
.isEqualTo(
29+
"https://sentry-sdks.sentry.io/performance/trace/trace-id/?project=5428559&span=span-id"
30+
)
31+
}
32+
33+
@Test
34+
fun `organization slug provides default base url`() {
35+
val links = BuddySentryUiLinks(organizationSlug = "sentry-sdks", projectId = "5428559")
36+
val item = liveFeedItem(BuddyLiveFeedItem.Category.ERROR, mapOf("event_id" to "abc123"))
37+
38+
assertThat(links.linkFor(item))
39+
.isEqualTo("https://sentry-sdks.sentry.io/issues/?project=5428559&query=event.id%3Aabc123")
40+
}
41+
42+
@Test
43+
fun `missing config returns no link`() {
44+
val links = BuddySentryUiLinks(baseUrl = "https://sentry-sdks.sentry.io")
45+
val item = liveFeedItem(BuddyLiveFeedItem.Category.ERROR, mapOf("event_id" to "abc123"))
46+
47+
assertThat(links.linkFor(item)).isNull()
48+
}
49+
50+
@Test
51+
fun `screen item returns no link`() {
52+
val links = BuddySentryUiLinks(baseUrl = "https://sentry-sdks.sentry.io", projectId = "5428559")
53+
val item = liveFeedItem(BuddyLiveFeedItem.Category.SCREEN, emptyMap())
54+
55+
assertThat(links.linkFor(item)).isNull()
56+
}
57+
58+
private fun liveFeedItem(
59+
category: BuddyLiveFeedItem.Category,
60+
data: Map<String, Any?>,
61+
): BuddyLiveFeedItem =
62+
BuddyLiveFeedItem(
63+
id = 1,
64+
timelineItem = BuddyTimelineItem(BuddyTimelineItem.Type.EVENT, Date(0), 0, data = data),
65+
category = category,
66+
)
67+
}

0 commit comments

Comments
 (0)