Sentry 是什么& docker部署

42 篇文章 0 订阅
1 篇文章 0 订阅

1. Sentry 是什么?

Sentry是一个实时事件的日志聚合平台。它专门监测错误并提取所有有用信息用于分析,不再麻烦地依赖用户反馈来定位问题。

What’s Sentry?
Sentry fundamentally is a service that helps you monitor and fix crashes in realtime. The server is in Python, but it contains a full API for sending events from any language, in any application.
官网地址,自行脑补
官网github地址
在这里插入图片描述

2. Sentry 利用镜像部署服务

2.1 下载镜像:

docker pull redis
docker pull postgres 
docker pull sentry

2.2 启动redis和postgres

docker run -d --name sentry-redis --restart=always redis
docker run -d --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry --restart=always postgres 

2.3 生成秘钥,生成后记得把秘钥保存

sai:Downloads ws$ docker run --rm sentry config generate-secret-key
1pmvmvs=ly89)k!8tw5x1nanj5@)0fk9u#zw^d&ai#n8z=t90w

2.4 初始化数据结构

这一步时间比较长一些,有耐心,中间会提示输入超级用户:

#初始化数据结构[在升级过程中,系统将提示您创建将充当超级用户的初始用户]
docker run -it --rm -e SENTRY_SECRET_KEY='1pmvmvs=ly89)k!8tw5x1nanj5@)0fk9u#zw^d&ai#n8z=t90w' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgrade

2.5 启动sentry的三个容器

my-sentry:sentry的web服务
sentry-cron:sentry的定时任务,活性检测等
sentry-worker:业务处理,数据持久化,报警等

docker run -d -p 9000:9000 --name my-sentry -e SENTRY_SECRET_KEY='1pmvmvs=ly89)k!8tw5x1nanj5@)0fk9u#zw^d&ai#n8z=t90w' --link sentry-redis:redis --link sentry-postgres:postgres sentry 

docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='1pmvmvs=ly89)k!8tw5x1nanj5@)0fk9u#zw^d&ai#n8z=t90w' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron 

docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='1pmvmvs=ly89)k!8tw5x1nanj5@)0fk9u#zw^d&ai#n8z=t90w' --link sentry-postgres:postgres --link sentry-redis:redis sentry run worker 

2.6 docker ps -a查看容器启动情况

注意:这里的容器,可以通过:docker restart 容器id 重启
在这里插入图片描述

2.7 访问 http://localhost:9000/auth/login/sentry/

在这里插入图片描述

2.8 使用,先设置成中文

在这里插入图片描述

2.9 创建项目,设置客户端密钥

在这里插入图片描述

3.0 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;

/**
 * @author honglei
 * @since 2019-09-17
 */
public class MySentry {

    private static SentryClient sentryClient;

    public static void main(String... args) {
        /*
         It is recommended that you use the DSN detection system, which
         will check the environment variable "SENTRY_DSN", the Java
         System Property "sentry.dsn", or the "sentry.properties" file
         in your classpath. This makes it easier to provide and adjust
         your DSN without needing to change your code. See the configuration
         page for more information.
         */
        //Sentry.init();

        // You can also manually provide the DSN to the ``init`` method.
        String dsn = "http://663bc279986a4f81a46e338fdef693df:313a924a38854dff9518080b68852de2@192.168.1.108:9000/2";
        sentryClient = Sentry.init(dsn);
        sentryClient.setServerName("demo");
        /*
         It is possible to go around the static ``Sentry`` API, which means
         you are responsible for making the SentryClient instance available
         to your code.
         */
        //sentry = SentryClientFactory.sentryClient();

        MySentry mySentry = new MySentry();
        mySentry.logWithStaticAPI();
        mySentry.logWithInstanceAPI();
        mySentry.logWithMyProject();
    }

    /**
     * Examples using the (recommended) static API.
     */
    void logWithStaticAPI() {
        // Note that all fields set on the context are optional. Context data is copied onto
        // all future events in the current context (until the context is cleared).

        // Record a breadcrumb in the current context. By default the last 100 breadcrumbs are kept.
        Sentry.getContext().recordBreadcrumb(
                new BreadcrumbBuilder().setMessage("User made an action").build()
        );

        // Set the user in the current context.
        Sentry.getContext().setUser(
                new UserBuilder().setEmail("409178623@qq.com").build()
        );

        // Add extra data to future events in this context.
        Sentry.getContext().addExtra("extra", "thing");

        // Add an additional tag to future events in this context.
        Sentry.getContext().addTag("StaticAPI", "StaticAPI" + (System.currentTimeMillis() / 1000) / 60);
        /*
         This sends a simple event to Sentry using the statically stored instance
         that was created in the ``main`` method.
         */
        Sentry.capture("This is a test 4 logWithStaticAPI" + (System.currentTimeMillis() / 1000) / 60);
        try {
            throw new UnsupportedOperationException("logWithStaticAPI: 这是一个logWithStaticAPI测试抛异常");
        } catch (Exception e) {
            // This sends an exception event to Sentry using the statically stored instance
            // that was created in the ``main`` method.
            //Sentry.capture(e);
        }
    }

    /**
     * Examples that use the SentryClient instance directly.
     */
    void logWithInstanceAPI() {
        // Retrieve the current context.
        Context context = sentryClient.getContext();

        // Record a breadcrumb in the current context. By default the last 100 breadcrumbs are kept.
        context.recordBreadcrumb(new BreadcrumbBuilder().setMessage("User made an action").build());

        // Set the user in the current context.
        context.setUser(new UserBuilder().setEmail("409178623@qq.com").build());

        // This sends a simple event to Sentry.
        sentryClient.sendMessage("This is a test 4 logWithInstanceAPI ");
        try {
            throw new IllegalArgumentException("logWithInstanceAPI: An example method that throws an exception.");
        } catch (Exception e) {
            // This sends an exception event to Sentry.
            //sentryClient.sendException(e);
        }
    }

    void logWithMyProject() {

        try {
            throw new Exception("这是一个exception测试抛异常");
        } catch (Exception e) {
            // Retrieve the current context.
            Context sentryContext = sentryClient.getContext();
            // Set the user in the current context.
            Sentry.getContext().setUser(new UserBuilder().setEmail("409178623@qq.com").build());
            // Add an additional tag to future events in this context.
            sentryContext.addTag("subject", "测试邮件服务" + System.currentTimeMillis() / 1000);
            // Add an additional tag to future events in this context.
            sentryContext.addTag("to", "hongleishen@hotmail.com");
            // This sends an exception event to Sentry.
            sentryClient.sendException(e);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[shenhonglei]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值