generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract HoneycombInstrumentedComposable to our distro
include compose compile options
- Loading branch information
1 parent
295d9cb
commit 7c45b19
Showing
4 changed files
with
69 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/main/java/io/honeycomb/opentelemetry/android/HoneycombInstrumentedComposable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.honeycomb.opentelemetry.android | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.SideEffect | ||
import io.opentelemetry.api.OpenTelemetry | ||
import java.time.Instant | ||
import kotlin.time.TimeSource.Monotonic.markNow | ||
|
||
/** | ||
* Heavily inspired by https://github.com/theapache64/boil/blob/master/files/LogComposition.kt | ||
*/ | ||
@Composable | ||
fun HoneycombInstrumentedComposable( | ||
name: String, | ||
otelRum: OpenTelemetry, | ||
composable: @Composable (() -> Unit), | ||
) { | ||
val tracer = otelRum.tracerProvider.tracerBuilder("io.honeycomb.render-instrumentation").build() | ||
val span = | ||
tracer | ||
.spanBuilder("View Render") | ||
.setAttribute("view.name", name) | ||
.startSpan() | ||
|
||
span.makeCurrent().use { | ||
val bodySpan = | ||
tracer | ||
.spanBuilder("View Body") | ||
.setAttribute("view.name", name) | ||
.startSpan() | ||
|
||
bodySpan.makeCurrent().use { | ||
val start = markNow() | ||
composable() | ||
val endTime = Instant.now() | ||
|
||
val bodyDuration = start.elapsedNow() | ||
// bodyDuration is in seconds | ||
// calling duration.inWholeSeconds would lose precision | ||
span.setAttribute("view.renderDuration", bodyDuration.inWholeMicroseconds / 1_000_000.toDouble()) | ||
|
||
SideEffect { | ||
bodySpan.end(endTime) | ||
val renderDuration = start.elapsedNow() | ||
span.setAttribute("view.totalDuration", renderDuration.inWholeMicroseconds / 1_000_000.toDouble()) | ||
span.end() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters