/
SentryService.kt
214 lines (189 loc) · 7.79 KB
/
SentryService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* 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.lib.crash.sentry
import android.content.Context
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
import mozilla.components.concept.base.crash.Breadcrumb as MozillaBreadcrumb
/**
* A [CrashReporterService] implementation that uploads crash reports using
* the Sentry SDK version 5.6.1 and above.
*
* This implementation will add default tags to every sent crash report
* (like which Android Components version is being used) prefixed with "ac".
*
* @param applicationContext The application [Context].
* @param dsn Data Source Name of the Sentry server.
* @param tags A list of additional tags that will be sent together with crash reports.
* @param environment An optional, environment name string or null to set none
* @param sendEventForNativeCrashes Allows configuring if native crashes should be submitted. Disabled by default.
* @param sentryProjectUrl Base URL of the Sentry web interface pointing to the app/project.
* @param sendCaughtExceptions Allows configuring if caught exceptions should be submitted. Enabled by default.
*/
class SentryService(
private val applicationContext: Context,
private val dsn: String,
private val tags: Map<String, String> = emptyMap(),
private val environment: String? = null,
private val sendEventForNativeCrashes: Boolean = false,
private val sentryProjectUrl: String? = null,
private val sendCaughtExceptions: Boolean = true,
) : CrashReporterService {
override val id: String = "new-sentry-instance"
override val name: String = "New Sentry Instance"
@VisibleForTesting
@GuardedBy("this")
internal var isInitialized: Boolean = false
override fun createCrashReportUrl(identifier: String): String? {
return sentryProjectUrl?.let {
val id = identifier.replace("-", "")
return "$it&query=$id"
}
}
override fun report(crash: Crash.UncaughtExceptionCrash): String {
prepareReport(crash.breadcrumbs, SentryLevel.FATAL)
return reportToSentry(crash.throwable)
}
override fun report(crash: Crash.NativeCodeCrash): String? {
return if (sendEventForNativeCrashes) {
val level = when (crash.isFatal) {
true -> SentryLevel.FATAL
else -> SentryLevel.ERROR
}
prepareReport(crash.breadcrumbs, level)
return reportToSentry(crash)
} else {
null
}
}
override fun report(throwable: Throwable, breadcrumbs: ArrayList<MozillaBreadcrumb>): String? {
if (!sendCaughtExceptions) {
return null
}
prepareReport(breadcrumbs, SentryLevel.INFO)
return reportToSentry(throwable)
}
@VisibleForTesting
internal fun reportToSentry(throwable: Throwable): String {
return Sentry.captureException(throwable).alsoClearBreadcrumbs()
}
@VisibleForTesting
internal fun reportToSentry(crash: Crash.NativeCodeCrash): String {
return Sentry.captureMessage(createMessage(crash)).alsoClearBreadcrumbs()
}
private fun addDefaultTags() {
Sentry.setTag("ac.version", Build.version)
Sentry.setTag("ac.git", Build.gitHash)
Sentry.setTag("ac.as.build_version", Build.applicationServicesVersion)
Sentry.setTag("ac.glean.build_version", Build.gleanSdkVersion)
Sentry.setTag("user.locale", Locale.getDefault().toString())
tags.forEach { entry ->
Sentry.setTag(entry.key, entry.value)
}
}
@Synchronized
@VisibleForTesting
internal fun initIfNeeded() {
if (isInitialized) {
return
}
initSentry()
addDefaultTags()
isInitialized = true
}
@VisibleForTesting
internal fun initSentry() {
SentryAndroid.init(applicationContext) { options ->
// Disable uncaught non-native exceptions from being reported.
// We already have our own uncaught exception handler [ExceptionHandler],
// so we don't need Sentry's default one.
options.setEnableUncaughtExceptionHandler(false)
// Disable uncaught native exceptions from being reported.
// Sentry don't have a way to disable uncaught native exceptions from being reported.
// As a fallback we had to disable all native integrations.
// More info can be found https://github.com/getsentry/sentry-java/issues/1993
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
}
}
@VisibleForTesting
internal fun prepareReport(
breadcrumbs: ArrayList<MozillaBreadcrumb>,
level: SentryLevel? = null,
) {
initIfNeeded()
breadcrumbs.forEach {
Sentry.addBreadcrumb(it.toSentryBreadcrumb())
}
level?.apply {
Sentry.setLevel(level)
}
}
private fun SentryId.alsoClearBreadcrumbs(): String {
Sentry.clearBreadcrumbs()
return this.toString()
}
@VisibleForTesting
internal fun createMessage(crash: Crash.NativeCodeCrash): String {
val fatal = crash.isFatal.toString()
val processType = crash.processType
val minidumpSuccess = crash.minidumpSuccess
return "NativeCodeCrash(fatal=$fatal, processType=$processType, minidumpSuccess=$minidumpSuccess)"
}
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun MozillaBreadcrumb.toSentryBreadcrumb(): Breadcrumb {
val sentryLevel = this.level.toSentryBreadcrumbLevel()
val breadcrumb = Breadcrumb(this.date).apply {
message = this@toSentryBreadcrumb.message
category = this@toSentryBreadcrumb.category
level = sentryLevel
type = this@toSentryBreadcrumb.type.value
}
this.data.forEach {
breadcrumb.setData(it.key, it.value)
}
return breadcrumb
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun MozillaBreadcrumb.Level.toSentryBreadcrumbLevel() = when (this) {
MozillaBreadcrumb.Level.CRITICAL -> SentryLevel.FATAL
MozillaBreadcrumb.Level.ERROR -> SentryLevel.ERROR
MozillaBreadcrumb.Level.WARNING -> SentryLevel.WARNING
MozillaBreadcrumb.Level.INFO -> SentryLevel.INFO
MozillaBreadcrumb.Level.DEBUG -> SentryLevel.DEBUG
}