Skip to content

Commit

Permalink
add enrollment_status metric
Browse files Browse the repository at this point in the history
  • Loading branch information
jeddai committed Oct 5, 2023
1 parent 9308fff commit 37f12b8
Show file tree
Hide file tree
Showing 25 changed files with 673 additions and 39 deletions.
166 changes: 162 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/nimbus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ unicode-segmentation = "1.8.0"
error-support = { path = "../support/error" }
remote_settings = { path = "../remote_settings", optional = true }
cfg-if = "1.0.0"
glean = "54.0.0"

[build-dependencies]
uniffi = { version = "0.24.1", features = ["build"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ import mozilla.telemetry.glean.Glean
import org.json.JSONObject
import org.mozilla.experiments.nimbus.GleanMetrics.NimbusEvents
import org.mozilla.experiments.nimbus.GleanMetrics.NimbusHealth
import org.mozilla.experiments.nimbus.internal.AppContext
import org.mozilla.experiments.nimbus.internal.*
import org.mozilla.experiments.nimbus.internal.AvailableExperiment
import org.mozilla.experiments.nimbus.internal.AvailableRandomizationUnits
import org.mozilla.experiments.nimbus.internal.EnrolledExperiment
import org.mozilla.experiments.nimbus.internal.EnrollmentChangeEvent
import org.mozilla.experiments.nimbus.internal.EnrollmentChangeEventType
import org.mozilla.experiments.nimbus.internal.NimbusClient
import org.mozilla.experiments.nimbus.internal.NimbusClientInterface
import org.mozilla.experiments.nimbus.internal.NimbusException
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -77,6 +71,29 @@ open class Nimbus(

private val logger = delegate.logger

private val metricsHandler = object : HostMetricsHandler {
override fun recordEnrollmentStatus(enrollmentStatusExtra: EnrollmentStatusExtraDef) {
NimbusEvents.enrollmentStatus.record(
NimbusEvents.EnrollmentStatusExtra(
branch = enrollmentStatusExtra.branch,
slug = enrollmentStatusExtra.slug,
status = enrollmentStatusExtra.status,
reason = enrollmentStatusExtra.reason,
errorString = enrollmentStatusExtra.errorString,
conflictSlug = enrollmentStatusExtra.conflictSlug,
)
)
}

override fun getEnrollmentStatusRecords(): List<EnrollmentStatusExtraDef> {
TODO("Not yet implemented")
}

override fun clear() {
TODO("Not yet implemented")
}
}

private val nimbusClient: NimbusClientInterface

override var globalUserParticipation: Boolean
Expand Down Expand Up @@ -118,6 +135,7 @@ open class Nimbus(
// The "dummy" field here is required for obscure reasons when generating code on desktop,
// so we just automatically set it to a dummy value.
AvailableRandomizationUnits(clientId = null, userId = null, nimbusId = null, dummy = 0),
metricsHandler,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mozilla.experiments.nimbus.GleanMetrics.NimbusEvents
import org.mozilla.experiments.nimbus.GleanMetrics.NimbusHealth
import org.mozilla.experiments.nimbus.internal.EnrollmentChangeEvent
import org.mozilla.experiments.nimbus.internal.EnrollmentChangeEventType
import org.mozilla.experiments.nimbus.internal.*
import org.robolectric.RobolectricTestRunner
import java.util.Calendar
import java.util.concurrent.Executors
Expand Down Expand Up @@ -644,6 +643,31 @@ class NimbusTests {

assertTrue(observed)
}

@Test
fun `Nimbus records EnrollmentStatus metrics`() {
suspend fun getString(): String {
return testExperimentsJsonString(appInfo, packageName)
}

val job = nimbus.applyLocalExperiments(::getString)
runBlocking {
job.join()
}

assertEquals(1, nimbus.getAvailableExperiments().size)
assertNotNull("Event must have a value", NimbusEvents.enrollmentStatus.testGetValue())
val enrollmentStatusEvents = NimbusEvents.enrollmentStatus.testGetValue()!!
assertEquals("Event count must match", enrollmentStatusEvents.count(), 1)

val enrolledExtra = enrollmentStatusEvents[0].extra!!
assertEquals("branch must match", "test-branch", enrolledExtra["branch"])
assertEquals("slug must match", "test-experiment", enrolledExtra["slug"])
assertEquals("status must match", "Enrolled", enrolledExtra["status"])
assertEquals("reason must match", "Qualified", enrolledExtra["reason"])
assertEquals("errorString must match", null, enrolledExtra["error_string"])
assertEquals("conflictSlug must match", null, enrolledExtra["conflict_slug"])
}
}

// Mocking utilities, from mozilla.components.support.test
Expand Down
Loading

0 comments on commit 37f12b8

Please sign in to comment.