Repository navigation
Expand file tree
/
Copy pathSentryTracedTest.kt
More file actions
544 lines (471 loc) · 17.2 KB
/
Copy pathSentryTracedTest.kt
File metadata and controls
544 lines (471 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package io.sentry.compose
import android.app.Application
import android.content.ComponentName
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import io.sentry.ISpan
import io.sentry.ITransaction
import io.sentry.NoOpSpan
import io.sentry.Sentry
import io.sentry.SentryOptions
import io.sentry.SpanStatus
import io.sentry.TransactionOptions
import kotlin.test.assertFailsWith
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.junit.runner.RunWith
import org.robolectric.Shadows
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@OptIn(ExperimentalComposeUiApi::class)
@RunWith(AndroidJUnit4::class)
@Config(sdk = [30])
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class SentryTracedTest {
// Register ComponentActivity explicitly so Robolectric can launch createAndComposeRule.
// Context: https://github.com/robolectric/robolectric/pull/4736#issuecomment-1831034882
@get:Rule(order = 1)
val addActivityToRobolectricRule =
object : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val appContext: Application = ApplicationProvider.getApplicationContext()
Shadows.shadowOf(appContext.packageManager)
.addActivityIfNotPresent(
ComponentName(appContext.packageName, ComponentActivity::class.java.name)
)
}
}
@get:Rule(order = 2) val rule = createAndroidComposeRule<ComponentActivity>()
@After
fun tearDown() {
rule.runOnUiThread { Sentry.close() }
Sentry.close()
}
@Test
fun `records a composition span for the initial composition`() {
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(tx) { SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp)) } }
}
tx.waitForSpanCount(OP_COMPOSE, 1)
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(1)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
assertThat(
tx.singleSpan(OP_PARENT_COMPOSITION).startDate.isAfter(tx.singleSpan(OP_COMPOSE).startDate)
)
.isFalse()
}
@Test
fun `falls back to the current transaction when no owner span is provided via LocalSentrySpan`() {
val tx = initSentryAndStartTransaction("tx")
rule.setContent { SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp)) } }
tx.waitForSpanCount(OP_COMPOSE, 1)
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(1)
}
@Test
fun `renders content without spans when no live owner span is available`() {
rule.runOnUiThread { Sentry.close() }
rule.setContent {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp).testTag("content")) }
}
rule.waitForIdle()
rule.onNodeWithTag("content").assertExists()
}
@Test
fun `renders content without spans when provided owner span is a no-op`() {
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(NoOpSpan.getInstance()) {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp).testTag("content")) }
}
}
rule.waitForIdle()
rule.onNodeWithTag("content").assertExists()
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(0)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(0)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
assertThat(tx.countSpans(OP_RENDER)).isEqualTo(0)
}
@Test
fun `renders content without spans when provided owner span is finished`() {
val tx = initSentryAndStartTransaction("tx")
rule.runOnUiThread { tx.finish() }
rule.setContent {
ProvideSentrySpan(tx) {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp).testTag("content")) }
}
}
rule.waitForIdle()
rule.onNodeWithTag("content").assertExists()
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(0)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(0)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
assertThat(tx.countSpans(OP_RENDER)).isEqualTo(0)
}
@Test
fun `does not create spans when origin is ignored`() {
val tx =
initSentryAndStartTransaction("tx") { options ->
options.setIgnoredSpanOrigins(listOf(OP_TRACE_ORIGIN))
}
rule.setContent {
ProvideSentrySpan(tx) {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp).testTag("content")) }
}
}
rule.waitForIdle()
drawContent()
rule.onNodeWithTag("content").assertExists()
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(0)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(0)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
assertThat(tx.countSpans(OP_RENDER)).isEqualTo(0)
}
@Test
fun `sibling traced composables with the same owner share the composition parent`() {
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(tx) {
Column {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp)) }
SentryTraced(tag = "add_to_cart_button") { Box(Modifier.size(1.dp)) }
}
}
}
tx.waitForSpanCount(OP_COMPOSE, 2)
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
}
@Test
fun `sibling traced composables with the same owner share the render parent`() {
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(tx) {
Column {
SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp)) }
SentryTraced(tag = "add_to_cart_button") { Box(Modifier.size(1.dp)) }
}
}
}
tx.waitForSpanCount(OP_COMPOSE, 2)
drawContent()
tx.waitForSpanCount(OP_RENDER, 2)
val renderParent = tx.singleSpan(OP_PARENT_RENDER)
val renderSpans = tx.spans.filter { it.operation == OP_RENDER }
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(1)
assertThat(renderSpans).hasSize(2)
renderSpans.forEach { renderSpan ->
assertThat(renderParent.startDate.isAfter(renderSpan.startDate)).isFalse()
}
assertThat(renderSpans.map { it.parentSpanId })
.containsExactly(
renderParent.spanContext.spanId,
renderParent.spanContext.spanId,
)
}
@Test
fun `records at most one composition span and one render span per owner span`() {
var step by mutableStateOf(0)
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(tx) {
val currentStep = step
SentryTraced(tag = "product_info") {
Box(Modifier.size((currentStep + 1).dp).testTag("content-$currentStep"))
}
}
}
tx.waitForSpanCount(OP_COMPOSE, 1)
drawContent()
tx.waitForSpanCount(OP_RENDER, 1)
rule.runOnIdle { step = 1 }
rule.waitForIdle()
drawContent()
rule.waitForIdle()
rule.onNodeWithTag("content-1").assertExists()
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(1)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(1)
assertThat(tx.countSpans(OP_RENDER)).isEqualTo(1)
}
@Test
fun `preserves content state after tracing completes`() {
var step by mutableStateOf(0)
var rememberedInstanceCount = 0
var disposeCount = 0
var rememberedState: Any? = null
val tx = initSentryAndStartTransaction("tx")
rule.setContent {
ProvideSentrySpan(tx) {
val currentStep = step
SentryTraced(tag = "product_info") {
val state = remember {
rememberedInstanceCount++
Any()
}
DisposableEffect(Unit) { onDispose { disposeCount++ } }
rememberedState = state
Box(Modifier.size((currentStep + 1).dp).testTag("content-$currentStep"))
}
}
}
tx.waitForSpanCount(OP_COMPOSE, 1)
drawContent()
tx.waitForSpanCount(OP_RENDER, 1)
val firstRememberedState = rememberedState
rule.runOnIdle { step = 1 }
rule.waitForIdle()
drawContent()
rule.waitForIdle()
rule.onNodeWithTag("content-1").assertExists()
assertThat(rememberedInstanceCount).isEqualTo(1)
assertThat(disposeCount).isEqualTo(0)
assertThat(rememberedState).isSameInstanceAs(firstRememberedState)
}
@Test
fun `starts recording once an owner span becomes available`() {
var step by mutableStateOf(0)
var sentrySpan by mutableStateOf<ISpan>(NoOpSpan.getInstance())
rule.runOnUiThread { Sentry.close() }
rule.setContent {
ProvideSentrySpan(sentrySpan) {
val currentStep = step
key(currentStep) {
SentryTraced(tag = "late-transaction-$currentStep") { Box(Modifier.size(1.dp)) }
}
}
}
rule.waitForIdle()
val tx = initSentryAndStartTransaction("tx")
rule.runOnIdle {
sentrySpan = tx
step = 1
}
rule.waitForIdle()
tx.waitForSpanCount(OP_COMPOSE, 1)
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(tx.countSpans(OP_COMPOSE)).isEqualTo(1)
}
@Test
fun `records spans under replacement owner after previous owner finishes`() {
var step by mutableStateOf(0)
val firstTx = initSentryAndStartTransaction("first-tx")
var sentrySpan by mutableStateOf<ISpan>(firstTx)
rule.setContent {
ProvideSentrySpan(sentrySpan) {
val currentStep = step
key(currentStep) {
SentryTraced(tag = "transaction-$currentStep") { Box(Modifier.size(1.dp)) }
}
}
}
firstTx.waitForSpanCount(OP_COMPOSE, 1)
lateinit var secondTx: ITransaction
rule.runOnUiThread {
firstTx.finish()
secondTx = startBoundTransaction("second-tx")
}
rule.runOnIdle {
sentrySpan = secondTx
step = 1
}
rule.waitForIdle()
secondTx.waitForSpanCount(OP_COMPOSE, 1)
assertThat(firstTx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_COMPOSE)).isEqualTo(1)
}
@Test
fun `records a new span group when the owner span changes for the same composable node`() {
val firstTx = initSentryAndStartTransaction("first-tx")
var sentrySpan by mutableStateOf<ISpan>(firstTx)
rule.setContent {
ProvideSentrySpan(sentrySpan) {
SentryTraced(tag = "transaction") { Box(Modifier.size(1.dp)) }
}
}
firstTx.waitForSpanCount(OP_COMPOSE, 1)
drawContent()
firstTx.waitForSpanCount(OP_RENDER, 1)
lateinit var secondTx: ITransaction
rule.runOnUiThread {
firstTx.finish()
secondTx = startBoundTransaction("second-tx")
}
rule.runOnIdle { sentrySpan = secondTx }
rule.waitForIdle()
secondTx.waitForSpanCount(OP_COMPOSE, 1)
drawContent()
secondTx.waitForSpanCount(OP_RENDER, 1)
assertThat(firstTx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(firstTx.countSpans(OP_PARENT_RENDER)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_COMPOSE)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_PARENT_RENDER)).isEqualTo(1)
assertThat(secondTx.countSpans(OP_RENDER)).isEqualTo(1)
}
@Test
fun `failed composition does not emit parent spans`() {
val tx = initSentryAndStartTransaction("tx")
assertFailsWith<IllegalStateException> {
rule.setContent {
ProvideSentrySpan(tx) { SentryTraced(tag = "throws") { error("boom") } }
}
}
assertThat(tx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(0)
assertThat(tx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
}
@Test
fun `render spans use the owner captured during composition`() {
val placeContent = mutableStateOf(false)
val compositionTx = initSentryAndStartTransaction("composition-tx")
rule.setContent {
Layout(
content = {
SentryTraced(
tag = "traced",
modifier = Modifier.fillMaxSize(),
enableUserInteractionTracing = false,
) {}
}
) { measurables, constraints ->
val placeable = measurables.single().measure(constraints)
layout(placeable.width, placeable.height) {
if (placeContent.value) {
placeable.place(0, 0)
}
}
}
}
rule.waitForIdle()
assertThat(compositionTx.countSpans(OP_PARENT_COMPOSITION)).isEqualTo(1)
assertThat(compositionTx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
lateinit var renderingTx: ITransaction
rule.runOnUiThread {
renderingTx = startBoundTransaction("rendering-tx")
placeContent.value = true
}
rule.waitForIdle()
drawContent()
val renderingParent = compositionTx.spans.single { it.operation == OP_PARENT_RENDER }
val renderSpan = compositionTx.spans.single { it.operation == OP_RENDER }
assertThat(renderingParent.parentSpanId).isEqualTo(compositionTx.spanContext.spanId)
assertThat(renderSpan.parentSpanId).isEqualTo(renderingParent.spanContext.spanId)
assertThat(renderSpan.spanContext.origin).isEqualTo(OP_TRACE_ORIGIN)
assertThat(renderingTx.countSpans(OP_PARENT_RENDER)).isEqualTo(0)
assertThat(renderingTx.countSpans(OP_RENDER)).isEqualTo(0)
}
@Test
fun `an idle transaction hosting a traced composable finishes after the idle timeout`() {
val tx =
initSentryAndStartTransaction(
"tx",
configureTransactionOptions = { transactionOptions ->
transactionOptions.idleTimeout = 1_000
transactionOptions.isWaitForChildren = true
},
)
rule.setContent {
ProvideSentrySpan(tx) { SentryTraced(tag = "product_info") { Box(Modifier.size(1.dp)) } }
}
tx.waitForSpanCount(OP_COMPOSE, 1)
rule.waitUntil(timeoutMillis = 5_000) { tx.isFinished }
assertThat(tx.status).isEqualTo(SpanStatus.OK)
assertThat(tx.singleSpan(OP_PARENT_COMPOSITION).isFinished).isTrue()
}
private fun initSentryAndStartTransaction(
name: String,
configureTransactionOptions: (TransactionOptions) -> Unit = {},
configureOptions: (SentryOptions) -> Unit = {},
): ITransaction {
lateinit var tx: ITransaction
rule.runOnUiThread {
Sentry.init(
{ options: SentryOptions ->
options.dsn = "https://key@sentry.io/proj"
options.tracesSampleRate = 1.0
configureOptions(options)
},
true,
)
tx = startBoundTransaction(name, configureTransactionOptions)
var boundTransaction: ITransaction? = null
Sentry.configureScope { boundTransaction = it.transaction }
assertThat(boundTransaction).isSameInstanceAs(tx)
}
return tx
}
private fun startBoundTransaction(
name: String,
configureTransactionOptions: (TransactionOptions) -> Unit = {},
): ITransaction {
val transactionOptions =
TransactionOptions().apply {
isBindToScope = true
configureTransactionOptions(this)
}
val transaction = Sentry.startTransaction(name, "test", transactionOptions)
Sentry.configureScope { it.setTransaction(transaction) }
return transaction
}
private fun ITransaction.waitForSpanCount(operation: String, count: Int) {
rule.waitUntil(timeoutMillis = 5_000) { countSpans(operation) >= count }
}
private fun ITransaction.countSpans(operation: String): Int = spans.count {
it.operation == operation
}
private fun ITransaction.singleSpan(operation: String): ISpan = spans.single {
it.operation == operation
}
private fun drawContent() {
rule.runOnUiThread {
val rootView = rule.activity.findViewById<View>(android.R.id.content)
val bitmap =
Bitmap.createBitmap(
rootView.width.coerceAtLeast(1),
rootView.height.coerceAtLeast(1),
Bitmap.Config.ARGB_8888,
)
try {
rootView.draw(Canvas(bitmap))
} finally {
bitmap.recycle()
}
}
}
private companion object {
private const val OP_PARENT_COMPOSITION = "ui.compose.composition"
private const val OP_COMPOSE = "ui.compose"
private const val OP_PARENT_RENDER = "ui.compose.rendering"
private const val OP_RENDER = "ui.render"
private const val OP_TRACE_ORIGIN = "auto.ui.jetpack_compose"
}
}