Skip to content
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

Demo App crash #584

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions demo-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ The OpenTelemetry Android Demo App currently supports the following features:
- Provides detailed insights into each lifecycle phase.
- Can be observed in the "About OpenTelemetry Android" activity, entered via "Learn more" on the main screen.

* Crash Reporting
- Automatically detects and reports a crash of the application.
- In order to crash the demo app, try to add to cart exactly 10 National Park Foundation Explorascopes (first product on the list after clicking "Go shopping") and click "Yes, I'm sure." on the alert pop-up. This will cause a multi-threaded crash of the app.
- Note: The crash is reported as an event and isn't visible in the Jaeger UI, only in the collector output.

* ANR Detection
- Automatically detects and reports ANRs in the app.
- ANR events are captured as spans with detailed stack traces, providing insights into the exact operations that caused the ANR.
Expand All @@ -53,9 +58,6 @@ The OpenTelemetry Android Demo App currently supports the following features:
### Known Gaps
As of now, there are a few areas where the instrumentation might not be comprehensive:

* Crash Reporting
App crashes are automatically reported, but the app currently does not include any features that intentionally trigger crashes.

* HTTP Client Instrumentation
OpenTelemetry Android supports automatic instrumentation for HTTP client libraries. This feature captures spans for HTTP requests with details. However, the demo app does not currently demonstrate this feature as it doesn't make any network requests.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.opentelemetry.android.demo.shop.ui.components

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable

@Composable
fun ConfirmCrashPopup(
onConfirm: () -> Unit,
onDismiss: () -> Unit
) {

AlertDialog(
onDismissRequest = { onDismiss() },
title = {
Text(text = "Are you sure?")
},
text = {
Text(text = "This will crash the app.")
},
confirmButton = {
TextButton(onClick = { onConfirm() }) {
Text(text = "Yes, I'm sure")
}
},
dismissButton = {
TextButton(onClick = { onDismiss() }) {
Text(text = "No, go back")
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import io.opentelemetry.android.demo.shop.ui.components.UpPressButton
import androidx.compose.ui.Alignment
import io.opentelemetry.android.demo.shop.clients.ProductCatalogClient
import io.opentelemetry.android.demo.shop.clients.RecommendationService
import io.opentelemetry.android.demo.shop.ui.components.ConfirmCrashPopup
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

@Composable
fun ProductDetails(
Expand Down Expand Up @@ -83,14 +86,7 @@ fun ProductDetails(
Spacer(modifier = Modifier.height(32.dp))
QuantityChooser(quantity = quantity, onQuantityChange = { quantity = it })
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = { cartViewModel.addProduct(product, quantity) },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(text = "Add to Cart")
}
AddToCartButton(cartViewModel = cartViewModel, product = product, quantity = quantity)
Spacer(modifier = Modifier.height(32.dp))
RecommendedSection(recommendedProducts = recommendedProducts, onProductClick = onProductClick)
}
Expand All @@ -104,4 +100,68 @@ fun ProductDetails(
}
}

@Composable
fun AddToCartButton(
cartViewModel: CartViewModel,
product: Product,
quantity: Int
) {

var showPopup by remember { mutableStateOf(false) }

Button(
onClick = {
if (product.id == "OLJCESPC7Z" && quantity == 10) {
showPopup = true
} else {
cartViewModel.addProduct(product, quantity)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(text = "Add to Cart")
}

if (showPopup) {
ConfirmCrashPopup(
onConfirm = {
multiThreadCrashing()
},
onDismiss = {
showPopup = false
}
)
}
}

fun multiThreadCrashing(numThreads : Int = 4) {
val latch = CountDownLatch(1)

for (i in 0..numThreads) {
val thread = Thread {
try {
if (latch.await(10, TimeUnit.SECONDS)) {
throw IllegalStateException("Failure from thread ${Thread.currentThread().name}")
}
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
}
thread.name = "crash-thread-$i"
thread.start()
}

try {
Thread.sleep(100)
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
return
}
latch.countDown()
}