Skip to content

Commit

Permalink
feat: sentry crash report
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed May 7, 2024
1 parent 6fa3c68 commit 6ac0c8b
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.vscode/
build/
local.properties
sentry.properties
obj/
service_account_credentials.json
__pycache__/
Expand Down
143 changes: 143 additions & 0 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import io.sentry.android.gradle.extensions.InstrumentationFeature
import io.sentry.android.gradle.instrumentation.logcat.LogcatLevel

apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "io.sentry.android.gradle"

repositories {
maven {
Expand Down Expand Up @@ -43,6 +47,8 @@ configurations {
def keystorePwd = null
def alias = null
def pwd = null
def sentry_token = System.getenv("SENTRY_AUTH_TOKEN")
def sentry_upload = project.rootProject.file("sentry.properties").exists()
def disableCMakeRelWithDebInfo = System.getenv("COMPILE_NATIVE") == null

Properties properties
Expand All @@ -59,6 +65,7 @@ if (properties != null) {
keystorePwd = properties.getProperty("KEYSTORE_PASS")
alias = properties.getProperty("ALIAS_NAME")
pwd = properties.getProperty("ALIAS_PASS")
sentry_token = properties.getProperty("SENTRY_AUTH_TOKEN", sentry_token)
}

keystorePwd = keystorePwd ?: System.getenv("KEYSTORE_PASS")
Expand Down Expand Up @@ -366,3 +373,139 @@ android {
}

}

sentry {
// Disables or enables debug log output, e.g. for for sentry-cli.
// Default is disabled.
debug = false

// The slug of the Sentry organization to use for uploading proguard mappings/source contexts.
org = "pagermaid"

// The slug of the Sentry project to use for uploading proguard mappings/source contexts.
projectName = "nagram"

// The authentication token to use for uploading proguard mappings/source contexts.
// WARNING: Do not expose this token in your build.gradle files, but rather set an environment
// variable and read it into this property.
authToken = sentry_token

// The url of your Sentry instance. If you're using SAAS (not self hosting) you do not have to
// set this. If you are self hosting you can set your URL here
url = null

// Disables or enables the handling of Proguard mapping for Sentry.
// If enabled the plugin will generate a UUID and will take care of
// uploading the mapping to Sentry. If disabled, all the logic
// related to proguard mapping will be excluded.
// Default is enabled.
includeProguardMapping = sentry_upload

// Whether the plugin should attempt to auto-upload the mapping file to Sentry or not.
// If disabled the plugin will run a dry-run and just generate a UUID.
// The mapping file has to be uploaded manually via sentry-cli in this case.
// Default is enabled.
autoUploadProguardMapping = sentry_upload

// Experimental flag to turn on support for GuardSquare's tools integration (Dexguard and External Proguard).
// If enabled, the plugin will try to consume and upload the mapping file produced by Dexguard and External Proguard.
// Default is disabled.
dexguardEnabled = false

// Disables or enables the automatic configuration of Native Symbols
// for Sentry. This executes sentry-cli automatically so
// you don't need to do it manually.
// Default is disabled.
uploadNativeSymbols = sentry_upload

// Whether the plugin should attempt to auto-upload the native debug symbols to Sentry or not.
// If disabled the plugin will run a dry-run.
// Default is enabled.
autoUploadNativeSymbols = sentry_upload

// Does or doesn't include the source code of native code for Sentry.
// This executes sentry-cli with the --include-sources param. automatically so
// you don't need to do it manually.
// This option has an effect only when [uploadNativeSymbols] is enabled.
// Default is disabled.
includeNativeSources = false

// Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
// This enables source context, allowing you to see your source
// code as part of your stack traces in Sentry.
includeSourceContext = sentry_upload

// Configure additional directories to be included in the source bundle which is used for
// source context. The directories should be specified relative to the Gradle module/project's
// root. For example, if you have a custom source set alongside 'main', the parameter would be
// 'src/custom/java'.
additionalSourceDirsForSourceContext = []

// Enable or disable the tracing instrumentation.
// Does auto instrumentation for specified features through bytecode manipulation.
// Default is enabled.
tracingInstrumentation {
enabled = true

// Specifies a set of instrumentation features that are eligible for bytecode manipulation.
// Defaults to all available values of InstrumentationFeature enum class.
features = [InstrumentationFeature.DATABASE, InstrumentationFeature.FILE_IO, InstrumentationFeature.OKHTTP, InstrumentationFeature.COMPOSE]

// Enable or disable logcat instrumentation through bytecode manipulation.
// Default is enabled.
logcat {
enabled = true

// Specifies a minimum log level for the logcat breadcrumb logging.
// Defaults to LogcatLevel.WARNING.
minLevel = LogcatLevel.WARNING
}

// The set of glob patterns to exclude from instrumentation. Classes matching any of these
// patterns in the project's sources and dependencies JARs won't be instrumented by the Sentry
// Gradle plugin.
//
// Don't include the file extension. Filtering is done on compiled classes and
// the .class suffix isn't included in the pattern matching.
//
// Example usage:
// ```
// excludes = ['com/example/donotinstrument/**', '**/*Test']
// ```
//
// Only supported when using Android Gradle plugin (AGP) version 7.4.0 and above.
excludes = []
}

// Enable auto-installation of Sentry components (sentry-android SDK and okhttp, timber and fragment integrations).
// Default is enabled.
// Only available v3.1.0 and above.
autoInstallation {
enabled = true

// Specifies a version of the sentry-android SDK and fragment, timber and okhttp integrations.
//
// This is also useful, when you have the sentry-android SDK already included into a transitive dependency/module and want to
// align integration versions with it (if it's a direct dependency, the version will be inferred).
//
// NOTE: if you have a higher version of the sentry-android SDK or integrations on the classpath, this setting will have no effect
// as Gradle will resolve it to the latest version.
//
// Defaults to the latest published Sentry version.
sentryVersion = '7.8.0'
}

// Disables or enables dependencies metadata reporting for Sentry.
// If enabled, the plugin will collect external dependencies and
// upload them to Sentry as part of events. If disabled, all the logic
// related to the dependencies metadata report will be excluded.
//
// Default is enabled.
includeDependenciesReport = true

// Whether the plugin should send telemetry data to Sentry.
// If disabled the plugin won't send telemetry data.
// This is auto disabled if running against a self hosted instance of Sentry.
// Default is enabled.
telemetry = true
}
2 changes: 2 additions & 0 deletions TMessagesProj/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@
android:allowAudioPlaybackCapture="true"
tools:replace="android:supportsRtl">

<meta-data android:name="io.sentry.auto-init" android:value="false" />

<activity-alias
android:enabled="true"
android:name="org.telegram.messenger.DefaultIcon"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.NekoXConfig;
import tw.nekomimi.nekogram.helpers.AnalyticsHelper;
import tw.nekomimi.nekogram.parts.SignturesKt;
import tw.nekomimi.nekogram.utils.FileUtil;
import xyz.nextalone.nagram.NaConfig;
Expand Down Expand Up @@ -111,6 +112,7 @@ protected void attachBaseContext(Context base) {
}
Thread.currentThread().setUncaughtExceptionHandler((thread, error) -> {
Log.e("nekox", "from " + thread.toString(), error);
AnalyticsHelper.captureException(error);
});
}

Expand Down Expand Up @@ -297,6 +299,8 @@ public void onCreate() {

super.onCreate();

AnalyticsHelper.start(this);

if (BuildVars.LOGS_ENABLED) {
FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime()));
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tw.nekomimi.nekogram.helpers;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import org.telegram.messenger.BuildConfig;
import org.telegram.messenger.BuildVars;


import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.android.core.SentryAndroid;

public class AnalyticsHelper {
public static String DSN = "https://58f21343622b885cb7ad43fee0943f77@o416616.ingest.us.sentry.io/4507212454428672";
public static boolean loaded = false;

public static void start(Application application) {
if (!getSentryStatus(application)) {
return;
}
SentryAndroid.init(application, options -> {
options.setDsn(DSN);
options.setEnvironment(BuildVars.DEBUG_VERSION ? "debug" : "release");
options.setEnableAutoSessionTracking(true);
options.setTracesSampleRate(1.0);
options.setAttachAnrThreadDump(true);
options.setRelease(BuildConfig.APPLICATION_ID + "@" + BuildConfig.VERSION_NAME + "+" + BuildConfig.VERSION_CODE);
options.setBeforeScreenshotCaptureCallback((event, hint, debounce) -> {
// always capture crashed events
if (event.isCrashed()) {
return true;
}

// if debounce is active, skip capturing
if (debounce) {
return false;
} else {
// also capture fatal events
return event.getLevel() == SentryLevel.FATAL;
}
});
});
loaded = true;
}

public static void captureException(Throwable e) {
if (loaded) {
Sentry.captureException(e);
}
}

public static boolean getSentryStatus(Application application) {
SharedPreferences preferences = application.getApplicationContext().getSharedPreferences(
"nkmrcfg",
Context.MODE_PRIVATE
);
return preferences.getBoolean("SentryAnalytics", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
private final AbstractConfigCell doNotShareMyPhoneNumberRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDoNotShareMyPhoneNumber()));
private final AbstractConfigCell disableSuggestionViewRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableSuggestionView()));
private final AbstractConfigCell disableAutoWebLoginRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableAutoWebLogin()));
private final AbstractConfigCell sentryAnalyticsRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getSentryAnalytics()));
private final AbstractConfigCell divider6 = cellGroup.appendCell(new ConfigCellDivider());

