Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-950 | Add method to set extra properties on the UserInfo object #952

Merged
merged 3 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dd-sdk-android/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object com.datadog.android.Datadog
fun setVerbosity(Int)
fun setTrackingConsent(com.datadog.android.privacy.TrackingConsent)
fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap())
fun setExtraInfo(Map<String, Any?> = emptyMap())
fun enableRumDebugging(Boolean)
object com.datadog.android.DatadogEndpoint
const val LOGS_US1: String
Expand Down
20 changes: 20 additions & 0 deletions dd-sdk-android/src/main/kotlin/com/datadog/android/Datadog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ object Datadog {
)
}

/**
* Sets additional information on the [UserInfo] object
*
* If properties had originally been set with [Datadog.setUserInfo], they will be preserved.
* In the event of a conflict on key, the new property will prevail.
*
* @param extraInfo additional information. An extra information can be
* nested up to 8 levels deep. Keys using more than 8 levels will be sanitized by SDK.
*/
@JvmStatic
@JvmOverloads
fun setExtraInfo(
This conversation was marked as resolved.
Show resolved Hide resolved
extraInfo: Map<String, Any?> = emptyMap()
) {
CoreFeature.userInfoProvider.setExtraProperties(
extraInfo
)
}


/**
* Utility setting to inspect the active RUM View.
* If set, a debugging outline will be displayed on top of the application, describing the name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ internal class DatadogUserInfoProvider(
internalUserInfo = userInfo
}

override fun setExtraProperties(properties: Map<String, Any?>) {
This conversation was marked as resolved.
Show resolved Hide resolved
// keep any existing properties
val currentProperties = internalUserInfo.additionalProperties

internalUserInfo = internalUserInfo.copy(
// preserve current properties. in the event of a conflict, let the new properties previal
additionalProperties = currentProperties + properties
)
}

override fun getUserInfo(): UserInfo {
return internalUserInfo
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ import com.datadog.tools.annotation.NoOpImplementation
internal interface MutableUserInfoProvider : UserInfoProvider {

fun setUserInfo(userInfo: UserInfo)
fun setExtraProperties(properties: Map<String, Any?>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,41 @@ internal class DatadogTest {
)
}

@Test
fun `𝕄 set additionalProperties 𝕎 setExtraProperties() is called`(
@StringForgery(type = StringForgeryType.HEXADECIMAL) id: String,
@StringForgery name: String,
@StringForgery(regex = "\\w+@\\w+") email: String,
) {
// Given
val mockUserInfoProvider = mock<MutableUserInfoProvider>()
CoreFeature.userInfoProvider = mockUserInfoProvider

// When
Datadog.setUserInfo(id, name, email)
Datadog.setExtraInfo(
mapOf(
"key1" to 1,
"key2" to "one",
)
)

// Then
verify(mockUserInfoProvider).setUserInfo(
UserInfo(
id,
name,
email
)
)
verify(mockUserInfoProvider).setExtraProperties(
properties = mapOf(
"key1" to 1,
"key2" to "one",
)
)
}

@Test
fun `𝕄 return true 𝕎 initialize(context, credential, , consent) + isInitialized()`() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

package com.datadog.android.log.internal.user

import com.datadog.android.Datadog
import com.datadog.android.core.internal.CoreFeature
import com.datadog.android.core.internal.persistence.DataWriter
import com.datadog.android.core.model.UserInfo
import com.datadog.android.utils.forge.Configurator
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import fr.xgouchet.elmyr.annotation.Forgery
import fr.xgouchet.elmyr.annotation.StringForgery
import fr.xgouchet.elmyr.annotation.StringForgeryType
import fr.xgouchet.elmyr.junit5.ForgeConfiguration
import fr.xgouchet.elmyr.junit5.ForgeExtension
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -65,4 +70,68 @@ internal class DatadogUserInfoProviderTest {
// Then
verify(mockWriter).write(userInfo)
}

@Test
fun `𝕄 keep existing properties 𝕎 setExtraProperties() is called`(
@Forgery userInfo: UserInfo
) {
// When
val originalProperties = mapOf(
"key1" to 1,
"key2" to "one",
)
val newProperties = mapOf(
"key3" to 1.0,
This conversation was marked as resolved.
Show resolved Hide resolved
)
testedProvider.setUserInfo(
userInfo.copy(
additionalProperties = originalProperties,
)
)
testedProvider.setExtraProperties(newProperties)


// Then
assertThat(
testedProvider.getUserInfo().additionalProperties
).isEqualTo(
mapOf(
"key1" to 1,
"key2" to "one",
"key3" to 1.0,
)
)
}

@Test
fun `𝕄 keep new property key 𝕎 setExtraProperties() is called and there already exists that key`(
@Forgery userInfo: UserInfo
) {
// When
val originalProperties = mapOf(
"key1" to 1,
"key2" to "one",
)
// this map has a conflicting key with the original properties map
val newProperties = mapOf(
"key1" to 1.0,
)
testedProvider.setUserInfo(
userInfo.copy(
additionalProperties = originalProperties,
)
)
testedProvider.setExtraProperties(newProperties)


// Then
assertThat(
testedProvider.getUserInfo().additionalProperties
).isEqualTo(
mapOf(
"key1" to 1.0,
"key2" to "one",
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.datadog.android.Datadog.setExtraInfo
import com.datadog.android.Datadog.setUserInfo
import com.datadog.android.ktx.tracing.withinSpan
import com.datadog.android.sample.Preferences
Expand Down Expand Up @@ -70,6 +71,8 @@ class UserFragment : Fragment(), View.OnClickListener {
id,
name,
email,
)
setExtraInfo(
mapOf(
GENDER_KEY to gender,
AGE_KEY to age
Expand Down