Skip to content

Commit

Permalink
RUMM-3508 Add the additionalProperties capability to telemetry debug …
Browse files Browse the repository at this point in the history
…logs
  • Loading branch information
mariusc83 committed Aug 14, 2023
1 parent 3d566c0 commit 3377375
Show file tree
Hide file tree
Showing 33 changed files with 462 additions and 146 deletions.
4 changes: 2 additions & 2 deletions dd-sdk-android-core/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ interface com.datadog.android.api.InternalLogger
- USER
- MAINTAINER
- TELEMETRY
fun log(Level, Target, () -> String, Throwable? = null, Boolean = false)
fun log(Level, List<Target>, () -> String, Throwable? = null, Boolean = false)
fun log(Level, Target, () -> String, Throwable? = null, Boolean = false, Map<String, Any?>? = null)
fun log(Level, List<Target>, () -> String, Throwable? = null, Boolean = false, Map<String, Any?>? = null)
companion object
val UNBOUND: InternalLogger
interface com.datadog.android.api.SdkCore
Expand Down
8 changes: 4 additions & 4 deletions dd-sdk-android-core/api/dd-sdk-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ public final class com/datadog/android/_InternalProxy$_TelemetryProxy {

public abstract interface class com/datadog/android/api/InternalLogger {
public static final field Companion Lcom/datadog/android/api/InternalLogger$Companion;
public abstract fun log (Lcom/datadog/android/api/InternalLogger$Level;Lcom/datadog/android/api/InternalLogger$Target;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;Z)V
public abstract fun log (Lcom/datadog/android/api/InternalLogger$Level;Ljava/util/List;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;Z)V
public abstract fun log (Lcom/datadog/android/api/InternalLogger$Level;Lcom/datadog/android/api/InternalLogger$Target;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZLjava/util/Map;)V
public abstract fun log (Lcom/datadog/android/api/InternalLogger$Level;Ljava/util/List;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZLjava/util/Map;)V
}

public final class com/datadog/android/api/InternalLogger$Companion {
public final fun getUNBOUND ()Lcom/datadog/android/api/InternalLogger;
}

public final class com/datadog/android/api/InternalLogger$DefaultImpls {
public static synthetic fun log$default (Lcom/datadog/android/api/InternalLogger;Lcom/datadog/android/api/InternalLogger$Level;Lcom/datadog/android/api/InternalLogger$Target;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZILjava/lang/Object;)V
public static synthetic fun log$default (Lcom/datadog/android/api/InternalLogger;Lcom/datadog/android/api/InternalLogger$Level;Ljava/util/List;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZILjava/lang/Object;)V
public static synthetic fun log$default (Lcom/datadog/android/api/InternalLogger;Lcom/datadog/android/api/InternalLogger$Level;Lcom/datadog/android/api/InternalLogger$Target;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZLjava/util/Map;ILjava/lang/Object;)V
public static synthetic fun log$default (Lcom/datadog/android/api/InternalLogger;Lcom/datadog/android/api/InternalLogger$Level;Ljava/util/List;Lkotlin/jvm/functions/Function0;Ljava/lang/Throwable;ZLjava/util/Map;ILjava/lang/Object;)V
}

public final class com/datadog/android/api/InternalLogger$Level : java/lang/Enum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ interface InternalLogger {
* @param throwable an optional throwable error
* @param onlyOnce whether only one instance of the message should be sent per lifetime of the
* logger (default is `false`)
* @param additionalProperties additional properties to add to the log
*/
fun log(
level: Level,
target: Target,
messageBuilder: () -> String,
throwable: Throwable? = null,
onlyOnce: Boolean = false
onlyOnce: Boolean = false,
additionalProperties: Map<String, Any?>? = null
)

/**
Expand All @@ -83,13 +85,15 @@ interface InternalLogger {
* @param throwable an optional throwable error
* @param onlyOnce whether only one instance of the message should be sent per lifetime of the
* logger (default is `false`, onlyOnce applies to each target independently)
* @param additionalProperties additional properties to add to the log
*/
fun log(
level: Level,
targets: List<Target>,
messageBuilder: () -> String,
throwable: Throwable? = null,
onlyOnce: Boolean = false
onlyOnce: Boolean = false,
additionalProperties: Map<String, Any?>? = null
)

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ internal class SdkInternalLogger(
target: InternalLogger.Target,
messageBuilder: () -> String,
throwable: Throwable?,
onlyOnce: Boolean
onlyOnce: Boolean,
additionalProperties: Map<String, Any?>?
) {
when (target) {
InternalLogger.Target.USER -> logToUser(level, messageBuilder, throwable, onlyOnce)
Expand All @@ -68,7 +69,8 @@ internal class SdkInternalLogger(
level,
messageBuilder,
throwable,
onlyOnce
onlyOnce,
additionalProperties
)
}
}
Expand All @@ -78,10 +80,11 @@ internal class SdkInternalLogger(
targets: List<InternalLogger.Target>,
messageBuilder: () -> String,
throwable: Throwable?,
onlyOnce: Boolean
onlyOnce: Boolean,
additionalProperties: Map<String, Any?>?
) {
targets.forEach {
log(level, it, messageBuilder, throwable)
log(level, it, messageBuilder, throwable, onlyOnce, additionalProperties)
}
}

Expand Down Expand Up @@ -148,7 +151,8 @@ internal class SdkInternalLogger(
level: InternalLogger.Level,
messageBuilder: () -> String,
error: Throwable?,
onlyOnce: Boolean
onlyOnce: Boolean,
additionalProperties: Map<String, Any?>?
) {
val rumFeature = sdkCore?.getFeature(Feature.RUM_FEATURE_NAME) ?: return
val message = messageBuilder()
Expand All @@ -166,14 +170,20 @@ internal class SdkInternalLogger(
error != null
) {
mapOf(
"type" to "telemetry_error",
"message" to message,
"throwable" to error
TYPE_KEY to "telemetry_error",
MESSAGE_KEY to message,
THROWABLE_KEY to error
)
} else if (!additionalProperties.isNullOrEmpty()) {
mapOf(
TYPE_KEY to "telemetry_debug",
MESSAGE_KEY to message,
ADDITIONAL_PROPERTIES_KEY to additionalProperties
)
} else {
mapOf(
"type" to "telemetry_debug",
"message" to message
TYPE_KEY to "telemetry_debug",
MESSAGE_KEY to message
)
}
rumFeature.sendEvent(telemetryEvent)
Expand Down Expand Up @@ -201,6 +211,10 @@ internal class SdkInternalLogger(
companion object {
internal const val SDK_LOG_TAG = "DD_LOG"
internal const val DEV_LOG_TAG = "Datadog"
private const val MESSAGE_KEY = "message"
private const val TYPE_KEY = "type"
private const val THROWABLE_KEY = "throwable"
private const val ADDITIONAL_PROPERTIES_KEY = "additionalProperties"
}

// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ internal class DatadogTest {
eq(InternalLogger.Target.USER),
capture(),
isNull(),
eq(false)
eq(false),
eq(null)
)
val actualMessage = firstValue()
val filteredActualMessage = actualMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,72 @@ internal class SdkInternalLoggerTest {
)
}

@Test
fun `𝕄 send telemetry log 𝕎 log { TELEMETRY target, additional properties + info or debug }`(
@StringForgery fakeMessage: String,
forge: Forge
) {
// Given
val fakeAdditionalProperties = forge.aMap {
forge.anAlphabeticalString() to forge.aNullable { anAlphabeticalString() }
}
val mockLambda: () -> String = mock()
whenever(mockLambda.invoke()) doReturn fakeMessage
val fakeLevel = forge.anElementFrom(InternalLogger.Level.INFO, InternalLogger.Level.DEBUG)
val mockRumFeatureScope = mock<FeatureScope>()
whenever(mockSdkCore.getFeature(Feature.RUM_FEATURE_NAME)) doReturn mockRumFeatureScope

// When
testedInternalLogger.log(
fakeLevel,
InternalLogger.Target.TELEMETRY,
mockLambda,
null,
additionalProperties = fakeAdditionalProperties
)

// Then
verify(mockRumFeatureScope)
.sendEvent(
mapOf(
"type" to "telemetry_debug",
"message" to fakeMessage,
"additionalProperties" to fakeAdditionalProperties
)
)
}

@Test
fun `𝕄 send telemetry log 𝕎 log { TELEMETRY target, additional prop empty + info or debug }`(
@StringForgery fakeMessage: String,
forge: Forge
) {
// Given
val mockLambda: () -> String = mock()
whenever(mockLambda.invoke()) doReturn fakeMessage
val fakeLevel = forge.anElementFrom(InternalLogger.Level.INFO, InternalLogger.Level.DEBUG)
val mockRumFeatureScope = mock<FeatureScope>()
whenever(mockSdkCore.getFeature(Feature.RUM_FEATURE_NAME)) doReturn mockRumFeatureScope

// When
testedInternalLogger.log(
fakeLevel,
InternalLogger.Target.TELEMETRY,
mockLambda,
null,
additionalProperties = emptyMap()
)

// Then
verify(mockRumFeatureScope)
.sendEvent(
mapOf(
"type" to "telemetry_debug",
"message" to fakeMessage
)
)
}

@Test
fun `𝕄 send telemetry log 𝕎 log { TELEMETRY target, no throwable + warn or error }`(
@StringForgery fakeMessage: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ internal class HostsSanitizerTest {
eq(InternalLogger.Target.USER),
capture(),
isNull(),
eq(false)
eq(false),
eq(null)
)
assertThat(allValues.map { it() })
.containsExactlyInAnyOrderElementsOf(expectedMessages)
Expand Down Expand Up @@ -171,7 +172,8 @@ internal class HostsSanitizerTest {
eq(InternalLogger.Target.USER),
capture(),
isNull(),
eq(false)
eq(false),
eq(null)
)
assertThat(allValues.map { it() })
.containsExactlyInAnyOrderElementsOf(expectedMessages)
Expand Down Expand Up @@ -256,7 +258,8 @@ internal class HostsSanitizerTest {
eq(InternalLogger.Target.USER),
capture(),
isNull(),
eq(false)
eq(false),
eq(null)
)
assertThat(allValues.map { it() })
.containsExactlyInAnyOrderElementsOf(expectedMessages)
Expand Down Expand Up @@ -289,7 +292,8 @@ internal class HostsSanitizerTest {
eq(InternalLogger.Target.USER),
capture(),
any<MalformedURLException>(),
eq(false)
eq(false),
eq(null)
)
assertThat(allValues.map { it() })
.containsExactlyInAnyOrderElementsOf(expectedMessages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ internal class DatadogDataConstraintsTest {
eq(InternalLogger.Target.USER),
capture(),
isNull(),
eq(false)
eq(false),
eq(null)
)

assertThat(allValues.map { it() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ internal class ConsentAwareStorageTest {
eq(listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY)),
any(),
isA<RejectedExecutionException>(),
eq(false)
eq(false),
eq(null)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ internal class PlainBatchFileReaderWriterTest {
eq(listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY)),
capture(),
any(),
eq(false)
eq(false),
eq(null)
)
assertThat(firstValue())
.isEqualTo(PlainBatchFileReaderWriter.ERROR_WRITE.format(Locale.US, file.path))
Expand Down Expand Up @@ -221,7 +222,8 @@ internal class PlainBatchFileReaderWriterTest {
eq(listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY)),
capture(),
any(),
eq(false)
eq(false),
eq(null)
)
assertThat(firstValue())
.isEqualTo(PlainBatchFileReaderWriter.ERROR_WRITE.format(Locale.US, file.path))
Expand Down Expand Up @@ -253,7 +255,8 @@ internal class PlainBatchFileReaderWriterTest {
eq(listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY)),
capture(),
any(),
eq(false)
eq(false),
eq(null)
)
assertThat(firstValue())
.isEqualTo(PlainBatchFileReaderWriter.ERROR_READ.format(Locale.US, file.path))
Expand All @@ -279,7 +282,8 @@ internal class PlainBatchFileReaderWriterTest {
eq(listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY)),
capture(),
any(),
eq(false)
eq(false),
eq(null)
)
assertThat(firstValue())
.isEqualTo(PlainBatchFileReaderWriter.ERROR_READ.format(Locale.US, file.path))
Expand Down Expand Up @@ -384,7 +388,8 @@ internal class PlainBatchFileReaderWriterTest {
eq(InternalLogger.Target.MAINTAINER),
capture(),
any(),
eq(false)
eq(false),
eq(null)
)
assertThat(firstValue())
.isEqualTo(PlainBatchFileReaderWriter.ERROR_FAILED_META_PARSE)
Expand Down
Loading

0 comments on commit 3377375

Please sign in to comment.