Commit a2566f9e authored by Sebastian Kaspari's avatar Sebastian Kaspari
Browse files

Issue #12731: Collect telemetry about the content process of tabs getting killed.

parent fc80dbfb
......@@ -4402,3 +4402,55 @@ contextual_menu:
notification_emails:
- fenix-core@mozilla.com
expires: "2021-06-01"
engine:
tab_kills:
type: labeled_counter
labels:
- foreground
- background
description: |
How often was the content process of a foreground (selected) or
background tab killed.
bugs:
- https://github.com/mozilla-mobile/android-components/issues/9366
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/17864
data_sensitivity:
- technical
notification_emails:
- fenix-core@mozilla.com
- skaspari@mozilla.com
expires: "2021-12-31"
kill_foreground_age:
type: timespan
time_unit: millisecond
description: |
Measures the age of the engine session of a foreground (selected) tab
at the time its content process got killed.
bugs:
- https://github.com/mozilla-mobile/android-components/issues/9366
data_reviews:
- TBD
data_sensitivity:
- technical
notification_emails:
- fenix-core@mozilla.com
- skaspari@mozilla.com
expires: "2021-12-31"
kill_background_age:
type: timespan
time_unit: millisecond
description: |
Measures the age of the engine session of a background tab at the
time its content process got killed.
bugs:
- https://github.com/mozilla-mobile/android-components/issues/9366
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/17864
data_sensitivity:
- technical
notification_emails:
- fenix-core@mozilla.com
- skaspari@mozilla.com
expires: "2021-12-31"
......@@ -8,17 +8,23 @@ import androidx.annotation.VisibleForTesting
import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.action.DownloadAction
import mozilla.components.browser.state.action.EngineAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.selector.findTab
import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.EngineState
import mozilla.components.browser.state.state.SessionState
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.support.base.android.Clock
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.search.telemetry.ads.AdsTelemetry
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.GleanMetrics.Engine as EngineMetrics
/**
* [Middleware] to record telemetry in response to [BrowserAction]s.
......@@ -90,6 +96,10 @@ class TelemetryMiddleware(
is DownloadAction.AddDownloadAction -> {
metrics.track(Event.DownloadAdded)
}
is EngineAction.KillEngineSessionAction -> {
val tab = context.state.findTabOrCustomTab(action.sessionId)
onEngineSessionKilled(context.state, tab)
}
}
next(action)
......@@ -112,4 +122,36 @@ class TelemetryMiddleware(
}
}
}
/**
* Collecting some engine-specific (GeckoView) telemetry.
* https://github.com/mozilla-mobile/android-components/issues/9366
*/
private fun onEngineSessionKilled(state: BrowserState, tab: SessionState?) {
if (tab == null) {
logger.debug("Could not find tab for killed engine session")
return
}
val isSelected = tab.id == state.selectedTabId
val ageNanos = tab.engineState.ageNanos()
// Increment the counter of killed foreground/background tabs
val tabKillLabel = if (isSelected) { "foreground" } else { "background" }
EngineMetrics.tabKills[tabKillLabel].add()
// Record the age of the engine session of the killed foreground/background tab.
if (isSelected && ageNanos != null) {
EngineMetrics.killForegroundAge.setRawNanos(ageNanos)
} else if (ageNanos != null) {
EngineMetrics.killBackgroundAge.setRawNanos(ageNanos)
}
}
}
@Suppress("MagicNumber")
private fun EngineState.ageNanos(): Long? {
val timestamp = (timestamp ?: return null)
val now = Clock.elapsedRealtime()
return (now - timestamp) * 1_000_000
}
......@@ -4,20 +4,28 @@
package org.mozilla.fenix
import androidx.test.core.app.ApplicationProvider
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import mozilla.components.browser.session.engine.EngineMiddleware
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.action.DownloadAction
import mozilla.components.browser.state.action.EngineAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.LoadRequestState
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.service.glean.testing.GleanTestRule
import mozilla.components.support.base.android.Clock
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
......@@ -30,6 +38,7 @@ import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.search.telemetry.ads.AdsTelemetry
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.GleanMetrics.Engine as EngineMetrics
@RunWith(FenixRobolectricTestRunner::class)
@ExperimentalCoroutinesApi
......@@ -45,8 +54,15 @@ class TelemetryMiddlewareTest {
@get:Rule
val coroutinesTestRule = MainCoroutineRule(testDispatcher)
@get:Rule
val gleanRule = GleanTestRule(ApplicationProvider.getApplicationContext())
private val clock = FakeClock()
@Before
fun setUp() {
Clock.delegate = clock
settings = Settings(testContext)
metrics = mockk(relaxed = true)
adsTelemetry = mockk()
......@@ -55,7 +71,15 @@ class TelemetryMiddlewareTest {
adsTelemetry,
metrics
)
store = BrowserStore(middleware = listOf(telemetryMiddleware))
store = BrowserStore(
middleware = listOf(telemetryMiddleware) + EngineMiddleware.create(engine = mockk(), sessionLookup = { null }),
initialState = BrowserState()
)
}
@After
fun tearDown() {
Clock.reset()
}
@Test
......@@ -244,4 +268,125 @@ class TelemetryMiddlewareTest {
verify { metrics.track(Event.DownloadAdded) }
}
@Test
fun `WHEN foreground tab getting killed THEN middleware counts it`() {
store.dispatch(TabListAction.RestoreAction(
listOf(
createTab("https://www.mozilla.org", id = "foreground"),
createTab("https://getpocket.com", id = "background_pocket"),
createTab("https://theverge.com", id = "background_verge")
),
selectedTabId = "foreground"
)).joinBlocking()
Assert.assertFalse(EngineMetrics.tabKills["foreground"].testHasValue())
Assert.assertFalse(EngineMetrics.tabKills["background"].testHasValue())
store.dispatch(
EngineAction.KillEngineSessionAction("foreground")
).joinBlocking()
Assert.assertTrue(EngineMetrics.tabKills["foreground"].testHasValue())
}
@Test
fun `WHEN background tabs getting killed THEN middleware counts it`() {
store.dispatch(TabListAction.RestoreAction(
listOf(
createTab("https://www.mozilla.org", id = "foreground"),
createTab("https://getpocket.com", id = "background_pocket"),
createTab("https://theverge.com", id = "background_verge")
),
selectedTabId = "foreground"
)).joinBlocking()
Assert.assertFalse(EngineMetrics.tabKills["foreground"].testHasValue())
Assert.assertFalse(EngineMetrics.tabKills["background"].testHasValue())
store.dispatch(
EngineAction.KillEngineSessionAction("background_pocket")
).joinBlocking()
Assert.assertFalse(EngineMetrics.tabKills["foreground"].testHasValue())
Assert.assertTrue(EngineMetrics.tabKills["background"].testHasValue())
assertEquals(1, EngineMetrics.tabKills["background"].testGetValue())
store.dispatch(
EngineAction.KillEngineSessionAction("background_verge")
).joinBlocking()
Assert.assertFalse(EngineMetrics.tabKills["foreground"].testHasValue())
Assert.assertTrue(EngineMetrics.tabKills["background"].testHasValue())
assertEquals(2, EngineMetrics.tabKills["background"].testGetValue())
}
@Test
fun `WHEN foreground tab gets killed THEN middleware records foreground age`() {
store.dispatch(TabListAction.RestoreAction(
listOf(
createTab("https://www.mozilla.org", id = "foreground"),
createTab("https://getpocket.com", id = "background_pocket"),
createTab("https://theverge.com", id = "background_verge")
),
selectedTabId = "foreground"
)).joinBlocking()
clock.elapsedTime = 100
store.dispatch(EngineAction.LinkEngineSessionAction(
sessionId = "foreground",
engineSession = mock()
)).joinBlocking()
Assert.assertFalse(EngineMetrics.killForegroundAge.testHasValue())
Assert.assertFalse(EngineMetrics.killBackgroundAge.testHasValue())
clock.elapsedTime = 500
store.dispatch(
EngineAction.KillEngineSessionAction("foreground")
).joinBlocking()
Assert.assertTrue(EngineMetrics.killForegroundAge.testHasValue())
Assert.assertFalse(EngineMetrics.killBackgroundAge.testHasValue())
assertEquals(400, EngineMetrics.killForegroundAge.testGetValue())
}
@Test
fun `WHEN background tab gets killed THEN middleware records background age`() {
store.dispatch(TabListAction.RestoreAction(
listOf(
createTab("https://www.mozilla.org", id = "foreground"),
createTab("https://getpocket.com", id = "background_pocket"),
createTab("https://theverge.com", id = "background_verge")
),
selectedTabId = "foreground"
)).joinBlocking()
clock.elapsedTime = 100
store.dispatch(EngineAction.LinkEngineSessionAction(
sessionId = "background_pocket",
engineSession = mock()
)).joinBlocking()
clock.elapsedTime = 700
Assert.assertFalse(EngineMetrics.killForegroundAge.testHasValue())
Assert.assertFalse(EngineMetrics.killBackgroundAge.testHasValue())
store.dispatch(
EngineAction.KillEngineSessionAction("background_pocket")
).joinBlocking()
Assert.assertTrue(EngineMetrics.killBackgroundAge.testHasValue())
Assert.assertFalse(EngineMetrics.killForegroundAge.testHasValue())
assertEquals(600, EngineMetrics.killBackgroundAge.testGetValue())
}
}
private class FakeClock : Clock.Delegate {
var elapsedTime: Long = 0
override fun elapsedRealtime(): Long = elapsedTime
}
......@@ -280,6 +280,9 @@ The following metrics are added to the ping:
| browser.search.ad_clicks |[labeled_counter](https://mozilla.github.io/glean/book/user/metrics/labeled_counters.html) |Records clicks of adverts on SERP pages. The key format is ‘<provider-name>’. |[1](https://github.com/mozilla-mobile/fenix/pull/10112), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |
| browser.search.in_content |[labeled_counter](https://mozilla.github.io/glean/book/user/metrics/labeled_counters.html) |Records the type of interaction a user has on SERP pages. |[1](https://github.com/mozilla-mobile/fenix/pull/10167), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |
| browser.search.with_ads |[labeled_counter](https://mozilla.github.io/glean/book/user/metrics/labeled_counters.html) |Records counts of SERP pages with adverts displayed. The key format is ‘<provider-name>’. |[1](https://github.com/mozilla-mobile/fenix/pull/10112), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |
| engine.kill_background_age |[timespan](https://mozilla.github.io/glean/book/user/metrics/timespan.html) |Measures the age of the engine session of a background tab at the time its content process got killed. |[1](https://github.com/mozilla-mobile/fenix/pull/17864)||2021-12-31 |1 |
| engine.kill_foreground_age |[timespan](https://mozilla.github.io/glean/book/user/metrics/timespan.html) |Measures the age of the engine session of a foreground (selected) tab at the time its content process got killed. |[1](TBD)||2021-12-31 |1 |
| engine.tab_kills |[labeled_counter](https://mozilla.github.io/glean/book/user/metrics/labeled_counters.html) |How often was the content process of a foreground (selected) or background tab killed. |[1](https://github.com/mozilla-mobile/fenix/pull/17864)|<ul><li>foreground</li><li>background</li></ul>|2021-12-31 |1 |
| events.total_uri_count |[counter](https://mozilla.github.io/glean/book/user/metrics/counter.html) |A counter of URIs visited by the user in the current session, including page reloads. This does not include background page requests and URIs from embedded pages or private browsing but may be incremented without user interaction by website scripts that programmatically redirect to a new location. |[1](https://github.com/mozilla-mobile/fenix/pull/1785), [2](https://github.com/mozilla-mobile/fenix/pull/8314), [3](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |
| metrics.adjust_ad_group |[string](https://mozilla.github.io/glean/book/user/metrics/string.html) |A string containing the Adjust ad group ID from which the user installed Fenix. This will not send on the first session the user runs. If the install is organic, this will be empty. |[1](https://github.com/mozilla-mobile/fenix/pull/9253), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |
| metrics.adjust_campaign |[string](https://mozilla.github.io/glean/book/user/metrics/string.html) |A string containing the Adjust campaign ID from which the user installed Fenix. This will not send on the first session the user runs. If the install is organic, this will be empty. |[1](https://github.com/mozilla-mobile/fenix/pull/5579), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |1 |
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment