Skip to content

Commit 07f3006

Browse files
committed
RUM-8375: fix tests and static analysis tools notes
1 parent fcafaa7 commit 07f3006

File tree

12 files changed

+129
-87
lines changed

12 files changed

+129
-87
lines changed

dd-sdk-android-core/api/apiSurface

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ sealed class com.datadog.android.core.feature.event.JvmCrash
255255
constructor(Throwable, String, List<ThreadDump>)
256256
data class com.datadog.android.core.feature.event.ThreadDump
257257
constructor(String, String, String, Boolean)
258+
interface com.datadog.android.core.internal.attributes.LocalAttribute
259+
enum Key
260+
constructor(String)
261+
- CREATION_SAMPLING_RATE
262+
- REPORTING_SAMPLING_RATE
263+
- VIEW_SCOPE_INSTRUMENTATION_TYPE
264+
interface Constant
265+
val key: Key
266+
val value: Any
267+
fun MutableMap<String, Any?>.enrichWithConstantAttribute(LocalAttribute.Constant)
268+
fun MutableMap<String, Any?>.enrichWithNonNullAttribute(LocalAttribute.Key, Any?)
269+
fun MutableMap<String, Any?>.enrichWithLocalAttribute(LocalAttribute.Key, Any?)
270+
enum com.datadog.android.core.internal.attributes.ViewScopeInstrumentationType : LocalAttribute.Constant
271+
constructor(String)
272+
- MANUAL
273+
- COMPOSE
274+
- ACTIVITY
275+
- FRAGMENT
276+
override val key: LocalAttribute.Key
258277
class com.datadog.android.core.internal.net.DefaultFirstPartyHostHeaderTypeResolver : FirstPartyHostHeaderTypeResolver
259278
constructor(Map<String, Set<com.datadog.android.trace.TracingHeaderType>>)
260279
override fun isFirstPartyUrl(okhttp3.HttpUrl): Boolean

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/attributes/LocalAttribute.kt

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.datadog.android.lint.InternalApi
1111
* Local attributes are used to pass additional metadata along with the event
1212
* and are never sent to the backend directly.
1313
*/
14+
@InternalApi
1415
interface LocalAttribute {
1516

1617
/**
@@ -53,14 +54,10 @@ interface LocalAttribute {
5354
*/
5455
@InternalApi
5556
interface Constant {
56-
/**
57-
* Constant attribute key. For enum constants will be same for all values.
58-
*/
57+
/** Constant attribute key. For enum constants will be same for all values. */
5958
val key: Key
6059

61-
/**
62-
* Constant attribute value. One item from set of possible finite values for a given constant attribute.
63-
*/
60+
/** Constant attribute value. One item from set of possible finite values for a given constant attribute.*/
6461
val value: Any
6562
}
6663
}
@@ -104,38 +101,3 @@ fun MutableMap<String, Any?>.enrichWithLocalAttribute(
104101
) = apply {
105102
this[key.string] = value
106103
}
107-
108-
/**
109-
* A set of constants describing the instrumentation that were used to define the view scope.
110-
*
111-
* @property value - String value of the constant
112-
* @property key - Key that is being used for this attribute.
113-
* Equals to [LocalAttribute.Key.VIEW_SCOPE_INSTRUMENTATION_TYPE] for every value.
114-
*/
115-
@Suppress("KDocUnresolvedReference")
116-
@InternalApi
117-
enum class ViewScopeInstrumentationType(
118-
override val value: String
119-
) : LocalAttribute.Constant {
120-
/**
121-
* Tracked manually through the RUMMonitor API.
122-
*/
123-
MANUAL("manual"),
124-
125-
/**
126-
* Tracked through [ComposeNavigationObserver] instrumentation
127-
*/
128-
COMPOSE("compose"),
129-
130-
/**
131-
* Tracked through [ActivityViewTrackingStrategy] instrumentation
132-
*/
133-
ACTIVITY("activity"),
134-
135-
/**
136-
* Tracked through [FragmentViewTrackingStrategy] instrumentation
137-
*/
138-
FRAGMENT("fragment");
139-
140-
override val key: LocalAttribute.Key = LocalAttribute.Key.VIEW_SCOPE_INSTRUMENTATION_TYPE
141-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
package com.datadog.android.core.internal.attributes
7+
8+
import com.datadog.android.lint.InternalApi
9+
10+
/**
11+
* A set of constants describing the instrumentation that were used to define the view scope.
12+
*/
13+
@InternalApi
14+
enum class ViewScopeInstrumentationType(
15+
override val value: String
16+
) : LocalAttribute.Constant {
17+
/** Tracked manually through the RUMMonitor API. */
18+
MANUAL("manual"),
19+
20+
/** Tracked through ComposeNavigationObserver instrumentation. */
21+
COMPOSE("compose"),
22+
23+
/** Tracked through ActivityViewTrackingStrategy instrumentation. */
24+
ACTIVITY("activity"),
25+
26+
/** Tracked through FragmentViewTrackingStrategy instrumentation. */
27+
FRAGMENT("fragment");
28+
29+
/** @inheritdoc */
30+
override val key: LocalAttribute.Key = LocalAttribute.Key.VIEW_SCOPE_INSTRUMENTATION_TYPE
31+
}

dd-sdk-android-internal/api/apiSurface

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ sealed class com.datadog.android.internal.telemetry.InternalTelemetryEvent
2828
class AddViewLoadingTime : ApiUsage
2929
constructor(Boolean, Boolean, Boolean, MutableMap<String, Any?> = mutableMapOf())
3030
object InterceptorInstantiated : InternalTelemetryEvent
31-
companion object
32-
const val CREATION_SAMPLING_RATE_KEY: String
33-
const val REPORTING_SAMPLING_RATE_KEY: String
3431
fun ByteArray.toHexString(): String
3532
object com.datadog.android.internal.utils.ImageViewUtils
3633
fun resolveParentRectAbsPosition(android.view.View, Boolean = true): android.graphics.Rect

features/dd-sdk-android-rum/api/apiSurface

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class com.datadog.android.rum.tracking.ActivityViewTrackingStrategy : ActivityLi
254254
override fun onActivityStopped(android.app.Activity)
255255
override fun equals(Any?): Boolean
256256
override fun hashCode(): Int
257-
fun android.os.Bundle?.convertToRumViewAttributes(): Map<String, Any?>
257+
fun android.os.Bundle?.convertToRumViewAttributes(): MutableMap<String, Any?>
258258
interface com.datadog.android.rum.tracking.ComponentPredicate<T>
259259
fun accept(T): Boolean
260260
fun getViewName(T): String?

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/internal/metric/ViewEndedMetricDispatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ internal class ViewEndedMetricDispatcher(
153153
@VisibleForTesting
154154
private fun toAttributeValue(
155155
instrumentationType: ViewScopeInstrumentationType?
156-
) = when (instrumentationType) {
156+
): String = when (instrumentationType) {
157157
ViewScopeInstrumentationType.COMPOSE -> VALUE_INSTRUMENTATION_TYPE_COMPOSE
158158
ViewScopeInstrumentationType.MANUAL -> VALUE_INSTRUMENTATION_TYPE_MANUAL
159159
ViewScopeInstrumentationType.ACTIVITY -> VALUE_INSTRUMENTATION_TYPE_ACTIVITY
160160
ViewScopeInstrumentationType.FRAGMENT -> VALUE_INSTRUMENTATION_TYPE_FRAGMENT
161-
null -> ViewScopeInstrumentationType.MANUAL
161+
null -> VALUE_INSTRUMENTATION_TYPE_MANUAL
162162
}
163163

164164
@VisibleForTesting

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/telemetry/internal/TelemetryEventHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ internal class TelemetryEventHandler(
287287
val sessionReplayFeatureContext =
288288
sdkCore.getFeatureContext(Feature.SESSION_REPLAY_FEATURE_NAME)
289289
val sessionReplaySampleRate = sessionReplayFeatureContext[SESSION_REPLAY_SAMPLE_RATE_KEY]
290-
as? Long
290+
as? Long
291291
val startRecordingImmediately =
292292
sessionReplayFeatureContext[SESSION_REPLAY_START_IMMEDIATE_RECORDING_KEY] as? Boolean
293293
val sessionReplayImagePrivacy =
@@ -429,7 +429,7 @@ internal class TelemetryEventHandler(
429429
InternalLogger.Target.TELEMETRY,
430430
{
431431
"GlobalTracer class exists in the runtime classpath, " +
432-
"but there is an error invoking isRegistered method"
432+
"but there is an error invoking isRegistered method"
433433
},
434434
t
435435
)

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/ActivityViewTrackingStrategyTest.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package com.datadog.android.rum
88

99
import android.app.Activity
1010
import android.os.Bundle
11+
import com.datadog.android.core.internal.attributes.ViewScopeInstrumentationType
12+
import com.datadog.android.core.internal.attributes.enrichWithConstantAttribute
1113
import com.datadog.android.rum.tracking.ActivityViewTrackingStrategy
1214
import com.datadog.android.rum.tracking.ComponentPredicate
1315
import com.datadog.android.rum.tracking.StubComponentPredicate
@@ -105,7 +107,7 @@ internal class ActivityViewTrackingStrategyTest :
105107
verify(rumMonitor.mockInstance).startView(
106108
mockActivity,
107109
mockActivity.resolveViewName(),
108-
emptyMap()
110+
mapOf(ViewScopeInstrumentationType.ACTIVITY.key.string to ViewScopeInstrumentationType.ACTIVITY.value)
109111
)
110112
}
111113

@@ -128,7 +130,8 @@ internal class ActivityViewTrackingStrategyTest :
128130
whenever(mockActivity.intent).thenReturn(mockIntent)
129131
whenever(mockPredicate.accept(mockActivity)) doReturn true
130132
val expectedAttributes = extras.map { (k, v) -> "view.arguments.$k" to v }
131-
.toMutableMap()
133+
.toMutableMap<String, Any?>()
134+
.enrichWithConstantAttribute(ViewScopeInstrumentationType.ACTIVITY)
132135
expectedAttributes["view.intent.action"] = action
133136
expectedAttributes["view.intent.uri"] = uri
134137
testedStrategy.register(rumMonitor.mockSdkCore, mockActivity)
@@ -157,7 +160,12 @@ internal class ActivityViewTrackingStrategyTest :
157160
whenever(mockIntent.dataString).thenReturn(uri)
158161
whenever(mockActivity.intent).thenReturn(mockIntent)
159162
whenever(mockPredicate.accept(mockActivity)) doReturn true
160-
val expectedAttributes = mapOf("view.intent.action" to action, "view.intent.uri" to uri)
163+
val expectedAttributes = mapOf<String, Any?>(
164+
"view.intent.action" to action,
165+
"view.intent.uri" to uri,
166+
ViewScopeInstrumentationType.ACTIVITY.key.string to ViewScopeInstrumentationType.ACTIVITY.value
167+
)
168+
161169
testedStrategy.register(rumMonitor.mockSdkCore, mockActivity)
162170

163171
// When
@@ -215,7 +223,7 @@ internal class ActivityViewTrackingStrategyTest :
215223
verify(rumMonitor.mockInstance).startView(
216224
mockActivity,
217225
fakeName,
218-
emptyMap()
226+
mapOf(ViewScopeInstrumentationType.ACTIVITY.key.string to ViewScopeInstrumentationType.ACTIVITY.value)
219227
)
220228
}
221229

@@ -236,7 +244,7 @@ internal class ActivityViewTrackingStrategyTest :
236244
verify(rumMonitor.mockInstance).startView(
237245
mockActivity,
238246
mockActivity.resolveViewName(),
239-
emptyMap()
247+
mapOf(ViewScopeInstrumentationType.ACTIVITY.key.string to ViewScopeInstrumentationType.ACTIVITY.value)
240248
)
241249
}
242250

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/metric/ViewEndedMetricDispatcherTest.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.datadog.android.rum.internal.metric.NoValueReason
1212
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher
1313
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_CONFIG
1414
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_DURATION
15+
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_INSTRUMENTATION_TYPE
1516
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_INTERACTION_TO_NEXT_VIEW
1617
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_LOADING_TIME
1718
import com.datadog.android.rum.internal.metric.ViewEndedMetricDispatcher.Companion.KEY_METRIC_TYPE
@@ -68,12 +69,14 @@ internal class ViewEndedMetricDispatcherTest {
6869
private lateinit var fakeViewType: RumViewType
6970
private lateinit var fakeInvState: ViewInitializationMetricsState
7071
private lateinit var fakeTnsState: ViewInitializationMetricsState
72+
private var fakeInstrumentationType: String? = null
7173

7274
private lateinit var dispatcherUnderTest: ViewEndedMetricDispatcher
7375

7476
@BeforeEach
7577
fun `set up`(forge: Forge) {
7678
fakeViewType = forge.aValueFrom(RumViewType::class.java)
79+
fakeInstrumentationType = forge.aNullable { anElementFrom("compose", "manual", "activity", "fragment") }
7780
fakeTnsState = forge.aViewInitializationMetricsState(NoValueReason.TimeToNetworkSettle::class.java)
7881
fakeInvState = forge.aViewInitializationMetricsState(NoValueReason.InteractionToNextView::class.java)
7982

@@ -220,7 +223,8 @@ internal class ViewEndedMetricDispatcherTest {
220223
loadingTime: Long? = fakeLoadingTime,
221224
viewType: RumViewType = fakeViewType,
222225
invState: ViewInitializationMetricsState = fakeInvState,
223-
tnsState: ViewInitializationMetricsState = fakeTnsState
226+
tnsState: ViewInitializationMetricsState = fakeTnsState,
227+
instrumentationType: String? = fakeInstrumentationType
224228
) = buildAttributesMap(
225229
duration = duration,
226230
loadingTime = loadingTime,
@@ -232,7 +236,8 @@ internal class ViewEndedMetricDispatcherTest {
232236

233237
tnsValue = tnsState.initializationTime,
234238
tnsConfig = toAttributeValue(tnsState.config),
235-
tnsNoValueReason = toAttributeValue(tnsState.noValueReason)
239+
tnsNoValueReason = toAttributeValue(tnsState.noValueReason),
240+
instrumentationType = instrumentationType
236241
)
237242

238243
companion object {
@@ -245,7 +250,8 @@ internal class ViewEndedMetricDispatcherTest {
245250
invNoValueReason: String?,
246251
tnsValue: Long?,
247252
tnsConfig: String,
248-
tnsNoValueReason: String?
253+
tnsNoValueReason: String?,
254+
instrumentationType: String?
249255
): Map<String, Any?> = mapOf(
250256
KEY_METRIC_TYPE to "rum view ended",
251257
KEY_RUM_VIEW_ENDED to mapOf(
@@ -261,7 +267,8 @@ internal class ViewEndedMetricDispatcherTest {
261267
invValue,
262268
invConfig,
263269
invNoValueReason
264-
)
270+
),
271+
KEY_INSTRUMENTATION_TYPE to (instrumentationType ?: "manual")
265272
)
266273
)
267274

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/tracking/FragmentViewTrackingStrategyTest.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import androidx.fragment.app.FragmentManager
1717
import com.datadog.android.api.InternalLogger
1818
import com.datadog.android.api.feature.Feature
1919
import com.datadog.android.api.feature.FeatureScope
20+
import com.datadog.android.core.internal.attributes.ViewScopeInstrumentationType
21+
import com.datadog.android.core.internal.attributes.enrichWithConstantAttribute
2022
import com.datadog.android.core.internal.system.BuildSdkVersionProvider
2123
import com.datadog.android.rum.internal.RumFeature
2224
import com.datadog.android.rum.internal.tracking.AndroidXFragmentLifecycleCallbacks
@@ -175,7 +177,9 @@ internal class FragmentViewTrackingStrategyTest : ObjectTest<FragmentViewTrackin
175177
// Given
176178
testedStrategy.register(rumMonitor.mockSdkCore, mockAppContext)
177179
val mockFragment: Fragment = mockFragmentWithArguments(forge)
178-
val expectedAttrs = mockFragment.arguments!!.toRumAttributes()
180+
val expectedAttrs = mockFragment.arguments!!
181+
.toRumAttributes()
182+
.enrichWithConstantAttribute(ViewScopeInstrumentationType.FRAGMENT)
179183
val argumentCaptor = argumentCaptor<FragmentManager.FragmentLifecycleCallbacks>()
180184

181185
// When
@@ -256,7 +260,9 @@ internal class FragmentViewTrackingStrategyTest : ObjectTest<FragmentViewTrackin
256260
testedStrategy = FragmentViewTrackingStrategy(false)
257261
testedStrategy.register(rumMonitor.mockSdkCore, mockAppContext)
258262
val mockFragment: Fragment = mockFragmentWithArguments(forge)
259-
val expectedAttrs = emptyMap<String, Any?>()
263+
val expectedAttrs = mapOf(
264+
ViewScopeInstrumentationType.FRAGMENT.key.string to ViewScopeInstrumentationType.FRAGMENT.value
265+
)
260266
val argumentCaptor = argumentCaptor<FragmentManager.FragmentLifecycleCallbacks>()
261267

262268
// When
@@ -376,7 +382,9 @@ internal class FragmentViewTrackingStrategyTest : ObjectTest<FragmentViewTrackin
376382
whenever(mockBuildSdkVersionProvider.version) doReturn fakeApiVersion
377383
testedStrategy.register(rumMonitor.mockSdkCore, mockAppContext)
378384
val mockFragment: android.app.Fragment = mockDeprecatedFragmentWithArguments(forge)
379-
val expectedAttrs = mockFragment.arguments.toRumAttributes()
385+
val expectedAttrs = mockFragment.arguments
386+
.toRumAttributes()
387+
.enrichWithConstantAttribute(ViewScopeInstrumentationType.FRAGMENT)
380388
val argumentCaptor =
381389
argumentCaptor<android.app.FragmentManager.FragmentLifecycleCallbacks>()
382390

@@ -469,7 +477,9 @@ internal class FragmentViewTrackingStrategyTest : ObjectTest<FragmentViewTrackin
469477
buildSdkVersionProvider = mockBuildSdkVersionProvider
470478
)
471479
testedStrategy.register(rumMonitor.mockSdkCore, mockAppContext)
472-
val expectedAttrs = emptyMap<String, Any?>()
480+
val expectedAttrs = mapOf(
481+
ViewScopeInstrumentationType.FRAGMENT.key.string to ViewScopeInstrumentationType.FRAGMENT.value
482+
)
473483
val mockFragment: android.app.Fragment = mockDeprecatedFragmentWithArguments(forge)
474484
val argumentCaptor =
475485
argumentCaptor<android.app.FragmentManager.FragmentLifecycleCallbacks>()
@@ -663,7 +673,7 @@ internal class FragmentViewTrackingStrategyTest : ObjectTest<FragmentViewTrackin
663673
return arguments
664674
}
665675

666-
private fun Bundle.toRumAttributes(): Map<String, Any?> {
676+
private fun Bundle.toRumAttributes(): MutableMap<String, Any?> {
667677
val attributes = mutableMapOf<String, Any?>()
668678
keySet().forEach {
669679
attributes["view.arguments.$it"] = get(it)

0 commit comments

Comments
 (0)