Sentry部署实践

3,559 阅读6分钟

使用官方文档进行部署。

1、准备工作

  • 1)sentry版本

选择部署的是选择21.4.1版本。 下载地址:github.com/getsentry/o…

可以直接下载zip包上传到服务器(虚拟机)

  • 2)操作系统版本

本次选择centOS 8.2版本。

建议使用root账号进行部署,如使用其它账号也需具备sudo权限,以下部署以root进行。

也可以选择其它版本差不多的Linux系统。

  • 3)部署要求

Docker 19.03.6+

Compose 1.24.1+

4 CPU Cores

8 GB RAM

20 GB Free Disk Space

  • 4)Python版本

官网建议:Sentry uses Python 3 by default since December 4th, 2020 and Sentry 21.1.0 is the last version to support Python 2.

选择Python 3.8版本。

2、Docker部署

  • 1)设置最新镜像

yum install -y yum-utils

yum-config-manager
--add-repo
download.docker.com/linux/cento…

  • 2)部署Docker

yum install docker-ce docker-ce-cli containerd.io -y

  • 3)启动Docker

systemctl enable docker

systemctl start docker

  • 4)检查Docker

docker version

Client: Docker Engine - Community
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:44:36 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:43:02 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

3、Python部署

部署Compose前需要部署Python。
  • 1)部署Python3.8

yum install python38 -y

  • 2)检查Python

python3.8

[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

4、Compose部署

  • 1)部署Compose

pip3.8 install compose

  • 2)检查Compose

docker-compose version

docker-compose version 1.29.2, build unknown
docker-py version: 5.0.0
CPython version: 3.8.3
OpenSSL version: OpenSSL 1.1.1c FIPS  28 May 2019

5、Sentry部署

  • 1)创建目录

mkdir -p /usr/sentry

  • 2)上传安装包

    通过ftp工具或rz命令上传,以下以rz命令上传到对用目录

cd /usr/sentry && rz

  • 3)解压安装包

unzip onpremise-21.4.0.zip

cd /usr/sentry/onpremise-21.4.0

ls -ltr

  • 4)自动化安装

./install.sh

▶ Parsing command line ...
  
▶ Setting up error handling ...

▶ Checking minimum requirements ...

▶ Creating volumes for persistent storage ...
Created sentry-clickhouse.
Created sentry-data.
Created sentry-kafka.
Created sentry-postgres.
Created sentry-redis.
Created sentry-symbolicator.
Created sentry-zookeeper.

▶ Ensuring files from examples ...
Creating ../sentry/sentry.conf.py...
Creating ../sentry/config.yml...
Creating ../symbolicator/config.yml...
Creating ../sentry/requirements.txt...

▶ Generating secret key ...
Secret key written to ../sentry/config.yml

▶ Replacing TSDB ...

▶ Fetching and updating Docker images ...
...
...

部署过程全程日志输出,Fetching and updating Docker images这过程需要拉取sentry所有组件的docker镜像,需要时间较多。 更多日志查看sentry_install_log-yyyy-MM-dd_xx.txt。当出现"Would you like to create a user account now? [Y/n]:"时,按提示创建登录账号,然后继续等待剩余部署。

...
...
▶ Setting up GeoIP integration ...
Setting up IP address geolocation ...
Installing (empty) IP address geolocation database ... done.
IP address geolocation is not configured for updates.
See https://develop.sentry.dev/self-hosted/geolocation/ for instructions.
Error setting up IP address geolocation.


-----------------------------------------------------------------

You're all done! Run the following command to get Sentry running:

  docker-compose up -d

-----------------------------------------------------------------

当看到上述日志时,证明sentry部署成功,如因为其他报错导致部署终止,需根据日志进行排查,然后重新执行部署。

  • 5)启动sentry

docker-compose up -d

  • 6)检查启动情况

docker ps -a

通过STATUS查看各个组件启动是否正常,通过docker logs CONTAINER ID 命令查看具体报错

  • 7)登录sentry

浏览器访问:http://IP:9000,具体地址根据虚拟机IP而定。

6、开启https

非https的项目可以直接跳过第6步

  • 1)页面修改dsn地址

在首次登录sentry页面后,将Root Web参数修改为https开头,并勾选TSL选项。

  • 2)开启sentry.conf.py配置

cd /usr/sentry/onpremise-21.4.0/sentry && vim sentry.conf.py

找到以下参数并开启

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
  • 3)配置nginx 开启https转发

cd /usr/sentry/onpremise-21.4.0/nginx

mv nginx.conf nginx.conf.bak (备份原文件)

vim nginx.conf (创建新配置文件,复制以下内容)

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
	        worker_connections 1024;
}



