Skip to content

Commit

Permalink
[APPC-4447] Update sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo-frango committed Nov 11, 2024
1 parent a31f352 commit 292e72a
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ gen-external-apklibs

#Misc
*.keystore
android-magnessdk-5.3.0.aar
android-magnessdk-5.3.0.aar
10 changes: 10 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id("com.google.gms.google-services")
id("androidx.navigation.safeargs.kotlin")
id("de.mannodermaus.android-junit5")
id("io.sentry.android.gradle") version "4.11.0"
}

android {
Expand Down Expand Up @@ -144,3 +145,12 @@ dependencies {
testImplementation(libs.test.turbine)
testImplementation(libs.kotlin.coroutines.test)
}

sentry {
org.set("sentry")
projectName.set("wallet-app")

// this will upload your source code to Sentry to show it as part of the stack traces
// disable if you don't want to expose your sources
includeSourceContext.set(true)
}
40 changes: 0 additions & 40 deletions app/src/debug/AndroidManifest.xml

This file was deleted.

17 changes: 15 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@

<application
android:name="com.asfoundation.wallet.App"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:testOnly="false"
android:theme="@style/MaterialAppTheme"
tools:ignore="UnusedAttribute"
tools:replace="android:name, android:theme, android:allowBackup">
tools:replace="android:name, android:theme, android:dataExtractionRules, android:fullBackupContent">

<meta-data
android:name="android.autofill"
Expand Down Expand Up @@ -361,6 +362,18 @@
android:enabled="true"
android:exported="false" />

<!-- Required: set your sentry.io project identifier (DSN) -->
<meta-data android:name="io.sentry.dsn" android:value="https://0a16b453ee62a151a660be89da58c2cd@error.dev.catappult.io/3" />
<!-- enable automatic breadcrumbs for user interactions (clicks, swipes, scrolls) -->
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
<!-- enable screenshot for crashes (could contain sensitive/PII data) -->
<meta-data android:name="io.sentry.attach-screenshot" android:value="true" />
<!-- enable view hierarchy for crashes -->
<meta-data android:name="io.sentry.attach-view-hierarchy" android:value="true" />
<!-- enable the performance API by setting a sample-rate, adjust in production env -->
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
<!-- enable profiling when starting transactions, adjust in production env -->
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.asfoundation.wallet.analytics

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatDelegate
import com.appcoins.wallet.core.analytics.analytics.AnalyticsLabels
import com.appcoins.wallet.core.analytics.analytics.IndicativeAnalytics
import com.appcoins.wallet.core.analytics.analytics.partners.PartnerAddressService
Expand All @@ -22,8 +20,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.Completable
import io.reactivex.Single
import io.sentry.Sentry
import io.sentry.android.AndroidSentryClientFactory
import io.sentry.event.User
import io.sentry.protocol.User
import javax.inject.Inject

class InitilizeDataAnalytics @Inject constructor(
Expand All @@ -37,30 +34,37 @@ class InitilizeDataAnalytics @Inject constructor(
) {

fun initializeSentry(): Completable {
Sentry.init(
BuildConfig.SENTRY_DSN_KEY,
AndroidSentryClientFactory(context.applicationContext as Application)
)
/*Sentry.init { options ->
options.dsn = BuildConfig.SENTRY_DSN_KEY
options.enableTracing = true
options.isDebug = true
}*/
val walletAddress = idsRepository.getActiveWalletAddress()
Sentry.getContext().apply {
user = User(walletAddress, null, null, null)
addExtra(AnalyticsLabels.USER_LEVEL, idsRepository.getGamificationLevel())
addExtra(AnalyticsLabels.HAS_GMS, hasGms())

val user = User().apply {
id = walletAddress
}

Sentry.setUser(user)
Sentry.setTag(AnalyticsLabels.USER_LEVEL, idsRepository.getGamificationLevel().toString())
Sentry.setTag(AnalyticsLabels.HAS_GMS, hasGms().toString())

logger.addReceiver(SentryReceiver())

return Completable.mergeArray(
idsRepository.getInstallerPackage(BuildConfig.APPLICATION_ID)
.doOnSuccess {
Sentry.getContext()
.addExtra(AnalyticsLabels.ENTRY_POINT, it.ifEmpty { "other" })
Sentry.setTag(AnalyticsLabels.ENTRY_POINT, it.ifEmpty { "other" })
}
.ignoreElement()
.onErrorComplete(),
promoCodeLocalDataSource.getSavedPromoCode()
.flatMap {
promotionsRepository.getWalletOrigin(walletAddress, it.code)
}
.doOnSuccess { Sentry.getContext().addExtra(AnalyticsLabels.WALLET_ORIGIN, it) }
.doOnSuccess {
Sentry.setTag(AnalyticsLabels.WALLET_ORIGIN, it.toString())
}
.ignoreElement()
.onErrorComplete()
)
Expand Down Expand Up @@ -111,7 +115,7 @@ class InitilizeDataAnalytics @Inject constructor(
isEmulator = it.deviceInfo.isProbablyEmulator,
ghOemId = it.ghOemId,
promoCode = it.promoCode.code ?: "",
flavor = mapFlavor(BuildConfig.FLAVOR),
flavor = mapFlavor(),
theme = it.theme
)
}
Expand All @@ -120,7 +124,7 @@ class InitilizeDataAnalytics @Inject constructor(
.ignoreElement()
}

private fun mapFlavor(flavor: String): String {
private fun mapFlavor(flavor: String = BuildConfig.FLAVOR): String {
return when (flavor) {
"aptoide" -> "aptoide"
"gp" -> "google"
Expand All @@ -129,7 +133,8 @@ class InitilizeDataAnalytics @Inject constructor(
}

fun getTheme(): String {
val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val currentNightMode =
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return mapTheme(currentNightMode == Configuration.UI_MODE_NIGHT_YES)
}

Expand Down
40 changes: 19 additions & 21 deletions app/src/main/java/com/asfoundation/wallet/logging/SentryReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,45 @@ package com.asfoundation.wallet.logging

import com.appcoins.wallet.core.utils.jvm_common.LogReceiver
import io.sentry.Sentry
import io.sentry.event.Breadcrumb
import io.sentry.event.BreadcrumbBuilder
import io.sentry.event.Event
import io.sentry.event.EventBuilder
import io.sentry.SentryLevel

class SentryReceiver : LogReceiver {

override fun log(tag: String?, throwable: Throwable?) {
throwable?.let {
Sentry.capture(throwable)
Sentry.captureException(it)
}
}

override fun log(tag: String?, message: String?, asError: Boolean, addToBreadcrumbs: Boolean) {
message?.let {
if (asError) {
val errorEvent = EventBuilder()
errorEvent.withLevel(Event.Level.ERROR)
errorEvent.withMessage(it)
Sentry.capture(errorEvent)
Sentry.captureMessage(it) { scope ->
scope.level = SentryLevel.ERROR
if (tag != null) {
scope.setTag("category", tag)
}
}
} else {
Sentry.capture("$tag: $message")
Sentry.captureMessage("$tag: $message")
}

if (addToBreadcrumbs) {
Sentry.getContext().recordBreadcrumb(
BreadcrumbBuilder()
.setType(Breadcrumb.Type.DEFAULT)
.setLevel(Breadcrumb.Level.ERROR)
.setMessage(tag)
.setCategory(tag)
.setData(mapOf(Pair("error", message)))
.build()
)
Sentry.captureMessage(tag ?: "Breadcrumb") { scope ->
scope.level = SentryLevel.ERROR
scope.setExtra("error", message)
if (tag != null) {
scope.setTag("category", tag)
}
}
}
}
}

override fun log(tag: String?, message: String?, throwable: Throwable?) {
throwable?.let {
Sentry.capture("$tag: $message")
Sentry.capture(it)
Sentry.captureMessage("$tag: $message")
Sentry.captureException(it)
}
}
}
17 changes: 17 additions & 0 deletions app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
<exclude domain="root" />
<exclude domain="file" />
<exclude domain="database" />
<exclude domain="sharedpref" />
<exclude domain="external" />
</cloud-backup>
<device-transfer>
<exclude domain="root" />
<exclude domain="file" />
<exclude domain="database" />
<exclude domain="sharedpref" />
<exclude domain="external" />
</device-transfer>
</data-extraction-rules>
3 changes: 2 additions & 1 deletion core/analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ dependencies {
implementation(libs.bundles.network)
implementation(libs.androidx.multidex)
implementation(libs.google.play.services)
}
implementation(libs.analytics.sentry)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
package com.appcoins.wallet.core.analytics.analytics

import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import io.sentry.Sentry
import io.sentry.event.Breadcrumb
import io.sentry.event.BreadcrumbBuilder
import io.sentry.event.User
import io.sentry.SentryLevel
import io.sentry.protocol.User
import org.json.JSONObject
import javax.inject.Inject

class SentryAnalytics @Inject constructor(
@ApplicationContext private val context: Context
) :
AnalyticsSetup {
class SentryAnalytics @Inject constructor() : AnalyticsSetup {

override fun setUserId(walletAddress: String) {
val old = Sentry.getContext().user.id
Sentry.getContext().recordBreadcrumb(
BreadcrumbBuilder()
.setType(Breadcrumb.Type.USER)
.setLevel(Breadcrumb.Level.INFO)
.setMessage("Changing wallet from $old to $walletAddress")
.setCategory("wallet")
.build()
)
Sentry.getContext().user = User(walletAddress, null, null, null)
var oldUserId = "unknown"
Sentry.withScope { scope ->
oldUserId = scope.user?.id ?: "unknown"

scope.setTag("category", "wallet")
scope.level = SentryLevel.INFO

val user = User().apply {
id = walletAddress
}
scope.user = user
}

// Capture the message AFTER setting the new user and outside the withScope block
Sentry.captureMessage("Changing wallet from $oldUserId to $walletAddress")
}

override fun setGamificationLevel(level: Int) {
Sentry.getContext().addExtra(AnalyticsLabels.USER_LEVEL, level)
Sentry.withScope { scope ->
scope.setTag(AnalyticsLabels.USER_LEVEL, level.toString())
}
}

override fun setWalletOrigin(origin: String) {
Sentry.getContext().addExtra(AnalyticsLabels.WALLET_ORIGIN, origin)
Sentry.withScope { scope ->
scope.setTag(AnalyticsLabels.WALLET_ORIGIN, origin)
}
}

override fun setPromoCode(code: String?, bonus: Double?, validity: Int?, appName: String?) {
Expand All @@ -41,6 +44,8 @@ class SentryAnalytics @Inject constructor(
promoCode.put("bonus", bonus)
promoCode.put("validity", validity)
promoCode.put("appName", appName)
Sentry.getContext().addExtra(AnalyticsLabels.PROMO_CODE, promoCode)
Sentry.withScope { scope ->
scope.setTag(AnalyticsLabels.PROMO_CODE, promoCode.toString())
}
}
}
Loading

0 comments on commit 292e72a

Please sign in to comment.