openresty 集成 sentry 异常系统

sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试

备注: sentry 部分的配置来自官方文档

环境准备

  • docker-compose 文件
# NOTE: This docker-compose.yml is meant to be just an example of how
# you could accomplish this on your own. It is not intended to work in
# all use-cases and must be adapted to fit your needs. This is merely
# a guideline.

# See docs.getsentry.com/on-premise/server/ for full
# instructions

version: '3.4'

x-defaults: &defaults
  restart: unless-stopped
  build: .
  depends_on:
    - redis
    - postgres
    - memcached
    - smtp
  env_file: .env
  environment:
    SENTRY_MEMCACHED_HOST: memcached
    SENTRY_REDIS_HOST: redis
    SENTRY_POSTGRES_HOST: postgres
    SENTRY_EMAIL_HOST: smtp
  volumes:
    - sentry-data:/var/lib/sentry/files


services:
  smtp:
    restart: unless-stopped
    image: tianon/exim4

  memcached:
    restart: unless-stopped
    image: memcached:1.5-alpine

  redis:
    restart: unless-stopped
    image: redis:3.2-alpine

  postgres:
    restart: unless-stopped
    image: postgres:9.5
    ports:
    - "5432:5432"
    volumes:
      - sentry-postgres:/var/lib/postgresql/data

  web:
    <<: *defaults
    ports:
      - '9000:9000'
  openresty:
    build: 
      context: ./
      dockerfile: ./Dockerfile-nginx
    ports:
    - "8080:80"  
    volumes:
    - "./nginx_lua/:/opt/app/"
    - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  cron:
    <<: *defaults
    command: run cron

  worker:
    <<: *defaults
    command: run worker


volumes:
    sentry-data:
      external: true
    sentry-postgres:
      external: true
  • openresty 配置
    nginx.conf
worker_processes 1;
user root;  
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    lua_code_cache off;
    lua_need_request_body on;
    gzip on;
    resolver 127.0.0.11 ipv6=off;          
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    lua_package_path '/opt/app/?.lua;;';
    server {
        listen 80;
        server_name localhost;
        charset utf-8;
        default_type text/html;
        location / {
           default_type text/plain;
           content_by_lua_block {
               require("sentry/init")()
           }
        }
        location = /favicon.ico {
            root /opt/app/static;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

    }
}

dockerfile 配置
主要是添加了raven 包

FROM openresty/openresty:alpine-fat
LABEL author="[email protected]"
RUN /usr/local/openresty/luajit/bin/luarocks install resty-raven
EXPOSE 80

调用代码

主要来自github 文档,同时这个是一个简单的测试,注意lua 包的问题,当前测试支持的只是旧版本的dsn 方式
新版本的运行有问题
nginx_lua/sentry/init.lua

local raven = require "raven"

function init()
    -- http://pub:[email protected]:8080/sentry/proj-id
local rvn = raven:new("http://092b2e429b4c480e83c2e7f18890b8f2:e41f2a264e5e47329d0026e99ae825b1@web:9000/2", {
    tags = { foo = "bar" },
 })

 -- Send a message to sentry
 local id, err = rvn:captureMessage(
   "Sentry is a realtime event logging and aggregation platform.",
   { tags = { abc = "def" } } -- optional
 )
 if not id then
    print(err)
 end

 -- Send an exception to sentry
 local exception = {{
    ["type"]= "SyntaxError",
    ["value"]= "Wattttt!",
    ["module"]= "__builtins__"
 }}
 local id, err = rvn:captureException(
    exception,
    { tags = { abc = "def" } } -- optional
 )
 if not id then
    print(err)
 end

 -- Catch an exception and send it to sentry
 function bad_func(n)
    return not_defined_func(n)
 end

 -- variable 'ok' should be false, and an exception will be sent to sentry
 local ok = rvn:call(bad_func, 1)
end

return init

环境启动

主要是sentry的启动,因为sentry 启动稍有点费事,依赖数据库以及key 的生成,基本流程如下:

  • sentry 启动
    来自官方文档,就懒得翻译了
  1. docker volume create --name=sentry-data && docker volume create --name=sentry-postgres - Make our local database and sentry volumes
    Docker volumes have to be created manually, as they are declared as external to be more durable.
  2. cp -n .env.example .env - create env config file
  3. docker-compose build - Build and tag the Docker services
  4. docker-compose run --rm web config generate-secret-key - Generate a secret key.
    Add it to .env as SENTRY_SECRET_KEY.
  5. docker-compose run --rm web upgrade - Build the database.
    Use the interactive prompts to create a user account.
  6. docker-compose up -d - Lift all services (detached/background mode).
  7. Access your instance at localhost:9000!
  • openresty 启动
    在启动sentry 的时候openresty 对应的镜像以及以及依赖的raven 已经同时处理了,基本可以不用管了

测试效果

打开http://localhsot:8080 即可

  • sentry 日志效果

说明

当前lua raven 支持的dsn 版本有点老,新版本的有问题,但是总的来说还是很不错的,在实际openresty 项目的开发中,可以作为一个
可选的问题分析方案,同时sentry 的功能还是很强大的,我们可以继承git 以及问题管理系统

参考资料

https://github.com/rongfengliang/openresty-sentry-docker-compose
https://github.com/UseFedora/raven-lua
https://github.com/getsentry/onpremise

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/10321405.html