Skip to content

Commit

Permalink
Demo app: Manual instrumentation (#577)
Browse files Browse the repository at this point in the history
* added emitting an event when the cart is cleared

* created a InstrumentedAstronomyShopNavController delegate class

* manually instrumented InstrumentedAstronomyShopNavController to emit some navigation events

* unused import
  • Loading branch information
magda-woj authored Sep 9, 2024
1 parent 87be431 commit e691739
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import androidx.compose.material3.*
import io.opentelemetry.android.demo.OtelDemoApplication


sealed class BottomNavItem(val route: String, val icon: ImageVector, val label: String) {
Expand All @@ -29,9 +30,9 @@ object MainDestinations {

@Composable
fun rememberAstronomyShopNavController(navController: NavHostController = rememberNavController())
: AstronomyShopNavController = remember(navController)
: InstrumentedAstronomyShopNavController = remember(navController)
{
AstronomyShopNavController(navController)
InstrumentedAstronomyShopNavController(AstronomyShopNavController(navController))
}

@Stable
Expand All @@ -52,7 +53,44 @@ class AstronomyShopNavController(
fun navigateToCheckoutInfo(){
navController.navigate(MainDestinations.CHECKOUT_INFO_ROUTE)
}
}

class InstrumentedAstronomyShopNavController(
private val delegate : AstronomyShopNavController
){
val navController: NavHostController
get() = delegate.navController

val currentRoute: String?
get() = delegate.currentRoute

fun upPress() {
delegate.upPress()
}

fun navigateToProductDetail(productId: String) {
delegate.navigateToProductDetail(productId)
generateNavigationEvent(
eventName = "navigate.to.product.details",
payload = mapOf("product.id" to productId)
)
}

fun navigateToCheckoutInfo() {
delegate.navigateToCheckoutInfo()
generateNavigationEvent(
eventName = "navigate.to.checkout.info",
payload = emptyMap()
)
}

private fun generateNavigationEvent(eventName: String, payload: Map<String, String>) {
val eventBuilder = OtelDemoApplication.eventBuilder("otel.demo.app.navigation", eventName)
payload.forEach { (key, value) ->
eventBuilder.put(key, value)
}
eventBuilder.emit()
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.opentelemetry.android.demo.shop.ui.products.ProductCard
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import io.opentelemetry.android.demo.OtelDemoApplication
import io.opentelemetry.android.demo.shop.clients.ProductCatalogClient
import io.opentelemetry.android.demo.shop.clients.RecommendationService
import io.opentelemetry.android.demo.shop.ui.products.RecommendedSection
Expand Down Expand Up @@ -41,7 +42,7 @@ fun CartScreen(
contentAlignment = Alignment.TopEnd
) {
OutlinedButton(
onClick = { cartViewModel.clearCart() },
onClick = { clearCart(cartViewModel) },
modifier = Modifier
) {
Text("Empty Cart", color = Color.Red)
Expand Down Expand Up @@ -90,3 +91,14 @@ fun CartScreen(
}
}
}

private fun clearCart(cartViewModel: CartViewModel) {
generateEmptiedCartEvent(cartViewModel)
cartViewModel.clearCart()
}

private fun generateEmptiedCartEvent(cartViewModel: CartViewModel) {
val eventBuilder = OtelDemoApplication.eventBuilder("otel.demo.app", "cart.emptied")
eventBuilder.put("cart.total.value", cartViewModel.getTotalPrice())
.emit()
}

0 comments on commit e691739

Please sign in to comment.