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: Event update #605

Merged
merged 11 commits into from
Sep 20, 2024
9 changes: 8 additions & 1 deletion demo-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ The OpenTelemetry Android Demo App currently supports the following features:

* Manual Instrumentation
- Provides access to the OpenTelemetry APIs for manual instrumentation, allowing developers to create custom spans and events as needed.
- Note: The only manual instrumentation that has been added to the demo app so far is an event after clicking on the OpenTelemetry logo.
- See `OtelDemoApplication.kt` for an example of a tracer and an event builder initialization.
- In the app, a custom span is emitted in `MainOtelButton.kt` after clicking on the OpenTelemetry logo button.
- Custom events are emitted:
- in `MainOtelButton.kt` after clicking on the OpenTelemetry logo button,
- in `Navigation.kt` for screen changes in the app,
- in `AstronomyShopActivity.kt` after placing an order in the shop,
- in `Cart.kt` after emptying a cart in the shop.
- Note: Events aren't visible in the Jaeger UI, only in the collector output.

### Known Gaps
As of now, there are a few areas where the instrumentation might not be comprehensive:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.opentelemetry.android.demo.shop.ui.products.ProductDetails
import io.opentelemetry.android.demo.shop.ui.products.ProductList
import io.opentelemetry.android.demo.shop.ui.cart.CartViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import io.opentelemetry.android.demo.OtelDemoApplication
import io.opentelemetry.android.demo.shop.ui.cart.CheckoutConfirmationScreen
import io.opentelemetry.android.demo.shop.ui.cart.CheckoutInfoViewModel
import io.opentelemetry.android.demo.shop.ui.cart.InfoScreen
Expand Down Expand Up @@ -107,7 +108,11 @@ fun AstronomyShopScreen() {
}
composable(MainDestinations.CHECKOUT_INFO_ROUTE) {
InfoScreen(
onPlaceOrderClick = {astronomyShopNavController.navigateToCheckoutConfirmation()},
onPlaceOrderClick = {instrumentedPlaceOrder(
astronomyShopNavController = astronomyShopNavController,
cartViewModel = cartViewModel,
checkoutInfoViewModel = checkoutInfoViewModel
)},
upPress = {astronomyShopNavController.upPress()},
checkoutInfoViewModel = checkoutInfoViewModel
)
Expand All @@ -123,3 +128,24 @@ fun AstronomyShopScreen() {
}
}
}

private fun instrumentedPlaceOrder(
astronomyShopNavController: InstrumentedAstronomyShopNavController,
cartViewModel: CartViewModel,
checkoutInfoViewModel: CheckoutInfoViewModel
){
generateOrderPlacedEvent(cartViewModel, checkoutInfoViewModel)
astronomyShopNavController.navigateToCheckoutConfirmation()
}

private fun generateOrderPlacedEvent(
cartViewModel: CartViewModel,
checkoutInfoViewModel: CheckoutInfoViewModel
) {
val eventBuilder = OtelDemoApplication.eventBuilder("otel.demo.app", "order.placed")
eventBuilder
.put("order.total.value", cartViewModel.getTotalPrice())
.put("buyer.state", checkoutInfoViewModel.shippingInfo.state)
.emit()
}