Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add support for app-services error reporting.
Browse files Browse the repository at this point in the history
- Added new component that hooks into the new app-services error
  reporter and listens for events.  When it sees an event it submits it
  to the crash reporter.
- Added support for customizing crash reports for these errors.
  • Loading branch information
bendk authored and mergify[bot] committed Jun 14, 2022
1 parent 1d07779 commit ac1a2df
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .buildconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ projects:
path: components/support/android-test
description: 'A collection of helpers for testing components from instrumented (on device) tests.'
publish: true
support-rusterrors:
path: components/support/rusterrors
description: 'A bridge for reporting Rust errors to Sentry/Glean'
publish: true
support-test-appservices:
path: components/support/test-appservices
description: 'A component for synchronizing Application Services'' unit testing dependencies used in Android Components.'
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ object Dependencies {
const val mozilla_full_megazord = "org.mozilla.appservices:full-megazord:${Versions.mozilla_appservices}"
const val mozilla_full_megazord_forUnitTests = "org.mozilla.appservices:full-megazord-forUnitTests:${Versions.mozilla_appservices}"

const val mozilla_errorsupport = "org.mozilla.appservices:errorsupport:${Versions.mozilla_appservices}"
const val mozilla_rustlog = "org.mozilla.appservices:rustlog:${Versions.mozilla_appservices}"
const val mozilla_sync15 = "org.mozilla.appservices:sync15:${Versions.mozilla_appservices}"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.concept.base.crash

/**
* Crash report for rust errors
*
* We implement this on exception classes that correspond to Rust errors to
* customize how the crash reports look.
*
* CrashReporting implementors should test if exceptions implement this
* interface. If so, they should try to customize their crash reports to match.
*/
interface RustCrashReport {
val typeName: String
val message: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import androidx.annotation.GuardedBy
import androidx.annotation.VisibleForTesting
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.SentryOptions.BeforeSendCallback
import io.sentry.android.core.SentryAndroid
import io.sentry.protocol.SentryId
import mozilla.components.Build
import mozilla.components.concept.base.crash.RustCrashReport
import mozilla.components.lib.crash.Crash
import mozilla.components.lib.crash.service.CrashReporterService
import java.util.Locale
Expand Down Expand Up @@ -131,6 +134,27 @@ class SentryService(
options.isEnableNdk = false
options.dsn = dsn
options.environment = environment
options.beforeSend = BeforeSendCallback { event, _ ->
val throwable = event.throwable
if (throwable is RustCrashReport) {
alterEventForRustCrash(event, throwable)
}
event
}
}
}

private fun alterEventForRustCrash(event: SentryEvent, crash: RustCrashReport) {
event.fingerprints = listOf(crash.typeName)
// Sentry supports multiple exceptions in an event, modify
// the top-level one controls how the event is displayed
//
// It's technically possible for the event to have a null
// or empty exception list, but that shouldn't happen in
// practice.
event.exceptions?.firstOrNull()?.let { sentryException ->
sentryException.type = crash.typeName
sentryException.value = crash.message
}
}

Expand Down
5 changes: 5 additions & 0 deletions components/support/rusterrors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Handles errors that come from Rust functions.

This component defines and installs an application-services `ApplicationErrorReporter` class that:
- Forwords error reports and breadcrumbs to `SentryServices`
- Reports error counts to Glean
38 changes: 38 additions & 0 deletions components/support/rusterrors/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion config.compileSdkVersion

defaultConfig {
minSdkVersion config.minSdkVersion
targetSdkVersion config.targetSdkVersion
}

lintOptions {
warningsAsErrors true
abortOnError true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
consumerProguardFiles 'proguard-rules-consumer.pro'
}
}
}

dependencies {
implementation Dependencies.mozilla_errorsupport
implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines
implementation project(':support-base')
}

apply from: '../../../publish.gradle'
ext.configurePublish(config.componentsGroupId, archivesBaseName, project.ext.description)
1 change: 1 addition & 0 deletions components/support/rusterrors/proguard-rules-consumer.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ProGuard rules for consumers of this library.
25 changes: 25 additions & 0 deletions components/support/rusterrors/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/sebastian/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
5 changes: 5 additions & 0 deletions components/support/rusterrors/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this file,
- You can obtain one at http://mozilla.org/MPL/2.0/. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mozilla.components.support.rusterrors" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.support.rusterrors

import mozilla.appservices.errorsupport.ApplicationErrorReporter
import mozilla.appservices.errorsupport.setApplicationErrorReporter
import mozilla.components.concept.base.crash.Breadcrumb
import mozilla.components.concept.base.crash.CrashReporting
import mozilla.components.concept.base.crash.RustCrashReport

/**
* Initialize application services error reporting
*
* Errors reports and breadcrumbs from Application Services will be forwarded
* to the CrashReporting instance. Error counting, which is used for expected
* errors like network errors, will be counted with Glean.
*/
public fun initializeRustErrors(crashReporter: CrashReporting) {
setApplicationErrorReporter(AndroidComponentsErrorReportor(crashReporter))
}

internal class AppServicesErrorReport(
override val typeName: String,
override val message: String,
) : Exception(typeName), RustCrashReport

private class AndroidComponentsErrorReportor(
val crashReporter: CrashReporting
) : ApplicationErrorReporter {
override fun reportError(typeName: String, message: String) {
crashReporter.submitCaughtException(AppServicesErrorReport(typeName, message))
}

override fun reportBreadcrumb(message: String, module: String, line: UInt, column: UInt) {
crashReporter.recordCrashBreadcrumb(Breadcrumb("$module[$line]: $message"))
}
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ permalink: /changelog/
Use `listOf` instead of `longArrayOf` or call `.toList`
* `TimingDistributionMetricType.start` now always returns a valid `TimerId`, `TimingDistributionMetricType.stopAndAccumulate` always requires a `TimerId`.

* **support-rusterrors**
* 🆕 New component to report Rust errors

# 102.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v101.0.0...v102.0.1)
* [Milestone](https://github.com/mozilla-mobile/android-components/milestone/149?closed=1)
Expand Down
1 change: 1 addition & 0 deletions taskcluster/ci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ treeherder:
support-locale: support-locale
support-migration: support-migration
support-rusthttp: support-rusthttp
support-rusterrors: support-rusterrors
support-rustlog: support-rustlog
support-sync-telemetry: support-sync-telemetry
support-test-appservices: support-test-appservices
Expand Down

0 comments on commit ac1a2df

Please sign in to comment.