Skip to content

Implement sampling #4896

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

Merged
merged 6 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ internal constructor(
backgroundDispatcher: CoroutineDispatcher,
transportFactoryProvider: Provider<TransportFactory>,
) {
private val sessionGenerator = SessionGenerator(collectEvents = true)
private val sessionSettings = SessionsSettings(firebaseApp.applicationContext)
private val sessionGenerator = SessionGenerator(collectEvents = shouldCollectEvents())
private val eventGDTLogger = EventGDTLogger(transportFactoryProvider)
private val sessionCoordinator =
SessionCoordinator(firebaseInstallations, backgroundDispatcher, eventGDTLogger)
private val sessionSettings = SessionsSettings(firebaseApp.applicationContext)
private val timeProvider: TimeProvider = Time()

init {
Expand All @@ -61,12 +61,22 @@ internal constructor(

private fun initiateSessionStart() {
val sessionDetails = sessionGenerator.generateNewSession()
val sessionEvent =
SessionEvents.startSession(firebaseApp, sessionDetails, sessionSettings, timeProvider)

if (sessionDetails.collectEvents) {
sessionCoordinator.attemptLoggingSessionEvent(sessionEvent)
if (!sessionGenerator.collectEvents) {
Log.d(TAG, "Sessions SDK has sampled this session")
return
}

sessionCoordinator.attemptLoggingSessionEvent(
SessionEvents.startSession(firebaseApp, sessionDetails, sessionSettings, timeProvider)
)
}

/** Calculate whether we should sample events using [sessionSettings] data. */
private fun shouldCollectEvents(): Boolean {
// Sampling rate of 1 means we do not sample.
val randomValue = Math.random()
return randomValue <= sessionSettings.samplingRate
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import java.util.UUID
internal data class SessionDetails(
val sessionId: String,
val firstSessionId: String,
val collectEvents: Boolean,
val sessionIndex: Int,
)

Expand All @@ -37,7 +36,7 @@ internal data class SessionDetails(
* @hide
*/
internal class SessionGenerator(
private val collectEvents: Boolean,
val collectEvents: Boolean,
private val uuidGenerator: () -> UUID = UUID::randomUUID
) {
private val firstSessionId = generateSessionId()
Expand All @@ -54,7 +53,6 @@ internal class SessionGenerator(
SessionDetails(
sessionId = if (sessionIndex == 0) firstSessionId else generateSessionId(),
firstSessionId,
collectEvents,
sessionIndex,
)
return currentSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class SessionGeneratorTest {
SessionDetails(
sessionId = SESSION_ID_1,
firstSessionId = SESSION_ID_1,
collectEvents = true,
sessionIndex = 0,
)
)
Expand All @@ -94,7 +93,6 @@ class SessionGeneratorTest {
SessionDetails(
sessionId = SESSION_ID_1,
firstSessionId = SESSION_ID_1,
collectEvents = true,
sessionIndex = 0,
)
)
Expand All @@ -110,7 +108,6 @@ class SessionGeneratorTest {
SessionDetails(
sessionId = SESSION_ID_2,
firstSessionId = SESSION_ID_1,
collectEvents = true,
sessionIndex = 1,
)
)
Expand All @@ -126,7 +123,6 @@ class SessionGeneratorTest {
SessionDetails(
sessionId = SESSION_ID_3,
firstSessionId = SESSION_ID_1,
collectEvents = true,
sessionIndex = 2,
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal object TestSessionEventData {
SessionDetails(
sessionId = "a1b2c3",
firstSessionId = "a1a1a1",
collectEvents = true,
sessionIndex = 3,
)

Expand Down