private final AbstractConfigCell header7 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("General")));
Expand Down Expand Up @@ -497,6 +498,8 @@ public void onItemClick(int id) {
ApplicationLoader.startPushService();
} else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
} else if (key.equals(NaConfig.INSTANCE.getSentryAnalytics().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ object NaConfig {
ConfigItem.configTypeBool,
false
)
val sentryAnalytics =
addConfig(
"SentryAnalytics",
ConfigItem.configTypeBool,
true
)

private fun addConfig(
k: String,
Expand Down
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@
<string name="PreviewSendPhoto">发送图片</string>
<string name="PreviewSendVideo">发送视频</string>
<string name="PreviewSendFile">发送文件</string>
<string name="SentryAnalytics">Sentry 崩溃自动上报</string>
</resources>
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@
<string name="PreviewSendPhoto">Send Photo</string>
<string name="PreviewSendVideo">Send Video</string>
<string name="PreviewSendFile">Send File</string>
<string name="SentryAnalytics">Sentry Crash Report</string>
</resources>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ buildscript {
//noinspection GradleDependency
classpath "com.github.triplet.gradle:play-publisher:3.6.0"
classpath 'cn.hutool:hutool-core:5.7.13'
classpath "io.sentry:sentry-android-gradle-plugin:4.5.1"
}
}

Expand Down

0 comments on commit 6ac0c8b

Please sign in to comment.