Skip to content

Commit 4557c36

Browse files
Don't send events if telemetry is disabled (#6512)
1 parent 934e668 commit 4557c36

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone.
77
#### Bug fixes and improvements
88
- Improved experience in tunnels and reduced the likelihood of losing road edge matching candidates. [#6510](https://github.com/mapbox/mapbox-navigation-android/pull/6510)
99
- Fixed an issue with `NavigationView` that caused road label position to not update in some cases. [#6508](https://github.com/mapbox/mapbox-navigation-android/pull/6508)
10+
- Fixed an issue where the SDK was trying to send telemetry events when telemetry is switched off globally. [#6512](https://github.com/mapbox/mapbox-navigation-android/pull/6512)
1011

1112
## Mapbox Navigation SDK 2.9.0-beta.3 - 21 October, 2022
1213
### Changelog
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.mapbox.navigation.base.internal.metric
2+
3+
import com.mapbox.bindgen.Value
4+
5+
fun Value.extractEventsNames(): List<String>? = (this.contents as? List<Value>)
6+
?.mapNotNull { (it.contents as? Map<String, Value>)?.get("event")?.contents as? String }

libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/MapboxMetricsReporter.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import com.mapbox.common.EventsServiceObserver
1212
import com.mapbox.common.TelemetryService
1313
import com.mapbox.common.TurnstileEvent
1414
import com.mapbox.navigation.base.internal.metric.MetricEventInternal
15+
import com.mapbox.navigation.base.internal.metric.extractEventsNames
1516
import com.mapbox.navigation.base.metrics.MetricEvent
1617
import com.mapbox.navigation.base.metrics.MetricsObserver
1718
import com.mapbox.navigation.base.metrics.MetricsReporter
1819
import com.mapbox.navigation.metrics.internal.EventsServiceProvider
1920
import com.mapbox.navigation.metrics.internal.TelemetryServiceProvider
21+
import com.mapbox.navigation.metrics.internal.TelemetryUtilsDelegate
2022
import com.mapbox.navigation.utils.internal.InternalJobControlFactory
2123
import com.mapbox.navigation.utils.internal.logD
2224
import com.mapbox.navigation.utils.internal.logE
@@ -35,7 +37,7 @@ object MapboxMetricsReporter : MetricsReporter {
3537
private lateinit var telemetryService: TelemetryService
3638

3739
@Volatile
38-
private var enableTelemetry = false
40+
private var isTelemetryInitialized = false
3941

4042
@Volatile
4143
private var metricsObserver: MetricsObserver? = null
@@ -44,11 +46,19 @@ object MapboxMetricsReporter : MetricsReporter {
4446
private val eventsServiceObserver =
4547
object : EventsServiceObserver {
4648
override fun didEncounterError(error: EventsServiceError, events: Value) {
47-
logE("EventsService failure: $error for event $events", LOG_CATEGORY)
49+
ifTelemetryIsRunning {
50+
logE(LOG_CATEGORY) {
51+
"EventsService failure: $error for events ${events.extractEventsNames()}"
52+
}
53+
}
4854
}
4955

5056
override fun didSendEvents(events: Value) {
51-
logD("Event has been sent $events", LOG_CATEGORY)
57+
ifTelemetryIsRunning {
58+
logD(LOG_CATEGORY) {
59+
"Events has been sent ${events.extractEventsNames()}"
60+
}
61+
}
5262
}
5363
}
5464

@@ -66,7 +76,7 @@ object MapboxMetricsReporter : MetricsReporter {
6676
accessToken: String,
6777
userAgent: String
6878
) {
69-
enableTelemetry = true
79+
isTelemetryInitialized = true
7080
val eventsServerOptions = EventsServerOptions(accessToken, userAgent, null)
7181
eventsService = EventsServiceProvider.provideEventsService(eventsServerOptions)
7282
telemetryService = TelemetryServiceProvider.provideTelemetryService(eventsServerOptions)
@@ -90,7 +100,7 @@ object MapboxMetricsReporter : MetricsReporter {
90100
*/
91101
@JvmStatic
92102
fun disable() {
93-
enableTelemetry = false
103+
isTelemetryInitialized = false
94104
removeObserver()
95105
eventsService.unregisterObserver(eventsServiceObserver)
96106
ioJobController.job.cancelChildren()
@@ -150,10 +160,10 @@ object MapboxMetricsReporter : MetricsReporter {
150160
}
151161

152162
private inline fun ifTelemetryIsRunning(func: () -> Unit) {
153-
if (enableTelemetry) {
163+
if (isTelemetryInitialized && TelemetryUtilsDelegate.getEventsCollectionState()) {
154164
func.invoke()
155165
} else {
156-
logW(
166+
logD(
157167
"Navigation Telemetry is disabled",
158168
LOG_CATEGORY
159169
)

libnavigation-metrics/src/test/java/com/mapbox/navigation/metrics/MapboxMetricsReporterTest.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import com.mapbox.navigation.testing.LoggingFrontendTestRule
1515
import com.mapbox.navigation.testing.MainCoroutineRule
1616
import com.mapbox.navigation.utils.internal.InternalJobControlFactory
1717
import com.mapbox.navigation.utils.internal.LoggerFrontend
18-
import com.mapbox.navigation.utils.internal.logW
1918
import io.mockk.every
2019
import io.mockk.just
2120
import io.mockk.mockk
@@ -53,7 +52,7 @@ class MapboxMetricsReporterTest {
5352
mockkObject(TelemetryServiceProvider)
5453

5554
every { TelemetryUtilsDelegate.setEventsCollectionState(any()) } just runs
56-
every { TelemetryUtilsDelegate.getEventsCollectionState() } returns false
55+
every { TelemetryUtilsDelegate.getEventsCollectionState() } returns true
5756
}
5857

5958
@After
@@ -76,7 +75,7 @@ class MapboxMetricsReporterTest {
7675
MapboxMetricsReporter.addEvent(mockk())
7776

7877
verify(exactly = 2) {
79-
logger.logW(
78+
logger.logD(
8079
"Navigation Telemetry is disabled",
8180
"MapboxMetricsReporter"
8281
)
@@ -96,6 +95,18 @@ class MapboxMetricsReporterTest {
9695
verify(exactly = 0) { eventService.sendEvent(any(), any()) }
9796
}
9897

98+
@Test
99+
fun `events aren't sent if telemetry is disabled globally`() {
100+
val eventService = initMetricsReporterWithTelemetry()
101+
102+
every { TelemetryUtilsDelegate.getEventsCollectionState() } returns false
103+
MapboxMetricsReporter.sendTurnstileEvent(mockk())
104+
MapboxMetricsReporter.addEvent(mockk())
105+
106+
verify(exactly = 0) { eventService.sendTurnstileEvent(any(), any()) }
107+
verify(exactly = 0) { eventService.sendEvent(any(), any()) }
108+
}
109+
99110
@Test
100111
fun telemetryPushCalledWhenAddValidEvent() = coroutineRule.runBlockingTest {
101112
mockkObject(InternalJobControlFactory) {

0 commit comments

Comments
 (0)