Skip to content

Commit 871f434

Browse files
committed
Bypass limit for log properties when not sending data to Embrace
1 parent 1f73f64 commit 871f434

File tree

4 files changed

+51
-76
lines changed

4 files changed

+51
-76
lines changed

embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/logs/EmbraceLogService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import io.embrace.android.embracesdk.internal.otel.attrs.embExceptionHandling
1616
import io.embrace.android.embracesdk.internal.payload.AppFramework
1717
import io.embrace.android.embracesdk.internal.payload.Envelope
1818
import io.embrace.android.embracesdk.internal.session.orchestrator.PayloadStore
19-
import io.embrace.android.embracesdk.internal.utils.PropertyUtils.normalizeProperties
19+
import io.embrace.android.embracesdk.internal.utils.PropertyUtils.sanitizeProperties
2020
import io.embrace.android.embracesdk.internal.utils.Uuid
2121
import io.opentelemetry.semconv.incubating.LogIncubatingAttributes
2222

@@ -31,6 +31,7 @@ class EmbraceLogService(
3131
) : LogService {
3232

3333
private val behavior = configService.logMessageBehavior
34+
private val bypassLimitsValidation = configService.isOnlyUsingOtelExporters()
3435
private val logCounters = mapOf(
3536
Severity.INFO to LogCounter(behavior::getInfoLogLimit),
3637
Severity.WARNING to LogCounter(behavior::getWarnLogLimit),
@@ -45,7 +46,7 @@ class EmbraceLogService(
4546
customLogAttrs: Map<String, String>,
4647
logAttachment: Attachment.EmbraceHosted?,
4748
) {
48-
val redactedProperties = redactSensitiveProperties(normalizeProperties(properties))
49+
val redactedProperties = redactSensitiveProperties(sanitizeProperties(properties, bypassLimitsValidation))
4950
val attrs = createTelemetryAttributes(redactedProperties, customLogAttrs)
5051

5152
val schemaProvider: (TelemetryAttributes) -> SchemaType = when {

embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/utils/PropertyUtils.kt

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,21 @@ object PropertyUtils {
1010

1111
const val MAX_PROPERTY_SIZE: Int = 10
1212

13-
/**
14-
* This method will normalize the map by applying the following rules:
15-
*
16-
* - Null key registries will be discarded.
17-
* - Null value registries will be renamed to null as a String.
18-
* - Cap the properties map to a maximum of [PropertyUtils.MAX_PROPERTY_SIZE] properties.
19-
*
20-
* @param properties properties to be normalized.
21-
* @return a normalized Map of the provided properties.
22-
*/
23-
@JvmStatic
24-
fun sanitizeProperties(properties: Map<String, Any?>?): Map<String, Any> {
25-
properties ?: return emptyMap()
26-
27-
return properties.entries
28-
.take(MAX_PROPERTY_SIZE)
29-
.associate { Pair(it.key, checkIfSerializable(it.value)) }
30-
}
31-
32-
@JvmStatic
33-
fun normalizeProperties(properties: Map<String, Any>?): Map<String, Any>? {
34-
var normalizedProperties: Map<String, Any> = HashMap()
35-
if (properties != null) {
36-
runCatching {
37-
normalizedProperties = sanitizeProperties(properties)
38-
}
39-
return normalizedProperties
13+
fun sanitizeProperties(properties: Map<String, Any>?, bypassPropertyLimit: Boolean = false): Map<String, Any> {
14+
return if (properties == null) {
15+
emptyMap()
4016
} else {
41-
return null
17+
runCatching {
18+
if (bypassPropertyLimit) {
19+
properties.entries
20+
} else {
21+
properties.entries.take(MAX_PROPERTY_SIZE)
22+
}.associate { Pair(it.key, checkIfSerializable(it.value)) }
23+
}.getOrDefault(emptyMap())
4224
}
4325
}
4426

45-
private fun checkIfSerializable(value: Any?): Any {
46-
if (value == null) {
47-
return "null"
48-
}
27+
private fun checkIfSerializable(value: Any): Any {
4928
if (!(value is Parcelable || value is Serializable)) {
5029
return "not serializable"
5130
}

embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/utils/PropertiesTest.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/utils/PropertyUtilsTest.kt

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package io.embrace.android.embracesdk.internal.utils
22

33
import io.embrace.android.embracesdk.internal.utils.PropertyUtils.sanitizeProperties
44
import org.junit.Assert.assertEquals
5+
import org.junit.Assert.assertNotNull
6+
import org.junit.Assert.assertTrue
57
import org.junit.Test
68

79
internal class PropertyUtilsTest {
@@ -19,11 +21,6 @@ internal class PropertyUtilsTest {
1921
assertEquals(expected, sanitizeProperties(input as Map<String, Any>?))
2022
}
2123

22-
@Test
23-
fun testNullValue() {
24-
assertEquals("null", sanitizeProperties(mapOf("a" to null))["a"])
25-
}
26-
2724
@Test
2825
fun testSerializableValue() {
2926
val obj = SerializableClass()
@@ -35,6 +32,41 @@ internal class PropertyUtilsTest {
3532
assertEquals("not serializable", sanitizeProperties(mapOf("a" to UnSerializableClass()))["a"])
3633
}
3734

35+
@Test
36+
fun `bypass limits`() {
37+
val input = (0..20).associateBy { "$it" }
38+
assertEquals(input.size, sanitizeProperties(input, true).size)
39+
}
40+
41+
@Test
42+
fun testPropertiesNormalization() {
43+
val sourceMap: MutableMap<String, Any> = HashMap()
44+
sourceMap[""] = "Empty key"
45+
sourceMap["EmptyValue"] = ""
46+
sourceMap["NullValue"] = ""
47+
for (i in 1..9) {
48+
sourceMap["Key$i"] = "Value$i"
49+
}
50+
val resultMap = sanitizeProperties(sourceMap)
51+
assertTrue(
52+
"Unexpected normalized map size.",
53+
resultMap.size <= PropertyUtils.MAX_PROPERTY_SIZE
54+
)
55+
resultMap.entries.stream()
56+
.peek { (key): Map.Entry<String, Any> ->
57+
assertNotNull(
58+
"Unexpected normalized map key: NULL.",
59+
key
60+
)
61+
}
62+
.peek { (_, value): Map.Entry<String, Any> ->
63+
assertNotNull(
64+
"Unexpected normalized map value: NULL.",
65+
value
66+
)
67+
}
68+
}
69+
3870
private class SerializableClass : java.io.Serializable
3971
private class UnSerializableClass
4072
}

0 commit comments

Comments
 (0)