Skip to content

Commit

Permalink
proof of concept composable instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaHaddara committed Dec 12, 2024
1 parent e74b747 commit 2e11d22
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand All @@ -52,6 +54,8 @@ enum class PlaygroundTab(
VIEW_INSTRUMENTATION("Render", Icons.Outlined.Straighten, Icons.Filled.Straighten),
}

val LocalOtelComposition = compositionLocalOf<OpenTelemetryRum?> { null }

/**
* An activity with various UI elements that cause telemetry to be emitted.
*/
Expand All @@ -66,16 +70,18 @@ class MainActivity : ComponentActivity() {
setContent {
val currentTab = remember { mutableStateOf(PlaygroundTab.CORE) }

HoneycombOpenTelemetryAndroidTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
bottomBar = { NavBar(currentTab) },
) { innerPadding ->
Playground(
otelRum,
currentTab,
modifier = Modifier.padding(innerPadding),
)
CompositionLocalProvider(LocalOtelComposition provides otelRum) {
HoneycombOpenTelemetryAndroidTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
bottomBar = { NavBar(currentTab) },
) { innerPadding ->
Playground(
otelRum,
currentTab,
modifier = Modifier.padding(innerPadding),
)
}
}
}
}
Expand All @@ -92,7 +98,10 @@ fun Playground(
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier.fillMaxSize().padding(20.dp),
modifier =
modifier
.fillMaxSize()
.padding(20.dp),
) {
Text(
text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,35 @@ import androidx.compose.ui.tooling.preview.Preview
import io.honeycomb.opentelemetry.android.example.ui.theme.HoneycombOpenTelemetryAndroidTheme
import java.math.BigDecimal
import java.math.RoundingMode
import kotlin.time.measureTime

@Composable
private fun HoneycombInstrumentedComposable(
name: String,
composable: @Composable (() -> Unit),
) {
val tracer = LocalOtelComposition.current!!.openTelemetry.tracerProvider.tracerBuilder("ViewInstrumentationPlayground").build()
val span = tracer.spanBuilder("Render").setAttribute("view.name", name).startSpan()
span.makeCurrent()

val duration =
measureTime {
composable()
}

// renderDuration is in seconds
// calling duration.inWholeSeconds would lose precision
span.setAttribute("view.renderDuration", duration.inWholeMicroseconds / 1_000_000.toDouble())

span.end()
}

@Composable
private fun NestedExpensiveView(delayMs: Long) {
Row {
Text(text = timeConsumingCalculation(delayMs))
HoneycombInstrumentedComposable("nested expensive text") {
Text(text = timeConsumingCalculation(delayMs))
}
}
}

Expand Down Expand Up @@ -50,11 +74,21 @@ private fun ExpensiveView() {
modifier = Modifier.fillMaxWidth(),
) {
DelayedSlider(delay = delay, onValueChange = setDelay)
Text(text = timeConsumingCalculation(delay))
Text(text = timeConsumingCalculation(delay))
Text(text = timeConsumingCalculation(delay))
NestedExpensiveView(delayMs = delay)
Text(text = timeConsumingCalculation(delay))
HoneycombInstrumentedComposable("expensive text 1") {
Text(text = timeConsumingCalculation(delay))
}
HoneycombInstrumentedComposable("expensive text 2") {
Text(text = timeConsumingCalculation(delay))
}
HoneycombInstrumentedComposable("expensive text 3") {
Text(text = timeConsumingCalculation(delay))
}
HoneycombInstrumentedComposable("nested expensive composable") {
NestedExpensiveView(delayMs = delay)
}
HoneycombInstrumentedComposable("expensive text 4") {
Text(text = timeConsumingCalculation(delay))
}
}
}

Expand Down

0 comments on commit 2e11d22

Please sign in to comment.