Skip to content

Commit

Permalink
Adds experiments to simple app
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarik Eshaq committed Jul 23, 2020
1 parent 8e856ef commit 2a55e46
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 31 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import static org.gradle.api.tasks.testing.TestResult.ResultType

buildscript {
repositories {
mavenLocal()
if (project.hasProperty("googleRepo")) {
maven {
name "Google"
Expand Down Expand Up @@ -48,6 +49,7 @@ plugins {

allprojects {
repositories {
mavenLocal()
if (project.hasProperty("googleRepo")) {
maven {
name "Google"
Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
}

repositories {
mavenLocal()
jcenter()
}

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Versions {

const val mozilla_appservices = "61.0.10"

const val mozilla_glean = "31.4.1"
const val mozilla_glean = "31.4.1-TESTING29"

const val material = "1.1.0"
const val nearby = "17.0.0"
Expand Down
1 change: 1 addition & 0 deletions components/browser/engine-gecko-beta/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
1 change: 1 addition & 0 deletions components/browser/engine-gecko-nightly/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
1 change: 1 addition & 0 deletions components/browser/engine-gecko/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
1 change: 1 addition & 0 deletions components/lib/crash/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
5 changes: 3 additions & 2 deletions components/service/glean/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ dependencies {
implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines
implementation Dependencies.androidx_work_runtime

api GLEAN_LIBRARY
api (GLEAN_LIBRARY) {
exclude group: 'org.mozilla.components', module: 'concept-fetch'
}

// So consumers can set a HTTP client.
api project(':concept-fetch')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mozilla.components.service.glean

import android.content.Context
import kotlinx.coroutines.*
import mozilla.telemetry.glean.Experiments as ExperimentsCore

object Experiments {
private var inner: ExperimentsCore = ExperimentsCore
private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO) + job
fun initialize(applicationContext: Context, dbPath: String) {
inner.initialize(applicationContext, dbPath)
}
fun getBranch(experimentName: String): String {
return inner.getBranch(experimentName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mozilla.components.service.glean


import mozilla.components.concept.fetch.Client
import mozilla.telemetry.glean.RustHttpConfig as HttpConfig

/**
* An object allowing configuring the HTTP client used by Rust code.
*/
object RustHttpConfig {

/**
* Set the HTTP client to be used by all Rust code.
*
* The `Lazy`'s value is not read until the first request is made.
*
* This must be called
* - after initializing a megazord for users using a custom megazord build.
* - before any other calls into application-services rust code which make HTTP requests.
*/
fun setClient(c: Lazy<Client>) {
HttpConfig.setClient(c)
}
}
1 change: 1 addition & 0 deletions components/support/migration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
1 change: 1 addition & 0 deletions components/support/sync-telemetry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
2 changes: 2 additions & 0 deletions components/tooling/glean-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {
}

repositories {
mavenLocal()
mavenCentral()
}

Expand Down Expand Up @@ -67,6 +68,7 @@ publishing {
}

repositories {
mavenLocal()
maven {
url = "$buildDir/maven"
}
Expand Down
1 change: 1 addition & 0 deletions samples/glean/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
1 change: 1 addition & 0 deletions samples/glean/samples-glean-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,66 @@ package org.mozilla.samples.glean

import android.app.Application
import android.content.Intent
import kotlinx.coroutines.*
import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient
import mozilla.components.service.experiments.Configuration as ExperimentsConfig
import mozilla.components.service.glean.Glean
import mozilla.components.service.glean.Experiments as Exp
import mozilla.components.service.glean.config.Configuration
import mozilla.components.service.glean.net.ConceptFetchHttpUploader
import mozilla.components.service.experiments.Experiments
import mozilla.components.service.glean.RustHttpConfig
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.sink.AndroidLogSink
import org.mozilla.samples.glean.GleanMetrics.Basic
import org.mozilla.samples.glean.GleanMetrics.Test
import org.mozilla.samples.glean.GleanMetrics.Custom
import org.mozilla.samples.glean.GleanMetrics.Pings
import kotlin.coroutines.CoroutineContext


class GleanApplication : Application() {

override fun onCreate() {
super.onCreate()

// We want the log messages of all builds to go to Android logcat
Log.addSink(AndroidLogSink())

// Register the sample application's custom pings.
Glean.registerPings(Pings)

// Initialize the Glean library. Ideally, this is the first thing that
// must be done right after enabling logging.
val client by lazy { HttpURLConnectionClient() }
val httpClient = ConceptFetchHttpUploader.fromClient(client)
val config = Configuration(httpClient = httpClient)
Glean.initialize(applicationContext, uploadEnabled = true, configuration = config)

// Initialize the Experiments library and pass in the callback that will generate a
// broadcast Intent to signal the application that experiments have been updated. This is
// only relevant to the experiments library, aside from recording the experiment in Glean.
Experiments.initialize(applicationContext, ExperimentsConfig(httpClient = client)) {
val intent = Intent()
intent.action = "org.mozilla.samples.glean.experiments.updated"
sendBroadcast(intent)
}
super.onCreate()
// We want the log messages of all builds to go to Android logcat
Log.addSink(AndroidLogSink())

// Register the sample application's custom pings.
Glean.registerPings(Pings)

Test.timespan.start()
// Initialize the Glean library. Ideally, this is the first thing that
// must be done right after enabling logging.
val client by lazy { HttpURLConnectionClient() }
val httpClient = ConceptFetchHttpUploader.fromClient(client)
val config = Configuration(httpClient = httpClient)
Glean.initialize(applicationContext, uploadEnabled = true, configuration = config)
RustHttpConfig.setClient(lazy { HttpURLConnectionClient() })
Thread {
GlobalScope.launch {
var exp = mozilla.components.service.glean.Experiments
exp.initialize(applicationContext, applicationContext.dataDir.path)
val res = exp.getBranch("button-color")
println(res)
//TODO
// Send an enrolled event for the experiment
// Send a saw button-color button. AKA "User saw some branch of the experiment"
}
}.start()
// Initialize the Experiments library and pass in the callback that will generate a
// broadcast Intent to signal the application that experiments have been updated. This is
// only relevant to the experiments library, aside from recording the experiment in Glean.
Experiments.initialize(applicationContext, ExperimentsConfig(httpClient = client)) {
val intent = Intent()
intent.action = "org.mozilla.samples.glean.experiments.updated"
sendBroadcast(intent)
}

Custom.counter.add()
Test.timespan.start()

// Set a sample value for a metric.
Basic.os.set("Android")
Custom.counter.add()

// Set a sample value for a metric.
Basic.os.set("Android")
}
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.yaml.snakeyaml.Yaml

buildscript {
repositories {
mavenLocal()
jcenter()
}

Expand Down

0 comments on commit 2a55e46

Please sign in to comment.