Skip to content

Commit

Permalink
Drop the Glean handle and move state into glean-core (#664)
Browse files Browse the repository at this point in the history
Drop the Glean handle and move state into glean-core
  • Loading branch information
badboy authored Jan 23, 2020
2 parents 19af88c + 90a4540 commit 896a2a6
Show file tree
Hide file tree
Showing 59 changed files with 603 additions and 803 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* **Deprecation Warning** Since `locale` is now in the `client_info` section, the one
in the baseline ping ([`glean.baseline.locale`](https://github.com/mozilla/glean/blob/c261205d6e84d2ab39c50003a8ffc3bd2b763768/glean-core/metrics.yaml#L28-L42))
is redundant and will be removed by the end of the quarter.
* Drop the Glean handle and move state into glean-core ([#664](https://github.com/mozilla/glean/pull/664))
* Android:
* Collections performed before initialization (preinit tasks) are now dispatched off
the main thread during initialization.
Expand Down
42 changes: 17 additions & 25 deletions glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import mozilla.telemetry.glean.config.FfiConfiguration
import mozilla.telemetry.glean.utils.getLocaleTag
import java.io.File
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.MetricHandle
import mozilla.telemetry.glean.rust.getAndConsumeRustString
import mozilla.telemetry.glean.rust.toBoolean
import mozilla.telemetry.glean.rust.toByte
Expand Down Expand Up @@ -55,8 +54,7 @@ open class GleanInternalAPI internal constructor () {
internal const val GLEAN_DATA_DIR: String = "glean_data"
}

// `internal` so this can be modified for testing
internal var handle: MetricHandle = 0L
private var initialized: Boolean = false

internal lateinit var configuration: Configuration

Expand Down Expand Up @@ -152,10 +150,10 @@ open class GleanInternalAPI internal constructor () {
delayPingLifetimeIO = false
)

handle = LibGleanFFI.INSTANCE.glean_initialize(cfg)
initialized = LibGleanFFI.INSTANCE.glean_initialize(cfg).toBoolean()

// If initialization of Glean fails we bail out and don't initialize further.
if (handle == 0L) {
if (!initialized) {
return
}

Expand All @@ -166,17 +164,15 @@ open class GleanInternalAPI internal constructor () {
// If this is the first time ever the Glean SDK runs, make sure to set
// some initial core metrics in case we need to generate early pings.
// The next times we start, we would have them around already.
val isFirstRun = LibGleanFFI.INSTANCE.glean_is_first_run(handle).toBoolean()
val isFirstRun = LibGleanFFI.INSTANCE.glean_is_first_run().toBoolean()
if (isFirstRun) {
initializeCoreMetrics(applicationContext)
}

// Deal with any pending events so we can start recording new ones
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.executeTask {
val pingSubmitted = LibGleanFFI.INSTANCE.glean_on_ready_to_submit_pings(
this@GleanInternalAPI.handle
).toBoolean()
val pingSubmitted = LibGleanFFI.INSTANCE.glean_on_ready_to_submit_pings().toBoolean()
if (pingSubmitted) {
PingUploadWorker.enqueueWorker(applicationContext)
}
Expand All @@ -194,7 +190,7 @@ open class GleanInternalAPI internal constructor () {
if (!isFirstRun) {
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.executeTask {
LibGleanFFI.INSTANCE.glean_clear_application_lifetime_metrics(handle)
LibGleanFFI.INSTANCE.glean_clear_application_lifetime_metrics()
initializeCoreMetrics(applicationContext)
}
}
Expand All @@ -220,7 +216,7 @@ open class GleanInternalAPI internal constructor () {
* Returns true if the Glean SDK has been initialized.
*/
internal fun isInitialized(): Boolean {
return handle != 0L
return initialized
}

/**
Expand Down Expand Up @@ -259,7 +255,7 @@ open class GleanInternalAPI internal constructor () {
// therefore need to obtain the lock from the PingUploader so that
// iterating over and deleting the pings doesn't happen at the same time.
synchronized(PingUploadWorker.pingQueueLock) {
LibGleanFFI.INSTANCE.glean_set_upload_enabled(handle, enabled.toByte())
LibGleanFFI.INSTANCE.glean_set_upload_enabled(enabled.toByte())

// Cancel any pending workers here so that we don't accidentally upload or
// collect data after the upload has been disabled.
Expand Down Expand Up @@ -290,7 +286,7 @@ open class GleanInternalAPI internal constructor () {
*/
fun getUploadEnabled(): Boolean {
if (isInitialized()) {
return LibGleanFFI.INSTANCE.glean_is_upload_enabled(handle).toBoolean()
return LibGleanFFI.INSTANCE.glean_is_upload_enabled().toBoolean()
} else {
return uploadEnabled
}
Expand Down Expand Up @@ -332,7 +328,6 @@ open class GleanInternalAPI internal constructor () {
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_set_experiment_active(
handle,
experimentId,
branch,
keys,
Expand All @@ -352,7 +347,7 @@ open class GleanInternalAPI internal constructor () {
// initialized, it doesn't get ignored and will be replayed after init.
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_set_experiment_inactive(handle, experimentId)
LibGleanFFI.INSTANCE.glean_set_experiment_inactive(experimentId)
}
}

Expand All @@ -367,7 +362,7 @@ open class GleanInternalAPI internal constructor () {
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()

return LibGleanFFI.INSTANCE.glean_experiment_test_is_active(handle, experimentId).toBoolean()
return LibGleanFFI.INSTANCE.glean_experiment_test_is_active(experimentId).toBoolean()
}

/**
Expand Down Expand Up @@ -398,7 +393,6 @@ open class GleanInternalAPI internal constructor () {
Dispatchers.API.assertInTestingMode()

val ptr = LibGleanFFI.INSTANCE.glean_experiment_test_get_data(
handle,
experimentId
)!!

Expand Down Expand Up @@ -473,7 +467,7 @@ open class GleanInternalAPI internal constructor () {
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
internal fun testCollect(ping: PingType): String? {
return LibGleanFFI.INSTANCE.glean_ping_collect(handle, ping.handle)?.getAndConsumeRustString()
return LibGleanFFI.INSTANCE.glean_ping_collect(ping.handle)?.getAndConsumeRustString()
}

/**
Expand Down Expand Up @@ -553,7 +547,6 @@ open class GleanInternalAPI internal constructor () {
val pingArray = StringArray(pingNames.toTypedArray(), "utf-8")
val pingArrayLen = pingNames.size
val submittedPing = LibGleanFFI.INSTANCE.glean_submit_pings_by_name(
handle,
pingArray,
pingArrayLen
).toBoolean()
Expand Down Expand Up @@ -594,7 +587,7 @@ open class GleanInternalAPI internal constructor () {

if (isInitialized() && clearStores) {
// Clear all the stored data.
LibGleanFFI.INSTANCE.glean_test_clear_all_stores(handle)
LibGleanFFI.INSTANCE.glean_test_clear_all_stores()
}

isMainProcess = null
Expand Down Expand Up @@ -633,12 +626,12 @@ open class GleanInternalAPI internal constructor () {
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
internal fun testDestroyGleanHandle() {
if (!isInitialized()) {
// We don't need to destroy the Glean handle: it wasn't initialized.
// We don't need to destroy Glean: it wasn't initialized.
return
}

LibGleanFFI.INSTANCE.glean_destroy_glean(handle)
handle = 0L
LibGleanFFI.INSTANCE.glean_destroy_glean()
initialized = false
}

/**
Expand All @@ -648,7 +641,6 @@ open class GleanInternalAPI internal constructor () {
internal fun registerPingType(pingType: PingType) {
if (this.isInitialized()) {
LibGleanFFI.INSTANCE.glean_register_ping_type(
handle,
pingType.handle
)
}
Expand All @@ -668,7 +660,7 @@ open class GleanInternalAPI internal constructor () {
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
internal fun testHasPingType(pingName: String): Boolean {
return LibGleanFFI.INSTANCE.glean_test_has_ping_type(handle, pingName).toBoolean()
return LibGleanFFI.INSTANCE.glean_test_has_ping_type(pingName).toBoolean()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package mozilla.telemetry.glean.private

import androidx.annotation.VisibleForTesting
import com.sun.jna.StringArray
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.toByte

Expand Down Expand Up @@ -70,7 +69,7 @@ class BooleanMetricType internal constructor(

@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_boolean_set(Glean.handle, this@BooleanMetricType.handle, value.toByte())
LibGleanFFI.INSTANCE.glean_boolean_set(this@BooleanMetricType.handle, value.toByte())
}
}

Expand All @@ -87,7 +86,6 @@ class BooleanMetricType internal constructor(
}

LibGleanFFI.INSTANCE.glean_boolean_set(
Glean.handle,
this@BooleanMetricType.handle,
value.toByte()
)
Expand All @@ -108,7 +106,7 @@ class BooleanMetricType internal constructor(
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()

val res = LibGleanFFI.INSTANCE.glean_boolean_test_has_value(Glean.handle, this.handle, pingName)
val res = LibGleanFFI.INSTANCE.glean_boolean_test_has_value(this.handle, pingName)
return res.toBoolean()
}

Expand All @@ -130,6 +128,6 @@ class BooleanMetricType internal constructor(
if (!testHasValue(pingName)) {
throw NullPointerException()
}
return LibGleanFFI.INSTANCE.glean_boolean_test_get_value(Glean.handle, this.handle, pingName).toBoolean()
return LibGleanFFI.INSTANCE.glean_boolean_test_get_value(this.handle, pingName).toBoolean()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package mozilla.telemetry.glean.private
import androidx.annotation.VisibleForTesting
import com.sun.jna.StringArray
import mozilla.telemetry.glean.Dispatchers
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.toBoolean
import mozilla.telemetry.glean.rust.toByte
Expand Down Expand Up @@ -73,7 +72,6 @@ class CounterMetricType internal constructor(
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_counter_add(
Glean.handle,
this@CounterMetricType.handle,
amount)
}
Expand All @@ -92,7 +90,6 @@ class CounterMetricType internal constructor(
}

LibGleanFFI.INSTANCE.glean_counter_add(
Glean.handle,
this@CounterMetricType.handle,
amount
)
Expand All @@ -113,7 +110,7 @@ class CounterMetricType internal constructor(
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()

val res = LibGleanFFI.INSTANCE.glean_counter_test_has_value(Glean.handle, this.handle, pingName)
val res = LibGleanFFI.INSTANCE.glean_counter_test_has_value(this.handle, pingName)
return res.toBoolean()
}

Expand All @@ -135,7 +132,7 @@ class CounterMetricType internal constructor(
if (!testHasValue(pingName)) {
throw NullPointerException()
}
return LibGleanFFI.INSTANCE.glean_counter_test_get_value(Glean.handle, this.handle, pingName)
return LibGleanFFI.INSTANCE.glean_counter_test_get_value(this.handle, pingName)
}

/**
Expand All @@ -153,7 +150,7 @@ class CounterMetricType internal constructor(
Dispatchers.API.assertInTestingMode()

return LibGleanFFI.INSTANCE.glean_counter_test_get_num_recorded_errors(
Glean.handle, this.handle, errorType.ordinal, pingName
this.handle, errorType.ordinal, pingName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package mozilla.telemetry.glean.private
import androidx.annotation.VisibleForTesting
import com.sun.jna.StringArray
import mozilla.telemetry.glean.Dispatchers
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.getAndConsumeRustString
import mozilla.telemetry.glean.rust.toBoolean
Expand Down Expand Up @@ -90,7 +89,6 @@ data class CustomDistributionMetricType(
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_custom_distribution_accumulate_samples(
Glean.handle,
this@CustomDistributionMetricType.handle,
samples,
samples.size
Expand All @@ -112,7 +110,7 @@ data class CustomDistributionMetricType(
Dispatchers.API.assertInTestingMode()

return LibGleanFFI
.INSTANCE.glean_custom_distribution_test_has_value(Glean.handle, this.handle, pingName)
.INSTANCE.glean_custom_distribution_test_has_value(this.handle, pingName)
.toBoolean()
}

Expand All @@ -135,7 +133,6 @@ data class CustomDistributionMetricType(
}

val ptr = LibGleanFFI.INSTANCE.glean_custom_distribution_test_get_value_as_json_string(
Glean.handle,
this.handle,
pingName)!!

Expand All @@ -157,7 +154,7 @@ data class CustomDistributionMetricType(
Dispatchers.API.assertInTestingMode()

return LibGleanFFI.INSTANCE.glean_custom_distribution_test_get_num_recorded_errors(
Glean.handle, this.handle, errorType.ordinal, pingName
this.handle, errorType.ordinal, pingName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import java.util.Calendar
import java.util.Date
import java.util.concurrent.TimeUnit as AndroidTimeUnit
import mozilla.telemetry.glean.Dispatchers
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.getAndConsumeRustString
import mozilla.telemetry.glean.rust.toBoolean
Expand Down Expand Up @@ -86,7 +85,6 @@ class DatetimeMetricType internal constructor(
}

LibGleanFFI.INSTANCE.glean_datetime_set(
Glean.handle,
this@DatetimeMetricType.handle,
year = cal.get(Calendar.YEAR),
month = cal.get(Calendar.MONTH) + 1,
Expand Down Expand Up @@ -118,7 +116,6 @@ class DatetimeMetricType internal constructor(
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
LibGleanFFI.INSTANCE.glean_datetime_set(
Glean.handle,
this@DatetimeMetricType.handle,
year = value.get(Calendar.YEAR),
month = value.get(Calendar.MONTH) + 1,
Expand Down Expand Up @@ -150,7 +147,7 @@ class DatetimeMetricType internal constructor(
Dispatchers.API.assertInTestingMode()

return LibGleanFFI
.INSTANCE.glean_datetime_test_has_value(Glean.handle, this.handle, pingName)
.INSTANCE.glean_datetime_test_has_value(this.handle, pingName)
.toBoolean()
}

Expand All @@ -175,7 +172,7 @@ class DatetimeMetricType internal constructor(
}
val ptr = LibGleanFFI
.INSTANCE
.glean_datetime_test_get_value_as_string(Glean.handle, this.handle, pingName)!!
.glean_datetime_test_get_value_as_string(this.handle, pingName)!!
return ptr.getAndConsumeRustString()
}

Expand Down Expand Up @@ -213,7 +210,7 @@ class DatetimeMetricType internal constructor(
Dispatchers.API.assertInTestingMode()

return LibGleanFFI.INSTANCE.glean_datetime_test_get_num_recorded_errors(
Glean.handle, this.handle, errorType.ordinal, pingName
this.handle, errorType.ordinal, pingName
)
}
}
Loading

0 comments on commit 896a2a6

Please sign in to comment.