iOS集成Sentry崩潰收集日常操作

前序

這玩意我是真的不太喜歡,但是應(yīng)領(lǐng)導(dǎo)需求,因?yàn)檫@玩意可以架在自己的服務(wù)器上,從某種程度上能夠避免自己項(xiàng)目的信息外露吧,或許是個(gè)優(yōu)勢(shì)?無奈只能選擇集成,集成的過程中遇到了不少問題,只怪官方文檔寫的渣渣,簡(jiǎn)單的不行,所以很多問題都需要自己解決了。

集成

按道理講,這是個(gè)很簡(jiǎn)單的過程,也就三步:

  1. pods安裝Sentry
  2. 在項(xiàng)目中啟用Sentry(填入自己服務(wù)器的Sentry地址)
  3. 配置dsym文件的上傳(官方推薦fastlane上傳)

以上的第一二步按照文檔來基本不費(fèi)功夫,這里就不費(fèi)口舌了,主要是第三步的內(nèi)容,真是遇到了不少問題。這里的上傳需要用sentry-cli這個(gè)工具來進(jìn)行上傳,安裝如下:

#方法一
brew install getsentry/tools/sentry-cli
#方法二
curl -sL [https://sentry.io/get-cli/](https://sentry.io/get-cli/) | bash

安裝完成之后,可以查看是否安裝成功了,如下命令:

sentry-cli --version

好了,這個(gè)時(shí)候就當(dāng)你已經(jīng)安裝成功之后了吧~

在上傳的時(shí)候可以通過兩種方法去進(jìn)行上傳:

方法一:通過fastlane 配置上傳內(nèi)容

fastlane的安裝這里不在敘述,說一下插件的安裝,進(jìn)入項(xiàng)目文件夾后,可以通過命令fastlane add_plugin sentry 進(jìn)行插件的安裝,完成之后,我們可以通過查看fastlane 文件夾下的Pluginfiles來確定插件的安裝情況,如下:

# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!

gem 'fastlane-plugin-pgyer'
gem 'fastlane-plugin-sentry'

完成插件安裝之后,我們需要配置一下sentrylane,打開Fastfile,創(chuàng)建一個(gè)新的lane,配置內(nèi)容如下:

platform :ios do
lane :upload_symbols do

  #服務(wù)器地址
  sentry_api_host = "XXXX"
  #授權(quán)token
  sentry_auth_token = "XXXX"
  #服務(wù)器組織名稱(存放項(xiàng)目的名稱)
  sentry_org_slug = "XXXX"
  #項(xiàng)目工作空間
  sentry_workspace = "XXXX.xcworkspace"

  puts "scheme->#{schemeName} , workspace-> #{sentry_workspace}"
  sentry_upload_dsym(
    url: "#{sentry_api_host}",
    auth_token: "#{sentry_auth_token}",
    org_slug: "#{sentry_org_slug}",
    project_slug: "#{project_slug}",
    # 默認(rèn)不用指定當(dāng)前的dsym內(nèi)容
    # dsym_path: './XXXX.app.dSYM.zip'
  )

end
end

這里sentry_upload_dsym動(dòng)作參數(shù)可能發(fā)生改變,可以通過命令fastlane action sentry_upload_dsym 來查看相關(guān)的參數(shù)信息介紹:

+------------------------+---------+--------------------------+
|                        Used plugins                         |
+------------------------+---------+--------------------------+
| Plugin                 | Version | Action                   |
+------------------------+---------+--------------------------+
| fastlane-plugin-pgyer  | 0.2.2   | pgyer                    |
| fastlane-plugin-sentry | 1.5.0   | sentry_upload_dsym       |
|                        |         | sentry_upload_sourcemap  |
|                        |         | sentry_finalize_release  |
|                        |         | sentry_upload_file       |
|                        |         | sentry_create_release    |
+------------------------+---------+--------------------------+

Loading documentation for sentry_upload_dsym:

+------------------------------------------------------------------------------------------------------------------+
|                                                sentry_upload_dsym                                                |
+------------------------------------------------------------------------------------------------------------------+
| Upload dSYM symbolication files to Sentry                                                                        |
|                                                                                                                  |
| This action allows you to upload symbolication files to Sentry. It's extra useful if you use it to download the  |
| latest dSYM files from Apple when you use Bitcode                                                                |
|                                                                                                                  |
| Created by joshdholtz, HazAT                                                                                     |
+------------------------------------------------------------------------------------------------------------------+

+--------------+----------------------------+---------------------+---------+
|                        sentry_upload_dsym Options                         |
+--------------+----------------------------+---------------------+---------+
| Key          | Description                | Env Var             | Default |
+--------------+----------------------------+---------------------+---------+
| url          | Url for Sentry             | SENTRY_URL          |         |
| auth_token   | Authentication token for   | SENTRY_AUTH_TOKEN   |         |
|              | Sentry                     |                     |         |
| api_key      | API key for Sentry         | SENTRY_API_KEY      |         |
| org_slug     | Organization slug for      | SENTRY_ORG_SLUG     |         |
|              | Sentry project             |                     |         |
| project_slug | Project slug for Sentry    | SENTRY_PROJECT_SLUG |         |
| dsym_path    | Path to your symbols       | SENTRY_DSYM_PATH    |         |
|              | file. For iOS and Mac      |                     |         |
|              | provide path to            |                     |         |
|              | app.dSYM.zip               |                     |         |
| dsym_paths   | Path to an array of your   | SENTRY_DSYM_PATHS   |         |
|              | symbols file. For iOS and  |                     |         |
|              | Mac provide path to        |                     |         |
|              | app.dSYM.zip               |                     |         |
| symbol_maps  | Optional path to           | SENTRY_SYMBOL_MAPS  |         |
|              | bcsymbolmap files which    |                     |         |
|              | are used to resolve        |                     |         |
|              | hidden symbols in the      |                     |         |
|              | actual dsym files. This    |                     |         |
|              | requires the dsymutil      |                     |         |
|              | tool to be available       |                     |         |
| info_plist   | Optional path to           | SENTRY_INFO_PLIST   |         |
|              | Info.plist to add version  |                     |         |
|              | information when           |                     |         |
|              | uploading debug symbols    |                     |         |
+--------------+----------------------------+---------------------+---------+
* = default value is dependent on the user's system

More information can be found on https://docs.fastlane.tools/actions/sentry_upload_dsym

這樣,fastlane上傳的配置信息就此配置完畢。這一塊上傳可以結(jié)合fastlane自動(dòng)打包來進(jìn)行自動(dòng)化上傳,將此lane接在自動(dòng)化打包之后即可。

方法二:通過shell腳本上傳到服務(wù)器中

通常的做法就是在生成dsym文件之后,通過sentry-cli執(zhí)行shell腳本來上傳該文件,命令如下:

if which sentry-cli >/dev/null; then
#項(xiàng)目存放地兒的名稱
export SENTRY_ORG=XXXX
#項(xiàng)目名稱
export SENTRY_PROJECT=XXXX
#授權(quán)token
export SENTRY_AUTH_TOKEN=XXXX
#服務(wù)器地址
export SENTRY_URL=XXXX
#調(diào)試模式
export SENTRY_LOG_LEVEL=debug

ERROR=$(sentry-cli upload-dsym 2>&1 >/dev/null)
echo "success ~~"
if [ ! $? -eq 0 ]; then
echo "warning: sentry-cli - $ERROR"
fi
else
echo "warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
fi

可以存為.bash腳本,每次手動(dòng)進(jìn)行上傳,但是這樣難免會(huì)顯得繁瑣,所以,為了更加方便的去操作當(dāng)前的上傳工作并且解放自己的雙手,所以我們可以將當(dāng)前的操作集成在XCodebuild phases中已達(dá)到自動(dòng)化的目的,在對(duì)應(yīng)的target中選擇build phases添加新的phase,如下:

配置

緊接著,在內(nèi)容中添加如下配置內(nèi)容:


# # 腳本默認(rèn)配置的版本格式為CFBundleShortVersionString(CFBundleVersion),  如果你修改默認(rèn)的版本格式, 請(qǐng)?jiān)O(shè)置此變量, 如果不想修改, 請(qǐng)忽略此設(shè)置
#CUSTOMIZED_APP_VERSION=""
#
# # Debug模式編譯是否上傳,1=上傳 0=不上傳,默認(rèn)不上傳
UPLOAD_DEBUG_SYMBOLS=0
#
# # 模擬器編譯是否上傳,1=上傳 0=不上傳,默認(rèn)不上傳
UPLOAD_SIMULATOR_SYMBOLS=0
#
#只有Archive操作時(shí)上傳, 1=支持Archive上傳 0=所有Release模式編譯都上傳
UPLOAD_ARCHIVE_ONLY=0

# 打印錯(cuò)誤信息
function exitWithMessage(){
echo "--------------------------------"
echo "${1}"
echo "--------------------------------"
exit ${2}
}

function sentryUpload(){

#Type a script or drag a script file from your workspace to insert its path.
if which sentry-cli >/dev/null; then
#項(xiàng)目存放地兒的名稱
export SENTRY_ORG=XXXX
#項(xiàng)目名稱
export SENTRY_PROJECT=XXXX
#授權(quán)token
export SENTRY_AUTH_TOKEN=XXXX
#服務(wù)器地址
export SENTRY_URL=XXXX
#調(diào)試模式
export SENTRY_LOG_LEVEL=debug

ERROR=$(sentry-cli upload-dsym 2>&1 >/dev/null)
echo "success ~~"
if [ ! $? -eq 0 ]; then
echo "warning: sentry-cli - $ERROR"
fi
else
echo "warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
fi

}

# 在Xcode工程中執(zhí)行
function runInXcode(){

echo "Uploading dSYM to Sentry in Xcode ..."
echo "Info.Plist : ${INFOPLIST_FILE}"

BUNDLE_VERSION=$(/usr/libexec/PlistBuddy -c 'Print CFBundleVersion' "${INFOPLIST_FILE}")
BUNDLE_SHORT_VERSION=$(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "${INFOPLIST_FILE}")


echo "--------------------------------"
echo "Prepare application information."
echo "--------------------------------"

echo "Product Name: ${PRODUCT_NAME}"
echo "Bundle Identifier: ${BUNDLE_IDENTIFIER}"
echo "Version: ${BUNDLE_SHORT_VERSION}"
echo "Build: ${BUNDLE_VERSION}"


echo "--------------------------------"
echo "Check the arguments ..."

##檢查模擬器編譯是否允許上傳符號(hào)
if [ "$EFFECTIVE_PLATFORM_NAME" == "-iphonesimulator" ]; then
if [ $UPLOAD_SIMULATOR_SYMBOLS -eq 0 ]; then
exitWithMessage "Warning: Build for simulator and skipping to upload. \nYou can modify 'UPLOAD_SIMULATOR_SYMBOLS' to 1 in the script." 0
fi
fi

##檢查是否是Release模式編譯
if [ "${CONFIGURATION=}" == "Debug" ]; then
if [ $UPLOAD_DEBUG_SYMBOLS -eq 0 ]; then
exitWithMessage "Warning: Build for debug mode and skipping to upload. \nYou can modify 'UPLOAD_DEBUG_SYMBOLS' to 1 in the script." 0
fi
fi

##檢查是否Archive操作
if [ $UPLOAD_ARCHIVE_ONLY -eq 1 ]; then
if [[ "$TARGET_BUILD_DIR" == *"/Archive"* ]]; then
echo "Archive the package"
else
exitWithMessage "Warning: Build for NOT Archive mode and skipping to upload. \nYou can modify 'UPLOAD_ARCHIVE_ONLY' to 0 in the script." 0
fi
fi

#上傳dSYM files
sentryUpload

}


# 根據(jù)Xcode的環(huán)境變量判斷是否處于Xcode環(huán)境
INFO_PLIST_FILE="${INFOPLIST_FILE}"

BuildInXcode="F"
if [ -f "${INFO_PLIST_FILE}" ]; then
BuildInXcode="T"
fi

if [ $BuildInXcode = "T" ]; then
runInXcode
else
echo "echo Not in XCode environment "
fi

配置完成之后,這樣就能夠在每次打包release的時(shí)候自動(dòng)將dsym文件上傳到自己的服務(wù)器了。

注意

關(guān)于sentry環(huán)境的區(qū)分,一開始我一直以為需要?jiǎng)?chuàng)建兩個(gè)項(xiàng)目來進(jìn)行區(qū)分sentry的環(huán)境(測(cè)試環(huán)境與正式環(huán)境)。后來發(fā)現(xiàn),在一個(gè)項(xiàng)目中,可以通過選擇不同的release內(nèi)容進(jìn)行區(qū)分(實(shí)際上是以bundleID)進(jìn)行區(qū)分的,由于公司項(xiàng)目的測(cè)試環(huán)境與正式環(huán)境使用了Target進(jìn)行區(qū)分且bundleID也不一樣,所以剛好能夠通過選擇該內(nèi)容進(jìn)行環(huán)境的區(qū)分。

假如bundleID是同一個(gè)的話,那么此時(shí)建議創(chuàng)建一個(gè)新的項(xiàng)目進(jìn)行區(qū)分即可,這樣在上傳dsym的時(shí)候也能夠獨(dú)立上傳,從而避免dsym上傳覆蓋問題的發(fā)生。

最后

關(guān)于dsym文件的上傳,個(gè)人比較傾向于第二種方法吧,因?yàn)闊o論使用fastlane還是XCode打包,都會(huì)執(zhí)行對(duì)應(yīng)的phase腳本內(nèi)容,也就是說文件dsym肯定會(huì)被上傳到服務(wù)器的,而且不會(huì)有遺漏情況。

怎么說呢,這玩意用著不習(xí)慣,還是比較喜歡bugly。但是由于它可以自己搭建,所以對(duì)于一些保密的項(xiàng)目的崩潰收集還是比較不錯(cuò)的一種選擇,各有優(yōu)劣,重在選擇,你覺得呢?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容