Skip to content

Commit 7eb66d8

Browse files
committed
RUM-10240: Add public APIs for set account information
1 parent 9d00117 commit 7eb66d8

File tree

7 files changed

+216
-1
lines changed

7 files changed

+216
-1
lines changed

dd-sdk-android-core/api/apiSurface

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ object com.datadog.android.Datadog
1111
DEPRECATED fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
1212
fun addUserProperties(Map<String, Any?>, com.datadog.android.api.SdkCore = getInstance())
1313
fun clearAllData(com.datadog.android.api.SdkCore = getInstance())
14+
fun setAccountInfo(String, String? = null, Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
15+
fun addAccountExtraInfo(Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
16+
fun clearAccountInfo(com.datadog.android.api.SdkCore = getInstance())
1417
fun _internalProxy(String? = null): _InternalProxy
1518
enum com.datadog.android.DatadogSite
1619
- US1
@@ -57,6 +60,9 @@ interface com.datadog.android.api.SdkCore
5760
fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap())
5861
fun addUserProperties(Map<String, Any?>)
5962
fun clearAllData()
63+
fun setAccountInfo(String, String? = null, Map<String, Any?> = emptyMap())
64+
fun addAccountExtraInfo(Map<String, Any?> = emptyMap())
65+
fun clearAccountInfo()
6066
data class com.datadog.android.api.context.DatadogContext
6167
constructor(com.datadog.android.DatadogSite, String, String, String, String, String, String, String, TimeInfo, ProcessInfo, NetworkInfo, DeviceInfo, UserInfo, com.datadog.android.privacy.TrackingConsent, String?, Map<String, Map<String, Any?>>)
6268
data class com.datadog.android.api.context.DeviceInfo

dd-sdk-android-core/api/dd-sdk-android-core.api

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ public final class com/datadog/android/Datadog {
1212
public static final field INSTANCE Lcom/datadog/android/Datadog;
1313
public final fun _internalProxy (Ljava/lang/String;)Lcom/datadog/android/_InternalProxy;
1414
public static synthetic fun _internalProxy$default (Lcom/datadog/android/Datadog;Ljava/lang/String;ILjava/lang/Object;)Lcom/datadog/android/_InternalProxy;
15+
public static final fun addAccountExtraInfo ()V
16+
public static final fun addAccountExtraInfo (Ljava/util/Map;)V
17+
public static final fun addAccountExtraInfo (Ljava/util/Map;Lcom/datadog/android/api/SdkCore;)V
18+
public static synthetic fun addAccountExtraInfo$default (Ljava/util/Map;Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
1519
public static final fun addUserProperties (Ljava/util/Map;)V
1620
public static final fun addUserProperties (Ljava/util/Map;Lcom/datadog/android/api/SdkCore;)V
1721
public static synthetic fun addUserProperties$default (Ljava/util/Map;Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
22+
public static final fun clearAccountInfo ()V
23+
public static final fun clearAccountInfo (Lcom/datadog/android/api/SdkCore;)V
24+
public static synthetic fun clearAccountInfo$default (Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
1825
public static final fun clearAllData ()V
1926
public static final fun clearAllData (Lcom/datadog/android/api/SdkCore;)V
2027
public static synthetic fun clearAllData$default (Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
@@ -27,6 +34,11 @@ public final class com/datadog/android/Datadog {
2734
public static final fun isInitialized ()Z
2835
public static final fun isInitialized (Ljava/lang/String;)Z
2936
public static synthetic fun isInitialized$default (Ljava/lang/String;ILjava/lang/Object;)Z
37+
public static final fun setAccountInfo (Ljava/lang/String;)V
38+
public static final fun setAccountInfo (Ljava/lang/String;Ljava/lang/String;)V
39+
public static final fun setAccountInfo (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
40+
public static final fun setAccountInfo (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Lcom/datadog/android/api/SdkCore;)V
41+
public static synthetic fun setAccountInfo$default (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
3042
public static final fun setTrackingConsent (Lcom/datadog/android/privacy/TrackingConsent;)V
3143
public static final fun setTrackingConsent (Lcom/datadog/android/privacy/TrackingConsent;Lcom/datadog/android/api/SdkCore;)V
3244
public static synthetic fun setTrackingConsent$default (Lcom/datadog/android/privacy/TrackingConsent;Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
@@ -118,17 +130,22 @@ public final class com/datadog/android/api/InternalLogger$Target : java/lang/Enu
118130
}
119131

120132
public abstract interface class com/datadog/android/api/SdkCore {
133+
public abstract fun addAccountExtraInfo (Ljava/util/Map;)V
121134
public abstract fun addUserProperties (Ljava/util/Map;)V
135+
public abstract fun clearAccountInfo ()V
122136
public abstract fun clearAllData ()V
123137
public abstract fun getName ()Ljava/lang/String;
124138
public abstract fun getService ()Ljava/lang/String;
125139
public abstract fun getTime ()Lcom/datadog/android/api/context/TimeInfo;
126140
public abstract fun isCoreActive ()Z
141+
public abstract fun setAccountInfo (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
127142
public abstract fun setTrackingConsent (Lcom/datadog/android/privacy/TrackingConsent;)V
128143
public abstract fun setUserInfo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
129144
}
130145

131146
public final class com/datadog/android/api/SdkCore$DefaultImpls {
147+
public static synthetic fun addAccountExtraInfo$default (Lcom/datadog/android/api/SdkCore;Ljava/util/Map;ILjava/lang/Object;)V
148+
public static synthetic fun setAccountInfo$default (Lcom/datadog/android/api/SdkCore;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V
132149
public static synthetic fun setUserInfo$default (Lcom/datadog/android/api/SdkCore;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V
133150
}
134151

dd-sdk-android-core/src/main/kotlin/com/datadog/android/Datadog.kt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,75 @@ object Datadog {
314314
sdkCore.clearAllData()
315315
}
316316

317+
/**
318+
* Sets current account information.
319+
*
320+
* Those will be added to logs, traces and RUM events automatically.
321+
*
322+
* @param id Account ID.
323+
* @param name representing the account, if exists.
324+
* @param extraInfo Account's custom attributes, if exists.
325+
* @param sdkCore SDK instance to set account information. If not provided, default SDK
326+
* instance will be used.
327+
*/
328+
@JvmStatic
329+
@JvmOverloads
330+
fun setAccountInfo(
331+
id: String,
332+
name: String? = null,
333+
extraInfo: Map<String, Any?> = emptyMap(),
334+
sdkCore: SdkCore = getInstance()
335+
) {
336+
sdkCore.setAccountInfo(
337+
id = id,
338+
name = name,
339+
extraInfo = extraInfo
340+
)
341+
}
342+
343+
/** Add custom attributes to the current account information
344+
*
345+
* This extra info will be added to already existing extra info that is added
346+
* to Logs, Traces and RUM events automatically.
347+
*
348+
* @param extraInfo Account's additional custom attributes.
349+
* @param sdkCore SDK instance to add account custom attributes. If not provided,
350+
* default SDK instance will be used.
351+
*/
352+
@JvmStatic
353+
@JvmOverloads
354+
fun addAccountExtraInfo(
355+
extraInfo: Map<String, Any?> = emptyMap(),
356+
sdkCore: SdkCore = getInstance()
357+
) {
358+
sdkCore.addAccountExtraInfo(extraInfo)
359+
}
360+
361+
/** Clear the current account information
362+
*
363+
* Account information will set to null
364+
* Following Logs, Traces, RUM Events will not include the account information anymore.
365+
*
366+
* Any active RUM Session, active RUM View at the time of call will have their `account` attribute cleared
367+
*
368+
* If you want to retain the current `account` on the active RUM session,
369+
* you need to stop the session first by using `GlobalRumMonitor.get().stopSession()`
370+
*
371+
* If you want to retain the current `account` on the active RUM views,
372+
* you need to stop the view first by using `GlobalRumMonitor.get().stopView()`.
373+
*
374+
* @param sdkCore SDK instance to clear account info. If not provided,
375+
* default SDK instance will be used.
376+
*
377+
*/
378+
@JvmStatic
379+
@JvmOverloads
380+
fun clearAccountInfo(
381+
sdkCore: SdkCore = getInstance()
382+
) {
383+
sdkCore.clearAccountInfo()
384+
}
385+
317386
// Executes all the pending queues in the upload/persistence executors.
318387
// Tries to send all the granted data for each feature and then clears the folders and shuts
319388
// down the persistence and the upload executors.

dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,45 @@ interface SdkCore {
8181
*/
8282
@AnyThread
8383
fun clearAllData()
84+
85+
/**
86+
* Sets current account information.
87+
*
88+
* Those will be added to logs, traces and RUM events automatically.
89+
*
90+
* @param id Account ID.
91+
* @param name representing the account, if exists.
92+
* @param extraInfo Account's custom attributes, if exists.
93+
*/
94+
fun setAccountInfo(
95+
id: String,
96+
name: String? = null,
97+
extraInfo: Map<String, Any?> = emptyMap()
98+
)
99+
100+
/** Add custom attributes to the current account information
101+
*
102+
* This extra info will be added to already existing extra info that is added
103+
* to logs traces and RUM events automatically.
104+
*
105+
* @param extraInfo Account's additional custom attributes.
106+
*/
107+
fun addAccountExtraInfo(
108+
extraInfo: Map<String, Any?> = emptyMap()
109+
)
110+
111+
/** Clear the current account information
112+
*
113+
* Account information will set to null
114+
* Following Logs, Traces, RUM Events will not include the account information anymore.
115+
*
116+
* Any active RUM Session, active RUM View at the time of call will have their `account` attribute cleared
117+
*
118+
* If you want to retain the current `account` on the active RUM session,
119+
* you need to stop the session first by using `GlobalRumMonitor.get().stopSession()`
120+
*
121+
* If you want to retain the current `account` on the active RUM views,
122+
* you need to stop the view first by using `GlobalRumMonitor.get().stopView()`
123+
*/
124+
fun clearAccountInfo()
84125
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ internal class DatadogCore(
188188
}
189189
}
190190

191+
override fun setAccountInfo(
192+
id: String,
193+
name: String?,
194+
extraInfo: Map<String, Any?>
195+
) {
196+
// TODO RUM-10241: Implement set account info in Datadog Core
197+
}
198+
199+
override fun addAccountExtraInfo(
200+
extraInfo: Map<String, Any?>
201+
) {
202+
// TODO RUM-10241: Implement set account info in Datadog Core
203+
}
204+
205+
override fun clearAccountInfo() {
206+
// TODO RUM-10241: Implement set account info in Datadog Core
207+
}
208+
191209
/** @inheritDoc */
192210
override fun updateFeatureContext(
193211
featureName: String,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ internal object NoOpInternalSdkCore : InternalSdkCore {
9090

9191
override fun clearAllData() = Unit
9292

93+
override fun setAccountInfo(
94+
id: String,
95+
name: String?,
96+
extraInfo: Map<String, Any?>
97+
) = Unit
98+
99+
override fun addAccountExtraInfo(extraInfo: Map<String, Any?>) = Unit
100+
101+
override fun clearAccountInfo() = Unit
102+
93103
override fun isCoreActive(): Boolean = false
94104

95105
// endregion

dd-sdk-android-core/src/test/kotlin/com/datadog/android/DatadogTest.kt

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.datadog.android.core.internal.HashGenerator
1818
import com.datadog.android.core.internal.NoOpInternalSdkCore
1919
import com.datadog.android.core.internal.SdkCoreRegistry
2020
import com.datadog.android.core.internal.Sha256HashGenerator
21-
import com.datadog.android.core.internal.utils.loggableStackTrace
2221
import com.datadog.android.internal.utils.loggableStackTrace
2322
import com.datadog.android.privacy.TrackingConsent
2423
import com.datadog.android.utils.config.ApplicationContextTestConfiguration
@@ -553,6 +552,61 @@ internal class DatadogTest {
553552
verify(mockSdkCore).clearAllData()
554553
}
555554

555+
@Test
556+
fun `M call Core set account info W setAccountInfo()`(
557+
@StringForgery(type = StringForgeryType.HEXADECIMAL) id: String,
558+
@StringForgery name: String,
559+
@MapForgery(
560+
key = AdvancedForgery(string = [StringForgery(StringForgeryType.ALPHA_NUMERICAL)]),
561+
value = AdvancedForgery(string = [StringForgery(StringForgeryType.ALPHA_NUMERICAL)])
562+
) fakeExtraInfo: Map<String, String>
563+
) {
564+
// Given
565+
val mockSdkCore = mock<SdkCore>()
566+
567+
// When
568+
Datadog.setAccountInfo(
569+
id = id,
570+
name = name,
571+
extraInfo = fakeExtraInfo,
572+
sdkCore = mockSdkCore
573+
)
574+
575+
// Then
576+
verify(mockSdkCore).setAccountInfo(id, name, fakeExtraInfo)
577+
}
578+
579+
@Test
580+
fun `M call Core add account extra info W addAccountExtraInfo()`(
581+
@MapForgery(
582+
key = AdvancedForgery(string = [StringForgery(StringForgeryType.ALPHA_NUMERICAL)]),
583+
value = AdvancedForgery(string = [StringForgery(StringForgeryType.ALPHA_NUMERICAL)])
584+
) fakeExtraInfo: Map<String, String>
585+
) {
586+
// Given
587+
val mockSdkCore = mock<SdkCore>()
588+
589+
// When
590+
Datadog.addAccountExtraInfo(
591+
extraInfo = fakeExtraInfo,
592+
sdkCore = mockSdkCore
593+
)
594+
595+
// Then
596+
verify(mockSdkCore).addAccountExtraInfo(fakeExtraInfo)
597+
}
598+
599+
@Test
600+
fun `M call Core clear account info W clearAccountInfo()`() {
601+
// Given
602+
val mockSdkCore = mock<SdkCore>()
603+
604+
// When
605+
Datadog.clearAccountInfo(sdkCore = mockSdkCore)
606+
607+
// Then
608+
verify(mockSdkCore).clearAccountInfo()
609+
}
556610
companion object {
557611
val appContext = ApplicationContextTestConfiguration(Application::class.java)
558612
val logger = InternalLoggerTestConfiguration()

0 commit comments

Comments
 (0)