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

fix: only take unique error logs and control limit #188

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 10 additions & 3 deletions core/src/main/java/com/amplitude/core/utilities/Diagnostics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import java.util.Collections

class Diagnostics() {
private var malformedEvents: MutableList<String>? = null
private var errorLogs: MutableList<String>? = null
private var errorLogs: MutableSet<String>? = null
qingzhuozhen marked this conversation as resolved.
Show resolved Hide resolved
qingzhuozhen marked this conversation as resolved.
Show resolved Hide resolved

companion object {
private const val MAX_ERROR_LOGS = 10
}

fun addMalformedEvent(event: String) {
if (malformedEvents == null) {
Expand All @@ -15,7 +19,10 @@ class Diagnostics() {

fun addErrorLog(log: String) {
if (errorLogs == null) {
errorLogs = Collections.synchronizedList(mutableListOf())
errorLogs = Collections.synchronizedSet(mutableSetOf())
}
if (errorLogs!!.size >= MAX_ERROR_LOGS) {
return
qingzhuozhen marked this conversation as resolved.
Show resolved Hide resolved
}
errorLogs?.add(log)
}
Expand All @@ -37,7 +44,7 @@ class Diagnostics() {
diagnostics["malformed_events"] = malformedEvents!!
}
if (errorLogs != null && errorLogs!!.isNotEmpty()) {
diagnostics["error_logs"] = errorLogs!!
diagnostics["error_logs"] = errorLogs!!.toList()
}
val result = diagnostics.toJSONObject().toString()
malformedEvents?.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ class DiagnosticsTest {
assertEquals("{\"error_logs\":[\"log\"]}", diagnostics.extractDiagnostics())
}

@Test
fun `test duplicate error logs`() {
val diagnostics = Diagnostics()
diagnostics.addErrorLog("log")
diagnostics.addErrorLog("log")
assertTrue(diagnostics.hasDiagnostics())
assertEquals("{\"error_logs\":[\"log\"]}", diagnostics.extractDiagnostics())
}

@Test
fun `test we only take have 10 error logs if many`() {
val diagnostics = Diagnostics()
for (i in 1..15) {
diagnostics.addErrorLog("log$i")
}
assertTrue(diagnostics.hasDiagnostics())
assertEquals(
"{\"error_logs\":[\"log1\",\"log2\",\"log3\",\"log4\",\"log5\",\"log6\",\"log7\",\"log8\",\"log9\",\"log10\"]}",
diagnostics.extractDiagnostics(),
)
assertFalse(diagnostics.hasDiagnostics())
}

@Test
fun `test hasDiagnostics`() {
val diagnostics = Diagnostics()
Expand Down