docker搭建sentry 日志监控服务(java rrweb)

338 阅读3分钟

1、服务器搭建

#拉取镜像
docker pull redis
docker pull postgres:12.3
docker pull sentry

#启动Redis
docker run -d --name sentry-redis redis

#启动postgres
docker run --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry -p 5432:5432 -v /home/postgresql/pgdata:/var/lib/postgresql/data -d postgres:12.3

#访问秘钥生成
docker run --rm sentry config generate-secret-key

#初始化数据库、最后会询问是否创建登录用户,选择Y,并输入邮箱账号及密码
docker run -it --rm -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgrade

#启动服务
docker run -d -p 9000:9000 --name my-sentry -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-redis:redis --link sentry-postgres:postgres sentry    
docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron    
docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='<secret-key>' --link sentry-postgres:postgres --link sentry-redis:redis sentry run worker

2、通过http://IP:9000 及 邮箱账号密码登录系统

3、创建项目、并查看DSN 地址如: http://192.168.10.113:9000/2 或者 http://5d8c49cebd2f4d16afb4d6249e8ea5d3:3306ec96b5b14a769f0efed2653912b6@192.168.10.113:9000/2

4、编写日志记录代码(java) 如:

 
import io.sentry.Sentry;  
import io.sentry.SentryClient;  
import io.sentry.SentryClientFactory;  
import io.sentry.context.Context;  
import io.sentry.event.BreadcrumbBuilder;  
import io.sentry.event.UserBuilder;  
  
  
public class DanbaosentryApplication {    
    private static SentryClient sentry;  
        public static void main(String... args) {  
        Sentry.init();          Sentry.init("http://5d8c49cebd2f4d16afb4d6249e8ea5d3:3306ec96b5b14a769f0efed2653912b6@192.168.10.113:9000/2");  
        sentry = SentryClientFactory.sentryClient();    
        DanbaosentryApplication myClass = new DanbaosentryApplication();  
        myClass.logWithStaticAPI();  
        myClass.logWithInstanceAPI();  
        }  


    void unsafeMethod() {  
        throw new UnsupportedOperationException("You shouldn't call this!");  
    }  
  

    void logWithStaticAPI() {  
        Sentry.getContext().recordBreadcrumb(  new BreadcrumbBuilder().setMessage("User made an ction").build() );  

        Sentry.getContext().setUser(  
        new UserBuilder().setEmail("hello@sentry.io").build()  
        );  

        Sentry.getContext().addExtra("extra", "thing");     
        Sentry.getContext().addTag("tagName", "tagValue");  
        Sentry.capture("This is a test1");  

        try {  
        unsafeMethod();  
        } catch (Exception e) {  
        Sentry.capture(e);  
        }  
    }  

 
    void logWithInstanceAPI() {  
        Context context = sentry.getContext();  
        context.recordBreadcrumb(new BreadcrumbBuilder().setMessage("User made an action").build());  
        context.setUser(new UserBuilder().setEmail("hello@sentry.io").build());  
        sentry.sendMessage("This is a test1");  
        try {  
        unsafeMethod();  
        } catch (Exception e) {  
        sentry.sendException(e);  
        }  
    }  
}

maven pom.xml

<dependency>  
    <groupId>io.sentry</groupId>  
    <artifactId>sentry</artifactId>  
    <version>1.7.23</version>  
</dependency>

5、通过 jentry查看日志快照,"This is a test1"

rrweb 日志采集(以下文简要介绍,未做验证)

rrweb结合sentry 的日志监控服务可实现,前端页面点击事件回放。改功能主要用于前端进行页面异常事件的记录。

1、rrweb 是什么

定义:rrweb 指的是record and replay the web,它是一个用于记录和回放用户在网络上的交互的工具;

2、基本概念

rrweb 主要由 rrweb 、 rrweb-player 和 rrweb-snapshot 三个库组成:

rrweb:提供了 record 和 replay 两个方法;record 方法用来记录页面上 DOM 的变化,replay 方法支持根据时间戳去还原 DOM 的变化。
rrweb-player:基于 svelte 模板实现,为 rrweb 提供了回放的 UI 工具,支持暂停、倍速播放、拖拽时间轴等功能。内部调用了 rrweb 的提供的 replay 等方法。
rrweb-snapshot:包括 snapshot 和 rebuilding 两大特性,snapshot 用来序列化 DOM 为增量快照(所以录屏体积比较小),rebuilding 负责将增量快照还原为 DOM。

3、安装

可以直接 script 标签引入,也可以使用 npm 安装;可查看中文文档;

4、录制和播放(vue)

1、安装依赖包

	npm install --save rrwebnpm install --save rrweb-player

2、录制(此处可将录制数据上传至sentry)

import rrweb from ‘rrweb’;let events = [];
let stopFn = rrweb.record({emit(event) {if (events.length > 100) {// 当事件数量大于 100 时停止录制stopFn();}// 将 event 存入 events 数组中events.push(event);},
});

rrweb 在录制时会不断将各类 event 传递给配置的 emit 方法,你可以使用任何方式存储这些 event 以便之后回放。调用 record 方法将返回一个函数,调用该函数可以终止录制;

3、播放(此处可通过sentry的json数据访问形式进行操作步骤播放)

import rrwebPlayer from 'rrweb-player';
import 'rrweb-player/dist/style.css';new rrwebPlayer({target: document.body, // 可以自定义 DOM 元素// 配置项props: {events,},
});

也可以使用 rrweb 提供的 replay 来实现回放,如果需要功能更强的回放播放器 UI,可以使用 rrweb-player;