搭建私有的前端监控服务: sentry

16,656 阅读2分钟

去年底写过一篇简单而完整地体验一遍sentry的sourcemap服务, 是完全基于使用层面的. 由于有需求需要自行搭建sentry, 整理一下搭建流程

版本

8.22.0 参考github release

搭建方式

官方推荐的方法是使用Docker, 我们这里以使用Docker为例

前期准备

daocloud上附有教程, 请自行查阅

$ docker --version
Docker version 18.05.0-ce, build f150324
$ docker-compose --version
docker-compose version 1.21.2, build a133471

搭建步骤

拉取onpremise

onpremise是官方提供的包含了sentry搭建所需要的全部依赖的引导程序

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

进到onpremise并新建本地数据库和sentry配置目录

$ cd onpremise
$ mkdir -p data/{sentry,postgres}

这样需要说明一下, 我一开始没执行这一步操作, 虽然在构建过程中程序会给我们新建这些本该有的目录. 然而, 后续出现的一连串让人崩溃的问题, 譬如There was an error loading data, 也会与此相关.

生成secret key

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

这个时候会在终端输出

Starting onpremise_redis_1     ... done
Starting onpremise_postgres_1  ... done
Starting onpremise_memcached_1 ... done
xxx+5%xxxxxxx!!xxxxxxxx&6(xxxxxxx%xoml)xxxxxxxxxx

复制秘钥(即最后一行)到docker-compose.yml中的SENTRY_SECRET_KEY对应的value

更新配置及创建super user

$ docker-compose run --rm web upgrade

正常的话, 终端会出现

...
Would you like to create a user account now...
...

正常键入即可

在部署到linux机器上时, 出现过一种情况就是: 根本没出现这一步, 果断从头再来. 还好, 问题就此打住.

邮件配置

不像国内, 很多应用都支持邮件手机二选一的注册方式. 而sentry, 少了邮件功能, 就好像被阉割了一样, 也没什么好用的了.

在onpremise根目录里, 有一个config.yml配置文件, 里面定义了一些常规配置, 包括邮件配置方式.

# Use dummy if you want to disable email entirely
mail.backend: 'smtp'
mail.host: 'smtp.qq.com'
mail.port: 587
mail.username: '123@qq.com'
# 邮箱授权码, 非邮箱密码
mail.password: '123'
mail.use-tls: true
# The email address to send on behalf of
mail.from: '123@qq.com'
# 请保持与域名严格一致
mail.list-namespace: 'sentry.yourdomain.com'

除了上述的注释外, 还有:

  • 我一开始尝试的是163的邮箱, 一直发不出邮件, 显示timeout, 所以转了qq, 还不知道为什么.
  • 用qq邮箱时端口号尽量用587, 用465会出现一些奇怪的问题. sentry只支持tls而非ssh,所以端口改587试试

启动服务

$ docker-compose up -d

如无意外, 一切正常, 端口默认是9000, 本地的话可以直接打开localhost:9000访问

用nginx配置http(s)

官网上也有相关说明

配置https不要忽略了文档末段的修改sentry.conf.py

import os
import os.path

# 添加变量
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

贴一下我的 nginx for https 配置

sentry.conf

server {
    listen 80;
    server_name sentry.yourdomain.com www.sentry.yourdomain.com;
    location / {
        if ($request_method = GET) {
            rewrite  ^ https://$host$request_uri? permanent;
        }
        return 405;
    }
    access_log /home/wwwlogs/sentry_yourdomain.log main;
}
server {
    listen 443;
    ssl on;
    ssl_certificate      /etc/nginx/ssl/yourdomain.crt;
    ssl_certificate_key  /etc/nginx/ssl/yourdomain.key;

    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;

    location / {
	proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://localhost:9000;
        add_header Strict-Transport-Security "max-age=31536000";
    }


    access_log /home/wwwlogs/sentry_yourdomain.log main;
}

访问并配置

使用super user账户登录

首次需要配置Root URL等信息, 其中的Root URL填写https://sentry.yourdomain.com即可, 不要填类似https://sentry.yourdomain.com/这种, 貌似对从邮件中点击跳转等操作不友好.

验证邮箱

团队内部使用可以使用邮件邀请机制!!!!

结合文章开始介绍的简单而完整地体验一遍sentry的sourcemap服务, 可以体验一下自己搭建并使用sentry的快感!!!!