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: Manual instrumentation #577

Merged
merged 4 commits into from
Sep 9, 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
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()
}