Java集成Sentry之上下文和面包屑设置

Java SDK实现了“上下文”的概念,以支持将附加信息附加到事件,例如面包屑。上下文可以指向Web框架的单个请求,Android应用程序的整个生命周期,或者更适合您的应用程序需求的其他内容。

没有单一的上下文定义适用于每个应用程序,因此必须根据应用程序的功能和结构来选择特定的实现。默认情况下,Sentry使用ThreadLocalContextManager,每个线程维护一个Context实例。这对于每个用户请求使用一个线程的框架(例如基于同步servlet API的框架)非常有用。 Sentry还安装了一个ServletRequestListener,它将在每个servlet请求完成后清除线程的上下文。

Sentry默认使用Android上的SingletonContextManager,它在应用程序的生命周期内为所有线程维护单个上下文实例。

要覆盖ContextManager,您需要覆盖DefaultSentryClientFactory中的getContextManager方法。将来可能会提供更简单的API。

一、使用用法

面包屑可用于描述导致发送事件的应用程序中发生的操作。例如,是否发出了外部API请求,或者用户是否单击了Android应用程序中的某些内容。默认情况下,每个上下文的最后100个面包屑将被存储并与未来事件一起发送。

可以根据上下文设置用户,以便了解每个事件的影响。

初始化SentryClient实例后,您可以开始在当前上下文中设置状态。

import io.sentry.Sentry;
import io.sentry.context.Context;
import io.sentry.event.BreadcrumbBuilder;
import io.sentry.event.UserBuilder;

public class MyClass {

    /**
      * Examples using the (recommended) static API.
      */
    public void staticAPIExample() {
        // Manually initialize the static client, you may also pass in a DSN and/or
        // SentryClientFactory to use. Note that the client will attempt to automatically
        // initialize on the first use of the static API, so this isn't strictly necessary.
        Sentry.init();

        // 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).

        // Set the current user in the context.
        Sentry.getContext().setUser(
            new UserBuilder().setUsername("user1").build()
        );

        // Record a breadcrumb in the context.
        Sentry.getContext().recordBreadcrumb(
            new BreadcrumbBuilder().setMessage("User did something specific again!").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("tagName", "tagValue");

        // Send an event with the context data attached.
        Sentry.capture("New event message");

        // Clear the context, useful if you need to add hooks in a framework
        // to empty context between requests.
        Sentry.clearContext();
    }

    /**
      * Examples that use the SentryClient instance directly.
      */
    public void instanceAPIExample() {
        // Create a SentryClient instance that you manage manually.
        SentryClient sentryClient = SentryClientFactory.sentryClient();

        // Get the current context instance.
        Context context = sentryClient.getContext();

        // 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).

        // Set the current user in the context.
        context.setUser(
            new UserBuilder().setUsername("user1").build()
        );

        // Record a breadcrumb in the context.
        context.recordBreadcrumb(
            new BreadcrumbBuilder().setMessage("User did something specific!").build()
        );

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

        // Add an additional tag to future events in this context.
        context.addTag("tagName", "tagValue");

        // Send an event with the context data attached.
        sentryClient.sendMessage("New event message");

        // Clear the context, useful if you need to add hooks in a framework
        // to empty context between requests.
        context.clear();
    }
}

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/83213892