导读 Sentry是程序的哨兵,它可以监控我们在生产环境中项目的运行状态,一旦某段代码运行报错,或者异常,会第一时间把报错的路由,异常文件,请求方式 等一些非常详细的信息以消息或者邮件给我们,让我们第一时间知道:程序出错了,然后我们可以从Sentry给我们的详细的错误信息中瞬间找到我们需要处理的代码,从而快速地修复Bug。

Sentry简介

在程序的开发过程中,代码运行时难免会抛出异常,而且项目在部署到测试、生产环境后,我们便不可能像在开发时那样容易的及时发现处理错误了。

一般我们都是在错误发生一段时间后,错误信息才会传递到开发⼈员那里,然后一顿操作查看程序运行的日志,但是往往我们会因为日志中缺少上下文关系,导致很难分析真正的错误是什么。

Sentry由此应运而生成为了解决这个问题的一个很好的工具,设计了诸多特性帮助开发者更快、更方便、更直观的监控错误信息。

Sentry是什么?Sentry是程序的哨兵,它可以监控我们在生产环境中项目的运行状态,一旦某段代码运行报错,或者异常,会第一时间把报错的路由,异常文件,请求方式 等一些非常详细的信息以消息或者邮件给我们,让我们第一时间知道:程序出错了,然后我们可以从Sentry给我们的详细的错误信息中瞬间找到我们需要处理的代码,从而快速地修复Bug。

Sentry是一个集中式日志管理系统,它具备以下优点:

  • 多项目,多用户
  • 界面友好
  • 可以配置异常触发规则,例如监控到程序出现异常后发送邮件
  • 支持多种主流语言和框架,如 React、Angular、Node、Django、PHP、Android、.NET、JAVA等
  • Sentry目前支持的语言和框架众多,下面展示一部分:

    Sentry部署

    sentry分为收费版和免费自建版,本文主要是介绍免费版。官方提供了docker-compose,也是它推荐的部署方式,参考地址https://github.com/getsentry/onpremise。部署要求如下:

    Docker 19.03.6+
    Compose 1.28.0+
    Python 3
    4 核
    8 GB 内存
    20 GB 可用磁盘空间
    

    本篇仅介绍在ubuntu18.04下的安装。

    1、安装docker
    sudo apt-get remove docker docker-engine docker-ce docker.io
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    sudo apt-get update
    sudo apt-get install -y docker-ce
    sudo systemctl start docker
    2、安装docker-compose
    sudo apt-get install python-pip
    sudo pip install docker-compose
    3、安装git
    sudo apt-get update -y
    sudo apt install git
    4、sentry部署]/erji]

    git clone https://github.com/getsentry/onpremise.git
    cd onpremise
    sudo ./install.sh
    安装期间定义用户名和密码
    Email:[xxxxxxxx@qq.com](mailto:326968597@qq.com)
    Pawword:xxxxxx

    [erji]5、启动

    `sudo docker-compose up -d`
    
    6、登录sentry客户端

    打开浏览器输入:http://ip:9000/,即可显示如下图:

    Sentry使用

    本篇仅介绍在django中的应用,其它语言自行阅读官方文档。

    1、安装依赖
    pip install --upgrade sentry-sdk
    2、在settings.py中配置
    import sentry_sdk
    from sentry_sdk.integrations.django import DjangoIntegration
    sentry_sdk.init(
        dsn="https://xxxxxxxxxxxx.sentry.io/0",
        integrations=[DjangoIntegration()],
        traces_sample_rate=1.0,
        send_default_pii=True,
    )
    
    
    3、验证您的 Sentry 安装
    from django.urls import path
    def trigger_error(request):
      division_by_zero = 1 / 0
    urlpatterns = [
      path('sentry-debug/', trigger_error),
    ]

    添加完成之后我们访问这个路由,我们可以在Sentry后台看到告警信息如下图:

    原文来自:https://www.51cto.com/article/716111.html

    本文地址:https://www.linuxprobe.com/sentry-log-monitor.html编辑:清蒸github,审核员:逄增宝

    Linux命令大全:https://www.linuxcool.com/

    Linux系统大全:https://www.linuxdown.com/

    红帽认证RHCE考试心得:https://www.rhce.net/