From 12e48c8f7cad0ee87e340dafff6f3e62a02c99f3 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Wed, 5 Nov 2025 16:43:51 +0100 Subject: [PATCH] Kotlin: For lateinit metrics, check that the metric was instantiated before recording on it This _should not_ happen, but apparently it does in Fenix. I have no idea why or how to prevent it. But we can mitigate it. Downside: If the instance is not instantiated by the time `set` actually runs we don't record any data. But then again before we just crashed, which also didn't record data. --- CHANGELOG.md | 3 +++ .../java/mozilla/telemetry/glean/private/BooleanMetricType.kt | 4 +++- .../java/mozilla/telemetry/glean/private/CounterMetricType.kt | 4 +++- .../mozilla/telemetry/glean/private/QuantityMetricType.kt | 4 +++- .../java/mozilla/telemetry/glean/private/StringMetricType.kt | 4 +++- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93aa9d7fb1..cebc96653f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ [Full changelog](https://github.com/mozilla/glean/compare/v66.1.0...main) +* Kotlin + * BUGFIX: For `lateinit` metrics, check that the metric was instantiated before recording on it ([#3309](https://github.com/mozilla/glean/pull/3309)) + # v66.1.0 (2025-11-03) [Full changelog](https://github.com/mozilla/glean/compare/v66.0.1...v66.1.0) diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/BooleanMetricType.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/BooleanMetricType.kt index 2c07973cae..b2642f0d44 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/BooleanMetricType.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/BooleanMetricType.kt @@ -37,7 +37,9 @@ class BooleanMetricType { */ fun set(value: Boolean) { Dispatchers.Delayed.launch { - inner.set(value) + if (this::inner.isInitialized) { + inner.set(value) + } } } diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/CounterMetricType.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/CounterMetricType.kt index fee038c709..3c6237f12b 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/CounterMetricType.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/CounterMetricType.kt @@ -38,7 +38,9 @@ class CounterMetricType { */ fun add(amount: Int = 1) { Dispatchers.Delayed.launch { - inner.add(amount) + if (this::inner.isInitialized) { + inner.add(amount) + } } } diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/QuantityMetricType.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/QuantityMetricType.kt index f05a8ee7a2..7ed057ca57 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/QuantityMetricType.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/QuantityMetricType.kt @@ -37,7 +37,9 @@ class QuantityMetricType { */ fun set(value: Long) { Dispatchers.Delayed.launch { - inner.set(value) + if (this::inner.isInitialized) { + inner.set(value) + } } } diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/StringMetricType.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/StringMetricType.kt index 8fbc887f32..de1a3aee99 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/private/StringMetricType.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/private/StringMetricType.kt @@ -38,7 +38,9 @@ class StringMetricType { */ fun set(value: String) { Dispatchers.Delayed.launch { - inner.set(value) + if (this::inner.isInitialized) { + inner.set(value) + } } }