Skip to content

Commit

Permalink
Add span name limit for internally logged spans
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Sep 11, 2024
1 parent 6c2d0e5 commit c25d6fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_CUSTOM
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_CUSTOM_ATTRIBUTE_KEY_LENGTH
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_CUSTOM_ATTRIBUTE_VALUE_LENGTH
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_CUSTOM_EVENT_COUNT
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_INTERNAL_NAME_LENGTH
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_NAME_LENGTH
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_TOTAL_EVENT_COUNT
import io.embrace.android.embracesdk.internal.utils.truncatedStacktraceText
Expand Down Expand Up @@ -333,6 +334,7 @@ internal class EmbraceSpanImpl(
internal fun attributeValid(key: String, value: String) =
key.length <= MAX_CUSTOM_ATTRIBUTE_KEY_LENGTH && value.length <= MAX_CUSTOM_ATTRIBUTE_VALUE_LENGTH

internal fun String.isValidName(internal: Boolean): Boolean = isNotBlank() && (internal || length <= MAX_NAME_LENGTH)
internal fun String.isValidName(internal: Boolean): Boolean =
isNotBlank() && ((internal && length <= MAX_INTERNAL_NAME_LENGTH) || length <= MAX_NAME_LENGTH)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.embrace.android.embracesdk.internal.spans

object EmbraceSpanLimits {
const val MAX_INTERNAL_NAME_LENGTH: Int = 2000
const val MAX_NAME_LENGTH: Int = 50
const val MAX_CUSTOM_EVENT_COUNT: Int = 10
const val MAX_TOTAL_EVENT_COUNT: Int = 11000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import io.embrace.android.embracesdk.arch.assertNotKeySpan
import io.embrace.android.embracesdk.arch.assertNotPrivateSpan
import io.embrace.android.embracesdk.fakes.FakeClock
import io.embrace.android.embracesdk.fakes.injection.FakeInitModule
import io.embrace.android.embracesdk.fixtures.MAX_LENGTH_INTERNAL_SPAN_NAME
import io.embrace.android.embracesdk.fixtures.MAX_LENGTH_SPAN_NAME
import io.embrace.android.embracesdk.fixtures.TOO_LONG_ATTRIBUTE_KEY
import io.embrace.android.embracesdk.fixtures.TOO_LONG_ATTRIBUTE_VALUE
import io.embrace.android.embracesdk.fixtures.TOO_LONG_INTERNAL_SPAN_NAME
import io.embrace.android.embracesdk.fixtures.TOO_LONG_SPAN_NAME
import io.embrace.android.embracesdk.fixtures.maxSizeAttributes
import io.embrace.android.embracesdk.fixtures.maxSizeEvents
Expand Down Expand Up @@ -484,6 +486,32 @@ internal class SpanServiceImplTest {
assertEquals(2, spanSink.completedSpans().size)
}

@Test
fun `check name length limit for spans by internally by the SDK`() {
assertNull(spansService.createSpan(name = TOO_LONG_INTERNAL_SPAN_NAME, internal = true))
assertFalse(
spansService.recordCompletedSpan(
name = TOO_LONG_INTERNAL_SPAN_NAME,
startTimeMs = 100L,
endTimeMs = 200L,
internal = true
)
)
assertNotNull(spansService.recordSpan(name = TOO_LONG_INTERNAL_SPAN_NAME, internal = true) { 1 })
assertEquals(0, spanSink.completedSpans().size)
assertNotNull(spansService.createSpan(name = MAX_LENGTH_INTERNAL_SPAN_NAME, internal = true))
assertNotNull(spansService.recordSpan(name = MAX_LENGTH_INTERNAL_SPAN_NAME, internal = true) { 2 })
assertTrue(
spansService.recordCompletedSpan(
name = MAX_LENGTH_INTERNAL_SPAN_NAME,
startTimeMs = 100L,
endTimeMs = 200L,
internal = true
)
)
assertEquals(2, spanSink.completedSpans().size)
}

@Test
fun `check events limit`() {
assertFalse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private const val MAX_EVENT_ATTRIBUTE_COUNT = 10

val MAX_LENGTH_SPAN_NAME: String = "s".repeat(EmbraceSpanLimits.MAX_NAME_LENGTH)
val TOO_LONG_SPAN_NAME: String = "s".repeat(EmbraceSpanLimits.MAX_NAME_LENGTH + 1)
val MAX_LENGTH_INTERNAL_SPAN_NAME: String = "s".repeat(EmbraceSpanLimits.MAX_INTERNAL_NAME_LENGTH)
val TOO_LONG_INTERNAL_SPAN_NAME: String = "s".repeat(EmbraceSpanLimits.MAX_INTERNAL_NAME_LENGTH + 1)
val MAX_LENGTH_EVENT_NAME: String = "s".repeat(MAX_EVENT_NAME_LENGTH)
val TOO_LONG_EVENT_NAME: String = "s".repeat(MAX_EVENT_NAME_LENGTH + 1)
val MAX_LENGTH_ATTRIBUTE_KEY: String = "s".repeat(EmbraceSpanLimits.MAX_CUSTOM_ATTRIBUTE_KEY_LENGTH)
Expand Down

0 comments on commit c25d6fb

Please sign in to comment.