Java整合Sentry之上下文和麵包屑設定
阿新 • • 發佈:2018-12-16
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(); } }