Skip to content

Commit

Permalink
Fixing logRnView (#924)
Browse files Browse the repository at this point in the history
* Remove unused force flag from changeView in ViewDataSource

* Remove unused timestamp parameter from logView in EmbraceBreadcrumbService

* Removed isAutomaticActivityCaptureEnabled config check from the ViewDataSource creation

We only need to check for this when we call it from the ActivityLifecycleTracker. It was conflicting with the logRnView method.

* Fixed LogRNView and added tests

If you call logRnView with the same view name, a new span will be created with the same view name.

* Brought back timestamps for logView as we will use them later
  • Loading branch information
priettt committed Jun 6, 2024
1 parent d46c29e commit bca00d2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.embrace.android.embracesdk.Embrace
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.arch.schema.EmbType
import io.embrace.android.embracesdk.findSpanAttribute
import io.embrace.android.embracesdk.findSpanSnapshotsOfType
import io.embrace.android.embracesdk.findSpansOfType
import io.embrace.android.embracesdk.internal.ApkToolsConfig
import io.embrace.android.embracesdk.internal.clock.nanosToMillis
Expand Down Expand Up @@ -139,7 +140,68 @@ internal class ReactNativeInternalInterfaceTest {
assertEquals("value", span.findSpanAttribute("emb.properties.key"))
assertEquals(1000, span.startTimeNanos.nanosToMillis())
assertEquals(5000, span.endTimeNanos.nanosToMillis())
}
}

/*
* The first view is logged and stored as a span, because we know that it ends when logRnView is called again.
* The second view is logged as a span snapshot, because we know that it ends when the session ends.
* */
@Test
fun `react native log RN view`() {
with(testRule) {
val message = checkNotNull(harness.recordSession {
embrace.reactNativeInternalInterface?.logRnView("HomeScreen")
harness.overriddenClock.tick(1000)
embrace.reactNativeInternalInterface?.logRnView("DetailsScreen")
})

val spans = message.findSpansOfType(EmbType.Ux.View)
assertEquals(1, spans.size)

val spanSnapshots = message.findSpanSnapshotsOfType(EmbType.Ux.View)
assertEquals(1, spanSnapshots.size)

val firstSpan = spans.single()
val secondSpan = spanSnapshots.single()

assertEquals("emb-screen-view", firstSpan.name)
assertEquals("emb-screen-view", secondSpan.name)
assertEquals("ux.view", firstSpan.findSpanAttribute("emb.type"))
assertEquals("ux.view", secondSpan.findSpanAttribute("emb.type"))
assertEquals("HomeScreen", firstSpan.findSpanAttribute("view.name"))
assertEquals("DetailsScreen", secondSpan.findSpanAttribute("view.name"))
}
}

/*
* The first view is logged and stored as a span, because we know that it ends when logRnView is called again.
* The second view is logged as a span snapshot, because we know that it ends when the session ends.
* */
@Test
fun `react native log RN view same name`() {
with(testRule) {
val message = checkNotNull(harness.recordSession {
embrace.reactNativeInternalInterface?.logRnView("HomeScreen")
harness.overriddenClock.tick(1000)
embrace.reactNativeInternalInterface?.logRnView("HomeScreen")
})

val spans = message.findSpansOfType(EmbType.Ux.View)
assertEquals(1, spans.size)

val spanSnapshots = message.findSpanSnapshotsOfType(EmbType.Ux.View)
assertEquals(1, spanSnapshots.size)

val firstSpan = spans.single()
val secondSpan = spanSnapshots.single()

assertEquals("emb-screen-view", firstSpan.name)
assertEquals("emb-screen-view", secondSpan.name)
assertEquals("ux.view", firstSpan.findSpanAttribute("emb.type"))
assertEquals("ux.view", secondSpan.findSpanAttribute("emb.type"))
assertEquals("HomeScreen", firstSpan.findSpanAttribute("view.name"))
assertEquals("HomeScreen", secondSpan.findSpanAttribute("view.name"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class EmbraceBreadcrumbService(
) : BreadcrumbService, ActivityLifecycleListener, MemoryCleanerListener {

override fun logView(screen: String?, timestamp: Long) {
dataSourceModuleProvider()?.viewDataSource?.dataSource?.changeView(screen, false)
dataSourceModuleProvider()?.viewDataSource?.dataSource?.changeView(screen)
}

override fun startView(name: String?): Boolean {
Expand Down Expand Up @@ -107,7 +107,7 @@ internal class EmbraceBreadcrumbService(
}

override fun onView(activity: Activity) {
if (configService.breadcrumbBehavior.isActivityBreadcrumbCaptureEnabled()) {
if (configService.breadcrumbBehavior.isAutomaticActivityCaptureEnabled()) {
logView(activity.javaClass.name, clock.now())
}
}
Expand All @@ -116,10 +116,9 @@ internal class EmbraceBreadcrumbService(
* Close all open fragments when the activity closes
*/
override fun onViewClose(activity: Activity) {
if (!configService.breadcrumbBehavior.isActivityBreadcrumbCaptureEnabled()) {
return
if (configService.breadcrumbBehavior.isAutomaticActivityCaptureEnabled()) {
dataSourceModuleProvider()?.viewDataSource?.dataSource?.onViewClose()
}
dataSourceModuleProvider()?.viewDataSource?.dataSource?.onViewClose()
}

override fun cleanCollections() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ internal class ViewDataSource(
/**
* Called when a view is started, ending the last view.
*/
fun changeView(name: String?, force: Boolean) {
fun changeView(name: String?) {
val lastView = viewSpans.keys.lastOrNull()
if (force || name.equals(lastView, ignoreCase = true)) {
endView(lastView)
}
endView(lastView)
startView(name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class BreadcrumbBehavior(
/**
* Controls whether activity lifecycle changes are captured in breadcrumbs
*/
fun isActivityBreadcrumbCaptureEnabled() =
fun isAutomaticActivityCaptureEnabled() =
local?.viewConfig?.enableAutomaticActivityCapture
?: ENABLE_AUTOMATIC_ACTIVITY_CAPTURE_DEFAULT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ internal class DataSourceModuleImpl(
otelModule.spanService,
initModule.logger
)
},
configGate = { configService.breadcrumbBehavior.isActivityBreadcrumbCaptureEnabled() }
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class BreadcrumbBehaviorTest {
assertEquals(100, getWebViewBreadcrumbLimit())
assertEquals(100, getFragmentBreadcrumbLimit())
assertTrue(isTapCoordinateCaptureEnabled())
assertTrue(isActivityBreadcrumbCaptureEnabled())
assertTrue(isAutomaticActivityCaptureEnabled())
assertTrue(isWebViewBreadcrumbCaptureEnabled())
assertTrue(isQueryParamCaptureEnabled())
assertFalse(isCaptureFcmPiiDataEnabled())
Expand All @@ -51,7 +51,7 @@ internal class BreadcrumbBehaviorTest {
fun testLocalOnly() {
with(fakeBreadcrumbBehavior(localCfg = { local })) {
assertFalse(isTapCoordinateCaptureEnabled())
assertFalse(isActivityBreadcrumbCaptureEnabled())
assertFalse(isAutomaticActivityCaptureEnabled())
assertFalse(isWebViewBreadcrumbCaptureEnabled())
assertFalse(isQueryParamCaptureEnabled())
assertTrue(isCaptureFcmPiiDataEnabled())
Expand Down

0 comments on commit bca00d2

Please sign in to comment.