Sentry 是什麼& docker部署

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:[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);
        }
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章