Sentry 是什么& docker部署

Sentry 是什么& docker部署

  • 1. Sentry 是什么?
  • 2. Sentry 利用镜像部署服务
    • 2.1 下载镜像:
    • 2.2 启动redis和postgres
    • 2.3 生成秘钥,生成后记得把秘钥保存
    • 2.4 初始化数据结构
    • 2.5 启动sentry的三个容器
    • 2.6 docker ps -a查看容器启动情况
      • 2.7 访问[ http://localhost:9000/auth/login/sentry/](http://localhost:9000/auth/login/sentry/)
      • 2.8 使用,先设置成中文
      • 2.9 创建项目,设置客户端密钥
      • 3.0 java项目使用:

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地址
Sentry 是什么& docker部署_第1张图片

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/

Sentry 是什么& docker部署_第2张图片

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:[email protected]: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("[email protected]").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("[email protected]").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("[email protected]").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", "[email protected]");
            // This sends an exception event to Sentry.
            sentryClient.sendException(e);
        }
    }
}

你可能感兴趣的:(Sentry,工具,docker)