Skip to content
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

[DO NOT MERGE] Adds experiments to simple app #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Adds experiments to simple app
Tarik Eshaq committed Jul 23, 2020
commit 2a55e46713154ed2c4897280033587de2c628c10
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import static org.gradle.api.tasks.testing.TestResult.ResultType

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

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

repositories {
mavenLocal()
jcenter()
}

2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions components/browser/engine-gecko-beta/build.gradle
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

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

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

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

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
5 changes: 3 additions & 2 deletions components/service/glean/build.gradle
Original file line number Diff line number Diff line change
@@ -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')
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
@@ -4,6 +4,7 @@

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

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

repositories {
mavenLocal()
mavenCentral()
}

@@ -67,6 +68,7 @@ publishing {
}

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

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

buildscript {
repositories {
mavenLocal()
maven {
url "https://maven.mozilla.org/maven2"
}
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a TODO here for us would be something like:

  • Send an enrolled event for the experiment
  • Send a saw button-color button. AKA "User saw some branch of the experiment"

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
@@ -6,6 +6,7 @@ import org.yaml.snakeyaml.Yaml

buildscript {
repositories {
mavenLocal()
jcenter()
}