http {
  # set REMOTE_ADDR from any internal proxies
  # see http://nginx.org/en/docs/http/ngx_http_realip_module.html
  set_real_ip_from 127.0.0.1;
  set_real_ip_from 10.0.0.0/8;
  real_ip_header X-Forwarded-For;
  real_ip_recursive on;
 
  # SSL configuration -- change these certs to match yours
  ssl_certificate      /etc/nginx/ssl.crt;
  ssl_certificate_key  /etc/nginx/ssl.key;
 
  # NOTE: These settings may not be the most-current recommended
  # defaults
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:128m;
  ssl_session_timeout 10m;
 
  server {
    listen   80;
 
    location / {
      if ($request_method = GET) {
        rewrite  ^ https://$host$request_uri? permanent;
      }
      return 405;
    }
  }
 
  server {
    listen   443 ssl;
 
    proxy_set_header   Host                 $http_host;
    proxy_set_header   X-Forwarded-Proto    $scheme;
    proxy_set_header   X-Forwarded-For      $remote_addr;
    proxy_redirect     off;
 
    # keepalive + raven.js is a disaster
    keepalive_timeout 0;
 
    # use very aggressive timeouts
    proxy_read_timeout 5s;
    proxy_send_timeout 5s;
    send_timeout 5s;
    resolver_timeout 5s;
    client_body_timeout 5s;
 
    # buffer larger messages
    client_max_body_size 5m;
    client_body_buffer_size 100k;
 
    location /api/store/ {
              proxy_pass http://relay:3000;
     }
     location ~ ^/api/[1-9]\d*/ {
              proxy_pass http://relay:3000;
     }
     location / {
              proxy_pass http://web:9000;
     }

      add_header Strict-Transport-Security "max-age=31536000";
  }
}
  • 4)修改docker nginx端口

cd /usr/sentry/onpremise-21.4.0 && vim docker-compose.yml

找到以下配置并修改端口为443,https默认是443

  nginx:
    <<: *restart_policy
    ports:
      - "$SENTRY_BIND:443/tcp"
  • 5)重启docker

systemctl restart docker

  • 6)删除docker应用并重启

docker-compose stop(停止docker应用)

docker-compose rm (选择y,删除docker应用)

docker-compose up -d

  • 7)检查https是否生效

docker ps -a|grep nginx (出现443为配置生效)

324b1e003104   nginx:1.16                             "nginx -g 'daemon of…"   4 days ago   Up 32 seconds                           80/tcp, 0.0.0.0:9000->443/tcp, :::9000->443/tcp   sentry_onpremise_nginx_1
  • 8)浏览器访问https地址,查看是否生效

浏览器访问:https://IP:9000 (具体地址根据虚拟机IP而定)

7、创建项目

进入页面点击"项目" -> 创建"项目" -> 选择"REACT" -> 点击"创建项目" 创建成功后提示如下,根据提示在React项目中引入Sentry

To instrument your React application with Sentry, first install the @sentry/react and @sentry/tracing packages:

# Using yarn
yarn add @sentry/react @sentry/tracing

# Using npm
npm install --save @sentry/react @sentry/tracing

Next, import and initialize the Sentry module as early as possible, before initializing React:

import React from "react";
import ReactDOM from "react-dom";
import * as Sentry from "@sentry/react";
import { Integrations } from "@sentry/tracing";
import App from "./App";

Sentry.init({
  dsn: "https://b8f9d60d6ad044c181230962e6e39d83@x.x.x.x:9000/3",
  integrations: [new Integrations.BrowserTracing()],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});

ReactDOM.render(<App />, document.getElementById("root"));

// Can also use with React Concurrent Mode
// ReactDOM.createRoot(document.getElementById('root')).render(<App />);

测试项目报错是否成功上传,如下:

image.png

8、邮件配置

  • 1)打开sentry配置文件路径 cd /usr/xxx/onpremise-21.4.0/sentry  (以实际部署路径为准)
  • 2)修改config.yml配置文件 根据实际告警邮箱信息,修改config.yml文件中的“Mail Server”部分

image.png

  • 3)重启sentry

docker-compose stop docker-compose up -d

  • 4)页面新增告警规则 进入项目:

image.png

新建告警规则:

image.png

创建告警规则:

image.png

9、配置SourceMap

  • 1)在项目文件的根目录新建一个.sentryclirc文件

image.png

  • 2)sentryclirc文件中的配置内容
[auth]  
token=f0a0ee24d2cd4b2eade96280e1698762c5df9bac87ea423a6dbe3090f3a3a72

[defaults]
url = https://*****/
project=projectName
org=sentry

[http]
verify_ssl=false

如果用了https需要配置verify_ssl=false

  • 3)配置参数解析 token:token为API令牌,不是安全令牌。(user->API keys->Auth Tokens)

url:这里为自建的sentry,所以url设置为自定义的域名(user->Admin->Settings->Root URL)

project:即项目名称

org:组织名称(Settings->General Settings -> Name)

更多其他配置和参数,请参考sentry官方文档中的内容:docs.sentry.io/cli/configu…

  • 4)webpack插件 上传sourcemap的方式有很多种,如手动上传和sentry-cli上传,本文使用的是sentry/webpack-plugin(webpack插件),它的特点是:

自动化,比如其他几种方式要更为方便 ;

相对安全,自己构建sourceMap文件,上传后删除,不会影响到线上,不会暴露token和密钥等信息 ;

安装和操作简单,两步设置完成 。

首先,安装插件:

npm install @sentry/cli

修改webpack配置:

image.png

增加sentry-cli命令:

image.png

在构建完成后调用该命令,release保持和sentry.init中一致

配置成功后打包构建测试运行页面,可以看到版本中有对应版本信息:

image.png

点击版本进去可以看到具体的问题对应的源码信息

image.png

10、页面展示

image.png

image.png

最后

附上sentry错误监控的部分原理解析:

Sentry前端错误监控原理