Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions kmp/src/commonMain/kotlin/com/onesignal/logger/CrashData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ package com.onesignal.logger
* so the platform is responsible for translating its native crash representation
* into this neutral shape before handing it to [ILogCrashReporter].
*
* @property attributed false when the platform could not prove the crash is ours. Those are
* captured anyway, so a failed attribution shows up in the data instead of a dropped report.
* @property attributed false when the platform could not prove the crash is ours, which is
* retained rather than dropped so it shows up in the data. No default on purpose: `true`
* would silently mark every forgotten call site as proven.
*/
data class CrashData(
val threadName: String,
val exceptionType: String,
val exceptionMessage: String,
val stacktrace: String,
val attributed: Boolean = true,
val attributed: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ILogCrashReporter {
/**
* Records a fatal, crash-class event on the retained, disk-buffered crash telemetry
* (`Severity.FATAL`, tagged `ossdk.crash.fatal = true`). Use for real crashes and
* foreground ANRs.
* foreground ANRs. Also tagged `ossdk.crash.attributed` from [CrashData.attributed].
*/
@Throws(Exception::class)
fun saveCrash(crash: CrashData)
Expand All @@ -23,6 +23,7 @@ interface ILogCrashReporter {
* `Severity.WARN` and tagged `ossdk.crash.fatal = false`, so it stays out of any
* severity-based crash/ANR metric while remaining queryable. Use for backgrounded
* main-thread blocks and other retained warnings that are not user-visible crashes.
* Also tagged `ossdk.crash.attributed` from [CrashData.attributed].
*/
@Throws(Exception::class)
fun saveNonFatal(crash: CrashData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class LogCrashReporter(
fatal: Boolean,
) {
val label = if (fatal) "crash report" else "non-fatal report"
logger.info("LogCrashReporter: saving $label for ${crash.exceptionType}")
logger.info("LogCrashReporter: saving $label for ${crash.exceptionType} (attributed=${crash.attributed})")

val body = crash.exceptionMessage.ifBlank { crash.exceptionType }
val record =
Expand Down
41 changes: 36 additions & 5 deletions kmp/src/commonTest/kotlin/com/onesignal/logger/LogCrashTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.onesignal.logger
import com.onesignal.logger.attributes.LogFieldsPerEvent
import com.onesignal.logger.attributes.LogFieldsTopLevel
import com.onesignal.logger.internal.LogTelemetryRemoteImpl
import com.onesignal.logger.otlp.OtlpLogEncoder
import kotlinx.coroutines.test.runTest
import kotlin.coroutines.cancellation.CancellationException
import kotlin.test.Test
Expand Down Expand Up @@ -37,6 +38,7 @@ class LogCrashTest {
exceptionType = "java.lang.NullPointerException",
exceptionMessage = "npe message",
stacktrace = "stack...",
attributed = true,
),
)

Expand All @@ -61,7 +63,7 @@ class LogCrashTest {
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())

reporter.saveCrash(
CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s"),
CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s", attributed = true),
)

val record = parseProto(store.entries[0].bytes).message(1).message(2).message(2)
Expand All @@ -80,7 +82,7 @@ class LogCrashTest {
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())

reporter.saveCrash(
CrashData(threadName = "main", exceptionType = "Anr", exceptionMessage = "blocked", stacktrace = "s"),
CrashData(threadName = "main", exceptionType = "Anr", exceptionMessage = "blocked", stacktrace = "s", attributed = true),
)

val record = parseProto(store.entries[0].bytes).message(1).message(2).message(2)
Expand All @@ -97,7 +99,7 @@ class LogCrashTest {
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())

reporter.saveCrash(
CrashData(threadName = "", exceptionType = "E", exceptionMessage = "m", stacktrace = "s"),
CrashData(threadName = "", exceptionType = "E", exceptionMessage = "m", stacktrace = "s", attributed = true),
)

val record = parseProto(store.entries[0].bytes).message(1).message(2).message(2)
Expand All @@ -112,7 +114,7 @@ class LogCrashTest {
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())

reporter.saveNonFatal(
CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s"),
CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s", attributed = true),
)

val record = parseProto(store.entries[0].bytes).message(1).message(2).message(2)
Expand All @@ -127,7 +129,7 @@ class LogCrashTest {
val store = FakeFileStore()
val crashTelemetry = LoggerFactory.createCrashLocalTelemetry(provider, store)
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())
val crash = CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s")
val crash = CrashData(threadName = "main", exceptionType = "E", exceptionMessage = "m", stacktrace = "s", attributed = true)

reporter.saveCrash(crash.copy(attributed = false))
reporter.saveCrash(crash)
Expand All @@ -140,6 +142,35 @@ class LogCrashTest {
assertEquals(1L, boolFlag(logRecord(store, 3), "ossdk.crash.attributed"))
}

@Test
fun attributionSurvivesTrimmingOnAnOversizedCrash() = runTest {
// A real crash is mostly stacktrace, so it is the case that gets trimmed. The flag is
// worthless if it drops out of exactly those records.
val provider = FakePlatformProvider()
val store = FakeFileStore()
val crashTelemetry = LoggerFactory.createCrashLocalTelemetry(provider, store)
val reporter = LoggerFactory.createCrashReporter(crashTelemetry, RecordingLogger())

reporter.saveCrash(
CrashData(
threadName = "main",
exceptionType = "java.lang.OutOfMemoryError",
exceptionMessage = "m".repeat(50_000),
stacktrace = "\tat com.onesignal.Frame.method(Frame.kt:1)\n".repeat(5_000),
attributed = false,
),
)

// Input is ~265 KB, so the trim definitely ran; the lower bound proves it fitted the
// record rather than gutting it, which would make the flag assertions vacuous.
val payload = store.entries[0].bytes
assertTrue(payload.size <= OtlpLogEncoder.MAX_PAYLOAD_BYTES, "payload over cap: ${payload.size}")
assertTrue(payload.size > OtlpLogEncoder.MAX_PAYLOAD_BYTES / 2, "record was gutted: ${payload.size}")
val record = logRecord(store, 0)
assertEquals(0L, boolFlag(record, "ossdk.crash.attributed"))
assertEquals(1L, boolFlag(record, "ossdk.crash.fatal"))
}

// Reads every AnyValue.string_value stored under [key] on an encoded LogRecord message.
private fun stringAttribute(record: ProtoMessage, key: String): List<String> =
record.all(6)
Expand Down
Loading