0%

macOS下搭建iOS端监控系统Sentry

什么是sentry

1
sentry是一个强大的分布式报错信息收集平台,图形化程度很高,可以帮助我们直观的发现当前部署的系统存在哪些问题。

准备工作

官方提供了两种部署方案

出于方便考虑,这里选择了docker

安装docker(只是Mac)

mac版docker 下载地址: https://download.docker.com/mac/stable/Docker.dmg

安装完成后终端执行 docker --version来查看版本号,确认是否安装成功。

搭建sentry(linux和Mac通用)

  1. 获取sentry

    从GitHub上获取最新的sentry

    git clone https://github.com/getsentry/onpremise.git

  2. 搭建sentry

    sentry的readme文件里有步骤

    1. 创建本地数据库和sentry容器

      docker volume create --name=sentry-data && docker volume create --name=sentry-postgres

      Docker volumes have to be created manually, as they are declared as external to be more durable.

    2. 创建配置文件.env

      cp -n .env.example .env

    3. 编译docker服务

      docker-compose build

    4. 生成secret key

      docker-compose run --rm web config generate-secret-key

      将key配置到.env文件里,SENTRY_SECRET_KEY: ‘***

    5. 构建数据库,创建一个超级用户

      docker-compose run --rm web upgrade

      这步会提示创建超级用户,用户名是邮箱,记住用户名和密码后面要用到。

    6. 启动所有服务

      docker-compose up -d

    7. 浏览器中输入localhost:9000,即可访问sentry服务了,用户名和密码用第5步设置的。

iOS项目接入sentry

可以参考sentry官方文档https://docs.sentry.io/clients/cocoa/

  1. 导入sentry

    podfile文件加入

    pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '4.1.0'

    terminal执行pod install

  2. 项目配置

    appdelegate文件didfinishLaunch方法中,dsn登录sentry后在新建Objective-C项目里查看。

    1
    2
    3
    4
    5
    6
    7
    NSError *error = nil;
    SentryClient *client = [[SentryClient alloc] initWithDsn:@"https://<key>@sentry.io/<project>" didFailWithError:&error];
    SentryClient.sharedClient = client;
    [SentryClient.sharedClient startCrashHandlerWithError:&error];
    if (nil != error) {
    NSLog(@"%@", error);
    }

以上两步之后,就可以正常上传crash日志了