-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Configure Sentry crash reporting #129
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
composeApp/src/commonMain/kotlin/org/ooni/probe/shared/monitoring/AppLogger.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.ooni.probe.shared.monitoring | ||
|
||
import co.touchlab.kermit.LogWriter | ||
import co.touchlab.kermit.Severity | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.datetime.LocalDateTime | ||
import okio.Path.Companion.toPath | ||
import org.ooni.probe.data.disk.DeleteFiles | ||
import org.ooni.probe.data.disk.ReadFile | ||
import org.ooni.probe.data.disk.WriteFile | ||
import org.ooni.probe.shared.now | ||
import org.ooni.probe.ui.shared.logFormat | ||
|
||
class AppLogger( | ||
private val readFile: ReadFile, | ||
private val writeFile: WriteFile, | ||
private val deleteFiles: DeleteFiles, | ||
private val backgroundDispatcher: CoroutineDispatcher, | ||
) { | ||
private val log = MutableStateFlow(emptyList<String>()) | ||
|
||
fun read(severity: Severity?): Flow<List<String>> = | ||
log | ||
.onStart { | ||
if (log.value.isEmpty()) { | ||
log.value = readFile(FILE_PATH).orEmpty().lines() | ||
} | ||
} | ||
.map { lines -> | ||
if (severity == null) { | ||
lines | ||
} else { | ||
lines.filter { line -> | ||
line.contains(": ${severity.name.uppercase()} :") | ||
} | ||
} | ||
} | ||
|
||
suspend fun clear() { | ||
withContext(backgroundDispatcher) { | ||
log.value = emptyList() | ||
deleteFiles(FILE_PATH) | ||
} | ||
} | ||
|
||
val logWriter = object : LogWriter() { | ||
override fun isLoggable( | ||
tag: String, | ||
severity: Severity, | ||
): Boolean = severity != Severity.Verbose | ||
|
||
override fun log( | ||
severity: Severity, | ||
message: String, | ||
tag: String, | ||
throwable: Throwable?, | ||
) { | ||
CoroutineScope(backgroundDispatcher).launch { | ||
val logMessage = | ||
"${LocalDateTime.now().logFormat()} : ${severity.name.uppercase()} : $message" | ||
log.update { lines -> | ||
val newLines = (lines + logMessage).takeLast(MAX_LINES) | ||
writeFile(FILE_PATH, newLines.joinToString("\n"), append = false) | ||
newLines | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val FILE_PATH = "Log/logger.txt".toPath() | ||
private const val MAX_LINES = 1000 | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
composeApp/src/commonMain/kotlin/org/ooni/probe/shared/monitoring/CrashMonitoring.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.ooni.probe.shared.monitoring | ||
|
||
import co.touchlab.kermit.LogWriter | ||
import co.touchlab.kermit.Severity | ||
import io.sentry.kotlin.multiplatform.Sentry | ||
import io.sentry.kotlin.multiplatform.SentryLevel | ||
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.onEach | ||
import org.ooni.probe.data.models.SettingsKey | ||
import org.ooni.probe.data.repositories.PreferenceRepository | ||
|
||
class CrashMonitoring( | ||
private val preferencesRepository: PreferenceRepository, | ||
) { | ||
private var isEnabled = false | ||
|
||
suspend fun setup() { | ||
preferencesRepository.getValueByKey(SettingsKey.SEND_CRASH) | ||
.onEach { sendCrash -> | ||
if (sendCrash == true) { | ||
Sentry.init { | ||
it.dsn = SENTRY_DSN | ||
} | ||
isEnabled = true | ||
} else { | ||
isEnabled = false | ||
Sentry.close() | ||
} | ||
} | ||
.collect() | ||
} | ||
|
||
val logWriter = object : LogWriter() { | ||
override fun isLoggable( | ||
tag: String, | ||
severity: Severity, | ||
): Boolean = isEnabled && severity != Severity.Verbose | ||
|
||
override fun log( | ||
severity: Severity, | ||
message: String, | ||
tag: String, | ||
throwable: Throwable?, | ||
) { | ||
if (!isEnabled) return | ||
|
||
if (severity == Severity.Error) { | ||
if (throwable != null) { | ||
addBreadcrumb(severity, message, tag) | ||
Sentry.captureException(throwable) | ||
} else { | ||
Sentry.captureMessage(message) | ||
} | ||
} else { | ||
addBreadcrumb(severity, message, tag) | ||
} | ||
} | ||
|
||
private fun addBreadcrumb( | ||
severity: Severity, | ||
message: String, | ||
tag: String, | ||
) { | ||
Sentry.addBreadcrumb( | ||
Breadcrumb( | ||
level = when (severity) { | ||
Severity.Verbose, | ||
Severity.Debug, | ||
-> SentryLevel.DEBUG | ||
|
||
Severity.Info -> SentryLevel.INFO | ||
Severity.Warn -> SentryLevel.WARNING | ||
Severity.Error -> SentryLevel.ERROR | ||
Severity.Assert -> SentryLevel.ERROR | ||
}, | ||
type = when (severity) { | ||
Severity.Debug -> "debug" | ||
Severity.Error -> "error" | ||
else -> "default" | ||
}, | ||
message = message, | ||
category = tag, | ||
), | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val SENTRY_DSN = | ||
"https://9dcd83d9519844188803aa817cdcd416@o155150.ingest.sentry.io/5619989" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If sentry is the only thing forcing us to jump versions, can we use an older version of sentry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was me during bug fixing. But I just copied the minimum we had on the iOS config files. I think we had a lower minimum here than there, so I just matched it. Our minimum is 15.3 right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its
13.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, then I have to fix it on a bunch of different places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #133