From 305baf5bd419920f4b80ff6f0075c13dd42c2b94 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 27 Mar 2024 16:30:22 +0100 Subject: [PATCH 01/57] replace hub with scopes --- .../api/sentry-android-core.api | 28 +- .../core/ActivityBreadcrumbsIntegration.java | 17 +- .../core/ActivityLifecycleIntegration.java | 32 +- .../core/AndroidTransactionProfiler.java | 12 +- .../sentry/android/core/AnrIntegration.java | 19 +- .../sentry/android/core/AnrV2Integration.java | 14 +- .../AppComponentsBreadcrumbsIntegration.java | 16 +- .../android/core/AppLifecycleIntegration.java | 14 +- .../core/CurrentActivityIntegration.java | 4 +- .../core/EnvelopeFileObserverIntegration.java | 14 +- .../android/core/InternalSentrySdk.java | 24 +- .../sentry/android/core/LifecycleWatcher.java | 22 +- .../sentry/android/core/NdkIntegration.java | 9 +- .../core/NetworkBreadcrumbsIntegration.java | 21 +- .../PhoneStateBreadcrumbsIntegration.java | 20 +- .../core/SendCachedEnvelopeIntegration.java | 20 +- .../io/sentry/android/core/SentryAndroid.java | 11 +- .../SystemEventsBreadcrumbsIntegration.java | 20 +- .../TempSensorBreadcrumbsIntegration.java | 12 +- .../core/UserInteractionIntegration.java | 12 +- .../gestures/SentryGestureListener.java | 18 +- .../core/AndroidTransactionProfilerTest.kt | 12 +- .../sentry/android/core/AnrIntegrationTest.kt | 28 +- .../android/core/AnrV2IntegrationTest.kt | 90 +-- ...AppComponentsBreadcrumbsIntegrationTest.kt | 48 +- .../core/AppLifecycleIntegrationTest.kt | 16 +- .../core/CurrentActivityIntegrationTest.kt | 6 +- .../core/DefaultAndroidEventProcessorTest.kt | 8 +- .../EnvelopeFileObserverIntegrationTest.kt | 20 +- .../android/core/LifecycleWatcherTest.kt | 48 +- .../sentry/android/core/NdkIntegrationTest.kt | 22 +- .../core/NetworkBreadcrumbsIntegrationTest.kt | 108 ++-- .../PerformanceAndroidEventProcessorTest.kt | 20 +- .../PhoneStateBreadcrumbsIntegrationTest.kt | 38 +- .../core/SendCachedEnvelopeIntegrationTest.kt | 30 +- .../SystemEventsBreadcrumbsIntegrationTest.kt | 24 +- .../TempSensorBreadcrumbsIntegrationTest.kt | 32 +- .../SentryGestureListenerClickTest.kt | 22 +- .../SentryGestureListenerScrollTest.kt | 26 +- .../SentryGestureListenerTracingTest.kt | 58 +- .../api/sentry-android-fragment.api | 8 +- .../fragment/FragmentLifecycleIntegration.kt | 10 +- .../SentryFragmentLifecycleCallbacks.kt | 18 +- .../FragmentLifecycleIntegrationTest.kt | 16 +- .../SentryFragmentLifecycleCallbacksTest.kt | 14 +- .../android/mockservers/RelayAsserter.kt | 2 +- .../api/sentry-android-navigation.api | 10 +- .../navigation/SentryNavigationListener.kt | 24 +- .../SentryNavigationListenerTest.kt | 54 +- .../api/sentry-android-okhttp.api | 18 +- .../okhttp/SentryOkHttpEventListener.kt | 22 +- .../android/okhttp/SentryOkHttpInterceptor.kt | 16 +- .../android/sqlite/SQLiteSpanManager.kt | 12 +- .../android/sqlite/SQLiteSpanManagerTest.kt | 12 +- .../sqlite/SentrySupportSQLiteDatabaseTest.kt | 12 +- .../SentrySupportSQLiteStatementTest.kt | 12 +- .../api/sentry-android-timber.api | 4 +- .../android/timber/SentryTimberIntegration.kt | 6 +- .../sentry/android/timber/SentryTimberTree.kt | 8 +- .../timber/SentryTimberIntegrationTest.kt | 18 +- .../android/timber/SentryTimberTreeTest.kt | 52 +- sentry-apollo-3/api/sentry-apollo-3.api | 20 +- .../apollo3/SentryApollo3HttpInterceptor.kt | 36 +- .../apollo3/SentryApolloBuilderExtensions.kt | 10 +- .../SentryApollo3InterceptorClientErrors.kt | 40 +- .../apollo3/SentryApollo3InterceptorTest.kt | 40 +- ...ntryApollo3InterceptorWithVariablesTest.kt | 22 +- sentry-apollo/api/sentry-apollo.api | 6 +- .../sentry/apollo/SentryApolloInterceptor.kt | 20 +- .../apollo/SentryApolloInterceptorTest.kt | 38 +- sentry-graphql/api/sentry-graphql.api | 20 +- .../io/sentry/graphql/ExceptionReporter.java | 43 +- .../graphql/NoOpSubscriptionHandler.java | 4 +- .../SentryDataFetcherExceptionHandler.java | 14 +- ...tryGenericDataFetcherExceptionHandler.java | 4 +- .../sentry/graphql/SentryInstrumentation.java | 38 +- .../graphql/SentrySubscriptionHandler.java | 4 +- .../sentry/graphql/ExceptionReporterTest.kt | 32 +- .../SentryDataFetcherExceptionHandlerTest.kt | 8 +- ...yGenericDataFetcherExceptionHandlerTest.kt | 6 +- .../SentryInstrumentationAnotherTest.kt | 40 +- .../graphql/SentryInstrumentationTest.kt | 36 +- sentry-jdbc/api/sentry-jdbc.api | 2 +- .../sentry/jdbc/SentryJdbcEventListener.java | 14 +- .../jdbc/SentryJdbcEventListenerTest.kt | 16 +- .../java/io/sentry/jul/SentryHandler.java | 6 +- .../api/sentry-kotlin-extensions.api | 8 +- .../java/io/sentry/kotlin/SentryContext.kt | 28 +- .../io/sentry/kotlin/SentryContextTest.kt | 6 +- sentry-log4j2/api/sentry-log4j2.api | 2 +- .../java/io/sentry/log4j2/SentryAppender.java | 20 +- .../io/sentry/log4j2/SentryAppenderTest.kt | 6 +- .../io/sentry/logback/SentryAppender.java | 6 +- sentry-okhttp/api/sentry-okhttp.api | 18 +- .../io/sentry/okhttp/SentryOkHttpEvent.kt | 16 +- .../okhttp/SentryOkHttpEventListener.kt | 24 +- .../sentry/okhttp/SentryOkHttpInterceptor.kt | 22 +- .../io/sentry/okhttp/SentryOkHttpUtils.kt | 18 +- .../okhttp/SentryOkHttpEventListenerTest.kt | 28 +- .../io/sentry/okhttp/SentryOkHttpEventTest.kt | 52 +- .../okhttp/SentryOkHttpInterceptorTest.kt | 50 +- .../io/sentry/okhttp/SentryOkHttpUtilsTest.kt | 22 +- sentry-openfeign/api/sentry-openfeign.api | 4 +- .../io/sentry/openfeign/SentryCapability.java | 17 +- .../sentry/openfeign/SentryFeignClient.java | 14 +- .../sentry/openfeign/SentryFeignClientTest.kt | 22 +- .../OpenTelemetryLinkErrorEventProcessor.java | 26 +- .../opentelemetry/SentryPropagator.java | 24 +- .../opentelemetry/SentrySpanProcessor.java | 50 +- .../test/kotlin/SentrySpanProcessorTest.kt | 48 +- sentry-quartz/api/sentry-quartz.api | 2 +- .../io/sentry/quartz/SentryJobListener.java | 28 +- .../samples/android/ProfilingActivity.kt | 2 +- .../samples/spring/jakarta/AppConfig.java | 6 +- .../samples/spring/jakarta/WebConfig.java | 8 +- .../io/sentry/samples/spring/AppConfig.java | 6 +- .../io/sentry/samples/spring/WebConfig.java | 8 +- .../api/sentry-servlet-jakarta.api | 2 +- .../jakarta/SentryServletRequestListener.java | 20 +- .../SentryServletRequestListenerTest.kt | 12 +- sentry-servlet/api/sentry-servlet.api | 2 +- .../servlet/SentryServletRequestListener.java | 20 +- .../SentryServletRequestListenerTest.kt | 12 +- .../api/sentry-spring-boot-jakarta.api | 2 +- .../boot/jakarta/SentryAutoConfiguration.java | 43 +- .../SentrySpanRestClientCustomizer.java | 6 +- .../SentrySpanRestTemplateCustomizer.java | 6 +- .../SentrySpanWebClientCustomizer.java | 6 +- .../SentryWebfluxAutoConfiguration.java | 21 +- .../jakarta/SentryAutoConfigurationTest.kt | 12 +- .../SentrySpanRestClientCustomizerTest.kt | 22 +- .../SentrySpanRestTemplateCustomizerTest.kt | 22 +- .../SentrySpanWebClientCustomizerTest.kt | 22 +- .../jakarta/it/SentrySpringIntegrationTest.kt | 6 +- sentry-spring-boot/api/sentry-spring-boot.api | 4 +- .../spring/boot/SentryAutoConfiguration.java | 39 +- .../SentrySpanRestTemplateCustomizer.java | 6 +- .../boot/SentrySpanWebClientCustomizer.java | 6 +- .../boot/SentryWebfluxAutoConfiguration.java | 15 +- .../boot/SentryAutoConfigurationTest.kt | 12 +- .../SentrySpanRestTemplateCustomizerTest.kt | 22 +- .../boot/SentrySpanWebClientCustomizerTest.kt | 22 +- .../boot/it/SentrySpringIntegrationTest.kt | 6 +- .../api/sentry-spring-jakarta.api | 61 +- .../sentry/spring/jakarta/EnableSentry.java | 2 +- .../jakarta/SentryExceptionResolver.java | 10 +- .../spring/jakarta/SentryHubRegistrar.java | 4 +- .../jakarta/SentryInitBeanPostProcessor.java | 16 +- .../spring/jakarta/SentryRequestResolver.java | 16 +- .../spring/jakarta/SentrySpringFilter.java | 38 +- .../spring/jakarta/SentryTaskDecorator.java | 18 +- .../spring/jakarta/SentryUserFilter.java | 12 +- .../jakarta/checkin/SentryCheckInAdvice.java | 28 +- ...SentryCaptureExceptionParameterAdvice.java | 14 +- .../graphql/SentryBatchLoaderRegistry.java | 16 +- .../graphql/SentryDgsSubscriptionHandler.java | 6 +- .../SentrySpringSubscriptionHandler.java | 6 +- .../jakarta/tracing/SentrySpanAdvice.java | 14 +- ...entrySpanClientHttpRequestInterceptor.java | 18 +- .../SentrySpanClientWebRequestFilter.java | 14 +- .../jakarta/tracing/SentryTracingFilter.java | 31 +- .../tracing/SentryTransactionAdvice.java | 20 +- .../webflux/AbstractSentryWebFilter.java | 35 +- .../spring/jakarta/webflux/ReactorUtils.java | 35 +- .../SentryReactorThreadLocalAccessor.java | 18 +- .../webflux/SentryRequestResolver.java | 13 +- .../jakarta/webflux/SentryScheduleHook.java | 12 +- .../webflux/SentryWebExceptionHandler.java | 18 +- .../jakarta/webflux/SentryWebFilter.java | 16 +- ...entryWebFilterWithThreadLocalAccessor.java | 13 +- .../sentry/spring/jakarta/EnableSentryTest.kt | 4 +- .../spring/jakarta/SentryCheckInAdviceTest.kt | 94 +-- .../jakarta/SentryExceptionResolverTest.kt | 28 +- .../SentryInitBeanPostProcessorTest.kt | 10 +- ...yRequestHttpServletRequestProcessorTest.kt | 6 +- .../spring/jakarta/SentrySpringFilterTest.kt | 20 +- .../spring/jakarta/SentryTaskDecoratorTest.kt | 16 +- .../spring/jakarta/SentryUserFilterTest.kt | 20 +- ...ntryCaptureExceptionParameterAdviceTest.kt | 16 +- .../SentrySpringSubscriptionHandlerTest.kt | 14 +- .../mvc/SentrySpringIntegrationTest.kt | 24 +- .../jakarta/tracing/SentrySpanAdviceTest.kt | 40 +- .../tracing/SentryTracingFilterTest.kt | 52 +- .../tracing/SentryTransactionAdviceTest.kt | 38 +- .../jakarta/webflux/ReactorUtilsTest.kt | 45 +- .../jakarta/webflux/SentryScheduleHookTest.kt | 16 +- .../webflux/SentryWebFluxTracingFilterTest.kt | 93 +-- .../webflux/SentryWebfluxIntegrationTest.kt | 10 +- sentry-spring/api/sentry-spring.api | 37 +- .../spring/SentryExceptionResolver.java | 10 +- .../io/sentry/spring/SentryHubRegistrar.java | 4 +- .../spring/SentryInitBeanPostProcessor.java | 16 +- .../sentry/spring/SentryRequestResolver.java | 16 +- .../io/sentry/spring/SentrySpringFilter.java | 38 +- .../io/sentry/spring/SentryTaskDecorator.java | 14 +- .../io/sentry/spring/SentryUserFilter.java | 12 +- .../spring/checkin/SentryCheckInAdvice.java | 28 +- ...SentryCaptureExceptionParameterAdvice.java | 14 +- .../graphql/SentryBatchLoaderRegistry.java | 16 +- .../graphql/SentryDgsSubscriptionHandler.java | 6 +- .../SentrySpringSubscriptionHandler.java | 6 +- .../spring/tracing/SentrySpanAdvice.java | 14 +- ...entrySpanClientHttpRequestInterceptor.java | 14 +- .../SentrySpanClientWebRequestFilter.java | 14 +- .../spring/tracing/SentryTracingFilter.java | 31 +- .../tracing/SentryTransactionAdvice.java | 20 +- .../spring/webflux/SentryRequestResolver.java | 13 +- .../spring/webflux/SentryScheduleHook.java | 12 +- .../webflux/SentryWebExceptionHandler.java | 10 +- .../spring/webflux/SentryWebFilter.java | 33 +- .../io/sentry/spring/EnableSentryTest.kt | 6 +- .../sentry/spring/SentryCheckInAdviceTest.kt | 94 +-- .../spring/SentryExceptionResolverTest.kt | 28 +- .../spring/SentryInitBeanPostProcessorTest.kt | 10 +- ...yRequestHttpServletRequestProcessorTest.kt | 6 +- .../sentry/spring/SentrySpringFilterTest.kt | 20 +- .../sentry/spring/SentryTaskDecoratorTest.kt | 16 +- .../io/sentry/spring/SentryUserFilterTest.kt | 20 +- ...ntryCaptureExceptionParameterAdviceTest.kt | 16 +- .../SentrySpringSubscriptionHandlerTest.kt | 14 +- .../spring/mvc/SentrySpringIntegrationTest.kt | 24 +- .../spring/tracing/SentrySpanAdviceTest.kt | 40 +- .../spring/tracing/SentryTracingFilterTest.kt | 52 +- .../tracing/SentryTransactionAdviceTest.kt | 38 +- .../spring/webflux/SentryScheduleHookTest.kt | 14 +- .../webflux/SentryWebFluxTracingFilterTest.kt | 93 +-- .../webflux/SentryWebfluxIntegrationTest.kt | 10 +- .../api/sentry-test-support.api | 1 + .../main/kotlin/io/sentry/test/Reflection.kt | 11 +- sentry/api/sentry.api | 333 +++++++--- sentry/src/main/java/io/sentry/Baggage.java | 4 +- .../java/io/sentry/DirectoryProcessor.java | 8 +- .../main/java/io/sentry/EnvelopeSender.java | 10 +- sentry/src/main/java/io/sentry/Hub.java | 1 + .../src/main/java/io/sentry/HubAdapter.java | 22 +- .../main/java/io/sentry/HubScopesWrapper.java | 279 ++++++++ sentry/src/main/java/io/sentry/IHub.java | 595 +----------------- sentry/src/main/java/io/sentry/IScopes.java | 594 +++++++++++++++++ .../src/main/java/io/sentry/Integration.java | 4 +- .../main/java/io/sentry/MonitorConfig.java | 2 +- sentry/src/main/java/io/sentry/NoOpHub.java | 7 + .../src/main/java/io/sentry/NoOpScopes.java | 243 +++++++ .../src/main/java/io/sentry/OutboxSender.java | 14 +- .../io/sentry/PreviousSessionFinalizer.java | 8 +- .../main/java/io/sentry/ScopesAdapter.java | 286 +++++++++ ...achedEnvelopeFireAndForgetIntegration.java | 20 +- .../SendFireAndForgetEnvelopeSender.java | 6 +- .../sentry/SendFireAndForgetOutboxSender.java | 6 +- sentry/src/main/java/io/sentry/Sentry.java | 176 +++--- .../src/main/java/io/sentry/SentryTracer.java | 85 +-- .../main/java/io/sentry/SentryWrapper.java | 21 +- .../io/sentry/ShutdownHookIntegration.java | 6 +- sentry/src/main/java/io/sentry/Span.java | 30 +- .../java/io/sentry/SpotlightIntegration.java | 2 +- .../UncaughtExceptionHandlerIntegration.java | 12 +- .../backpressure/BackpressureMonitor.java | 11 +- .../file/FileIOSpanManager.java | 6 +- .../file/SentryFileInputStream.java | 42 +- .../file/SentryFileOutputStream.java | 44 +- .../file/SentryFileReader.java | 7 +- .../file/SentryFileWriter.java | 6 +- .../java/io/sentry/util/CheckInUtils.java | 15 +- .../java/io/sentry/util/TracingUtils.java | 18 +- ...aultTransactionPerformanceCollectorTest.kt | 8 +- .../java/io/sentry/DirectoryProcessorTest.kt | 16 +- .../test/java/io/sentry/EnvelopeSenderTest.kt | 20 +- .../src/test/java/io/sentry/HubAdapterTest.kt | 92 +-- sentry/src/test/java/io/sentry/HubTest.kt | 430 ++++++------- .../test/java/io/sentry/JsonSerializerTest.kt | 8 +- .../java/io/sentry/MainEventProcessorTest.kt | 6 +- .../test/java/io/sentry/OutboxSenderTest.kt | 28 +- .../io/sentry/PreviousSessionFinalizerTest.kt | 22 +- sentry/src/test/java/io/sentry/ScopeTest.kt | 24 +- .../test/java/io/sentry/ScopesAdapterTest.kt | 265 ++++++++ ...hedEnvelopeFireAndForgetIntegrationTest.kt | 34 +- .../test/java/io/sentry/SentryClientTest.kt | 28 +- sentry/src/test/java/io/sentry/SentryTest.kt | 112 ++-- .../test/java/io/sentry/SentryWrapperTest.kt | 32 +- .../io/sentry/ShutdownHookIntegrationTest.kt | 22 +- sentry/src/test/java/io/sentry/SpanTest.kt | 24 +- .../sentry/TraceContextSerializationTest.kt | 6 +- ...UncaughtExceptionHandlerIntegrationTest.kt | 62 +- .../backpressure/BackpressureMonitorTest.kt | 12 +- .../sentry/clientreport/ClientReportTest.kt | 8 +- .../file/FileIOSpanManagerTest.kt | 14 +- .../file/SentryFileInputStreamTest.kt | 22 +- .../file/SentryFileOutputStreamTest.kt | 12 +- .../file/SentryFileReaderTest.kt | 12 +- .../file/SentryFileWriterTest.kt | 12 +- .../internal/SpotlightIntegrationTest.kt | 10 +- .../java/io/sentry/protocol/SentrySpanTest.kt | 4 +- .../io/sentry/transport/RateLimiterTest.kt | 32 +- .../java/io/sentry/util/CheckInUtilsTest.kt | 91 +-- .../java/io/sentry/util/TracingUtilsTest.kt | 30 +- 294 files changed, 5314 insertions(+), 3889 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/HubScopesWrapper.java create mode 100644 sentry/src/main/java/io/sentry/IScopes.java create mode 100644 sentry/src/main/java/io/sentry/NoOpScopes.java create mode 100644 sentry/src/main/java/io/sentry/ScopesAdapter.java create mode 100644 sentry/src/test/java/io/sentry/ScopesAdapterTest.kt diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 8eb017346d..2afe788ef8 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -8,7 +8,7 @@ public final class io/sentry/android/core/ActivityBreadcrumbsIntegration : andro public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ActivityFramesTracker { @@ -33,7 +33,7 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AndroidCpuCollector : io/sentry/IPerformanceSnapshotCollector { @@ -87,7 +87,7 @@ public class io/sentry/android/core/AndroidProfiler$ProfileStartData { public final class io/sentry/android/core/AnrIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AnrIntegrationFactory { @@ -104,7 +104,7 @@ public final class io/sentry/android/core/AnrV2EventProcessor : io/sentry/Backfi public class io/sentry/android/core/AnrV2Integration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AnrV2Integration$AnrV2Hint : io/sentry/hints/BlockingFlushHint, io/sentry/hints/AbnormalExit, io/sentry/hints/Backfillable { @@ -123,13 +123,13 @@ public final class io/sentry/android/core/AppComponentsBreadcrumbsIntegration : public fun onConfigurationChanged (Landroid/content/res/Configuration;)V public fun onLowMemory ()V public fun onTrimMemory (I)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AppLifecycleIntegration : io/sentry/Integration, java/io/Closeable { public fun ()V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AppState { @@ -177,7 +177,7 @@ public final class io/sentry/android/core/CurrentActivityIntegration : android/a public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/DeviceInfoUtil { @@ -193,7 +193,7 @@ public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : i public fun ()V public fun close ()V public static fun getOutboxFileObserver ()Lio/sentry/android/core/EnvelopeFileObserverIntegration; - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public abstract interface class io/sentry/android/core/IDebugImagesLoader { @@ -219,19 +219,19 @@ public final class io/sentry/android/core/NdkIntegration : io/sentry/Integration public static final field SENTRY_NDK_CLASS_NAME Ljava/lang/String; public fun (Ljava/lang/Class;)V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/NetworkBreadcrumbsIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/ILogger;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/PhoneStateBreadcrumbsIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor { @@ -360,7 +360,7 @@ public final class io/sentry/android/core/SystemEventsBreadcrumbsIntegration : i public fun (Landroid/content/Context;)V public fun (Landroid/content/Context;Ljava/util/List;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/TempSensorBreadcrumbsIntegration : android/hardware/SensorEventListener, io/sentry/Integration, java/io/Closeable { @@ -368,7 +368,7 @@ public final class io/sentry/android/core/TempSensorBreadcrumbsIntegration : and public fun close ()V public fun onAccuracyChanged (Landroid/hardware/Sensor;I)V public fun onSensorChanged (Landroid/hardware/SensorEvent;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/UserInteractionIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/Integration, java/io/Closeable { @@ -381,7 +381,7 @@ public final class io/sentry/android/core/UserInteractionIntegration : android/a public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentry/EventProcessor { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java index dc03abe808..4a5ca63717 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java @@ -8,7 +8,7 @@ import android.os.Bundle; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -23,7 +23,7 @@ public final class ActivityBreadcrumbsIntegration implements Integration, Closeable, Application.ActivityLifecycleCallbacks { private final @NotNull Application application; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private boolean enabled; public ActivityBreadcrumbsIntegration(final @NotNull Application application) { @@ -31,13 +31,13 @@ public ActivityBreadcrumbsIntegration(final @NotNull Application application) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { final SentryAndroidOptions androidOptions = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.enabled = androidOptions.isEnableActivityLifecycleBreadcrumbs(); options .getLogger() @@ -54,8 +54,9 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio public void close() throws IOException { if (enabled) { application.unregisterActivityLifecycleCallbacks(this); - if (hub != null) { - hub.getOptions() + if (scopes != null) { + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "ActivityBreadcrumbsIntegration removed."); } @@ -100,7 +101,7 @@ public synchronized void onActivityDestroyed(final @NotNull Activity activity) { } private void addBreadcrumb(final @NotNull Activity activity, final @NotNull String state) { - if (hub == null) { + if (scopes == null) { return; } @@ -114,7 +115,7 @@ private void addBreadcrumb(final @NotNull Activity activity, final @NotNull Stri final Hint hint = new Hint(); hint.set(ANDROID_ACTIVITY, activity); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } private @NotNull String getActivityName(final @NotNull Activity activity) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java index 1121a6bfe7..76bedae5d0 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java @@ -12,8 +12,8 @@ import android.view.View; import androidx.annotation.NonNull; import io.sentry.FullyDisplayedReporter; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.Instrumenter; @@ -60,7 +60,7 @@ public final class ActivityLifecycleIntegration private final @NotNull Application application; private final @NotNull BuildInfoProvider buildInfoProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private boolean performanceEnabled = false; @@ -102,13 +102,13 @@ public ActivityLifecycleIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); performanceEnabled = isPerformanceEnabled(this.options); fullyDisplayedReporter = this.options.getFullyDisplayedReporter(); @@ -150,10 +150,10 @@ private void stopPreviousTransactions() { private void startTracing(final @NotNull Activity activity) { WeakReference weakActivity = new WeakReference<>(activity); - if (hub != null && !isRunningTransactionOrTrace(activity)) { + if (scopes != null && !isRunningTransactionOrTrace(activity)) { if (!performanceEnabled) { activitiesWithOngoingTransactions.put(activity, NoOpTransaction.getInstance()); - TracingUtils.startNewTrace(hub); + TracingUtils.startNewTrace(scopes); } else { // as we allow a single transaction running on the bound Scope, we finish the previous ones stopPreviousTransactions(); @@ -225,7 +225,7 @@ private void startTracing(final @NotNull Activity activity) { // we can only bind to the scope if there's no running transaction ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext( activityName, TransactionNameSource.COMPONENT, @@ -278,7 +278,7 @@ private void startTracing(final @NotNull Activity activity) { } // lets bind to the scope so other integrations can pick it up - hub.configureScope( + scopes.configureScope( scope -> { applyScope(scope, transaction); }); @@ -356,10 +356,10 @@ private void finishTransaction( status = SpanStatus.OK; } transaction.finish(status); - if (hub != null) { + if (scopes != null) { // make sure to remove the transaction from scope, as it may contain running children, // therefore `finish` method will not remove it from scope - hub.configureScope( + scopes.configureScope( scope -> { clearScope(scope, transaction); }); @@ -371,9 +371,9 @@ private void finishTransaction( public synchronized void onActivityCreated( final @NotNull Activity activity, final @Nullable Bundle savedInstanceState) { setColdStart(savedInstanceState); - if (hub != null) { + if (scopes != null) { final @Nullable String activityClassName = ClassUtil.getClassName(activity); - hub.configureScope(scope -> scope.setScreen(activityClassName)); + scopes.configureScope(scope -> scope.setScreen(activityClassName)); } startTracing(activity); final @Nullable ISpan ttfdSpan = ttfdSpanMap.get(activity); @@ -429,10 +429,10 @@ public void onActivityPrePaused(@NonNull Activity activity) { // well // this ensures any newly launched activity will not use the app start timestamp as txn start firstActivityCreated = true; - if (hub == null) { + if (scopes == null) { lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime(); } else { - lastPausedTime = hub.getOptions().getDateProvider().now(); + lastPausedTime = scopes.getOptions().getDateProvider().now(); } } } @@ -445,10 +445,10 @@ public synchronized void onActivityPaused(final @NotNull Activity activity) { // well // this ensures any newly launched activity will not use the app start timestamp as txn start firstActivityCreated = true; - if (hub == null) { + if (scopes == null) { lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime(); } else { - lastPausedTime = hub.getOptions().getDateProvider().now(); + lastPausedTime = scopes.getOptions().getDateProvider().now(); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java index 3abfe1306a..0d232a6ada 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java @@ -9,15 +9,15 @@ import android.os.Build; import android.os.Process; import android.os.SystemClock; -import io.sentry.HubAdapter; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.ISentryExecutorService; import io.sentry.ITransaction; import io.sentry.ITransactionProfiler; import io.sentry.PerformanceCollectionData; import io.sentry.ProfilingTraceData; import io.sentry.ProfilingTransactionData; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.android.core.internal.util.CpuInfoUtils; @@ -46,8 +46,8 @@ final class AndroidTransactionProfiler implements ITransactionProfiler { private long profileStartCpuMillis; /** - * @deprecated please use a constructor that doesn't takes a {@link IHub} instead, as it would be - * ignored anyway. + * @deprecated please use a constructor that doesn't takes a {@link IScopes} instead, as it would + * be ignored anyway. */ @Deprecated public AndroidTransactionProfiler( @@ -55,7 +55,7 @@ public AndroidTransactionProfiler( final @NotNull SentryAndroidOptions sentryAndroidOptions, final @NotNull BuildInfoProvider buildInfoProvider, final @NotNull SentryFrameMetricsCollector frameMetricsCollector, - final @NotNull IHub hub) { + final @NotNull IScopes scopes) { this(context, sentryAndroidOptions, buildInfoProvider, frameMetricsCollector); } @@ -311,7 +311,7 @@ public void close() { currentProfilingTransactionData.getTraceId(), true, null, - HubAdapter.getInstance().getOptions()); + ScopesAdapter.getInstance().getOptions()); } else if (transactionsCounter != 0) { // in case the app start profiling is running, and it's not bound to a transaction, we still // stop profiling, but we also have to manually update the counter. diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java index 0ad2c242da..90d53a3c9b 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java @@ -5,7 +5,7 @@ import android.annotation.SuppressLint; import android.content.Context; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -48,12 +48,13 @@ public AnrIntegration(final @NotNull Context context) { private static final @NotNull Object watchDogLock = new Object(); @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { this.options = Objects.requireNonNull(options, "SentryOptions is required"); - register(hub, (SentryAndroidOptions) options); + register(scopes, (SentryAndroidOptions) options); } - private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + private void register( + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { options .getLogger() .log(SentryLevel.DEBUG, "AnrIntegration enabled: %s", options.isAnrEnabled()); @@ -67,7 +68,7 @@ private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptio () -> { synchronized (startLock) { if (!isClosed) { - startAnrWatchdog(hub, options); + startAnrWatchdog(scopes, options); } } }); @@ -80,7 +81,7 @@ private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptio } private void startAnrWatchdog( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { synchronized (watchDogLock) { if (anrWatchDog == null) { options @@ -94,7 +95,7 @@ private void startAnrWatchdog( new ANRWatchDog( options.getAnrTimeoutIntervalMillis(), options.isAnrReportInDebug(), - error -> reportANR(hub, options, error), + error -> reportANR(scopes, options, error), options.getLogger(), context); anrWatchDog.start(); @@ -106,7 +107,7 @@ private void startAnrWatchdog( @TestOnly void reportANR( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options, final @NotNull ApplicationNotResponding error) { options.getLogger().log(SentryLevel.INFO, "ANR triggered with message: %s", error.getMessage()); @@ -122,7 +123,7 @@ void reportANR( final AnrHint anrHint = new AnrHint(isAppInBackground); final Hint hint = HintUtils.createWithTypeCheckHint(anrHint); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } private @NotNull Throwable buildAnrThrowable( diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java index 669233bb09..152ceed322 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java @@ -9,8 +9,8 @@ import io.sentry.Attachment; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -69,7 +69,7 @@ public AnrV2Integration(final @NotNull Context context) { @SuppressLint("NewApi") // we do the check in the AnrIntegrationFactory @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -90,7 +90,7 @@ public void register(@NotNull IHub hub, @NotNull SentryOptions options) { try { options .getExecutorService() - .submit(new AnrProcessor(context, hub, this.options, dateProvider)); + .submit(new AnrProcessor(context, scopes, this.options, dateProvider)); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to start AnrProcessor.", e); } @@ -109,17 +109,17 @@ public void close() throws IOException { static class AnrProcessor implements Runnable { private final @NotNull Context context; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryAndroidOptions options; private final long threshold; AnrProcessor( final @NotNull Context context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options, final @NotNull ICurrentDateProvider dateProvider) { this.context = context; - this.hub = hub; + this.scopes = scopes; this.options = options; this.threshold = dateProvider.getCurrentTimeMillis() - NINETY_DAYS_THRESHOLD; } @@ -277,7 +277,7 @@ private void reportAsSentryEvent( } } - final @NotNull SentryId sentryId = hub.captureEvent(event, hint); + final @NotNull SentryId sentryId = scopes.captureEvent(event, hint); final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID); if (!isEventDropped) { // Block until the event is flushed to disk and the last_reported_anr marker is updated diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java index eef4707683..0d20dc7a90 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java @@ -8,7 +8,7 @@ import android.content.res.Configuration; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -25,7 +25,7 @@ public final class AppComponentsBreadcrumbsIntegration implements Integration, Closeable, ComponentCallbacks2 { private final @NotNull Context context; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { @@ -33,8 +33,8 @@ public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -84,7 +84,7 @@ public void close() throws IOException { @SuppressWarnings("deprecation") @Override public void onConfigurationChanged(@NotNull Configuration newConfig) { - if (hub != null) { + if (scopes != null) { final Device.DeviceOrientation deviceOrientation = DeviceOrientations.getOrientation(context.getResources().getConfiguration().orientation); @@ -104,7 +104,7 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) { final Hint hint = new Hint(); hint.set(ANDROID_CONFIGURATION, newConfig); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } @@ -119,7 +119,7 @@ public void onTrimMemory(final int level) { } private void createLowMemoryBreadcrumb(final @Nullable Integer level) { - if (hub != null) { + if (scopes != null) { final Breadcrumb breadcrumb = new Breadcrumb(); if (level != null) { // only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or @@ -143,7 +143,7 @@ private void createLowMemoryBreadcrumb(final @Nullable Integer level) { breadcrumb.setMessage("Low memory"); breadcrumb.setData("action", "LOW_MEMORY"); breadcrumb.setLevel(SentryLevel.WARNING); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java index 3e8fe6383f..8614a60061 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java @@ -3,7 +3,7 @@ import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; import androidx.lifecycle.ProcessLifecycleOwner; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -32,8 +32,8 @@ public AppLifecycleIntegration() { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -59,11 +59,11 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio Class.forName("androidx.lifecycle.DefaultLifecycleObserver"); Class.forName("androidx.lifecycle.ProcessLifecycleOwner"); if (AndroidMainThreadChecker.getInstance().isMainThread()) { - addObserver(hub); + addObserver(scopes); } else { // some versions of the androidx lifecycle-process require this to be executed on the main // thread. - handler.post(() -> addObserver(hub)); + handler.post(() -> addObserver(scopes)); } } catch (ClassNotFoundException e) { options @@ -80,7 +80,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } } - private void addObserver(final @NotNull IHub hub) { + private void addObserver(final @NotNull IScopes scopes) { // this should never happen, check added to avoid warnings from NullAway if (this.options == null) { return; @@ -88,7 +88,7 @@ private void addObserver(final @NotNull IHub hub) { watcher = new LifecycleWatcher( - hub, + scopes, this.options.getSessionTrackingIntervalMillis(), this.options.isEnableAutoSessionTracking(), this.options.isEnableAppLifecycleBreadcrumbs()); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java index b4c5f1ed02..0b618636d3 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java @@ -4,7 +4,7 @@ import android.app.Application; import android.os.Bundle; import androidx.annotation.NonNull; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryOptions; import io.sentry.util.Objects; @@ -25,7 +25,7 @@ public CurrentActivityIntegration(final @NotNull Application application) { } @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { application.registerActivityLifecycleCallbacks(this); } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java index f99294584b..6e821e5be7 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java @@ -1,7 +1,7 @@ package io.sentry.android.core; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.OutboxSender; import io.sentry.SentryLevel; @@ -24,8 +24,8 @@ public abstract class EnvelopeFileObserverIntegration implements Integration, Cl } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); logger = options.getLogger(); @@ -46,7 +46,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions () -> { synchronized (startLock) { if (!isClosed) { - startOutboxSender(hub, options, path); + startOutboxSender(scopes, options, path); } } }); @@ -60,10 +60,12 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions } private void startOutboxSender( - final @NotNull IHub hub, final @NotNull SentryOptions options, final @NotNull String path) { + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull String path) { final OutboxSender outboxSender = new OutboxSender( - hub, + scopes, options.getEnvelopeReader(), options.getSerializer(), options.getLogger(), diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index 9bdbe86a77..692d8562f8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -4,12 +4,12 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; import io.sentry.ILogger; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISerializer; import io.sentry.ObjectWriter; +import io.sentry.ScopesAdapter; import io.sentry.SentryEnvelope; import io.sentry.SentryEnvelopeItem; import io.sentry.SentryEvent; @@ -39,12 +39,12 @@ public final class InternalSentrySdk { /** - * @return a copy of the current hub's topmost scope, or null in case the hub is disabled + * @return a copy of the current scopes's topmost scope, or null in case the scopes is disabled */ @Nullable public static IScope getCurrentScope() { final @NotNull AtomicReference scopeRef = new AtomicReference<>(); - HubAdapter.getInstance() + ScopesAdapter.getInstance() .configureScope( scope -> { scopeRef.set(scope.clone()); @@ -134,8 +134,8 @@ public static Map serializeScope( } /** - * Captures the provided envelope. Compared to {@link IHub#captureEvent(SentryEvent)} this method - *
+ * Captures the provided envelope. Compared to {@link IScopes#captureEvent(SentryEvent)} this + * method
* - will not enrich events with additional data (e.g. scope)
* - will not execute beforeSend: it's up to the caller to take care of this
* - will not perform any sampling: it's up to the caller to take care of this
@@ -147,8 +147,8 @@ public static Map serializeScope( */ @Nullable public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { - final @NotNull IHub hub = HubAdapter.getInstance(); - final @NotNull SentryOptions options = hub.getOptions(); + final @NotNull IScopes scopes = ScopesAdapter.getInstance(); + final @NotNull SentryOptions options = scopes.getOptions(); try (final InputStream envelopeInputStream = new ByteArrayInputStream(envelopeData)) { final @NotNull ISerializer serializer = options.getSerializer(); @@ -178,7 +178,7 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { } // update session and add it to envelope if necessary - final @Nullable Session session = updateSession(hub, options, status, crashedOrErrored); + final @Nullable Session session = updateSession(scopes, options, status, crashedOrErrored); if (session != null) { final SentryEnvelopeItem sessionItem = SentryEnvelopeItem.fromSession(serializer, session); envelopeItems.add(sessionItem); @@ -186,7 +186,7 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { final SentryEnvelope repackagedEnvelope = new SentryEnvelope(envelope.getHeader(), envelopeItems); - return hub.captureEnvelope(repackagedEnvelope); + return scopes.captureEnvelope(repackagedEnvelope); } catch (Throwable t) { options.getLogger().log(SentryLevel.ERROR, "Failed to capture envelope", t); } @@ -195,12 +195,12 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { @Nullable private static Session updateSession( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryOptions options, final @Nullable Session.State status, final boolean crashedOrErrored) { final @NotNull AtomicReference sessionRef = new AtomicReference<>(); - hub.configureScope( + scopes.configureScope( scope -> { final @Nullable Session session = scope.getSession(); if (session != null) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java b/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java index 7b38bcd9c2..a32fa51d3f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java @@ -3,7 +3,7 @@ import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.LifecycleOwner; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.Session; import io.sentry.android.core.internal.util.BreadcrumbFactory; @@ -25,19 +25,19 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { private @Nullable TimerTask timerTask; private final @Nullable Timer timer; private final @NotNull Object timerLock = new Object(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final boolean enableSessionTracking; private final boolean enableAppLifecycleBreadcrumbs; private final @NotNull ICurrentDateProvider currentDateProvider; LifecycleWatcher( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final long sessionIntervalMillis, final boolean enableSessionTracking, final boolean enableAppLifecycleBreadcrumbs) { this( - hub, + scopes, sessionIntervalMillis, enableSessionTracking, enableAppLifecycleBreadcrumbs, @@ -45,7 +45,7 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { } LifecycleWatcher( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final long sessionIntervalMillis, final boolean enableSessionTracking, final boolean enableAppLifecycleBreadcrumbs, @@ -53,7 +53,7 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { this.sessionIntervalMillis = sessionIntervalMillis; this.enableSessionTracking = enableSessionTracking; this.enableAppLifecycleBreadcrumbs = enableAppLifecycleBreadcrumbs; - this.hub = hub; + this.scopes = scopes; this.currentDateProvider = currentDateProvider; if (enableSessionTracking) { timer = new Timer(true); @@ -79,7 +79,7 @@ private void startSession() { final long currentTimeMillis = currentDateProvider.getCurrentTimeMillis(); - hub.configureScope( + scopes.configureScope( scope -> { if (lastUpdatedSession.get() == 0L) { final @Nullable Session currentSession = scope.getSession(); @@ -93,7 +93,7 @@ private void startSession() { if (lastUpdatedSession == 0L || (lastUpdatedSession + sessionIntervalMillis) <= currentTimeMillis) { addSessionBreadcrumb("start"); - hub.startSession(); + scopes.startSession(); } this.lastUpdatedSession.set(currentTimeMillis); } @@ -123,7 +123,7 @@ private void scheduleEndSession() { @Override public void run() { addSessionBreadcrumb("end"); - hub.endSession(); + scopes.endSession(); } }; @@ -148,13 +148,13 @@ private void addAppBreadcrumb(final @NotNull String state) { breadcrumb.setData("state", state); breadcrumb.setCategory("app.lifecycle"); breadcrumb.setLevel(SentryLevel.INFO); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } private void addSessionBreadcrumb(final @NotNull String state) { final Breadcrumb breadcrumb = BreadcrumbFactory.forSession(state); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } @TestOnly diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java index 3a4a91498e..dc464303c6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java @@ -2,7 +2,7 @@ import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -28,8 +28,8 @@ public NdkIntegration(final @Nullable Class sentryNdkClass) { } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -38,7 +38,8 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions final boolean enabled = this.options.isEnableNdk(); this.options.getLogger().log(SentryLevel.DEBUG, "NdkIntegration enabled: %s", enabled); - // Note: `hub` isn't used here because the NDK integration writes files to disk which are picked + // Note: `scopes` isn't used here because the NDK integration writes files to disk which are + // picked // up by another integration (EnvelopeFileObserverIntegration). if (enabled && sentryNdkClass != null) { final String cachedDir = this.options.getCacheDirPath(); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java index 1cd42e9dab..9f1dd3ecf6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java @@ -13,8 +13,8 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryDateProvider; import io.sentry.SentryLevel; @@ -50,8 +50,8 @@ public NetworkBreadcrumbsIntegration( @SuppressLint("NewApi") @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); SentryAndroidOptions androidOptions = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -72,7 +72,8 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } networkCallback = - new NetworkBreadcrumbsNetworkCallback(hub, buildInfoProvider, options.getDateProvider()); + new NetworkBreadcrumbsNetworkCallback( + scopes, buildInfoProvider, options.getDateProvider()); final boolean registered = AndroidConnectionStatusProvider.registerNetworkCallback( context, logger, buildInfoProvider, networkCallback); @@ -101,7 +102,7 @@ public void close() throws IOException { @SuppressLint("ObsoleteSdkInt") @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager.NetworkCallback { - final @NotNull IHub hub; + final @NotNull IScopes scopes; final @NotNull BuildInfoProvider buildInfoProvider; @Nullable Network currentNetwork = null; @@ -111,10 +112,10 @@ static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager final @NotNull SentryDateProvider dateProvider; NetworkBreadcrumbsNetworkCallback( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull BuildInfoProvider buildInfoProvider, final @NotNull SentryDateProvider dateProvider) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.buildInfoProvider = Objects.requireNonNull(buildInfoProvider, "BuildInfoProvider is required"); this.dateProvider = Objects.requireNonNull(dateProvider, "SentryDateProvider is required"); @@ -126,7 +127,7 @@ public void onAvailable(final @NonNull Network network) { return; } final Breadcrumb breadcrumb = createBreadcrumb("NETWORK_AVAILABLE"); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); currentNetwork = network; lastCapabilities = null; } @@ -156,7 +157,7 @@ public void onCapabilitiesChanged( } Hint hint = new Hint(); hint.set(TypeCheckHint.ANDROID_NETWORK_CAPABILITIES, connectionDetail); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } @Override @@ -165,7 +166,7 @@ public void onLost(final @NonNull Network network) { return; } final Breadcrumb breadcrumb = createBreadcrumb("NETWORK_LOST"); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); currentNetwork = null; lastCapabilities = null; } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java index c10d25b057..cae1492d3e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java @@ -6,7 +6,7 @@ import android.content.Context; import android.telephony.TelephonyManager; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -32,8 +32,8 @@ public PhoneStateBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -55,7 +55,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio () -> { synchronized (startLock) { if (!isClosed) { - startTelephonyListener(hub, options); + startTelephonyListener(scopes, options); } } }); @@ -72,11 +72,11 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio @SuppressWarnings("deprecation") private void startTelephonyListener( - final @NotNull IHub hub, final @NotNull SentryOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { try { - listener = new PhoneStateChangeListener(hub); + listener = new PhoneStateChangeListener(scopes); telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE); options.getLogger().log(SentryLevel.DEBUG, "PhoneStateBreadcrumbsIntegration installed."); @@ -110,10 +110,10 @@ public void close() throws IOException { @SuppressWarnings("deprecation") static final class PhoneStateChangeListener extends android.telephony.PhoneStateListener { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - PhoneStateChangeListener(final @NotNull IHub hub) { - this.hub = hub; + PhoneStateChangeListener(final @NotNull IScopes scopes) { + this.scopes = scopes; } @SuppressWarnings("deprecation") @@ -128,7 +128,7 @@ public void onCallStateChanged(int state, String incomingNumber) { breadcrumb.setData("action", "CALL_STATE_RINGING"); breadcrumb.setMessage("Device ringing"); breadcrumb.setLevel(SentryLevel.INFO); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java index 66e534bb7d..64f1cab362 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java @@ -2,7 +2,7 @@ import io.sentry.DataCategory; import io.sentry.IConnectionStatusProvider; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SendCachedEnvelopeFireAndForgetIntegration; import io.sentry.SentryLevel; @@ -28,7 +28,7 @@ final class SendCachedEnvelopeIntegration private final @NotNull LazyEvaluator startupCrashMarkerEvaluator; private final AtomicBoolean startupCrashHandled = new AtomicBoolean(false); private @Nullable IConnectionStatusProvider connectionStatusProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget sender; private final AtomicBoolean isInitialized = new AtomicBoolean(false); @@ -42,8 +42,8 @@ public SendCachedEnvelopeIntegration( } @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -55,7 +55,7 @@ public void register(@NotNull IHub hub, @NotNull SentryOptions options) { return; } - sendCachedEnvelopes(hub, this.options); + sendCachedEnvelopes(scopes, this.options); } @Override @@ -69,14 +69,14 @@ public void close() throws IOException { @Override public void onConnectionStatusChanged( final @NotNull IConnectionStatusProvider.ConnectionStatus status) { - if (hub != null && options != null) { - sendCachedEnvelopes(hub, options); + if (scopes != null && options != null) { + sendCachedEnvelopes(scopes, options); } } @SuppressWarnings({"NullAway"}) private synchronized void sendCachedEnvelopes( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { try { final Future future = options @@ -97,7 +97,7 @@ private synchronized void sendCachedEnvelopes( connectionStatusProvider = options.getConnectionStatusProvider(); connectionStatusProvider.addConnectionStatusObserver(this); - sender = factory.create(hub, options); + sender = factory.create(scopes, options); } if (connectionStatusProvider != null @@ -110,7 +110,7 @@ private synchronized void sendCachedEnvelopes( } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { options diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index af68a026fb..424de4d82e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -4,8 +4,8 @@ import android.content.Context; import android.os.Process; import android.os.SystemClock; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.OptionsContainer; import io.sentry.Sentry; @@ -144,10 +144,11 @@ public static synchronized void init( }, true); - final @NotNull IHub hub = Sentry.getCurrentHub(); - if (hub.getOptions().isEnableAutoSessionTracking() && ContextUtils.isForegroundImportance()) { - hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start")); - hub.startSession(); + final @NotNull IScopes scopes = Sentry.getCurrentScopes(); + if (scopes.getOptions().isEnableAutoSessionTracking() + && ContextUtils.isForegroundImportance()) { + scopes.addBreadcrumb(BreadcrumbFactory.forSession("session.start")); + scopes.startSession(); } } catch (IllegalAccessException e) { logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java index 1c22a7dcc8..333ece2148 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java @@ -40,8 +40,8 @@ import android.os.Bundle; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -80,8 +80,8 @@ public SystemEventsBreadcrumbsIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -103,7 +103,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio () -> { synchronized (startLock) { if (!isClosed) { - startSystemEventsReceiver(hub, (SentryAndroidOptions) options); + startSystemEventsReceiver(scopes, (SentryAndroidOptions) options); } } }); @@ -119,8 +119,8 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } private void startSystemEventsReceiver( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { - receiver = new SystemEventsBroadcastReceiver(hub, options.getLogger()); + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { + receiver = new SystemEventsBroadcastReceiver(scopes, options.getLogger()); final IntentFilter filter = new IntentFilter(); for (String item : actions) { filter.addAction(item); @@ -204,11 +204,11 @@ public void close() throws IOException { static final class SystemEventsBroadcastReceiver extends BroadcastReceiver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ILogger logger; - SystemEventsBroadcastReceiver(final @NotNull IHub hub, final @NotNull ILogger logger) { - this.hub = hub; + SystemEventsBroadcastReceiver(final @NotNull IScopes scopes, final @NotNull ILogger logger) { + this.scopes = scopes; this.logger = logger; } @@ -249,7 +249,7 @@ public void onReceive(Context context, Intent intent) { final Hint hint = new Hint(); hint.set(ANDROID_INTENT, intent); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java index eaf5c64991..4d0e9c7e60 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java @@ -11,7 +11,7 @@ import android.hardware.SensorManager; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -26,7 +26,7 @@ public final class TempSensorBreadcrumbsIntegration implements Integration, Closeable, SensorEventListener { private final @NotNull Context context; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; @TestOnly @Nullable SensorManager sensorManager; @@ -38,8 +38,8 @@ public TempSensorBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -121,7 +121,7 @@ public void onSensorChanged(final @NotNull SensorEvent event) { return; } - if (hub != null) { + if (scopes != null) { final Breadcrumb breadcrumb = new Breadcrumb(); breadcrumb.setType("system"); breadcrumb.setCategory("device.event"); @@ -134,7 +134,7 @@ public void onSensorChanged(final @NotNull SensorEvent event) { final Hint hint = new Hint(); hint.set(ANDROID_SENSOR_EVENT, event); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java index c361529671..712651b460 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java @@ -6,7 +6,7 @@ import android.app.Application; import android.os.Bundle; import android.view.Window; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -23,7 +23,7 @@ public final class UserInteractionIntegration implements Integration, Closeable, Application.ActivityLifecycleCallbacks { private final @NotNull Application application; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private final boolean isAndroidXAvailable; @@ -44,14 +44,14 @@ private void startTracking(final @NotNull Activity activity) { return; } - if (hub != null && options != null) { + if (scopes != null && options != null) { Window.Callback delegate = window.getCallback(); if (delegate == null) { delegate = new NoOpWindowCallback(); } final SentryGestureListener gestureListener = - new SentryGestureListener(activity, hub, options); + new SentryGestureListener(activity, scopes, options); window.setCallback(new SentryWindowCallback(delegate, activity, gestureListener, options)); } } @@ -102,13 +102,13 @@ public void onActivitySaveInstanceState(@NotNull Activity activity, @NotNull Bun public void onActivityDestroyed(@NotNull Activity activity) {} @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); final boolean integrationEnabled = this.options.isEnableUserInteractionBreadcrumbs() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java index 0ec0d83258..9154f3e7c6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java @@ -10,8 +10,8 @@ import android.view.Window; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.SentryLevel; import io.sentry.SpanStatus; @@ -43,7 +43,7 @@ private enum GestureType { private static final String TRACE_ORIGIN = "auto.ui.gesture_listener"; private final @NotNull WeakReference activityRef; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryAndroidOptions options; private @Nullable UiElement activeUiElement = null; @@ -54,10 +54,10 @@ private enum GestureType { public SentryGestureListener( final @NotNull Activity currentActivity, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { this.activityRef = new WeakReference<>(currentActivity); - this.hub = hub; + this.scopes = scopes; this.options = options; } @@ -185,7 +185,7 @@ private void addBreadcrumb( hint.set(ANDROID_MOTION_EVENT, motionEvent); hint.set(ANDROID_VIEW, target.getView()); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.userInteraction( type, target.getResourceName(), target.getClassName(), target.getTag(), additionalData), hint); @@ -202,7 +202,7 @@ private void startTracing(final @NotNull UiElement target, final @NotNull Gestur if (!(options.isTracingEnabled() && options.isEnableUserInteractionTracing())) { if (isNewInteraction) { - TracingUtils.startNewTrace(hub); + TracingUtils.startNewTrace(scopes); activeUiElement = target; activeEventType = eventType; } @@ -253,12 +253,12 @@ private void startTracing(final @NotNull UiElement target, final @NotNull Gestur transactionOptions.setTrimEnd(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(name, TransactionNameSource.COMPONENT, op), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN + "." + target.getOrigin()); - hub.configureScope( + scopes.configureScope( scope -> { applyScope(scope, transaction); }); @@ -278,7 +278,7 @@ void stopTracing(final @NotNull SpanStatus status) { activeTransaction.finish(); } } - hub.configureScope( + scopes.configureScope( scope -> { // avoid method refs on Android due to some issues with older AGP setups // noinspection Convert2MethodRef diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt index 405aa6dc98..436c3ee6f9 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt @@ -5,8 +5,8 @@ import android.os.Build import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.CpuCollectionData -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.MemoryCollectionData import io.sentry.PerformanceCollectionData @@ -89,7 +89,7 @@ class AndroidTransactionProfilerTest { executorService = mockExecutorService } - val hub: IHub = mock() + val scopes: IScopes = mock() val frameMetricsCollector: SentryFrameMetricsCollector = mock() lateinit var transaction1: SentryTracer @@ -97,10 +97,10 @@ class AndroidTransactionProfilerTest { lateinit var transaction3: SentryTracer fun getSut(context: Context, buildInfoProvider: BuildInfoProvider = buildInfo): AndroidTransactionProfiler { - whenever(hub.options).thenReturn(options) - transaction1 = SentryTracer(TransactionContext("", ""), hub) - transaction2 = SentryTracer(TransactionContext("", ""), hub) - transaction3 = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(options) + transaction1 = SentryTracer(TransactionContext("", ""), scopes) + transaction2 = SentryTracer(TransactionContext("", ""), scopes) + transaction3 = SentryTracer(TransactionContext("", ""), scopes) return AndroidTransactionProfiler(context, options, buildInfoProvider, frameMetricsCollector) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt index cceabc9774..1a74a47ae1 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt @@ -2,7 +2,7 @@ package io.sentry.android.core import android.content.Context import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.android.core.AnrIntegration.AnrHint import io.sentry.exception.ExceptionMechanismException @@ -24,7 +24,7 @@ class AnrIntegrationTest { private class Fixture { val context = mock() - val hub = mock() + val scopes = mock() var options: SentryAndroidOptions = SentryAndroidOptions().apply { setLogger(mock()) } @@ -49,7 +49,7 @@ class AnrIntegrationTest { fixture.options.executorService = ImmediateExecutorService() val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.anrWatchDog) assertTrue((sut.anrWatchDog as ANRWatchDog).isAlive) @@ -60,7 +60,7 @@ class AnrIntegrationTest { fixture.options.executorService = mock() val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) } @@ -70,7 +70,7 @@ class AnrIntegrationTest { val sut = fixture.getSut() fixture.options.isAnrEnabled = false - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) } @@ -79,9 +79,9 @@ class AnrIntegrationTest { fun `When ANR watch dog is triggered, it should capture an error event with AnrHint`() { val sut = fixture.getSut() - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.ERROR, it.level) }, @@ -97,7 +97,7 @@ class AnrIntegrationTest { val sut = fixture.getSut() fixture.options.executorService = ImmediateExecutorService() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.anrWatchDog) @@ -107,11 +107,11 @@ class AnrIntegrationTest { } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut() fixture.options.executorService = deferredExecutorService - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) sut.close() deferredExecutorService.runAll() @@ -122,9 +122,9 @@ class AnrIntegrationTest { fun `When ANR watch dog is triggered, constructs exception with proper mechanism and snapshot flag`() { val sut = fixture.getSut() - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val ex = it.throwableMechanism as ExceptionMechanismException assertTrue(ex.isSnapshot) @@ -139,9 +139,9 @@ class AnrIntegrationTest { val sut = fixture.getSut() AppState.getInstance().setInBackground(true) - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val message = it.throwable?.message assertTrue(message?.startsWith("Background") == true) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt index 885ad22c8f..1abcd43719 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt @@ -6,8 +6,8 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hint -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryEnvelope import io.sentry.SentryLevel import io.sentry.android.core.AnrV2Integration.AnrV2Hint @@ -59,7 +59,7 @@ class AnrV2IntegrationTest { lateinit var lastReportedAnrFile: File val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val logger = mock() fun getSut( @@ -93,7 +93,7 @@ class AnrV2IntegrationTest { lastReportedAnrFile = File(cacheDir, AndroidEnvelopeCache.LAST_ANR_REPORT) lastReportedAnrFile.writeText(lastReportedAnrTimestamp.toString()) } - whenever(hub.captureEvent(any(), anyOrNull())).thenReturn(lastEventId) + whenever(scopes.captureEvent(any(), anyOrNull())).thenReturn(lastEventId) return AnrV2Integration(context) } @@ -170,7 +170,7 @@ class AnrV2IntegrationTest { fun `when cacheDir is not set, does not process historical exits`() { val integration = fixture.getSut(null, useImmediateExecutorService = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.options.executorService, never()).submit(any()) } @@ -180,7 +180,7 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, isAnrEnabled = false, useImmediateExecutorService = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.options.executorService, never()).submit(any()) } @@ -189,9 +189,9 @@ class AnrV2IntegrationTest { fun `when historical exit list is empty, does not process historical exits`() { val integration = fixture.getSut(tmpDir) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -199,9 +199,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(reason = null) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -212,9 +212,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -222,9 +222,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -232,9 +232,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = null) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @Test @@ -242,9 +242,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(newTimestamp, it.timestamp.time) assertEquals(SentryLevel.FATAL, it.level) @@ -291,9 +291,9 @@ class AnrV2IntegrationTest { importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -311,7 +311,7 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - whenever(fixture.hub.captureEvent(any(), any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureEvent(any(), any())).thenAnswer { invocation -> val hint = HintUtils.getSentrySdkHint(invocation.getArgument(1)) as DiskFlushNotification thread { @@ -321,9 +321,9 @@ class AnrV2IntegrationTest { SentryId() } - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) // shouldn't fall into timed out state, because we marked event as flushed on another thread verify(fixture.logger, never()).log( any(), @@ -341,9 +341,9 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) // we do not call markFlushed, hence it should time out waiting for flush, but because // we drop the event, it should not even come to this if-check verify(fixture.logger, never()).log( @@ -360,9 +360,9 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, times(2)).captureEvent( + verify(fixture.scopes, times(2)).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -382,10 +382,10 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // only the latest anr is reported which should be enrichable - verify(fixture.hub, atMost(1)).captureEvent( + verify(fixture.scopes, atMost(1)).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -402,20 +402,20 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - TimeUnit.DAYS.toMillis(1)) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // the order is reverse here, so the oldest ANR will be reported first to keep track of // last reported ANR in a marker file - inOrder(fixture.hub) { - verify(fixture.hub).captureEvent( + inOrder(fixture.scopes) { + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp - TimeUnit.DAYS.toMillis(2) }, anyOrNull() ) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp - TimeUnit.DAYS.toMillis(1) }, anyOrNull() ) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp }, anyOrNull() ) @@ -427,9 +427,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -443,9 +443,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -472,7 +472,7 @@ class AnrV2IntegrationTest { ) } - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // we store envelope with StartSessionHint on different thread after some delay, which // triggers the previous session flush, so no timeout @@ -493,14 +493,14 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.logger, never()).log( any(), argThat { startsWith("Timed out waiting to flush previous session to its own file.") }, any() ) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -512,7 +512,7 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.logger).log( any(), @@ -532,9 +532,9 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { assertNotNull(it.threadDump) @@ -547,8 +547,8 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addTrace = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt index 15a6d690e5..5f8792c850 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt @@ -5,7 +5,7 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import org.junit.runner.RunWith import org.mockito.kotlin.any @@ -37,8 +37,8 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) verify(fixture.context).registerComponentCallbacks(any()) } @@ -46,10 +46,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is enabled, but ComponentCallbacks is not ready, do not throw`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) whenever(fixture.context.registerComponentCallbacks(any())).thenThrow(NullPointerException()) - sut.register(hub, options) + sut.register(scopes, options) assertFalse(options.isEnableAppComponentBreadcrumbs) } @@ -59,8 +59,8 @@ class AppComponentsBreadcrumbsIntegrationTest { val options = SentryAndroidOptions().apply { isEnableAppComponentBreadcrumbs = false } - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) verify(fixture.context, never()).registerComponentCallbacks(any()) } @@ -68,8 +68,8 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When AppComponentsBreadcrumbsIntegrationTest is closed, it should unregister the callback`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.close() verify(fixture.context).unregisterComponentCallbacks(any()) } @@ -78,10 +78,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is closed, but ComponentCallbacks is not ready, do not throw`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() whenever(fixture.context.registerComponentCallbacks(any())).thenThrow(NullPointerException()) whenever(fixture.context.unregisterComponentCallbacks(any())).thenThrow(NullPointerException()) - sut.register(hub, options) + sut.register(scopes, options) sut.close() } @@ -89,10 +89,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When low memory event, a breadcrumb with type, category and level should be set`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onLowMemory() - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -105,10 +105,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When trim memory event with level, a breadcrumb with type, category and level should be set`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -121,20 +121,20 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When trim memory event with level not so high, do not add a breadcrumb`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } @Test fun `When device orientation event, a breadcrumb with type, category and level should be set`() { val sut = AppComponentsBreadcrumbsIntegration(ApplicationProvider.getApplicationContext()) val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onConfigurationChanged(mock()) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.orientation", it.category) assertEquals("navigation", it.type) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt index ed8d53227c..733aefa8d6 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt @@ -2,7 +2,7 @@ package io.sentry.android.core import android.os.Looper import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.IHub +import io.sentry.IScopes import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.mock @@ -17,7 +17,7 @@ import kotlin.test.assertNull class AppLifecycleIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var handler: MainLooperHandler val options = SentryAndroidOptions() @@ -33,7 +33,7 @@ class AppLifecycleIntegrationTest { fun `When AppLifecycleIntegration is added, lifecycle watcher should be started`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) } @@ -46,7 +46,7 @@ class AppLifecycleIntegrationTest { isEnableAutoSessionTracking = false } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.watcher) } @@ -55,7 +55,7 @@ class AppLifecycleIntegrationTest { fun `When AppLifecycleIntegration is closed, lifecycle watcher should be closed`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) @@ -70,7 +70,7 @@ class AppLifecycleIntegrationTest { val latch = CountDownLatch(1) Thread { - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) latch.countDown() }.start() @@ -84,7 +84,7 @@ class AppLifecycleIntegrationTest { val sut = fixture.getSut() val latch = CountDownLatch(1) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) @@ -103,7 +103,7 @@ class AppLifecycleIntegrationTest { val sut = fixture.getSut(mockHandler = false) val latch = CountDownLatch(1) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt index 6330623121..ecdbff5104 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.app.Activity import android.app.Application import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.IHub +import io.sentry.IScopes import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.mock @@ -19,7 +19,7 @@ class CurrentActivityIntegrationTest { private class Fixture { val application = mock() val activity = mock() - val hub = mock() + val scopes = mock() val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" @@ -27,7 +27,7 @@ class CurrentActivityIntegrationTest { fun getSut(): CurrentActivityIntegration { val integration = CurrentActivityIntegration(application) - integration.register(hub, options) + integration.register(scopes, options) return integration } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt index 80954f67a5..c4fef01cc2 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt @@ -7,7 +7,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.DiagnosticLogger import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.SentryTracer @@ -62,13 +62,13 @@ class DefaultAndroidEventProcessorTest { sdkVersion = SdkVersion("test", "1.2.3") } - val hub: IHub = mock() + val scopes: IScopes = mock() lateinit var sentryTracer: SentryTracer fun getSut(context: Context): DefaultAndroidEventProcessor { - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("", ""), scopes) return DefaultAndroidEventProcessor(context, buildInfo, options) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt index 699fa2d2f2..192565f101 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt @@ -2,8 +2,8 @@ package io.sentry.android.core import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hub -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.test.DeferredExecutorService @@ -24,7 +24,7 @@ import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) class EnvelopeFileObserverIntegrationTest { inner class Fixture { - val hub: IHub = mock() + val scopes: IScopes = mock() private lateinit var options: SentryAndroidOptions val logger = mock() @@ -33,7 +33,7 @@ class EnvelopeFileObserverIntegrationTest { options.setLogger(logger) options.isDebug = true optionConfiguration(options) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return object : EnvelopeFileObserverIntegration() { override fun getPath(options: SentryOptions): String? = file.absolutePath @@ -65,7 +65,7 @@ class EnvelopeFileObserverIntegrationTest { } @Test - fun `when hub is closed, integrations should be closed`() { + fun `when scopes is closed, integrations should be closed`() { val integrationMock = mock() val options = SentryOptions() options.dsn = "https://key@sentry.io/proj" @@ -73,19 +73,19 @@ class EnvelopeFileObserverIntegrationTest { options.addIntegration(integrationMock) options.setSerializer(mock()) // val expected = HubAdapter.getInstance() - val hub = Hub(options) + val scopes = Hub(options) // verify(integrationMock).register(expected, options) - hub.close() + scopes.close() verify(integrationMock).close() } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val integration = fixture.getSut { it.executorService = deferredExecutorService } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) integration.close() deferredExecutorService.runAll() verify(fixture.logger, never()).log(eq(SentryLevel.DEBUG), eq("EnvelopeFileObserverIntegration installed.")) @@ -96,7 +96,7 @@ class EnvelopeFileObserverIntegrationTest { val integration = fixture.getSut { it.executorService = mock() } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) verify(fixture.logger).log( eq(SentryLevel.DEBUG), eq("Registering EnvelopeFileObserverIntegration for path: %s"), @@ -110,7 +110,7 @@ class EnvelopeFileObserverIntegrationTest { val integration = fixture.getSut { it.executorService = ImmediateExecutorService() } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) verify(fixture.logger).log( eq(SentryLevel.DEBUG), eq("Registering EnvelopeFileObserverIntegration for path: %s"), diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt index be30993142..73571a5ad4 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt @@ -3,8 +3,8 @@ package io.sentry.android.core import androidx.lifecycle.LifecycleOwner import io.sentry.Breadcrumb import io.sentry.DateUtils -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.ScopeCallback import io.sentry.SentryLevel import io.sentry.Session @@ -32,7 +32,7 @@ class LifecycleWatcherTest { private class Fixture { val ownerMock = mock() - val hub = mock() + val scopes = mock() val dateProvider = mock() fun getSUT( @@ -44,12 +44,12 @@ class LifecycleWatcherTest { val argumentCaptor: ArgumentCaptor = ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = mock() whenever(scope.session).thenReturn(session) - whenever(hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } return LifecycleWatcher( - hub, + scopes, sessionIntervalMillis, enableAutoSessionTracking, enableAppLifecycleBreadcrumbs, @@ -69,7 +69,7 @@ class LifecycleWatcherTest { fun `if last started session is 0, start new session`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test @@ -78,7 +78,7 @@ class LifecycleWatcherTest { whenever(fixture.dateProvider.currentTimeMillis).thenReturn(1L, 2L) watcher.onStart(fixture.ownerMock) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, times(2)).startSession() + verify(fixture.scopes, times(2)).startSession() } @Test @@ -87,7 +87,7 @@ class LifecycleWatcherTest { whenever(fixture.dateProvider.currentTimeMillis).thenReturn(2L, 1L) watcher.onStart(fixture.ownerMock) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test @@ -95,7 +95,7 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, timeout(10000)).endSession() + verify(fixture.scopes, timeout(10000)).endSession() } @Test @@ -109,14 +109,14 @@ class LifecycleWatcherTest { watcher.onStart(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).endSession() + verify(fixture.scopes, never()).endSession() } @Test fun `When session tracking is disabled, do not start session`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).startSession() + verify(fixture.scopes, never()).startSession() } @Test @@ -124,14 +124,14 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).endSession() + verify(fixture.scopes, never()).endSession() } @Test fun `When session tracking is enabled, add breadcrumb on start`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("session", it.type) @@ -145,8 +145,8 @@ class LifecycleWatcherTest { fun `When session tracking is enabled, add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, timeout(10000)).endSession() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes, timeout(10000)).endSession() + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("session", it.type) @@ -160,7 +160,7 @@ class LifecycleWatcherTest { fun `When session tracking is disabled, do not add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -168,14 +168,14 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("navigation", it.type) @@ -189,14 +189,14 @@ class LifecycleWatcherTest { fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("navigation", it.type) @@ -210,7 +210,7 @@ class LifecycleWatcherTest { fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -226,7 +226,7 @@ class LifecycleWatcherTest { } @Test - fun `if the hub has already a fresh session running, don't start new one`() { + fun `if the scopes has already a fresh session running, don't start new one`() { val watcher = fixture.getSUT( enableAppLifecycleBreadcrumbs = false, session = Session( @@ -248,11 +248,11 @@ class LifecycleWatcherTest { ) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).startSession() + verify(fixture.scopes, never()).startSession() } @Test - fun `if the hub has a long running session, start new one`() { + fun `if the scopes has a long running session, start new one`() { val watcher = fixture.getSUT( enableAppLifecycleBreadcrumbs = false, session = Session( @@ -274,7 +274,7 @@ class LifecycleWatcherTest { ) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt index e86de06814..e282ad7141 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.core -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryLevel import org.mockito.kotlin.any import org.mockito.kotlin.eq @@ -15,7 +15,7 @@ import kotlin.test.assertTrue class NdkIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() val logger = mock() fun getSut(clazz: Class<*>? = SentryNdk::class.java): NdkIntegration { @@ -31,7 +31,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) assertTrue(options.isEnableNdk) @@ -44,7 +44,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) assertTrue(options.isEnableNdk) assertTrue(options.isEnableScopeSync) @@ -62,7 +62,7 @@ class NdkIntegrationTest { val options = getOptions(enableNdk = false) - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -76,7 +76,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -90,7 +90,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) @@ -104,7 +104,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) assertTrue(options.isEnableNdk) assertTrue(options.isEnableScopeSync) @@ -122,7 +122,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) @@ -136,7 +136,7 @@ class NdkIntegrationTest { val options = getOptions(cacheDir = null) - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any()) @@ -150,7 +150,7 @@ class NdkIntegrationTest { val options = getOptions(cacheDir = "") - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any()) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt index 146f229fdf..c28cf64085 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt @@ -8,7 +8,7 @@ import android.net.NetworkCapabilities import android.os.Build import io.sentry.Breadcrumb import io.sentry.DateUtils -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryDateProvider import io.sentry.SentryLevel import io.sentry.SentryNanotimeDate @@ -39,7 +39,7 @@ class NetworkBreadcrumbsIntegrationTest { private class Fixture { val context = mock() var options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val mockBuildInfoProvider = mock() val connectivityManager = mock() var nowMs: Long = 0 @@ -68,7 +68,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When network events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager).registerDefaultNetworkCallback(any()) assertNotNull(sut.networkCallback) @@ -78,7 +78,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut(enableNetworkEventBreadcrumbs = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager, never()).registerDefaultNetworkCallback(any()) assertNull(sut.networkCallback) @@ -90,7 +90,7 @@ class NetworkBreadcrumbsIntegrationTest { whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.M) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager, never()).registerDefaultNetworkCallback(any()) assertNull(sut.networkCallback) @@ -100,7 +100,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When NetworkBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.connectivityManager).unregisterNetworkCallback(any()) @@ -114,7 +114,7 @@ class NetworkBreadcrumbsIntegrationTest { val sut = fixture.getSut(buildInfo = buildInfo) assertNull(sut.networkCallback) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.connectivityManager, never()).unregisterNetworkCallback(any()) @@ -124,12 +124,12 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When connected to a new network, a breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(mock()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -142,27 +142,27 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When connected to the same network without disconnecting from the previous one, only one breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) callback.onAvailable(fixture.network) - verify(fixture.hub, times(1)).addBreadcrumb(any()) + verify(fixture.scopes, times(1)).addBreadcrumb(any()) } @Test fun `When disconnected from a network, a breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) callback.onLost(fixture.network) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -175,12 +175,12 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When disconnected from a network, a breadcrumb is captured only if previously connected to that network`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) // callback.onAvailable(network) was not called, so no breadcrumb should be captured callback.onLost(mock()) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -188,7 +188,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -204,7 +204,7 @@ class NetworkBreadcrumbsIntegrationTest { isCellular = false ) ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -223,18 +223,18 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When a network connection detail changes, a breadcrumb is captured only if previously connected to that network`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) // callback.onAvailable(network) was not called, so no breadcrumb should be captured onCapabilitiesChanged(callback, mock()) - verify(fixture.hub, never()).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, never()).addBreadcrumb(any(), anyOrNull()) } @Test fun `When a network connection detail changes, a new breadcrumb is captured if vpn flag changes`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -245,17 +245,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1) onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertFalse(it.isVpn) } verifyBreadcrumbInOrder { assertTrue(it.isVpn) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `When a network connection detail changes, a new breadcrumb is captured if type changes`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -266,10 +266,10 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1) onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals("wifi", it.type) } verifyBreadcrumbInOrder { assertEquals("cellular", it.type) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -278,7 +278,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -289,10 +289,10 @@ class NetworkBreadcrumbsIntegrationTest { // A change of signal strength of 5 doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(50, it.signalStrength) } verifyBreadcrumbInOrder { assertEquals(56, it.signalStrength) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -301,7 +301,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -317,11 +317,11 @@ class NetworkBreadcrumbsIntegrationTest { // A change of download bandwidth of 10% (more than 1000) doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details4) onCapabilitiesChanged(callback, details5) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1000, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(20000, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(22001, it.downBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -330,7 +330,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -346,18 +346,18 @@ class NetworkBreadcrumbsIntegrationTest { // A change of upload bandwidth of 10% (more than 1000) doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details4) onCapabilitiesChanged(callback, details5) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1000, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(20000, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(22001, it.upBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `signal strength is 0 if not on Android Q+`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -371,7 +371,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -384,7 +384,7 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `A breadcrumb is captured when vpn status changes, regardless of the timestamp`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -392,17 +392,17 @@ class NetworkBreadcrumbsIntegrationTest { val details2 = createConnectionDetail(isVpn = true) onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertFalse(it.isVpn) } verifyBreadcrumbInOrder { assertTrue(it.isVpn) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when connection type changes, regardless of the timestamp`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -412,11 +412,11 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 0) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals("wifi", it.type) } verifyBreadcrumbInOrder { assertEquals("cellular", it.type) } verifyBreadcrumbInOrder { assertEquals("ethernet", it.type) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -425,7 +425,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -435,17 +435,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.signalStrength) } verifyBreadcrumbInOrder { assertEquals(51, it.signalStrength) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when downBandwidth changes at most once every 5 seconds`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -455,17 +455,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(2001, it.downBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when upBandwidth changes at most once every 5 seconds`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -475,15 +475,15 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(2001, it.upBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } private fun KInOrder.verifyBreadcrumbInOrder(check: (detail: NetworkBreadcrumbConnectionDetail) -> Unit) { - verify(fixture.hub, times(1)).addBreadcrumb( + verify(fixture.scopes, times(1)).addBreadcrumb( any(), check { val connectionDetail = it[TypeCheckHint.ANDROID_NETWORK_CAPABILITIES] as NetworkBreadcrumbConnectionDetail @@ -493,7 +493,7 @@ class NetworkBreadcrumbsIntegrationTest { } private fun verifyBreadcrumb(check: (detail: NetworkBreadcrumbConnectionDetail) -> Unit) { - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( any(), check { val connectionDetail = it[TypeCheckHint.ANDROID_NETWORK_CAPABILITIES] as NetworkBreadcrumbConnectionDetail diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt index 4c23691e63..e1593e600f 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.content.ContentProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.MeasurementUnit import io.sentry.SentryTracer import io.sentry.SpanContext @@ -35,7 +35,7 @@ class PerformanceAndroidEventProcessorTest { private class Fixture { val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val context = TransactionContext("name", "op", TracesSamplingDecision(true)) lateinit var tracer: SentryTracer val activityFramesTracker = mock() @@ -46,8 +46,8 @@ class PerformanceAndroidEventProcessorTest { ): PerformanceAndroidEventProcessor { options.tracesSampleRate = tracesSampleRate options.isEnablePerformanceV2 = enablePerformanceV2 - whenever(hub.options).thenReturn(options) - tracer = SentryTracer(context, hub) + whenever(scopes.options).thenReturn(options) + tracer = SentryTracer(context, scopes) return PerformanceAndroidEventProcessor(options, activityFramesTracker) } } @@ -181,7 +181,7 @@ class PerformanceAndroidEventProcessorTest { fun `add slow and frozen frames if auto transaction`() { val sut = fixture.getSut() val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val metrics = mapOf( @@ -227,7 +227,7 @@ class PerformanceAndroidEventProcessorTest { // when an activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) // and it contains an app.start.cold span @@ -297,7 +297,7 @@ class PerformanceAndroidEventProcessorTest { // when an activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) // then the app start metrics should not be attached @@ -326,7 +326,7 @@ class PerformanceAndroidEventProcessorTest { // when the first activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, @@ -377,7 +377,7 @@ class PerformanceAndroidEventProcessorTest { val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, @@ -424,7 +424,7 @@ class PerformanceAndroidEventProcessorTest { val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt index 2b6ca801da..c764d11c2d 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt @@ -4,7 +4,7 @@ import android.content.Context import android.telephony.PhoneStateListener import android.telephony.TelephonyManager import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.test.DeferredExecutorService @@ -41,8 +41,8 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) verify(fixture.manager).listen(any(), eq(PhoneStateListener.LISTEN_CALL_STATE)) assertNotNull(sut.listener) } @@ -50,8 +50,8 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `Phone state callback is registered in the executorService`() { val sut = fixture.getSut(mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.listener) } @@ -59,9 +59,9 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut() - val hub = mock() + val scopes = mock() sut.register( - hub, + scopes, fixture.options.apply { isEnableSystemEventBreadcrumbs = false } @@ -73,15 +73,15 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When ActivityBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.close() verify(fixture.manager).listen(any(), eq(PhoneStateListener.LISTEN_NONE)) assertNull(sut.listener) } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) sut.register(mock(), fixture.options) @@ -94,11 +94,11 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When on call state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -111,18 +111,18 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When on idle state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_IDLE, null) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } @Test fun `When on offhook state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt index 403f40ee70..f1e345eefc 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt @@ -2,8 +2,8 @@ package io.sentry.android.core import io.sentry.IConnectionStatusProvider import io.sentry.IConnectionStatusProvider.ConnectionStatus -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget import io.sentry.SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory @@ -28,7 +28,7 @@ import kotlin.test.Test class SendCachedEnvelopeIntegrationTest { private class Fixture { - val hub: IHub = mock() + val scopes: IScopes = mock() val options = SentryAndroidOptions() val logger = mock() val factory = mock() @@ -74,7 +74,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when cacheDirPath is not set, does nothing`() { val sut = fixture.getSut(cacheDirPath = null) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory, never()).create(any(), any()) } @@ -83,7 +83,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when factory returns null, does nothing`() { val sut = fixture.getSut(hasSender = false, mockExecutorService = ImmediateExecutorService()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory).create(any(), any()) verify(fixture.sender, never()).send() @@ -93,7 +93,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when has factory and cacheDirPath set, submits task into queue`() { val sut = fixture.getSut(mockExecutorService = ImmediateExecutorService()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) await.untilFalse(fixture.flag) verify(fixture.sender).send() @@ -102,7 +102,7 @@ class SendCachedEnvelopeIntegrationTest { @Test fun `when executorService is fake, does nothing`() { val sut = fixture.getSut(mockExecutorService = mock()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory, never()).create(any(), any()) verify(fixture.sender, never()).send() @@ -112,7 +112,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when has startup crash marker, awaits the task on the calling thread`() { val sut = fixture.getSut(hasStartupCrashMarker = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // we do not need to await here, because it's executed synchronously verify(fixture.sender).send() @@ -123,7 +123,7 @@ class SendCachedEnvelopeIntegrationTest { val sut = fixture.getSut(hasStartupCrashMarker = true, delaySend = 1000) fixture.options.startupCrashFlushTimeoutMillis = 100 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // first wait until synchronous send times out and check that the logger was hit in the catch block await.atLeast(500, MILLISECONDS) @@ -144,7 +144,7 @@ class SendCachedEnvelopeIntegrationTest { val connectionStatusProvider = mock() fixture.options.connectionStatusProvider = connectionStatusProvider - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(connectionStatusProvider).addConnectionStatusObserver(any()) } @@ -159,7 +159,7 @@ class SendCachedEnvelopeIntegrationTest { ConnectionStatus.DISCONNECTED ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() } @@ -174,7 +174,7 @@ class SendCachedEnvelopeIntegrationTest { ConnectionStatus.UNKNOWN ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory).create(any(), any()) } @@ -187,7 +187,7 @@ class SendCachedEnvelopeIntegrationTest { whenever(connectionStatusProvider.connectionStatus).thenReturn( ConnectionStatus.DISCONNECTED ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // when there's no connection no factory create call should be done verify(fixture.sender, never()).send() @@ -215,9 +215,9 @@ class SendCachedEnvelopeIntegrationTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(fixture.hub.rateLimiter).thenReturn(rateLimiter) + whenever(fixture.scopes.rateLimiter).thenReturn(rateLimiter) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // no factory call should be done if there's rate limiting active verify(fixture.sender, never()).send() @@ -228,7 +228,7 @@ class SendCachedEnvelopeIntegrationTest { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(mockExecutorService = deferredExecutorService) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() sut.close() diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt index f8293f9b87..146abb617e 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.content.Context import android.content.Intent import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.test.DeferredExecutorService @@ -26,7 +26,7 @@ class SystemEventsBreadcrumbsIntegrationTest { private class Fixture { val context = mock() var options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() fun getSut(enableSystemEventBreadcrumbs: Boolean = true, executorService: ISentryExecutorService = ImmediateExecutorService()): SystemEventsBreadcrumbsIntegration { options = SentryAndroidOptions().apply { @@ -43,7 +43,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.context).registerReceiver(any(), any()) assertNotNull(sut.receiver) @@ -52,8 +52,8 @@ class SystemEventsBreadcrumbsIntegrationTest { @Test fun `system events callback is registered in the executorService`() { val sut = fixture.getSut(executorService = mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.receiver) } @@ -62,7 +62,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut(enableSystemEventBreadcrumbs = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.context, never()).registerReceiver(any(), any()) assertNull(sut.receiver) @@ -72,7 +72,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When ActivityBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.context).unregisterReceiver(any()) @@ -80,10 +80,10 @@ class SystemEventsBreadcrumbsIntegrationTest { } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.receiver) sut.close() deferredExecutorService.runAll() @@ -94,13 +94,13 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When broadcast received, added breadcrumb with type and category`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val intent = Intent().apply { action = Intent.ACTION_TIME_CHANGED } sut.receiver!!.onReceive(fixture.context, intent) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -116,7 +116,7 @@ class SystemEventsBreadcrumbsIntegrationTest { val sut = fixture.getSut() whenever(fixture.context.registerReceiver(any(), any())).thenThrow(SecurityException()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertFalse(fixture.options.isEnableSystemEventBreadcrumbs) } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt index d443b1e345..5d049e3dad 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt @@ -7,7 +7,7 @@ import android.hardware.SensorEventListener import android.hardware.SensorManager import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.TypeCheckHint @@ -47,8 +47,8 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) verify(fixture.manager).registerListener(any(), any(), eq(SensorManager.SENSOR_DELAY_NORMAL)) assertNotNull(sut.sensorManager) } @@ -56,8 +56,8 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `temp sensor listener is registered in the executorService`() { val sut = fixture.getSut(executorService = mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.sensorManager) } @@ -65,9 +65,9 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is disabled, it should not register a callback`() { val sut = fixture.getSut() - val hub = mock() + val scopes = mock() sut.register( - hub, + scopes, fixture.options.apply { isEnableSystemEventBreadcrumbs = false } @@ -79,15 +79,15 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When TempSensorBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.close() verify(fixture.manager).unregisterListener(any()) assertNull(sut.sensorManager) } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) sut.register(mock(), fixture.options) @@ -100,14 +100,14 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When onSensorChanged received, add a breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) val sensorCtor = "android.hardware.SensorEvent".getDeclaredCtor(emptyArray()) val sensorEvent: SensorEvent = sensorCtor.newInstance() as SensorEvent sensorEvent.injectForField("values", FloatArray(2) { 1F }) sut.onSensorChanged(sensorEvent) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -122,12 +122,12 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When onSensorChanged received and null values, do not add a breadcrumb`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) val event = mock() assertNull(event.values) sut.onSensorChanged(event) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt index 1e6652276a..74edfb4302 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt @@ -10,8 +10,8 @@ import android.view.Window import android.widget.CheckBox import android.widget.RadioButton import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.Scope.IWithPropagationContext import io.sentry.ScopeCallback @@ -40,7 +40,7 @@ class SentryGestureListenerClickTest { gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val scope = mock() val propagationContext = PropagationContext() lateinit var target: View @@ -86,11 +86,11 @@ class SentryGestureListenerClickTest { whenever(context.resources).thenReturn(resources) whenever(this.target.context).thenReturn(context) whenever(activity.window).thenReturn(window) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) doAnswer { (it.arguments[0] as IWithPropagationContext).accept(propagationContext); propagationContext; }.whenever(scope).withPropagationContext(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -123,7 +123,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.click", it.category) assertEquals("user", it.type) @@ -146,7 +146,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("radio_button", it.data["view.id"]) assertEquals("android.widget.RadioButton", it.data["view.class"]) @@ -166,7 +166,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("check_box", it.data["view.id"]) assertEquals("android.widget.CheckBox", it.data["view.class"]) @@ -185,7 +185,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -198,7 +198,7 @@ class SentryGestureListenerClickTest { val sut = fixture.getSut(event, "decor_view", targetOverride = decorView) sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(decorView.javaClass.canonicalName, it.data["view.class"]) assertEquals("decor_view", it.data["view.id"]) @@ -214,7 +214,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -230,7 +230,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.target.javaClass.simpleName, it.data["view.class"]) }, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt index 5d39b64753..e5a9623c4d 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt @@ -11,8 +11,8 @@ import android.widget.AbsListView import android.widget.ListAdapter import androidx.core.view.ScrollingView import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.Scope import io.sentry.ScopeCallback @@ -44,7 +44,7 @@ class SentryGestureListenerScrollTest { isEnableUserInteractionTracing = true gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) } - val hub = mock() + val scopes = mock() val scope = mock() val propagationContext = PropagationContext() @@ -77,11 +77,11 @@ class SentryGestureListenerScrollTest { endEvent.mockDirection(firstEvent, direction) } whenever(activity.window).thenReturn(window) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) doAnswer { (it.arguments[0] as Scope.IWithPropagationContext).accept(propagationContext); propagationContext }.whenever(scope).withPropagationContext(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -99,7 +99,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.scroll", it.category) assertEquals("user", it.type) @@ -122,7 +122,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -143,8 +143,8 @@ class SentryGestureListenerScrollTest { sut.onFling(fixture.firstEvent, fixture.endEvent, 1.0f, 1.0f) sut.onUp(fixture.endEvent) - inOrder(fixture.hub) { - verify(fixture.hub).addBreadcrumb( + inOrder(fixture.scopes) { + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.swipe", it.category) assertEquals("user", it.type) @@ -155,8 +155,8 @@ class SentryGestureListenerScrollTest { }, anyOrNull() ) - verify(fixture.hub).configureScope(anyOrNull()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).configureScope(anyOrNull()) + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.swipe", it.category) assertEquals("user", it.type) @@ -168,7 +168,7 @@ class SentryGestureListenerScrollTest { anyOrNull() ) } - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -177,7 +177,7 @@ class SentryGestureListenerScrollTest { sut.onUp(fixture.firstEvent) sut.onDown(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -190,7 +190,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt index c7ada69c88..d3f6647c2a 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt @@ -9,8 +9,8 @@ import android.view.ViewGroup import android.view.Window import android.widget.AbsListView import android.widget.ListAdapter -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryTracer @@ -46,7 +46,7 @@ class SentryGestureListenerTracingTest { val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val event = mock() val scope = mock() lateinit var target: View @@ -64,9 +64,9 @@ class SentryGestureListenerTracingTest { options.isEnableUserInteractionBreadcrumbs = true options.gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - this.transaction = transaction ?: SentryTracer(TransactionContext("name", "op"), hub) + this.transaction = transaction ?: SentryTracer(TransactionContext("name", "op"), scopes) target = mockView(event = event, clickable = true, context = context) window.mockDecorView(event = event, context = context) { @@ -86,13 +86,13 @@ class SentryGestureListenerTracingTest { whenever(activity.window).thenReturn(window) - whenever(hub.startTransaction(any(), any())) + whenever(scopes.startTransaction(any(), any())) .thenReturn(this.transaction) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -106,7 +106,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -118,7 +118,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -130,7 +130,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -140,7 +140,7 @@ class SentryGestureListenerTracingTest { fun `when transaction is created, set transaction to the bound Scope`() { val sut = fixture.getSut() - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) sut.applyScope(scope, fixture.transaction) @@ -155,9 +155,9 @@ class SentryGestureListenerTracingTest { fun `when transaction is created, do not overwrite transaction already bound to the Scope`() { val sut = fixture.getSut() - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) - val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.scopes) scope.transaction = previousTransaction sut.applyScope(scope, fixture.transaction) @@ -173,14 +173,14 @@ class SentryGestureListenerTracingTest { val sut = fixture.getSut() val expectedStatus = SpanStatus.CANCELLED - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) sut.applyScope(scope, fixture.transaction) } sut.onSingleTapUp(fixture.event) - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) scope.transaction = fixture.transaction @@ -199,7 +199,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -214,7 +214,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { transactionOptions -> assertEquals(fixture.options.idleTimeout, transactionOptions.idleTimeout) @@ -232,7 +232,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("ui.action.click", it.operation) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -248,7 +248,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -256,7 +256,7 @@ class SentryGestureListenerTracingTest { any() ) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) // second view interaction with another view val newTarget = mockView(event = fixture.event, clickable = true, context = fixture.context) val newContext = mock() @@ -269,16 +269,16 @@ class SentryGestureListenerTracingTest { whenever(it.getChildAt(0)).thenReturn(newTarget) } - whenever(fixture.hub.startTransaction(any(), any())) + whenever(fixture.scopes.startTransaction(any(), any())) .thenAnswer { // verify that the active transaction gets finished when a new one appears assertEquals(true, fixture.transaction.isFinished) - SentryTracer(TransactionContext("name", "op"), fixture.hub) + SentryTracer(TransactionContext("name", "op"), fixture.scopes) } sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_checkbox", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -293,7 +293,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_scroll_view", it.name) assertEquals("ui.action.click", it.operation) @@ -302,20 +302,20 @@ class SentryGestureListenerTracingTest { any() ) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) // second view interaction with a different interaction type (scroll) - whenever(fixture.hub.startTransaction(any(), any())) + whenever(fixture.scopes.startTransaction(any(), any())) .thenAnswer { // verify that the active transaction gets finished when a new one appears assertEquals(true, fixture.transaction.isFinished) - SentryTracer(TransactionContext("name", "op"), fixture.hub) + SentryTracer(TransactionContext("name", "op"), fixture.scopes) } sut.onScroll(fixture.event, mock(), 10.0f, 0f) sut.onUp(mock()) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_scroll_view", it.name) assertEquals("ui.action.scroll", it.operation) @@ -340,7 +340,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) // then two transaction should be captured - verify(fixture.hub, times(2)).startTransaction( + verify(fixture.scopes, times(2)).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) diff --git a/sentry-android-fragment/api/sentry-android-fragment.api b/sentry-android-fragment/api/sentry-android-fragment.api index 4b3487c36e..f2f3334280 100644 --- a/sentry-android-fragment/api/sentry-android-fragment.api +++ b/sentry-android-fragment/api/sentry-android-fragment.api @@ -18,7 +18,7 @@ public final class io/sentry/android/fragment/FragmentLifecycleIntegration : and public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/fragment/FragmentLifecycleState : java/lang/Enum { @@ -40,9 +40,9 @@ public final class io/sentry/android/fragment/FragmentLifecycleState : java/lang public final class io/sentry/android/fragment/SentryFragmentLifecycleCallbacks : androidx/fragment/app/FragmentManager$FragmentLifecycleCallbacks { public static final field Companion Lio/sentry/android/fragment/SentryFragmentLifecycleCallbacks$Companion; public static final field FRAGMENT_LOAD_OP Ljava/lang/String; - public fun (Lio/sentry/IHub;Ljava/util/Set;Z)V - public synthetic fun (Lio/sentry/IHub;Ljava/util/Set;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;ZZ)V + public fun (Lio/sentry/IScopes;Ljava/util/Set;Z)V + public synthetic fun (Lio/sentry/IScopes;Ljava/util/Set;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;ZZ)V public fun (ZZ)V public synthetic fun (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getEnableAutoFragmentLifecycleTracing ()Z diff --git a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt index 4129ea4356..d2fd393200 100644 --- a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt +++ b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt @@ -5,7 +5,7 @@ import android.app.Application import android.app.Application.ActivityLifecycleCallbacks import android.os.Bundle import androidx.fragment.app.FragmentActivity -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Integration import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel.DEBUG @@ -40,11 +40,11 @@ class FragmentLifecycleIntegration( enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) - private lateinit var hub: IHub + private lateinit var scopes: IScopes private lateinit var options: SentryOptions - override fun register(hub: IHub, options: SentryOptions) { - this.hub = hub + override fun register(scopes: IScopes, options: SentryOptions) { + this.scopes = scopes this.options = options application.registerActivityLifecycleCallbacks(this) @@ -66,7 +66,7 @@ class FragmentLifecycleIntegration( ?.supportFragmentManager ?.registerFragmentLifecycleCallbacks( SentryFragmentLifecycleCallbacks( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = filterFragmentLifecycleBreadcrumbs, enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ), diff --git a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt index 78f45474e2..64ecb21d41 100644 --- a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt +++ b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt @@ -8,9 +8,9 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryLevel.INFO import io.sentry.SpanStatus import io.sentry.TypeCheckHint.ANDROID_FRAGMENT @@ -20,17 +20,17 @@ private const val TRACE_ORIGIN = "auto.ui.fragment" @Suppress("TooManyFunctions") class SentryFragmentLifecycleCallbacks( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), val filterFragmentLifecycleBreadcrumbs: Set, val enableAutoFragmentLifecycleTracing: Boolean ) : FragmentLifecycleCallbacks() { constructor( - hub: IHub, + scopes: IScopes, enableFragmentLifecycleBreadcrumbs: Boolean, enableAutoFragmentLifecycleTracing: Boolean ) : this( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = FragmentLifecycleState.values().toSet() .takeIf { enableFragmentLifecycleBreadcrumbs } .orEmpty(), @@ -41,14 +41,14 @@ class SentryFragmentLifecycleCallbacks( enableFragmentLifecycleBreadcrumbs: Boolean = true, enableAutoFragmentLifecycleTracing: Boolean = false ) : this( - hub = HubAdapter.getInstance(), + scopes = ScopesAdapter.getInstance(), filterFragmentLifecycleBreadcrumbs = FragmentLifecycleState.values().toSet() .takeIf { enableFragmentLifecycleBreadcrumbs } .orEmpty(), enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) - private val isPerformanceEnabled get() = hub.options.isTracingEnabled && enableAutoFragmentLifecycleTracing + private val isPerformanceEnabled get() = scopes.options.isTracingEnabled && enableAutoFragmentLifecycleTracing private val fragmentsWithOngoingTransactions = WeakHashMap() @@ -141,7 +141,7 @@ class SentryFragmentLifecycleCallbacks( val hint = Hint() .also { it.set(ANDROID_FRAGMENT, fragment) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun getFragmentName(fragment: Fragment): String { @@ -157,7 +157,7 @@ class SentryFragmentLifecycleCallbacks( } var transaction: ISpan? = null - hub.configureScope { + scopes.configureScope { transaction = it.transaction } diff --git a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt index 032aef58e1..84286503b9 100644 --- a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt +++ b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt @@ -4,7 +4,7 @@ import android.app.Activity import android.app.Application import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentManager -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import org.mockito.kotlin.check import org.mockito.kotlin.doReturn @@ -24,14 +24,14 @@ class FragmentLifecycleIntegrationTest { val fragmentActivity = mock { on { supportFragmentManager } doReturn fragmentManager } - val hub = mock() + val scopes = mock() val options = SentryOptions() fun getSut( enableFragmentLifecycleBreadcrumbs: Boolean = true, enableAutoFragmentLifecycleTracing: Boolean = false ): FragmentLifecycleIntegration { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return FragmentLifecycleIntegration( application = application, enableFragmentLifecycleBreadcrumbs = enableFragmentLifecycleBreadcrumbs, @@ -46,7 +46,7 @@ class FragmentLifecycleIntegrationTest { fun `When register, it should register activity lifecycle callbacks`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application).registerActivityLifecycleCallbacks(sut) } @@ -55,7 +55,7 @@ class FragmentLifecycleIntegrationTest { fun `When close, it should unregister lifecycle callbacks`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.application).unregisterActivityLifecycleCallbacks(sut) @@ -69,7 +69,7 @@ class FragmentLifecycleIntegrationTest { on { supportFragmentManager } doReturn fragmentManager } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(fragmentActivity, savedInstanceState = null) verify(fragmentManager).registerFragmentLifecycleCallbacks( @@ -84,7 +84,7 @@ class FragmentLifecycleIntegrationTest { fun `When FragmentActivity is created, it should register fragment lifecycle callbacks with passed config`() { val sut = fixture.getSut(enableFragmentLifecycleBreadcrumbs = false, enableAutoFragmentLifecycleTracing = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(fixture.fragmentActivity, savedInstanceState = null) verify(fixture.fragmentManager).registerFragmentLifecycleCallbacks( @@ -102,7 +102,7 @@ class FragmentLifecycleIntegrationTest { val sut = fixture.getSut() val activity = mock() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, savedInstanceState = null) } diff --git a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt index 9b7fd5c5f2..26cb5b211a 100644 --- a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt +++ b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt @@ -5,8 +5,8 @@ import android.os.Bundle import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import io.sentry.Breadcrumb -import io.sentry.Hub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.ScopeCallback @@ -32,7 +32,7 @@ class SentryFragmentLifecycleCallbacksTest { private class Fixture { val fragmentManager = mock() - val hub = mock() + val scopes = mock() val fragment = mock() val context = mock() val scope = mock() @@ -45,7 +45,7 @@ class SentryFragmentLifecycleCallbacksTest { tracesSampleRate: Double? = 1.0, isAdded: Boolean = true ): SentryFragmentLifecycleCallbacks { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { setTracesSampleRate(tracesSampleRate) } @@ -55,12 +55,12 @@ class SentryFragmentLifecycleCallbacksTest { ) whenever(transaction.startChild(any(), any())).thenReturn(span) whenever(scope.transaction).thenReturn(transaction) - whenever(hub.configureScope(any())).thenAnswer { + whenever(scopes.configureScope(any())).thenAnswer { (it.arguments[0] as ScopeCallback).run(scope) } whenever(fragment.isAdded).thenReturn(isAdded) return SentryFragmentLifecycleCallbacks( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = loggedFragmentLifecycleStates, enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) @@ -272,7 +272,7 @@ class SentryFragmentLifecycleCallbacksTest { } private fun verifyBreadcrumbAdded(expectedState: String) { - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { breadcrumb: Breadcrumb -> assertEquals("ui.fragment.lifecycle", breadcrumb.category) assertEquals("navigation", breadcrumb.type) @@ -285,6 +285,6 @@ class SentryFragmentLifecycleCallbacksTest { } private fun verifyBreadcrumbAddedCount(count: Int) { - verify(fixture.hub, times(count)).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, times(count)).addBreadcrumb(any(), anyOrNull()) } } diff --git a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt index e956978086..f685e84874 100644 --- a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt +++ b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt @@ -93,7 +93,7 @@ class RelayAsserter( /** Request parsed as envelope. */ val envelope: SentryEnvelope? by lazy { try { - EnvelopeReader(Sentry.getCurrentHub().options.serializer) + EnvelopeReader(Sentry.getCurrentScopes().options.serializer) .read(GZIPInputStream(request.body.inputStream())) } catch (e: IOException) { null diff --git a/sentry-android-navigation/api/sentry-android-navigation.api b/sentry-android-navigation/api/sentry-android-navigation.api index 79151bb3fb..03a46d8b87 100644 --- a/sentry-android-navigation/api/sentry-android-navigation.api +++ b/sentry-android-navigation/api/sentry-android-navigation.api @@ -10,11 +10,11 @@ public final class io/sentry/android/navigation/SentryNavigationListener : andro public static final field Companion Lio/sentry/android/navigation/SentryNavigationListener$Companion; public static final field NAVIGATION_OP Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Z)V - public fun (Lio/sentry/IHub;ZZ)V - public fun (Lio/sentry/IHub;ZZLjava/lang/String;)V - public synthetic fun (Lio/sentry/IHub;ZZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Z)V + public fun (Lio/sentry/IScopes;ZZ)V + public fun (Lio/sentry/IScopes;ZZLjava/lang/String;)V + public synthetic fun (Lio/sentry/IScopes;ZZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun onDestinationChanged (Landroidx/navigation/NavController;Landroidx/navigation/NavDestination;Landroid/os/Bundle;)V } diff --git a/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt b/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt index 8fdf8b0df8..bb06d66b3c 100644 --- a/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt +++ b/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt @@ -6,9 +6,9 @@ import androidx.navigation.NavController import androidx.navigation.NavDestination import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel.DEBUG import io.sentry.SentryLevel.INFO @@ -34,7 +34,7 @@ private const val TRACE_ORIGIN = "auto.navigation" * with [SentryOptions.idleTimeout] for navigation events. */ class SentryNavigationListener @JvmOverloads constructor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val enableNavigationBreadcrumbs: Boolean = true, private val enableNavigationTracing: Boolean = true, private val traceOriginAppendix: String? = null @@ -43,7 +43,7 @@ class SentryNavigationListener @JvmOverloads constructor( private var previousDestinationRef: WeakReference? = null private var previousArgs: Bundle? = null - private val isPerformanceEnabled get() = hub.options.isTracingEnabled && enableNavigationTracing + private val isPerformanceEnabled get() = scopes.options.isTracingEnabled && enableNavigationTracing private var activeTransaction: ITransaction? = null @@ -91,7 +91,7 @@ class SentryNavigationListener @JvmOverloads constructor( } val hint = Hint() hint.set(TypeCheckHint.ANDROID_NAV_DESTINATION, destination) - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun startTracing( @@ -100,7 +100,7 @@ class SentryNavigationListener @JvmOverloads constructor( arguments: Map ) { if (!isPerformanceEnabled) { - TracingUtils.startNewTrace(hub) + TracingUtils.startNewTrace(scopes) return } @@ -111,7 +111,7 @@ class SentryNavigationListener @JvmOverloads constructor( if (destination.navigatorName == "activity") { // we do not trace navigation between activities to avoid clashing with activity lifecycle tracing - hub.options.logger.log( + scopes.options.logger.log( DEBUG, "Navigating to activity destination, no transaction captured." ) @@ -122,7 +122,7 @@ class SentryNavigationListener @JvmOverloads constructor( var name = destination.route ?: try { controller.context.resources.getResourceEntryName(destination.id) } catch (e: NotFoundException) { - hub.options.logger.log( + scopes.options.logger.log( DEBUG, "Destination id cannot be retrieved from Resources, no transaction captured." ) @@ -134,12 +134,12 @@ class SentryNavigationListener @JvmOverloads constructor( val transactionOptions = TransactionOptions().also { it.isWaitForChildren = true - it.idleTimeout = hub.options.idleTimeout + it.idleTimeout = scopes.options.idleTimeout it.deadlineTimeout = TransactionOptions.DEFAULT_DEADLINE_TIMEOUT_AUTO_TRANSACTION it.isTrimEnd = true } - val transaction = hub.startTransaction( + val transaction = scopes.startTransaction( TransactionContext(name, TransactionNameSource.ROUTE, NAVIGATION_OP), transactionOptions ) @@ -151,7 +151,7 @@ class SentryNavigationListener @JvmOverloads constructor( if (arguments.isNotEmpty()) { transaction.setData("arguments", arguments) } - hub.configureScope { scope -> + scopes.configureScope { scope -> scope.withTransaction { tx -> if (tx == null) { scope.transaction = transaction @@ -166,7 +166,7 @@ class SentryNavigationListener @JvmOverloads constructor( activeTransaction?.finish(status) // clear transaction from scope so others can bind to it - hub.configureScope { scope -> + scopes.configureScope { scope -> scope.withTransaction { tx -> if (tx == activeTransaction) { scope.clearTransaction() diff --git a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt index 76c57159c3..7308b6b659 100644 --- a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt +++ b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt @@ -7,8 +7,8 @@ import androidx.navigation.NavController import androidx.navigation.NavDestination import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Scope.IWithTransaction import io.sentry.ScopeCallback @@ -39,7 +39,7 @@ import kotlin.test.assertNull class SentryNavigationListenerTest { class Fixture { - val hub = mock() + val scopes = mock() val destination = mock() val navController = mock() @@ -67,20 +67,20 @@ class SentryNavigationListenerTest { tracesSampleRate ) } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) this.transaction = transaction ?: SentryTracer( TransactionContext( "/$toRoute", SentryNavigationListener.NAVIGATION_OP ), - hub + scopes ) - whenever(hub.startTransaction(any(), any())) + whenever(scopes.startTransaction(any(), any())) .thenReturn(this.transaction) - whenever(hub.configureScope(any())).thenAnswer { + whenever(scopes.configureScope(any())).thenAnswer { (it.arguments[0] as ScopeCallback).run(scope) } @@ -96,7 +96,7 @@ class SentryNavigationListenerTest { whenever(navController.context).thenReturn(context) whenever(destination.route).thenReturn(toRoute) return SentryNavigationListener( - hub, + scopes, enableBreadcrumbs, enableTracing, traceOriginAppendix @@ -112,7 +112,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("navigation", it.type) assertEquals("navigation", it.category) @@ -133,7 +133,7 @@ class SentryNavigationListenerTest { bundleOf("arg1" to "foo", "arg2" to "bar") ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("/route", it.data["to"]) assertEquals(mapOf("arg1" to "foo", "arg2" to "bar"), it.data["to_arguments"]) @@ -152,7 +152,7 @@ class SentryNavigationListenerTest { bundleOf() ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("/route", it.data["to"]) assertNull(it.data["to_arguments"]) @@ -180,7 +180,7 @@ class SentryNavigationListenerTest { bundleOf("to_arg1" to "to_foo") ) val captor = argumentCaptor() - verify(fixture.hub, times(2)).addBreadcrumb(captor.capture(), any()) + verify(fixture.scopes, times(2)).addBreadcrumb(captor.capture(), any()) captor.secondValue.let { assertEquals("/route_from", it.data["from"]) assertEquals(mapOf("from_arg1" to "from_foo"), it.data["from_arguments"]) @@ -196,7 +196,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -205,7 +205,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -217,7 +217,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -230,7 +230,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -242,7 +242,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -254,7 +254,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/route", it.name) assertEquals(SentryNavigationListener.NAVIGATION_OP, it.operation) @@ -266,13 +266,13 @@ class SentryNavigationListenerTest { @Test fun `onDestinationChanged strips out route parameters from transaction name`() { - val sut = fixture.getSut(toRoute = "github/{user_id}?per_page={per_page}") + val sut = fixture.getSut(toRoute = "gitscopes/{user_id}?per_page={per_page}") sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { - assertEquals("/github", it.name) + assertEquals("/gitscopes", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) }, any() @@ -285,7 +285,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/destination-id-1", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) @@ -296,7 +296,7 @@ class SentryNavigationListenerTest { @Test fun `onDestinationChanged captures arguments as additional data for transaction`() { - val sut = fixture.getSut(toRoute = "github/{user_id}?per_page={per_page}") + val sut = fixture.getSut(toRoute = "gitscopes/{user_id}?per_page={per_page}") sut.onDestinationChanged( fixture.navController, @@ -304,9 +304,9 @@ class SentryNavigationListenerTest { bundleOf("user_id" to 123, "per_page" to 10) ) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { - assertEquals("/github", it.name) + assertEquals("/gitscopes", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) }, any() @@ -365,13 +365,13 @@ class SentryNavigationListenerTest { ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = Scope(fixture.options) val propagationContextAtStart = scope.propagationContext - whenever(fixture.hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(fixture.scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).configureScope(any()) + verify(fixture.scopes).configureScope(any()) assertNotSame(propagationContextAtStart, scope.propagationContext) } @@ -399,7 +399,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { options -> assertEquals(TransactionOptions.DEFAULT_DEADLINE_TIMEOUT_AUTO_TRANSACTION, options.deadlineTimeout) diff --git a/sentry-android-okhttp/api/sentry-android-okhttp.api b/sentry-android-okhttp/api/sentry-android-okhttp.api index a1ad9114a2..35f42842dd 100644 --- a/sentry-android-okhttp/api/sentry-android-okhttp.api +++ b/sentry-android-okhttp/api/sentry-android-okhttp.api @@ -8,12 +8,12 @@ public final class io/sentry/android/okhttp/BuildConfig { public final class io/sentry/android/okhttp/SentryOkHttpEventListener : okhttp3/EventListener { public fun ()V - public fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V - public synthetic fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V + public synthetic fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lokhttp3/EventListener$Factory;)V public fun (Lokhttp3/EventListener;)V public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V @@ -49,9 +49,9 @@ public final class io/sentry/android/okhttp/SentryOkHttpEventListener : okhttp3/ public final class io/sentry/android/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response; } diff --git a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt index 7ca5313d8f..f99106e8d9 100644 --- a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt +++ b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt @@ -1,7 +1,7 @@ package io.sentry.android.okhttp -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import okhttp3.Call import okhttp3.Connection import okhttp3.EventListener @@ -42,35 +42,35 @@ import java.net.Proxy ) @Suppress("TooManyFunctions") class SentryOkHttpEventListener( - hub: IHub = HubAdapter.getInstance(), + scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerCreator: ((call: Call) -> EventListener)? = null ) : EventListener() { constructor() : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = null ) constructor(originalEventListener: EventListener) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListener } ) constructor(originalEventListenerFactory: Factory) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this( - hub, + constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListener: EventListener) : this( + scopes, originalEventListenerCreator = { originalEventListener } ) - constructor(hub: IHub = HubAdapter.getInstance(), originalEventListenerFactory: Factory) : this( - hub, + constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerFactory: Factory) : this( + scopes, originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - private val delegate = io.sentry.okhttp.SentryOkHttpEventListener(hub, originalEventListenerCreator) + private val delegate = io.sentry.okhttp.SentryOkHttpEventListener(scopes, originalEventListenerCreator) override fun cacheConditionalHit(call: Call, cachedResponse: Response) { delegate.cacheConditionalHit(call, cachedResponse) diff --git a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt index 971af925ff..3c7e590fe1 100644 --- a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt @@ -1,9 +1,9 @@ package io.sentry.android.okhttp import io.sentry.HttpStatusCodeRange -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.android.okhttp.SentryOkHttpInterceptor.BeforeSpanCallback @@ -17,7 +17,7 @@ import okhttp3.Response * out of the active span bound to the scope for each HTTP Request. * If [captureFailedRequests] is enabled, the SDK will capture HTTP Client errors as well. * - * @param hub The [IHub], internal and only used for testing. + * @param scopes The [IScopes], internal and only used for testing. * @param beforeSpan The [ISpan] can be customized or dropped with the [BeforeSpanCallback]. * @param captureFailedRequests The SDK will only capture HTTP Client errors if it is enabled, * Defaults to false. @@ -31,7 +31,7 @@ import okhttp3.Response ReplaceWith("SentryOkHttpInterceptor", "io.sentry.okhttp.SentryOkHttpInterceptor") ) class SentryOkHttpInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = true, private val failedRequestStatusCodes: List = listOf( @@ -39,7 +39,7 @@ class SentryOkHttpInterceptor( ), private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) ) : Interceptor by io.sentry.okhttp.SentryOkHttpInterceptor( - hub, + scopes, { span, request, response -> beforeSpan ?: return@SentryOkHttpInterceptor span beforeSpan.execute(span, request, response) @@ -49,9 +49,9 @@ class SentryOkHttpInterceptor( failedRequestTargets ) { - constructor() : this(HubAdapter.getInstance()) - constructor(hub: IHub) : this(hub, null) - constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + constructor() : this(ScopesAdapter.getInstance()) + constructor(scopes: IScopes) : this(scopes, null) + constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) diff --git a/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt b/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt index 3bfa855d53..2e39bf76ec 100644 --- a/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt +++ b/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt @@ -1,8 +1,8 @@ package io.sentry.android.sqlite import android.database.SQLException -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryStackTraceFactory import io.sentry.SpanDataConvention @@ -11,10 +11,10 @@ import io.sentry.SpanStatus private const val TRACE_ORIGIN = "auto.db.sqlite" internal class SQLiteSpanManager( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val databaseName: String? = null ) { - private val stackTraceFactory = SentryStackTraceFactory(hub.options) + private val stackTraceFactory = SentryStackTraceFactory(scopes.options) init { SentryIntegrationPackageStorage.getInstance().addIntegration("SQLite") @@ -30,7 +30,7 @@ internal class SQLiteSpanManager( @Suppress("TooGenericExceptionCaught") @Throws(SQLException::class) fun performSql(sql: String, operation: () -> T): T { - val span = hub.span?.startChild("db.sql.query", sql) + val span = scopes.span?.startChild("db.sql.query", sql) span?.spanContext?.origin = TRACE_ORIGIN return try { val result = operation() @@ -42,7 +42,7 @@ internal class SQLiteSpanManager( throw e } finally { span?.apply { - val isMainThread: Boolean = hub.options.mainThreadChecker.isMainThread + val isMainThread: Boolean = scopes.options.mainThreadChecker.isMainThread setData(SpanDataConvention.BLOCKED_MAIN_THREAD_KEY, isMainThread) if (isMainThread) { setData(SpanDataConvention.CALL_STACK_KEY, stackTraceFactory.inAppCallStack) diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt index e2fa0c2e4d..9265cd260a 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.sqlite import android.database.SQLException -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -22,7 +22,7 @@ import kotlin.test.assertTrue class SQLiteSpanManagerTest { private class Fixture { - private val hub = mock() + private val scopes = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -30,13 +30,13 @@ class SQLiteSpanManagerTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SQLiteSpanManager(hub, databaseName) + return SQLiteSpanManager(scopes, databaseName) } } diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt index cf22c3b0ec..99e1d5f4a0 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.sqlite import android.database.Cursor import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteQuery -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -23,8 +23,8 @@ import kotlin.test.assertTrue class SentrySupportSQLiteDatabaseTest { private class Fixture { - private val hub = mock() - private val spanManager = SQLiteSpanManager(hub) + private val scopes = mock() + private val spanManager = SQLiteSpanManager(scopes) val mockDatabase = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -37,11 +37,11 @@ class SentrySupportSQLiteDatabaseTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return SentrySupportSQLiteDatabase(mockDatabase, spanManager) diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt index 9078ba8b08..4b6292bd27 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.sqlite import androidx.sqlite.db.SupportSQLiteStatement -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -18,8 +18,8 @@ import kotlin.test.assertTrue class SentrySupportSQLiteStatementTest { private class Fixture { - private val hub = mock() - private val spanManager = SQLiteSpanManager(hub) + private val scopes = mock() + private val spanManager = SQLiteSpanManager(scopes) val mockStatement = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -28,11 +28,11 @@ class SentrySupportSQLiteStatementTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return SentrySupportSQLiteStatement(mockStatement, spanManager, sql) } diff --git a/sentry-android-timber/api/sentry-android-timber.api b/sentry-android-timber/api/sentry-android-timber.api index 808e91bf10..2d71f67570 100644 --- a/sentry-android-timber/api/sentry-android-timber.api +++ b/sentry-android-timber/api/sentry-android-timber.api @@ -14,11 +14,11 @@ public final class io/sentry/android/timber/SentryTimberIntegration : io/sentry/ public fun close ()V public final fun getMinBreadcrumbLevel ()Lio/sentry/SentryLevel; public final fun getMinEventLevel ()Lio/sentry/SentryLevel; - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/timber/SentryTimberTree : timber/log/Timber$Tree { - public fun (Lio/sentry/IHub;Lio/sentry/SentryLevel;Lio/sentry/SentryLevel;)V + public fun (Lio/sentry/IScopes;Lio/sentry/SentryLevel;Lio/sentry/SentryLevel;)V public fun d (Ljava/lang/String;[Ljava/lang/Object;)V public fun d (Ljava/lang/Throwable;)V public fun d (Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/Object;)V diff --git a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt index d043faa5f6..334146a218 100644 --- a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt +++ b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt @@ -1,7 +1,7 @@ package io.sentry.android.timber -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.Integration import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel @@ -21,10 +21,10 @@ class SentryTimberIntegration( private lateinit var tree: SentryTimberTree private lateinit var logger: ILogger - override fun register(hub: IHub, options: SentryOptions) { + override fun register(scopes: IScopes, options: SentryOptions) { logger = options.logger - tree = SentryTimberTree(hub, minEventLevel, minBreadcrumbLevel) + tree = SentryTimberTree(scopes, minEventLevel, minBreadcrumbLevel) Timber.plant(tree) logger.log(SentryLevel.DEBUG, "SentryTimberIntegration installed.") diff --git a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt index f3a0f599a9..dddab75133 100644 --- a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt +++ b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt @@ -2,7 +2,7 @@ package io.sentry.android.timber import android.util.Log import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.protocol.Message @@ -13,7 +13,7 @@ import timber.log.Timber */ @Suppress("TooManyFunctions") // we have to override all methods to be able to tweak logging class SentryTimberTree( - private val hub: IHub, + private val scopes: IScopes, private val minEventLevel: SentryLevel, private val minBreadcrumbLevel: SentryLevel ) : Timber.Tree() { @@ -269,7 +269,7 @@ class SentryTimberTree( logger = "Timber" } - hub.captureEvent(sentryEvent) + scopes.captureEvent(sentryEvent) } } @@ -296,7 +296,7 @@ class SentryTimberTree( else -> null } - breadCrumb?.let { hub.addBreadcrumb(it) } + breadCrumb?.let { scopes.addBreadcrumb(it) } } } diff --git a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt index a57853e059..8bb85aa085 100644 --- a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt +++ b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.android.timber -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.protocol.SdkVersion @@ -16,7 +16,7 @@ import kotlin.test.assertTrue class SentryTimberIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() val options = SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") } @@ -41,7 +41,7 @@ class SentryTimberIntegrationTest { @Test fun `Integrations plants a tree into Timber on register`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(1, Timber.treeCount()) @@ -53,16 +53,16 @@ class SentryTimberIntegrationTest { @Test fun `Integrations plants the SentryTimberTree tree`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) Timber.e(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Integrations removes a tree from Timber on close integration`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(1, Timber.treeCount()) @@ -84,7 +84,7 @@ class SentryTimberIntegrationTest { minEventLevel = SentryLevel.INFO, minBreadcrumbLevel = SentryLevel.DEBUG ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(sut.minEventLevel, SentryLevel.INFO) assertEquals(sut.minBreadcrumbLevel, SentryLevel.DEBUG) @@ -93,7 +93,7 @@ class SentryTimberIntegrationTest { @Test fun `Integration adds itself to the package list`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.packageSet.any { @@ -106,7 +106,7 @@ class SentryTimberIntegrationTest { @Test fun `Integration adds itself to the integration list`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.integrationSet.contains("Timber") diff --git a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt index 3d82b139ec..2ab7ff64db 100644 --- a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt +++ b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.timber import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.getExc import org.mockito.kotlin.any @@ -19,13 +19,13 @@ import kotlin.test.assertNull class SentryTimberTreeTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut( minEventLevel: SentryLevel = SentryLevel.ERROR, minBreadcrumbLevel: SentryLevel = SentryLevel.INFO ): SentryTimberTree { - return SentryTimberTree(hub, minEventLevel, minBreadcrumbLevel) + return SentryTimberTree(scopes, minEventLevel, minBreadcrumbLevel) } } @@ -40,28 +40,28 @@ class SentryTimberTreeTest { fun `Tree captures an event if min level is equal`() { val sut = fixture.getSut() sut.e(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Tree captures an event if min level is higher`() { val sut = fixture.getSut() sut.wtf(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Tree won't capture an event if min level is lower`() { val sut = fixture.getSut() sut.d(Throwable()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) } @Test fun `Tree captures debug level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.d(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.DEBUG, it.level) } @@ -72,7 +72,7 @@ class SentryTimberTreeTest { fun `Tree captures info level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.i(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.INFO, it.level) } @@ -83,7 +83,7 @@ class SentryTimberTreeTest { fun `Tree captures warning level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.w(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.WARNING, it.level) } @@ -94,7 +94,7 @@ class SentryTimberTreeTest { fun `Tree captures error level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.e(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.ERROR, it.level) } @@ -105,7 +105,7 @@ class SentryTimberTreeTest { fun `Tree captures fatal level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.wtf(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.FATAL, it.level) } @@ -116,7 +116,7 @@ class SentryTimberTreeTest { fun `Tree captures unknown as debug level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.log(15, Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.DEBUG, it.level) } @@ -128,7 +128,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() val throwable = Throwable() sut.e(throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(throwable, it.getExc()) } @@ -139,7 +139,7 @@ class SentryTimberTreeTest { fun `Tree captures an event without an exception`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNull(it.getExc()) } @@ -150,7 +150,7 @@ class SentryTimberTreeTest { fun `Tree captures an event and sets Timber as a logger`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals("Timber", it.logger) } @@ -164,7 +164,7 @@ class SentryTimberTreeTest { // only available thru static class Timber.tag("tag") Timber.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals("tag", it.getTag("TimberTag")) } @@ -176,7 +176,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() Timber.plant(sut) Timber.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNull(it.getTag("TimberTag")) } @@ -187,7 +187,7 @@ class SentryTimberTreeTest { fun `Tree captures an event with given message`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNotNull(it.message) { message -> assertEquals("message", message.message) @@ -200,7 +200,7 @@ class SentryTimberTreeTest { fun `Tree captures an event with formatted message and arguments, when provided`() { val sut = fixture.getSut() sut.e("test count: %d", 32) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNotNull(it.message) { message -> assertEquals("test count: %d", message.message) @@ -216,7 +216,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() sut.e("test count: %d", 32) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("test count: 32", it.message) } @@ -227,28 +227,28 @@ class SentryTimberTreeTest { fun `Tree adds a breadcrumb if min level is equal`() { val sut = fixture.getSut() sut.i(Throwable("test")) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) } @Test fun `Tree adds a breadcrumb if min level is higher`() { val sut = fixture.getSut() sut.e(Throwable("test")) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) } @Test fun `Tree won't add a breadcrumb if min level is lower`() { val sut = fixture.getSut(minBreadcrumbLevel = SentryLevel.ERROR) sut.i(Throwable("test")) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `Tree adds an info breadcrumb`() { val sut = fixture.getSut() sut.i("message") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("Timber", it.category) assertEquals(SentryLevel.INFO, it.level) @@ -261,7 +261,7 @@ class SentryTimberTreeTest { fun `Tree adds an error breadcrumb`() { val sut = fixture.getSut() sut.e(Throwable("test")) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("exception", it.category) assertEquals(SentryLevel.ERROR, it.level) @@ -274,7 +274,7 @@ class SentryTimberTreeTest { fun `Tree does not add a breadcrumb, if no message provided`() { val sut = fixture.getSut() sut.e(Throwable()) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test diff --git a/sentry-apollo-3/api/sentry-apollo-3.api b/sentry-apollo-3/api/sentry-apollo-3.api index 1c80e1950b..e106585156 100644 --- a/sentry-apollo-3/api/sentry-apollo-3.api +++ b/sentry-apollo-3/api/sentry-apollo-3.api @@ -17,11 +17,11 @@ public final class io/sentry/apollo3/SentryApollo3HttpInterceptor : com/apollogr public static final field SENTRY_APOLLO_3_OPERATION_TYPE Ljava/lang/String; public static final field SENTRY_APOLLO_3_VARIABLES Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;Z)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;Z)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun dispose ()V public fun intercept (Lcom/apollographql/apollo3/api/http/HttpRequest;Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } @@ -40,12 +40,12 @@ public final class io/sentry/apollo3/SentryApollo3Interceptor : com/apollographq public final class io/sentry/apollo3/SentryApolloBuilderExtensionsKt { public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; } diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt index 08cab179a5..10e56ce7be 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt @@ -11,9 +11,9 @@ import com.apollographql.apollo3.network.http.HttpInterceptorChain import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryEvent import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel @@ -41,7 +41,7 @@ import java.util.Locale private const val TRACE_ORIGIN = "auto.graphql.apollo3" class SentryApollo3HttpInterceptor @JvmOverloads constructor( - @ApiStatus.Internal private val hub: IHub = HubAdapter.getInstance(), + @ApiStatus.Internal private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = DEFAULT_CAPTURE_FAILED_REQUESTS, private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) @@ -65,7 +65,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( request: HttpRequest, chain: HttpInterceptorChain ): HttpResponse { - val activeSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val activeSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span val operationName = getHeader(HEADER_APOLLO_OPERATION_NAME, request.headers) val operationType = decodeHeaderValue(request, SENTRY_APOLLO_3_OPERATION_TYPE) @@ -77,7 +77,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( span = startChild(request, activeSpan, operationName, operationType, operationId) } - val modifiedRequest = maybeAddTracingHeaders(hub, request, span) + val modifiedRequest = maybeAddTracingHeaders(scopes, request, span) var httpResponse: HttpResponse? = null var statusCode: Int? = null @@ -91,7 +91,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( return httpResponse } catch (e: Throwable) { - // https://github.com/apollographql/apollo-kotlin/issues/4711 will change error handling in v4 + // https://gitscopes.com/apollographql/apollo-kotlin/issues/4711 will change error handling in v4 when (e) { is ApolloHttpException -> { statusCode = e.statusCode @@ -117,10 +117,10 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( } } - private fun maybeAddTracingHeaders(hub: IHub, request: HttpRequest, span: ISpan?): HttpRequest { + private fun maybeAddTracingHeaders(scopes: IScopes, request: HttpRequest, span: ISpan?): HttpRequest { var cleanedHeaders = removeSentryInternalHeaders(request.headers).toMutableList() - TracingUtils.traceIfAllowed(hub, request.url, request.headers.filter { it.name == BaggageHeader.BAGGAGE_HEADER }.map { it.value }, span)?.let { + TracingUtils.traceIfAllowed(scopes, request.url, request.headers.filter { it.name == BaggageHeader.BAGGAGE_HEADER }.map { it.value }, span)?.let { cleanedHeaders.add(HttpHeader(it.sentryTraceHeader.name, it.sentryTraceHeader.value)) it.baggageHeader?.let { baggageHeader -> cleanedHeaders = cleanedHeaders.filterNot { it.name == BaggageHeader.BAGGAGE_HEADER }.toMutableList().apply { @@ -179,7 +179,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( try { String(Base64.decode(it, Base64.NO_WRAP)) } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error decoding internal apolloHeader $headerName", e @@ -218,7 +218,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( span.spanContext.sampled = false } } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e @@ -256,7 +256,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( hint.set(APOLLO_RESPONSE, httpResponse) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } // Extensions @@ -273,7 +273,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( private fun getHeaders(headers: List): MutableMap? { // Headers are only sent if isSendDefaultPii is enabled due to PII - if (!hub.options.isSendDefaultPii) { + if (!scopes.options.isSendDefaultPii) { return null } @@ -311,7 +311,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( val body = try { response.body?.peek()?.readUtf8() ?: "" } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error reading the response body.", e @@ -368,7 +368,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( urlDetails.applyToRequest(this) // Cookie is only sent if isSendDefaultPii is enabled cookies = - if (hub.options.isSendDefaultPii) getHeader("Cookie", request.headers) else null + if (scopes.options.isSendDefaultPii) getHeader("Cookie", request.headers) else null method = request.method.name headers = getHeaders(request.headers) apiTarget = "graphql" @@ -382,7 +382,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( it.writeTo(buffer) data = buffer.readUtf8() } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error reading the request body.", e @@ -396,7 +396,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( val sentryResponse = Response().apply { // Set-Cookie is only sent if isSendDefaultPii is enabled due to PII - cookies = if (hub.options.isSendDefaultPii) { + cookies = if (scopes.options.isSendDefaultPii) { getHeader( "Set-Cookie", response.headers @@ -419,9 +419,9 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( event.contexts.setResponse(sentryResponse) event.fingerprints = fingerprints - hub.captureEvent(event, hint) + scopes.captureEvent(event, hint) } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error capturing the GraphQL error.", e diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt index 2cdbc148fb..b40b1c183d 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt @@ -1,14 +1,14 @@ package io.sentry.apollo3 import com.apollographql.apollo3.ApolloClient -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.apollo3.SentryApollo3HttpInterceptor.Companion.DEFAULT_CAPTURE_FAILED_REQUESTS @JvmOverloads fun ApolloClient.Builder.sentryTracing( - hub: IHub = HubAdapter.getInstance(), + scopes: IScopes = ScopesAdapter.getInstance(), captureFailedRequests: Boolean = DEFAULT_CAPTURE_FAILED_REQUESTS, failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS), beforeSpan: SentryApollo3HttpInterceptor.BeforeSpanCallback? = null @@ -16,7 +16,7 @@ fun ApolloClient.Builder.sentryTracing( addInterceptor(SentryApollo3Interceptor()) addHttpInterceptor( SentryApollo3HttpInterceptor( - hub = hub, + scopes = scopes, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, beforeSpan = beforeSpan @@ -31,7 +31,7 @@ fun ApolloClient.Builder.sentryTracing( beforeSpan: SentryApollo3HttpInterceptor.BeforeSpanCallback? = null ): ApolloClient.Builder { return sentryTracing( - hub = HubAdapter.getInstance(), + scopes = ScopesAdapter.getInstance(), captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, beforeSpan = beforeSpan diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt index 40406b77b5..b3f8b6d57e 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt @@ -5,7 +5,7 @@ import com.apollographql.apollo3.api.http.HttpRequest import com.apollographql.apollo3.api.http.HttpResponse import com.apollographql.apollo3.exception.ApolloException import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS @@ -35,7 +35,7 @@ import kotlin.test.assertTrue class SentryApollo3InterceptorClientErrors { class Fixture { val server = MockWebServer() - lateinit var hub: IHub + lateinit var scopes: IScopes private val responseBodyOk = """{ @@ -75,7 +75,7 @@ class SentryApollo3InterceptorClientErrors { ): ApolloClient { SentryIntegrationPackageStorage.getInstance().clearStorage() - hub = mock().apply { + scopes = mock().apply { whenever(options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" @@ -84,7 +84,7 @@ class SentryApollo3InterceptorClientErrors { } ) } - whenever(hub.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) + whenever(scopes.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) val response = MockResponse() .setBody(responseBody) @@ -102,7 +102,7 @@ class SentryApollo3InterceptorClientErrors { val builder = ApolloClient.Builder() .serverUrl(server.url("?myQuery=query#myFragment").toString()) .sentryTracing( - hub = hub, + scopes = scopes, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets ) @@ -123,7 +123,7 @@ class SentryApollo3InterceptorClientErrors { val sut = fixture.getSut(captureFailedRequests = false, responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -132,7 +132,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } // endregion @@ -165,7 +165,7 @@ class SentryApollo3InterceptorClientErrors { ) executeQuery(sut) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -174,7 +174,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } // endregion @@ -187,7 +187,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertEquals("SentryApollo3Interceptor", throwable.exceptionMechanism.type) @@ -202,7 +202,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertEquals("GraphQL Request failed, name: LaunchDetails, type: query", throwable.throwable.message) @@ -217,7 +217,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertTrue(throwable.isSnapshot) @@ -238,7 +238,7 @@ class SentryApollo3InterceptorClientErrors { {"operationName":"LaunchDetails","variables":{"id":"83"},"query":"query LaunchDetails($escapeDolar: ID!) { launch(id: $escapeDolar) { id site mission { name missionPatch(size: LARGE) } rocket { name type } } }"} """.trimIndent() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val request = it.request!! @@ -262,7 +262,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val request = it.request!! @@ -280,7 +280,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val response = it.contexts.response!! @@ -300,7 +300,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val response = it.contexts.response!! @@ -318,7 +318,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(listOf("LaunchDetails", "query", "200"), it.fingerprints) }, @@ -337,7 +337,7 @@ class SentryApollo3InterceptorClientErrors { executeQuery(sut) // HttpInterceptor does not throw for >= 400 - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -345,7 +345,7 @@ class SentryApollo3InterceptorClientErrors { val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk) - whenever(fixture.hub.captureEvent(any(), any())).thenThrow(RuntimeException()) + whenever(fixture.scopes.captureEvent(any(), any())).thenThrow(RuntimeException()) executeQuery(sut) } @@ -360,7 +360,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { val request = it.get(TypeCheckHint.APOLLO_REQUEST) diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt index 44d8bfd624..3b836f45b9 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt @@ -9,7 +9,7 @@ import com.apollographql.apollo3.network.http.HttpInterceptor import com.apollographql.apollo3.network.http.HttpInterceptorChain import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.Scope import io.sentry.ScopeCallback @@ -57,11 +57,11 @@ class SentryApollo3InterceptorTest { sdkVersion = SdkVersion("test", "1.2.3") } val scope = Scope(options) - val hub = mock().also { + val scopes = mock().also { whenever(it.options).thenReturn(options) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(it).configureScope(any()) } - private var httpInterceptor = SentryApollo3HttpInterceptor(hub, captureFailedRequests = false) + private var httpInterceptor = SentryApollo3HttpInterceptor(scopes, captureFailedRequests = false) @SuppressWarnings("LongParameterList") fun getSut( @@ -93,7 +93,7 @@ class SentryApollo3InterceptorTest { ) if (beforeSpan != null) { - httpInterceptor = SentryApollo3HttpInterceptor(hub, beforeSpan, captureFailedRequests = false) + httpInterceptor = SentryApollo3HttpInterceptor(scopes, beforeSpan, captureFailedRequests = false) } val builder = ApolloClient.Builder() @@ -124,7 +124,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 200) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -139,7 +139,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 403) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -159,7 +159,7 @@ class SentryApollo3InterceptorTest { } executeQuery(fixture.getSut(interceptor = failingInterceptor)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 404, contentLength = null) assertEquals("POST", it.spans.first().data?.get(SpanDataConvention.HTTP_METHOD_KEY)) @@ -176,7 +176,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = null, contentLength = null) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -241,7 +241,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) val httpClientSpan = it.spans.first() @@ -261,7 +261,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(0, it.spans.size) }, @@ -281,7 +281,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) }, @@ -294,7 +294,7 @@ class SentryApollo3InterceptorTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) // response_body_size is added but mock webserver returns 0 always @@ -309,9 +309,9 @@ class SentryApollo3InterceptorTest { @Test fun `sets SDKVersion Info`() { - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("Apollo3")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo-3" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("Apollo3")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo-3" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } @@ -320,14 +320,14 @@ class SentryApollo3InterceptorTest { fun `attaches to root transaction on Android`() { Apollo3PlatformTestManipulator.pretendIsAndroid(true) executeQuery(fixture.getSut()) - verify(fixture.hub).transaction + verify(fixture.scopes).transaction } @Test fun `attaches to child span on non-Android`() { Apollo3PlatformTestManipulator.pretendIsAndroid(false) executeQuery(fixture.getSut()) - verify(fixture.hub).span + verify(fixture.scopes).span } private fun assertTransactionDetails(it: SentryTransaction, httpStatusCode: Int? = 200, contentLength: Long? = 0L) { @@ -350,9 +350,9 @@ class SentryApollo3InterceptorTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true, id: String = "83") = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.transaction).thenReturn(tx) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.transaction).thenReturn(tx) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt index 81775efc18..3ac3d80d7d 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt @@ -3,7 +3,7 @@ package io.sentry.apollo3 import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.exception.ApolloException import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -32,7 +32,7 @@ class SentryApollo3InterceptorWithVariablesTest { class Fixture { val server = MockWebServer() - val hub = mock() + val scopes = mock() @SuppressWarnings("LongParameterList") fun getSut( @@ -54,7 +54,7 @@ class SentryApollo3InterceptorWithVariablesTest { socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, beforeSpan: BeforeSpanCallback? = null ): ApolloClient { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "http://key@localhost/proj" } @@ -68,7 +68,7 @@ class SentryApollo3InterceptorWithVariablesTest { ) return ApolloClient.Builder().serverUrl(server.url("/").toString()) - .sentryTracing(hub = hub, beforeSpan = beforeSpan, captureFailedRequests = false) + .sentryTracing(scopes = scopes, beforeSpan = beforeSpan, captureFailedRequests = false) .build() } } @@ -79,7 +79,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -94,7 +94,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -109,7 +109,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -124,7 +124,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `handles non-ascii header values correctly`() { executeQuery(id = "á") - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -138,7 +138,7 @@ class SentryApollo3InterceptorWithVariablesTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) // response_body_size is added but mock webserver returns 0 always @@ -173,8 +173,8 @@ class SentryApollo3InterceptorWithVariablesTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true, id: String = "83") = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { diff --git a/sentry-apollo/api/sentry-apollo.api b/sentry-apollo/api/sentry-apollo.api index 8c18bce06e..63eac6a193 100644 --- a/sentry-apollo/api/sentry-apollo.api +++ b/sentry-apollo/api/sentry-apollo.api @@ -5,9 +5,9 @@ public final class io/sentry/apollo/BuildConfig { public final class io/sentry/apollo/SentryApolloInterceptor : com/apollographql/apollo/interceptor/ApolloInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V public fun dispose ()V public fun interceptAsync (Lcom/apollographql/apollo/interceptor/ApolloInterceptor$InterceptorRequest;Lcom/apollographql/apollo/interceptor/ApolloInterceptorChain;Ljava/util/concurrent/Executor;Lcom/apollographql/apollo/interceptor/ApolloInterceptor$CallBack;)V diff --git a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt index faa8a549a9..fe5a6a4762 100644 --- a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt +++ b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt @@ -15,9 +15,9 @@ import com.apollographql.apollo.request.RequestHeaders import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel import io.sentry.SpanDataConvention @@ -32,12 +32,12 @@ import java.util.concurrent.Executor private const val TRACE_ORIGIN = "auto.graphql.apollo" class SentryApolloInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null ) : ApolloInterceptor { - constructor(hub: IHub) : this(hub, null) - constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + constructor(scopes: IScopes) : this(scopes, null) + constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) @@ -45,7 +45,7 @@ class SentryApolloInterceptor( } override fun interceptAsync(request: InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: CallBack) { - val activeSpan = if (io.sentry.util.Platform.isAndroid()) hub.transaction else hub.span + val activeSpan = if (io.sentry.util.Platform.isAndroid()) scopes.transaction else scopes.span if (activeSpan == null) { val headers = addTracingHeaders(request, null) val modifiedRequest = request.toBuilder().requestHeaders(headers).build() @@ -115,10 +115,10 @@ class SentryApolloInterceptor( private fun addTracingHeaders(request: InterceptorRequest, span: ISpan?): RequestHeaders { val requestHeaderBuilder = request.requestHeaders.toBuilder() - if (hub.options.isTraceSampling) { + if (scopes.options.isTraceSampling) { // we have no access to URI, no way to verify tracing origins TracingUtils.trace( - hub, + scopes, listOf(request.requestHeaders.headerValue(BaggageHeader.BAGGAGE_HEADER)), span )?.let { tracingHeaders -> @@ -154,7 +154,7 @@ class SentryApolloInterceptor( try { newSpan = beforeSpan.execute(span, request, response) } catch (e: Exception) { - hub.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) + scopes.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) } } if (newSpan == null) { @@ -182,7 +182,7 @@ class SentryApolloInterceptor( set(APOLLO_REQUEST, httpRequest) set(APOLLO_RESPONSE, httpResponse) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } } } diff --git a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt index d22c2fd3e5..b1b118c334 100644 --- a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt +++ b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt @@ -5,7 +5,7 @@ import com.apollographql.apollo.coroutines.await import com.apollographql.apollo.exception.ApolloException import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.Scope import io.sentry.ScopeCallback @@ -48,13 +48,13 @@ class SentryApolloInterceptorTest { sdkVersion = SdkVersion("test", "1.2.3") } val scope = Scope(options) - val hub = mock().also { + val scopes = mock().also { whenever(it.options).thenReturn(options) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(it).configureScope( any() ) } - private var interceptor = SentryApolloInterceptor(hub) + private var interceptor = SentryApolloInterceptor(scopes) @SuppressWarnings("LongParameterList") fun getSut( @@ -84,7 +84,7 @@ class SentryApolloInterceptorTest { ) if (beforeSpan != null) { - interceptor = SentryApolloInterceptor(hub, beforeSpan) + interceptor = SentryApolloInterceptor(scopes, beforeSpan) } return ApolloClient.builder() .serverUrl(server.url("/")) @@ -104,7 +104,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -120,7 +120,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -138,7 +138,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -176,7 +176,7 @@ class SentryApolloInterceptorTest { } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) val httpClientSpan = it.spans.first() @@ -196,7 +196,7 @@ class SentryApolloInterceptorTest { } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTrue(it.spans.isEmpty()) }, @@ -212,7 +212,7 @@ class SentryApolloInterceptorTest { fixture.getSut { _, _, _ -> throw RuntimeException() } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) }, @@ -225,7 +225,7 @@ class SentryApolloInterceptorTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(280L, it.data["response_body_size"]) @@ -237,9 +237,9 @@ class SentryApolloInterceptorTest { @Test fun `sets SDKVersion Info`() { - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("Apollo")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("Apollo")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } @@ -248,14 +248,14 @@ class SentryApolloInterceptorTest { fun `attaches to root transaction on Android`() { ApolloPlatformTestManipulator.pretendIsAndroid(true) executeQuery(fixture.getSut()) - verify(fixture.hub).transaction + verify(fixture.scopes).transaction } @Test fun `attaches to child span on non-Android`() { ApolloPlatformTestManipulator.pretendIsAndroid(false) executeQuery(fixture.getSut()) - verify(fixture.hub).span + verify(fixture.scopes).span } private fun assertTransactionDetails(it: SentryTransaction) { @@ -273,9 +273,9 @@ class SentryApolloInterceptorTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true) = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.transaction).thenReturn(tx) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.transaction).thenReturn(tx) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { diff --git a/sentry-graphql/api/sentry-graphql.api b/sentry-graphql/api/sentry-graphql.api index 57c253e23c..d119256010 100644 --- a/sentry-graphql/api/sentry-graphql.api +++ b/sentry-graphql/api/sentry-graphql.api @@ -9,10 +9,11 @@ public final class io/sentry/graphql/ExceptionReporter { } public final class io/sentry/graphql/ExceptionReporter$ExceptionDetails { - public fun (Lio/sentry/IHub;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V - public fun (Lio/sentry/IHub;Lgraphql/schema/DataFetchingEnvironment;Z)V - public fun getHub ()Lio/sentry/IHub; + public fun (Lio/sentry/IScopes;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V + public fun (Lio/sentry/IScopes;Lgraphql/schema/DataFetchingEnvironment;Z)V + public fun getHub ()Lio/sentry/IScopes; public fun getQuery ()Ljava/lang/String; + public fun getScopes ()Lio/sentry/IScopes; public fun getVariables ()Ljava/util/Map; public fun isSubscription ()Z } @@ -26,19 +27,19 @@ public final class io/sentry/graphql/GraphqlStringUtils { public final class io/sentry/graphql/NoOpSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public static fun getInstance ()Lio/sentry/graphql/NoOpSubscriptionHandler; - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/graphql/SentryDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler { public fun (Lgraphql/execution/DataFetcherExceptionHandler;)V - public fun (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V + public fun (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture; public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult; } public final class io/sentry/graphql/SentryGenericDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler { public fun (Lgraphql/execution/DataFetcherExceptionHandler;)V - public fun (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V + public fun (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture; public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult; } @@ -51,9 +52,10 @@ public final class io/sentry/graphql/SentryGraphqlExceptionHandler { public final class io/sentry/graphql/SentryInstrumentation : graphql/execution/instrumentation/SimpleInstrumentation { public static final field SENTRY_EXCEPTIONS_CONTEXT_KEY Ljava/lang/String; public static final field SENTRY_HUB_CONTEXT_KEY Ljava/lang/String; + public static final field SENTRY_SCOPES_CONTEXT_KEY Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Lio/sentry/graphql/ExceptionReporter;Ljava/util/List;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Z)V @@ -71,6 +73,6 @@ public abstract interface class io/sentry/graphql/SentryInstrumentation$BeforeSp } public abstract interface class io/sentry/graphql/SentrySubscriptionHandler { - public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java b/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java index 30ccb21425..843ca77494 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java @@ -5,7 +5,7 @@ import graphql.language.AstPrinter; import graphql.schema.DataFetchingEnvironment; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -33,7 +33,7 @@ public void captureThrowable( final @NotNull Throwable throwable, final @NotNull ExceptionDetails exceptionDetails, final @Nullable ExecutionResult result) { - final @NotNull IHub hub = exceptionDetails.getHub(); + final @NotNull IScopes scopes = exceptionDetails.getScopes(); final @NotNull Mechanism mechanism = new Mechanism(); mechanism.setType(MECHANISM_TYPE); mechanism.setHandled(false); @@ -43,44 +43,44 @@ public void captureThrowable( event.setLevel(SentryLevel.FATAL); final @NotNull Hint hint = new Hint(); - setRequestDetailsOnEvent(hub, exceptionDetails, event); + setRequestDetailsOnEvent(scopes, exceptionDetails, event); - if (result != null && isAllowedToAttachBody(hub)) { + if (result != null && isAllowedToAttachBody(scopes)) { final @NotNull Response response = new Response(); final @NotNull Map responseBody = result.toSpecification(); response.setData(responseBody); event.getContexts().setResponse(response); } - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } - private boolean isAllowedToAttachBody(final @NotNull IHub hub) { - final @NotNull SentryOptions options = hub.getOptions(); + private boolean isAllowedToAttachBody(final @NotNull IScopes scopes) { + final @NotNull SentryOptions options = scopes.getOptions(); return options.isSendDefaultPii() && !SentryOptions.RequestSize.NONE.equals(options.getMaxRequestBodySize()); } private void setRequestDetailsOnEvent( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionDetails exceptionDetails, final @NotNull SentryEvent event) { - hub.configureScope( + scopes.configureScope( (scope) -> { final @Nullable Request scopeRequest = scope.getRequest(); final @NotNull Request request = scopeRequest == null ? new Request() : scopeRequest; - setDetailsOnRequest(hub, exceptionDetails, request); + setDetailsOnRequest(scopes, exceptionDetails, request); event.setRequest(request); }); } private void setDetailsOnRequest( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionDetails exceptionDetails, final @NotNull Request request) { request.setApiTarget("graphql"); - if (isAllowedToAttachBody(hub) + if (isAllowedToAttachBody(scopes) && (exceptionDetails.isSubscription() || captureRequestBodyForNonSubscriptions)) { final @NotNull Map data = new HashMap<>(); @@ -99,27 +99,27 @@ private void setDetailsOnRequest( public static final class ExceptionDetails { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters; private final @Nullable DataFetchingEnvironment dataFetchingEnvironment; private final boolean isSubscription; public ExceptionDetails( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters, final boolean isSubscription) { - this.hub = hub; + this.scopes = scopes; this.instrumentationExecutionParameters = instrumentationExecutionParameters; dataFetchingEnvironment = null; this.isSubscription = isSubscription; } public ExceptionDetails( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable DataFetchingEnvironment dataFetchingEnvironment, final boolean isSubscription) { - this.hub = hub; + this.scopes = scopes; this.dataFetchingEnvironment = dataFetchingEnvironment; instrumentationExecutionParameters = null; this.isSubscription = isSubscription; @@ -149,8 +149,13 @@ public boolean isSubscription() { return isSubscription; } - public @NotNull IHub getHub() { - return hub; + @Deprecated + public @NotNull IScopes getHub() { + return scopes; + } + + public @NotNull IScopes getScopes() { + return scopes; } } } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java index df241ce35b..839f413719 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import org.jetbrains.annotations.NotNull; public final class NoOpSubscriptionHandler implements SentrySubscriptionHandler { @@ -17,7 +17,7 @@ private NoOpSubscriptionHandler() {} @Override public @NotNull Object onSubscriptionResult( @NotNull Object result, - @NotNull IHub hub, + @NotNull IScopes scopes, @NotNull ExceptionReporter exceptionReporter, @NotNull InstrumentationFieldFetchParameters parameters) { return result; diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java index c0467c0089..0813aab851 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java @@ -6,8 +6,8 @@ import graphql.execution.DataFetcherExceptionHandlerParameters; import graphql.execution.DataFetcherExceptionHandlerResult; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.util.Objects; import java.util.concurrent.CompletableFuture; @@ -24,18 +24,18 @@ */ @Deprecated public final class SentryDataFetcherExceptionHandler implements DataFetcherExceptionHandler { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull DataFetcherExceptionHandler delegate; public SentryDataFetcherExceptionHandler( - final @NotNull IHub hub, final @NotNull DataFetcherExceptionHandler delegate) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.delegate = Objects.requireNonNull(delegate, "delegate is required"); SentryIntegrationPackageStorage.getInstance().addIntegration("GrahQLLegacyExceptionHandler"); } public SentryDataFetcherExceptionHandler(final @NotNull DataFetcherExceptionHandler delegate) { - this(HubAdapter.getInstance(), delegate); + this(ScopesAdapter.getInstance(), delegate); } @Override @@ -44,7 +44,7 @@ public CompletableFuture handleException( final Hint hint = new Hint(); hint.set(GRAPHQL_HANDLER_PARAMETERS, handlerParameters); - hub.captureException(handlerParameters.getException(), hint); + scopes.captureException(handlerParameters.getException(), hint); return delegate.handleException(handlerParameters); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java index 6251d00779..1287d38caa 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java @@ -3,7 +3,7 @@ import graphql.execution.DataFetcherExceptionHandler; import graphql.execution.DataFetcherExceptionHandlerParameters; import graphql.execution.DataFetcherExceptionHandlerResult; -import io.sentry.IHub; +import io.sentry.IScopes; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import org.jetbrains.annotations.NotNull; @@ -17,7 +17,7 @@ public final class SentryGenericDataFetcherExceptionHandler implements DataFetch private final @NotNull SentryGraphqlExceptionHandler handler; public SentryGenericDataFetcherExceptionHandler( - final @Nullable IHub hub, final @NotNull DataFetcherExceptionHandler delegate) { + final @Nullable IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) { this.handler = new SentryGraphqlExceptionHandler(delegate); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java index d2d62c99d8..e4f85d12a2 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java @@ -18,9 +18,9 @@ import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLOutputType; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SpanStatus; @@ -46,7 +46,11 @@ public final class SentryInstrumentation "INTERNAL", // Netflix DGS "DataFetchingException" // raw graphql-java ); - public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = "sentry.hub"; + public static final @NotNull String SENTRY_SCOPES_CONTEXT_KEY = "sentry.scopes"; + + @Deprecated + public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = SENTRY_SCOPES_CONTEXT_KEY; + public static final @NotNull String SENTRY_EXCEPTIONS_CONTEXT_KEY = "sentry.exceptions"; private static final String TRACE_ORIGIN = "auto.graphql.graphql"; private final @Nullable BeforeSpanCallback beforeSpan; @@ -70,7 +74,7 @@ public SentryInstrumentation() { */ @Deprecated @SuppressWarnings("InlineMeSuggester") - public SentryInstrumentation(final @Nullable IHub hub) { + public SentryInstrumentation(final @Nullable IScopes scopes) { this(null, NoOpSubscriptionHandler.getInstance(), true); } @@ -89,7 +93,7 @@ public SentryInstrumentation(final @Nullable BeforeSpanCallback beforeSpan) { @Deprecated @SuppressWarnings("InlineMeSuggester") public SentryInstrumentation( - final @Nullable IHub hub, final @Nullable BeforeSpanCallback beforeSpan) { + final @Nullable IScopes scopes, final @Nullable BeforeSpanCallback beforeSpan) { this(beforeSpan, NoOpSubscriptionHandler.getInstance(), true); } @@ -172,9 +176,9 @@ public SentryInstrumentation( public @NotNull InstrumentationContext beginExecution( final @NotNull InstrumentationExecutionParameters parameters) { final TracingState tracingState = parameters.getInstrumentationState(); - final @NotNull IHub currentHub = Sentry.getCurrentHub(); - tracingState.setTransaction(currentHub.getSpan()); - parameters.getGraphQLContext().put(SENTRY_HUB_CONTEXT_KEY, currentHub); + final @NotNull IScopes currentScopes = Sentry.getCurrentScopes(); + tracingState.setTransaction(currentScopes.getSpan()); + parameters.getGraphQLContext().put(SENTRY_SCOPES_CONTEXT_KEY, currentScopes); return super.beginExecution(parameters); } @@ -195,7 +199,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( throwable, new ExceptionReporter.ExceptionDetails( - hubFromContext(graphQLContext), parameters, false), + scopesFromContext(graphQLContext), parameters, false), result); } } @@ -207,7 +211,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( new RuntimeException(error.getMessage()), new ExceptionReporter.ExceptionDetails( - hubFromContext(graphQLContext), parameters, false), + scopesFromContext(graphQLContext), parameters, false), result); } } @@ -217,7 +221,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( exception, new ExceptionReporter.ExceptionDetails( - hubFromContext(parameters.getGraphQLContext()), parameters, false), + scopesFromContext(parameters.getGraphQLContext()), parameters, false), null); } }); @@ -262,7 +266,7 @@ private boolean isIgnored(final @Nullable String errorType) { operationDefinition.getOperation(); final @Nullable String operationType = operation == null ? null : operation.name().toLowerCase(Locale.ROOT); - hubFromContext(parameters.getExecutionContext().getGraphQLContext()) + scopesFromContext(parameters.getExecutionContext().getGraphQLContext()) .addBreadcrumb( Breadcrumb.graphqlOperation( operationDefinition.getName(), @@ -273,11 +277,11 @@ private boolean isIgnored(final @Nullable String errorType) { return super.beginExecuteOperation(parameters); } - private @NotNull IHub hubFromContext(final @Nullable GraphQLContext context) { + private @NotNull IScopes scopesFromContext(final @Nullable GraphQLContext context) { if (context == null) { - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } - return context.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return context.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } @Override @@ -293,7 +297,7 @@ private boolean isIgnored(final @Nullable String errorType) { return environment -> { final @Nullable ExecutionStepInfo executionStepInfo = environment.getExecutionStepInfo(); if (executionStepInfo != null) { - hubFromContext(parameters.getExecutionContext().getGraphQLContext()) + scopesFromContext(parameters.getExecutionContext().getGraphQLContext()) .addBreadcrumb( Breadcrumb.graphqlDataFetcher( StringUtils.toString(executionStepInfo.getPath()), @@ -351,7 +355,7 @@ private boolean isIgnored(final @Nullable String errorType) { environment.getOperationDefinition().getOperation())) { return subscriptionHandler.onSubscriptionResult( tmpResult, - hubFromContext(environment.getGraphQlContext()), + scopesFromContext(environment.getGraphQlContext()), exceptionReporter, parameters); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java index bfc962b501..0a5538ce22 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java @@ -1,14 +1,14 @@ package io.sentry.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import org.jetbrains.annotations.NotNull; public interface SentrySubscriptionHandler { @NotNull Object onSubscriptionResult( @NotNull Object result, - @NotNull IHub hub, + @NotNull IScopes scopes, @NotNull ExceptionReporter exceptionReporter, @NotNull InstrumentationFieldFetchParameters parameters); } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt index a2b2b0f101..3a798a2f86 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt @@ -12,8 +12,8 @@ import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLScalarType import graphql.schema.GraphQLSchema import io.sentry.Hint -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,7 +39,7 @@ class ExceptionReporterTest { it.maxRequestBodySize = SentryOptions.RequestSize.ALWAYS } val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() lateinit var instrumentationExecutionParameters: InstrumentationExecutionParameters lateinit var executionResult: ExecutionResult lateinit var scope: IScope @@ -47,7 +47,7 @@ class ExceptionReporterTest { val variables = mapOf("variableA" to "value a") fun getSut(options: SentryOptions = defaultOptions, captureRequestBodyForNonSubscriptions: Boolean = true): ExceptionReporter { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) scope = Scope(options) val exceptionReporter = ExceptionReporter(captureRequestBodyForNonSubscriptions) executionResult = ExecutionResultImpl.newExecutionResult() @@ -77,7 +77,7 @@ class ExceptionReporterTest { ).build() val instrumentationState = SentryInstrumentation.TracingState() instrumentationExecutionParameters = InstrumentationExecutionParameters(executionInput, schema, instrumentationState) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) return exceptionReporter } @@ -88,9 +88,9 @@ class ExceptionReporterTest { @Test fun `captures throwable`() { val exceptionReporter = fixture.getSut() - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -112,9 +112,9 @@ class ExceptionReporterTest { val exceptionReporter = fixture.getSut() val headers = mapOf("some-header" to "some-header-value") fixture.scope.request = Request().also { it.headers = headers } - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -136,9 +136,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if spring`() { val exceptionReporter = fixture.getSut(captureRequestBodyForNonSubscriptions = false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -156,9 +156,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if no max body size is set`() { val exceptionReporter = fixture.getSut(SentryOptions().also { it.isSendDefaultPii = true }, false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -176,9 +176,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if sendDefaultPii is false`() { val exceptionReporter = fixture.getSut(SentryOptions().also { it.maxRequestBodySize = SentryOptions.RequestSize.ALWAYS }, false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -196,9 +196,9 @@ class ExceptionReporterTest { @Test fun `attaches query and variables if spring and subscription`() { val exceptionReporter = fixture.getSut(captureRequestBodyForNonSubscriptions = false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, true), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, true), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt index b571fa8218..de51abac21 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt @@ -3,7 +3,7 @@ package io.sentry.graphql import graphql.execution.DataFetcherExceptionHandler import graphql.execution.DataFetcherExceptionHandlerParameters import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq import org.mockito.kotlin.mock @@ -14,15 +14,15 @@ class SentryDataFetcherExceptionHandlerTest { @Test fun `passes exception to Sentry and invokes delegate`() { - val hub = mock() + val scopes = mock() val delegate = mock() - val handler = SentryDataFetcherExceptionHandler(hub, delegate) + val handler = SentryDataFetcherExceptionHandler(scopes, delegate) val exception = RuntimeException() val parameters = DataFetcherExceptionHandlerParameters.newExceptionParameters().exception(exception).build() handler.onException(parameters) - verify(hub).captureException(eq(exception), anyOrNull()) + verify(scopes).captureException(eq(exception), anyOrNull()) verify(delegate).handleException(parameters) } } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt index 6d643baf01..88e2f5df55 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.GraphQLContext import graphql.execution.DataFetcherExceptionHandler import graphql.execution.DataFetcherExceptionHandlerParameters import graphql.schema.DataFetchingEnvironmentImpl -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import kotlin.test.Test @@ -15,10 +15,10 @@ class SentryGenericDataFetcherExceptionHandlerTest { @Test fun `collects exception into GraphQLContext and invokes delegate`() { - val hub = mock() + val scopes = mock() val delegate = mock() val handler = SentryGenericDataFetcherExceptionHandler( - hub, + scopes, delegate ) diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt index e30bbc9415..087a1dfa72 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt @@ -28,7 +28,8 @@ import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLScalarType import graphql.schema.GraphQLSchema import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -52,7 +53,7 @@ import kotlin.test.assertSame class SentryInstrumentationAnotherTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var activeSpan: SentryTracer lateinit var dataFetcher: DataFetcher lateinit var fieldFetchParameters: InstrumentationFieldFetchParameters @@ -70,17 +71,17 @@ class SentryInstrumentationAnotherTest { val variables = mapOf("variableA" to "value a") fun getSut(isTransactionActive: Boolean = true, operation: OperationDefinition.Operation = OperationDefinition.Operation.QUERY, graphQLContextParam: Map? = null, addTransactionToTracingState: Boolean = true, ignoredErrors: List = emptyList()): SentryInstrumentation { - whenever(hub.options).thenReturn(SentryOptions()) - activeSpan = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(SentryOptions()) + activeSpan = SentryTracer(TransactionContext("name", "op"), scopes) if (isTransactionActive) { - whenever(hub.span).thenReturn(activeSpan) + whenever(scopes.span).thenReturn(activeSpan) } else { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) } val defaultGraphQLContext = mapOf( - SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY to hub + SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY to scopes ) val mergedField = MergedField.newMergedField().addField(Field.newField("myFieldName").build()).build() @@ -165,7 +166,7 @@ class SentryInstrumentationAnotherTest { val result = instrumentedDataFetcher.get(fixture.environment) assertEquals("result modified by subscription handler", result) - verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.hub), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) + verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.scopes), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) } @Test @@ -175,7 +176,7 @@ class SentryInstrumentationAnotherTest { val result = instrumentedDataFetcher.get(fixture.environment) assertEquals("result modified by subscription handler", result) - verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.hub), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) + verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.scopes), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) } @Test @@ -222,7 +223,7 @@ class SentryInstrumentationAnotherTest { fun `adds a breadcrumb for operation`() { val instrumentation = fixture.getSut() instrumentation.beginExecuteOperation(fixture.instrumentationExecuteOperationParameters) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( org.mockito.kotlin.check { breadcrumb -> assertEquals("graphql", breadcrumb.type) assertEquals("query", breadcrumb.category) @@ -237,7 +238,7 @@ class SentryInstrumentationAnotherTest { fun `adds a breadcrumb for data fetcher`() { val instrumentation = fixture.getSut() instrumentation.instrumentDataFetcher(fixture.dataFetcher, fixture.fieldFetchParameters).get(fixture.environment) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( org.mockito.kotlin.check { breadcrumb -> assertEquals("graphql", breadcrumb.type) assertEquals("graphql.fetcher", breadcrumb.category) @@ -250,11 +251,11 @@ class SentryInstrumentationAnotherTest { } @Test - fun `stores hub in context and adds transaction to state`() { + fun `stores scopes in context and adds transaction to state`() { val instrumentation = fixture.getSut(isTransactionActive = true, operation = OperationDefinition.Operation.MUTATION, graphQLContextParam = emptyMap(), addTransactionToTracingState = false) - withMockHub { + withMockScopes { instrumentation.beginExecution(fixture.instrumentationExecutionParameters) - assertSame(fixture.hub, fixture.instrumentationExecutionParameters.graphQLContext.get(SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY)) + assertSame(fixture.scopes, fixture.instrumentationExecutionParameters.graphQLContext.get(SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY)) assertNotNull(fixture.instrumentationState.transaction) } } @@ -276,7 +277,7 @@ class SentryInstrumentationAnotherTest { assertEquals("exception message", it.message) }, org.mockito.kotlin.check { - assertSame(fixture.hub, it.hub) + assertSame(fixture.scopes, it.scopes) assertSame(fixture.query, it.query) assertEquals(false, it.isSubscription) assertEquals(fixture.variables, it.variables) @@ -293,7 +294,7 @@ class SentryInstrumentationAnotherTest { val instrumentation = fixture.getSut( graphQLContextParam = mapOf( SENTRY_EXCEPTIONS_CONTEXT_KEY to listOf(exception), - SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY to fixture.hub + SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY to fixture.scopes ) ) val executionResult = ExecutionResultImpl.newExecutionResult() @@ -305,7 +306,7 @@ class SentryInstrumentationAnotherTest { assertSame(exception, it) }, org.mockito.kotlin.check { - assertSame(fixture.hub, it.hub) + assertSame(fixture.scopes, it.scopes) assertSame(fixture.query, it.query) assertEquals(false, it.isSubscription) assertEquals(fixture.variables, it.variables) @@ -356,8 +357,9 @@ class SentryInstrumentationAnotherTest { assertSame(executionResult, result) } - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt index 8a579e2687..2bb46f79fc 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt @@ -18,7 +18,8 @@ import graphql.schema.GraphQLScalarType import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaGenerator import graphql.schema.idl.SchemaParser -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -39,12 +40,12 @@ import kotlin.test.assertTrue class SentryInstrumentationTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var activeSpan: SentryTracer fun getSut(isTransactionActive: Boolean = true, dataFetcherThrows: Boolean = false, beforeSpan: SentryInstrumentation.BeforeSpanCallback? = null): GraphQL { - whenever(hub.options).thenReturn(SentryOptions()) - activeSpan = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(SentryOptions()) + activeSpan = SentryTracer(TransactionContext("name", "op"), scopes) val schema = """ type Query { shows: [Show] @@ -61,9 +62,9 @@ class SentryInstrumentationTest { .build() if (isTransactionActive) { - whenever(hub.span).thenReturn(activeSpan) + whenever(scopes.span).thenReturn(activeSpan) } else { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) } return graphQL @@ -87,7 +88,7 @@ class SentryInstrumentationTest { fun `when transaction is active, creates inner spans`() { val sut = fixture.getSut() - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -105,7 +106,7 @@ class SentryInstrumentationTest { fun `when transaction is active, and data fetcher throws, creates inner spans`() { val sut = fixture.getSut(dataFetcherThrows = true) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isNotEmpty()) @@ -122,7 +123,7 @@ class SentryInstrumentationTest { fun `when transaction is not active, does not create spans`() { val sut = fixture.getSut(isTransactionActive = false) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -134,7 +135,7 @@ class SentryInstrumentationTest { fun `beforeSpan can drop spans`() { val sut = fixture.getSut(beforeSpan = SentryInstrumentation.BeforeSpanCallback { _, _, _ -> null }) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -152,7 +153,7 @@ class SentryInstrumentationTest { fun `beforeSpan can modify spans`() { val sut = fixture.getSut(beforeSpan = SentryInstrumentation.BeforeSpanCallback { span, _, _ -> span.apply { description = "changed" } }) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -208,19 +209,20 @@ class SentryInstrumentationTest { @Test fun `Integration adds itself to integration and package list`() { - withMockHub { + withMockScopes { val sut = fixture.getSut() - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("GraphQL")) + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("GraphQL")) val packageInfo = - fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-graphql" } + fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-graphql" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } } - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } diff --git a/sentry-jdbc/api/sentry-jdbc.api b/sentry-jdbc/api/sentry-jdbc.api index cff0f37fd2..700cbb2d69 100644 --- a/sentry-jdbc/api/sentry-jdbc.api +++ b/sentry-jdbc/api/sentry-jdbc.api @@ -16,7 +16,7 @@ public final class io/sentry/jdbc/DatabaseUtils$DatabaseDetails { public class io/sentry/jdbc/SentryJdbcEventListener : com/p6spy/engine/event/SimpleJdbcEventListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun onAfterAnyExecute (Lcom/p6spy/engine/common/StatementInformation;JLjava/sql/SQLException;)V public fun onBeforeAnyExecute (Lcom/p6spy/engine/common/StatementInformation;)V } diff --git a/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java b/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java index 0346d2d0b9..4cb21188e4 100644 --- a/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java +++ b/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java @@ -6,9 +6,9 @@ import com.jakewharton.nopen.annotation.Open; import com.p6spy.engine.common.StatementInformation; import com.p6spy.engine.event.SimpleJdbcEventListener; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.Span; import io.sentry.SpanStatus; @@ -21,24 +21,24 @@ @Open public class SentryJdbcEventListener extends SimpleJdbcEventListener { private static final String TRACE_ORIGIN = "auto.db.jdbc"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private static final @NotNull ThreadLocal CURRENT_SPAN = new ThreadLocal<>(); private volatile @Nullable DatabaseUtils.DatabaseDetails cachedDatabaseDetails = null; private final @NotNull Object databaseDetailsLock = new Object(); - public SentryJdbcEventListener(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryJdbcEventListener(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); addPackageAndIntegrationInfo(); } public SentryJdbcEventListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void onBeforeAnyExecute(final @NotNull StatementInformation statementInformation) { - final ISpan parent = hub.getSpan(); + final ISpan parent = scopes.getSpan(); if (parent != null && !parent.isNoOp()) { final ISpan span = parent.startChild("db.query", statementInformation.getSql()); CURRENT_SPAN.set(span); diff --git a/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt b/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt index 78c5d4cf12..00ce03de41 100644 --- a/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt +++ b/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt @@ -2,7 +2,7 @@ package io.sentry.jdbc import com.p6spy.engine.common.StatementInformation import com.p6spy.engine.spy.P6DataSource -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention.DB_NAME_KEY @@ -26,7 +26,7 @@ import kotlin.test.assertTrue class SentryJdbcEventListenerTest { class Fixture { - val hub = mock().apply { + val scopes = mock().apply { whenever(options).thenReturn( SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") @@ -37,9 +37,9 @@ class SentryJdbcEventListenerTest { val actualDataSource = JDBCDataSource() fun getSut(withRunningTransaction: Boolean = true, existingRow: Int? = null): DataSource { - tx = SentryTracer(TransactionContext("name", "op"), hub) + tx = SentryTracer(TransactionContext("name", "op"), scopes) if (withRunningTransaction) { - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) } actualDataSource.setURL("jdbc:hsqldb:mem:testdb") @@ -54,7 +54,7 @@ class SentryJdbcEventListenerTest { } } - val sentryQueryExecutionListener = SentryJdbcEventListener(hub) + val sentryQueryExecutionListener = SentryJdbcEventListener(scopes) val p6spyDataSource = P6DataSource(actualDataSource) p6spyDataSource.setJdbcEventListenerFactory { sentryQueryExecutionListener } return p6spyDataSource @@ -131,9 +131,9 @@ class SentryJdbcEventListenerTest { @Test fun `sets SDKVersion Info`() { val sut = fixture.getSut() - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("JDBC")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("JDBC")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } diff --git a/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java b/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java index 2ca775572b..c52b4706f2 100644 --- a/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java +++ b/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java @@ -6,7 +6,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -210,9 +210,9 @@ SentryEvent createEvent(final @NotNull LogRecord record) { mdcProperties = CollectionUtils.filterMapEntries(mdcProperties, entry -> entry.getValue() != null); if (!mdcProperties.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = HubAdapter.getInstance().getOptions().getContextTags(); + final List contextTags = ScopesAdapter.getInstance().getOptions().getContextTags(); if (!contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag diff --git a/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api b/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api index d501240a3a..7e3be67279 100644 --- a/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api +++ b/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api @@ -1,16 +1,16 @@ public final class io/sentry/kotlin/SentryContext : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/CopyableThreadContextElement { public fun ()V - public fun (Lio/sentry/IHub;)V - public synthetic fun (Lio/sentry/IHub;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public synthetic fun (Lio/sentry/IScopes;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun copyForChild ()Lkotlinx/coroutines/CopyableThreadContextElement; public fun fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element; public fun mergeForChild (Lkotlin/coroutines/CoroutineContext$Element;)Lkotlin/coroutines/CoroutineContext; public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext; public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext; - public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IHub;)V + public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IScopes;)V public synthetic fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V - public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IHub; + public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IScopes; public synthetic fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object; } diff --git a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt index 3cf22a20da..4c814f2805 100644 --- a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt +++ b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt @@ -1,6 +1,6 @@ package io.sentry.kotlin -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import kotlinx.coroutines.CopyableThreadContextElement import kotlin.coroutines.AbstractCoroutineContextElement @@ -9,26 +9,32 @@ import kotlin.coroutines.CoroutineContext /** * Sentry context element for [CoroutineContext]. */ -public class SentryContext(private val hub: IHub = Sentry.getCurrentHub().clone()) : - CopyableThreadContextElement, AbstractCoroutineContextElement(Key) { +@SuppressWarnings("deprecation") +// TODO fork instead +public class SentryContext(private val scopes: IScopes = Sentry.getCurrentScopes().clone()) : + CopyableThreadContextElement, AbstractCoroutineContextElement(Key) { private companion object Key : CoroutineContext.Key - override fun copyForChild(): CopyableThreadContextElement { - return SentryContext(hub.clone()) + @SuppressWarnings("deprecation") + override fun copyForChild(): CopyableThreadContextElement { + // TODO fork instead + return SentryContext(scopes.clone()) } + @SuppressWarnings("deprecation") override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext { - return overwritingElement[Key] ?: SentryContext(hub.clone()) + // TODO fork instead? + return overwritingElement[Key] ?: SentryContext(scopes.clone()) } - override fun updateThreadContext(context: CoroutineContext): IHub { - val oldState = Sentry.getCurrentHub() - Sentry.setCurrentHub(hub) + override fun updateThreadContext(context: CoroutineContext): IScopes { + val oldState = Sentry.getCurrentScopes() + Sentry.setCurrentScopes(scopes) return oldState } - override fun restoreThreadContext(context: CoroutineContext, oldState: IHub) { - Sentry.setCurrentHub(oldState) + override fun restoreThreadContext(context: CoroutineContext, oldState: IScopes) { + Sentry.setCurrentScopes(oldState) } } diff --git a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt index b54ceabc51..578b610267 100644 --- a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt +++ b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt @@ -119,7 +119,7 @@ class SentryContextTest { val c2 = launch( SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) @@ -145,7 +145,7 @@ class SentryContextTest { @Test fun `mergeForChild returns copy of initial context if Key not present`() { val initialContextElement = SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) @@ -158,7 +158,7 @@ class SentryContextTest { @Test fun `mergeForChild returns passed context`() { val initialContextElement = SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) diff --git a/sentry-log4j2/api/sentry-log4j2.api b/sentry-log4j2/api/sentry-log4j2.api index 76aa3e823e..b7fe8b3273 100644 --- a/sentry-log4j2/api/sentry-log4j2.api +++ b/sentry-log4j2/api/sentry-log4j2.api @@ -5,7 +5,7 @@ public final class io/sentry/log4j2/BuildConfig { public class io/sentry/log4j2/SentryAppender : org/apache/logging/log4j/core/appender/AbstractAppender { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/Boolean;Lio/sentry/ITransportFactory;Lio/sentry/IHub;[Ljava/lang/String;)V + public fun (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/Boolean;Lio/sentry/ITransportFactory;Lio/sentry/IScopes;[Ljava/lang/String;)V public fun append (Lorg/apache/logging/log4j/core/LogEvent;)V public static fun createAppender (Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/String;Ljava/lang/Boolean;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;)Lio/sentry/log4j2/SentryAppender; protected fun createBreadcrumb (Lorg/apache/logging/log4j/core/LogEvent;)Lio/sentry/Breadcrumb; diff --git a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java index 4cf4ad4a86..4ee07ab7b9 100644 --- a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java +++ b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java @@ -7,9 +7,9 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -50,7 +50,7 @@ public class SentryAppender extends AbstractAppender { private @NotNull Level minimumBreadcrumbLevel = Level.INFO; private @NotNull Level minimumEventLevel = Level.ERROR; private final @Nullable Boolean debug; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable List contextTags; public SentryAppender( @@ -61,7 +61,7 @@ public SentryAppender( final @Nullable Level minimumEventLevel, final @Nullable Boolean debug, final @Nullable ITransportFactory transportFactory, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable String[] contextTags) { super(name, filter, null, true, null); this.dsn = dsn; @@ -73,7 +73,7 @@ public SentryAppender( } this.debug = debug; this.transportFactory = transportFactory; - this.hub = hub; + this.scopes = scopes; this.contextTags = contextTags != null ? Arrays.asList(contextTags) : null; } @@ -110,7 +110,7 @@ public SentryAppender( minimumEventLevel, debug, null, - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), contextTags != null ? contextTags.split(",") : null); } @@ -149,13 +149,13 @@ public void append(final @NotNull LogEvent eventObject) { final Hint hint = new Hint(); hint.set(SENTRY_SYNTHETIC_EXCEPTION, eventObject); - hub.captureEvent(createEvent(eventObject), hint); + scopes.captureEvent(createEvent(eventObject), hint); } if (eventObject.getLevel().isMoreSpecificThan(minimumBreadcrumbLevel)) { final Hint hint = new Hint(); hint.set(LOG4J_LOG_EVENT, eventObject); - hub.addBreadcrumb(createBreadcrumb(eventObject), hint); + scopes.addBreadcrumb(createBreadcrumb(eventObject), hint); } } @@ -199,9 +199,9 @@ public void append(final @NotNull LogEvent eventObject) { CollectionUtils.filterMapEntries( loggingEvent.getContextData().toMap(), entry -> entry.getValue() != null); if (!contextData.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = hub.getOptions().getContextTags(); + final List contextTags = scopes.getOptions().getContextTags(); if (contextTags != null && !contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag diff --git a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt index b6f004232c..3786555a61 100644 --- a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt +++ b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt @@ -1,7 +1,7 @@ package io.sentry.log4j2 -import io.sentry.HubAdapter import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.SentryLevel import io.sentry.checkEvent @@ -49,7 +49,7 @@ class SentryAppenderTest { } loggerContext.start() val config: Configuration = loggerContext.configuration - val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, debug, this.transportFactory, HubAdapter.getInstance(), contextTags?.toTypedArray()) + val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, debug, this.transportFactory, ScopesAdapter.getInstance(), contextTags?.toTypedArray()) config.addAppender(appender) val ref = AppenderRef.createAppenderRef("sentry", null, null) @@ -445,6 +445,6 @@ class SentryAppenderTest { @Test fun `sets the debug mode`() { fixture.getSut(debug = true) - assertTrue(HubAdapter.getInstance().options.isDebug) + assertTrue(ScopesAdapter.getInstance().options.isDebug) } } diff --git a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java index d0be108149..56db0b4dbc 100644 --- a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java +++ b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java @@ -12,8 +12,8 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.HubAdapter; import io.sentry.ITransportFactory; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -134,9 +134,9 @@ protected void append(@NotNull ILoggingEvent eventObject) { CollectionUtils.filterMapEntries( loggingEvent.getMDCPropertyMap(), entry -> entry.getValue() != null); if (!mdcProperties.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = HubAdapter.getInstance().getOptions().getContextTags(); + final List contextTags = ScopesAdapter.getInstance().getOptions().getContextTags(); if (!contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag diff --git a/sentry-okhttp/api/sentry-okhttp.api b/sentry-okhttp/api/sentry-okhttp.api index 3095659c88..9cb875ff34 100644 --- a/sentry-okhttp/api/sentry-okhttp.api +++ b/sentry-okhttp/api/sentry-okhttp.api @@ -6,12 +6,12 @@ public final class io/sentry/okhttp/BuildConfig { public class io/sentry/okhttp/SentryOkHttpEventListener : okhttp3/EventListener { public static final field Companion Lio/sentry/okhttp/SentryOkHttpEventListener$Companion; public fun ()V - public fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V - public synthetic fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V + public synthetic fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lokhttp3/EventListener$Factory;)V public fun (Lokhttp3/EventListener;)V public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V @@ -50,9 +50,9 @@ public final class io/sentry/okhttp/SentryOkHttpEventListener$Companion { public class io/sentry/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response; } diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt index 00cc26e754..153499210c 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryDate import io.sentry.SentryLevel @@ -30,7 +30,7 @@ private const val RESPONSE_BODY_TIMEOUT_MILLIS = 800L internal const val TRACE_ORIGIN = "auto.http.okhttp" @Suppress("TooManyFunctions") -internal class SentryOkHttpEvent(private val hub: IHub, private val request: Request) { +internal class SentryOkHttpEvent(private val scopes: IScopes, private val request: Request) { private val eventSpans: MutableMap = ConcurrentHashMap() private val breadcrumb: Breadcrumb internal val callRootSpan: ISpan? @@ -47,7 +47,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req val method: String = request.method // We start the call span that will contain all the others - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span callRootSpan = parentSpan?.startChild("http.client", "$method $url") callRootSpan?.spanContext?.origin = TRACE_ORIGIN urlDetails.applyToSpan(callRootSpan) @@ -149,13 +149,13 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) } // We send the breadcrumb even without spans. - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) // No span is created (e.g. no transaction is running) if (callRootSpan == null) { // We report the client error even without spans. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } return } @@ -173,7 +173,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req // We report the client error here, after all sub-spans finished, so that it will be bound // to the root call span. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } if (finishDate != null) { callRootSpan.finish(callRootSpan.status, finishDate) @@ -204,7 +204,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req fun scheduleFinish(timestamp: SentryDate) { try { - hub.options.executorService.schedule({ + scopes.options.executorService.schedule({ if (!isReadingResponseBody.get() && (eventSpans.values.all { it.isFinished } || callRootSpan?.isFinished != true) ) { @@ -212,7 +212,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req } }, RESPONSE_BODY_TIMEOUT_MILLIS) } catch (e: RejectedExecutionException) { - hub.options + scopes.options .logger .log( SentryLevel.ERROR, diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt index 67a8cd8b56..f20e2ef0cb 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SpanDataConvention import io.sentry.SpanStatus import okhttp3.Call @@ -41,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap */ @Suppress("TooManyFunctions") public open class SentryOkHttpEventListener( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val originalEventListenerCreator: ((call: Call) -> EventListener)? = null ) : EventListener() { @@ -62,27 +62,27 @@ public open class SentryOkHttpEventListener( } public constructor() : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = null ) public constructor(originalEventListener: EventListener) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListener } ) public constructor(originalEventListenerFactory: Factory) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListener: EventListener) : this( + scopes, originalEventListenerCreator = { originalEventListener } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListenerFactory: Factory) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerFactory: Factory) : this( + scopes, originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) @@ -92,7 +92,7 @@ public open class SentryOkHttpEventListener( // If the wrapped EventListener is ours, we can just delegate the calls, // without creating other events that would create duplicates if (canCreateEventSpan()) { - eventMap[call] = SentryOkHttpEvent(hub, call.request()) + eventMap[call] = SentryOkHttpEvent(scopes, call.request()) } } @@ -318,7 +318,7 @@ public open class SentryOkHttpEventListener( it.status = SpanStatus.fromHttpStatusCode(response.code) } } - okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: hub.options.dateProvider.now()) + okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: scopes.options.dateProvider.now()) } override fun responseBodyStart(call: Call) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt index efa472963d..a079864612 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt @@ -4,9 +4,9 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.SpanDataConvention @@ -29,7 +29,7 @@ import java.io.IOException * out of the active span bound to the scope for each HTTP Request. * If [captureFailedRequests] is enabled, the SDK will capture HTTP Client errors as well. * - * @param hub The [IHub], internal and only used for testing. + * @param scopes The [IScopes], internal and only used for testing. * @param beforeSpan The [ISpan] can be customized or dropped with the [BeforeSpanCallback]. * @param captureFailedRequests The SDK will only capture HTTP Client errors if it is enabled, * Defaults to false. @@ -39,7 +39,7 @@ import java.io.IOException * is a match for any of the defined targets. */ public open class SentryOkHttpInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = true, private val failedRequestStatusCodes: List = listOf( @@ -48,9 +48,9 @@ public open class SentryOkHttpInterceptor( private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) ) : Interceptor { - public constructor() : this(HubAdapter.getInstance()) - public constructor(hub: IHub) : this(hub, null) - public constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + public constructor() : this(ScopesAdapter.getInstance()) + public constructor(scopes: IScopes) : this(scopes, null) + public constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) @@ -76,7 +76,7 @@ public open class SentryOkHttpInterceptor( } else { // read the span from the bound scope okHttpEvent = null - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span span = parentSpan?.startChild("http.client", "$method $url") } @@ -92,7 +92,7 @@ public open class SentryOkHttpInterceptor( val requestBuilder = request.newBuilder() TracingUtils.traceIfAllowed( - hub, + scopes, request.url.toString(), request.headers(BaggageHeader.BAGGAGE_HEADER), span @@ -121,7 +121,7 @@ public open class SentryOkHttpInterceptor( if (isFromEventListener && okHttpEvent != null) { okHttpEvent.setClientErrorResponse(response) } else { - SentryOkHttpUtils.captureClientError(hub, request, response) + SentryOkHttpUtils.captureClientError(scopes, request, response) } } @@ -157,7 +157,7 @@ public open class SentryOkHttpInterceptor( hint[OKHTTP_RESPONSE] = it } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun finishSpan(span: ISpan?, request: Request, response: Response?, isFromEventListener: Boolean) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt index 0cfc1c5a75..eea35ca22e 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.TypeCheckHint import io.sentry.exception.ExceptionMechanismException @@ -15,7 +15,7 @@ import okhttp3.Response internal object SentryOkHttpUtils { - internal fun captureClientError(hub: IHub, request: Request, response: Response) { + internal fun captureClientError(scopes: IScopes, request: Request, response: Response) { // not possible to get a parameterized url, but we remove at least the // query string and the fragment. // url example: https://api.github.com/users/getsentry/repos/#fragment?query=query @@ -40,9 +40,9 @@ internal object SentryOkHttpUtils { val sentryRequest = io.sentry.protocol.Request().apply { urlDetails.applyToRequest(this) // Cookie is only sent if isSendDefaultPii is enabled - cookies = if (hub.options.isSendDefaultPii) request.headers["Cookie"] else null + cookies = if (scopes.options.isSendDefaultPii) request.headers["Cookie"] else null method = request.method - headers = getHeaders(hub, request.headers) + headers = getHeaders(scopes, request.headers) request.body?.contentLength().ifHasValidLength { bodySize = it @@ -51,8 +51,8 @@ internal object SentryOkHttpUtils { val sentryResponse = io.sentry.protocol.Response().apply { // Set-Cookie is only sent if isSendDefaultPii is enabled due to PII - cookies = if (hub.options.isSendDefaultPii) response.headers["Set-Cookie"] else null - headers = getHeaders(hub, response.headers) + cookies = if (scopes.options.isSendDefaultPii) response.headers["Set-Cookie"] else null + headers = getHeaders(scopes, response.headers) statusCode = response.code response.body?.contentLength().ifHasValidLength { @@ -63,7 +63,7 @@ internal object SentryOkHttpUtils { event.request = sentryRequest event.contexts.setResponse(sentryResponse) - hub.captureEvent(event, hint) + scopes.captureEvent(event, hint) } private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { @@ -72,9 +72,9 @@ internal object SentryOkHttpUtils { } } - private fun getHeaders(hub: IHub, requestHeaders: Headers): MutableMap? { + private fun getHeaders(scopes: IScopes, requestHeaders: Headers): MutableMap? { // Headers are only sent if isSendDefaultPii is enabled due to PII - if (!hub.options.isSendDefaultPii) { + if (!scopes.options.isSendDefaultPii) { return null } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt index 1d90da70fe..3a90a4c05c 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.BaggageHeader -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTraceHeader import io.sentry.SentryTracer @@ -36,7 +36,7 @@ import kotlin.test.assertTrue class SentryOkHttpEventListenerTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val mockEventListener = mock() val mockEventListenerFactory = mock() @@ -63,12 +63,12 @@ class SentryOkHttpEventListenerTest { isSendDefaultPii = sendDefaultPii configureOptions(this) } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -80,12 +80,12 @@ class SentryOkHttpEventListenerTest { val builder = OkHttpClient.Builder() if (useInterceptor) { - builder.addInterceptor(SentryOkHttpInterceptor(hub)) + builder.addInterceptor(SentryOkHttpInterceptor(scopes)) } sentryOkHttpEventListener = when { - eventListenerFactory != null -> SentryOkHttpEventListener(hub, eventListenerFactory) - eventListener != null -> SentryOkHttpEventListener(hub, eventListener) - else -> SentryOkHttpEventListener(hub) + eventListenerFactory != null -> SentryOkHttpEventListener(scopes, eventListenerFactory) + eventListener != null -> SentryOkHttpEventListener(scopes, eventListener) + else -> SentryOkHttpEventListener(scopes) } return builder.eventListener(sentryOkHttpEventListener).build() } @@ -276,7 +276,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -288,7 +288,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener factory passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListenerFactory = { originalListener }) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -300,7 +300,7 @@ class SentryOkHttpEventListenerTest { @Test fun `does not duplicated spans if an SentryOkHttpEventListener is passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val request = postRequest(body = "requestBody") val call = sut.newCall(request) @@ -363,8 +363,8 @@ class SentryOkHttpEventListenerTest { @Test fun `responseHeadersEnd schedules event finish`() { - val listener = SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener) - whenever(fixture.hub.options).thenReturn(SentryOptions()) + val listener = SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener) + whenever(fixture.scopes.options).thenReturn(SentryOptions()) val call = mock() whenever(call.request()).thenReturn(getRequest()) val okHttpEvent = mock() diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt index 337f722848..4d9f005143 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.ISpan import io.sentry.SentryDate @@ -50,14 +50,14 @@ import kotlin.test.assertTrue class SentryOkHttpEventTest { private class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val span: ISpan val mockRequest: Request val response: Response init { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -65,8 +65,8 @@ class SentryOkHttpEventTest { span = Span( TransactionContext("name", "op", TracesSamplingDecision(true)), - SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), hub), - hub, + SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), scopes), + scopes, null, SpanOptions() ) @@ -86,7 +86,7 @@ class SentryOkHttpEventTest { } fun getSut(currentSpan: ISpan? = span, requestUrl: String ? = null): SentryOkHttpEvent { - whenever(hub.span).thenReturn(currentSpan) + whenever(scopes.span).thenReturn(currentSpan) val request = if (requestUrl == null) { mockRequest } else { @@ -96,7 +96,7 @@ class SentryOkHttpEventTest { .url(server.url(requestUrl)) .build() } - return SentryOkHttpEvent(hub, request) + return SentryOkHttpEvent(scopes, request) } } @@ -126,7 +126,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut(currentSpan = null) assertNull(sut.callRootSpan) sut.finishEvent() - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test @@ -240,7 +240,7 @@ class SentryOkHttpEventTest { fun `when finishEvent, a breadcrumb is captured with request in the hint`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.mockRequest.url.toString(), it.data["url"]) assertEquals(fixture.mockRequest.url.host, it.data["host"]) @@ -258,7 +258,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut() sut.finishEvent() sut.finishEvent() - verify(fixture.hub, times(1)).addBreadcrumb(any(), any()) + verify(fixture.scopes, times(1)).addBreadcrumb(any(), any()) } @Test @@ -283,7 +283,7 @@ class SentryOkHttpEventTest { assertEquals(fixture.response.code, sut.callRootSpan?.getData(SpanDataConvention.HTTP_STATUS_CODE_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.response.protocol.name, it.data["protocol"]) assertEquals(fixture.response.code, it.data["status_code"]) @@ -300,7 +300,7 @@ class SentryOkHttpEventTest { sut.setProtocol("protocol") assertEquals("protocol", sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("protocol", it.data["protocol"]) }, @@ -314,7 +314,7 @@ class SentryOkHttpEventTest { sut.setProtocol(null) assertNull(sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["protocol"]) }, @@ -328,7 +328,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(10) assertEquals(10L, sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["request_content_length"]) }, @@ -342,7 +342,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(-1) assertNull(sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["request_content_length"]) }, @@ -356,7 +356,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(10) assertEquals(10L, sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["response_content_length"]) }, @@ -370,7 +370,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(-1) assertNull(sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["response_content_length"]) }, @@ -384,7 +384,7 @@ class SentryOkHttpEventTest { sut.setError("errorMessage") assertEquals("errorMessage", sut.callRootSpan?.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("errorMessage", it.data["error_message"]) }, @@ -399,7 +399,7 @@ class SentryOkHttpEventTest { assertNotNull(sut.callRootSpan) assertNull(sut.callRootSpan.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["error_message"]) }, @@ -532,7 +532,7 @@ class SentryOkHttpEventTest { @Test fun `scheduleFinish schedules finishEvent and finish running spans to specific timestamp`() { - fixture.hub.options.executorService = ImmediateExecutorService() + fixture.scopes.options.executorService = ImmediateExecutorService() val sut = spy(fixture.getSut()) val timestamp = mock() sut.startSpan(CONNECTION_EVENT) @@ -554,7 +554,7 @@ class SentryOkHttpEventTest { fun `scheduleFinish does not throw if executor is shut down`() { val executorService = mock() whenever(executorService.schedule(any(), any())).thenThrow(RejectedExecutionException()) - whenever(fixture.hub.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) + whenever(fixture.scopes.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) val sut = fixture.getSut() sut.scheduleFinish(mock()) } @@ -565,10 +565,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNotNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -586,10 +586,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -605,7 +605,7 @@ class SentryOkHttpEventTest { fun `when setClientErrorResponse is not called, no client error is captured`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } /** Retrieve all the spans started in the event using reflection. */ diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt index fce16d9220..f40b2c4cb5 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt @@ -6,8 +6,8 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -47,7 +47,7 @@ import kotlin.test.fail class SentryOkHttpInterceptorTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -82,13 +82,13 @@ class SentryOkHttpInterceptorTest { isSendDefaultPii = sendDefaultPii } scope = Scope(options) - whenever(hub.options).thenReturn(options) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -100,14 +100,14 @@ class SentryOkHttpInterceptorTest { val interceptor = when (captureFailedRequests) { null -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, failedRequestTargets = failedRequestTargets, failedRequestStatusCodes = failedRequestStatusCodes ) else -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, @@ -281,7 +281,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls succeeds`() { val sut = fixture.getSut(responseBody = "response body") sut.newCall(postRequest()).execute() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(13L, it.data[SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY]) @@ -296,7 +296,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls results in exception`() { // to setup mocks fixture.getSut() - val interceptor = SentryOkHttpInterceptor(fixture.hub) + val interceptor = SentryOkHttpInterceptor(fixture.scopes) val chain = mock() whenever(chain.call()).thenReturn(mock()) whenever(chain.proceed(any())).thenThrow(IOException()) @@ -308,7 +308,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) }, @@ -385,7 +385,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -396,7 +396,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -406,7 +406,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -417,7 +417,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -429,7 +429,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -440,7 +440,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { assertNotNull(it.get(TypeCheckHint.OKHTTP_REQUEST)) @@ -462,7 +462,7 @@ class SentryOkHttpInterceptorTest { val request = getRequest(url = "/hello?myQuery=myValue#myFragment") val response = sut.newCall(request).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("http://localhost:${fixture.server.port}/hello", sentryRequest.url) @@ -503,7 +503,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(postRequest(body = body)).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals(body.contentLength(), sentryRequest.bodySize) @@ -522,7 +522,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("myValue", sentryRequest.headers!!["myHeader"]) @@ -540,7 +540,7 @@ class SentryOkHttpInterceptorTest { // to setup mocks fixture.getSut() val interceptor = SentryOkHttpInterceptor( - fixture.hub, + fixture.scopes, captureFailedRequests = true ) val chain = mock() @@ -554,7 +554,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -565,7 +565,7 @@ class SentryOkHttpInterceptorTest { call.execute() val httpClientSpan = fixture.sentryTracer.children.firstOrNull() assertNull(httpClientSpan) - verify(fixture.hub, never()).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, never()).addBreadcrumb(any(), anyOrNull()) } @Test @@ -573,7 +573,7 @@ class SentryOkHttpInterceptorTest { val sut = fixture.getSut(captureFailedRequests = true, httpStatusCode = 500) val call = sut.newCall(getRequest()) call.execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -582,6 +582,6 @@ class SentryOkHttpInterceptorTest { val call = sut.newCall(getRequest()) SentryOkHttpEventListener.eventMap[call] = mock() call.execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt index ec19454327..c7194e5994 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.TransactionContext @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class SentryOkHttpUtilsTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() fun getSut( @@ -43,11 +43,11 @@ class SentryOkHttpUtilsTest { setTracePropagationTargets(listOf(server.hostName)) isSendDefaultPii = sendDefaultPii } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) server.enqueue( MockResponse() @@ -78,8 +78,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -103,8 +103,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -127,8 +127,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response diff --git a/sentry-openfeign/api/sentry-openfeign.api b/sentry-openfeign/api/sentry-openfeign.api index beb15c9e02..4ab65a5ca4 100644 --- a/sentry-openfeign/api/sentry-openfeign.api +++ b/sentry-openfeign/api/sentry-openfeign.api @@ -1,12 +1,12 @@ public final class io/sentry/openfeign/SentryCapability : feign/Capability { public fun ()V - public fun (Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun (Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun enrich (Lfeign/Client;)Lfeign/Client; } public final class io/sentry/openfeign/SentryFeignClient : feign/Client { - public fun (Lfeign/Client;Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V + public fun (Lfeign/Client;Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun execute (Lfeign/Request;Lfeign/Request$Options;)Lfeign/Response; } diff --git a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java index b65685c3fd..1ad6b1f274 100644 --- a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java +++ b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java @@ -2,33 +2,34 @@ import feign.Capability; import feign.Client; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** Adds Sentry tracing capability to Feign clients. */ public final class SentryCapability implements Capability { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan; public SentryCapability( - final @NotNull IHub hub, final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { - this.hub = hub; + final @NotNull IScopes scopes, + final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { + this.scopes = scopes; this.beforeSpan = beforeSpan; } public SentryCapability(final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { - this(HubAdapter.getInstance(), beforeSpan); + this(ScopesAdapter.getInstance(), beforeSpan); } public SentryCapability() { - this(HubAdapter.getInstance(), null); + this(ScopesAdapter.getInstance(), null); } @Override public @NotNull Client enrich(final @NotNull Client client) { - return new SentryFeignClient(client, hub, beforeSpan); + return new SentryFeignClient(client, scopes, beforeSpan); } } diff --git a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java index cb8aa3d9e0..037768c7ad 100644 --- a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java +++ b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java @@ -9,7 +9,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -30,15 +30,15 @@ public final class SentryFeignClient implements Client { private static final String TRACE_ORIGIN = "auto.http.openfeign"; private final @NotNull Client delegate; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable BeforeSpanCallback beforeSpan; public SentryFeignClient( final @NotNull Client delegate, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable BeforeSpanCallback beforeSpan) { this.delegate = Objects.requireNonNull(delegate, "delegate is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.beforeSpan = beforeSpan; } @@ -47,7 +47,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O throws IOException { Response response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { final @NotNull Request modifiedRequest = maybeAddTracingHeaders(request, null); @@ -102,7 +102,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url(), (requestBaggageHeaders != null ? new ArrayList<>(requestBaggageHeaders) : null), span); @@ -139,7 +139,7 @@ private void addBreadcrumb(final @NotNull Request request, final @Nullable Respo hint.set(OPEN_FEIGN_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } static final class RequestWrapper { diff --git a/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt b/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt index 65e56ab02b..959b890d46 100644 --- a/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt +++ b/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt @@ -7,7 +7,7 @@ import feign.HeaderMap import feign.RequestLine import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentryFeignClientTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val sentryTracer: SentryTracer val sentryOptions = SentryOptions().apply { @@ -46,9 +46,9 @@ class SentryFeignClientTest { val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) } fun getSut( @@ -59,7 +59,7 @@ class SentryFeignClientTest { beforeSpan: SentryFeignClient.BeforeSpanCallback? = null ): MockApi { if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -70,12 +70,12 @@ class SentryFeignClientTest { return if (!networkError) { Feign.builder() - .addCapability(SentryCapability(hub, beforeSpan)) + .addCapability(SentryCapability(scopes, beforeSpan)) } else { val mockClient = mock() whenever(mockClient.execute(any(), any())).thenThrow(RuntimeException::class.java) Feign.builder() - .client(SentryFeignClient(mockClient, hub, beforeSpan)) + .client(SentryFeignClient(mockClient, scopes, beforeSpan)) }.target(MockApi::class.java, server.url("/").toUrl().toString()) } } @@ -201,7 +201,7 @@ class SentryFeignClientTest { fun `adds breadcrumb when http calls succeeds`() { val sut = fixture.getSut(responseBody = "response body") sut.postWithBody("request-body") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(13, it.data["response_body_size"]) @@ -215,7 +215,7 @@ class SentryFeignClientTest { fun `adds breadcrumb when http calls succeeds even though response body is null`() { val sut = fixture.getSut(responseBody = "") sut.postWithBody("request-body") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(0, it.data["response_body_size"]) @@ -236,7 +236,7 @@ class SentryFeignClientTest { } catch (e: Exception) { // ignore me } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) }, diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java index 1e373ece9c..bfc4cd05f1 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java @@ -5,10 +5,10 @@ import io.opentelemetry.api.trace.TraceId; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.Instrumenter; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentrySpanStorage; @@ -20,21 +20,21 @@ public final class OpenTelemetryLinkErrorEventProcessor implements EventProcessor { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); public OpenTelemetryLinkErrorEventProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @TestOnly - OpenTelemetryLinkErrorEventProcessor(final @NotNull IHub hub) { - this.hub = hub; + OpenTelemetryLinkErrorEventProcessor(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override public @Nullable SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) { - final @NotNull Instrumenter instrumenter = hub.getOptions().getInstrumenter(); + final @NotNull Instrumenter instrumenter = scopes.getOptions().getInstrumenter(); if (Instrumenter.OTEL.equals(instrumenter)) { @NotNull final Span otelSpan = Span.current(); @NotNull final String traceId = otelSpan.getSpanContext().getTraceId(); @@ -55,7 +55,8 @@ public OpenTelemetryLinkErrorEventProcessor() { null); event.getContexts().setTrace(spanContext); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -64,7 +65,8 @@ public OpenTelemetryLinkErrorEventProcessor() { spanId, traceId); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -74,7 +76,8 @@ public OpenTelemetryLinkErrorEventProcessor() { traceId); } } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -84,7 +87,8 @@ public OpenTelemetryLinkErrorEventProcessor() { spanId); } } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java index 14ac12323b..ed3e243f4d 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java @@ -10,9 +10,9 @@ import io.opentelemetry.context.propagation.TextMapSetter; import io.sentry.Baggage; import io.sentry.BaggageHeader; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.SentrySpanStorage; import io.sentry.SentryTraceHeader; @@ -29,14 +29,14 @@ public final class SentryPropagator implements TextMapPropagator { private static final @NotNull List FIELDS = Arrays.asList(SentryTraceHeader.SENTRY_TRACE_HEADER, BaggageHeader.BAGGAGE_HEADER); private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryPropagator() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryPropagator(final @NotNull IHub hub) { - this.hub = hub; + SentryPropagator(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override @@ -49,7 +49,8 @@ public void inject(final Context context, final C carrier, final TextMapSett final @NotNull Span otelSpan = Span.fromContext(context); final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext(); if (!otelSpanContext.isValid()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -58,7 +59,8 @@ public void inject(final Context context, final C carrier, final TextMapSett } final @Nullable ISpan sentrySpan = spanStorage.get(otelSpanContext.getSpanId()); if (sentrySpan == null || sentrySpan.isNoOp()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -106,13 +108,15 @@ public Context extract( Span wrappedSpan = Span.wrap(otelSpanContext); modifiedContext = modifiedContext.with(wrappedSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "Continuing Sentry trace %s", sentryTraceHeader.getTraceId()); return modifiedContext; } catch (InvalidSentryTraceHeaderException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.ERROR, diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java index a9e70f66a0..6b7797153b 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java @@ -13,12 +13,12 @@ import io.opentelemetry.semconv.SemanticAttributes; import io.sentry.Baggage; import io.sentry.DsnUtil; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.Instrumenter; import io.sentry.PropagationContext; +import io.sentry.ScopesAdapter; import io.sentry.SentryDate; import io.sentry.SentryLevel; import io.sentry.SentryLongDate; @@ -46,14 +46,14 @@ public final class SentrySpanProcessor implements SpanProcessor { private final @NotNull SpanDescriptionExtractor spanDescriptionExtractor = new SpanDescriptionExtractor(); private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentrySpanProcessor(final @NotNull IHub hub) { - this.hub = hub; + SentrySpanProcessor(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override @@ -65,7 +65,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri final @NotNull TraceData traceData = getTraceData(otelSpan, parentContext); if (isSentryRequest(otelSpan)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -78,7 +79,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri traceData.getParentSpanId() == null ? null : spanStorage.get(traceData.getParentSpanId()); if (sentryParentSpan != null) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -94,7 +96,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri sentryChildSpan.getSpanContext().setOrigin(TRACE_ORIGN); spanStorage.store(traceData.getSpanId(), sentryChildSpan); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -123,7 +126,7 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri transactionOptions.setStartTimestamp( new SentryLongDate(otelSpan.toSpanData().getStartEpochNanos())); - ISpan sentryTransaction = hub.startTransaction(transactionContext, transactionOptions); + ISpan sentryTransaction = scopes.startTransaction(transactionContext, transactionOptions); sentryTransaction.getSpanContext().setOrigin(TRACE_ORIGN); spanStorage.store(traceData.getSpanId(), sentryTransaction); } @@ -144,7 +147,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { final @Nullable ISpan sentrySpan = spanStorage.removeAndGet(traceData.getSpanId()); if (sentrySpan == null) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -155,7 +159,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { } if (isSentryRequest(otelSpan)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -168,7 +173,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { if (sentrySpan instanceof ITransaction) { final @NotNull ITransaction sentryTransaction = (ITransaction) sentrySpan; updateTransactionWithOtelData(sentryTransaction, otelSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -178,7 +184,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { traceData.getTraceId()); } else { updateSpanWithOtelData(sentrySpan, otelSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -201,7 +208,8 @@ public boolean isEndRequired() { private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { if (!hasSentryBeenInitialized()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -209,9 +217,10 @@ private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { return false; } - final @NotNull Instrumenter instrumenter = hub.getOptions().getInstrumenter(); + final @NotNull Instrumenter instrumenter = scopes.getOptions().getInstrumenter(); if (!Instrumenter.OTEL.equals(instrumenter)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -222,7 +231,8 @@ private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext(); if (!otelSpanContext.isValid()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -241,7 +251,7 @@ private boolean isSentryRequest(final @NotNull ReadableSpan otelSpan) { } final @Nullable String httpUrl = otelSpan.getAttribute(SemanticAttributes.HTTP_URL); - return DsnUtil.urlContainsDsnHost(hub.getOptions(), httpUrl); + return DsnUtil.urlContainsDsnHost(scopes.getOptions(), httpUrl); } private @NotNull TraceData getTraceData( @@ -334,7 +344,7 @@ private SpanStatus mapOtelStatus(final @NotNull ReadableSpan otelSpan) { } private boolean hasSentryBeenInitialized() { - return hub.isEnabled(); + return scopes.isEnabled(); } private @NotNull Map toMapWithStringKeys(final @Nullable Attributes attributes) { diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt index 5ed757ba16..625fd54004 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt @@ -21,7 +21,7 @@ import io.opentelemetry.semconv.SemanticAttributes import io.sentry.Baggage import io.sentry.BaggageHeader import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.Instrumenter @@ -65,7 +65,7 @@ class SentrySpanProcessorTest { it.dsn = "https://key@sentry.io/proj" it.instrumenter = Instrumenter.OTEL } - val hub = mock() + val scopes = mock() val transaction = mock() val span = mock() val spanContext = mock() @@ -75,9 +75,9 @@ class SentrySpanProcessorTest { val baggage = Baggage.fromHeader(BAGGAGE_HEADER_STRING) fun setup() { - whenever(hub.isEnabled).thenReturn(true) - whenever(hub.options).thenReturn(options) - whenever(hub.startTransaction(any(), any())).thenReturn(transaction) + whenever(scopes.isEnabled).thenReturn(true) + whenever(scopes.options).thenReturn(options) + whenever(scopes.startTransaction(any(), any())).thenReturn(transaction) whenever(spanContext.operation).thenReturn("spanContextOp") whenever(spanContext.parentSpanId).thenReturn(io.sentry.SpanId("cedf5b7571cb4972")) @@ -94,7 +94,7 @@ class SentrySpanProcessorTest { whenever(transaction.startChild(any(), anyOrNull(), anyOrNull(), eq(Instrumenter.OTEL))).thenReturn(span) val sdkTracerProvider = SdkTracerProvider.builder() - .addSpanProcessor(SentrySpanProcessor(hub)) + .addSpanProcessor(SentrySpanProcessor(scopes)) .build() openTelemetry = OpenTelemetrySdk.builder() @@ -146,13 +146,13 @@ class SentrySpanProcessorTest { val context = mock() val span = mock() - whenever(fixture.hub.isEnabled).thenReturn(false) + whenever(fixture.scopes.isEnabled).thenReturn(false) - SentrySpanProcessor(fixture.hub).onStart(context, span) + SentrySpanProcessor(fixture.scopes).onStart(context, span) - verify(fixture.hub).isEnabled - verify(fixture.hub).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).options + verifyNoMoreInteractions(fixture.scopes) verifyNoInteractions(context, span) } @@ -161,13 +161,13 @@ class SentrySpanProcessorTest { fixture.setup() val span = mock() - whenever(fixture.hub.isEnabled).thenReturn(false) + whenever(fixture.scopes.isEnabled).thenReturn(false) - SentrySpanProcessor(fixture.hub).onEnd(span) + SentrySpanProcessor(fixture.scopes).onEnd(span) - verify(fixture.hub).isEnabled - verify(fixture.hub).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).options + verifyNoMoreInteractions(fixture.scopes) verifyNoInteractions(span) } @@ -178,7 +178,7 @@ class SentrySpanProcessorTest { val mockSpanContext = mock() whenever(mockSpanContext.spanId).thenReturn(SpanId.getInvalid()) whenever(mockSpan.spanContext).thenReturn(mockSpanContext) - SentrySpanProcessor(fixture.hub).onStart(Context.current(), mockSpan) + SentrySpanProcessor(fixture.scopes).onStart(Context.current(), mockSpan) thenNoTransactionIsStarted() } @@ -190,7 +190,7 @@ class SentrySpanProcessorTest { whenever(mockSpanContext.spanId).thenReturn(SpanId.fromBytes("seed".toByteArray())) whenever(mockSpanContext.traceId).thenReturn(TraceId.getInvalid()) whenever(mockSpan.spanContext).thenReturn(mockSpanContext) - SentrySpanProcessor(fixture.hub).onStart(Context.current(), mockSpan) + SentrySpanProcessor(fixture.scopes).onStart(Context.current(), mockSpan) thenNoTransactionIsStarted() } @@ -303,7 +303,7 @@ class SentrySpanProcessorTest { thenChildSpanIsStarted() otelChildSpan.setStatus(StatusCode.ERROR) - otelChildSpan.setAttribute(SemanticAttributes.HTTP_URL, "http://github.com/getsentry/sentry-java") + otelChildSpan.setAttribute(SemanticAttributes.HTTP_URL, "http://gitscopes.com/getsentry/sentry-java") otelChildSpan.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, 404L) otelChildSpan.end() @@ -342,7 +342,7 @@ class SentrySpanProcessorTest { thenTransactionIsStarted(otelSpan, isContinued = true) otelSpan.makeCurrent().use { _ -> - val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.hub).process(SentryEvent(), Hint()) + val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.scopes).process(SentryEvent(), Hint()) val traceContext = processedEvent!!.contexts.trace!! assertEquals("2722d9f6ec019ade60c776169d9a8904", traceContext.traceId.toString()) @@ -361,7 +361,7 @@ class SentrySpanProcessorTest { fixture.options.instrumenter = Instrumenter.SENTRY fixture.setup() - val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.hub).process(SentryEvent(), Hint()) + val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.scopes).process(SentryEvent(), Hint()) thenNoTraceContextHasBeenAddedToEvent(processedEvent) } @@ -393,7 +393,7 @@ class SentrySpanProcessorTest { private fun thenTransactionIsStarted(otelSpan: Span, isContinued: Boolean = false, continuesWithFilledBaggage: Boolean = true) { if (isContinued) { - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("testspan", it.name) assertEquals(TransactionNameSource.CUSTOM, it.transactionNameSource) @@ -423,7 +423,7 @@ class SentrySpanProcessorTest { } ) } else { - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("testspan", it.name) assertEquals(TransactionNameSource.CUSTOM, it.transactionNameSource) @@ -451,7 +451,7 @@ class SentrySpanProcessorTest { } private fun thenNoTransactionIsStarted() { - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) diff --git a/sentry-quartz/api/sentry-quartz.api b/sentry-quartz/api/sentry-quartz.api index ff32280dc5..23fce49e7d 100644 --- a/sentry-quartz/api/sentry-quartz.api +++ b/sentry-quartz/api/sentry-quartz.api @@ -7,7 +7,7 @@ public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener { public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String; public static final field SENTRY_SLUG_KEY Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun getName ()Ljava/lang/String; public fun jobExecutionVetoed (Lorg/quartz/JobExecutionContext;)V public fun jobToBeExecuted (Lorg/quartz/JobExecutionContext;)V diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index 28a0e51200..f9c22022cc 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -3,8 +3,8 @@ import io.sentry.BuildConfig; import io.sentry.CheckIn; import io.sentry.CheckInStatus; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; @@ -24,14 +24,14 @@ public final class SentryJobListener implements JobListener { public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; public static final String SENTRY_SLUG_KEY = "sentry-slug"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryJobListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryJobListener(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryJobListener(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); SentryIntegrationPackageStorage.getInstance().addIntegration("Quartz"); SentryIntegrationPackageStorage.getInstance() .addPackage("maven:io.sentry:sentry-quartz", BuildConfig.VERSION_NAME); @@ -49,15 +49,16 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { if (maybeSlug == null) { return; } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); final @NotNull String slug = maybeSlug; final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); - final @NotNull SentryId checkInId = hub.captureCheckIn(checkIn); + final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn); context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); context.put(SENTRY_SLUG_KEY, slug); } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobToBeExecuted.", t); } @@ -94,14 +95,15 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo if (slug != null) { final boolean isFailed = jobException != null; final @NotNull CheckInStatus status = isFailed ? CheckInStatus.ERROR : CheckInStatus.OK; - hub.captureCheckIn(new CheckIn(checkInId, slug, status)); + scopes.captureCheckIn(new CheckIn(checkInId, slug, status)); } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); } finally { - hub.popScope(); + scopes.popScope(); } } } diff --git a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt index 610fc1534d..a7004deb35 100644 --- a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt +++ b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt @@ -100,7 +100,7 @@ class ProfilingActivity : AppCompatActivity() { val traceData = ProfilingTraceData(profile, t) // Create envelope item from copied profile val item = - SentryEnvelopeItem.fromProfilingTrace(traceData, Long.MAX_VALUE, Sentry.getCurrentHub().options.serializer) + SentryEnvelopeItem.fromProfilingTrace(traceData, Long.MAX_VALUE, Sentry.getCurrentScopes().options.serializer) val itemData = item.data // Compress the envelope item using Gzip diff --git a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java index f78c3f71d5..72ecb14e2f 100644 --- a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java +++ b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.SentryUserFilter; import io.sentry.spring.jakarta.SentryUserProvider; import java.util.List; @@ -14,7 +14,7 @@ public class AppConfig { @Bean SentryUserFilter sentryUserFilter( - final IHub hub, final List sentryUserProviders) { - return new SentryUserFilter(hub, sentryUserProviders); + final IScopes scopes, final List sentryUserProviders) { + return new SentryUserFilter(scopes, sentryUserProviders); } } diff --git a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java index 92b48b138c..73d425b286 100644 --- a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java +++ b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.Collections; import org.springframework.context.annotation.Bean; @@ -20,14 +20,14 @@ public class WebConfig { * Creates a {@link RestTemplate} which calls are intercepted with {@link * SentrySpanClientHttpRequestInterceptor} to create spans around HTTP calls. * - * @param hub - sentry hub + * @param scopes - sentry scopes * @return RestTemplate */ @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } diff --git a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java index 7d46f09fb9..89a968834a 100644 --- a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java +++ b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.SentryUserFilter; import io.sentry.spring.SentryUserProvider; import java.util.List; @@ -14,7 +14,7 @@ public class AppConfig { @Bean SentryUserFilter sentryUserFilter( - final IHub hub, final List sentryUserProviders) { - return new SentryUserFilter(hub, sentryUserProviders); + final IScopes scopes, final List sentryUserProviders) { + return new SentryUserFilter(scopes, sentryUserProviders); } } diff --git a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java index e135cbe233..2990ba8a38 100644 --- a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java +++ b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.Collections; import org.springframework.context.annotation.Bean; @@ -20,14 +20,14 @@ public class WebConfig { * Creates a {@link RestTemplate} which calls are intercepted with {@link * SentrySpanClientHttpRequestInterceptor} to create spans around HTTP calls. * - * @param hub - sentry hub + * @param scopes - sentry scopes * @return RestTemplate */ @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } diff --git a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api index d0367e5195..a5421e7453 100644 --- a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api +++ b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api @@ -10,7 +10,7 @@ public class io/sentry/servlet/jakarta/SentryServletContainerInitializer : jakar public class io/sentry/servlet/jakarta/SentryServletRequestListener : jakarta/servlet/ServletRequestListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljakarta/servlet/ServletRequestEvent;)V public fun requestInitialized (Ljakarta/servlet/ServletRequestEvent;)V } diff --git a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java index e3811157f8..54775386fd 100644 --- a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java +++ b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java @@ -5,8 +5,8 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.util.Objects; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletRequestEvent; @@ -21,24 +21,24 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - private final IHub hub; + private final IScopes scopes; - public SentryServletRequestListener(@NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryServletRequestListener(@NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public SentryServletRequestListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - hub.popScope(); + scopes.popScope(); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - hub.pushScope(); + scopes.pushScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); if (servletRequest instanceof HttpServletRequest) { @@ -47,10 +47,10 @@ public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) final Hint hint = new Hint(); hint.set(SERVLET_REQUEST, httpRequest); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.http(httpRequest.getRequestURI(), httpRequest.getMethod()), hint); - hub.configureScope( + scopes.configureScope( scope -> { scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(httpRequest)); }); diff --git a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt index b87ea218a2..3be76d1cd2 100644 --- a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt +++ b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.servlet.jakarta import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import jakarta.servlet.ServletRequestEvent import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check @@ -13,9 +13,9 @@ import kotlin.test.assertEquals class SentryServletRequestListenerTest { private class Fixture { - val hub = mock() + val scopes = mock() val listener = - SentryServletRequestListener(hub) + SentryServletRequestListener(scopes) val request = mockRequest( url = "http://localhost:8080/some-uri", method = "POST" @@ -33,14 +33,14 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test fun `adds breadcrumb when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> assertEquals("/some-uri", it.getData("url")) assertEquals("POST", it.getData("method")) @@ -54,6 +54,6 @@ class SentryServletRequestListenerTest { fun `pops scope when request gets destroyed`() { fixture.listener.requestDestroyed(fixture.event) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-servlet/api/sentry-servlet.api b/sentry-servlet/api/sentry-servlet.api index a0a2a1e0d2..fd7aee819b 100644 --- a/sentry-servlet/api/sentry-servlet.api +++ b/sentry-servlet/api/sentry-servlet.api @@ -10,7 +10,7 @@ public class io/sentry/servlet/SentryServletContainerInitializer : javax/servlet public class io/sentry/servlet/SentryServletRequestListener : javax/servlet/ServletRequestListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljavax/servlet/ServletRequestEvent;)V public fun requestInitialized (Ljavax/servlet/ServletRequestEvent;)V } diff --git a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java index 9b981676c4..97c37e1133 100644 --- a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java +++ b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java @@ -5,8 +5,8 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.util.Objects; import javax.servlet.ServletRequest; import javax.servlet.ServletRequestEvent; @@ -21,24 +21,24 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - private final IHub hub; + private final IScopes scopes; - public SentryServletRequestListener(@NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryServletRequestListener(@NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public SentryServletRequestListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - hub.popScope(); + scopes.popScope(); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - hub.pushScope(); + scopes.pushScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); if (servletRequest instanceof HttpServletRequest) { @@ -47,10 +47,10 @@ public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) final Hint hint = new Hint(); hint.set(SERVLET_REQUEST, httpRequest); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.http(httpRequest.getRequestURI(), httpRequest.getMethod()), hint); - hub.configureScope( + scopes.configureScope( scope -> { scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(httpRequest)); }); diff --git a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt index b94e73f2ef..bfa216f738 100644 --- a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt +++ b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.servlet import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import org.assertj.core.api.Assertions.assertThat import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check @@ -14,8 +14,8 @@ import kotlin.test.Test class SentryServletRequestListenerTest { private class Fixture { - val hub = mock() - val listener = SentryServletRequestListener(hub) + val scopes = mock() + val listener = SentryServletRequestListener(scopes) val request = MockHttpServletRequest() val event = mock() @@ -32,14 +32,14 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test fun `adds breadcrumb when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") assertThat(it.getData("method")).isEqualTo("POST") @@ -53,6 +53,6 @@ class SentryServletRequestListenerTest { fun `pops scope when request gets destroyed`() { fixture.listener.requestDestroyed(fixture.event) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api b/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api index a392ff75ee..009036082e 100644 --- a/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api +++ b/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api @@ -62,7 +62,7 @@ public class io/sentry/spring/boot/jakarta/SentryProperties$Reactive { public class io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration { public fun ()V - public fun sentryWebExceptionHandler (Lio/sentry/IHub;)Lio/sentry/spring/jakarta/webflux/SentryWebExceptionHandler; + public fun sentryWebExceptionHandler (Lio/sentry/IScopes;)Lio/sentry/spring/jakarta/webflux/SentryWebExceptionHandler; } public class io/sentry/spring/boot/jakarta/graphql/SentryGraphqlAutoConfiguration { diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java index fbf3162280..b2c8df213c 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java @@ -3,10 +3,10 @@ import com.jakewharton.nopen.annotation.Open; import graphql.GraphQLError; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -116,7 +116,7 @@ static class HubConfiguration { } @Bean - public @NotNull IHub sentryHub( + public @NotNull IScopes sentryHub( final @NotNull List> optionsConfigurations, final @NotNull SentryProperties options, final @NotNull ObjectProvider gitProperties) { @@ -139,7 +139,7 @@ static class HubConfiguration { // here we make sure that only classes that extend throwable are set on this field options.getIgnoredExceptionsForType().removeIf(it -> !Throwable.class.isAssignableFrom(it)); Sentry.init(options); - return HubAdapter.getInstance(); + return ScopesAdapter.getInstance(); } @Configuration(proxyBeanMethods = false) @@ -239,7 +239,7 @@ static class SentrySecurityConfiguration { * HttpServletRequest#getUserPrincipal()}. If Spring Security is auto-configured, its order is * set to run after Spring Security. * - * @param hub the Sentry hub + * @param scopes the Sentry scopes * @param sentryProperties the Sentry properties * @param sentryUserProvider the user provider * @return {@link SentryUserFilter} registration bean @@ -247,11 +247,11 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnBean(SentryUserProvider.class) public @NotNull FilterRegistrationBean sentryUserFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryProperties sentryProperties, final @NotNull List sentryUserProvider) { final FilterRegistrationBean filter = new FilterRegistrationBean<>(); - filter.setFilter(new SentryUserFilter(hub, sentryUserProvider)); + filter.setFilter(new SentryUserFilter(scopes, sentryUserProvider)); filter.setOrder(resolveUserFilterOrder(sentryProperties)); return filter; } @@ -263,8 +263,8 @@ static class SentrySecurityConfiguration { } @Bean - public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IHub hub) { - return new SentryRequestResolver(hub); + public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IScopes scopes) { + return new SentryRequestResolver(scopes); } @Bean @@ -276,12 +276,12 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentrySpringFilter") public @NotNull FilterRegistrationBean sentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = new FilterRegistrationBean<>( - new SentrySpringFilter(hub, requestResolver, transactionNameProvider)); + new SentrySpringFilter(scopes, requestResolver, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE); return filter; } @@ -289,9 +289,10 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentryTracingFilter") public FilterRegistrationBean sentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = - new FilterRegistrationBean<>(new SentryTracingFilter(hub, transactionNameProvider)); + new FilterRegistrationBean<>(new SentryTracingFilter(scopes, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE + 1); // must run after SentrySpringFilter return filter; } @@ -300,11 +301,11 @@ public FilterRegistrationBean sentryTracingFilter( @ConditionalOnMissingBean @ConditionalOnClass(HandlerExceptionResolver.class) public @NotNull SentryExceptionResolver sentryExceptionResolver( - final @NotNull IHub sentryHub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final @NotNull SentryProperties options) { return new SentryExceptionResolver( - sentryHub, transactionNameProvider, options.getExceptionResolverOrder()); + scopes, transactionNameProvider, options.getExceptionResolverOrder()); } } @@ -354,8 +355,8 @@ static class SentrySpanPointcutAutoConfiguration {} @Open static class SentryPerformanceRestTemplateConfiguration { @Bean - public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hub) { - return new SentrySpanRestTemplateCustomizer(hub); + public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IScopes scopes) { + return new SentrySpanRestTemplateCustomizer(scopes); } } @@ -365,8 +366,8 @@ public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hu @Open static class SentrySpanRestClientConfiguration { @Bean - public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IHub hub) { - return new SentrySpanRestClientCustomizer(hub); + public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IScopes scopes) { + return new SentrySpanRestClientCustomizer(scopes); } } @@ -376,8 +377,8 @@ public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IHub hub) { @Open static class SentryPerformanceWebClientConfiguration { @Bean - public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IHub hub) { - return new SentrySpanWebClientCustomizer(hub); + public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IScopes scopes) { + return new SentrySpanWebClientCustomizer(scopes); } } diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java index 243a4d1f50..304fb6911e 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.client.RestClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanRestClientCustomizer implements RestClientCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestClientCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub, false); + public SentrySpanRestClientCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes, false); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java index c1ded006e8..4a0faa9875 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.ArrayList; import java.util.List; @@ -14,8 +14,8 @@ class SentrySpanRestTemplateCustomizer implements RestTemplateCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestTemplateCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub); + public SentrySpanRestTemplateCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java index afbe2eb6c4..d349ac4c6e 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientWebRequestFilter; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanWebClientCustomizer implements WebClientCustomizer { private final @NotNull SentrySpanClientWebRequestFilter filter; - public SentrySpanWebClientCustomizer(final @NotNull IHub hub) { - this.filter = new SentrySpanClientWebRequestFilter(hub); + public SentrySpanWebClientCustomizer(final @NotNull IScopes scopes) { + this.filter = new SentrySpanClientWebRequestFilter(scopes); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java index d1cda8b4d2..2bc8bc87ed 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java @@ -1,8 +1,8 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.spring.jakarta.webflux.SentryScheduleHook; import io.sentry.spring.jakarta.webflux.SentryWebExceptionHandler; import io.sentry.spring.jakarta.webflux.SentryWebFilter; @@ -28,7 +28,7 @@ /** Configures Sentry integration for Spring Webflux and Project Reactor. */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) -@ConditionalOnBean(IHub.class) +@ConditionalOnBean(IScopes.class) @ConditionalOnClass(Schedulers.class) @Open @ApiStatus.Experimental @@ -44,14 +44,14 @@ static class SentryWebfluxFilterThreadLocalAccessorConfiguration { * Configures a filter that sets up Sentry {@link IScope} for each request. * *

Makes use of newer reactor-core and context-propagation library feature - * ThreadLocalAccessor to propagate the Sentry hub. + * ThreadLocalAccessor to propagate the Sentry scopes. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) public @NotNull SentryWebFilterWithThreadLocalAccessor sentryWebFilterWithContextPropagation( - final @NotNull IHub hub) { + final @NotNull IScopes scopes) { Hooks.enableAutomaticContextPropagation(); - return new SentryWebFilterWithThreadLocalAccessor(hub); + return new SentryWebFilterWithThreadLocalAccessor(scopes); } } @@ -60,7 +60,7 @@ static class SentryWebfluxFilterThreadLocalAccessorConfiguration { @Open static class SentryWebfluxFilterConfiguration { - /** Configures hook that sets correct hub on the executing thread. */ + /** Configures hook that sets correct scopes on the executing thread. */ @Bean public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() { return args -> { @@ -71,15 +71,16 @@ static class SentryWebfluxFilterConfiguration { /** Configures a filter that sets up Sentry {@link IScope} for each request. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) - public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IHub hub) { - return new SentryWebFilter(hub); + public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IScopes scopes) { + return new SentryWebFilter(scopes); } } /** Configures exception handler that handles unhandled exceptions and sends them to Sentry. */ @Bean - public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler(final @NotNull IHub hub) { - return new SentryWebExceptionHandler(hub); + public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler( + final @NotNull IScopes scopes) { + return new SentryWebExceptionHandler(scopes); } static final class SentryLegacyFilterConfigurationCondition extends AnyNestedCondition { diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt index aecb1b4712..df0fe96cf0 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt @@ -5,7 +5,7 @@ import io.sentry.AsyncHttpTransportFactory import io.sentry.Breadcrumb import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.NoOpTransportFactory @@ -77,18 +77,18 @@ class SentryAutoConfigurationTest { .withConfiguration(AutoConfigurations.of(SentryAutoConfiguration::class.java, WebMvcAutoConfiguration::class.java)) @Test - fun `hub is not created when auto-configuration dsn is not set`() { + fun `scopes is not created when auto-configuration dsn is not set`() { contextRunner .run { - assertThat(it).doesNotHaveBean(IHub::class.java) + assertThat(it).doesNotHaveBean(IScopes::class.java) } } @Test - fun `hub is created when dsn is provided`() { + fun `scopes is created when dsn is provided`() { contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj") .run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } @@ -961,7 +961,7 @@ class SentryAutoConfigurationTest { } class CustomIntegration : Integration { - override fun register(hub: IHub, options: SentryOptions) {} + override fun register(scopes: IScopes, options: SentryOptions) {} } @Configuration(proxyBeanMethods = false) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt index f9f55ffede..9ba9bf57d2 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -36,18 +36,18 @@ import kotlin.test.assertTrue class SentrySpanRestClientCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restClientBuilder = RestClient.builder() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestClientCustomizer(hub) + internal val customizer = SentrySpanRestClientCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut( @@ -75,7 +75,7 @@ class SentrySpanRestClientCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restClientBuilder.apply { @@ -247,7 +247,7 @@ class SentrySpanRestClientCustomizerTest { .body("content") .retrieve() .toEntity(String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -269,7 +269,7 @@ class SentrySpanRestClientCustomizerTest { .toEntity(String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -287,7 +287,7 @@ class SentrySpanRestClientCustomizerTest { .body("content") .retrieve() .toEntity(String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -309,7 +309,7 @@ class SentrySpanRestClientCustomizerTest { .toEntity(String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt index db5b25de44..4ab3205b31 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,21 +37,21 @@ import kotlin.test.assertTrue class SentrySpanRestTemplateCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restTemplate = RestTemplateBuilder() .setConnectTimeout(Duration.ofSeconds(2)) .setReadTimeout(Duration.ofSeconds(2)) .build() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestTemplateCustomizer(hub) + internal val customizer = SentrySpanRestTemplateCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, includeMockServerInTracingOrigins: Boolean = true): RestTemplate { @@ -74,7 +74,7 @@ class SentrySpanRestTemplateCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restTemplate @@ -209,7 +209,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = true).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -227,7 +227,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = true, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -240,7 +240,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is not active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = false).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -258,7 +258,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = false, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt index 51f0a6cb3d..d3fb5f7d31 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt @@ -2,8 +2,8 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,10 +39,10 @@ class SentrySpanWebClientCustomizerTest { class Fixture { lateinit var sentryOptions: SentryOptions lateinit var scope: IScope - val hub = mock() + val scopes = mock() var mockServer = MockWebServer() lateinit var transaction: SentryTracer - private val customizer = SentrySpanWebClientCustomizer(hub) + private val customizer = SentrySpanWebClientCustomizer(scopes) fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, throwIOException: Boolean = false, includeMockServerInTracingOrigins: Boolean = true): WebClient { sentryOptions = SentryOptions().apply { @@ -54,9 +54,9 @@ class SentrySpanWebClientCustomizerTest { dsn = "http://key@localhost/proj" } scope = Scope(sentryOptions) - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) val webClientBuilder = WebClient.builder() customizer.customize(webClientBuilder) val webClient = webClientBuilder.build() @@ -64,7 +64,7 @@ class SentrySpanWebClientCustomizerTest { if (isTransactionActive) { val scope = Scope(sentryOptions) scope.transaction = transaction - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } val dispatcher: Dispatcher = object : Dispatcher() { @@ -236,7 +236,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -259,7 +259,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -279,7 +279,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -302,7 +302,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt index 4b8a0c26be..13a207d8eb 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.boot.jakarta.it -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.checkEvent @@ -60,7 +60,7 @@ class SentrySpringIntegrationTest { lateinit var transport: ITransport @SpyBean - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -188,7 +188,7 @@ class SentrySpringIntegrationTest { restTemplate.getForEntity("http://localhost:$port/throws-handled", String::class.java) - verify(hub, never()).captureEvent(any()) + verify(scopes, never()).captureEvent(any()) } @Test diff --git a/sentry-spring-boot/api/sentry-spring-boot.api b/sentry-spring-boot/api/sentry-spring-boot.api index 32d7cc8c60..d97e2e1111 100644 --- a/sentry-spring-boot/api/sentry-spring-boot.api +++ b/sentry-spring-boot/api/sentry-spring-boot.api @@ -53,8 +53,8 @@ public class io/sentry/spring/boot/SentryProperties$Logging { public class io/sentry/spring/boot/SentryWebfluxAutoConfiguration { public fun ()V public fun sentryScheduleHookApplicationRunner ()Lorg/springframework/boot/ApplicationRunner; - public fun sentryWebExceptionHandler (Lio/sentry/IHub;)Lio/sentry/spring/webflux/SentryWebExceptionHandler; - public fun sentryWebFilter (Lio/sentry/IHub;)Lio/sentry/spring/webflux/SentryWebFilter; + public fun sentryWebExceptionHandler (Lio/sentry/IScopes;)Lio/sentry/spring/webflux/SentryWebExceptionHandler; + public fun sentryWebFilter (Lio/sentry/IScopes;)Lio/sentry/spring/webflux/SentryWebFilter; } public class io/sentry/spring/boot/graphql/SentryGraphqlAutoConfiguration { diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java index edbfee271d..7d6a6a6fc4 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java @@ -3,10 +3,10 @@ import com.jakewharton.nopen.annotation.Open; import graphql.GraphQLError; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -114,7 +114,7 @@ static class HubConfiguration { } @Bean - public @NotNull IHub sentryHub( + public @NotNull IScopes sentryHub( final @NotNull List> optionsConfigurations, final @NotNull SentryProperties options, final @NotNull ObjectProvider gitProperties) { @@ -137,7 +137,7 @@ static class HubConfiguration { // here we make sure that only classes that extend throwable are set on this field options.getIgnoredExceptionsForType().removeIf(it -> !Throwable.class.isAssignableFrom(it)); Sentry.init(options); - return HubAdapter.getInstance(); + return ScopesAdapter.getInstance(); } @Configuration(proxyBeanMethods = false) @@ -237,7 +237,7 @@ static class SentrySecurityConfiguration { * HttpServletRequest#getUserPrincipal()}. If Spring Security is auto-configured, its order is * set to run after Spring Security. * - * @param hub the Sentry hub + * @param scopes the Sentry scopes * @param sentryProperties the Sentry properties * @param sentryUserProvider the user provider * @return {@link SentryUserFilter} registration bean @@ -245,11 +245,11 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnBean(SentryUserProvider.class) public @NotNull FilterRegistrationBean sentryUserFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryProperties sentryProperties, final @NotNull List sentryUserProvider) { final FilterRegistrationBean filter = new FilterRegistrationBean<>(); - filter.setFilter(new SentryUserFilter(hub, sentryUserProvider)); + filter.setFilter(new SentryUserFilter(scopes, sentryUserProvider)); filter.setOrder(resolveUserFilterOrder(sentryProperties)); return filter; } @@ -261,8 +261,8 @@ static class SentrySecurityConfiguration { } @Bean - public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IHub hub) { - return new SentryRequestResolver(hub); + public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IScopes scopes) { + return new SentryRequestResolver(scopes); } @Bean @@ -274,12 +274,12 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentrySpringFilter") public @NotNull FilterRegistrationBean sentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = new FilterRegistrationBean<>( - new SentrySpringFilter(hub, requestResolver, transactionNameProvider)); + new SentrySpringFilter(scopes, requestResolver, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE); return filter; } @@ -287,9 +287,10 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentryTracingFilter") public FilterRegistrationBean sentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = - new FilterRegistrationBean<>(new SentryTracingFilter(hub, transactionNameProvider)); + new FilterRegistrationBean<>(new SentryTracingFilter(scopes, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE + 1); // must run after SentrySpringFilter return filter; } @@ -298,11 +299,11 @@ public FilterRegistrationBean sentryTracingFilter( @ConditionalOnMissingBean @ConditionalOnClass(HandlerExceptionResolver.class) public @NotNull SentryExceptionResolver sentryExceptionResolver( - final @NotNull IHub sentryHub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final @NotNull SentryProperties options) { return new SentryExceptionResolver( - sentryHub, transactionNameProvider, options.getExceptionResolverOrder()); + scopes, transactionNameProvider, options.getExceptionResolverOrder()); } } @@ -348,8 +349,8 @@ static class SentrySpanPointcutAutoConfiguration {} @Open static class SentryPerformanceRestTemplateConfiguration { @Bean - public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hub) { - return new SentrySpanRestTemplateCustomizer(hub); + public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IScopes scopes) { + return new SentrySpanRestTemplateCustomizer(scopes); } } @@ -359,8 +360,8 @@ public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hu @Open static class SentryPerformanceWebClientConfiguration { @Bean - public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IHub hub) { - return new SentrySpanWebClientCustomizer(hub); + public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IScopes scopes) { + return new SentrySpanWebClientCustomizer(scopes); } } diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java index bd311c55f1..2a5e4f1be5 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.ArrayList; import java.util.List; @@ -14,8 +14,8 @@ class SentrySpanRestTemplateCustomizer implements RestTemplateCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestTemplateCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub); + public SentrySpanRestTemplateCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes); } @Override diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java index 0b8aa4055c..79e59f1cf0 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientWebRequestFilter; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanWebClientCustomizer implements WebClientCustomizer { private final @NotNull SentrySpanClientWebRequestFilter filter; - public SentrySpanWebClientCustomizer(final @NotNull IHub hub) { - this.filter = new SentrySpanClientWebRequestFilter(hub); + public SentrySpanWebClientCustomizer(final @NotNull IScopes scopes) { + this.filter = new SentrySpanClientWebRequestFilter(scopes); } @Override diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java index 3d507f1b6b..e7f6a444b8 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java @@ -1,8 +1,8 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.spring.webflux.SentryScheduleHook; import io.sentry.spring.webflux.SentryWebExceptionHandler; import io.sentry.spring.webflux.SentryWebFilter; @@ -21,14 +21,14 @@ /** Configures Sentry integration for Spring Webflux and Project Reactor. */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) -@ConditionalOnBean(IHub.class) +@ConditionalOnBean(IScopes.class) @ConditionalOnClass(Schedulers.class) @Open @ApiStatus.Experimental public class SentryWebfluxAutoConfiguration { private static final int SENTRY_SPRING_FILTER_PRECEDENCE = Ordered.HIGHEST_PRECEDENCE; - /** Configures hook that sets correct hub on the executing thread. */ + /** Configures hook that sets correct scopes on the executing thread. */ @Bean public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() { return args -> { @@ -39,13 +39,14 @@ public class SentryWebfluxAutoConfiguration { /** Configures a filter that sets up Sentry {@link IScope} for each request. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) - public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IHub hub) { - return new SentryWebFilter(hub); + public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IScopes scopes) { + return new SentryWebFilter(scopes); } /** Configures exception handler that handles unhandled exceptions and sends them to Sentry. */ @Bean - public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler(final @NotNull IHub hub) { - return new SentryWebExceptionHandler(hub); + public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler( + final @NotNull IScopes scopes) { + return new SentryWebExceptionHandler(scopes); } } diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt index c7fd177ec0..e1fe220aea 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt @@ -5,7 +5,7 @@ import io.sentry.AsyncHttpTransportFactory import io.sentry.Breadcrumb import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.NoOpTransportFactory @@ -76,18 +76,18 @@ class SentryAutoConfigurationTest { .withConfiguration(AutoConfigurations.of(SentryAutoConfiguration::class.java, WebMvcAutoConfiguration::class.java)) @Test - fun `hub is not created when auto-configuration dsn is not set`() { + fun `scopes is not created when auto-configuration dsn is not set`() { contextRunner .run { - assertThat(it).doesNotHaveBean(IHub::class.java) + assertThat(it).doesNotHaveBean(IScopes::class.java) } } @Test - fun `hub is created when dsn is provided`() { + fun `scopes is created when dsn is provided`() { contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj") .run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } @@ -943,7 +943,7 @@ class SentryAutoConfigurationTest { } class CustomIntegration : Integration { - override fun register(hub: IHub, options: SentryOptions) {} + override fun register(scopes: IScopes, options: SentryOptions) {} } @Configuration(proxyBeanMethods = false) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt index 0d675b6841..33d7974d8a 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,23 +37,23 @@ import kotlin.test.assertTrue class SentrySpanRestTemplateCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restTemplate = RestTemplateBuilder() .setConnectTimeout(Duration.ofSeconds(2)) .setReadTimeout(Duration.ofSeconds(2)) .build() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestTemplateCustomizer(hub) + internal val customizer = SentrySpanRestTemplateCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope( + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope( any() ) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, includeMockServerInTracingOrigins: Boolean = true): RestTemplate { @@ -76,7 +76,7 @@ class SentrySpanRestTemplateCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restTemplate @@ -211,7 +211,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = true).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -229,7 +229,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = true, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -242,7 +242,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is not active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = false).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -260,7 +260,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = false, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt index f925435fd3..4f1f70d75e 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt @@ -2,8 +2,8 @@ package io.sentry.spring.boot import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,10 +39,10 @@ class SentrySpanWebClientCustomizerTest { class Fixture { lateinit var sentryOptions: SentryOptions lateinit var scope: IScope - val hub = mock() + val scopes = mock() var mockServer = MockWebServer() lateinit var transaction: SentryTracer - private val customizer = SentrySpanWebClientCustomizer(hub) + private val customizer = SentrySpanWebClientCustomizer(scopes) fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, throwIOException: Boolean = false, includeMockServerInTracingOrigins: Boolean = true): WebClient { sentryOptions = SentryOptions().apply { @@ -54,11 +54,11 @@ class SentrySpanWebClientCustomizerTest { dsn = "http://key@localhost/proj" } scope = Scope(sentryOptions) - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope( + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope( any() ) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) val webClientBuilder = WebClient.builder() customizer.customize(webClientBuilder) val webClient = webClientBuilder.build() @@ -66,7 +66,7 @@ class SentrySpanWebClientCustomizerTest { if (isTransactionActive) { val scope = Scope(sentryOptions) scope.transaction = transaction - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } val dispatcher: Dispatcher = object : Dispatcher() { @@ -238,7 +238,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -261,7 +261,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -281,7 +281,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -304,7 +304,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt index eb6d159a7c..3bbcb2c3e7 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.boot.it -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.checkEvent @@ -60,7 +60,7 @@ class SentrySpringIntegrationTest { lateinit var transport: ITransport @SpyBean - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -188,7 +188,7 @@ class SentrySpringIntegrationTest { restTemplate.getForEntity("http://localhost:$port/throws-handled", String::class.java) - verify(hub, never()).captureEvent(any()) + verify(scopes, never()).captureEvent(any()) } @Test diff --git a/sentry-spring-jakarta/api/sentry-spring-jakarta.api b/sentry-spring-jakarta/api/sentry-spring-jakarta.api index cebbd26b4f..13eb6033f9 100644 --- a/sentry-spring-jakarta/api/sentry-spring-jakarta.api +++ b/sentry-spring-jakarta/api/sentry-spring-jakarta.api @@ -22,7 +22,7 @@ public final class io/sentry/spring/jakarta/HttpServletRequestSentryUserProvider public class io/sentry/spring/jakarta/SentryExceptionResolver : org/springframework/core/Ordered, org/springframework/web/servlet/HandlerExceptionResolver { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;I)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;I)V protected fun createEvent (Ljakarta/servlet/http/HttpServletRequest;Ljava/lang/Exception;)Lio/sentry/SentryEvent; protected fun createHint (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;)Lio/sentry/Hint; public fun getOrder ()I @@ -47,14 +47,14 @@ public class io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor : } public class io/sentry/spring/jakarta/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Ljakarta/servlet/http/HttpServletRequest;)Lio/sentry/protocol/Request; } public class io/sentry/spring/jakarta/SentrySpringFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/SentryRequestResolver;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/SentryRequestResolver;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V } @@ -69,7 +69,7 @@ public final class io/sentry/spring/jakarta/SentryTaskDecorator : org/springfram } public class io/sentry/spring/jakarta/SentryUserFilter : org/springframework/web/filter/OncePerRequestFilter { - public fun (Lio/sentry/IHub;Ljava/util/List;)V + public fun (Lio/sentry/IScopes;Ljava/util/List;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V public fun getSentryUserProviders ()Ljava/util/List; } @@ -96,7 +96,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/checkin/Sent public class io/sentry/spring/jakarta/checkin/SentryCheckInAdvice : org/aopalliance/intercept/MethodInterceptor, org/springframework/context/EmbeddedValueResolverAware { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; public fun setEmbeddedValueResolver (Lorg/springframework/util/StringValueResolver;)V } @@ -127,7 +127,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/exception/Se public class io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -169,7 +169,7 @@ public final class io/sentry/spring/jakarta/graphql/SentryDataFetcherExceptionRe public final class io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/spring/jakarta/graphql/SentryGraphqlBeanPostProcessor : org/springframework/beans/factory/config/BeanPostProcessor, org/springframework/core/PriorityOrdered { @@ -188,7 +188,7 @@ public class io/sentry/spring/jakarta/graphql/SentryGraphqlConfiguration { public final class io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public class io/sentry/spring/jakarta/tracing/SentryAdviceConfiguration { @@ -207,18 +207,18 @@ public abstract interface annotation class io/sentry/spring/jakarta/tracing/Sent public class io/sentry/spring/jakarta/tracing/SentrySpanAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } public class io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor : org/springframework/http/client/ClientHttpRequestInterceptor { - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Z)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Z)V public fun intercept (Lorg/springframework/http/HttpRequest;[BLorg/springframework/http/client/ClientHttpRequestExecution;)Lorg/springframework/http/client/ClientHttpResponse; } public class io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter : org/springframework/web/reactive/function/client/ExchangeFilterFunction { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/reactive/function/client/ClientRequest;Lorg/springframework/web/reactive/function/client/ExchangeFunction;)Lreactor/core/publisher/Mono; } @@ -233,8 +233,8 @@ public class io/sentry/spring/jakarta/tracing/SentryTracingConfiguration { public class io/sentry/spring/jakarta/tracing/SentryTracingFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V } @@ -246,7 +246,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/tracing/Sent public class io/sentry/spring/jakarta/tracing/SentryTransactionAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -268,21 +268,22 @@ public abstract interface class io/sentry/spring/jakarta/tracing/TransactionName public abstract class io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter : org/springframework/web/server/WebFilter { public static final field SENTRY_HUB_KEY Ljava/lang/String; - public fun (Lio/sentry/IHub;)V - protected fun doFinally (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IHub;Lio/sentry/ITransaction;)V - protected fun doFirst (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IHub;)V + public static final field SENTRY_SCOPES_KEY Ljava/lang/String; + public fun (Lio/sentry/IScopes;)V + protected fun doFinally (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IScopes;Lio/sentry/ITransaction;)V + protected fun doFirst (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IScopes;)V protected fun doOnError (Lio/sentry/ITransaction;Ljava/lang/Throwable;)V - protected fun maybeStartTransaction (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/ITransaction; - protected fun shouldTraceRequest (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Z - protected fun startTransaction (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; + protected fun maybeStartTransaction (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/ITransaction; + protected fun shouldTraceRequest (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Z + protected fun startTransaction (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; } public final class io/sentry/spring/jakarta/webflux/ReactorUtils { public fun ()V public static fun withSentry (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentry (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; - public static fun withSentryHub (Lreactor/core/publisher/Flux;Lio/sentry/IHub;)Lreactor/core/publisher/Flux; - public static fun withSentryHub (Lreactor/core/publisher/Mono;Lio/sentry/IHub;)Lreactor/core/publisher/Mono; + public static fun withSentryHub (Lreactor/core/publisher/Flux;Lio/sentry/IScopes;)Lreactor/core/publisher/Flux; + public static fun withSentryHub (Lreactor/core/publisher/Mono;Lio/sentry/IScopes;)Lreactor/core/publisher/Mono; public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; } @@ -290,16 +291,16 @@ public final class io/sentry/spring/jakarta/webflux/ReactorUtils { public final class io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor : io/micrometer/context/ThreadLocalAccessor { public static final field KEY Ljava/lang/String; public fun ()V - public fun getValue ()Lio/sentry/IHub; + public fun getValue ()Lio/sentry/IScopes; public synthetic fun getValue ()Ljava/lang/Object; public fun key ()Ljava/lang/Object; public fun reset ()V - public fun setValue (Lio/sentry/IHub;)V + public fun setValue (Lio/sentry/IScopes;)V public synthetic fun setValue (Ljava/lang/Object;)V } public class io/sentry/spring/jakarta/webflux/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/protocol/Request; } @@ -311,18 +312,18 @@ public final class io/sentry/spring/jakarta/webflux/SentryScheduleHook : java/ut public final class io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler : org/springframework/web/server/WebExceptionHandler { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun handle (Lorg/springframework/web/server/ServerWebExchange;Ljava/lang/Throwable;)Lreactor/core/publisher/Mono; } public class io/sentry/spring/jakarta/webflux/SentryWebFilter : io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } public final class io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor : io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter { public static final field TRACE_ORIGIN Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java index 1395e035a1..e8cd91f3f4 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java @@ -12,7 +12,7 @@ * *

    *
  • creates bean of type {@link io.sentry.SentryOptions} - *
  • registers {@link io.sentry.IHub} for sending Sentry events + *
  • registers {@link io.sentry.IScopes} for sending Sentry events *
  • registers {@link SentryExceptionResolver} to send Sentry event for any uncaught exception * in Spring MVC flow. *
diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java index 6d4098d624..efa98ef581 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java @@ -5,7 +5,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -29,15 +29,15 @@ public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered { public static final String MECHANISM_TYPE = "Spring6ExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull TransactionNameProvider transactionNameProvider; private final int order; public SentryExceptionResolver( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final int order) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); this.order = order; @@ -53,7 +53,7 @@ public SentryExceptionResolver( final SentryEvent event = createEvent(request, ex); final Hint hint = createHint(request, response); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); // null = run other HandlerExceptionResolvers to actually handle the exception return null; diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java index 4a8789c812..9598f0c926 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; import io.sentry.protocol.SdkVersion; @@ -60,7 +60,7 @@ private void registerSentryOptions( private void registerSentryHubBean(final @NotNull BeanDefinitionRegistry registry) { final BeanDefinitionBuilder builder = - BeanDefinitionBuilder.genericBeanDefinition(HubAdapter.class); + BeanDefinitionBuilder.genericBeanDefinition(ScopesAdapter.class); builder.setInitMethodName("getInstance"); registry.registerBeanDefinition("sentryHub", builder.getBeanDefinition()); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java index 9937bfbf1a..d33dfca8d8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java @@ -2,10 +2,10 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryOptions; import io.sentry.SentryOptions.TracesSamplerCallback; @@ -27,15 +27,15 @@ public class SentryInitBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, DisposableBean { private @Nullable ApplicationContext applicationContext; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryInitBeanPostProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryInitBeanPostProcessor(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.hub = hub; + SentryInitBeanPostProcessor(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "Scopes are required"); + this.scopes = scopes; } @Override @@ -86,6 +86,6 @@ public void setApplicationContext(final @NotNull ApplicationContext applicationC @Override public void destroy() { - hub.close(); + scopes.close(); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java index e1bc45ac64..71f9079f9f 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; @@ -20,11 +20,11 @@ @Open public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private volatile @Nullable List extraSecurityCookies; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "options is required"); } // httpRequest.getRequestURL() returns StringBuffer which is considered an obsolete class. @@ -40,7 +40,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { extractSecurityCookieNamesOrUseCached(httpRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest, additionalSecurityCookieNames)); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String cookieName = HttpUtils.COOKIE_HEADER_NAME; final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( @@ -57,7 +57,8 @@ Map resolveHeadersMap( final Map headersMap = new HashMap<>(); for (String headerName : Collections.list(request.getHeaderNames())) { // do not copy personal information identifiable headers - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( request.getHeaders(headerName), headerName, additionalSecurityCookieNames); @@ -94,7 +95,8 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to extract session cookie name from request.", t); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index 129ea739d6..be06d3d253 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -8,8 +8,8 @@ import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -29,26 +29,26 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; public SentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentrySpringFilter(final @NotNull IHub hub) { - this(hub, new SentryRequestResolver(hub), new SpringMvcTransactionNameProvider()); + public SentrySpringFilter(final @NotNull IScopes scopes) { + this(scopes, new SentryRequestResolver(scopes), new SpringMvcTransactionNameProvider()); } public SentrySpringFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override @@ -57,20 +57,20 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - hub.pushScope(); + scopes.pushScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - hub.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); configureScope(request); filterChain.doFilter(request, response); } finally { - hub.popScope(); + scopes.popScope(); } } else { filterChain.doFilter(servletRequest, response); @@ -79,7 +79,7 @@ protected void doFilterInternal( private void configureScope(HttpServletRequest request) { try { - hub.configureScope( + scopes.configureScope( scope -> { // set basic request information on the scope scope.setRequest(requestResolver.resolveSentryRequest(request)); @@ -92,11 +92,12 @@ private void configureScope(HttpServletRequest request) { // request processing if (request instanceof CachedBodyHttpServletRequest) { scope.addEventProcessor( - new RequestBodyExtractingEventProcessor(request, hub.getOptions())); + new RequestBodyExtractingEventProcessor(request, scopes.getOptions())); } }); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Failed to set scope for HTTP request", e); } @@ -104,12 +105,13 @@ private void configureScope(HttpServletRequest request) { private @NotNull HttpServletRequest resolveHttpServletRequest( final @NotNull HttpServletRequest request) { - if (hub.getOptions().isSendDefaultPii() - && qualifiesForCaching(request, hub.getOptions().getMaxRequestBodySize())) { + if (scopes.getOptions().isSendDefaultPii() + && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { return new CachedBodyHttpServletRequest(request); } catch (IOException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java index 5c4f37e521..c99abf3e21 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -8,22 +8,24 @@ import org.springframework.scheduling.annotation.Async; /** - * Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate - * the current {@link IHub} on the thread executing async task - like MVC controller methods - * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. + * Sets a current scopes on a thread running a {@link Runnable} given by parameter. Used to + * propagate the current {@link IScopes} on the thread executing async task - like MVC controller + * methods returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override + @SuppressWarnings("deprecation") public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java index f7b8bc62d6..31cc73a346 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.IpAddressUtils; import io.sentry.protocol.User; import io.sentry.util.Objects; @@ -26,12 +26,12 @@ */ @Open public class SentryUserFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull List sentryUserProviders; public SentryUserFilter( - final @NotNull IHub hub, final @NotNull List sentryUserProviders) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull List sentryUserProviders) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.sentryUserProviders = Objects.requireNonNull(sentryUserProviders, "sentryUserProviders list is required"); } @@ -46,13 +46,13 @@ protected void doFilterInternal( for (final SentryUserProvider provider : sentryUserProviders) { apply(user, provider.provideUser()); } - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { if (IpAddressUtils.isDefault(user.getIpAddress())) { // unset {{auto}} as it would set the server's ip address as a user ip address user.setIpAddress(null); } } - hub.setUser(user); + scopes.setUser(user); chain.doFilter(request, response); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index 5a4e329fa8..4a366a8b01 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -4,8 +4,8 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; @@ -30,16 +30,16 @@ @ApiStatus.Experimental @Open public class SentryCheckInAdvice implements MethodInterceptor, EmbeddedValueResolverAware { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @Nullable StringValueResolver resolver; public SentryCheckInAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCheckInAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCheckInAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -66,7 +66,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl // expressions. Testing shows this can also happen if properties cannot be resolved (without // an exception being thrown). Sentry should alert the user about missed checkins in this // case since the monitor slug won't match what is configured in Sentry. - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -76,7 +77,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } if (ObjectUtils.isEmpty(monitorSlug)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -84,8 +86,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); @@ -93,7 +95,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { if (!isHeartbeatOnly) { - checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); } return invocation.proceed(); } catch (Throwable e) { @@ -103,8 +105,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java index f989382045..c6537f853c 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.exception; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.exception.ExceptionMechanismException; import io.sentry.protocol.Mechanism; import io.sentry.util.Objects; @@ -22,14 +22,14 @@ @Open public class SentryCaptureExceptionParameterAdvice implements MethodInterceptor { private static final String MECHANISM_TYPE = "SentrySpring6CaptureExceptionParameterAdvice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryCaptureExceptionParameterAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -58,6 +58,6 @@ private void captureException(final @NotNull Throwable throwable) { mechanism.setHandled(true); final Throwable mechanismException = new ExceptionMechanismException(mechanism, throwable, Thread.currentThread()); - hub.captureException(mechanismException); + scopes.captureException(mechanismException); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java index 1d5576c596..3e2223b694 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java @@ -1,11 +1,11 @@ package io.sentry.spring.jakarta.graphql; -import static io.sentry.graphql.SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY; +import static io.sentry.graphql.SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY; import graphql.GraphQLContext; import io.sentry.Breadcrumb; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import java.util.List; import java.util.Map; import java.util.Set; @@ -89,7 +89,7 @@ public BatchLoaderRegistry.RegistrationSpec withOptions(DataLoaderOptions public void registerBatchLoader(BiFunction, BatchLoaderEnvironment, Flux> loader) { delegate.registerBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); @@ -100,20 +100,20 @@ public void registerMappedBatchLoader( BiFunction, BatchLoaderEnvironment, Mono>> loader) { delegate.registerMappedBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); } - private @NotNull IHub hubFromContext(final @NotNull BatchLoaderEnvironment environment) { + private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) { Object context = environment.getContext(); if (context instanceof GraphQLContext) { GraphQLContext graphqlContext = (GraphQLContext) context; - return graphqlContext.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java index 83a090954d..a7a6cccd3e 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; @@ -17,7 +17,7 @@ public SentryDgsSubscriptionHandler() { @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -25,7 +25,7 @@ public SentryDgsSubscriptionHandler() { return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); exceptionReporter.captureThrowable(throwable, exceptionDetails, null); }); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java index 4c51981035..eec86f5e8b 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; import org.jetbrains.annotations.NotNull; @@ -13,7 +13,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -21,7 +21,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); if (throwable instanceof SubscriptionPublisherException && throwable.getCause() != null) { exceptionReporter.captureThrowable(throwable.getCause(), exceptionDetails, null); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java index 5f345a4e02..e8de36487f 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.jakarta.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.util.Objects; import java.lang.reflect.Method; @@ -22,20 +22,20 @@ @Open public class SentrySpanAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring_jakarta.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentrySpanAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @Override public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null || activeSpan.isNoOp()) { // there is no active transaction, we do not start new span diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java index 59c1926ff3..7a787fb29d 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java @@ -8,7 +8,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -28,16 +28,16 @@ public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private static final String TRACE_ORIGIN_REST_TEMPLATE = "auto.http.spring_jakarta.resttemplate"; private static final String TRACE_ORIGIN_REST_CLIENT = "auto.http.spring_jakarta.restclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull String traceOrigin; - public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { - this(hub, true); + public SentrySpanClientHttpRequestInterceptor(final @NotNull IScopes scopes) { + this(scopes, true); } public SentrySpanClientHttpRequestInterceptor( - final @NotNull IHub hub, final @NotNull boolean isRestTemplate) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull boolean isRestTemplate) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.traceOrigin = isRestTemplate ? TRACE_ORIGIN_REST_TEMPLATE : TRACE_ORIGIN_REST_CLIENT; } @@ -50,7 +50,7 @@ public SentrySpanClientHttpRequestInterceptor( Integer responseStatusCode = null; ClientHttpResponse response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { maybeAddTracingHeaders(request, null); return execution.execute(request, body); @@ -91,7 +91,7 @@ private void maybeAddTracingHeaders( final @NotNull HttpRequest request, final @Nullable ISpan span) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.getURI().toString(), request.getHeaders().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -128,6 +128,6 @@ private void addBreadcrumb( hint.set(SPRING_REQUEST_INTERCEPTOR_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java index 4ac511721e..bc5c0edfab 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java @@ -7,7 +7,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -25,16 +25,16 @@ @Open public class SentrySpanClientWebRequestFilter implements ExchangeFilterFunction { private static final String TRACE_ORIGIN = "auto.http.spring_jakarta.webclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientWebRequestFilter(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); } @Override public @NotNull Mono filter( final @NotNull ClientRequest request, final @NotNull ExchangeFunction next) { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { final @NotNull ClientRequest modifiedRequest = maybeAddTracingHeaders(request, null); addBreadcrumb(modifiedRequest, null); @@ -74,7 +74,7 @@ public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url().toString(), request.headers().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -111,6 +111,6 @@ private void addBreadcrumb( hint.set(SPRING_EXCHANGE_FILTER_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java index ed91f85a29..097528ac44 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java @@ -3,9 +3,9 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.BaggageHeader; import io.sentry.CustomSamplingContext; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; import io.sentry.TransactionContext; @@ -38,7 +38,7 @@ public class SentryTracingFilter extends OncePerRequestFilter { private static final String TRACE_ORIGIN = "auto.http.spring_jakarta.webmvc"; private final @NotNull TransactionNameProvider transactionNameProvider; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; /** * Creates filter that resolves transaction name using {@link SpringMvcTransactionNameProvider}. @@ -49,25 +49,26 @@ public class SentryTracingFilter extends OncePerRequestFilter { * jakarta.servlet.Filter}. */ public SentryTracingFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } /** * Creates filter that resolves transaction name using transaction name provider given by * parameter. * - * @param hub - the hub + * @param scopes - the scopes * @param transactionNameProvider - transaction name provider. */ public SentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentryTracingFilter(final @NotNull IHub hub) { - this(hub, new SpringMvcTransactionNameProvider()); + public SentryTracingFilter(final @NotNull IScopes scopes) { + this(scopes, new SpringMvcTransactionNameProvider()); } @Override @@ -76,14 +77,14 @@ protected void doFilterInternal( final @NotNull HttpServletResponse httpResponse, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { final @Nullable String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER)); final @Nullable TransactionContext transactionContext = - hub.continueTrace(sentryTraceHeader, baggageHeader); - if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { + scopes.continueTrace(sentryTraceHeader, baggageHeader); + if (scopes.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext); } else { filterChain.doFilter(httpRequest, httpResponse); @@ -130,7 +131,7 @@ private void doFilterWithTransaction( } private boolean shouldTraceRequest(final @NotNull HttpServletRequest request) { - return hub.getOptions().isTraceOptionsRequests() + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.name().equals(request.getMethod()); } @@ -152,14 +153,14 @@ private ITransaction startTransaction( transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, "http.server"), transactionOptions); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java index 5b264defa4..f04b3dd7a6 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.jakarta.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.TransactionContext; import io.sentry.TransactionOptions; @@ -28,14 +28,14 @@ public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring_jakarta.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryTransactionAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryTransactionAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryTransactionAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -68,11 +68,11 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - hub.pushScope(); + scopes.pushScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(nameAndSource.name, nameAndSource.source, operation), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN); @@ -86,7 +86,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - hub.popScope(); + scopes.popScope(); } } } @@ -106,7 +106,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } private boolean isTransactionActive() { - return hub.getSpan() != null; + return scopes.getSpan() != null; } private static class TransactionNameAndSource { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java index bf25aa5f49..3321874dd8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java @@ -7,10 +7,10 @@ import io.sentry.Breadcrumb; import io.sentry.CustomSamplingContext; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; @@ -34,16 +34,17 @@ @ApiStatus.Experimental public abstract class AbstractSentryWebFilter implements WebFilter { private final @NotNull SentryRequestResolver sentryRequestResolver; - public static final String SENTRY_HUB_KEY = "sentry-hub"; + public static final String SENTRY_SCOPES_KEY = "sentry-scopes"; + @Deprecated public static final String SENTRY_HUB_KEY = SENTRY_SCOPES_KEY; private static final String TRANSACTION_OP = "http.server"; - public AbstractSentryWebFilter(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.sentryRequestResolver = new SentryRequestResolver(hub); + public AbstractSentryWebFilter(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.sentryRequestResolver = new SentryRequestResolver(scopes); } protected @Nullable ITransaction maybeStartTransaction( - final @NotNull IHub requestHub, final @NotNull ServerHttpRequest request) { + final @NotNull IScopes requestHub, final @NotNull ServerHttpRequest request) { if (requestHub.isEnabled()) { final @NotNull HttpHeaders headers = request.getHeaders(); final @Nullable String sentryTraceHeader = @@ -62,21 +63,23 @@ public AbstractSentryWebFilter(final @NotNull IHub hub) { protected void doFinally( final @NotNull ServerWebExchange serverWebExchange, - final @NotNull IHub requestHub, + final @NotNull IScopes requestHub, final @Nullable ITransaction transaction) { if (transaction != null) { finishTransaction(serverWebExchange, transaction); } if (requestHub.isEnabled()) { + // TODO close lifecycle token instead of popscope requestHub.popScope(); } - Sentry.setCurrentHub(NoOpHub.getInstance()); + Sentry.setCurrentScopes(NoOpScopes.getInstance()); } protected void doFirst( - final @NotNull ServerWebExchange serverWebExchange, final @NotNull IHub requestHub) { + final @NotNull ServerWebExchange serverWebExchange, final @NotNull IScopes requestHub) { if (requestHub.isEnabled()) { - serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); + // TODO fork instead requestHub.pushScope(); final ServerHttpRequest request = serverWebExchange.getRequest(); final ServerHttpResponse response = serverWebExchange.getResponse(); @@ -100,8 +103,8 @@ protected void doOnError(final @Nullable ITransaction transaction, final @NotNul } protected boolean shouldTraceRequest( - final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { - return hub.getOptions().isTraceOptionsRequests() + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request) { + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.equals(request.getMethod()); } @@ -130,7 +133,7 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact } protected @NotNull ITransaction startTransaction( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request, final @Nullable TransactionContext transactionContext) { final @NotNull String name = request.getMethod() + " " + request.getURI().getPath(); @@ -146,10 +149,10 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact transactionContext.setTransactionNameSource(TransactionNameSource.URL); transactionContext.setOperation(TRANSACTION_OP); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, TRANSACTION_OP), transactionOptions); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java index 4af641af29..41dd2e4bc0 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -8,11 +8,12 @@ import reactor.core.publisher.Mono; import reactor.util.context.Context; +// TODO deprecate and replace with "withSentryScopes" etc. @ApiStatus.Experimental public final class ReactorUtils { /** - * Writes the current Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the current Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -20,14 +21,16 @@ public final class ReactorUtils { * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental + @SuppressWarnings("deprecation") public static Mono withSentry(final @NotNull Mono mono) { - final @NotNull IHub oldHub = Sentry.getCurrentHub(); - final @NotNull IHub clonedHub = oldHub.clone(); + final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); + // TODO fork + final @NotNull IScopes clonedHub = oldHub.clone(); return withSentryHub(mono, clonedHub); } /** - * Writes a new Sentry {@link IHub} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -36,12 +39,12 @@ public static Mono withSentry(final @NotNull Mono mono) { */ @ApiStatus.Experimental public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) { - final @NotNull IHub hub = Sentry.cloneMainHub(); + final @NotNull IScopes hub = Sentry.cloneMainHub(); return withSentryHub(mono, hub); } /** - * Writes the given Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the given Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -49,7 +52,7 @@ public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental - public static Mono withSentryHub(final @NotNull Mono mono, final @NotNull IHub hub) { + public static Mono withSentryHub(final @NotNull Mono mono, final @NotNull IScopes hub) { /** * WARNING: Cannot set the hub as current. It would be used by others to clone again causing * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in @@ -62,7 +65,7 @@ public static Mono withSentryHub(final @NotNull Mono mono, final @NotN } /** - * Writes the current Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the current Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -70,15 +73,17 @@ public static Mono withSentryHub(final @NotNull Mono mono, final @NotN * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental + @SuppressWarnings("deprecation") public static Flux withSentry(final @NotNull Flux flux) { - final @NotNull IHub oldHub = Sentry.getCurrentHub(); - final @NotNull IHub clonedHub = oldHub.clone(); + final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); + // TODO fork + final @NotNull IScopes clonedHub = oldHub.clone(); return withSentryHub(flux, clonedHub); } /** - * Writes a new Sentry {@link IHub} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -87,12 +92,12 @@ public static Flux withSentry(final @NotNull Flux flux) { */ @ApiStatus.Experimental public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) { - final @NotNull IHub hub = Sentry.cloneMainHub(); + final @NotNull IScopes hub = Sentry.cloneMainHub(); return withSentryHub(flux, hub); } /** - * Writes the given Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the given Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -100,7 +105,7 @@ public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental - public static Flux withSentryHub(final @NotNull Flux flux, final @NotNull IHub hub) { + public static Flux withSentryHub(final @NotNull Flux flux, final @NotNull IScopes hub) { /** * WARNING: Cannot set the hub as current. It would be used by others to clone again causing * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java index c64cf3d634..9b7e51db73 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java @@ -1,15 +1,15 @@ package io.sentry.spring.jakarta.webflux; import io.micrometer.context.ThreadLocalAccessor; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @ApiStatus.Experimental -public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor { +public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor { - public static final String KEY = "sentry-hub"; + public static final String KEY = "sentry-scopes"; @Override public Object key() { @@ -17,18 +17,18 @@ public Object key() { } @Override - public IHub getValue() { - return Sentry.getCurrentHub(); + public IScopes getValue() { + return Sentry.getCurrentScopes(); } @Override - public void setValue(IHub value) { - Sentry.setCurrentHub(value); + public void setValue(IScopes value) { + Sentry.setCurrentScopes(value); } @Override @SuppressWarnings("deprecation") public void reset() { - Sentry.setCurrentHub(NoOpHub.getInstance()); + Sentry.setCurrentScopes(NoOpScopes.getInstance()); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java index 2f41ba93ca..d58291ade6 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; @@ -20,10 +20,10 @@ @Open @ApiStatus.Experimental public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public @NotNull Request resolveSentryRequest(final @NotNull ServerHttpRequest httpRequest) { @@ -36,7 +36,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { urlDetails.applyToRequest(sentryRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest.getHeaders())); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String headerName = HttpUtils.COOKIE_HEADER_NAME; sentryRequest.setCookies( toString( @@ -52,7 +52,8 @@ Map resolveHeadersMap(final HttpHeaders request) { for (Map.Entry> entry : request.entrySet()) { // do not copy personal information identifiable headers String headerName = entry.getKey(); - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { headersMap.put( headerName, toString( diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java index 2bf27f246d..882a0b268a 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -13,16 +13,18 @@ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override + @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java index 24439cd0e9..40b0ed4e87 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java @@ -5,7 +5,7 @@ import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_RESPONSE; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -27,18 +27,18 @@ @ApiStatus.Experimental public final class SentryWebExceptionHandler implements WebExceptionHandler { public static final String MECHANISM_TYPE = "Spring6WebFluxExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryWebExceptionHandler(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryWebExceptionHandler(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override public @NotNull Mono handle( final @NotNull ServerWebExchange serverWebExchange, final @NotNull Throwable ex) { - final @Nullable IHub requestHub = - serverWebExchange.getAttributeOrDefault(SentryWebFilter.SENTRY_HUB_KEY, null); - final @NotNull IHub hubToUse = requestHub != null ? requestHub : hub; + final @Nullable IScopes requestScopes = + serverWebExchange.getAttributeOrDefault(SentryWebFilter.SENTRY_SCOPES_KEY, null); + final @NotNull IScopes scopesToUse = requestScopes != null ? requestScopes : scopes; return ReactorUtils.withSentryHub( Mono.just(ex) @@ -61,12 +61,12 @@ public SentryWebExceptionHandler(final @NotNull IHub hub) { WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse()); hint.set(WEBFLUX_EXCEPTION_HANDLER_EXCHANGE, serverWebExchange); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } return it; }), - hubToUse) + scopesToUse) .flatMap(it -> Mono.error(ex)); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java index a57a389499..dab985eecf 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @@ -20,28 +20,28 @@ public class SentryWebFilter extends AbstractSentryWebFilter { private static final String TRACE_ORIGIN = "auto.spring_jakarta.webflux"; - public SentryWebFilter(final @NotNull IHub hub) { - super(hub); + public SentryWebFilter(final @NotNull IScopes scopes) { + super(scopes); } @Override public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IHub requestHub = Sentry.cloneMainHub(); + @NotNull IScopes requestScopes = Sentry.cloneMainHub(); final ServerHttpRequest request = serverWebExchange.getRequest(); - final @Nullable ITransaction transaction = maybeStartTransaction(requestHub, request); + final @Nullable ITransaction transaction = maybeStartTransaction(requestScopes, request); if (transaction != null) { transaction.getSpanContext().setOrigin(TRACE_ORIGIN); } return webFilterChain .filter(serverWebExchange) - .doFinally(__ -> doFinally(serverWebExchange, requestHub, transaction)) + .doFinally(__ -> doFinally(serverWebExchange, requestScopes, transaction)) .doOnError(e -> doOnError(transaction, e)) .doFirst( () -> { - Sentry.setCurrentHub(requestHub); - doFirst(serverWebExchange, requestHub); + Sentry.setCurrentScopes(requestScopes); + doFirst(serverWebExchange, requestScopes); }); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java index 278c2b8e7e..e760ef8f3e 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @@ -17,8 +17,8 @@ public final class SentryWebFilterWithThreadLocalAccessor extends AbstractSentry public static final String TRACE_ORIGIN = "auto.spring_jakarta.webflux"; - public SentryWebFilterWithThreadLocalAccessor(final @NotNull IHub hub) { - super(hub); + public SentryWebFilterWithThreadLocalAccessor(final @NotNull IScopes scopes) { + super(scopes); } @Override @@ -33,14 +33,15 @@ public Mono filter( __ -> doFinally( serverWebExchange, - Sentry.getCurrentHub(), + Sentry.getCurrentScopes(), transactionContainer.transaction)) .doOnError(e -> doOnError(transactionContainer.transaction, e)) .doFirst( () -> { - doFirst(serverWebExchange, Sentry.getCurrentHub()); + doFirst(serverWebExchange, Sentry.getCurrentScopes()); final ITransaction transaction = - maybeStartTransaction(Sentry.getCurrentHub(), serverWebExchange.getRequest()); + maybeStartTransaction( + Sentry.getCurrentScopes(), serverWebExchange.getRequest()); transactionContainer.transaction = transaction; if (transaction != null) { transaction.getSpanContext().setOrigin(TRACE_ORIGIN); diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt index 7b51bbc1e7..37853c3101 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.EventProcessor -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.Sentry @@ -67,7 +67,7 @@ class EnableSentryTest { @Test fun `creates Sentry Hub`() { contextRunner.run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt index 05b97ba24c..5d093f50f1 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.jakarta import io.sentry.CheckIn import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -54,19 +54,19 @@ class SentryCheckInAdviceTest { lateinit var sampleServiceSpringProperties: SampleServiceSpringProperties @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.options).thenReturn(SentryOptions()) + reset(scopes) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleService.hello() assertEquals(1, result) assertEquals(2, checkInCaptor.allValues.size) @@ -79,17 +79,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleService.oops() } @@ -103,17 +103,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceHeartbeat.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -123,17 +123,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleServiceHeartbeat.oops() } @@ -144,31 +144,31 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn but slug is missing, does not create check-in`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(hub, never()).pushScope() - verify(hub, never()).captureCheckIn(any()) - verify(hub, never()).popScope() + verify(scopes, never()).pushScope() + verify(scopes, never()).captureCheckIn(any()) + verify(scopes, never()).popScope() } @Test fun `when @SentryCheckIn is passed a spring property it is resolved correctly`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -178,17 +178,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that does not exist, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloUnresolvedProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -198,17 +198,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that causes an exception, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloExceptionProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -218,10 +218,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Configuration @@ -242,10 +242,10 @@ class SentryCheckInAdviceTest { open fun sampleServiceSpringProperties() = SampleServiceSpringProperties() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } companion object { diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt index 797d5aa5ac..431137aabd 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.exception.ExceptionMechanismException @@ -17,7 +17,7 @@ import org.mockito.kotlin.whenever import kotlin.test.Test class SentryExceptionResolverTest { - private val hub = mock() + private val scopes = mock() private val transactionNameProvider = mock() private val request = mock() @@ -26,10 +26,10 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets wrapped exception for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) val expectedCause = RuntimeException("test") - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, expectedCause) assertThat(eventCaptor.firstValue.throwable).isEqualTo(expectedCause) @@ -46,9 +46,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets fatal level for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.level).isEqualTo(SentryLevel.FATAL) @@ -59,9 +59,9 @@ class SentryExceptionResolverTest { val expectedTransactionName = "test-transaction" whenever(transactionNameProvider.provideTransactionName(any())).thenReturn(expectedTransactionName) val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.transaction).isEqualTo(expectedTransactionName) @@ -71,9 +71,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, provides spring resolver hint`() { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) with(hintCaptor.firstValue) { @@ -86,8 +86,8 @@ class SentryExceptionResolverTest { fun `when custom create event method provided, uses it to capture event`() { val expectedEvent = SentryEvent() val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createEvent(request: HttpServletRequest, ex: Exception) = expectedEvent } @@ -100,8 +100,8 @@ class SentryExceptionResolverTest { fun `when custom create hint method provided, uses it to capture event`() { val expectedHint = Hint() val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createHint(request: HttpServletRequest, response: HttpServletResponse) = expectedHint } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt index 4168605823..54b6acb0f3 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -13,18 +13,18 @@ class SentryInitBeanPostProcessorTest { @Test fun closesSentryOnApplicationContextDestroy() { val ctx = AnnotationConfigApplicationContext(TestConfig::class.java) - val hub = ctx.getBean(IHub::class.java) + val scopes = ctx.getBean(IScopes::class.java) ctx.close() - verify(hub).close() + verify(scopes).close() } @Configuration open class TestConfig { @Bean(destroyMethod = "") - open fun hub() = mock() + open fun scopes() = mock() @Bean - open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(hub()) + open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(scopes()) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt index 279abce417..8faa243f83 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryOptions import io.sentry.spring.jakarta.tracing.SpringMvcTransactionNameProvider @@ -19,10 +19,10 @@ import kotlin.test.assertNotNull class SentryRequestHttpServletRequestProcessorTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut(request: HttpServletRequest, options: SentryOptions = SentryOptions()): SentryRequestHttpServletRequestProcessor { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return SentryRequestHttpServletRequestProcessor(SpringMvcTransactionNameProvider(), request) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt index 4e1bbb0ee5..b6bce77a0b 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val response = MockHttpServletResponse() val chain = mock() lateinit var scope: IScope @@ -45,15 +45,15 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) - whenever(hub.options).thenReturn(options) - whenever(hub.isEnabled).thenReturn(true) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + whenever(scopes.isEnabled).thenReturn(true) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(hub) + return SentrySpringFilter(scopes) } } @@ -64,7 +64,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test @@ -72,7 +72,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> Assertions.assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") Assertions.assertThat(it.getData("method")).isEqualTo("POST") @@ -87,7 +87,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } @Test @@ -99,7 +99,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt index aa809259d1..e5f8704b49 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt @@ -32,28 +32,28 @@ class SentryTaskDecoratorTest { val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt index 2f128cc4bb..b30dc937e5 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.protocol.User import jakarta.servlet.FilterChain @@ -16,7 +16,7 @@ import kotlin.test.assertNull class SentryUserFilterTest { class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -25,8 +25,8 @@ class SentryUserFilterTest { val options = SentryOptions().apply { this.isSendDefaultPii = isSendDefaultPii } - whenever(hub.options).thenReturn(options) - return SentryUserFilter(hub, userProviders) + whenever(scopes.options).thenReturn(options) + return SentryUserFilter(scopes, userProviders) } } @@ -52,7 +52,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -72,7 +72,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -92,7 +92,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -118,7 +118,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(mapOf("key" to "value", "new-key" to "new-value"), it.others) } @@ -140,7 +140,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals("192.168.0.1", it.ipAddress) } @@ -162,7 +162,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertNull(it.ipAddress) } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt index 0da5047251..3f8371ca3d 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.exception import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.exception.ExceptionMechanismException import org.junit.runner.RunWith @@ -30,18 +30,18 @@ class SentryCaptureExceptionParameterAdviceTest { lateinit var sampleService: SampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) + reset(scopes) } @Test fun `captures exception passed to method annotated with @SentryCaptureException`() { val exception = RuntimeException("test exception") sampleService.methodTakingAnException(exception) - verify(hub).captureException( + verify(scopes).captureException( check { assertTrue(it is ExceptionMechanismException) assertEquals(exception, it.throwable) @@ -60,10 +60,10 @@ class SentryCaptureExceptionParameterAdviceTest { open fun sampleService() = SampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt index 3f71eaf23e..c2ebb95e48 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchPar import graphql.language.Document import graphql.language.OperationDefinition import graphql.schema.DataFetchingEnvironment -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.graphql.ExceptionReporter import io.sentry.spring.jakarta.graphql.SentrySpringSubscriptionHandler import org.junit.jupiter.api.assertThrows @@ -24,7 +24,7 @@ class SentrySpringSubscriptionHandlerTest { @Test fun `reports exception`() { val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -33,7 +33,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -42,7 +42,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() @@ -53,7 +53,7 @@ class SentrySpringSubscriptionHandlerTest { fun `unwraps SubscriptionPublisherException and reports cause`() { val exception = IllegalStateException("some exception") val wrappedException = SubscriptionPublisherException(emptyList(), exception) - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -62,7 +62,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -71,7 +71,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt index f472a75a65..0b4be869d0 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.mvc -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.SentryOptions @@ -104,7 +104,7 @@ class SentrySpringIntegrationTest { lateinit var anotherService: AnotherService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -260,7 +260,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -276,7 +276,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodWithInnerSpanThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -370,20 +370,20 @@ open class App { open fun springSecuritySentryUserProvider(sentryOptions: SentryOptions) = SpringSecuritySentryUserProvider(sentryOptions) @Bean - open fun sentryUserFilter(hub: IHub, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { - this.filter = SentryUserFilter(hub, sentryUserProviders) + open fun sentryUserFilter(scopes: IScopes, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { + this.filter = SentryUserFilter(scopes, sentryUserProviders) this.order = Ordered.LOWEST_PRECEDENCE } @Bean - open fun sentrySpringFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentrySpringFilter(hub) + open fun sentrySpringFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentrySpringFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE } @Bean - open fun sentryTracingFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentryTracingFilter(hub) + open fun sentryTracingFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentryTracingFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE + 1 // must run after SentrySpringFilter } @@ -391,13 +391,13 @@ open class App { open fun sentryTaskDecorator() = SentryTaskDecorator() @Bean - open fun webClient(hub: IHub): WebClient { + open fun webClient(scopes: IScopes): WebClient { return WebClient.builder() .filter( ExchangeFilterFunctions .basicAuthentication("user", "password") ) - .filter(SentrySpanClientWebRequestFilter(hub)).build() + .filter(SentrySpanClientWebRequestFilter(scopes)).build() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt index 2910e7aac6..8b74b08fb1 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Sentry import io.sentry.SentryOptions @@ -37,20 +37,20 @@ class SentrySpanAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - whenever(hub.options).thenReturn(SentryOptions()) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when class is annotated with @SentrySpan, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -62,10 +62,10 @@ class SentrySpanAdviceTest { @Test fun `when class is annotated with @SentrySpan with operation set, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedWithOperationSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -76,10 +76,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan with properties set, attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -90,10 +90,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan without properties set, attaches span to existing transaction and sets Span description as className dot methodName`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithoutSpanDescriptionSet() assertEquals(2, result) assertEquals(1, tx.spans.size) @@ -104,10 +104,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and returns, attached span has status OK`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) sampleService.methodWithSpanDescriptionSet() assertEquals(SpanStatus.OK, tx.spans.first().status) } @@ -115,10 +115,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and throws exception, attached span has throwable set and INTERNAL_ERROR status`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) var throwable: Throwable? = null try { sampleService.methodThrowingException() @@ -131,7 +131,7 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and there is no active transaction, span is not created and method is executed`() { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) } @@ -151,10 +151,10 @@ class SentrySpanAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt index 0424696fad..265d607b70 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -38,7 +38,7 @@ import kotlin.test.fail class SentryTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -50,7 +50,7 @@ class SentryTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: Int = 200, sentryTraceHeader: String? = null, baggageHeaders: List? = null): SentryTracingFilter { @@ -61,16 +61,16 @@ class SentryTracingFilterTest { whenever(transactionNameProvider.provideTransactionSource()).thenReturn(TransactionNameSource.CUSTOM) if (sentryTraceHeader != null) { request.addHeader("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { request.addHeader("baggage", baggageHeaders) } response.status = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) - whenever(hub.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryTracingFilter(hub, transactionNameProvider) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) + whenever(scopes.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryTracingFilter(scopes, transactionNameProvider) } } @@ -82,7 +82,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -95,7 +95,7 @@ class SentryTracingFilterTest { } ) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -114,7 +114,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -130,7 +130,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -146,7 +146,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -163,7 +163,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -174,15 +174,15 @@ class SentryTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) filter.doFilter(fixture.request, fixture.response, fixture.chain) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -196,7 +196,7 @@ class SentryTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -216,10 +216,10 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub, times(2)).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes, times(2)).options + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -233,7 +233,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -253,7 +253,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -275,9 +275,9 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt index 5d2863310f..390b4d8241 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -44,13 +44,13 @@ class SentryTransactionAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.options).thenReturn( + reset(scopes) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -60,7 +60,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction around method annotated with @SentryTransaction`() { sampleService.methodWithTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("customName") assertThat(it.contexts.trace!!.operation).isEqualTo("bean") @@ -76,7 +76,7 @@ class SentryTransactionAdviceTest { @Test fun `when method annotated with @SentryTransaction throws exception, sets error status on transaction`() { assertThrows { sampleService.methodThrowingException() } - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -89,7 +89,7 @@ class SentryTransactionAdviceTest { @Test fun `when @SentryTransaction has no name set, sets transaction name as className dot methodName`() { sampleService.methodWithoutTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("SampleService.methodWithoutTransactionNameSet") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -102,18 +102,18 @@ class SentryTransactionAdviceTest { @Test fun `when transaction is already active, does not start new transaction`() { - whenever(hub.options).thenReturn(SentryOptions()) - whenever(hub.span).then { SentryTracer(TransactionContext("aTransaction", "op"), hub) } + whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.span).then { SentryTracer(TransactionContext("aTransaction", "op"), scopes) } sampleService.methodWithTransactionNameSet() - verify(hub, times(0)).captureTransaction(any(), any()) + verify(scopes, times(0)).captureTransaction(any(), any()) } @Test fun `creates transaction around method in class annotated with @SentryTransaction`() { classAnnotatedSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -127,7 +127,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction with operation set around method in class annotated with @SentryTransaction`() { classAnnotatedWithOperationSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedWithOperationSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("my-op") @@ -141,13 +141,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(hub).popScope() + verify(scopes).popScope() } @Configuration @@ -165,10 +165,10 @@ class SentryTransactionAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt index ad335333ed..9c851cde11 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt @@ -1,7 +1,8 @@ package io.sentry.spring.jakarta.webflux import io.sentry.IHub -import io.sentry.NoOpHub +import io.sentry.IScopes +import io.sentry.NoOpScopes import io.sentry.Sentry import org.mockito.kotlin.mock import org.mockito.kotlin.verify @@ -26,18 +27,18 @@ class ReactorUtilsTest { @AfterTest fun teardown() { - Sentry.setCurrentHub(NoOpHub.getInstance()) + Sentry.setCurrentScopes(NoOpScopes.getInstance()) } @Test fun `propagates hub inside mono`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val mono = ReactorUtils.withSentryHub( Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it }, hubToUse @@ -49,13 +50,13 @@ class ReactorUtilsTest { @Test fun `propagates hub inside flux`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val flux = ReactorUtils.withSentryHub( Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it }, hubToUse @@ -67,12 +68,12 @@ class ReactorUtilsTest { @Test fun `without reactive utils hub is not propagated to mono`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val mono = Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it } @@ -82,12 +83,12 @@ class ReactorUtilsTest { @Test fun `without reactive utils hub is not propagated to flux`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val flux = Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it } @@ -97,21 +98,21 @@ class ReactorUtilsTest { @Test fun `clones hub for mono`() { - val mockHub = mock() - whenever(mockHub.clone()).thenReturn(mock()) - Sentry.setCurrentHub(mockHub) + val mockScopes = mock() + whenever(mockScopes.clone()).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Mono.just("hello")).block() - verify(mockHub).clone() + verify(mockScopes).clone() } @Test fun `clones hub for flux`() { - val mockHub = mock() - whenever(mockHub.clone()).thenReturn(mock()) - Sentry.setCurrentHub(mockHub) + val mockScopes = mock() + whenever(mockScopes.clone()).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Flux.just("hello")).blockFirst() - verify(mockHub).clone() + verify(mockScopes).clone() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt index 1eb06d0afe..5403caa7e0 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt @@ -33,28 +33,28 @@ class SentryScheduleHookTest { val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt index eac0b9c60f..e936394039 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,8 +2,9 @@ package io.sentry.spring.jakarta.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.HubScopesWrapper import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.ScopeCallback import io.sentry.Sentry @@ -17,7 +18,7 @@ import io.sentry.TransactionOptions import io.sentry.protocol.SentryId import io.sentry.protocol.SentryTransaction import io.sentry.protocol.TransactionNameSource -import io.sentry.spring.jakarta.webflux.AbstractSentryWebFilter.SENTRY_HUB_KEY +import io.sentry.spring.jakarta.webflux.AbstractSentryWebFilter.SENTRY_SCOPES_KEY import org.assertj.core.api.Assertions.assertThat import org.mockito.Mockito import org.mockito.kotlin.any @@ -47,7 +48,7 @@ import kotlin.test.fail class SentryWebFluxTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var request: MockServerHttpRequest lateinit var exchange: MockServerWebExchange val chain = mock() @@ -58,45 +59,47 @@ class SentryWebFluxTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: HttpStatus = HttpStatus.OK, sentryTraceHeader: String? = null, baggageHeaders: List? = null, method: HttpMethod = HttpMethod.POST): SentryWebFilter { var requestBuilder = MockServerHttpRequest.method(method, "/product/{id}", 12) if (sentryTraceHeader != null) { requestBuilder = requestBuilder.header("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { requestBuilder = requestBuilder.header("baggage", *baggageHeaders.toTypedArray()) } request = requestBuilder.build() exchange = MockServerWebExchange.builder(request).build() - exchange.attributes.put(SENTRY_HUB_KEY, hub) + exchange.attributes.put(SENTRY_SCOPES_KEY, scopes) exchange.attributes.put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, PathPatternParser().parse("/product/{id}")) exchange.response.statusCode = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) whenever(chain.filter(any())).thenReturn(Mono.create { s -> s.success() }) - whenever(hub.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryWebFilter(hub) + whenever(scopes.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryWebFilter(scopes) } } private val fixture = Fixture() - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) + it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) closure.invoke() } @Test fun `creates transaction around the request`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -109,7 +112,7 @@ class SentryWebFluxTracingFilterTest { } ) verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -128,10 +131,10 @@ class SentryWebFluxTracingFilterTest { fun `sets correct span status based on the response status`() { val filter = fixture.getSut(status = HttpStatus.INTERNAL_SERVER_ERROR) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) assertThat(it.contexts.response!!.statusCode).isEqualTo(500) @@ -147,10 +150,10 @@ class SentryWebFluxTracingFilterTest { fun `does not set span status for response status that dont match predefined span statuses`() { val filter = fixture.getSut(status = HttpStatus.FOUND) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -165,10 +168,10 @@ class SentryWebFluxTracingFilterTest { fun `when sentry trace is not present, transaction does not have parentSpanId set`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -184,10 +187,10 @@ class SentryWebFluxTracingFilterTest { val parentSpanId = SpanId() val filter = fixture.getSut(sentryTraceHeader = "${SentryId()}-$parentSpanId-1") - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -199,16 +202,16 @@ class SentryWebFluxTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, times(3)).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes, times(3)).isEnabled + verifyNoMoreInteractions(fixture.scopes) } } @@ -216,7 +219,7 @@ class SentryWebFluxTracingFilterTest { fun `sets status to internal server error when chain throws exception`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { whenever(fixture.chain.filter(any())).thenReturn(Mono.error(RuntimeException("error"))) try { @@ -224,7 +227,7 @@ class SentryWebFluxTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -239,21 +242,21 @@ class SentryWebFluxTracingFilterTest { fun `does not track OPTIONS request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, times(3)).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub).pushScope() - verify(fixture.hub).addBreadcrumb(any(), any()) - verify(fixture.hub).configureScope(any()) - verify(fixture.hub).popScope() - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes, times(3)).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes).pushScope() + verify(fixture.scopes).addBreadcrumb(any(), any()) + verify(fixture.scopes).configureScope(any()) + verify(fixture.scopes).popScope() + verifyNoMoreInteractions(fixture.scopes) } } @@ -261,14 +264,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks OPTIONS request with traceOptionsRequests=true`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = true filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -283,14 +286,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks POST request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.POST) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -309,19 +312,19 @@ class SentryWebFluxTracingFilterTest { fixture.options.enableTracing = false val filter = fixture.getSut(sentryTraceHeader = sentryTraceHeaderString, baggageHeaders = baggageHeaderStrings) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull() ) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) } } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt index 3f4628ea3c..59f9a700b6 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.webflux -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.checkEvent import io.sentry.checkTransaction @@ -160,13 +160,13 @@ open class App { open fun mockTransport() = transport @Bean - open fun hub() = HubAdapter.getInstance() + open fun scopes() = ScopesAdapter.getInstance() @Bean - open fun sentryFilter(hub: IHub) = SentryWebFilter(hub) + open fun sentryFilter(scopes: IScopes) = SentryWebFilter(scopes) @Bean - open fun sentryWebExceptionHandler(hub: IHub) = SentryWebExceptionHandler(hub) + open fun sentryWebExceptionHandler(scopes: IScopes) = SentryWebExceptionHandler(scopes) @Bean open fun sentryScheduleHookRegistrar() = ApplicationRunner { diff --git a/sentry-spring/api/sentry-spring.api b/sentry-spring/api/sentry-spring.api index 9ef6b5bb3a..58de26098f 100644 --- a/sentry-spring/api/sentry-spring.api +++ b/sentry-spring/api/sentry-spring.api @@ -22,7 +22,7 @@ public final class io/sentry/spring/HttpServletRequestSentryUserProvider : io/se public class io/sentry/spring/SentryExceptionResolver : org/springframework/core/Ordered, org/springframework/web/servlet/HandlerExceptionResolver { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;Lio/sentry/spring/tracing/TransactionNameProvider;I)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/tracing/TransactionNameProvider;I)V protected fun createEvent (Ljavax/servlet/http/HttpServletRequest;Ljava/lang/Exception;)Lio/sentry/SentryEvent; protected fun createHint (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lio/sentry/Hint; public fun getOrder ()I @@ -47,14 +47,14 @@ public class io/sentry/spring/SentryRequestHttpServletRequestProcessor : io/sent } public class io/sentry/spring/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Ljavax/servlet/http/HttpServletRequest;)Lio/sentry/protocol/Request; } public class io/sentry/spring/SentrySpringFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/SentryRequestResolver;Lio/sentry/spring/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/SentryRequestResolver;Lio/sentry/spring/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V } @@ -69,7 +69,7 @@ public final class io/sentry/spring/SentryTaskDecorator : org/springframework/co } public class io/sentry/spring/SentryUserFilter : org/springframework/web/filter/OncePerRequestFilter { - public fun (Lio/sentry/IHub;Ljava/util/List;)V + public fun (Lio/sentry/IScopes;Ljava/util/List;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V public fun getSentryUserProviders ()Ljava/util/List; } @@ -96,7 +96,7 @@ public abstract interface annotation class io/sentry/spring/checkin/SentryCheckI public class io/sentry/spring/checkin/SentryCheckInAdvice : org/aopalliance/intercept/MethodInterceptor, org/springframework/context/EmbeddedValueResolverAware { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; public fun setEmbeddedValueResolver (Lorg/springframework/util/StringValueResolver;)V } @@ -127,7 +127,7 @@ public abstract interface annotation class io/sentry/spring/exception/SentryCapt public class io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -169,7 +169,7 @@ public final class io/sentry/spring/graphql/SentryDataFetcherExceptionResolverAd public final class io/sentry/spring/graphql/SentryDgsSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/spring/graphql/SentryGraphqlBeanPostProcessor : org/springframework/beans/factory/config/BeanPostProcessor, org/springframework/core/PriorityOrdered { @@ -188,7 +188,7 @@ public class io/sentry/spring/graphql/SentryGraphqlConfiguration { public final class io/sentry/spring/graphql/SentrySpringSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public class io/sentry/spring/tracing/SentryAdviceConfiguration { @@ -207,17 +207,17 @@ public abstract interface annotation class io/sentry/spring/tracing/SentrySpan : public class io/sentry/spring/tracing/SentrySpanAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } public class io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor : org/springframework/http/client/ClientHttpRequestInterceptor { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun intercept (Lorg/springframework/http/HttpRequest;[BLorg/springframework/http/client/ClientHttpRequestExecution;)Lorg/springframework/http/client/ClientHttpResponse; } public class io/sentry/spring/tracing/SentrySpanClientWebRequestFilter : org/springframework/web/reactive/function/client/ExchangeFilterFunction { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/reactive/function/client/ClientRequest;Lorg/springframework/web/reactive/function/client/ExchangeFunction;)Lreactor/core/publisher/Mono; } @@ -232,8 +232,8 @@ public class io/sentry/spring/tracing/SentryTracingConfiguration { public class io/sentry/spring/tracing/SentryTracingFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V } @@ -245,7 +245,7 @@ public abstract interface annotation class io/sentry/spring/tracing/SentryTransa public class io/sentry/spring/tracing/SentryTransactionAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -266,7 +266,7 @@ public abstract interface class io/sentry/spring/tracing/TransactionNameProvider } public class io/sentry/spring/webflux/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/protocol/Request; } @@ -278,13 +278,14 @@ public final class io/sentry/spring/webflux/SentryScheduleHook : java/util/funct public final class io/sentry/spring/webflux/SentryWebExceptionHandler : org/springframework/web/server/WebExceptionHandler { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun handle (Lorg/springframework/web/server/ServerWebExchange;Ljava/lang/Throwable;)Lreactor/core/publisher/Mono; } public final class io/sentry/spring/webflux/SentryWebFilter : org/springframework/web/server/WebFilter { public static final field SENTRY_HUB_KEY Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public static final field SENTRY_SCOPES_KEY Ljava/lang/String; + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java index fc9da87933..ba0586eade 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java @@ -5,7 +5,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -29,15 +29,15 @@ public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered { public static final String MECHANISM_TYPE = "Spring5ExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull TransactionNameProvider transactionNameProvider; private final int order; public SentryExceptionResolver( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final int order) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); this.order = order; @@ -53,7 +53,7 @@ public SentryExceptionResolver( final SentryEvent event = createEvent(request, ex); final Hint hint = createHint(request, response); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); // null = run other HandlerExceptionResolvers to actually handle the exception return null; diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java b/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java index e597d175b6..195a88e277 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java @@ -1,7 +1,7 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; import io.sentry.protocol.SdkVersion; @@ -60,7 +60,7 @@ private void registerSentryOptions( private void registerSentryHubBean(final @NotNull BeanDefinitionRegistry registry) { final BeanDefinitionBuilder builder = - BeanDefinitionBuilder.genericBeanDefinition(HubAdapter.class); + BeanDefinitionBuilder.genericBeanDefinition(ScopesAdapter.class); builder.setInitMethodName("getInstance"); registry.registerBeanDefinition("sentryHub", builder.getBeanDefinition()); diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java b/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java index 9e455dfb04..ca431aae14 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java @@ -2,10 +2,10 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryOptions; import io.sentry.SentryOptions.TracesSamplerCallback; @@ -27,15 +27,15 @@ public class SentryInitBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, DisposableBean { private @Nullable ApplicationContext applicationContext; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryInitBeanPostProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryInitBeanPostProcessor(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.hub = hub; + SentryInitBeanPostProcessor(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.scopes = scopes; } @Override @@ -86,6 +86,6 @@ public void setApplicationContext(final @NotNull ApplicationContext applicationC @Override public void destroy() { - hub.close(); + scopes.close(); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java index 442644fcac..2d9e2996f7 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; @@ -20,11 +20,11 @@ @Open public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private volatile @Nullable List extraSecurityCookies; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } // httpRequest.getRequestURL() returns StringBuffer which is considered an obsolete class. @@ -40,7 +40,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { extractSecurityCookieNamesOrUseCached(httpRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest, additionalSecurityCookieNames)); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String cookieName = HttpUtils.COOKIE_HEADER_NAME; final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( @@ -57,7 +57,8 @@ Map resolveHeadersMap( final Map headersMap = new HashMap<>(); for (String headerName : Collections.list(request.getHeaderNames())) { // do not copy personal information identifiable headers - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( request.getHeaders(headerName), headerName, additionalSecurityCookieNames); @@ -94,7 +95,8 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to extract session cookie name from request.", t); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 8fd8180941..7695545f04 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -8,8 +8,8 @@ import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -29,26 +29,26 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; public SentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentrySpringFilter(final @NotNull IHub hub) { - this(hub, new SentryRequestResolver(hub), new SpringMvcTransactionNameProvider()); + public SentrySpringFilter(final @NotNull IScopes scopes) { + this(scopes, new SentryRequestResolver(scopes), new SpringMvcTransactionNameProvider()); } public SentrySpringFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override @@ -57,20 +57,20 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - hub.pushScope(); + scopes.pushScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - hub.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); configureScope(request); filterChain.doFilter(request, response); } finally { - hub.popScope(); + scopes.popScope(); } } else { filterChain.doFilter(servletRequest, response); @@ -79,7 +79,7 @@ protected void doFilterInternal( private void configureScope(HttpServletRequest request) { try { - hub.configureScope( + scopes.configureScope( scope -> { // set basic request information on the scope scope.setRequest(requestResolver.resolveSentryRequest(request)); @@ -92,11 +92,12 @@ private void configureScope(HttpServletRequest request) { // request processing if (request instanceof CachedBodyHttpServletRequest) { scope.addEventProcessor( - new RequestBodyExtractingEventProcessor(request, hub.getOptions())); + new RequestBodyExtractingEventProcessor(request, scopes.getOptions())); } }); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Failed to set scope for HTTP request", e); } @@ -104,12 +105,13 @@ private void configureScope(HttpServletRequest request) { private @NotNull HttpServletRequest resolveHttpServletRequest( final @NotNull HttpServletRequest request) { - if (hub.getOptions().isSendDefaultPii() - && qualifiesForCaching(request, hub.getOptions().getMaxRequestBodySize())) { + if (scopes.getOptions().isSendDefaultPii() + && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { return new CachedBodyHttpServletRequest(request); } catch (IOException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index cb4a700696..88d205a57e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -1,6 +1,6 @@ package io.sentry.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -9,21 +9,23 @@ /** * Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate - * the current {@link IHub} on the thread executing async task - like MVC controller methods + * the current {@link IScopes} on the thread executing async task - like MVC controller methods * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override + @SuppressWarnings("deprecation") public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java index 55c5826d40..e0b4e9c1ba 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.IpAddressUtils; import io.sentry.protocol.User; import io.sentry.util.Objects; @@ -26,12 +26,12 @@ */ @Open public class SentryUserFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull List sentryUserProviders; public SentryUserFilter( - final @NotNull IHub hub, final @NotNull List sentryUserProviders) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull List sentryUserProviders) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.sentryUserProviders = Objects.requireNonNull(sentryUserProviders, "sentryUserProviders list is required"); } @@ -46,13 +46,13 @@ protected void doFilterInternal( for (final SentryUserProvider provider : sentryUserProviders) { apply(user, provider.provideUser()); } - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { if (IpAddressUtils.isDefault(user.getIpAddress())) { // unset {{auto}} as it would set the server's ip address as a user ip address user.setIpAddress(null); } } - hub.setUser(user); + scopes.setUser(user); chain.doFilter(request, response); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index c29ad44900..edae74c2ac 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -4,8 +4,8 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; @@ -30,16 +30,16 @@ @ApiStatus.Experimental @Open public class SentryCheckInAdvice implements MethodInterceptor, EmbeddedValueResolverAware { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @Nullable StringValueResolver resolver; public SentryCheckInAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCheckInAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCheckInAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -69,7 +69,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl // Sentry should alert the user about missed checkins in this case since the monitor slug // won't match // what is configured in Sentry. - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -79,7 +80,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } if (ObjectUtils.isEmpty(monitorSlug)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -87,8 +89,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); @@ -96,7 +98,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { if (!isHeartbeatOnly) { - checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); } return invocation.proceed(); } catch (Throwable e) { @@ -106,8 +108,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java index bababbce77..119f39ba6c 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java @@ -1,8 +1,8 @@ package io.sentry.spring.exception; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.exception.ExceptionMechanismException; import io.sentry.protocol.Mechanism; import io.sentry.util.Objects; @@ -22,14 +22,14 @@ @Open public class SentryCaptureExceptionParameterAdvice implements MethodInterceptor { private static final String MECHANISM_TYPE = "SentrySpring5CaptureExceptionParameterAdvice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryCaptureExceptionParameterAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -58,6 +58,6 @@ private void captureException(final @NotNull Throwable throwable) { mechanism.setHandled(true); final Throwable mechanismException = new ExceptionMechanismException(mechanism, throwable, Thread.currentThread()); - hub.captureException(mechanismException); + scopes.captureException(mechanismException); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java index 62a8669f89..f1d8717598 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java @@ -1,11 +1,11 @@ package io.sentry.spring.graphql; -import static io.sentry.graphql.SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY; +import static io.sentry.graphql.SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY; import graphql.GraphQLContext; import io.sentry.Breadcrumb; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import java.util.List; import java.util.Map; import java.util.Set; @@ -89,7 +89,7 @@ public BatchLoaderRegistry.RegistrationSpec withOptions(DataLoaderOptions public void registerBatchLoader(BiFunction, BatchLoaderEnvironment, Flux> loader) { delegate.registerBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); @@ -100,20 +100,20 @@ public void registerMappedBatchLoader( BiFunction, BatchLoaderEnvironment, Mono>> loader) { delegate.registerMappedBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); } - private @NotNull IHub hubFromContext(final @NotNull BatchLoaderEnvironment environment) { + private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) { Object context = environment.getContext(); if (context instanceof GraphQLContext) { GraphQLContext graphqlContext = (GraphQLContext) context; - return graphqlContext.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java index fb4e09e889..eef5fdae69 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; @@ -17,7 +17,7 @@ public SentryDgsSubscriptionHandler() { @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -25,7 +25,7 @@ public SentryDgsSubscriptionHandler() { return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); exceptionReporter.captureThrowable(throwable, exceptionDetails, null); }); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java index a7809eb230..b3f0b9830e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; import org.jetbrains.annotations.NotNull; @@ -13,7 +13,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -21,7 +21,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); if (throwable instanceof SubscriptionPublisherException && throwable.getCause() != null) { exceptionReporter.captureThrowable(throwable.getCause(), exceptionDetails, null); diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java index 96c4275836..c1b7305d4f 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.util.Objects; import java.lang.reflect.Method; @@ -22,20 +22,20 @@ @Open public class SentrySpanAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentrySpanAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @Override public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null || activeSpan.isNoOp()) { // there is no active transaction, we do not start new span diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java index 4067c69c8c..bdf8417642 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java @@ -8,7 +8,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -27,10 +27,10 @@ @Open public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private static final String TRACE_ORIGIN = "auto.http.spring.resttemplate"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientHttpRequestInterceptor(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -42,7 +42,7 @@ public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { Integer responseStatusCode = null; ClientHttpResponse response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { maybeAddTracingHeaders(request, null); return execution.execute(request, body); @@ -83,7 +83,7 @@ private void maybeAddTracingHeaders( final @NotNull HttpRequest request, final @Nullable ISpan span) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.getURI().toString(), request.getHeaders().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -120,6 +120,6 @@ private void addBreadcrumb( hint.set(SPRING_REQUEST_INTERCEPTOR_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java index 00ad91bbb0..c526cb64cc 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java @@ -7,7 +7,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -26,16 +26,16 @@ @Open public class SentrySpanClientWebRequestFilter implements ExchangeFilterFunction { private static final String TRACE_ORIGIN = "auto.http.spring.webclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientWebRequestFilter(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override public @NotNull Mono filter( final @NotNull ClientRequest request, final @NotNull ExchangeFunction next) { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { addBreadcrumb(request, null); return next.exchange(maybeAddHeaders(request, null)); @@ -76,7 +76,7 @@ private ClientRequest maybeAddHeaders( final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url().toString(), request.headers().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -113,6 +113,6 @@ private void addBreadcrumb( hint.set(SPRING_EXCHANGE_FILTER_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java index 5af329f600..50cdb8dc3a 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java @@ -3,9 +3,9 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.BaggageHeader; import io.sentry.CustomSamplingContext; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; import io.sentry.TransactionContext; @@ -35,7 +35,7 @@ public class SentryTracingFilter extends OncePerRequestFilter { private static final String TRACE_ORIGIN = "auto.http.spring.webmvc"; private final @NotNull TransactionNameProvider transactionNameProvider; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; /** * Creates filter that resolves transaction name using {@link SpringMvcTransactionNameProvider}. @@ -46,25 +46,26 @@ public class SentryTracingFilter extends OncePerRequestFilter { * javax.servlet.Filter}. */ public SentryTracingFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } /** * Creates filter that resolves transaction name using transaction name provider given by * parameter. * - * @param hub - the hub + * @param scopes - the scopes * @param transactionNameProvider - transaction name provider. */ public SentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { + this.scopes = Objects.requireNonNull(scopes, "scopes is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentryTracingFilter(final @NotNull IHub hub) { - this(hub, new SpringMvcTransactionNameProvider()); + public SentryTracingFilter(final @NotNull IScopes scopes) { + this(scopes, new SpringMvcTransactionNameProvider()); } @Override @@ -74,15 +75,15 @@ protected void doFilterInternal( final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { final @Nullable String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER)); final @Nullable TransactionContext transactionContext = - hub.continueTrace(sentryTraceHeader, baggageHeader); + scopes.continueTrace(sentryTraceHeader, baggageHeader); - if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { + if (scopes.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext); } else { filterChain.doFilter(httpRequest, httpResponse); @@ -129,7 +130,7 @@ private void doFilterWithTransaction( } private boolean shouldTraceRequest(final @NotNull HttpServletRequest request) { - return hub.getOptions().isTraceOptionsRequests() + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.name().equals(request.getMethod()); } @@ -151,14 +152,14 @@ private ITransaction startTransaction( transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, "http.server"), transactionOptions); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java index c417f14a14..8f4f5bbdfc 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.TransactionContext; import io.sentry.TransactionOptions; @@ -27,14 +27,14 @@ @Open public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryTransactionAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryTransactionAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryTransactionAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -67,11 +67,11 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - hub.pushScope(); + scopes.pushScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(nameAndSource.name, nameAndSource.source, operation), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN); @@ -85,7 +85,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - hub.popScope(); + scopes.popScope(); } } } @@ -105,7 +105,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } private boolean isTransactionActive() { - return hub.getSpan() != null; + return scopes.getSpan() != null; } private static class TransactionNameAndSource { diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java index a710438c46..76e50985e5 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; @@ -20,10 +20,10 @@ @Open @ApiStatus.Experimental public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public @NotNull Request resolveSentryRequest(final @NotNull ServerHttpRequest httpRequest) { @@ -36,7 +36,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { urlDetails.applyToRequest(sentryRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest.getHeaders())); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String headerName = HttpUtils.COOKIE_HEADER_NAME; sentryRequest.setCookies( toString( @@ -52,7 +52,8 @@ Map resolveHeadersMap(final HttpHeaders request) { for (Map.Entry> entry : request.entrySet()) { // do not copy personal information identifiable headers String headerName = entry.getKey(); - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { headersMap.put( headerName, toString( diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java index 7775d4e482..20f494168d 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java @@ -1,6 +1,6 @@ package io.sentry.spring.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -13,16 +13,18 @@ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override + @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java index 98d3855a04..042d1d48ec 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java @@ -5,7 +5,7 @@ import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_RESPONSE; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -26,10 +26,10 @@ @ApiStatus.Experimental public final class SentryWebExceptionHandler implements WebExceptionHandler { public static final String MECHANISM_TYPE = "Spring5WebFluxExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryWebExceptionHandler(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryWebExceptionHandler(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -50,7 +50,7 @@ public SentryWebExceptionHandler(final @NotNull IHub hub) { hint.set(WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse()); hint.set(WEBFLUX_EXCEPTION_HANDLER_EXCHANGE, serverWebExchange); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } return Mono.error(ex); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index f68a87ae0f..3bb7de5ae4 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -7,10 +7,10 @@ import io.sentry.Breadcrumb; import io.sentry.CustomSamplingContext; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; @@ -34,22 +34,24 @@ /** Manages {@link IScope} in Webflux request processing. */ @ApiStatus.Experimental public final class SentryWebFilter implements WebFilter { - public static final String SENTRY_HUB_KEY = "sentry-hub"; + public static final String SENTRY_SCOPES_KEY = "sentry-scopes"; + @Deprecated public static final String SENTRY_HUB_KEY = SENTRY_SCOPES_KEY; private static final String TRANSACTION_OP = "http.server"; private static final String TRACE_ORIGIN = "auto.spring.webflux"; private final @NotNull SentryRequestResolver sentryRequestResolver; - public SentryWebFilter(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.sentryRequestResolver = new SentryRequestResolver(hub); + public SentryWebFilter(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.sentryRequestResolver = new SentryRequestResolver(scopes); } @Override public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IHub requestHub = Sentry.cloneMainHub(); + @NotNull IScopes requestHub = Sentry.cloneMainHub(); + // TODO do not push / pop, use fork instead if (!requestHub.isEnabled()) { return webFilterChain.filter(serverWebExchange); } @@ -80,7 +82,8 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) finishTransaction(serverWebExchange, transaction); } requestHub.popScope(); - Sentry.setCurrentHub(NoOpHub.getInstance()); + // TODO token based cleanup instead? + Sentry.setCurrentScopes(NoOpScopes.getInstance()); }) .doOnError( e -> { @@ -91,8 +94,8 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) }) .doFirst( () -> { - serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); - Sentry.setCurrentHub(requestHub); + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); + Sentry.setCurrentScopes(requestHub); requestHub.pushScope(); final ServerHttpResponse response = serverWebExchange.getResponse(); @@ -109,13 +112,13 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) } private boolean shouldTraceRequest( - final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { - return hub.getOptions().isTraceOptionsRequests() + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request) { + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.equals(request.getMethod()); } private @NotNull ITransaction startTransaction( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request, final @Nullable TransactionContext transactionContext) { final @NotNull String name = request.getMethod() + " " + request.getURI().getPath(); @@ -131,10 +134,10 @@ private boolean shouldTraceRequest( transactionContext.setTransactionNameSource(TransactionNameSource.URL); transactionContext.setOperation(TRANSACTION_OP); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, TRANSACTION_OP), transactionOptions); } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt index 5a8fec3053..5182309416 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.EventProcessor -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.Sentry @@ -65,9 +65,9 @@ class EnableSentryTest { } @Test - fun `creates Sentry Hub`() { + fun `creates Sentry Scopes`() { contextRunner.run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt index 4f94fd7ce0..23807e1fd9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring import io.sentry.CheckIn import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -55,19 +55,19 @@ class SentryCheckInAdviceTest { lateinit var sampleServiceSpringProperties: SampleServiceSpringProperties @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.options).thenReturn(SentryOptions()) + reset(scopes) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleService.hello() assertEquals(1, result) assertEquals(2, checkInCaptor.allValues.size) @@ -80,17 +80,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleService.oops() } @@ -104,17 +104,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceHeartbeat.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -124,17 +124,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleServiceHeartbeat.oops() } @@ -145,31 +145,31 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn but slug is missing, does not create check-in`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(hub, never()).pushScope() - verify(hub, never()).captureCheckIn(any()) - verify(hub, never()).popScope() + verify(scopes, never()).pushScope() + verify(scopes, never()).captureCheckIn(any()) + verify(scopes, never()).popScope() } @Test fun `when @SentryCheckIn is passed a spring property it is resolved correctly`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -179,17 +179,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that does not exist, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloUnresolvedProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -199,17 +199,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that causes an exception, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloExceptionProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -219,10 +219,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Configuration @@ -243,10 +243,10 @@ class SentryCheckInAdviceTest { open fun sampleServiceSpringProperties() = SampleServiceSpringProperties() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } companion object { diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt index f3e4c32fb0..048166107b 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.exception.ExceptionMechanismException @@ -17,7 +17,7 @@ import javax.servlet.http.HttpServletResponse import kotlin.test.Test class SentryExceptionResolverTest { - private val hub = mock() + private val scopes = mock() private val transactionNameProvider = mock() private val request = mock() @@ -26,10 +26,10 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets wrapped exception for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) val expectedCause = RuntimeException("test") - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, expectedCause) assertThat(eventCaptor.firstValue.throwable).isEqualTo(expectedCause) @@ -46,9 +46,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets fatal level for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.level).isEqualTo(SentryLevel.FATAL) @@ -59,9 +59,9 @@ class SentryExceptionResolverTest { val expectedTransactionName = "test-transaction" whenever(transactionNameProvider.provideTransactionName(any())).thenReturn(expectedTransactionName) val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.transaction).isEqualTo(expectedTransactionName) @@ -71,9 +71,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, provides spring resolver hint`() { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) with(hintCaptor.firstValue) { @@ -86,8 +86,8 @@ class SentryExceptionResolverTest { fun `when custom create event method provided, uses it to capture event`() { val expectedEvent = SentryEvent() val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createEvent(request: HttpServletRequest, ex: Exception) = expectedEvent } @@ -100,8 +100,8 @@ class SentryExceptionResolverTest { fun `when custom create hint method provided, uses it to capture event`() { val expectedHint = Hint() val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createHint(request: HttpServletRequest, response: HttpServletResponse) = expectedHint } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt index 78ebf961c9..dc4a14e656 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -13,18 +13,18 @@ class SentryInitBeanPostProcessorTest { @Test fun closesSentryOnApplicationContextDestroy() { val ctx = AnnotationConfigApplicationContext(TestConfig::class.java) - val hub = ctx.getBean(IHub::class.java) + val scopes = ctx.getBean(IScopes::class.java) ctx.close() - verify(hub).close() + verify(scopes).close() } @Configuration open class TestConfig { @Bean(destroyMethod = "") - open fun hub() = mock() + open fun scopes() = mock() @Bean - open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(hub()) + open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(scopes()) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt index 3fe9b60743..c7277a2240 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryOptions import io.sentry.spring.tracing.SpringMvcTransactionNameProvider @@ -19,10 +19,10 @@ import kotlin.test.assertNotNull class SentryRequestHttpServletRequestProcessorTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut(request: HttpServletRequest, options: SentryOptions = SentryOptions()): SentryRequestHttpServletRequestProcessor { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return SentryRequestHttpServletRequestProcessor(SpringMvcTransactionNameProvider(), request) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt index ce83c4b9b7..c6ac952531 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val response = MockHttpServletResponse() val chain = mock() lateinit var scope: IScope @@ -45,15 +45,15 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) - whenever(hub.options).thenReturn(options) - whenever(hub.isEnabled).thenReturn(true) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + whenever(scopes.isEnabled).thenReturn(true) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(hub) + return SentrySpringFilter(scopes) } } @@ -64,7 +64,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test @@ -72,7 +72,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> Assertions.assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") Assertions.assertThat(it.getData("method")).isEqualTo("POST") @@ -87,7 +87,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } @Test @@ -99,7 +99,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt index e202deca86..3f34ab9d9d 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt @@ -32,28 +32,28 @@ class SentryTaskDecoratorTest { val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt index 2dac9a57ed..7f9be1ba8b 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.protocol.User import org.mockito.kotlin.check @@ -16,7 +16,7 @@ import kotlin.test.assertNull class SentryUserFilterTest { class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -25,8 +25,8 @@ class SentryUserFilterTest { val options = SentryOptions().apply { this.isSendDefaultPii = isSendDefaultPii } - whenever(hub.options).thenReturn(options) - return SentryUserFilter(hub, userProviders) + whenever(scopes.options).thenReturn(options) + return SentryUserFilter(scopes, userProviders) } } @@ -52,7 +52,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -72,7 +72,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -92,7 +92,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -118,7 +118,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(mapOf("key" to "value", "new-key" to "new-value"), it.others) } @@ -140,7 +140,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals("192.168.0.1", it.ipAddress) } @@ -162,7 +162,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertNull(it.ipAddress) } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt index 39bc66fabb..dca1c4c592 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.exception import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.exception.ExceptionMechanismException import org.junit.runner.RunWith @@ -30,18 +30,18 @@ class SentryCaptureExceptionParameterAdviceTest { lateinit var sampleService: SampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) + reset(scopes) } @Test fun `captures exception passed to method annotated with @SentryCaptureException`() { val exception = RuntimeException("test exception") sampleService.methodTakingAnException(exception) - verify(hub).captureException( + verify(scopes).captureException( check { assertTrue(it is ExceptionMechanismException) assertEquals(exception, it.throwable) @@ -60,10 +60,10 @@ class SentryCaptureExceptionParameterAdviceTest { open fun sampleService() = SampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt index df2df5b3ef..1aa97cd262 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchPar import graphql.language.Document import graphql.language.OperationDefinition import graphql.schema.DataFetchingEnvironment -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.graphql.ExceptionReporter import org.junit.jupiter.api.assertThrows import org.mockito.kotlin.anyOrNull @@ -23,7 +23,7 @@ class SentrySpringSubscriptionHandlerTest { @Test fun `reports exception`() { val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -32,7 +32,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -41,7 +41,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() @@ -52,7 +52,7 @@ class SentrySpringSubscriptionHandlerTest { fun `unwraps SubscriptionPublisherException and reports cause`() { val exception = IllegalStateException("some exception") val wrappedException = SubscriptionPublisherException(emptyList(), exception) - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -61,7 +61,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -70,7 +70,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt index 998b27c7e8..ad9734def6 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.mvc -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.SentryOptions @@ -104,7 +104,7 @@ class SentrySpringIntegrationTest { lateinit var anotherService: AnotherService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -260,7 +260,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -276,7 +276,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodWithInnerSpanThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -370,20 +370,20 @@ open class App { open fun springSecuritySentryUserProvider(sentryOptions: SentryOptions) = SpringSecuritySentryUserProvider(sentryOptions) @Bean - open fun sentryUserFilter(hub: IHub, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { - this.filter = SentryUserFilter(hub, sentryUserProviders) + open fun sentryUserFilter(scopes: IScopes, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { + this.filter = SentryUserFilter(scopes, sentryUserProviders) this.order = Ordered.LOWEST_PRECEDENCE } @Bean - open fun sentrySpringFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentrySpringFilter(hub) + open fun sentrySpringFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentrySpringFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE } @Bean - open fun sentryTracingFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentryTracingFilter(hub) + open fun sentryTracingFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentryTracingFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE + 1 // must run after SentrySpringFilter } @@ -391,13 +391,13 @@ open class App { open fun sentryTaskDecorator() = SentryTaskDecorator() @Bean - open fun webClient(hub: IHub): WebClient { + open fun webClient(scopes: IScopes): WebClient { return WebClient.builder() .filter( ExchangeFilterFunctions .basicAuthentication("user", "password") ) - .filter(SentrySpanClientWebRequestFilter(hub)).build() + .filter(SentrySpanClientWebRequestFilter(scopes)).build() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt index dc6f00eb46..a6f59971c9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Sentry import io.sentry.SentryOptions @@ -37,20 +37,20 @@ class SentrySpanAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - whenever(hub.options).thenReturn(SentryOptions()) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when class is annotated with @SentrySpan, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -62,10 +62,10 @@ class SentrySpanAdviceTest { @Test fun `when class is annotated with @SentrySpan with operation set, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedWithOperationSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -76,10 +76,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan with properties set, attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -90,10 +90,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan without properties set, attaches span to existing transaction and sets Span description as className dot methodName`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithoutSpanDescriptionSet() assertEquals(2, result) assertEquals(1, tx.spans.size) @@ -104,10 +104,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and returns, attached span has status OK`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) sampleService.methodWithSpanDescriptionSet() assertEquals(SpanStatus.OK, tx.spans.first().status) } @@ -115,10 +115,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and throws exception, attached span has throwable set and INTERNAL_ERROR status`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) var throwable: Throwable? = null try { sampleService.methodThrowingException() @@ -131,7 +131,7 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and there is no active transaction, span is not created and method is executed`() { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) } @@ -151,10 +151,10 @@ class SentrySpanAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt index 6c4c06ebff..aca54809cc 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.tracing -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -38,7 +38,7 @@ import kotlin.test.fail class SentryTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -50,7 +50,7 @@ class SentryTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: Int = 200, sentryTraceHeader: String? = null, baggageHeaders: List? = null): SentryTracingFilter { @@ -61,16 +61,16 @@ class SentryTracingFilterTest { whenever(transactionNameProvider.provideTransactionSource()).thenReturn(TransactionNameSource.CUSTOM) if (sentryTraceHeader != null) { request.addHeader("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { request.addHeader("baggage", baggageHeaders) } response.status = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) - whenever(hub.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryTracingFilter(hub, transactionNameProvider) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) + whenever(scopes.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryTracingFilter(scopes, transactionNameProvider) } } @@ -82,7 +82,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -95,7 +95,7 @@ class SentryTracingFilterTest { } ) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -114,7 +114,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -130,7 +130,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -146,7 +146,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -163,7 +163,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -174,15 +174,15 @@ class SentryTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) filter.doFilter(fixture.request, fixture.response, fixture.chain) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -196,7 +196,7 @@ class SentryTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -216,10 +216,10 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -233,7 +233,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -253,7 +253,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -275,9 +275,9 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt index 0fa9abbb29..f53acde8aa 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -44,13 +44,13 @@ class SentryTransactionAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.options).thenReturn( + reset(scopes) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -60,7 +60,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction around method annotated with @SentryTransaction`() { sampleService.methodWithTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("customName") assertThat(it.contexts.trace!!.operation).isEqualTo("bean") @@ -76,7 +76,7 @@ class SentryTransactionAdviceTest { @Test fun `when method annotated with @SentryTransaction throws exception, sets error status on transaction`() { assertThrows { sampleService.methodThrowingException() } - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -89,7 +89,7 @@ class SentryTransactionAdviceTest { @Test fun `when @SentryTransaction has no name set, sets transaction name as className dot methodName`() { sampleService.methodWithoutTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("SampleService.methodWithoutTransactionNameSet") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -102,18 +102,18 @@ class SentryTransactionAdviceTest { @Test fun `when transaction is already active, does not start new transaction`() { - whenever(hub.options).thenReturn(SentryOptions()) - whenever(hub.span).then { SentryTracer(TransactionContext("aTransaction", "op"), hub) } + whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.span).then { SentryTracer(TransactionContext("aTransaction", "op"), scopes) } sampleService.methodWithTransactionNameSet() - verify(hub, times(0)).captureTransaction(any(), any()) + verify(scopes, times(0)).captureTransaction(any(), any()) } @Test fun `creates transaction around method in class annotated with @SentryTransaction`() { classAnnotatedSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -127,7 +127,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction with operation set around method in class annotated with @SentryTransaction`() { classAnnotatedWithOperationSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedWithOperationSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("my-op") @@ -141,13 +141,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(hub).popScope() + verify(scopes).popScope() } @Configuration @@ -165,10 +165,10 @@ class SentryTransactionAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt index b09011d544..7a8b2993f9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt @@ -33,28 +33,28 @@ class SentryScheduleHookTest { val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { Sentry.setCurrentHub(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt index be56a8391d..2113c748ee 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,8 +2,9 @@ package io.sentry.spring.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.HubScopesWrapper import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.ScopeCallback import io.sentry.Sentry @@ -17,7 +18,7 @@ import io.sentry.TransactionOptions import io.sentry.protocol.SentryId import io.sentry.protocol.SentryTransaction import io.sentry.protocol.TransactionNameSource -import io.sentry.spring.webflux.SentryWebFilter.SENTRY_HUB_KEY +import io.sentry.spring.webflux.SentryWebFilter.SENTRY_SCOPES_KEY import org.assertj.core.api.Assertions.assertThat import org.mockito.Mockito import org.mockito.kotlin.any @@ -47,7 +48,7 @@ import kotlin.test.fail class SentryWebFluxTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var request: MockServerHttpRequest lateinit var exchange: MockServerWebExchange val chain = mock() @@ -58,35 +59,37 @@ class SentryWebFluxTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: HttpStatus = HttpStatus.OK, sentryTraceHeader: String? = null, baggageHeaders: List? = null, method: HttpMethod = HttpMethod.POST): SentryWebFilter { var requestBuilder = MockServerHttpRequest.method(method, "/product/{id}", 12) if (sentryTraceHeader != null) { requestBuilder = requestBuilder.header("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { requestBuilder = requestBuilder.header("baggage", *baggageHeaders.toTypedArray()) } request = requestBuilder.build() exchange = MockServerWebExchange.builder(request).build() - exchange.attributes.put(SENTRY_HUB_KEY, hub) + exchange.attributes.put(SENTRY_SCOPES_KEY, scopes) exchange.attributes.put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, PathPatternParser().parse("/product/{id}")) exchange.response.statusCode = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) whenever(chain.filter(any())).thenReturn(Mono.create { s -> s.success() }) - whenever(hub.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryWebFilter(hub) + whenever(scopes.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryWebFilter(scopes) } } private val fixture = Fixture() - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) + it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) closure.invoke() } @@ -94,10 +97,10 @@ class SentryWebFluxTracingFilterTest { fun `creates transaction around the request`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -110,7 +113,7 @@ class SentryWebFluxTracingFilterTest { } ) verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -129,10 +132,10 @@ class SentryWebFluxTracingFilterTest { fun `sets correct span status based on the response status`() { val filter = fixture.getSut(status = HttpStatus.INTERNAL_SERVER_ERROR) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) assertThat(it.contexts.response!!.statusCode).isEqualTo(500) @@ -148,10 +151,10 @@ class SentryWebFluxTracingFilterTest { fun `does not set span status for response status that dont match predefined span statuses`() { val filter = fixture.getSut(status = HttpStatus.FOUND) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -166,10 +169,10 @@ class SentryWebFluxTracingFilterTest { fun `when sentry trace is not present, transaction does not have parentSpanId set`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -185,10 +188,10 @@ class SentryWebFluxTracingFilterTest { val parentSpanId = SpanId() val filter = fixture.getSut(sentryTraceHeader = "${SentryId()}-$parentSpanId-1") - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -200,16 +203,16 @@ class SentryWebFluxTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) } } @@ -217,7 +220,7 @@ class SentryWebFluxTracingFilterTest { fun `sets status to internal server error when chain throws exception`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { whenever(fixture.chain.filter(any())).thenReturn(Mono.error(RuntimeException("error"))) try { @@ -225,7 +228,7 @@ class SentryWebFluxTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -240,21 +243,21 @@ class SentryWebFluxTracingFilterTest { fun `does not track OPTIONS request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub).pushScope() - verify(fixture.hub).addBreadcrumb(any(), any()) - verify(fixture.hub).configureScope(any()) - verify(fixture.hub).popScope() - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes).pushScope() + verify(fixture.scopes).addBreadcrumb(any(), any()) + verify(fixture.scopes).configureScope(any()) + verify(fixture.scopes).popScope() + verifyNoMoreInteractions(fixture.scopes) } } @@ -262,14 +265,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks OPTIONS request with traceOptionsRequests=true`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = true filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -284,14 +287,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks POST request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.POST) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -310,19 +313,19 @@ class SentryWebFluxTracingFilterTest { fixture.options.enableTracing = false val filter = fixture.getSut(sentryTraceHeader = sentryTraceHeaderString, baggageHeaders = baggageHeaderStrings) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull() ) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) } } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt index 0d4346c5ae..8c5aeb1c0a 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.webflux -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.checkEvent import io.sentry.checkTransaction @@ -160,13 +160,13 @@ open class App { open fun mockTransport() = transport @Bean - open fun hub() = HubAdapter.getInstance() + open fun scopes() = ScopesAdapter.getInstance() @Bean - open fun sentryFilter(hub: IHub) = SentryWebFilter(hub) + open fun sentryFilter(scopes: IScopes) = SentryWebFilter(scopes) @Bean - open fun sentryWebExceptionHandler(hub: IHub) = SentryWebExceptionHandler(hub) + open fun sentryWebExceptionHandler(scopes: IScopes) = SentryWebExceptionHandler(scopes) @Bean open fun sentryScheduleHookRegistrar() = ApplicationRunner { diff --git a/sentry-test-support/api/sentry-test-support.api b/sentry-test-support/api/sentry-test-support.api index ffce23a516..dd1a4b69d3 100644 --- a/sentry-test-support/api/sentry-test-support.api +++ b/sentry-test-support/api/sentry-test-support.api @@ -32,6 +32,7 @@ public final class io/sentry/test/ImmediateExecutorService : io/sentry/ISentryEx } public final class io/sentry/test/ReflectionKt { + public static final fun collectInterfaceHierarchy (Ljava/lang/Class;)Ljava/util/List; public static final fun containsMethod (Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Class;)Z public static final fun containsMethod (Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Z public static final fun getCtor (Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor; diff --git a/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt b/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt index 690dcc8725..19d11676e1 100644 --- a/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt +++ b/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt @@ -12,16 +12,23 @@ inline fun T.callMethod(name: String, parameterTypes: Class<*> val declaredMethod = try { T::class.java.getDeclaredMethod(name, parameterTypes) } catch (e: NoSuchMethodException) { - T::class.java.interfaces.first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, parameterTypes) + collectInterfaceHierarchy(T::class.java).first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, parameterTypes) } return declaredMethod.invoke(this, value) } +fun collectInterfaceHierarchy(clazz: Class<*>): List> { + if (clazz.interfaces.isEmpty()) { + return listOf(clazz) + } + return clazz.interfaces.flatMap { iface -> collectInterfaceHierarchy(iface) }.also { it.toMutableList().add(clazz) } +} + inline fun T.callMethod(name: String, parameterTypes: Array>, vararg value: Any?): Any? { val declaredMethod = try { T::class.java.getDeclaredMethod(name, *parameterTypes) } catch (e: NoSuchMethodException) { - T::class.java.interfaces.first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, *parameterTypes) + collectInterfaceHierarchy(T::class.java).first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, *parameterTypes) } return declaredMethod.invoke(this, *value) } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index bcfc1c9194..52eb5df888 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -293,7 +293,7 @@ public final class io/sentry/EnvelopeReader : io/sentry/IEnvelopeReader { } public final class io/sentry/EnvelopeSender : io/sentry/IEnvelopeSender { - public fun (Lio/sentry/IHub;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V + public fun (Lio/sentry/IScopes;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V public synthetic fun processDirectory (Ljava/io/File;)V public fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } @@ -520,6 +520,59 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public fun withScope (Lio/sentry/ScopeCallback;)V } +public final class io/sentry/HubScopesWrapper : io/sentry/IHub { + public fun (Lio/sentry/IScopes;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public abstract interface class io/sentry/IConnectionStatusProvider { public abstract fun addConnectionStatusObserver (Lio/sentry/IConnectionStatusProvider$IConnectionStatusObserver;)Z public abstract fun getConnectionStatus ()Lio/sentry/IConnectionStatusProvider$ConnectionStatus; @@ -548,71 +601,7 @@ public abstract interface class io/sentry/IEnvelopeSender { public abstract fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } -public abstract interface class io/sentry/IHub { - public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;)V - public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V - public fun addBreadcrumb (Ljava/lang/String;)V - public fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun bindClient (Lio/sentry/ISentryClient;)V - public abstract fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; - public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; - public abstract fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;)Lio/sentry/protocol/SentryId; - public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;)Lio/sentry/protocol/SentryId; - public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; - public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; - public abstract fun captureUserFeedback (Lio/sentry/UserFeedback;)V - public abstract fun clearBreadcrumbs ()V - public abstract fun clone ()Lio/sentry/IHub; - public abstract fun close ()V - public abstract fun close (Z)V - public abstract fun configureScope (Lio/sentry/ScopeCallback;)V - public abstract fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; - public abstract fun endSession ()V - public abstract fun flush (J)V - public abstract fun getBaggage ()Lio/sentry/BaggageHeader; - public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; - public abstract fun getOptions ()Lio/sentry/SentryOptions; - public abstract fun getRateLimiter ()Lio/sentry/transport/RateLimiter; - public abstract fun getSpan ()Lio/sentry/ISpan; - public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader; - public abstract fun getTransaction ()Lio/sentry/ITransaction; - public abstract fun isCrashedLastRun ()Ljava/lang/Boolean; - public abstract fun isEnabled ()Z - public abstract fun isHealthy ()Z - public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; - public abstract fun popScope ()V - public abstract fun pushScope ()V - public abstract fun removeExtra (Ljava/lang/String;)V - public abstract fun removeTag (Ljava/lang/String;)V - public fun reportFullDisplayed ()V - public abstract fun reportFullyDisplayed ()V - public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun setFingerprint (Ljava/util/List;)V - public abstract fun setLevel (Lio/sentry/SentryLevel;)V - public abstract fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V - public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun setTransaction (Ljava/lang/String;)V - public abstract fun setUser (Lio/sentry/protocol/User;)V - public abstract fun startSession ()V - public fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; - public abstract fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; - public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction; - public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; - public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader; - public abstract fun withScope (Lio/sentry/ScopeCallback;)V +public abstract interface class io/sentry/IHub : io/sentry/IScopes { } public abstract interface class io/sentry/ILogger { @@ -731,6 +720,74 @@ public abstract interface class io/sentry/IScopeObserver { public abstract fun setUser (Lio/sentry/protocol/User;)V } +public abstract interface class io/sentry/IScopes { + public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun addBreadcrumb (Ljava/lang/String;)V + public fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun bindClient (Lio/sentry/ISentryClient;)V + public abstract fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; + public abstract fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;)Lio/sentry/protocol/SentryId; + public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;)Lio/sentry/protocol/SentryId; + public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public abstract fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public abstract fun clearBreadcrumbs ()V + public abstract fun clone ()Lio/sentry/IHub; + public abstract fun close ()V + public abstract fun close (Z)V + public abstract fun configureScope (Lio/sentry/ScopeCallback;)V + public abstract fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public abstract fun endSession ()V + public abstract fun flush (J)V + public abstract fun getBaggage ()Lio/sentry/BaggageHeader; + public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; + public abstract fun getOptions ()Lio/sentry/SentryOptions; + public abstract fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public abstract fun getSpan ()Lio/sentry/ISpan; + public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public abstract fun getTransaction ()Lio/sentry/ITransaction; + public abstract fun isCrashedLastRun ()Ljava/lang/Boolean; + public abstract fun isEnabled ()Z + public abstract fun isHealthy ()Z + public fun isNoOp ()Z + public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; + public abstract fun popScope ()V + public abstract fun pushScope ()V + public abstract fun removeExtra (Ljava/lang/String;)V + public abstract fun removeTag (Ljava/lang/String;)V + public fun reportFullDisplayed ()V + public abstract fun reportFullyDisplayed ()V + public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun setFingerprint (Ljava/util/List;)V + public abstract fun setLevel (Lio/sentry/SentryLevel;)V + public abstract fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun setTransaction (Ljava/lang/String;)V + public abstract fun setUser (Lio/sentry/protocol/User;)V + public abstract fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; + public abstract fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction; + public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public abstract fun withScope (Lio/sentry/ScopeCallback;)V +} + public abstract interface class io/sentry/ISentryClient { public abstract fun captureCheckIn (Lio/sentry/CheckIn;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; @@ -853,7 +910,7 @@ public final class io/sentry/Instrumenter : java/lang/Enum { } public abstract interface class io/sentry/Integration { - public abstract fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public abstract fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/IpAddressUtils { @@ -1187,6 +1244,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun isNoOp ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V public fun pushScope ()V @@ -1270,6 +1328,60 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun withTransaction (Lio/sentry/Scope$IWithTransaction;)V } +public final class io/sentry/NoOpScopes : io/sentry/IScopes { + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public static fun getInstance ()Lio/sentry/NoOpScopes; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun isNoOp ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public final class io/sentry/NoOpSpan : io/sentry/ISpan { public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V @@ -1403,7 +1515,7 @@ public final class io/sentry/OptionsContainer { } public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender { - public fun (Lio/sentry/IHub;Lio/sentry/IEnvelopeReader;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V + public fun (Lio/sentry/IScopes;Lio/sentry/IEnvelopeReader;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V public synthetic fun processDirectory (Ljava/io/File;)V public fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } @@ -1668,11 +1780,64 @@ public abstract class io/sentry/ScopeObserverAdapter : io/sentry/IScopeObserver public fun setUser (Lio/sentry/protocol/User;)V } +public final class io/sentry/ScopesAdapter : io/sentry/IScopes { + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public static fun getInstance ()Lio/sentry/ScopesAdapter; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public final class io/sentry/SendCachedEnvelopeFireAndForgetIntegration : io/sentry/IConnectionStatusProvider$IConnectionStatusObserver, io/sentry/Integration, java/io/Closeable { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory;)V public fun close ()V public fun onConnectionStatusChanged (Lio/sentry/IConnectionStatusProvider$ConnectionStatus;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget { @@ -1684,19 +1849,19 @@ public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegra } public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { - public abstract fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public abstract fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; public fun hasValidPath (Ljava/lang/String;Lio/sentry/ILogger;)Z public fun processDir (Lio/sentry/DirectoryProcessor;Ljava/lang/String;Lio/sentry/ILogger;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/SendFireAndForgetEnvelopeSender : io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetDirPath;)V - public fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/SendFireAndForgetOutboxSender : io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetDirPath;)V - public fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/Sentry { @@ -1721,7 +1886,7 @@ public final class io/sentry/Sentry { public static fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; public static fun captureUserFeedback (Lio/sentry/UserFeedback;)V public static fun clearBreadcrumbs ()V - public static fun cloneMainHub ()Lio/sentry/IHub; + public static fun cloneMainHub ()Lio/sentry/IScopes; public static fun close ()V public static fun configureScope (Lio/sentry/ScopeCallback;)V public static fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; @@ -1729,6 +1894,7 @@ public final class io/sentry/Sentry { public static fun flush (J)V public static fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getCurrentHub ()Lio/sentry/IHub; + public static fun getCurrentScopes ()Lio/sentry/IScopes; public static fun getLastEventId ()Lio/sentry/protocol/SentryId; public static fun getSpan ()Lio/sentry/ISpan; public static fun getTraceparent ()Lio/sentry/SentryTraceHeader; @@ -1750,6 +1916,7 @@ public final class io/sentry/Sentry { public static fun reportFullDisplayed ()V public static fun reportFullyDisplayed ()V public static fun setCurrentHub (Lio/sentry/IHub;)V + public static fun setCurrentScopes (Lio/sentry/IScopes;)V public static fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public static fun setFingerprint (Ljava/util/List;)V public static fun setLevel (Lio/sentry/SentryLevel;)V @@ -2500,8 +2667,8 @@ public final class io/sentry/SentryTraceHeader { } public final class io/sentry/SentryTracer : io/sentry/ITransaction { - public fun (Lio/sentry/TransactionContext;Lio/sentry/IHub;)V - public fun (Lio/sentry/TransactionContext;Lio/sentry/IHub;Lio/sentry/TransactionOptions;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/IScopes;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/IScopes;Lio/sentry/TransactionOptions;)V public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V @@ -2630,11 +2797,11 @@ public final class io/sentry/ShutdownHookIntegration : io/sentry/Integration, ja public fun ()V public fun (Ljava/lang/Runtime;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/Span : io/sentry/ISpan { - public fun (Lio/sentry/TransactionContext;Lio/sentry/SentryTracer;Lio/sentry/IHub;Lio/sentry/SentryDate;Lio/sentry/SpanOptions;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/SentryTracer;Lio/sentry/IScopes;Lio/sentry/SentryDate;Lio/sentry/SpanOptions;)V public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V @@ -2815,7 +2982,7 @@ public final class io/sentry/SpotlightIntegration : io/sentry/Integration, io/se public fun close ()V public fun execute (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)V public fun getSpotlightConnectionUrl ()Ljava/lang/String; - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/SystemOutLogger : io/sentry/ILogger { @@ -2975,7 +3142,7 @@ public final class io/sentry/TypeCheckHint { public final class io/sentry/UncaughtExceptionHandlerIntegration : io/sentry/Integration, java/io/Closeable, java/lang/Thread$UncaughtExceptionHandler { public fun ()V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V public fun uncaughtException (Ljava/lang/Thread;Ljava/lang/Throwable;)V } @@ -3016,7 +3183,7 @@ public final class io/sentry/UserFeedback$JsonKeys { } public final class io/sentry/backpressure/BackpressureMonitor : io/sentry/backpressure/IBackpressureMonitor, java/lang/Runnable { - public fun (Lio/sentry/SentryOptions;Lio/sentry/IHub;)V + public fun (Lio/sentry/SentryOptions;Lio/sentry/IScopes;)V public fun getDownsampleFactor ()I public fun run ()V public fun start ()V @@ -5102,9 +5269,9 @@ public final class io/sentry/util/StringUtils { public final class io/sentry/util/TracingUtils { public fun ()V public static fun maybeUpdateBaggage (Lio/sentry/IScope;Lio/sentry/SentryOptions;)Lio/sentry/PropagationContext; - public static fun startNewTrace (Lio/sentry/IHub;)V - public static fun trace (Lio/sentry/IHub;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; - public static fun traceIfAllowed (Lio/sentry/IHub;Ljava/lang/String;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; + public static fun startNewTrace (Lio/sentry/IScopes;)V + public static fun trace (Lio/sentry/IScopes;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; + public static fun traceIfAllowed (Lio/sentry/IScopes;Ljava/lang/String;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; } public final class io/sentry/util/TracingUtils$TracingHeaders { diff --git a/sentry/src/main/java/io/sentry/Baggage.java b/sentry/src/main/java/io/sentry/Baggage.java index 8e19fceaf8..4a637bacdf 100644 --- a/sentry/src/main/java/io/sentry/Baggage.java +++ b/sentry/src/main/java/io/sentry/Baggage.java @@ -39,13 +39,13 @@ public final class Baggage { @NotNull public static Baggage fromHeader(final @Nullable String headerValue) { return Baggage.fromHeader( - headerValue, false, HubAdapter.getInstance().getOptions().getLogger()); + headerValue, false, ScopesAdapter.getInstance().getOptions().getLogger()); } @NotNull public static Baggage fromHeader(final @Nullable List headerValues) { return Baggage.fromHeader( - headerValues, false, HubAdapter.getInstance().getOptions().getLogger()); + headerValues, false, ScopesAdapter.getInstance().getOptions().getLogger()); } @ApiStatus.Internal diff --git a/sentry/src/main/java/io/sentry/DirectoryProcessor.java b/sentry/src/main/java/io/sentry/DirectoryProcessor.java index 5d60feba60..a6bb258f30 100644 --- a/sentry/src/main/java/io/sentry/DirectoryProcessor.java +++ b/sentry/src/main/java/io/sentry/DirectoryProcessor.java @@ -19,17 +19,17 @@ abstract class DirectoryProcessor { private static final long ENVELOPE_PROCESSING_DELAY = 100L; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ILogger logger; private final long flushTimeoutMillis; private final Queue processedEnvelopes; DirectoryProcessor( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - this.hub = hub; + this.scopes = scopes; this.logger = logger; this.flushTimeoutMillis = flushTimeoutMillis; this.processedEnvelopes = @@ -86,7 +86,7 @@ public void processDirectory(final @NotNull File directory) { } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { logger.log(SentryLevel.INFO, "DirectoryProcessor, rate limiting active."); return; diff --git a/sentry/src/main/java/io/sentry/EnvelopeSender.java b/sentry/src/main/java/io/sentry/EnvelopeSender.java index 598caad280..3a157f59d3 100644 --- a/sentry/src/main/java/io/sentry/EnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/EnvelopeSender.java @@ -17,18 +17,18 @@ @ApiStatus.Internal public final class EnvelopeSender extends DirectoryProcessor implements IEnvelopeSender { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ISerializer serializer; private final @NotNull ILogger logger; public EnvelopeSender( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ISerializer serializer, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - super(hub, logger, flushTimeoutMillis, maxQueueSize); - this.hub = Objects.requireNonNull(hub, "Hub is required."); + super(scopes, logger, flushTimeoutMillis, maxQueueSize); + this.scopes = Objects.requireNonNull(scopes, "Hub is required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); } @@ -60,7 +60,7 @@ protected void processFile(final @NotNull File file, final @NotNull Hint hint) { logger.log( SentryLevel.ERROR, "Failed to deserialize cached envelope %s", file.getAbsolutePath()); } else { - hub.captureEnvelope(envelope, hint); + scopes.captureEnvelope(envelope, hint); } HintUtils.runIfHasTypeLogIfNot( diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 6b6da88f09..6a98bb2367 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +@Deprecated public final class Hub implements IHub, MetricsApi.IMetricsInterface { private volatile @NotNull SentryId lastEventId; diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index b31d853192..746d51f0cc 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -10,6 +10,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * @deprecated use {@link ScopesAdapter} instead + */ +@Deprecated public final class HubAdapter implements IHub { private static final HubAdapter INSTANCE = new HubAdapter(); @@ -50,7 +54,7 @@ public boolean isEnabled() { @ApiStatus.Internal @Override public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { - return Sentry.getCurrentHub().captureEnvelope(envelope, hint); + return Sentry.getCurrentScopes().captureEnvelope(envelope, hint); } @Override @@ -186,7 +190,7 @@ public void flush(long timeoutMillis) { @Override public @NotNull IHub clone() { - return Sentry.getCurrentHub().clone(); + return Sentry.getCurrentScopes().clone(); } @Override @@ -195,7 +199,7 @@ public void flush(long timeoutMillis) { @Nullable TraceContext traceContext, @Nullable Hint hint, @Nullable ProfilingTraceData profilingTraceData) { - return Sentry.getCurrentHub() + return Sentry.getCurrentScopes() .captureTransaction(transaction, traceContext, hint, profilingTraceData); } @@ -217,23 +221,23 @@ public void setSpanContext( final @NotNull Throwable throwable, final @NotNull ISpan span, final @NotNull String transactionName) { - Sentry.getCurrentHub().setSpanContext(throwable, span, transactionName); + Sentry.getCurrentScopes().setSpanContext(throwable, span, transactionName); } @Override public @Nullable ISpan getSpan() { - return Sentry.getCurrentHub().getSpan(); + return Sentry.getCurrentScopes().getSpan(); } @Override @ApiStatus.Internal public @Nullable ITransaction getTransaction() { - return Sentry.getCurrentHub().getTransaction(); + return Sentry.getCurrentScopes().getTransaction(); } @Override public @NotNull SentryOptions getOptions() { - return Sentry.getCurrentHub().getOptions(); + return Sentry.getCurrentScopes().getOptions(); } @Override @@ -271,11 +275,11 @@ public void reportFullyDisplayed() { @ApiStatus.Internal @Override public @Nullable RateLimiter getRateLimiter() { - return Sentry.getCurrentHub().getRateLimiter(); + return Sentry.getCurrentScopes().getRateLimiter(); } @Override public @NotNull MetricsApi metrics() { - return Sentry.getCurrentHub().metrics(); + return Sentry.getCurrentScopes().metrics(); } } diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java new file mode 100644 index 0000000000..9b294d05b7 --- /dev/null +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -0,0 +1,279 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@SuppressWarnings("deprecation") +@Deprecated +public final class HubScopesWrapper implements IHub { + + private final IScopes scopes; + + public HubScopesWrapper(final @NotNull IScopes scopes) { + this.scopes = scopes; + } + + @Override + public boolean isEnabled() { + return scopes.isEnabled(); + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return scopes.captureEvent(event, hint); + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return scopes.captureEvent(event, hint, callback); + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return scopes.captureMessage(message, level); + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return scopes.captureMessage(message, level, callback); + } + + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return scopes.captureEnvelope(envelope, hint); + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return scopes.captureException(throwable, hint); + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return scopes.captureException(throwable, hint, callback); + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) { + scopes.captureUserFeedback(userFeedback); + } + + @Override + public void startSession() { + scopes.startSession(); + } + + @Override + public void endSession() { + scopes.endSession(); + } + + @Override + public void close() { + scopes.close(); + } + + @Override + public void close(boolean isRestarting) { + scopes.close(isRestarting); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { + scopes.addBreadcrumb(breadcrumb, hint); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb) { + scopes.addBreadcrumb(breadcrumb); + } + + @Override + public void setLevel(@Nullable SentryLevel level) { + scopes.setLevel(level); + } + + @Override + public void setTransaction(@Nullable String transaction) { + scopes.setTransaction(transaction); + } + + @Override + public void setUser(@Nullable User user) { + scopes.setUser(user); + } + + @Override + public void setFingerprint(@NotNull List fingerprint) { + scopes.setFingerprint(fingerprint); + } + + @Override + public void clearBreadcrumbs() { + scopes.clearBreadcrumbs(); + } + + @Override + public void setTag(@NotNull String key, @NotNull String value) { + scopes.setTag(key, value); + } + + @Override + public void removeTag(@NotNull String key) { + scopes.removeTag(key); + } + + @Override + public void setExtra(@NotNull String key, @NotNull String value) { + scopes.setExtra(key, value); + } + + @Override + public void removeExtra(@NotNull String key) { + scopes.removeExtra(key); + } + + @Override + public @NotNull SentryId getLastEventId() { + return scopes.getLastEventId(); + } + + @Override + public void pushScope() { + scopes.pushScope(); + } + + @Override + public void popScope() { + scopes.popScope(); + } + + @Override + public void withScope(@NotNull ScopeCallback callback) { + scopes.withScope(callback); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) { + scopes.configureScope(callback); + } + + @Override + public void bindClient(@NotNull ISentryClient client) { + scopes.bindClient(client); + } + + @Override + public boolean isHealthy() { + return scopes.isHealthy(); + } + + @Override + public void flush(long timeoutMillis) { + scopes.flush(timeoutMillis); + } + + @Override + public @NotNull IHub clone() { + return scopes.clone(); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData) { + return scopes.captureTransaction(transaction, traceContext, hint, profilingTraceData); + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return scopes.startTransaction(transactionContext, transactionOptions); + } + + @Override + public @Nullable SentryTraceHeader traceHeaders() { + return scopes.traceHeaders(); + } + + @ApiStatus.Internal + @Override + public void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) { + scopes.setSpanContext(throwable, span, transactionName); + } + + @Override + public @Nullable ISpan getSpan() { + return scopes.getSpan(); + } + + @ApiStatus.Internal + @Override + public @Nullable ITransaction getTransaction() { + return scopes.getTransaction(); + } + + @Override + public @NotNull SentryOptions getOptions() { + return scopes.getOptions(); + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return scopes.isCrashedLastRun(); + } + + @Override + public void reportFullyDisplayed() { + scopes.reportFullyDisplayed(); + } + + @Override + public @Nullable TransactionContext continueTrace( + @Nullable String sentryTrace, @Nullable List baggageHeaders) { + return scopes.continueTrace(sentryTrace, baggageHeaders); + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return scopes.getTraceparent(); + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return scopes.getBaggage(); + } + + @ApiStatus.Experimental + @Override + public @NotNull SentryId captureCheckIn(@NotNull CheckIn checkIn) { + return scopes.captureCheckIn(checkIn); + } + + @ApiStatus.Internal + @Override + public @Nullable RateLimiter getRateLimiter() { + return scopes.getRateLimiter(); + } + + @ApiStatus.Experimental + @Override + public @NotNull MetricsApi metrics() { + return scopes.metrics(); + } +} diff --git a/sentry/src/main/java/io/sentry/IHub.java b/sentry/src/main/java/io/sentry/IHub.java index 684d8ec528..23a7bed7de 100644 --- a/sentry/src/main/java/io/sentry/IHub.java +++ b/sentry/src/main/java/io/sentry/IHub.java @@ -1,590 +1,9 @@ package io.sentry; -import io.sentry.metrics.MetricsApi; -import io.sentry.protocol.SentryId; -import io.sentry.protocol.SentryTransaction; -import io.sentry.protocol.User; -import io.sentry.transport.RateLimiter; -import java.util.List; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** SDK API contract which combines a client and scope management */ -public interface IHub { - - /** - * Check if the Hub is enabled/active. - * - * @return true if its enabled or false otherwise. - */ - boolean isEnabled(); - - /** - * Captures the event. - * - * @param event the event - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint); - - /** - * Captures the event. - * - * @param event the event - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEvent(@NotNull SentryEvent event) { - return captureEvent(event, new Hint()); - } - - /** - * Captures the event. - * - * @param event the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEvent( - @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { - return captureEvent(event, new Hint(), callback); - } - - /** - * Captures the event. - * - * @param event the event - * @param hint SDK specific but provides high level information about the origin of the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEvent( - final @NotNull SentryEvent event, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback); - - /** - * Captures the message. - * - * @param message The message to send. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureMessage(@NotNull String message) { - return captureMessage(message, SentryLevel.INFO); - } - - /** - * Captures the message. - * - * @param message The message to send. - * @param level The message level. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level); - - /** - * Captures the message. - * - * @param message The message to send. - * @param level The message level. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureMessage( - @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback); - - /** - * Captures the message. - * - * @param message The message to send. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureMessage( - @NotNull String message, @NotNull ScopeCallback callback) { - return captureMessage(message, SentryLevel.INFO, callback); - } - - /** - * Captures an envelope. - * - * @param envelope the SentryEnvelope to send. - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint); - - /** - * Captures an envelope. - * - * @param envelope the SentryEnvelope to send. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope) { - return captureEnvelope(envelope, new Hint()); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint); - - /** - * Captures the exception. - * - * @param throwable The exception. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureException(@NotNull Throwable throwable) { - return captureException(throwable, new Hint()); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureException( - @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { - return captureException(throwable, new Hint(), callback); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param hint SDK specific but provides high level information about the origin of the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureException( - final @NotNull Throwable throwable, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback); - - /** - * Captures a manually created user feedback and sends it to Sentry. - * - * @param userFeedback The user feedback to send to Sentry. - */ - void captureUserFeedback(@NotNull UserFeedback userFeedback); - - /** Starts a new session. If there's a running session, it ends it before starting the new one. */ - void startSession(); - - /** Ends the current session */ - void endSession(); - - /** Flushes out the queue for up to timeout seconds and disable the Hub. */ - void close(); - - /** - * Flushes out the queue for up to timeout seconds and disable the Hub. - * - * @param isRestarting if true, avoids locking the main thread when finishing the queue. - */ - void close(boolean isRestarting); - - /** - * Adds a breadcrumb to the current Scope - * - * @param breadcrumb the breadcrumb - * @param hint SDK specific but provides high level information about the origin of the event - */ - void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint); - - /** - * Adds a breadcrumb to the current Scope - * - * @param breadcrumb the breadcrumb - */ - void addBreadcrumb(@NotNull Breadcrumb breadcrumb); - - /** - * Adds a breadcrumb to the current Scope - * - * @param message rendered as text and the whitespace is preserved. - */ - default void addBreadcrumb(@NotNull String message) { - addBreadcrumb(new Breadcrumb(message)); - } - - /** - * Adds a breadcrumb to the current Scope - * - * @param message rendered as text and the whitespace is preserved. - * @param category Categories are dotted strings that indicate what the crumb is or where it comes - * from. - */ - default void addBreadcrumb(@NotNull String message, @NotNull String category) { - Breadcrumb breadcrumb = new Breadcrumb(message); - breadcrumb.setCategory(category); - addBreadcrumb(breadcrumb); - } - - /** - * Sets the level of all events sent within current Scope - * - * @param level the Sentry level - */ - void setLevel(@Nullable SentryLevel level); - - /** - * Sets the name of the current transaction to the current Scope. - * - * @param transaction the transaction - */ - void setTransaction(@Nullable String transaction); - - /** - * Shallow merges user configuration (email, username, etc) to the current Scope. - * - * @param user the user - */ - void setUser(@Nullable User user); - - /** - * Sets the fingerprint to group specific events together to the current Scope. - * - * @param fingerprint the fingerprints - */ - void setFingerprint(@NotNull List fingerprint); - - /** Deletes current breadcrumbs from the current scope. */ - void clearBreadcrumbs(); - - /** - * Sets the tag to a string value to the current Scope, overwriting a potential previous value - * - * @param key the key - * @param value the value - */ - void setTag(@NotNull String key, @NotNull String value); - - /** - * Removes the tag to a string value to the current Scope - * - * @param key the key - */ - void removeTag(@NotNull String key); - - /** - * Sets the extra key to an arbitrary value to the current Scope, overwriting a potential previous - * value - * - * @param key the key - * @param value the value - */ - void setExtra(@NotNull String key, @NotNull String value); - - /** - * Removes the extra key to an arbitrary value to the current Scope - * - * @param key the key - */ - void removeExtra(@NotNull String key); - - /** - * Last event id recorded in the current scope - * - * @return last SentryId - */ - @NotNull - SentryId getLastEventId(); - - /** Pushes a new scope while inheriting the current scope's data. */ - void pushScope(); - - /** Removes the first scope */ - void popScope(); - - /** - * Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry - * SDK in globalHubMode (defaults to true on Android) {@link - * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope - * changes may be dropped when executed in parallel. Use {@link - * IHub#configureScope(ScopeCallback)} instead. - * - * @param callback the callback - */ - void withScope(@NotNull ScopeCallback callback); - - /** - * Configures the scope through the callback. - * - * @param callback The configure scope callback. - */ - void configureScope(@NotNull ScopeCallback callback); - - /** - * Binds a different client to the hub - * - * @param client the client. - */ - void bindClient(@NotNull ISentryClient client); - - /** - * Whether the transport is healthy. - * - * @return true if the transport is healthy - */ - boolean isHealthy(); - - /** - * Flushes events queued up, but keeps the Hub enabled. Not implemented yet. - * - * @param timeoutMillis time in milliseconds - */ - void flush(long timeoutMillis); - - /** - * Clones the Hub - * - * @return the cloned Hub - */ - @NotNull - IHub clone(); - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @param hint the hints - * @param profilingTraceData the profiling trace data - * @return transaction's id - */ - @ApiStatus.Internal - @NotNull - SentryId captureTransaction( - @NotNull SentryTransaction transaction, - @Nullable TraceContext traceContext, - @Nullable Hint hint, - @Nullable ProfilingTraceData profilingTraceData); - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @param hint the hints - * @return transaction's id - */ - @ApiStatus.Internal - @NotNull - default SentryId captureTransaction( - @NotNull SentryTransaction transaction, - @Nullable TraceContext traceContext, - @Nullable Hint hint) { - return captureTransaction(transaction, traceContext, hint, null); - } - - @ApiStatus.Internal - @NotNull - default SentryId captureTransaction(@NotNull SentryTransaction transaction, @Nullable Hint hint) { - return captureTransaction(transaction, null, hint); - } - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @return transaction's id - */ - @ApiStatus.Internal - default @NotNull SentryId captureTransaction( - @NotNull SentryTransaction transaction, @Nullable TraceContext traceContext) { - return captureTransaction(transaction, traceContext, null); - } - - /** - * Creates a Transaction and returns the instance. - * - * @param transactionContexts the transaction contexts - * @return created transaction - */ - default @NotNull ITransaction startTransaction(@NotNull TransactionContext transactionContexts) { - return startTransaction(transactionContexts, new TransactionOptions()); - } - - /** - * Creates a Transaction and returns the instance. Based on the {@link - * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by - * {@link TracesSampler}. - * - * @param name the transaction name - * @param operation the operation - * @return created transaction - */ - default @NotNull ITransaction startTransaction( - final @NotNull String name, final @NotNull String operation) { - return startTransaction(name, operation, new TransactionOptions()); - } - - /** - * Creates a Transaction and returns the instance. Based on the {@link - * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by - * {@link TracesSampler}. - * - * @param name the transaction name - * @param operation the operation - * @param transactionOptions the transaction options - * @return created transaction - */ - default @NotNull ITransaction startTransaction( - final @NotNull String name, - final @NotNull String operation, - final @NotNull TransactionOptions transactionOptions) { - return startTransaction(new TransactionContext(name, operation), transactionOptions); - } - - /** - * Creates a Transaction and returns the instance. Based on the passed transaction context and - * transaction options the decision if transaction is sampled will be taken by {@link - * TracesSampler}. - * - * @param transactionContext the transaction context - * @param transactionOptions the transaction options - * @return created transaction. - */ - @NotNull - ITransaction startTransaction( - final @NotNull TransactionContext transactionContext, - final @NotNull TransactionOptions transactionOptions); - - /** - * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. - * - * @deprecated please use {@link IHub#getTraceparent()} instead. - * @return sentry trace header or null - */ - @Deprecated - @Nullable - SentryTraceHeader traceHeaders(); - - /** - * Associates {@link ISpan} and the transaction name with the {@link Throwable}. Used to determine - * in which trace the exception has been thrown in framework integrations. - * - * @param throwable the throwable - * @param span the span context - * @param transactionName the transaction name - */ - @ApiStatus.Internal - void setSpanContext( - @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName); - - /** - * Gets the current active transaction or span. - * - * @return the active span or null when no active transaction is running - */ - @Nullable - ISpan getSpan(); - - /** - * Returns the transaction. - * - * @return the transaction or null when no active transaction is running. - */ - @ApiStatus.Internal - @Nullable - ITransaction getTransaction(); - - /** - * Gets the {@link SentryOptions} attached to current scope. - * - * @return the options attached to current scope. - */ - @NotNull - SentryOptions getOptions(); - - /** - * Returns if the App has crashed (Process has terminated) during the last run. It only returns - * true or false if offline caching {{@link SentryOptions#getCacheDirPath()} } is set with a valid - * dir. - * - *

If the call to this method is early in the App lifecycle and the SDK could not check if the - * App has crashed in the background, the check is gonna do IO in the calling thread. - * - * @return true if App has crashed, false otherwise, and null if not evaluated yet - */ - @Nullable - Boolean isCrashedLastRun(); - - /** - * Report a screen has been fully loaded. That means all data needed by the UI was loaded. If - * time-to-full-display tracing {{@link SentryOptions#isEnableTimeToFullDisplayTracing()} } is - * disabled this call is ignored. - * - *

This method is safe to be called multiple times. If the time-to-full-display span is already - * finished, this call will be ignored. - */ - void reportFullyDisplayed(); - - /** - * @deprecated See {@link IHub#reportFullyDisplayed()}. - */ - @Deprecated - default void reportFullDisplayed() { - reportFullyDisplayed(); - } - - /** - * Continue a trace based on HTTP header values. If no "sentry-trace" header is provided a random - * trace ID and span ID is created. - * - * @param sentryTrace "sentry-trace" header - * @param baggageHeaders "baggage" headers - * @return a transaction context for starting a transaction or null if performance is disabled - */ - @Nullable - TransactionContext continueTrace( - final @Nullable String sentryTrace, final @Nullable List baggageHeaders); - - /** - * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. - * - * @return sentry trace header or null - */ - @Nullable - SentryTraceHeader getTraceparent(); - - /** - * Returns the "baggage" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getTraceparent()}. - * - * @return baggage header or null - */ - @Nullable - BaggageHeader getBaggage(); - - @ApiStatus.Experimental - @NotNull - SentryId captureCheckIn(final @NotNull CheckIn checkIn); - - @ApiStatus.Internal - @Nullable - RateLimiter getRateLimiter(); - - @ApiStatus.Experimental - @NotNull - MetricsApi metrics(); -} +/** + * SDK API contract which combines a client and scope management + * + * @deprecated please use {@link IScopes} instead + */ +@Deprecated +public interface IHub extends IScopes {} diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java new file mode 100644 index 0000000000..03662457e4 --- /dev/null +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -0,0 +1,594 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface IScopes { + + /** + * Check if the Hub is enabled/active. + * + * @return true if its enabled or false otherwise. + */ + boolean isEnabled(); + + /** + * Captures the event. + * + * @param event the event + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint); + + /** + * Captures the event. + * + * @param event the event + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEvent(@NotNull SentryEvent event) { + return captureEvent(event, new Hint()); + } + + /** + * Captures the event. + * + * @param event the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEvent( + @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { + return captureEvent(event, new Hint(), callback); + } + + /** + * Captures the event. + * + * @param event the event + * @param hint SDK specific but provides high level information about the origin of the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEvent( + final @NotNull SentryEvent event, + final @Nullable Hint hint, + final @NotNull ScopeCallback callback); + + /** + * Captures the message. + * + * @param message The message to send. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureMessage(@NotNull String message) { + return captureMessage(message, SentryLevel.INFO); + } + + /** + * Captures the message. + * + * @param message The message to send. + * @param level The message level. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level); + + /** + * Captures the message. + * + * @param message The message to send. + * @param level The message level. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback); + + /** + * Captures the message. + * + * @param message The message to send. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureMessage( + @NotNull String message, @NotNull ScopeCallback callback) { + return captureMessage(message, SentryLevel.INFO, callback); + } + + /** + * Captures an envelope. + * + * @param envelope the SentryEnvelope to send. + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint); + + /** + * Captures an envelope. + * + * @param envelope the SentryEnvelope to send. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope) { + return captureEnvelope(envelope, new Hint()); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint); + + /** + * Captures the exception. + * + * @param throwable The exception. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureException(@NotNull Throwable throwable) { + return captureException(throwable, new Hint()); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureException( + @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { + return captureException(throwable, new Hint(), callback); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param hint SDK specific but provides high level information about the origin of the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureException( + final @NotNull Throwable throwable, + final @Nullable Hint hint, + final @NotNull ScopeCallback callback); + + /** + * Captures a manually created user feedback and sends it to Sentry. + * + * @param userFeedback The user feedback to send to Sentry. + */ + void captureUserFeedback(@NotNull UserFeedback userFeedback); + + /** Starts a new session. If there's a running session, it ends it before starting the new one. */ + void startSession(); + + /** Ends the current session */ + void endSession(); + + /** Flushes out the queue for up to timeout seconds and disable the Hub. */ + void close(); + + /** + * Flushes out the queue for up to timeout seconds and disable the Hub. + * + * @param isRestarting if true, avoids locking the main thread when finishing the queue. + */ + void close(boolean isRestarting); + + /** + * Adds a breadcrumb to the current Scope + * + * @param breadcrumb the breadcrumb + * @param hint SDK specific but provides high level information about the origin of the event + */ + void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint); + + /** + * Adds a breadcrumb to the current Scope + * + * @param breadcrumb the breadcrumb + */ + void addBreadcrumb(@NotNull Breadcrumb breadcrumb); + + /** + * Adds a breadcrumb to the current Scope + * + * @param message rendered as text and the whitespace is preserved. + */ + default void addBreadcrumb(@NotNull String message) { + addBreadcrumb(new Breadcrumb(message)); + } + + /** + * Adds a breadcrumb to the current Scope + * + * @param message rendered as text and the whitespace is preserved. + * @param category Categories are dotted strings that indicate what the crumb is or where it comes + * from. + */ + default void addBreadcrumb(@NotNull String message, @NotNull String category) { + Breadcrumb breadcrumb = new Breadcrumb(message); + breadcrumb.setCategory(category); + addBreadcrumb(breadcrumb); + } + + /** + * Sets the level of all events sent within current Scope + * + * @param level the Sentry level + */ + void setLevel(@Nullable SentryLevel level); + + /** + * Sets the name of the current transaction to the current Scope. + * + * @param transaction the transaction + */ + void setTransaction(@Nullable String transaction); + + /** + * Shallow merges user configuration (email, username, etc) to the current Scope. + * + * @param user the user + */ + void setUser(@Nullable User user); + + /** + * Sets the fingerprint to group specific events together to the current Scope. + * + * @param fingerprint the fingerprints + */ + void setFingerprint(@NotNull List fingerprint); + + /** Deletes current breadcrumbs from the current scope. */ + void clearBreadcrumbs(); + + /** + * Sets the tag to a string value to the current Scope, overwriting a potential previous value + * + * @param key the key + * @param value the value + */ + void setTag(@NotNull String key, @NotNull String value); + + /** + * Removes the tag to a string value to the current Scope + * + * @param key the key + */ + void removeTag(@NotNull String key); + + /** + * Sets the extra key to an arbitrary value to the current Scope, overwriting a potential previous + * value + * + * @param key the key + * @param value the value + */ + void setExtra(@NotNull String key, @NotNull String value); + + /** + * Removes the extra key to an arbitrary value to the current Scope + * + * @param key the key + */ + void removeExtra(@NotNull String key); + + /** + * Last event id recorded in the current scope + * + * @return last SentryId + */ + @NotNull + SentryId getLastEventId(); + + /** Pushes a new scope while inheriting the current scope's data. */ + void pushScope(); + + /** Removes the first scope */ + void popScope(); + + /** + * Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry + * SDK in globalHubMode (defaults to true on Android) {@link + * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope + * changes may be dropped when executed in parallel. Use {@link + * IHub#configureScope(ScopeCallback)} instead. + * + * @param callback the callback + */ + void withScope(@NotNull ScopeCallback callback); + + /** + * Configures the scope through the callback. + * + * @param callback The configure scope callback. + */ + void configureScope(@NotNull ScopeCallback callback); + + /** + * Binds a different client to the hub + * + * @param client the client. + */ + void bindClient(@NotNull ISentryClient client); + + /** + * Whether the transport is healthy. + * + * @return true if the transport is healthy + */ + boolean isHealthy(); + + /** + * Flushes events queued up, but keeps the Hub enabled. Not implemented yet. + * + * @param timeoutMillis time in milliseconds + */ + void flush(long timeoutMillis); + + /** + * Clones the Hub + * + * @return the cloned Hub + */ + @NotNull + @Deprecated + IHub clone(); + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @param hint the hints + * @param profilingTraceData the profiling trace data + * @return transaction's id + */ + @ApiStatus.Internal + @NotNull + SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData); + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @param hint the hints + * @return transaction's id + */ + @ApiStatus.Internal + @NotNull + default SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint) { + return captureTransaction(transaction, traceContext, hint, null); + } + + @ApiStatus.Internal + @NotNull + default SentryId captureTransaction(@NotNull SentryTransaction transaction, @Nullable Hint hint) { + return captureTransaction(transaction, null, hint); + } + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @return transaction's id + */ + @ApiStatus.Internal + default @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, @Nullable TraceContext traceContext) { + return captureTransaction(transaction, traceContext, null); + } + + /** + * Creates a Transaction and returns the instance. + * + * @param transactionContexts the transaction contexts + * @return created transaction + */ + default @NotNull ITransaction startTransaction(@NotNull TransactionContext transactionContexts) { + return startTransaction(transactionContexts, new TransactionOptions()); + } + + /** + * Creates a Transaction and returns the instance. Based on the {@link + * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by + * {@link TracesSampler}. + * + * @param name the transaction name + * @param operation the operation + * @return created transaction + */ + default @NotNull ITransaction startTransaction( + final @NotNull String name, final @NotNull String operation) { + return startTransaction(name, operation, new TransactionOptions()); + } + + /** + * Creates a Transaction and returns the instance. Based on the {@link + * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by + * {@link TracesSampler}. + * + * @param name the transaction name + * @param operation the operation + * @param transactionOptions the transaction options + * @return created transaction + */ + default @NotNull ITransaction startTransaction( + final @NotNull String name, + final @NotNull String operation, + final @NotNull TransactionOptions transactionOptions) { + return startTransaction(new TransactionContext(name, operation), transactionOptions); + } + + /** + * Creates a Transaction and returns the instance. Based on the passed transaction context and + * transaction options the decision if transaction is sampled will be taken by {@link + * TracesSampler}. + * + * @param transactionContext the transaction context + * @param transactionOptions the transaction options + * @return created transaction. + */ + @NotNull + ITransaction startTransaction( + final @NotNull TransactionContext transactionContext, + final @NotNull TransactionOptions transactionOptions); + + /** + * Returns the "sentry-trace" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * + * @deprecated please use {@link IHub#getTraceparent()} instead. + * @return sentry trace header or null + */ + @Deprecated + @Nullable + SentryTraceHeader traceHeaders(); + + /** + * Associates {@link ISpan} and the transaction name with the {@link Throwable}. Used to determine + * in which trace the exception has been thrown in framework integrations. + * + * @param throwable the throwable + * @param span the span context + * @param transactionName the transaction name + */ + @ApiStatus.Internal + void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName); + + /** + * Gets the current active transaction or span. + * + * @return the active span or null when no active transaction is running + */ + @Nullable + ISpan getSpan(); + + /** + * Returns the transaction. + * + * @return the transaction or null when no active transaction is running. + */ + @ApiStatus.Internal + @Nullable + ITransaction getTransaction(); + + /** + * Gets the {@link SentryOptions} attached to current scope. + * + * @return the options attached to current scope. + */ + @NotNull + SentryOptions getOptions(); + + /** + * Returns if the App has crashed (Process has terminated) during the last run. It only returns + * true or false if offline caching {{@link SentryOptions#getCacheDirPath()} } is set with a valid + * dir. + * + *

If the call to this method is early in the App lifecycle and the SDK could not check if the + * App has crashed in the background, the check is gonna do IO in the calling thread. + * + * @return true if App has crashed, false otherwise, and null if not evaluated yet + */ + @Nullable + Boolean isCrashedLastRun(); + + /** + * Report a screen has been fully loaded. That means all data needed by the UI was loaded. If + * time-to-full-display tracing {{@link SentryOptions#isEnableTimeToFullDisplayTracing()} } is + * disabled this call is ignored. + * + *

This method is safe to be called multiple times. If the time-to-full-display span is already + * finished, this call will be ignored. + */ + void reportFullyDisplayed(); + + /** + * @deprecated See {@link IHub#reportFullyDisplayed()}. + */ + @Deprecated + default void reportFullDisplayed() { + reportFullyDisplayed(); + } + + /** + * Continue a trace based on HTTP header values. If no "sentry-trace" header is provided a random + * trace ID and span ID is created. + * + * @param sentryTrace "sentry-trace" header + * @param baggageHeaders "baggage" headers + * @return a transaction context for starting a transaction or null if performance is disabled + */ + @Nullable + TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders); + + /** + * Returns the "sentry-trace" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * + * @return sentry trace header or null + */ + @Nullable + SentryTraceHeader getTraceparent(); + + /** + * Returns the "baggage" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getTraceparent()}. + * + * @return baggage header or null + */ + @Nullable + BaggageHeader getBaggage(); + + @ApiStatus.Experimental + @NotNull + SentryId captureCheckIn(final @NotNull CheckIn checkIn); + + @ApiStatus.Internal + @Nullable + RateLimiter getRateLimiter(); + + @ApiStatus.Experimental + @NotNull + MetricsApi metrics(); + + default boolean isNoOp() { + return false; + } +} diff --git a/sentry/src/main/java/io/sentry/Integration.java b/sentry/src/main/java/io/sentry/Integration.java index 54b17e4d51..1b1a520473 100644 --- a/sentry/src/main/java/io/sentry/Integration.java +++ b/sentry/src/main/java/io/sentry/Integration.java @@ -10,8 +10,8 @@ public interface Integration { /** * Registers an integration * - * @param hub the Hub + * @param scopes the Scopes * @param options the options */ - void register(@NotNull IHub hub, @NotNull SentryOptions options); + void register(@NotNull IScopes scopes, @NotNull SentryOptions options); } diff --git a/sentry/src/main/java/io/sentry/MonitorConfig.java b/sentry/src/main/java/io/sentry/MonitorConfig.java index d954a50466..763e3b65a4 100644 --- a/sentry/src/main/java/io/sentry/MonitorConfig.java +++ b/sentry/src/main/java/io/sentry/MonitorConfig.java @@ -21,7 +21,7 @@ public final class MonitorConfig implements JsonUnknown, JsonSerializable { public MonitorConfig(final @NotNull MonitorSchedule schedule) { this.schedule = schedule; - final SentryOptions.Cron defaultCron = HubAdapter.getInstance().getOptions().getCron(); + final SentryOptions.Cron defaultCron = ScopesAdapter.getInstance().getOptions().getCron(); if (defaultCron != null) { this.checkinMargin = defaultCron.getDefaultCheckinMargin(); this.maxRuntime = defaultCron.getDefaultMaxRuntime(); diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index e51cea8d2d..704bd2b44a 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +@Deprecated public final class NoOpHub implements IHub { private static final NoOpHub instance = new NoOpHub(); @@ -21,6 +22,7 @@ public final class NoOpHub implements IHub { private NoOpHub() {} + @Deprecated public static NoOpHub getInstance() { return instance; } @@ -234,4 +236,9 @@ public void reportFullyDisplayed() {} public @NotNull MetricsApi metrics() { return metricsApi; } + + @Override + public boolean isNoOp() { + return true; + } } diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java new file mode 100644 index 0000000000..6fef262944 --- /dev/null +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -0,0 +1,243 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.metrics.NoopMetricsAggregator; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class NoOpScopes implements IScopes { + + private static final NoOpScopes instance = new NoOpScopes(); + + private final @NotNull SentryOptions emptyOptions = SentryOptions.empty(); + private final @NotNull MetricsApi metricsApi = + new MetricsApi(NoopMetricsAggregator.getInstance()); + + private NoOpScopes() {} + + public static NoOpScopes getInstance() { + return instance; + } + + @Override + public boolean isEnabled() { + return false; + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) {} + + @Override + public void startSession() {} + + @Override + public void endSession() {} + + @Override + public void close() {} + + @Override + public void close(final boolean isRestarting) {} + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) {} + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) {} + + @Override + public void setLevel(@Nullable SentryLevel level) {} + + @Override + public void setTransaction(@Nullable String transaction) {} + + @Override + public void setUser(@Nullable User user) {} + + @Override + public void setFingerprint(@NotNull List fingerprint) {} + + @Override + public void clearBreadcrumbs() {} + + @Override + public void setTag(@NotNull String key, @NotNull String value) {} + + @Override + public void removeTag(@NotNull String key) {} + + @Override + public void setExtra(@NotNull String key, @NotNull String value) {} + + @Override + public void removeExtra(@NotNull String key) {} + + @Override + public @NotNull SentryId getLastEventId() { + return SentryId.EMPTY_ID; + } + + @Override + public void pushScope() {} + + @Override + public void popScope() {} + + @Override + public void withScope(@NotNull ScopeCallback callback) { + callback.run(NoOpScope.getInstance()); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) {} + + @Override + public void bindClient(@NotNull ISentryClient client) {} + + @Override + public boolean isHealthy() { + return true; + } + + @Override + public void flush(long timeoutMillis) {} + + @Deprecated + @Override + public @NotNull IHub clone() { + return NoOpHub.getInstance(); + } + + @Override + public @NotNull SentryId captureTransaction( + final @NotNull SentryTransaction transaction, + final @Nullable TraceContext traceContext, + final @Nullable Hint hint, + final @Nullable ProfilingTraceData profilingTraceData) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return NoOpTransaction.getInstance(); + } + + @Override + @Deprecated + @SuppressWarnings("InlineMeSuggester") + public @NotNull SentryTraceHeader traceHeaders() { + return new SentryTraceHeader(SentryId.EMPTY_ID, SpanId.EMPTY_ID, true); + } + + @Override + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan spanContext, + final @NotNull String transactionName) {} + + @Override + public @Nullable ISpan getSpan() { + return null; + } + + @Override + public @Nullable ITransaction getTransaction() { + return null; + } + + @Override + public @NotNull SentryOptions getOptions() { + return emptyOptions; + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return null; + } + + @Override + public void reportFullyDisplayed() {} + + @Override + public @Nullable TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { + return null; + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return null; + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return null; + } + + @Override + @ApiStatus.Experimental + public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { + return SentryId.EMPTY_ID; + } + + @Override + public @Nullable RateLimiter getRateLimiter() { + return null; + } + + @Override + public @NotNull MetricsApi metrics() { + return metricsApi; + } + + @Override + public boolean isNoOp() { + return true; + } +} diff --git a/sentry/src/main/java/io/sentry/OutboxSender.java b/sentry/src/main/java/io/sentry/OutboxSender.java index 709cbb8580..f80bf030c8 100644 --- a/sentry/src/main/java/io/sentry/OutboxSender.java +++ b/sentry/src/main/java/io/sentry/OutboxSender.java @@ -36,20 +36,20 @@ public final class OutboxSender extends DirectoryProcessor implements IEnvelopeS @SuppressWarnings("CharsetObjectCanBeUsed") private static final Charset UTF_8 = Charset.forName("UTF-8"); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull IEnvelopeReader envelopeReader; private final @NotNull ISerializer serializer; private final @NotNull ILogger logger; public OutboxSender( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull IEnvelopeReader envelopeReader, final @NotNull ISerializer serializer, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - super(hub, logger, flushTimeoutMillis, maxQueueSize); - this.hub = Objects.requireNonNull(hub, "Hub is required."); + super(scopes, logger, flushTimeoutMillis, maxQueueSize); + this.scopes = Objects.requireNonNull(scopes, "Hub is required."); this.envelopeReader = Objects.requireNonNull(envelopeReader, "Envelope reader is required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); @@ -144,7 +144,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN logUnexpectedEventId(envelope, event.getEventId(), currentItem); continue; } - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); logItemCaptured(currentItem); if (!waitFlush(hint)) { @@ -181,7 +181,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN .getTrace() .setSamplingDecision(extractSamplingDecision(traceContext)); } - hub.captureTransaction(transaction, traceContext, hint); + scopes.captureTransaction(transaction, traceContext, hint); logItemCaptured(currentItem); if (!waitFlush(hint)) { @@ -197,7 +197,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN final SentryEnvelope newEnvelope = new SentryEnvelope( envelope.getHeader().getEventId(), envelope.getHeader().getSdkVersion(), item); - hub.captureEnvelope(newEnvelope, hint); + scopes.captureEnvelope(newEnvelope, hint); logger.log( SentryLevel.DEBUG, "%s item %d is being captured.", diff --git a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java index 458c6532ba..bda2a14477 100644 --- a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java +++ b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java @@ -33,11 +33,11 @@ final class PreviousSessionFinalizer implements Runnable { private final @NotNull SentryOptions options; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - PreviousSessionFinalizer(final @NotNull SentryOptions options, final @NotNull IHub hub) { + PreviousSessionFinalizer(final @NotNull SentryOptions options, final @NotNull IScopes scopes) { this.options = options; - this.hub = hub; + this.scopes = scopes; } @Override @@ -116,7 +116,7 @@ public void run() { // SdkVersion will be outdated. final SentryEnvelope fromSession = SentryEnvelope.from(serializer, session, options.getSdkVersion()); - hub.captureEnvelope(fromSession); + scopes.captureEnvelope(fromSession); } } catch (Throwable e) { options.getLogger().log(SentryLevel.ERROR, "Error processing previous session.", e); diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java new file mode 100644 index 0000000000..1ecd31e248 --- /dev/null +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -0,0 +1,286 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class ScopesAdapter implements IScopes { + + private static final ScopesAdapter INSTANCE = new ScopesAdapter(); + + private ScopesAdapter() {} + + public static ScopesAdapter getInstance() { + return INSTANCE; + } + + @Override + public boolean isEnabled() { + return Sentry.isEnabled(); + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return Sentry.captureEvent(event, hint); + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return Sentry.captureEvent(event, hint, callback); + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return Sentry.captureMessage(message, level); + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return Sentry.captureMessage(message, level, callback); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return Sentry.getCurrentScopes().captureEnvelope(envelope, hint); + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return Sentry.captureException(throwable, hint); + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return Sentry.captureException(throwable, hint, callback); + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) { + Sentry.captureUserFeedback(userFeedback); + } + + @Override + public void startSession() { + Sentry.startSession(); + } + + @Override + public void endSession() { + Sentry.endSession(); + } + + @Override + public void close(final boolean isRestarting) { + Sentry.close(); + } + + @Override + public void close() { + Sentry.close(); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { + Sentry.addBreadcrumb(breadcrumb, hint); + } + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { + addBreadcrumb(breadcrumb, new Hint()); + } + + @Override + public void setLevel(@Nullable SentryLevel level) { + Sentry.setLevel(level); + } + + @Override + public void setTransaction(@Nullable String transaction) { + Sentry.setTransaction(transaction); + } + + @Override + public void setUser(@Nullable User user) { + Sentry.setUser(user); + } + + @Override + public void setFingerprint(@NotNull List fingerprint) { + Sentry.setFingerprint(fingerprint); + } + + @Override + public void clearBreadcrumbs() { + Sentry.clearBreadcrumbs(); + } + + @Override + public void setTag(@NotNull String key, @NotNull String value) { + Sentry.setTag(key, value); + } + + @Override + public void removeTag(@NotNull String key) { + Sentry.removeTag(key); + } + + @Override + public void setExtra(@NotNull String key, @NotNull String value) { + Sentry.setExtra(key, value); + } + + @Override + public void removeExtra(@NotNull String key) { + Sentry.removeExtra(key); + } + + @Override + public @NotNull SentryId getLastEventId() { + return Sentry.getLastEventId(); + } + + @Override + public void pushScope() { + Sentry.pushScope(); + } + + @Override + public void popScope() { + Sentry.popScope(); + } + + @Override + public void withScope(@NotNull ScopeCallback callback) { + Sentry.withScope(callback); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) { + Sentry.configureScope(callback); + } + + @Override + public void bindClient(@NotNull ISentryClient client) { + Sentry.bindClient(client); + } + + @Override + public boolean isHealthy() { + return Sentry.isHealthy(); + } + + @Override + public void flush(long timeoutMillis) { + Sentry.flush(timeoutMillis); + } + + @Override + @SuppressWarnings("deprecation") + public @NotNull IHub clone() { + return Sentry.getCurrentScopes().clone(); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData) { + return Sentry.getCurrentScopes() + .captureTransaction(transaction, traceContext, hint, profilingTraceData); + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return Sentry.startTransaction(transactionContext, transactionOptions); + } + + @Deprecated + @Override + @SuppressWarnings("deprecation") + public @Nullable SentryTraceHeader traceHeaders() { + return Sentry.traceHeaders(); + } + + @ApiStatus.Internal + @Override + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan span, + final @NotNull String transactionName) { + Sentry.getCurrentScopes().setSpanContext(throwable, span, transactionName); + } + + @Override + public @Nullable ISpan getSpan() { + return Sentry.getCurrentScopes().getSpan(); + } + + @Override + @ApiStatus.Internal + public @Nullable ITransaction getTransaction() { + return Sentry.getCurrentScopes().getTransaction(); + } + + @Override + public @NotNull SentryOptions getOptions() { + return Sentry.getCurrentScopes().getOptions(); + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return Sentry.isCrashedLastRun(); + } + + @Override + public void reportFullyDisplayed() { + Sentry.reportFullyDisplayed(); + } + + @Override + public @Nullable TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { + return Sentry.continueTrace(sentryTrace, baggageHeaders); + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return Sentry.getTraceparent(); + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return Sentry.getBaggage(); + } + + @Override + @ApiStatus.Experimental + public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { + return Sentry.captureCheckIn(checkIn); + } + + @ApiStatus.Internal + @Override + public @Nullable RateLimiter getRateLimiter() { + return Sentry.getCurrentScopes().getRateLimiter(); + } + + @ApiStatus.Experimental + @Override + public @NotNull MetricsApi metrics() { + return Sentry.getCurrentScopes().metrics(); + } +} diff --git a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java index bc813fdd1e..2160d1607e 100644 --- a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java +++ b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java @@ -18,7 +18,7 @@ public final class SendCachedEnvelopeFireAndForgetIntegration private final @NotNull SendFireAndForgetFactory factory; private @Nullable IConnectionStatusProvider connectionStatusProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryOptions options; private @Nullable SendFireAndForget sender; private final AtomicBoolean isInitialized = new AtomicBoolean(false); @@ -35,7 +35,7 @@ public interface SendFireAndForgetDirPath { public interface SendFireAndForgetFactory { @Nullable - SendFireAndForget create(@NotNull IHub hub, @NotNull SentryOptions options); + SendFireAndForget create(@NotNull IScopes scopes, @NotNull SentryOptions options); default boolean hasValidPath(final @Nullable String dirPath, final @NotNull ILogger logger) { if (dirPath == null || dirPath.isEmpty()) { @@ -66,8 +66,8 @@ public SendCachedEnvelopeFireAndForgetIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Hub is required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); final String cachedDir = options.getCacheDirPath(); @@ -81,7 +81,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio .log(SentryLevel.DEBUG, "SendCachedEventFireAndForgetIntegration installed."); addIntegrationToSdkVersion(getClass()); - sendCachedEnvelopes(hub, options); + sendCachedEnvelopes(scopes, options); } @Override @@ -95,14 +95,14 @@ public void close() throws IOException { @Override public void onConnectionStatusChanged( final @NotNull IConnectionStatusProvider.ConnectionStatus status) { - if (hub != null && options != null) { - sendCachedEnvelopes(hub, options); + if (scopes != null && options != null) { + sendCachedEnvelopes(scopes, options); } } @SuppressWarnings({"FutureReturnValueIgnored", "NullAway"}) private synchronized void sendCachedEnvelopes( - final @NotNull IHub hub, final @NotNull SentryOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { try { options .getExecutorService() @@ -122,7 +122,7 @@ private synchronized void sendCachedEnvelopes( connectionStatusProvider = options.getConnectionStatusProvider(); connectionStatusProvider.addConnectionStatusObserver(this); - sender = factory.create(hub, options); + sender = factory.create(scopes, options); } // skip run only if we're certainly disconnected @@ -138,7 +138,7 @@ private synchronized void sendCachedEnvelopes( } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { options .getLogger() diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java index e44d18a8d6..155946b95a 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java @@ -21,8 +21,8 @@ public SendFireAndForgetEnvelopeSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( - final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Hub is required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); @@ -33,7 +33,7 @@ public SendFireAndForgetEnvelopeSender( final EnvelopeSender envelopeSender = new EnvelopeSender( - hub, + scopes, options.getSerializer(), options.getLogger(), options.getFlushTimeoutMillis(), diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java index fda41610fd..e7913b5b2f 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java @@ -21,8 +21,8 @@ public SendFireAndForgetOutboxSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( - final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Hub is required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); @@ -33,7 +33,7 @@ public SendFireAndForgetOutboxSender( final OutboxSender outboxSender = new OutboxSender( - hub, + scopes, options.getEnvelopeReader(), options.getSerializer(), options.getLogger(), diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 5196d0f31a..206ffd8d20 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -43,11 +43,11 @@ public final class Sentry { private Sentry() {} - /** Holds Hubs per thread or only mainHub if globalHubMode is enabled. */ - private static final @NotNull ThreadLocal currentHub = new ThreadLocal<>(); + /** Holds Hubs per thread or only mainScopes if globalHubMode is enabled. */ + private static final @NotNull ThreadLocal currentScopes = new ThreadLocal<>(); /** The Main Hub or NoOp if Sentry is disabled. */ - private static volatile @NotNull IHub mainHub = NoOpHub.getInstance(); + private static volatile @NotNull IScopes mainScopes = NoOpScopes.getInstance(); /** Default value for globalHubMode is false */ private static final boolean GLOBAL_HUB_DEFAULT_MODE = false; @@ -66,40 +66,58 @@ private Sentry() {} private static final long classCreationTimestamp = System.currentTimeMillis(); /** - * Returns the current (threads) hub, if none, clones the mainHub and returns it. + * Returns the current (threads) hub, if none, clones the mainScopes and returns it. * * @return the hub */ @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @SuppressWarnings("deprecation") + @Deprecated public static @NotNull IHub getCurrentHub() { + return new HubScopesWrapper(getCurrentScopes()); + } + + @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @SuppressWarnings("deprecation") + public static @NotNull IScopes getCurrentScopes() { if (globalHubMode) { - return mainHub; + return mainScopes; } - IHub hub = currentHub.get(); - if (hub == null || hub instanceof NoOpHub) { - hub = mainHub.clone(); - currentHub.set(hub); + IScopes hub = currentScopes.get(); + if (hub == null || hub.isNoOp()) { + // TODO fork instead + hub = mainScopes.clone(); + currentScopes.set(hub); } return hub; } /** - * Returns a new hub which is cloned from the mainHub. + * Returns a new hub which is cloned from the mainScopes. * * @return the hub */ @ApiStatus.Internal @ApiStatus.Experimental - public static @NotNull IHub cloneMainHub() { + @SuppressWarnings("deprecation") + public static @NotNull IScopes cloneMainHub() { if (globalHubMode) { - return mainHub; + return mainScopes; } - return mainHub.clone(); + // TODO fork instead + return mainScopes.clone(); } @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @Deprecated + @SuppressWarnings("deprecation") public static void setCurrentHub(final @NotNull IHub hub) { - currentHub.set(hub); + currentScopes.set(hub); + } + + @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + public static void setCurrentScopes(final @NotNull IScopes scopes) { + currentScopes.set(scopes); } /** @@ -108,7 +126,7 @@ public static void setCurrentHub(final @NotNull IHub hub) { * @return true if its enabled or false otherwise. */ public static boolean isEnabled() { - return getCurrentHub().isEnabled(); + return getCurrentScopes().isEnabled(); } /** Initializes the SDK */ @@ -217,6 +235,7 @@ public static void init(final @NotNull SentryOptions options) { * @param options options the SentryOptions * @param globalHubMode the globalHubMode */ + @SuppressWarnings("deprecation") private static synchronized void init( final @NotNull SentryOptions options, final boolean globalHubMode) { if (isEnabled()) { @@ -234,10 +253,11 @@ private static synchronized void init( options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode)); Sentry.globalHubMode = globalHubMode; - final IHub hub = getCurrentHub(); - mainHub = new Hub(options); + final IScopes hub = getCurrentScopes(); + // TODO new Scopes() + mainScopes = new Hub(options); - currentHub.set(mainHub); + currentScopes.set(mainScopes); hub.close(true); @@ -252,12 +272,12 @@ private static synchronized void init( // and hub was still NoOp. // Registering integrations here make sure that Hub is already created. for (final Integration integration : options.getIntegrations()) { - integration.register(HubAdapter.getInstance(), options); + integration.register(ScopesAdapter.getInstance(), options); } notifyOptionsObservers(options); - finalizePreviousSession(options, HubAdapter.getInstance()); + finalizePreviousSession(options, ScopesAdapter.getInstance()); handleAppStartProfilingConfig(options, options.getExecutorService()); } @@ -327,12 +347,12 @@ private static void handleAppStartProfilingConfig( @SuppressWarnings("FutureReturnValueIgnored") private static void finalizePreviousSession( - final @NotNull SentryOptions options, final @NotNull IHub hub) { + final @NotNull SentryOptions options, final @NotNull IScopes scopes) { // enqueue a task to finalize previous session. Since the executor // is single-threaded, this task will be enqueued sequentially after all integrations that have // to modify the previous session have done their work, even if they do that async. try { - options.getExecutorService().submit(new PreviousSessionFinalizer(options, hub)); + options.getExecutorService().submit(new PreviousSessionFinalizer(options, scopes)); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to finalize previous session.", e); } @@ -475,7 +495,7 @@ private static boolean initConfigurations(final @NotNull SentryOptions options) } if (options.isEnableBackpressureHandling()) { - options.setBackpressureMonitor(new BackpressureMonitor(options, HubAdapter.getInstance())); + options.setBackpressureMonitor(new BackpressureMonitor(options, ScopesAdapter.getInstance())); options.getBackpressureMonitor().start(); } @@ -484,11 +504,11 @@ private static boolean initConfigurations(final @NotNull SentryOptions options) /** Close the SDK */ public static synchronized void close() { - final IHub hub = getCurrentHub(); - mainHub = NoOpHub.getInstance(); + final IScopes scopes = getCurrentScopes(); + mainScopes = NoOpScopes.getInstance(); // remove thread local to avoid memory leak - currentHub.remove(); - hub.close(false); + currentScopes.remove(); + scopes.close(false); } /** @@ -498,7 +518,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureEvent(final @NotNull SentryEvent event) { - return getCurrentHub().captureEvent(event); + return getCurrentScopes().captureEvent(event); } /** @@ -510,7 +530,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureEvent( final @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureEvent(event, callback); + return getCurrentScopes().captureEvent(event, callback); } /** @@ -522,7 +542,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureEvent( final @NotNull SentryEvent event, final @Nullable Hint hint) { - return getCurrentHub().captureEvent(event, hint); + return getCurrentScopes().captureEvent(event, hint); } /** @@ -537,7 +557,7 @@ public static synchronized void close() { final @NotNull SentryEvent event, final @Nullable Hint hint, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureEvent(event, hint, callback); + return getCurrentScopes().captureEvent(event, hint, callback); } /** @@ -547,7 +567,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureMessage(final @NotNull String message) { - return getCurrentHub().captureMessage(message); + return getCurrentScopes().captureMessage(message); } /** @@ -559,7 +579,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureMessage( final @NotNull String message, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureMessage(message, callback); + return getCurrentScopes().captureMessage(message, callback); } /** @@ -571,7 +591,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureMessage( final @NotNull String message, final @NotNull SentryLevel level) { - return getCurrentHub().captureMessage(message, level); + return getCurrentScopes().captureMessage(message, level); } /** @@ -586,7 +606,7 @@ public static synchronized void close() { final @NotNull String message, final @NotNull SentryLevel level, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureMessage(message, level, callback); + return getCurrentScopes().captureMessage(message, level, callback); } /** @@ -596,7 +616,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureException(final @NotNull Throwable throwable) { - return getCurrentHub().captureException(throwable); + return getCurrentScopes().captureException(throwable); } /** @@ -608,7 +628,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureException( final @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureException(throwable, callback); + return getCurrentScopes().captureException(throwable, callback); } /** @@ -620,7 +640,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureException( final @NotNull Throwable throwable, final @Nullable Hint hint) { - return getCurrentHub().captureException(throwable, hint); + return getCurrentScopes().captureException(throwable, hint); } /** @@ -635,7 +655,7 @@ public static synchronized void close() { final @NotNull Throwable throwable, final @Nullable Hint hint, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureException(throwable, hint, callback); + return getCurrentScopes().captureException(throwable, hint, callback); } /** @@ -644,7 +664,7 @@ public static synchronized void close() { * @param userFeedback The user feedback to send to Sentry. */ public static void captureUserFeedback(final @NotNull UserFeedback userFeedback) { - getCurrentHub().captureUserFeedback(userFeedback); + getCurrentScopes().captureUserFeedback(userFeedback); } /** @@ -655,7 +675,7 @@ public static void captureUserFeedback(final @NotNull UserFeedback userFeedback) */ public static void addBreadcrumb( final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) { - getCurrentHub().addBreadcrumb(breadcrumb, hint); + getCurrentScopes().addBreadcrumb(breadcrumb, hint); } /** @@ -664,7 +684,7 @@ public static void addBreadcrumb( * @param breadcrumb the breadcrumb */ public static void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { - getCurrentHub().addBreadcrumb(breadcrumb); + getCurrentScopes().addBreadcrumb(breadcrumb); } /** @@ -673,7 +693,7 @@ public static void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { * @param message rendered as text and the whitespace is preserved. */ public static void addBreadcrumb(final @NotNull String message) { - getCurrentHub().addBreadcrumb(message); + getCurrentScopes().addBreadcrumb(message); } /** @@ -684,7 +704,7 @@ public static void addBreadcrumb(final @NotNull String message) { * from. */ public static void addBreadcrumb(final @NotNull String message, final @NotNull String category) { - getCurrentHub().addBreadcrumb(message, category); + getCurrentScopes().addBreadcrumb(message, category); } /** @@ -693,7 +713,7 @@ public static void addBreadcrumb(final @NotNull String message, final @NotNull S * @param level the Sentry level */ public static void setLevel(final @Nullable SentryLevel level) { - getCurrentHub().setLevel(level); + getCurrentScopes().setLevel(level); } /** @@ -702,7 +722,7 @@ public static void setLevel(final @Nullable SentryLevel level) { * @param transaction the transaction */ public static void setTransaction(final @Nullable String transaction) { - getCurrentHub().setTransaction(transaction); + getCurrentScopes().setTransaction(transaction); } /** @@ -711,7 +731,7 @@ public static void setTransaction(final @Nullable String transaction) { * @param user the user */ public static void setUser(final @Nullable User user) { - getCurrentHub().setUser(user); + getCurrentScopes().setUser(user); } /** @@ -720,12 +740,12 @@ public static void setUser(final @Nullable User user) { * @param fingerprint the fingerprints */ public static void setFingerprint(final @NotNull List fingerprint) { - getCurrentHub().setFingerprint(fingerprint); + getCurrentScopes().setFingerprint(fingerprint); } /** Deletes current breadcrumbs from the current scope. */ public static void clearBreadcrumbs() { - getCurrentHub().clearBreadcrumbs(); + getCurrentScopes().clearBreadcrumbs(); } /** @@ -735,7 +755,7 @@ public static void clearBreadcrumbs() { * @param value the value */ public static void setTag(final @NotNull String key, final @NotNull String value) { - getCurrentHub().setTag(key, value); + getCurrentScopes().setTag(key, value); } /** @@ -744,7 +764,7 @@ public static void setTag(final @NotNull String key, final @NotNull String value * @param key the key */ public static void removeTag(final @NotNull String key) { - getCurrentHub().removeTag(key); + getCurrentScopes().removeTag(key); } /** @@ -755,7 +775,7 @@ public static void removeTag(final @NotNull String key) { * @param value the value */ public static void setExtra(final @NotNull String key, final @NotNull String value) { - getCurrentHub().setExtra(key, value); + getCurrentScopes().setExtra(key, value); } /** @@ -764,7 +784,7 @@ public static void setExtra(final @NotNull String key, final @NotNull String val * @param key the key */ public static void removeExtra(final @NotNull String key) { - getCurrentHub().removeExtra(key); + getCurrentScopes().removeExtra(key); } /** @@ -773,14 +793,14 @@ public static void removeExtra(final @NotNull String key) { * @return last SentryId */ public static @NotNull SentryId getLastEventId() { - return getCurrentHub().getLastEventId(); + return getCurrentScopes().getLastEventId(); } /** Pushes a new scope while inheriting the current scope's data. */ public static void pushScope() { // pushScope is no-op in global hub mode if (!globalHubMode) { - getCurrentHub().pushScope(); + getCurrentScopes().pushScope(); } } @@ -788,7 +808,7 @@ public static void pushScope() { public static void popScope() { // popScope is no-op in global hub mode if (!globalHubMode) { - getCurrentHub().popScope(); + getCurrentScopes().popScope(); } } @@ -798,7 +818,7 @@ public static void popScope() { * @param callback the callback */ public static void withScope(final @NotNull ScopeCallback callback) { - getCurrentHub().withScope(callback); + getCurrentScopes().withScope(callback); } /** @@ -807,7 +827,7 @@ public static void withScope(final @NotNull ScopeCallback callback) { * @param callback The configure scope callback. */ public static void configureScope(final @NotNull ScopeCallback callback) { - getCurrentHub().configureScope(callback); + getCurrentScopes().configureScope(callback); } /** @@ -816,11 +836,11 @@ public static void configureScope(final @NotNull ScopeCallback callback) { * @param client the client. */ public static void bindClient(final @NotNull ISentryClient client) { - getCurrentHub().bindClient(client); + getCurrentScopes().bindClient(client); } public static boolean isHealthy() { - return getCurrentHub().isHealthy(); + return getCurrentScopes().isHealthy(); } /** @@ -829,17 +849,17 @@ public static boolean isHealthy() { * @param timeoutMillis time in milliseconds */ public static void flush(final long timeoutMillis) { - getCurrentHub().flush(timeoutMillis); + getCurrentScopes().flush(timeoutMillis); } /** Starts a new session. If there's a running session, it ends it before starting the new one. */ public static void startSession() { - getCurrentHub().startSession(); + getCurrentScopes().startSession(); } /** Ends the current session */ public static void endSession() { - getCurrentHub().endSession(); + getCurrentScopes().endSession(); } /** @@ -851,7 +871,7 @@ public static void endSession() { */ public static @NotNull ITransaction startTransaction( final @NotNull String name, final @NotNull String operation) { - return getCurrentHub().startTransaction(name, operation); + return getCurrentScopes().startTransaction(name, operation); } /** @@ -866,7 +886,7 @@ public static void endSession() { final @NotNull String name, final @NotNull String operation, final @NotNull TransactionOptions transactionOptions) { - return getCurrentHub().startTransaction(name, operation, transactionOptions); + return getCurrentScopes().startTransaction(name, operation, transactionOptions); } /** @@ -884,7 +904,7 @@ public static void endSession() { final @Nullable String description, final @NotNull TransactionOptions transactionOptions) { final ITransaction transaction = - getCurrentHub().startTransaction(name, operation, transactionOptions); + getCurrentScopes().startTransaction(name, operation, transactionOptions); transaction.setDescription(description); return transaction; } @@ -897,7 +917,7 @@ public static void endSession() { */ public static @NotNull ITransaction startTransaction( final @NotNull TransactionContext transactionContexts) { - return getCurrentHub().startTransaction(transactionContexts); + return getCurrentScopes().startTransaction(transactionContexts); } /** @@ -910,7 +930,7 @@ public static void endSession() { public static @NotNull ITransaction startTransaction( final @NotNull TransactionContext transactionContext, final @NotNull TransactionOptions transactionOptions) { - return getCurrentHub().startTransaction(transactionContext, transactionOptions); + return getCurrentScopes().startTransaction(transactionContext, transactionOptions); } /** @@ -923,7 +943,7 @@ public static void endSession() { @Deprecated @SuppressWarnings("InlineMeSuggester") public static @Nullable SentryTraceHeader traceHeaders() { - return getCurrentHub().traceHeaders(); + return getCurrentScopes().traceHeaders(); } /** @@ -935,9 +955,9 @@ public static void endSession() { */ public static @Nullable ISpan getSpan() { if (globalHubMode && Platform.isAndroid()) { - return getCurrentHub().getTransaction(); + return getCurrentScopes().getTransaction(); } else { - return getCurrentHub().getSpan(); + return getCurrentScopes().getSpan(); } } @@ -952,7 +972,7 @@ public static void endSession() { * @return true if App has crashed, false otherwise, and null if not evaluated yet */ public static @Nullable Boolean isCrashedLastRun() { - return getCurrentHub().isCrashedLastRun(); + return getCurrentScopes().isCrashedLastRun(); } /** @@ -964,7 +984,7 @@ public static void endSession() { * finished, this call will be ignored. */ public static void reportFullyDisplayed() { - getCurrentHub().reportFullyDisplayed(); + getCurrentScopes().reportFullyDisplayed(); } /** @@ -980,7 +1000,7 @@ public static void reportFullDisplayed() { @NotNull @ApiStatus.Experimental public static MetricsApi metrics() { - return getCurrentHub().metrics(); + return getCurrentScopes().metrics(); } /** @@ -1009,7 +1029,7 @@ public interface OptionsConfiguration { // return TransactionContext (if performance enabled) or null (if performance disabled) public static @Nullable TransactionContext continueTrace( final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { - return getCurrentHub().continueTrace(sentryTrace, baggageHeaders); + return getCurrentScopes().continueTrace(sentryTrace, baggageHeaders); } /** @@ -1019,7 +1039,7 @@ public interface OptionsConfiguration { * @return sentry trace header or null */ public static @Nullable SentryTraceHeader getTraceparent() { - return getCurrentHub().getTraceparent(); + return getCurrentScopes().getTraceparent(); } /** @@ -1029,11 +1049,11 @@ public interface OptionsConfiguration { * @return baggage header or null */ public static @Nullable BaggageHeader getBaggage() { - return getCurrentHub().getBaggage(); + return getCurrentScopes().getBaggage(); } @ApiStatus.Experimental public static @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { - return getCurrentHub().captureCheckIn(checkIn); + return getCurrentScopes().captureCheckIn(checkIn); } } diff --git a/sentry/src/main/java/io/sentry/SentryTracer.java b/sentry/src/main/java/io/sentry/SentryTracer.java index bc3b5eb531..7d82bbbc4a 100644 --- a/sentry/src/main/java/io/sentry/SentryTracer.java +++ b/sentry/src/main/java/io/sentry/SentryTracer.java @@ -26,7 +26,7 @@ public final class SentryTracer implements ITransaction { private final @NotNull SentryId eventId = new SentryId(); private final @NotNull Span root; private final @NotNull List children = new CopyOnWriteArrayList<>(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @NotNull String name; /** @@ -52,31 +52,31 @@ public final class SentryTracer implements ITransaction { private final @Nullable TransactionPerformanceCollector transactionPerformanceCollector; private final @NotNull TransactionOptions transactionOptions; - public SentryTracer(final @NotNull TransactionContext context, final @NotNull IHub hub) { - this(context, hub, new TransactionOptions(), null); + public SentryTracer(final @NotNull TransactionContext context, final @NotNull IScopes scopes) { + this(context, scopes, new TransactionOptions(), null); } public SentryTracer( final @NotNull TransactionContext context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionOptions transactionOptions) { - this(context, hub, transactionOptions, null); + this(context, scopes, transactionOptions, null); } SentryTracer( final @NotNull TransactionContext context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionOptions transactionOptions, final @Nullable TransactionPerformanceCollector transactionPerformanceCollector) { Objects.requireNonNull(context, "context is required"); - Objects.requireNonNull(hub, "hub is required"); + Objects.requireNonNull(scopes, "scopes are required"); this.root = - new Span(context, this, hub, transactionOptions.getStartTimestamp(), transactionOptions); + new Span(context, this, scopes, transactionOptions.getStartTimestamp(), transactionOptions); this.name = context.getName(); this.instrumenter = context.getInstrumenter(); - this.hub = hub; + this.scopes = scopes; this.transactionPerformanceCollector = transactionPerformanceCollector; this.transactionNameSource = context.getTransactionNameSource(); this.transactionOptions = transactionOptions; @@ -84,7 +84,7 @@ public SentryTracer( if (context.getBaggage() != null) { this.baggage = context.getBaggage(); } else { - this.baggage = new Baggage(hub.getOptions().getLogger()); + this.baggage = new Baggage(scopes.getOptions().getLogger()); } // We are currently sending the performance data only in profiles, so there's no point in @@ -122,7 +122,8 @@ public void run() { try { timer.schedule(idleTimeoutTask, idleTimeout); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to schedule finish timer", e); // if we failed to schedule the finish timer for some reason, we finish it here right @@ -156,7 +157,7 @@ private void onDeadlineTimeoutReached() { return; } - final @NotNull SentryDate finishTimestamp = hub.getOptions().getDateProvider().now(); + final @NotNull SentryDate finishTimestamp = scopes.getOptions().getDateProvider().now(); // abort all child-spans first, this ensures the transaction can be finished, // even if waitForChildren is true @@ -186,7 +187,7 @@ public void finish( // if it's not set -> fallback to the current time if (finishTimestamp == null) { - finishTimestamp = hub.getOptions().getDateProvider().now(); + finishTimestamp = scopes.getOptions().getDateProvider().now(); } // auto-finish any idle spans first @@ -207,9 +208,10 @@ public void finish( ProfilingTraceData profilingTraceData = null; if (Boolean.TRUE.equals(isSampled()) && Boolean.TRUE.equals(isProfileSampled())) { profilingTraceData = - hub.getOptions() + scopes + .getOptions() .getTransactionProfiler() - .onTransactionFinish(this, performanceCollectionData, hub.getOptions()); + .onTransactionFinish(this, performanceCollectionData, scopes.getOptions()); } if (performanceCollectionData != null) { performanceCollectionData.clear(); @@ -222,7 +224,7 @@ public void finish( root.finish(finishStatus.spanStatus, finishTimestamp); - hub.configureScope( + scopes.configureScope( scope -> { scope.withTransaction( transaction -> { @@ -251,7 +253,8 @@ public void finish( if (dropIfNoChildren && children.isEmpty() && transactionOptions.getIdleTimeout() != null) { // if it's an idle transaction which has no children, we drop it to save user's quota - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -261,7 +264,7 @@ public void finish( } transaction.getMeasurements().putAll(root.getMeasurements()); - hub.captureTransaction(transaction, traceContext(), hint, profilingTraceData); + scopes.captureTransaction(transaction, traceContext(), hint, profilingTraceData); } } @@ -292,7 +295,8 @@ public void run() { try { timer.schedule(deadlineTimeoutTask, deadlineTimeOut); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to schedule finish timer", e); // if we failed to schedule the finish timer for some reason, we finish it here right @@ -418,7 +422,7 @@ private ISpan createChild( return NoOpSpan.getInstance(); } - if (children.size() < hub.getOptions().getMaxSpans()) { + if (children.size() < scopes.getOptions().getMaxSpans()) { Objects.requireNonNull(parentSpanId, "parentSpanId is required"); Objects.requireNonNull(operation, "operation is required"); cancelIdleTimer(); @@ -428,7 +432,7 @@ private ISpan createChild( parentSpanId, this, operation, - this.hub, + this.scopes, timestamp, spanOptions, finishingSpan -> { @@ -451,7 +455,7 @@ private ISpan createChild( span.setData(SpanDataConvention.THREAD_ID, String.valueOf(Thread.currentThread().getId())); span.setData( SpanDataConvention.THREAD_NAME, - hub.getOptions().getMainThreadChecker().isMainThread() + scopes.getOptions().getMainThreadChecker().isMainThread() ? "main" : Thread.currentThread().getName()); this.children.add(span); @@ -460,7 +464,8 @@ private ISpan createChild( } return span; } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -529,10 +534,11 @@ private ISpan createChild( return NoOpSpan.getInstance(); } - if (children.size() < hub.getOptions().getMaxSpans()) { + if (children.size() < scopes.getOptions().getMaxSpans()) { return root.startChild(operation, description, timestamp, instrumenter, spanOptions); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -567,7 +573,7 @@ public void finish(@Nullable SpanStatus status, @Nullable SentryDate finishDate) @Override public @Nullable TraceContext traceContext() { - if (hub.getOptions().isTraceSampling()) { + if (scopes.getOptions().isTraceSampling()) { updateBaggageValues(); return baggage.toTraceContext(); } else { @@ -579,12 +585,12 @@ private void updateBaggageValues() { synchronized (this) { if (baggage.isMutable()) { final AtomicReference userAtomicReference = new AtomicReference<>(); - hub.configureScope( + scopes.configureScope( scope -> { userAtomicReference.set(scope.getUser()); }); baggage.setValuesFromTransaction( - this, userAtomicReference.get(), hub.getOptions(), this.getSamplingDecision()); + this, userAtomicReference.get(), scopes.getOptions(), this.getSamplingDecision()); baggage.freeze(); } } @@ -592,7 +598,7 @@ private void updateBaggageValues() { @Override public @Nullable BaggageHeader toBaggageHeader(@Nullable List thirdPartyBaggageHeaders) { - if (hub.getOptions().isTraceSampling()) { + if (scopes.getOptions().isTraceSampling()) { updateBaggageValues(); return BaggageHeader.fromBaggageAndOutgoingHeader(baggage, thirdPartyBaggageHeaders); @@ -616,7 +622,8 @@ private boolean hasAllChildrenFinished() { @Override public void setOperation(final @NotNull String operation) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -636,7 +643,8 @@ public void setOperation(final @NotNull String operation) { @Override public void setDescription(final @Nullable String description) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -656,7 +664,8 @@ public void setDescription(final @Nullable String description) { @Override public void setStatus(final @Nullable SpanStatus status) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -676,7 +685,8 @@ public void setStatus(final @Nullable SpanStatus status) { @Override public void setThrowable(final @Nullable Throwable throwable) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "The transaction is already finished. Throwable cannot be set"); return; @@ -698,7 +708,8 @@ public void setThrowable(final @Nullable Throwable throwable) { @Override public void setTag(final @NotNull String key, final @NotNull String value) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "The transaction is already finished. Tag %s cannot be set", key); return; @@ -720,7 +731,8 @@ public boolean isFinished() { @Override public void setData(@NotNull String key, @NotNull Object value) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, "The transaction is already finished. Data %s cannot be set", key); @@ -795,7 +807,8 @@ public void setName(@NotNull String name) { @Override public void setName(@NotNull String name, @NotNull TransactionNameSource transactionNameSource) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index 1a39adee99..165ace7c83 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -27,16 +27,18 @@ public final class SentryWrapper { * @return the wrapped {@link Callable} * @param - the result type of the {@link Callable} */ + @SuppressWarnings("deprecation") public static Callable wrapCallable(final @NotNull Callable callable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO replace with forking + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { return callable.call(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } @@ -51,17 +53,18 @@ public static Callable wrapCallable(final @NotNull Callable callable) * @return the wrapped {@link Supplier} * @param - the result type of the {@link Supplier} */ + @SuppressWarnings("deprecation") public static Supplier wrapSupplier(final @NotNull Supplier supplier) { - - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO replace with forking + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { return supplier.get(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java index b144f2d88a..d957a87ccf 100644 --- a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java +++ b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java @@ -27,12 +27,12 @@ public ShutdownHookIntegration() { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); if (options.isEnableShutdownHook()) { - thread = new Thread(() -> hub.flush(options.getFlushTimeoutMillis())); + thread = new Thread(() -> scopes.flush(options.getFlushTimeoutMillis())); runtime.addShutdownHook(thread); options.getLogger().log(SentryLevel.DEBUG, "ShutdownHookIntegration installed."); addIntegrationToSdkVersion(getClass()); diff --git a/sentry/src/main/java/io/sentry/Span.java b/sentry/src/main/java/io/sentry/Span.java index 850276dac3..1c9d180b29 100644 --- a/sentry/src/main/java/io/sentry/Span.java +++ b/sentry/src/main/java/io/sentry/Span.java @@ -35,7 +35,7 @@ public final class Span implements ISpan { /** A throwable thrown during the execution of the span. */ private @Nullable Throwable throwable; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull AtomicBoolean finished = new AtomicBoolean(false); @@ -55,8 +55,8 @@ public final class Span implements ISpan { final @Nullable SpanId parentSpanId, final @NotNull SentryTracer transaction, final @NotNull String operation, - final @NotNull IHub hub) { - this(traceId, parentSpanId, transaction, operation, hub, null, new SpanOptions(), null); + final @NotNull IScopes scopes) { + this(traceId, parentSpanId, transaction, operation, scopes, null, new SpanOptions(), null); } Span( @@ -64,7 +64,7 @@ public final class Span implements ISpan { final @Nullable SpanId parentSpanId, final @NotNull SentryTracer transaction, final @NotNull String operation, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable SentryDate startTimestamp, final @NotNull SpanOptions options, final @Nullable SpanFinishedCallback spanFinishedCallback) { @@ -72,30 +72,30 @@ public final class Span implements ISpan { new SpanContext( traceId, new SpanId(), operation, parentSpanId, transaction.getSamplingDecision()); this.transaction = Objects.requireNonNull(transaction, "transaction is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = options; this.spanFinishedCallback = spanFinishedCallback; if (startTimestamp != null) { this.startTimestamp = startTimestamp; } else { - this.startTimestamp = hub.getOptions().getDateProvider().now(); + this.startTimestamp = scopes.getOptions().getDateProvider().now(); } } public Span( final @NotNull TransactionContext context, final @NotNull SentryTracer sentryTracer, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable SentryDate startTimestamp, final @NotNull SpanOptions options) { this.context = Objects.requireNonNull(context, "context is required"); this.transaction = Objects.requireNonNull(sentryTracer, "sentryTracer is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.spanFinishedCallback = null; if (startTimestamp != null) { this.startTimestamp = startTimestamp; } else { - this.startTimestamp = hub.getOptions().getDateProvider().now(); + this.startTimestamp = scopes.getOptions().getDateProvider().now(); } this.options = options; } @@ -180,7 +180,7 @@ public void finish() { @Override public void finish(@Nullable SpanStatus status) { - finish(status, hub.getOptions().getDateProvider().now()); + finish(status, scopes.getOptions().getDateProvider().now()); } /** @@ -197,7 +197,7 @@ public void finish(final @Nullable SpanStatus status, final @Nullable SentryDate } this.context.setStatus(status); - this.timestamp = timestamp == null ? hub.getOptions().getDateProvider().now() : timestamp; + this.timestamp = timestamp == null ? scopes.getOptions().getDateProvider().now() : timestamp; if (options.isTrimStart() || options.isTrimEnd()) { @Nullable SentryDate minChildStart = null; @Nullable SentryDate maxChildEnd = null; @@ -230,7 +230,7 @@ public void finish(final @Nullable SpanStatus status, final @Nullable SentryDate } if (throwable != null) { - hub.setSpanContext(throwable, this, this.transaction.getName()); + scopes.setSpanContext(throwable, this, this.transaction.getName()); } if (spanFinishedCallback != null) { spanFinishedCallback.execute(this); @@ -343,7 +343,8 @@ public void setData(final @NotNull String key, final @NotNull Object value) { @Override public void setMeasurement(final @NotNull String name, final @NotNull Number value) { if (isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -365,7 +366,8 @@ public void setMeasurement( final @NotNull Number value, final @NotNull MeasurementUnit unit) { if (isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry/src/main/java/io/sentry/SpotlightIntegration.java b/sentry/src/main/java/io/sentry/SpotlightIntegration.java index 6d488bcbce..0b69ae79be 100644 --- a/sentry/src/main/java/io/sentry/SpotlightIntegration.java +++ b/sentry/src/main/java/io/sentry/SpotlightIntegration.java @@ -26,7 +26,7 @@ public final class SpotlightIntegration private @NotNull ISentryExecutorService executorService = NoOpSentryExecutorService.getInstance(); @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = options; this.logger = options.getLogger(); diff --git a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java index 33e1a4a815..47ceaa084e 100644 --- a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java +++ b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java @@ -28,7 +28,7 @@ public final class UncaughtExceptionHandlerIntegration /** Reference to the pre-existing uncaught exception handler. */ private @Nullable Thread.UncaughtExceptionHandler defaultExceptionHandler; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryOptions options; private boolean registered = false; @@ -43,7 +43,7 @@ public UncaughtExceptionHandlerIntegration() { } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { if (registered) { options .getLogger() @@ -54,7 +54,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions } registered = true; - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Hub is required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); this.options @@ -89,7 +89,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions @Override public void uncaughtException(Thread thread, Throwable thrown) { - if (options != null && hub != null) { + if (options != null && scopes != null) { options.getLogger().log(SentryLevel.INFO, "Uncaught exception received."); try { @@ -99,14 +99,14 @@ public void uncaughtException(Thread thread, Throwable thrown) { final SentryEvent event = new SentryEvent(throwable); event.setLevel(SentryLevel.FATAL); - final ITransaction transaction = hub.getTransaction(); + final ITransaction transaction = scopes.getTransaction(); if (transaction == null && event.getEventId() != null) { // if there's no active transaction on scope, this event can trigger flush notification exceptionHint.setFlushable(event.getEventId()); } final Hint hint = HintUtils.createWithTypeCheckHint(exceptionHint); - final @NotNull SentryId sentryId = hub.captureEvent(event, hint); + final @NotNull SentryId sentryId = scopes.captureEvent(event, hint); final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID); final EventDropReason eventDropReason = HintUtils.getEventDropReason(hint); // in case the event has been dropped by multithreaded deduplicator, the other threads will diff --git a/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java b/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java index 2008a38c76..6541a7586a 100644 --- a/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java +++ b/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java @@ -1,6 +1,6 @@ package io.sentry.backpressure; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISentryExecutorService; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -12,12 +12,13 @@ public final class BackpressureMonitor implements IBackpressureMonitor, Runnable private static final int CHECK_INTERVAL_IN_MS = 10 * 1000; private final @NotNull SentryOptions sentryOptions; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private int downsampleFactor = 0; - public BackpressureMonitor(final @NotNull SentryOptions sentryOptions, final @NotNull IHub hub) { + public BackpressureMonitor( + final @NotNull SentryOptions sentryOptions, final @NotNull IScopes scopes) { this.sentryOptions = sentryOptions; - this.hub = hub; + this.scopes = scopes; } @Override @@ -66,6 +67,6 @@ private void reschedule(final int delay) { } private boolean isHealthy() { - return hub.isHealthy(); + return scopes.isHealthy(); } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java b/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java index 956996ce04..52963413b6 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -28,8 +28,8 @@ final class FileIOSpanManager { private final @NotNull SentryStackTraceFactory stackTraceFactory; - static @Nullable ISpan startSpan(final @NotNull IHub hub, final @NotNull String op) { - final ISpan parent = Platform.isAndroid() ? hub.getTransaction() : hub.getSpan(); + static @Nullable ISpan startSpan(final @NotNull IScopes scopes, final @NotNull String op) { + final ISpan parent = Platform.isAndroid() ? scopes.getTransaction() : scopes.getSpan(); return parent != null ? parent.startChild(op) : null; } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java index 04bb87ae7c..ea7d7f09a5 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java @@ -1,8 +1,8 @@ package io.sentry.instrumentation.file; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; @@ -25,24 +25,24 @@ public final class SentryFileInputStream extends FileInputStream { private final @NotNull FileIOSpanManager spanManager; public SentryFileInputStream(final @Nullable String name) throws FileNotFoundException { - this(name != null ? new File(name) : null, HubAdapter.getInstance()); + this(name != null ? new File(name) : null, ScopesAdapter.getInstance()); } public SentryFileInputStream(final @Nullable File file) throws FileNotFoundException { - this(file, HubAdapter.getInstance()); + this(file, ScopesAdapter.getInstance()); } public SentryFileInputStream(final @NotNull FileDescriptor fdObj) { - this(fdObj, HubAdapter.getInstance()); + this(fdObj, ScopesAdapter.getInstance()); } - SentryFileInputStream(final @Nullable File file, final @NotNull IHub hub) + SentryFileInputStream(final @Nullable File file, final @NotNull IScopes scopes) throws FileNotFoundException { - this(init(file, null, hub)); + this(init(file, null, scopes)); } - SentryFileInputStream(final @NotNull FileDescriptor fdObj, final @NotNull IHub hub) { - this(init(fdObj, null, hub), fdObj); + SentryFileInputStream(final @NotNull FileDescriptor fdObj, final @NotNull IScopes scopes) { + this(init(fdObj, null, scopes), fdObj); } private SentryFileInputStream( @@ -60,24 +60,24 @@ private SentryFileInputStream(final @NotNull FileInputStreamInitData data) } private static FileInputStreamInitData init( - final @Nullable File file, @Nullable FileInputStream delegate, final @NotNull IHub hub) + final @Nullable File file, @Nullable FileInputStream delegate, final @NotNull IScopes scopes) throws FileNotFoundException { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.read"); + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.read"); if (delegate == null) { delegate = new FileInputStream(file); } - return new FileInputStreamInitData(file, span, delegate, hub.getOptions()); + return new FileInputStreamInitData(file, span, delegate, scopes.getOptions()); } private static FileInputStreamInitData init( final @NotNull FileDescriptor fd, @Nullable FileInputStream delegate, - final @NotNull IHub hub) { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.read"); + final @NotNull IScopes scopes) { + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.read"); if (delegate == null) { delegate = new FileInputStream(fd); } - return new FileInputStreamInitData(null, span, delegate, hub.getOptions()); + return new FileInputStreamInitData(null, span, delegate, scopes.getOptions()); } @Override @@ -128,25 +128,27 @@ public static FileInputStream create( final @NotNull FileInputStream delegate, final @Nullable String name) throws FileNotFoundException { return new SentryFileInputStream( - init(name != null ? new File(name) : null, delegate, HubAdapter.getInstance())); + init(name != null ? new File(name) : null, delegate, ScopesAdapter.getInstance())); } public static FileInputStream create( final @NotNull FileInputStream delegate, final @Nullable File file) throws FileNotFoundException { - return new SentryFileInputStream(init(file, delegate, HubAdapter.getInstance())); + return new SentryFileInputStream(init(file, delegate, ScopesAdapter.getInstance())); } public static FileInputStream create( final @NotNull FileInputStream delegate, final @NotNull FileDescriptor descriptor) { return new SentryFileInputStream( - init(descriptor, delegate, HubAdapter.getInstance()), descriptor); + init(descriptor, delegate, ScopesAdapter.getInstance()), descriptor); } static FileInputStream create( - final @NotNull FileInputStream delegate, final @Nullable File file, final @NotNull IHub hub) + final @NotNull FileInputStream delegate, + final @Nullable File file, + final @NotNull IScopes scopes) throws FileNotFoundException { - return new SentryFileInputStream(init(file, delegate, hub)); + return new SentryFileInputStream(init(file, delegate, scopes)); } } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java index 9424710d71..4ef5022e1c 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java @@ -1,8 +1,8 @@ package io.sentry.instrumentation.file; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -24,30 +24,31 @@ public final class SentryFileOutputStream extends FileOutputStream { private final @NotNull FileIOSpanManager spanManager; public SentryFileOutputStream(final @Nullable String name) throws FileNotFoundException { - this(name != null ? new File(name) : null, false, HubAdapter.getInstance()); + this(name != null ? new File(name) : null, false, ScopesAdapter.getInstance()); } public SentryFileOutputStream(final @Nullable String name, final boolean append) throws FileNotFoundException { - this(init(name != null ? new File(name) : null, append, null, HubAdapter.getInstance())); + this(init(name != null ? new File(name) : null, append, null, ScopesAdapter.getInstance())); } public SentryFileOutputStream(final @Nullable File file) throws FileNotFoundException { - this(file, false, HubAdapter.getInstance()); + this(file, false, ScopesAdapter.getInstance()); } public SentryFileOutputStream(final @Nullable File file, final boolean append) throws FileNotFoundException { - this(init(file, append, null, HubAdapter.getInstance())); + this(init(file, append, null, ScopesAdapter.getInstance())); } public SentryFileOutputStream(final @NotNull FileDescriptor fdObj) { - this(init(fdObj, null, HubAdapter.getInstance()), fdObj); + this(init(fdObj, null, ScopesAdapter.getInstance()), fdObj); } - SentryFileOutputStream(final @Nullable File file, final boolean append, final @NotNull IHub hub) + SentryFileOutputStream( + final @Nullable File file, final boolean append, final @NotNull IScopes scopes) throws FileNotFoundException { - this(init(file, append, null, hub)); + this(init(file, append, null, scopes)); } private SentryFileOutputStream( @@ -68,22 +69,24 @@ private static FileOutputStreamInitData init( final @Nullable File file, final boolean append, @Nullable FileOutputStream delegate, - @NotNull IHub hub) + @NotNull IScopes scopes) throws FileNotFoundException { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.write"); + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write"); if (delegate == null) { delegate = new FileOutputStream(file, append); } - return new FileOutputStreamInitData(file, append, span, delegate, hub.getOptions()); + return new FileOutputStreamInitData(file, append, span, delegate, scopes.getOptions()); } private static FileOutputStreamInitData init( - final @NotNull FileDescriptor fd, @Nullable FileOutputStream delegate, @NotNull IHub hub) { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.write"); + final @NotNull FileDescriptor fd, + @Nullable FileOutputStream delegate, + @NotNull IScopes scopes) { + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write"); if (delegate == null) { delegate = new FileOutputStream(fd); } - return new FileOutputStreamInitData(null, false, span, delegate, hub.getOptions()); + return new FileOutputStreamInitData(null, false, span, delegate, scopes.getOptions()); } @Override @@ -132,31 +135,32 @@ public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable String name) throws FileNotFoundException { return new SentryFileOutputStream( - init(name != null ? new File(name) : null, false, delegate, HubAdapter.getInstance())); + init(name != null ? new File(name) : null, false, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable String name, final boolean append) throws FileNotFoundException { return new SentryFileOutputStream( - init(name != null ? new File(name) : null, append, delegate, HubAdapter.getInstance())); + init( + name != null ? new File(name) : null, append, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable File file) throws FileNotFoundException { - return new SentryFileOutputStream(init(file, false, delegate, HubAdapter.getInstance())); + return new SentryFileOutputStream(init(file, false, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable File file, final boolean append) throws FileNotFoundException { - return new SentryFileOutputStream(init(file, append, delegate, HubAdapter.getInstance())); + return new SentryFileOutputStream(init(file, append, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @NotNull FileDescriptor fdObj) { - return new SentryFileOutputStream(init(fdObj, delegate, HubAdapter.getInstance()), fdObj); + return new SentryFileOutputStream(init(fdObj, delegate, ScopesAdapter.getInstance()), fdObj); } } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java index 0a225e65a5..38a83c7ff6 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -20,7 +20,8 @@ public SentryFileReader(final @NotNull FileDescriptor fd) { super(new SentryFileInputStream(fd)); } - SentryFileReader(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException { - super(new SentryFileInputStream(file, hub)); + SentryFileReader(final @NotNull File file, final @NotNull IScopes scopes) + throws FileNotFoundException { + super(new SentryFileInputStream(file, scopes)); } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java index 9588984612..93c901ec6c 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -30,8 +30,8 @@ public SentryFileWriter(final @NotNull FileDescriptor fd) { super(new SentryFileOutputStream(fd)); } - SentryFileWriter(final @NotNull File file, final boolean append, final @NotNull IHub hub) + SentryFileWriter(final @NotNull File file, final boolean append, final @NotNull IScopes scopes) throws FileNotFoundException { - super(new SentryFileOutputStream(file, append, hub)); + super(new SentryFileOutputStream(file, append, scopes)); } } diff --git a/sentry/src/main/java/io/sentry/util/CheckInUtils.java b/sentry/src/main/java/io/sentry/util/CheckInUtils.java index e15603adaf..6719e24839 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -3,7 +3,7 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.MonitorConfig; import io.sentry.Sentry; import io.sentry.protocol.SentryId; @@ -30,18 +30,19 @@ public static U withCheckIn( final @Nullable MonitorConfig monitorConfig, final @NotNull Callable callable) throws Exception { - final @NotNull IHub hub = Sentry.getCurrentHub(); + final @NotNull IScopes scopes = Sentry.getCurrentScopes(); final long startTime = System.currentTimeMillis(); boolean didError = false; - hub.pushScope(); - TracingUtils.startNewTrace(hub); + // TODO fork instead + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); if (monitorConfig != null) { inProgressCheckIn.setMonitorConfig(monitorConfig); } - @Nullable SentryId checkInId = hub.captureCheckIn(inProgressCheckIn); + @Nullable SentryId checkInId = scopes.captureCheckIn(inProgressCheckIn); try { return callable.call(); } catch (Throwable t) { @@ -51,8 +52,8 @@ public static U withCheckIn( final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry/src/main/java/io/sentry/util/TracingUtils.java b/sentry/src/main/java/io/sentry/util/TracingUtils.java index 2aeb613f2d..67f6459660 100644 --- a/sentry/src/main/java/io/sentry/util/TracingUtils.java +++ b/sentry/src/main/java/io/sentry/util/TracingUtils.java @@ -2,8 +2,8 @@ import io.sentry.Baggage; import io.sentry.BaggageHeader; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.PropagationContext; import io.sentry.SentryOptions; @@ -14,8 +14,8 @@ public final class TracingUtils { - public static void startNewTrace(final @NotNull IHub hub) { - hub.configureScope( + public static void startNewTrace(final @NotNull IScopes scopes) { + scopes.configureScope( scope -> { scope.withPropagationContext( propagationContext -> { @@ -25,30 +25,30 @@ public static void startNewTrace(final @NotNull IHub hub) { } public static @Nullable TracingHeaders traceIfAllowed( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull String requestUrl, @Nullable List thirdPartyBaggageHeaders, final @Nullable ISpan span) { - final @NotNull SentryOptions sentryOptions = hub.getOptions(); + final @NotNull SentryOptions sentryOptions = scopes.getOptions(); if (sentryOptions.isTraceSampling() && shouldAttachTracingHeaders(requestUrl, sentryOptions)) { - return trace(hub, thirdPartyBaggageHeaders, span); + return trace(scopes, thirdPartyBaggageHeaders, span); } return null; } public static @Nullable TracingHeaders trace( - final @NotNull IHub hub, + final @NotNull IScopes scopes, @Nullable List thirdPartyBaggageHeaders, final @Nullable ISpan span) { - final @NotNull SentryOptions sentryOptions = hub.getOptions(); + final @NotNull SentryOptions sentryOptions = scopes.getOptions(); if (span != null && !span.isNoOp()) { return new TracingHeaders( span.toSentryTrace(), span.toBaggageHeader(thirdPartyBaggageHeaders)); } else { final @NotNull PropagationContextHolder returnValue = new PropagationContextHolder(); - hub.configureScope( + scopes.configureScope( (scope) -> { returnValue.propagationContext = maybeUpdateBaggage(scope, sentryOptions); }); diff --git a/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt b/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt index c0445efb23..1416fbbe3f 100644 --- a/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt @@ -33,7 +33,7 @@ class DefaultTransactionPerformanceCollectorTest { private class Fixture { lateinit var transaction1: ITransaction lateinit var transaction2: ITransaction - val hub: IHub = mock() + val scopes: IScopes = mock() val options = SentryOptions() var mockTimer: Timer? = null val deferredExecutorService = DeferredExecutorService() @@ -47,7 +47,7 @@ class DefaultTransactionPerformanceCollectorTest { } init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(memoryCollector: IPerformanceSnapshotCollector? = JavaMemoryCollector(), cpuCollector: IPerformanceSnapshotCollector? = mockCpuCollector, executorService: ISentryExecutorService = deferredExecutorService): TransactionPerformanceCollector { @@ -59,8 +59,8 @@ class DefaultTransactionPerformanceCollectorTest { if (memoryCollector != null) { options.addPerformanceCollector(memoryCollector) } - transaction1 = SentryTracer(TransactionContext("", ""), hub) - transaction2 = SentryTracer(TransactionContext("", ""), hub) + transaction1 = SentryTracer(TransactionContext("", ""), scopes) + transaction2 = SentryTracer(TransactionContext("", ""), scopes) val collector = DefaultTransactionPerformanceCollector(options) val timer: Timer = collector.getProperty("timer") ?: Timer(true) mockTimer = spy(timer) diff --git a/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt b/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt index e87f4256d5..0507b8499d 100644 --- a/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt +++ b/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt @@ -27,7 +27,7 @@ class DirectoryProcessorTest { private class Fixture { - var hub: IHub = mock() + var scopes: IScopes = mock() var envelopeReader: IEnvelopeReader = mock() var serializer: ISerializer = mock() var logger: ILogger = mock() @@ -40,7 +40,7 @@ class DirectoryProcessorTest { fun getSut(isRetryable: Boolean = false, isRateLimitingActive: Boolean = false): OutboxSender { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).then { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).then { HintUtils.runIfHasType( hintCaptor.firstValue, Enqueable::class.java @@ -52,7 +52,7 @@ class DirectoryProcessorTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(hub.rateLimiter).thenReturn(rateLimiter) + whenever(scopes.rateLimiter).thenReturn(rateLimiter) } } HintUtils.runIfHasType( @@ -62,7 +62,7 @@ class DirectoryProcessorTest { retryable.isRetry = isRetryable } } - return OutboxSender(hub, envelopeReader, serializer, logger, 500, 30) + return OutboxSender(scopes, envelopeReader, serializer, logger, 500, 30) } } @@ -91,7 +91,7 @@ class DirectoryProcessorTest { whenever(fixture.serializer.deserialize(any(), eq(SentryEvent::class.java))).thenReturn(event) fixture.getSut().processDirectory(file) - verify(fixture.hub).captureEvent(any(), argWhere { !HintUtils.hasType(it, ApplyScopeData::class.java) }) + verify(fixture.scopes).captureEvent(any(), argWhere { !HintUtils.hasType(it, ApplyScopeData::class.java) }) } @Test @@ -100,7 +100,7 @@ class DirectoryProcessorTest { dir.mkdirs() assertTrue(dir.exists()) // sanity check fixture.getSut().processDirectory(file) - verify(fixture.hub, never()).captureEnvelope(any(), any()) + verify(fixture.scopes, never()).captureEnvelope(any(), any()) } @Test @@ -121,7 +121,7 @@ class DirectoryProcessorTest { sut.processDirectory(file) // should only capture once - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @Test @@ -139,7 +139,7 @@ class DirectoryProcessorTest { sut.processDirectory(file) // should only capture once - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } private fun getTempEnvelope(fileName: String): String { diff --git a/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt b/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt index 6f0ea9cb8a..d63ee81854 100644 --- a/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt +++ b/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt @@ -23,7 +23,7 @@ import kotlin.test.assertFalse class EnvelopeSenderTest { private class Fixture { - var hub: IHub? = mock() + var scopes: IScopes? = mock() var logger: ILogger? = mock() var serializer: ISerializer? = mock() var options = SentryOptions().noFlushTimeout() @@ -35,7 +35,7 @@ class EnvelopeSenderTest { fun getSut(): EnvelopeSender { return EnvelopeSender( - hub!!, + scopes!!, serializer!!, logger!!, options.flushTimeoutMillis, @@ -62,7 +62,7 @@ class EnvelopeSenderTest { val sut = fixture.getSut() sut.processDirectory(File("i don't exist")) verify(fixture.logger)!!.log(eq(SentryLevel.WARNING), eq("Directory '%s' doesn't exist. No cached events to send."), any()) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -72,7 +72,7 @@ class EnvelopeSenderTest { testFile.deleteOnExit() sut.processDirectory(testFile) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq("Cache dir %s is not a directory."), any()) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -82,11 +82,11 @@ class EnvelopeSenderTest { sut.processDirectory(File(tempDirectory.toUri())) testFile.deleteOnExit() verify(fixture.logger)!!.log(eq(SentryLevel.DEBUG), eq("File '%s' doesn't match extension expected."), any()) - verify(fixture.hub, never())!!.captureEnvelope(any(), anyOrNull()) + verify(fixture.scopes, never())!!.captureEnvelope(any(), anyOrNull()) } @Test - fun `when directory has event files, processDirectory captures with hub`() { + fun `when directory has event files, processDirectory captures with scopes`() { val event = SentryEvent() val envelope = SentryEnvelope.from(fixture.serializer!!, event, null) whenever(fixture.serializer!!.deserializeEnvelope(any())).thenReturn(envelope) @@ -94,7 +94,7 @@ class EnvelopeSenderTest { val testFile = File(Files.createTempFile(tempDirectory, "send-cached-event-test", EnvelopeCache.SUFFIX_ENVELOPE_FILE).toUri()) testFile.deleteOnExit() sut.processDirectory(File(tempDirectory.toUri())) - verify(fixture.hub)!!.captureEnvelope(eq(envelope), any()) + verify(fixture.scopes)!!.captureEnvelope(eq(envelope), any()) } @Test @@ -108,12 +108,12 @@ class EnvelopeSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processFile(testFile, hints) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq(expected), eq("Failed to capture cached envelope %s"), eq(testFile.absolutePath)) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) assertFalse(testFile.exists()) } @Test - fun `when hub throws, file gets deleted`() { + fun `when scopes throws, file gets deleted`() { val expected = RuntimeException() whenever(fixture.serializer!!.deserializeEnvelope(any())).doThrow(expected) val sut = fixture.getSut() @@ -121,6 +121,6 @@ class EnvelopeSenderTest { testFile.deleteOnExit() sut.processFile(testFile, Hint()) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq(expected), eq("Failed to capture cached envelope %s"), eq(testFile.absolutePath)) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } } diff --git a/sentry/src/test/java/io/sentry/HubAdapterTest.kt b/sentry/src/test/java/io/sentry/HubAdapterTest.kt index 9686250d20..0e7e1d0f77 100644 --- a/sentry/src/test/java/io/sentry/HubAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/HubAdapterTest.kt @@ -13,11 +13,11 @@ import kotlin.test.Test class HubAdapterTest { - val hub: Hub = mock() + val scopes: IScopes = mock() @BeforeTest fun `set up`() { - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) } @AfterTest @@ -27,7 +27,7 @@ class HubAdapterTest { @Test fun `isEnabled calls Hub`() { HubAdapter.getInstance().isEnabled - verify(hub).isEnabled + verify(scopes).isEnabled } @Test fun `captureEvent calls Hub`() { @@ -35,27 +35,27 @@ class HubAdapterTest { val hint = mock() val scopeCallback = mock() HubAdapter.getInstance().captureEvent(event, hint) - verify(hub).captureEvent(eq(event), eq(hint)) + verify(scopes).captureEvent(eq(event), eq(hint)) HubAdapter.getInstance().captureEvent(event, hint, scopeCallback) - verify(hub).captureEvent(eq(event), eq(hint), eq(scopeCallback)) + verify(scopes).captureEvent(eq(event), eq(hint), eq(scopeCallback)) } @Test fun `captureMessage calls Hub`() { val scopeCallback = mock() val sentryLevel = mock() HubAdapter.getInstance().captureMessage("message", sentryLevel) - verify(hub).captureMessage(eq("message"), eq(sentryLevel)) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel)) HubAdapter.getInstance().captureMessage("message", sentryLevel, scopeCallback) - verify(hub).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) } @Test fun `captureEnvelope calls Hub`() { val envelope = mock() val hint = mock() HubAdapter.getInstance().captureEnvelope(envelope, hint) - verify(hub).captureEnvelope(eq(envelope), eq(hint)) + verify(scopes).captureEnvelope(eq(envelope), eq(hint)) } @Test fun `captureException calls Hub`() { @@ -63,145 +63,145 @@ class HubAdapterTest { val hint = mock() val scopeCallback = mock() HubAdapter.getInstance().captureException(throwable, hint) - verify(hub).captureException(eq(throwable), eq(hint)) + verify(scopes).captureException(eq(throwable), eq(hint)) HubAdapter.getInstance().captureException(throwable, hint, scopeCallback) - verify(hub).captureException(eq(throwable), eq(hint), eq(scopeCallback)) + verify(scopes).captureException(eq(throwable), eq(hint), eq(scopeCallback)) } @Test fun `captureUserFeedback calls Hub`() { val userFeedback = mock() HubAdapter.getInstance().captureUserFeedback(userFeedback) - verify(hub).captureUserFeedback(eq(userFeedback)) + verify(scopes).captureUserFeedback(eq(userFeedback)) } @Test fun `captureCheckIn calls Hub`() { val checkIn = mock() HubAdapter.getInstance().captureCheckIn(checkIn) - verify(hub).captureCheckIn(eq(checkIn)) + verify(scopes).captureCheckIn(eq(checkIn)) } @Test fun `startSession calls Hub`() { HubAdapter.getInstance().startSession() - verify(hub).startSession() + verify(scopes).startSession() } @Test fun `endSession calls Hub`() { HubAdapter.getInstance().endSession() - verify(hub).endSession() + verify(scopes).endSession() } @Test fun `close calls Hub`() { HubAdapter.getInstance().close() - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `close with isRestarting true calls Hub with isRestarting false`() { HubAdapter.getInstance().close(true) - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `close with isRestarting false calls Hub with isRestarting false`() { HubAdapter.getInstance().close(false) - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `addBreadcrumb calls Hub`() { val breadcrumb = mock() val hint = mock() HubAdapter.getInstance().addBreadcrumb(breadcrumb, hint) - verify(hub).addBreadcrumb(eq(breadcrumb), eq(hint)) + verify(scopes).addBreadcrumb(eq(breadcrumb), eq(hint)) } @Test fun `setLevel calls Hub`() { val sentryLevel = mock() HubAdapter.getInstance().setLevel(sentryLevel) - verify(hub).setLevel(eq(sentryLevel)) + verify(scopes).setLevel(eq(sentryLevel)) } @Test fun `setTransaction calls Hub`() { HubAdapter.getInstance().setTransaction("transaction") - verify(hub).setTransaction(eq("transaction")) + verify(scopes).setTransaction(eq("transaction")) } @Test fun `setUser calls Hub`() { val user = mock() HubAdapter.getInstance().setUser(user) - verify(hub).setUser(eq(user)) + verify(scopes).setUser(eq(user)) } @Test fun `setFingerprint calls Hub`() { val fingerprint = ArrayList() HubAdapter.getInstance().setFingerprint(fingerprint) - verify(hub).setFingerprint(eq(fingerprint)) + verify(scopes).setFingerprint(eq(fingerprint)) } @Test fun `clearBreadcrumbs calls Hub`() { HubAdapter.getInstance().clearBreadcrumbs() - verify(hub).clearBreadcrumbs() + verify(scopes).clearBreadcrumbs() } @Test fun `setTag calls Hub`() { HubAdapter.getInstance().setTag("key", "value") - verify(hub).setTag(eq("key"), eq("value")) + verify(scopes).setTag(eq("key"), eq("value")) } @Test fun `removeTag calls Hub`() { HubAdapter.getInstance().removeTag("key") - verify(hub).removeTag(eq("key")) + verify(scopes).removeTag(eq("key")) } @Test fun `setExtra calls Hub`() { HubAdapter.getInstance().setExtra("key", "value") - verify(hub).setExtra(eq("key"), eq("value")) + verify(scopes).setExtra(eq("key"), eq("value")) } @Test fun `removeExtra calls Hub`() { HubAdapter.getInstance().removeExtra("key") - verify(hub).removeExtra(eq("key")) + verify(scopes).removeExtra(eq("key")) } @Test fun `getLastEventId calls Hub`() { HubAdapter.getInstance().lastEventId - verify(hub).lastEventId + verify(scopes).lastEventId } @Test fun `pushScope calls Hub`() { HubAdapter.getInstance().pushScope() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `popScope calls Hub`() { HubAdapter.getInstance().popScope() - verify(hub).popScope() + verify(scopes).popScope() } @Test fun `withScope calls Hub`() { val scopeCallback = mock() HubAdapter.getInstance().withScope(scopeCallback) - verify(hub).withScope(eq(scopeCallback)) + verify(scopes).withScope(eq(scopeCallback)) } @Test fun `configureScope calls Hub`() { val scopeCallback = mock() HubAdapter.getInstance().configureScope(scopeCallback) - verify(hub).configureScope(eq(scopeCallback)) + verify(scopes).configureScope(eq(scopeCallback)) } @Test fun `bindClient calls Hub`() { val client = mock() HubAdapter.getInstance().bindClient(client) - verify(hub).bindClient(eq(client)) + verify(scopes).bindClient(eq(client)) } @Test fun `flush calls Hub`() { HubAdapter.getInstance().flush(1) - verify(hub).flush(eq(1)) + verify(scopes).flush(eq(1)) } @Test fun `clone calls Hub`() { HubAdapter.getInstance().clone() - verify(hub).clone() + verify(scopes).clone() } @Test fun `captureTransaction calls Hub`() { @@ -210,7 +210,7 @@ class HubAdapterTest { val hint = mock() val profilingTraceData = mock() HubAdapter.getInstance().captureTransaction(transaction, traceContext, hint, profilingTraceData) - verify(hub).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) + verify(scopes).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) } @Test fun `startTransaction calls Hub`() { @@ -218,48 +218,48 @@ class HubAdapterTest { val samplingContext = mock() val transactionOptions = mock() HubAdapter.getInstance().startTransaction(transactionContext) - verify(hub).startTransaction(eq(transactionContext), any()) + verify(scopes).startTransaction(eq(transactionContext), any()) - reset(hub) + reset(scopes) HubAdapter.getInstance().startTransaction(transactionContext, transactionOptions) - verify(hub).startTransaction(eq(transactionContext), eq(transactionOptions)) + verify(scopes).startTransaction(eq(transactionContext), eq(transactionOptions)) } @Test fun `traceHeaders calls Hub`() { HubAdapter.getInstance().traceHeaders() - verify(hub).traceHeaders() + verify(scopes).traceHeaders() } @Test fun `setSpanContext calls Hub`() { val throwable = mock() val span = mock() HubAdapter.getInstance().setSpanContext(throwable, span, "transactionName") - verify(hub).setSpanContext(eq(throwable), eq(span), eq("transactionName")) + verify(scopes).setSpanContext(eq(throwable), eq(span), eq("transactionName")) } @Test fun `getSpan calls Hub`() { HubAdapter.getInstance().span - verify(hub).span + verify(scopes).span } @Test fun `getTransaction calls Hub`() { HubAdapter.getInstance().transaction - verify(hub).transaction + verify(scopes).transaction } @Test fun `getOptions calls Hub`() { HubAdapter.getInstance().options - verify(hub).options + verify(scopes).options } @Test fun `isCrashedLastRun calls Hub`() { HubAdapter.getInstance().isCrashedLastRun - verify(hub).isCrashedLastRun + verify(scopes).isCrashedLastRun } @Test fun `reportFullyDisplayed calls Hub`() { HubAdapter.getInstance().reportFullyDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } } diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt index 8fac963e70..f30fd0a966 100644 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ b/sentry/src/test/java/io/sentry/HubTest.kt @@ -72,7 +72,7 @@ class HubTest { } @Test - fun `when hub is cloned, integrations are not registered`() { + fun `when scopes is cloned, integrations are not registered`() { val integrationMock = mock() val options = SentryOptions() options.cacheDirPath = file.absolutePath @@ -80,36 +80,36 @@ class HubTest { options.setSerializer(mock()) options.addIntegration(integrationMock) // val expected = HubAdapter.getInstance() - val hub = Hub(options) + val scopes = Hub(options) // verify(integrationMock).register(expected, options) - hub.clone() + scopes.clone() verifyNoMoreInteractions(integrationMock) } @Test - fun `when hub is cloned, scope changes are isolated`() { + fun `when scopes is cloned, scope changes are isolated`() { val options = SentryOptions() options.cacheDirPath = file.absolutePath options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) - val hub = Hub(options) + val scopes = Hub(options) var firstScope: IScope? = null - hub.configureScope { + scopes.configureScope { firstScope = it - it.setTag("hub", "a") + it.setTag("scopes", "a") } var cloneScope: IScope? = null - val clone = hub.clone() + val clone = scopes.clone() clone.configureScope { cloneScope = it - it.setTag("hub", "b") + it.setTag("scopes", "b") } - assertEquals("a", firstScope!!.tags["hub"]) - assertEquals("b", cloneScope!!.tags["hub"]) + assertEquals("a", firstScope!!.tags["scopes"]) + assertEquals("b", cloneScope!!.tags["scopes"]) } @Test - fun `when hub is initialized, breadcrumbs are capped as per options`() { + fun `when scopes is initialized, breadcrumbs are capped as per options`() { val options = SentryOptions() options.cacheDirPath = file.absolutePath options.maxBreadcrumbs = 5 @@ -288,7 +288,7 @@ class HubTest { } @Test - fun `when captureEvent is called on disabled hub, lastEventId does not get overwritten`() { + fun `when captureEvent is called on disabled scopes, lastEventId does not get overwritten`() { val (sut, mockClient) = getEnabledHub() whenever(mockClient.captureEvent(any(), any(), anyOrNull())).thenReturn(SentryId(UUID.randomUUID())) val event = SentryEvent() @@ -827,14 +827,14 @@ class HubTest { @Test fun `when withScope throws an exception then it should be caught`() { - val (hub, _, logger) = getEnabledHub() + val (scopes, _, logger) = getEnabledHub() val exception = Exception("scope callback exception") val scopeCallback = ScopeCallback { throw exception } - hub.withScope(scopeCallback) + scopes.withScope(scopeCallback) verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) } @@ -864,25 +864,25 @@ class HubTest { @Test fun `when configureScope throws an exception then it should be caught`() { - val (hub, _, logger) = getEnabledHub() + val (scopes, _, logger) = getEnabledHub() val exception = Exception("scope callback exception") val scopeCallback = ScopeCallback { throw exception } - hub.configureScope(scopeCallback) + scopes.configureScope(scopeCallback) verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) } //endregion @Test - fun `when integration is registered, hub is enabled`() { + fun `when integration is registered, scopes is enabled`() { val mock = mock() var options: SentryOptions? = null - // init main hub and make it enabled + // init main scopes and make it enabled Sentry.init { it.addIntegration(mock) it.dsn = "https://key@sentry.io/proj" @@ -892,8 +892,8 @@ class HubTest { } doAnswer { - val hub = it.arguments[0] as IHub - assertTrue(hub.isEnabled) + val scopes = it.arguments[0] as IScopes + assertTrue(scopes.isEnabled) }.whenever(mock).register(any(), eq(options!!)) verify(mock).register(any(), eq(options!!)) @@ -902,26 +902,26 @@ class HubTest { //region setLevel tests @Test fun `when setLevel is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setLevel(SentryLevel.INFO) + scopes.setLevel(SentryLevel.INFO) assertNull(scope?.level) } @Test fun `when setLevel is called, level is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setLevel(SentryLevel.INFO) + scopes.setLevel(SentryLevel.INFO) assertEquals(SentryLevel.INFO, scope?.level) } //endregion @@ -929,74 +929,74 @@ class HubTest { //region setTransaction tests @Test fun `when setTransaction is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setTransaction("test") + scopes.setTransaction("test") assertNull(scope?.transactionName) } @Test fun `when setTransaction is called, and transaction is not set, transaction name is changed`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setTransaction("test") + scopes.setTransaction("test") assertEquals("test", scope?.transactionName) } @Test fun `when setTransaction is called, and transaction is set, transaction name is changed`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - val tx = hub.startTransaction("test", "op") - hub.configureScope { it.setTransaction(tx) } + val tx = scopes.startTransaction("test", "op") + scopes.configureScope { it.setTransaction(tx) } assertEquals("test", scope?.transactionName) } @Test fun `when startTransaction is called with different instrumenter, no-op is returned`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } val transactionOptions = TransactionOptions() - val tx = hub.startTransaction(transactionContext, transactionOptions) + val tx = scopes.startTransaction(transactionContext, transactionOptions) assertTrue(tx is NoOpTransaction) } @Test fun `when startTransaction is called with different instrumenter, no-op is returned 2`() { - val hub = generateHub() { + val scopes = generateHub() { it.instrumenter = Instrumenter.OTEL } - val tx = hub.startTransaction("test", "op") + val tx = scopes.startTransaction("test", "op") assertTrue(tx is NoOpTransaction) } @Test fun `when startTransaction is called with configured instrumenter, it works`() { - val hub = generateHub() { + val scopes = generateHub() { it.instrumenter = Instrumenter.OTEL } val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } val transactionOptions = TransactionOptions() - val tx = hub.startTransaction(transactionContext, transactionOptions) + val tx = scopes.startTransaction(transactionContext, transactionOptions) assertFalse(tx is NoOpTransaction) } @@ -1005,27 +1005,27 @@ class HubTest { //region setUser tests @Test fun `when setUser is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setUser(User()) + scopes.setUser(User()) assertNull(scope?.user) } @Test fun `when setUser is called, user is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } val user = User() - hub.setUser(user) + scopes.setUser(user) assertEquals(user, scope?.user) } //endregion @@ -1033,40 +1033,40 @@ class HubTest { //region setFingerprint tests @Test fun `when setFingerprint is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() val fingerprint = listOf("abc") - hub.setFingerprint(fingerprint) + scopes.setFingerprint(fingerprint) assertEquals(0, scope?.fingerprint?.count()) } @Test fun `when setFingerprint is called with null parameter, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setFingerprint", List::class.java, null) + scopes.callMethod("setFingerprint", List::class.java, null) assertEquals(0, scope?.fingerprint?.count()) } @Test fun `when setFingerprint is called, fingerprint is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } val fingerprint = listOf("abc") - hub.setFingerprint(fingerprint) + scopes.setFingerprint(fingerprint) assertEquals(1, scope?.fingerprint?.count()) } //endregion @@ -1074,30 +1074,30 @@ class HubTest { //region clearBreadcrumbs tests @Test fun `when clearBreadcrumbs is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.addBreadcrumb(Breadcrumb()) + scopes.addBreadcrumb(Breadcrumb()) assertEquals(1, scope?.breadcrumbs?.count()) - hub.close() + scopes.close() assertEquals(0, scope?.breadcrumbs?.count()) } @Test fun `when clearBreadcrumbs is called, clear breadcrumbs`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.addBreadcrumb(Breadcrumb()) + scopes.addBreadcrumb(Breadcrumb()) assertEquals(1, scope?.breadcrumbs?.count()) - hub.clearBreadcrumbs() + scopes.clearBreadcrumbs() assertEquals(0, scope?.breadcrumbs?.count()) } //endregion @@ -1105,38 +1105,38 @@ class HubTest { //region setTag tests @Test fun `when setTag is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setTag("test", "test") + scopes.setTag("test", "test") assertEquals(0, scope?.tags?.count()) } @Test fun `when setTag is called with null parameters, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + scopes.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) assertEquals(0, scope?.tags?.count()) } @Test fun `when setTag is called, tag is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setTag("test", "test") + scopes.setTag("test", "test") assertEquals(1, scope?.tags?.count()) } //endregion @@ -1144,38 +1144,38 @@ class HubTest { //region setExtra tests @Test fun `when setExtra is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setExtra("test", "test") + scopes.setExtra("test", "test") assertEquals(0, scope?.extras?.count()) } @Test fun `when setExtra is called with null parameters, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + scopes.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) assertEquals(0, scope?.extras?.count()) } @Test fun `when setExtra is called, extra is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setExtra("test", "test") + scopes.setExtra("test", "test") assertEquals(1, scope?.extras?.count()) } //endregion @@ -1488,19 +1488,19 @@ class HubTest { val mockTransactionProfiler = mock() val mockClient = mock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val hub = generateHub { + val scopes = generateHub { it.setTransactionProfiler(mockTransactionProfiler) } - hub.bindClient(mockClient) + scopes.bindClient(mockClient) // Transaction is not sampled, so it should not be profiled val contexts = TransactionContext("name", "op", TracesSamplingDecision(false, null, true, null)) - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) transaction.finish() verify(mockClient, never()).captureEnvelope(any()) // Transaction is sampled, so it should be profiled val sampledContexts = TransactionContext("name", "op", TracesSamplingDecision(true, null, true, null)) - val sampledTransaction = hub.startTransaction(sampledContexts) + val sampledTransaction = scopes.startTransaction(sampledContexts) sampledTransaction.finish() verify(mockClient).captureEnvelope(any()) } @@ -1510,13 +1510,13 @@ class HubTest { val mockTransactionProfiler = mock() val mockClient = mock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 0.0 it.setTransactionProfiler(mockTransactionProfiler) } - hub.bindClient(mockClient) + scopes.bindClient(mockClient) val contexts = TransactionContext("name", "op") - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) transaction.finish() verify(mockClient, never()).captureEnvelope(any()) } @@ -1525,12 +1525,12 @@ class HubTest { fun `when profiler is running and isAppStartTransaction is false, startTransaction does not interact with profiler`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) verify(mockTransactionProfiler, never()).start() verify(mockTransactionProfiler, never()).bindTransaction(any()) } @@ -1539,12 +1539,12 @@ class HubTest { fun `when profiler is running and isAppStartTransaction is true, startTransaction binds current profile`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - val transaction = hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) verify(mockTransactionProfiler, never()).start() verify(mockTransactionProfiler).bindTransaction(eq(transaction)) } @@ -1553,12 +1553,12 @@ class HubTest { fun `when profiler is not running, startTransaction starts and binds current profile`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(false) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - val transaction = hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) verify(mockTransactionProfiler).start() verify(mockTransactionProfiler).bindTransaction(eq(transaction)) } @@ -1567,75 +1567,75 @@ class HubTest { //region startTransaction tests @Test fun `when startTransaction, creates transaction`() { - val hub = generateHub() + val scopes = generateHub() val contexts = TransactionContext("name", "op") - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) assertTrue(transaction is SentryTracer) assertEquals(contexts, transaction.root.spanContext) } @Test fun `when startTransaction with bindToScope set to false, transaction is not attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - hub.startTransaction("name", "op", TransactionOptions()) + scopes.startTransaction("name", "op", TransactionOptions()) - hub.configureScope { + scopes.configureScope { assertNull(it.span) } } @Test fun `when startTransaction without bindToScope set, transaction is not attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - hub.startTransaction("name", "op") + scopes.startTransaction("name", "op") - hub.configureScope { + scopes.configureScope { assertNull(it.span) } } @Test fun `when startTransaction with bindToScope set to true, transaction is attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - val transaction = hub.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) + val transaction = scopes.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) - hub.configureScope { + scopes.configureScope { assertEquals(transaction, it.span) } } @Test fun `when startTransaction and no tracing sampling is configured, event is not sampled`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = 0.0 } - val transaction = hub.startTransaction("name", "op") + val transaction = scopes.startTransaction("name", "op") assertFalse(transaction.isSampled!!) } @Test fun `when startTransaction and no profile sampling is configured, profile is not sampled`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = 1.0 it.profilesSampleRate = 0.0 } - val transaction = hub.startTransaction("name", "op") + val transaction = scopes.startTransaction("name", "op") assertTrue(transaction.isSampled!!) assertFalse(transaction.isProfileSampled!!) } @Test fun `when startTransaction with parent sampled and no traces sampler provided, transaction inherits sampling decision`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("name", "op") transactionContext.parentSampled = true - val transaction = hub.startTransaction(transactionContext) + val transaction = scopes.startTransaction(transactionContext) assertNotNull(transaction) assertNotNull(transaction.isSampled) assertTrue(transaction.isSampled!!) @@ -1643,10 +1643,10 @@ class HubTest { @Test fun `when startTransaction with parent profile sampled and no profile sampler provided, transaction inherits profile sampling decision`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("name", "op") transactionContext.setParentSampled(true, true) - val transaction = hub.startTransaction(transactionContext) + val transaction = scopes.startTransaction(transactionContext) assertTrue(transaction.isProfileSampled!!) } @@ -1717,11 +1717,11 @@ class HubTest { @Test fun `when tracesSampleRate and tracesSampler are not set on SentryOptions, startTransaction returns NoOp`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = null it.tracesSampler = null } - val transaction = hub.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) + val transaction = scopes.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) assertTrue(transaction is NoOpTransaction) } //endregion @@ -1729,81 +1729,81 @@ class HubTest { //region startTransaction tests @Test fun `when traceHeaders and no transaction is active, traceHeaders are generated from scope`() { - val hub = generateHub() + val scopes = generateHub() var spanId: SpanId? = null - hub.configureScope { spanId = it.propagationContext.spanId } + scopes.configureScope { spanId = it.propagationContext.spanId } - val traceHeader = hub.traceHeaders() + val traceHeader = scopes.traceHeaders() assertNotNull(traceHeader) assertEquals(spanId, traceHeader.spanId) } @Test fun `when traceHeaders and there is an active transaction, traceHeaders are not null`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.setTransaction(tx) } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } - assertNotNull(hub.traceHeaders()) + assertNotNull(scopes.traceHeaders()) } //endregion //region getSpan tests @Test fun `when there is no active transaction, getSpan returns null`() { - val hub = generateHub() - assertNull(hub.span) + val scopes = generateHub() + assertNull(scopes.span) } @Test fun `when there is no active transaction, getTransaction returns null`() { - val hub = generateHub() - assertNull(hub.transaction) + val scopes = generateHub() + assertNull(scopes.transaction) } @Test fun `when there is active transaction bound to the scope, getTransaction and getSpan return active transaction`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.transaction = tx } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.transaction = tx } - assertEquals(tx, hub.transaction) - assertEquals(tx, hub.span) + assertEquals(tx, scopes.transaction) + assertEquals(tx, scopes.span) } @Test - fun `when there is a transaction but the hub is closed, getTransaction returns null`() { - val hub = generateHub() - hub.startTransaction("name", "op") - hub.close() + fun `when there is a transaction but the scopes is closed, getTransaction returns null`() { + val scopes = generateHub() + scopes.startTransaction("name", "op") + scopes.close() - assertNull(hub.transaction) + assertNull(scopes.transaction) } @Test fun `when there is active span within a transaction bound to the scope, getSpan returns active span`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.setTransaction(tx) } - hub.configureScope { it.setTransaction(tx) } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } + scopes.configureScope { it.setTransaction(tx) } val span = tx.startChild("op") - assertEquals(tx, hub.transaction) - assertEquals(span, hub.span) + assertEquals(tx, scopes.transaction) + assertEquals(span, scopes.span) } // endregion //region setSpanContext @Test fun `associates span context with throwable`() { - val (hub, mockClient) = getEnabledHub() - val transaction = hub.startTransaction("aTransaction", "op") + val (scopes, mockClient) = getEnabledHub() + val transaction = scopes.startTransaction("aTransaction", "op") val span = transaction.startChild("op") val exception = RuntimeException() - hub.setSpanContext(exception, span, "tx-name") - hub.captureEvent(SentryEvent(exception)) + scopes.setSpanContext(exception, span, "tx-name") + scopes.captureEvent(SentryEvent(exception)) verify(mockClient).captureEvent( check { @@ -1816,8 +1816,8 @@ class HubTest { @Test fun `returns null when no span context associated with throwable`() { - val hub = generateHub() as Hub - assertNull(hub.getSpanContext(RuntimeException())) + val scopes = generateHub() as Hub + assertNull(scopes.getSpanContext(RuntimeException())) } // endregion @@ -1826,9 +1826,9 @@ class HubTest { val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) nativeMarker.mkdirs() nativeMarker.createNewFile() - val hub = generateHub() as Hub + val scopes = generateHub() as Hub - assertTrue(hub.isCrashedLastRun!!) + assertTrue(scopes.isCrashedLastRun!!) assertTrue(nativeMarker.exists()) } @@ -1837,69 +1837,69 @@ class HubTest { val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) nativeMarker.mkdirs() nativeMarker.createNewFile() - val hub = generateHub { + val scopes = generateHub { it.isEnableAutoSessionTracking = false } - assertTrue(hub.isCrashedLastRun!!) + assertTrue(scopes.isCrashedLastRun!!) assertFalse(nativeMarker.exists()) } @Test fun `reportFullyDisplayed is ignored if TimeToFullDisplayTracing is disabled`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertFalse(called) } @Test fun `reportFullyDisplayed calls FullyDisplayedReporter if TimeToFullDisplayTracing is enabled`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.isEnableTimeToFullDisplayTracing = true it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) } @Test fun `reportFullyDisplayed calls FullyDisplayedReporter only once`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.isEnableTimeToFullDisplayTracing = true it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) } @Test fun `reportFullDisplayed calls reportFullyDisplayed`() { - val hub = spy(generateHub()) - hub.reportFullDisplayed() - verify(hub).reportFullyDisplayed() + val scopes = spy(generateHub()) + scopes.reportFullDisplayed() + verify(scopes).reportFullyDisplayed() } @Test fun `continueTrace creates propagation context from headers and returns transaction context if performance enabled`() { - val hub = generateHub() + val scopes = generateHub() val traceId = SentryId() val parentSpanId = SpanId() - val transactionContext = hub.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertEquals(traceId, scope.propagationContext.traceId) assertEquals(parentSpanId, scope.propagationContext.parentSpanId) } @@ -1910,16 +1910,16 @@ class HubTest { @Test fun `continueTrace creates new propagation context if header invalid and returns transaction context if performance enabled`() { - val hub = generateHub() + val scopes = generateHub() val traceId = SentryId() var propagationContextHolder = AtomicReference() - hub.configureScope { propagationContextHolder.set(it.propagationContext) } + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } val propagationContextAtStart = propagationContextHolder.get()!! - val transactionContext = hub.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) @@ -1932,12 +1932,12 @@ class HubTest { @Test fun `continueTrace creates propagation context from headers and returns null if performance disabled`() { - val hub = generateHub { it.enableTracing = false } + val scopes = generateHub { it.enableTracing = false } val traceId = SentryId() val parentSpanId = SpanId() - val transactionContext = hub.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertEquals(traceId, scope.propagationContext.traceId) assertEquals(parentSpanId, scope.propagationContext.parentSpanId) } @@ -1947,16 +1947,16 @@ class HubTest { @Test fun `continueTrace creates new propagation context if header invalid and returns null if performance disabled`() { - val hub = generateHub { it.enableTracing = false } + val scopes = generateHub { it.enableTracing = false } val traceId = SentryId() var propagationContextHolder = AtomicReference() - hub.configureScope { propagationContextHolder.set(it.propagationContext) } + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } val propagationContextAtStart = propagationContextHolder.get()!! - val transactionContext = hub.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) @@ -1966,32 +1966,32 @@ class HubTest { } @Test - fun `hub provides no tags for metrics, if metric option is disabled`() { - val hub = generateHub { + fun `scopes provides no tags for metrics, if metric option is disabled`() { + val scopes = generateHub { it.isEnableMetrics = false it.isEnableDefaultTagsForMetrics = true } as Hub assertTrue( - hub.defaultTagsForMetrics.isEmpty() + scopes.defaultTagsForMetrics.isEmpty() ) } @Test - fun `hub provides no tags for metrics, if default tags option is disabled`() { - val hub = generateHub { + fun `scopes provides no tags for metrics, if default tags option is disabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = false } as Hub assertTrue( - hub.defaultTagsForMetrics.isEmpty() + scopes.defaultTagsForMetrics.isEmpty() ) } @Test - fun `hub provides minimum default tags for metrics, if nothing is set up`() { - val hub = generateHub { + fun `scopes provides minimum default tags for metrics, if nothing is set up`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = true } as Hub @@ -2000,19 +2000,19 @@ class HubTest { mapOf( "environment" to "production" ), - hub.defaultTagsForMetrics + scopes.defaultTagsForMetrics ) } @Test - fun `hub provides default tags for metrics, based on options and running transaction`() { - val hub = generateHub { + fun `scopes provides default tags for metrics, based on options and running transaction`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = true it.environment = "test" it.release = "1.0" } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } @@ -2024,72 +2024,72 @@ class HubTest { "release" to "1.0", "transaction" to "name" ), - hub.defaultTagsForMetrics + scopes.defaultTagsForMetrics ) } @Test - fun `hub provides no local metric aggregator if metrics feature is disabled`() { - val hub = generateHub { + fun `scopes provides no local metric aggregator if metrics feature is disabled`() { + val scopes = generateHub { it.isEnableMetrics = false it.isEnableSpanLocalMetricAggregation = true } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNull(hub.localMetricsAggregator) + assertNull(scopes.localMetricsAggregator) } @Test - fun `hub provides no local metric aggregator if local aggregation feature is disabled`() { - val hub = generateHub { + fun `scopes provides no local metric aggregator if local aggregation feature is disabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = false } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNull(hub.localMetricsAggregator) + assertNull(scopes.localMetricsAggregator) } @Test - fun `hub provides local metric aggregator if feature is enabled`() { - val hub = generateHub { + fun `scopes provides local metric aggregator if feature is enabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = true } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNotNull(hub.localMetricsAggregator) + assertNotNull(scopes.localMetricsAggregator) } @Test - fun `hub startSpanForMetric starts a child span`() { - val hub = generateHub { + fun `scopes startSpanForMetric starts a child span`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = true it.sampleRate = 1.0 } as Hub - val txn = hub.startTransaction( + val txn = scopes.startTransaction( "name.txn", "op.txn", TransactionOptions().apply { isBindToScope = true } ) - val span = hub.startSpanForMetric("op", "key")!! + val span = scopes.startSpanForMetric("op", "key")!! assertEquals("op", span.spanContext.op) assertEquals("key", span.spanContext.description) @@ -2098,7 +2098,7 @@ class HubTest { private val dsnTest = "https://key@sentry.io/proj" - private fun generateHub(optionsConfiguration: Sentry.OptionsConfiguration? = null): IHub { + private fun generateHub(optionsConfiguration: Sentry.OptionsConfiguration? = null): IScopes { val options = SentryOptions().apply { dsn = dsnTest cacheDirPath = file.absolutePath diff --git a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt index 8dc4f804bc..bd3a3c2cff 100644 --- a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt @@ -43,7 +43,7 @@ class JsonSerializerTest { private class Fixture { val logger: ILogger = mock() val serializer: ISerializer - val hub = mock() + val scopes = mock() val traceFile = Files.createTempFile("test", "here").toFile() val options = SentryOptions() @@ -51,7 +51,7 @@ class JsonSerializerTest { options.dsn = "https://key@sentry.io/proj" options.setLogger(logger) options.isDebug = true - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) serializer = JsonSerializer(options) options.setSerializer(serializer) options.setEnvelopeReader(EnvelopeReader(serializer)) @@ -830,7 +830,7 @@ class JsonSerializerTest { trace.status = SpanStatus.OK trace.setTag("myTag", "myValue") trace.sampled = true - val tracer = SentryTracer(trace, fixture.hub) + val tracer = SentryTracer(trace, fixture.scopes) tracer.setData("dataKey", "dataValue") val span = tracer.startChild("child") span.finish(SpanStatus.OK) @@ -1300,7 +1300,7 @@ class JsonSerializerTest { status = SpanStatus.OK setTag("myTag", "myValue") } - val tracer = SentryTracer(trace, fixture.hub) + val tracer = SentryTracer(trace, fixture.scopes) val span = tracer.startChild("child") span.setMeasurement("test_measurement", 1, MeasurementUnit.Custom("test")) span.finish(SpanStatus.OK) diff --git a/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt b/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt index ec932ebc86..682626f08c 100644 --- a/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt +++ b/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt @@ -33,7 +33,7 @@ class MainEventProcessorTest { dist = "dist" sdkVersion = SdkVersion("test", "1.2.3") } - val hub = mock() + val scopes = mock() val getLocalhost = mock() lateinit var sentryTracer: SentryTracer private val hostnameCacheMock = Mockito.mockStatic(HostnameCache::class.java) @@ -72,8 +72,8 @@ class MainEventProcessorTest { } host } - whenever(hub.options).thenReturn(sentryOptions) - sentryTracer = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(sentryOptions) + sentryTracer = SentryTracer(TransactionContext("", ""), scopes) val hostnameCache = HostnameCache(hostnameCacheDuration) { getLocalhost } hostnameCacheMock.`when` { HostnameCache.getInstance() }.thenReturn(hostnameCache) diff --git a/sentry/src/test/java/io/sentry/OutboxSenderTest.kt b/sentry/src/test/java/io/sentry/OutboxSenderTest.kt index ab50e054d0..9274ed4400 100644 --- a/sentry/src/test/java/io/sentry/OutboxSenderTest.kt +++ b/sentry/src/test/java/io/sentry/OutboxSenderTest.kt @@ -30,7 +30,7 @@ class OutboxSenderTest { private class Fixture { val options = mock() - val hub = mock() + val scopes = mock() var envelopeReader = mock() val serializer = mock() val logger = mock() @@ -39,11 +39,11 @@ class OutboxSenderTest { whenever(options.dsn).thenReturn("https://key@sentry.io/proj") whenever(options.dateProvider).thenReturn(SentryNanotimeDateProvider()) whenever(options.mainThreadChecker).thenReturn(NoOpMainThreadChecker.getInstance()) - whenever(hub.options).thenReturn(this.options) + whenever(scopes.options).thenReturn(this.options) } fun getSut(): OutboxSender { - return OutboxSender(hub, envelopeReader, serializer, logger, 15000, 30) + return OutboxSender(scopes, envelopeReader, serializer, logger, 15000, 30) } } @@ -83,7 +83,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEvent(eq(expected), any()) + verify(fixture.scopes).captureEvent(eq(expected), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -94,7 +94,7 @@ class OutboxSenderTest { fun `when parser is EnvelopeReader and serializer return SentryTransaction, transaction captured, transactions sampled, file is deleted`() { fixture.envelopeReader = EnvelopeReader(JsonSerializer(fixture.options)) whenever(fixture.options.maxSpans).thenReturn(1000) - whenever(fixture.hub.options).thenReturn(fixture.options) + whenever(fixture.scopes.options).thenReturn(fixture.options) whenever(fixture.options.transactionProfiler).thenReturn(NoOpTransactionProfiler.getInstance()) val transactionContext = TransactionContext("fixture-name", "http") @@ -102,7 +102,7 @@ class OutboxSenderTest { transactionContext.status = SpanStatus.OK transactionContext.setTag("fixture-tag", "fixture-value") - val sentryTracer = SentryTracer(transactionContext, fixture.hub) + val sentryTracer = SentryTracer(transactionContext, fixture.scopes) val span = sentryTracer.startChild("child") span.finish(SpanStatus.OK) sentryTracer.finish() @@ -120,7 +120,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(expected, it) assertTrue(it.isSampled) @@ -139,7 +139,7 @@ class OutboxSenderTest { fun `restores sampleRate`() { fixture.envelopeReader = EnvelopeReader(JsonSerializer(fixture.options)) whenever(fixture.options.maxSpans).thenReturn(1000) - whenever(fixture.hub.options).thenReturn(fixture.options) + whenever(fixture.scopes.options).thenReturn(fixture.options) whenever(fixture.options.transactionProfiler).thenReturn(NoOpTransactionProfiler.getInstance()) val transactionContext = TransactionContext("fixture-name", "http") @@ -148,7 +148,7 @@ class OutboxSenderTest { transactionContext.setTag("fixture-tag", "fixture-value") transactionContext.samplingDecision = TracesSamplingDecision(true, 0.00000021) - val sentryTracer = SentryTracer(transactionContext, fixture.hub) + val sentryTracer = SentryTracer(transactionContext, fixture.scopes) val span = sentryTracer.startChild("child") span.finish(SpanStatus.OK) sentryTracer.finish() @@ -166,7 +166,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(expected, it) assertTrue(it.isSampled) @@ -207,7 +207,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -225,7 +225,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEnvelope(any(), any()) + verify(fixture.scopes).captureEnvelope(any(), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -245,7 +245,7 @@ class OutboxSenderTest { // Additionally make sure we have no errors logged verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) assertFalse(File(path).exists()) } @@ -263,7 +263,7 @@ class OutboxSenderTest { // Additionally make sure we have no errors logged verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) assertFalse(File(path).exists()) } diff --git a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt index 239e90905e..87aa5e6715 100644 --- a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt +++ b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt @@ -21,7 +21,7 @@ class PreviousSessionFinalizerTest { class Fixture { val options = SentryOptions() - val hub = mock() + val scopes = mock() val logger = mock() lateinit var sessionFile: File @@ -61,7 +61,7 @@ class PreviousSessionFinalizerTest { nativeCrashMarker.writeText(nativeCrashTimestamp.toString()) } } - return PreviousSessionFinalizer(options, hub) + return PreviousSessionFinalizer(options, scopes) } fun sessionFromEnvelope(envelope: SentryEnvelope): Session { @@ -80,7 +80,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(null) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -88,7 +88,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = false) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -96,7 +96,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = true, session = null) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -107,7 +107,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -133,7 +133,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -156,7 +156,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -170,7 +170,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = true) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) assertFalse(fixture.sessionFile.exists()) } @@ -189,7 +189,7 @@ class PreviousSessionFinalizerTest { argThat { startsWith("Timed out waiting to flush previous session to its own file in session finalizer.") }, any() ) - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -202,6 +202,6 @@ class PreviousSessionFinalizerTest { argThat { startsWith("Timed out waiting to flush previous session to its own file in session finalizer.") }, any() ) - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } } diff --git a/sentry/src/test/java/io/sentry/ScopeTest.kt b/sentry/src/test/java/io/sentry/ScopeTest.kt index 906c897c62..86794d7b19 100644 --- a/sentry/src/test/java/io/sentry/ScopeTest.kt +++ b/sentry/src/test/java/io/sentry/ScopeTest.kt @@ -114,7 +114,7 @@ class ScopeTest { scope.setExtra("extra", "extra") val transaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val attachment = Attachment("path/log.txt") @@ -192,7 +192,7 @@ class ScopeTest { scope.setTransaction( SentryTracer( TransactionContext("newTransaction", "op"), - NoOpHub.getInstance() + NoOpScopes.getInstance() ) ) @@ -265,7 +265,7 @@ class ScopeTest { fun `clear scope resets scope to default state`() { val scope = Scope(SentryOptions()) scope.level = SentryLevel.WARNING - scope.setTransaction(SentryTracer(TransactionContext("", "op"), NoOpHub.getInstance())) + scope.setTransaction(SentryTracer(TransactionContext("", "op"), NoOpScopes.getInstance())) scope.user = User() scope.request = Request() scope.fingerprint = mutableListOf("finger") @@ -822,7 +822,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the transaction if there is no active span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction assertEquals(transaction, scope.span) } @@ -830,7 +830,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the current span if there is an unfinished span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") assertEquals(span, scope.span) @@ -839,7 +839,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the current span if there is a finished span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") span.finish() @@ -849,7 +849,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the latest span if there is a list of active span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") val innerSpan = span.startChild("op") @@ -859,7 +859,7 @@ class ScopeTest { @Test fun `Scope setTransaction sets transaction name`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction scope.setTransaction("new-name") assertNotNull(scope.transaction) { @@ -871,7 +871,7 @@ class ScopeTest { @Test fun `Scope setTransaction with null does not clear transaction`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction scope.callMethod("setTransaction", String::class.java, null) assertNotNull(scope.transaction) @@ -936,7 +936,7 @@ class ScopeTest { fun `when transaction is started, sets transaction name on the transaction object`() { val scope = Scope(SentryOptions()) val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.transaction = sentryTransaction assertEquals("transaction-name", scope.transactionName) scope.setTransaction("new-name") @@ -950,7 +950,7 @@ class ScopeTest { val scope = Scope(SentryOptions()) scope.setTransaction("transaction-a") val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.setTransaction(sentryTransaction) assertEquals("transaction-name", scope.transactionName) scope.clearTransaction() @@ -961,7 +961,7 @@ class ScopeTest { fun `withTransaction returns the current Transaction bound to the Scope`() { val scope = Scope(SentryOptions()) val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.setTransaction(sentryTransaction) scope.withTransaction { diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt new file mode 100644 index 0000000000..85a0b6ef75 --- /dev/null +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -0,0 +1,265 @@ +package io.sentry + +import io.sentry.protocol.SentryTransaction +import io.sentry.protocol.User +import org.mockito.kotlin.any +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.reset +import org.mockito.kotlin.verify +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +class ScopesAdapterTest { + + val scopes: IScopes = mock() + + @BeforeTest + fun `set up`() { + Sentry.setCurrentScopes(scopes) + } + + @AfterTest + fun shutdown() { + Sentry.close() + } + + @Test fun `isEnabled calls Hub`() { + ScopesAdapter.getInstance().isEnabled + verify(scopes).isEnabled + } + + @Test fun `captureEvent calls Hub`() { + val event = mock() + val hint = mock() + val scopeCallback = mock() + ScopesAdapter.getInstance().captureEvent(event, hint) + verify(scopes).captureEvent(eq(event), eq(hint)) + + ScopesAdapter.getInstance().captureEvent(event, hint, scopeCallback) + verify(scopes).captureEvent(eq(event), eq(hint), eq(scopeCallback)) + } + + @Test fun `captureMessage calls Hub`() { + val scopeCallback = mock() + val sentryLevel = mock() + ScopesAdapter.getInstance().captureMessage("message", sentryLevel) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel)) + + ScopesAdapter.getInstance().captureMessage("message", sentryLevel, scopeCallback) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) + } + + @Test fun `captureEnvelope calls Hub`() { + val envelope = mock() + val hint = mock() + ScopesAdapter.getInstance().captureEnvelope(envelope, hint) + verify(scopes).captureEnvelope(eq(envelope), eq(hint)) + } + + @Test fun `captureException calls Hub`() { + val throwable = mock() + val hint = mock() + val scopeCallback = mock() + ScopesAdapter.getInstance().captureException(throwable, hint) + verify(scopes).captureException(eq(throwable), eq(hint)) + + ScopesAdapter.getInstance().captureException(throwable, hint, scopeCallback) + verify(scopes).captureException(eq(throwable), eq(hint), eq(scopeCallback)) + } + + @Test fun `captureUserFeedback calls Hub`() { + val userFeedback = mock() + ScopesAdapter.getInstance().captureUserFeedback(userFeedback) + verify(scopes).captureUserFeedback(eq(userFeedback)) + } + + @Test fun `captureCheckIn calls Hub`() { + val checkIn = mock() + ScopesAdapter.getInstance().captureCheckIn(checkIn) + verify(scopes).captureCheckIn(eq(checkIn)) + } + + @Test fun `startSession calls Hub`() { + ScopesAdapter.getInstance().startSession() + verify(scopes).startSession() + } + + @Test fun `endSession calls Hub`() { + ScopesAdapter.getInstance().endSession() + verify(scopes).endSession() + } + + @Test fun `close calls Hub`() { + ScopesAdapter.getInstance().close() + verify(scopes).close(false) + } + + @Test fun `close with isRestarting true calls Hub with isRestarting false`() { + ScopesAdapter.getInstance().close(true) + verify(scopes).close(false) + } + + @Test fun `close with isRestarting false calls Hub with isRestarting false`() { + ScopesAdapter.getInstance().close(false) + verify(scopes).close(false) + } + + @Test fun `addBreadcrumb calls Hub`() { + val breadcrumb = mock() + val hint = mock() + ScopesAdapter.getInstance().addBreadcrumb(breadcrumb, hint) + verify(scopes).addBreadcrumb(eq(breadcrumb), eq(hint)) + } + + @Test fun `setLevel calls Hub`() { + val sentryLevel = mock() + ScopesAdapter.getInstance().setLevel(sentryLevel) + verify(scopes).setLevel(eq(sentryLevel)) + } + + @Test fun `setTransaction calls Hub`() { + ScopesAdapter.getInstance().setTransaction("transaction") + verify(scopes).setTransaction(eq("transaction")) + } + + @Test fun `setUser calls Hub`() { + val user = mock() + ScopesAdapter.getInstance().setUser(user) + verify(scopes).setUser(eq(user)) + } + + @Test fun `setFingerprint calls Hub`() { + val fingerprint = ArrayList() + ScopesAdapter.getInstance().setFingerprint(fingerprint) + verify(scopes).setFingerprint(eq(fingerprint)) + } + + @Test fun `clearBreadcrumbs calls Hub`() { + ScopesAdapter.getInstance().clearBreadcrumbs() + verify(scopes).clearBreadcrumbs() + } + + @Test fun `setTag calls Hub`() { + ScopesAdapter.getInstance().setTag("key", "value") + verify(scopes).setTag(eq("key"), eq("value")) + } + + @Test fun `removeTag calls Hub`() { + ScopesAdapter.getInstance().removeTag("key") + verify(scopes).removeTag(eq("key")) + } + + @Test fun `setExtra calls Hub`() { + ScopesAdapter.getInstance().setExtra("key", "value") + verify(scopes).setExtra(eq("key"), eq("value")) + } + + @Test fun `removeExtra calls Hub`() { + ScopesAdapter.getInstance().removeExtra("key") + verify(scopes).removeExtra(eq("key")) + } + + @Test fun `getLastEventId calls Hub`() { + ScopesAdapter.getInstance().lastEventId + verify(scopes).lastEventId + } + + @Test fun `pushScope calls Hub`() { + ScopesAdapter.getInstance().pushScope() + verify(scopes).pushScope() + } + + @Test fun `popScope calls Hub`() { + ScopesAdapter.getInstance().popScope() + verify(scopes).popScope() + } + + @Test fun `withScope calls Hub`() { + val scopeCallback = mock() + ScopesAdapter.getInstance().withScope(scopeCallback) + verify(scopes).withScope(eq(scopeCallback)) + } + + @Test fun `configureScope calls Hub`() { + val scopeCallback = mock() + ScopesAdapter.getInstance().configureScope(scopeCallback) + verify(scopes).configureScope(eq(scopeCallback)) + } + + @Test fun `bindClient calls Hub`() { + val client = mock() + ScopesAdapter.getInstance().bindClient(client) + verify(scopes).bindClient(eq(client)) + } + + @Test fun `flush calls Hub`() { + ScopesAdapter.getInstance().flush(1) + verify(scopes).flush(eq(1)) + } + + @Test fun `clone calls Hub`() { + ScopesAdapter.getInstance().clone() + verify(scopes).clone() + } + + @Test fun `captureTransaction calls Hub`() { + val transaction = mock() + val traceContext = mock() + val hint = mock() + val profilingTraceData = mock() + ScopesAdapter.getInstance().captureTransaction(transaction, traceContext, hint, profilingTraceData) + verify(scopes).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) + } + + @Test fun `startTransaction calls Hub`() { + val transactionContext = mock() + val samplingContext = mock() + val transactionOptions = mock() + ScopesAdapter.getInstance().startTransaction(transactionContext) + verify(scopes).startTransaction(eq(transactionContext), any()) + + reset(scopes) + + ScopesAdapter.getInstance().startTransaction(transactionContext, transactionOptions) + verify(scopes).startTransaction(eq(transactionContext), eq(transactionOptions)) + } + + @Test fun `traceHeaders calls Hub`() { + ScopesAdapter.getInstance().traceHeaders() + verify(scopes).traceHeaders() + } + + @Test fun `setSpanContext calls Hub`() { + val throwable = mock() + val span = mock() + ScopesAdapter.getInstance().setSpanContext(throwable, span, "transactionName") + verify(scopes).setSpanContext(eq(throwable), eq(span), eq("transactionName")) + } + + @Test fun `getSpan calls Hub`() { + ScopesAdapter.getInstance().span + verify(scopes).span + } + + @Test fun `getTransaction calls Hub`() { + ScopesAdapter.getInstance().transaction + verify(scopes).transaction + } + + @Test fun `getOptions calls Hub`() { + ScopesAdapter.getInstance().options + verify(scopes).options + } + + @Test fun `isCrashedLastRun calls Hub`() { + ScopesAdapter.getInstance().isCrashedLastRun + verify(scopes).isCrashedLastRun + } + + @Test fun `reportFullyDisplayed calls Hub`() { + ScopesAdapter.getInstance().reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() + } +} diff --git a/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt b/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt index 78623f90a7..beed31a198 100644 --- a/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt @@ -19,7 +19,7 @@ import kotlin.test.assertTrue class SendCachedEnvelopeFireAndForgetIntegrationTest { private class Fixture { - var hub: IHub = mock() + var scopes: IScopes = mock() var logger: ILogger = mock() var options = SentryOptions() val sender = mock() @@ -45,7 +45,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fun `when cacheDirPath returns null, register logs and exit`() { fixture.options.cacheDirPath = null val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("No cache dir path is defined in options.")) verify(fixture.sender, never()).send() } @@ -73,7 +73,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { val sut = SendCachedEnvelopeFireAndForgetIntegration(CustomFactory()) fixture.options.cacheDirPath = "abc" fixture.options.executorService = ImmediateExecutorService() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("SendFireAndForget factory is null.")) verify(fixture.sender, never()).send() } @@ -85,7 +85,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { mock() ) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(fixture.options.sdkVersion) assert(fixture.options.sdkVersion!!.integrationSet.contains("SendCachedEnvelopeFireAndForget")) } @@ -96,7 +96,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService.close(0) whenever(fixture.callback.create(any(), any())).thenReturn(mock()) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("Failed to call the executor. Cached events will not be sent. Did you call Sentry.close()?"), any()) } @@ -108,7 +108,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(connectionStatusProvider).addConnectionStatusObserver(any()) } @@ -122,9 +122,9 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() } @@ -139,7 +139,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender).send() } @@ -155,7 +155,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // when there's no connection no factory create call should be done verify(fixture.sender, never()).send() @@ -183,9 +183,9 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(fixture.hub.rateLimiter).thenReturn(rateLimiter) + whenever(fixture.scopes.rateLimiter).thenReturn(rateLimiter) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // no factory call should be done if there's rate limiting active verify(fixture.sender, never()).send() @@ -196,8 +196,8 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = ImmediateExecutorService() fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) - verify(fixture.callback).create(eq(fixture.hub), eq(fixture.options)) + sut.register(fixture.scopes, fixture.options) + verify(fixture.callback).create(eq(fixture.scopes), eq(fixture.options)) } @Test @@ -205,7 +205,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = mock() fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.callback, never()).create(any(), any()) } @@ -215,7 +215,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = deferredExecutorService val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() sut.close() @@ -224,7 +224,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { } private class CustomFactory : SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory { - override fun create(hub: IHub, options: SentryOptions): SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget? { + override fun create(scopes: IScopes, options: SentryOptions): SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget? { return null } } diff --git a/sentry/src/test/java/io/sentry/SentryClientTest.kt b/sentry/src/test/java/io/sentry/SentryClientTest.kt index de540bf90c..eac2b0be29 100644 --- a/sentry/src/test/java/io/sentry/SentryClientTest.kt +++ b/sentry/src/test/java/io/sentry/SentryClientTest.kt @@ -70,7 +70,7 @@ class SentryClientTest { var transport = mock() var factory = mock() val maxAttachmentSize: Long = (5 * 1024 * 1024).toLong() - val hub = mock() + val scopes = mock() val sentryTracer: SentryTracer var sentryOptions: SentryOptions = SentryOptions().apply { @@ -88,8 +88,8 @@ class SentryClientTest { init { whenever(factory.create(any(), any())).thenReturn(transport) - whenever(hub.options).thenReturn(sentryOptions) - sentryTracer = SentryTracer(TransactionContext("a-transaction", "op"), hub) + whenever(scopes.options).thenReturn(sentryOptions) + sentryTracer = SentryTracer(TransactionContext("a-transaction", "op"), scopes) } var attachment = Attachment("hello".toByteArray(), "hello.txt", "text/plain", true) @@ -1456,9 +1456,9 @@ class SentryClientTest { @Test fun `when captureTransaction with scope, transaction should use user data`() { - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("tx", "op"), hub)) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) + val transaction = SentryTransaction(SentryTracer(TransactionContext("tx", "op"), scopes)) val scope = createScope() val sut = fixture.getSut() @@ -1487,7 +1487,7 @@ class SentryClientTest { val event = SentryEvent() val sut = fixture.getSut() val scope = createScope() - val transaction = SentryTracer(TransactionContext("a-transaction", "op"), fixture.hub) + val transaction = SentryTracer(TransactionContext("a-transaction", "op"), fixture.scopes) scope.setTransaction(transaction) val span = transaction.startChild("op") sut.captureEvent(event, scope) @@ -1558,7 +1558,7 @@ class SentryClientTest { fixture.sentryOptions.release = "optionsRelease" fixture.sentryOptions.environment = "optionsEnvironment" val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.release = "transactionRelease" transaction.environment = "transactionEnvironment" @@ -1571,7 +1571,7 @@ class SentryClientTest { fun `when transaction does not have SDK version set, and the SDK version is set on options, options values are applied to transactions`() { fixture.sentryOptions.sdkVersion = SdkVersion("sdk.name", "version") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals(fixture.sentryOptions.sdkVersion, transaction.sdk) @@ -1581,7 +1581,7 @@ class SentryClientTest { fun `when transaction has SDK version set, and the SDK version is set on options, options values are not applied to transactions`() { fixture.sentryOptions.sdkVersion = SdkVersion("sdk.name", "version") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) val sdkVersion = SdkVersion("transaction.sdk.name", "version") transaction.sdk = sdkVersion @@ -1593,7 +1593,7 @@ class SentryClientTest { fun `when transaction does not have tags, and tags are set on options, options values are applied to transactions`() { fixture.sentryOptions.setTag("tag1", "value1") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals(mapOf("tag1" to "value1"), transaction.tags) @@ -1604,7 +1604,7 @@ class SentryClientTest { fixture.sentryOptions.setTag("tag1", "value1") fixture.sentryOptions.setTag("tag2", "value2") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.setTag("tag3", "value3") transaction.setTag("tag2", "transaction-tag") @@ -1618,7 +1618,7 @@ class SentryClientTest { @Test fun `captured transactions without a platform, have the default platform set`() { val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals("java", transaction.platform) @@ -1627,7 +1627,7 @@ class SentryClientTest { @Test fun `captured transactions with a platform, do not get the platform overwritten`() { val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.platform = "abc" sut.captureTransaction(transaction, sentryTracer.traceContext()) diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 0f4966b44a..70728d2900 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -63,27 +63,27 @@ class SentryTest { } @Test - fun `init multiple times calls hub close with isRestarting true`() { - val hub = mock() + fun `init multiple times calls scopes close with isRestarting true`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.init { it.dsn = dsn } - verify(hub).close(eq(true)) + verify(scopes).close(eq(true)) } @Test - fun `close calls hub close with isRestarting false`() { - val hub = mock() + fun `close calls scopes close with isRestarting false`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.close() - verify(hub).close(eq(false)) + verify(scopes).close(eq(false)) } @Test @@ -213,7 +213,7 @@ class SentryTest { Sentry.init { it.isEnableExternalConfiguration = true } - assertTrue(HubAdapter.getInstance().isEnabled) + assertTrue(ScopesAdapter.getInstance().isEnabled) } finally { temporaryFolder.delete() } @@ -229,10 +229,10 @@ class SentryTest { Sentry.setTag("none", "shouldNotExist") var value: String? = null - Sentry.getCurrentHub().configureScope { + Sentry.getCurrentScopes().configureScope { value = it.tags[value] } - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) assertNull(value) } @@ -245,10 +245,10 @@ class SentryTest { Sentry.setTag("none", "shouldNotExist") var value: String? = null - Sentry.getCurrentHub().configureScope { + Sentry.getCurrentScopes().configureScope { value = it.tags[value] } - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) assertNull(value) } @@ -267,7 +267,7 @@ class SentryTest { Sentry.init { it.dsn = dsn } val client = mock() - Sentry.getCurrentHub().bindClient(client) + Sentry.getCurrentScopes().bindClient(client) val userFeedback = UserFeedback(SentryId.EMPTY_ID) Sentry.captureUserFeedback(userFeedback) @@ -369,11 +369,11 @@ class SentryTest { } @Test - fun `using sentry before calling init creates NoOpHub but after init Sentry uses a new clone`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `using sentry before calling init creates NoOpScopes but after init Sentry uses a new clone`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.captureMessage("noop caused") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) // init Sentry in another thread val thread = Thread() { @@ -387,18 +387,18 @@ class SentryTest { Sentry.captureMessage("should work now") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(scopes.isNoOp) } @Test - fun `main hub can be cloned and does not share scope with current hub`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `main scopes can be cloned and does not share scope with current scopes`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.addBreadcrumb("breadcrumbNoOp") Sentry.captureMessage("messageNoOp") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) val capturedEvents = mutableListOf() @@ -418,14 +418,14 @@ class SentryTest { Sentry.addBreadcrumb("breadcrumbCurrent") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(Sentry.getCurrentScopes().isNoOp) val newMainHubClone = Sentry.cloneMainHub() newMainHubClone.addBreadcrumb("breadcrumbMainClone") - hub.captureMessage("messageCurrent") + scopes.captureMessage("messageCurrent") newMainHubClone.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) @@ -444,12 +444,12 @@ class SentryTest { } @Test - fun `main hub is not cloned in global hub mode and shares scope with current hub`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `main scopes is not cloned in global scopes mode and shares scope with current scopes`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.addBreadcrumb("breadcrumbNoOp") Sentry.captureMessage("messageNoOp") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) val capturedEvents = mutableListOf() @@ -469,14 +469,14 @@ class SentryTest { Sentry.addBreadcrumb("breadcrumbCurrent") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(scopes.isNoOp) val newMainHubClone = Sentry.cloneMainHub() newMainHubClone.addBreadcrumb("breadcrumbMainClone") - hub.captureMessage("messageCurrent") + scopes.captureMessage("messageCurrent") newMainHubClone.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) @@ -669,25 +669,25 @@ class SentryTest { } @Test - fun `reportFullyDisplayed calls hub reportFullyDisplayed`() { - val hub = mock() + fun `reportFullyDisplayed calls scopes reportFullyDisplayed`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.reportFullyDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } @Test fun `reportFullDisplayed calls reportFullyDisplayed`() { - val hub = mock() + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.reportFullDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } @Test @@ -806,11 +806,11 @@ class SentryTest { it.serializer.deserialize(previousSessionFile.bufferedReader(), Session::class.java)!!.environment ) - it.addIntegration { hub, _ -> + it.addIntegration { scopes, _ -> // this is just a hack to trigger the previousSessionFlush latch, so the finalizer // does not time out waiting. We have to do it as integration, because this is where - // the hub is already initialized - hub.startSession() + // the scopes is already initialized + scopes.startSession() } } @@ -861,7 +861,7 @@ class SentryTest { Sentry.init { it.dsn = dsn } val client = mock() - Sentry.getCurrentHub().bindClient(client) + Sentry.getCurrentScopes().bindClient(client) val checkIn = CheckIn("some_slug", CheckInStatus.OK) Sentry.captureCheckIn(checkIn) @@ -910,18 +910,18 @@ class SentryTest { } @Test - fun `getSpan calls hub getSpan`() { - val hub = mock() + fun `getSpan calls scopes getSpan`() { + val scopes = mock() Sentry.init({ it.dsn = dsn }, false) - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.getSpan() - verify(hub).span + verify(scopes).span } @Test - fun `getSpan calls returns root span if globalhub mode is enabled on Android`() { + fun `getSpan calls returns root span if globalscopes mode is enabled on Android`() { PlatformTestManipulator.pretendIsAndroid(true) Sentry.init({ it.dsn = dsn @@ -938,7 +938,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalhub mode is enabled, but the platform is not Android`() { + fun `getSpan calls returns child span if globalscopes mode is enabled, but the platform is not Android`() { PlatformTestManipulator.pretendIsAndroid(false) Sentry.init({ it.dsn = dsn @@ -954,7 +954,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalhub mode is disabled`() { + fun `getSpan calls returns child span if globalscopes mode is disabled`() { Sentry.init({ it.dsn = dsn it.enableTracing = true @@ -1140,15 +1140,15 @@ class SentryTest { } @Test - fun `metrics calls hub getMetrics`() { - val hub = mock() + fun `metrics calls scopes getMetrics`() { + val scopes = mock() Sentry.init({ it.dsn = dsn }, false) - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.metrics() - verify(hub).metrics() + verify(scopes).metrics() } private class InMemoryOptionsObserver : IOptionsObserver { diff --git a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt index 7f6d449eac..2fb9b38566 100644 --- a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt +++ b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt @@ -35,20 +35,20 @@ class SentryWrapperTest { } } - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = CompletableFuture.supplyAsync( SentryWrapper.wrapSupplier { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) "Result 1" }, executor @@ -57,8 +57,8 @@ class SentryWrapperTest { callableFuture.join() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } @@ -169,20 +169,20 @@ class SentryWrapperTest { it.dsn = dsn } - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( SentryWrapper.wrapCallable { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) "Result 1" } ) @@ -190,8 +190,8 @@ class SentryWrapperTest { callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt index d6ce0ff043..428a34635f 100644 --- a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt @@ -16,7 +16,7 @@ class ShutdownHookIntegrationTest { private class Fixture { val runtime = mock() val options = SentryOptions() - val hub = mock() + val scopes = mock() fun getSut(): ShutdownHookIntegration { return ShutdownHookIntegration(runtime) @@ -29,7 +29,7 @@ class ShutdownHookIntegrationTest { fun `registration attaches shutdown hook to runtime`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.runtime).addShutdownHook(any()) } @@ -39,7 +39,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() fixture.options.isEnableShutdownHook = false - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.runtime, never()).addShutdownHook(any()) } @@ -48,7 +48,7 @@ class ShutdownHookIntegrationTest { fun `registration removes shutdown hook from runtime`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) integration.close() verify(fixture.runtime).removeShutdownHook(any()) @@ -58,13 +58,13 @@ class ShutdownHookIntegrationTest { fun `hook calls flush`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertNotNull(integration.hook) { it.start() it.join() } - verify(fixture.hub).flush(any()) + verify(fixture.scopes).flush(any()) } @Test @@ -72,13 +72,13 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() fixture.options.flushTimeoutMillis = 10000 - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertNotNull(integration.hook) { it.start() it.join() } - verify(fixture.hub).flush(eq(10000)) + verify(fixture.scopes).flush(eq(10000)) } @Test @@ -86,7 +86,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException("Shutdown in progress")) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) integration.close() verify(fixture.runtime).removeShutdownHook(any()) @@ -97,7 +97,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException()) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertFails { integration.close() @@ -110,7 +110,7 @@ class ShutdownHookIntegrationTest { fun `Integration adds itself to integration list`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.integrationSet.contains("ShutdownHook") diff --git a/sentry/src/test/java/io/sentry/SpanTest.kt b/sentry/src/test/java/io/sentry/SpanTest.kt index ae9d9bd07f..fd36c31933 100644 --- a/sentry/src/test/java/io/sentry/SpanTest.kt +++ b/sentry/src/test/java/io/sentry/SpanTest.kt @@ -21,10 +21,10 @@ import kotlin.test.assertTrue class SpanTest { private class Fixture { - val hub = mock() + val scopes = mock() init { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" isTraceSampling = true @@ -36,9 +36,9 @@ class SpanTest { return Span( SentryId(), SpanId(), - SentryTracer(TransactionContext("name", "op"), hub), + SentryTracer(TransactionContext("name", "op"), scopes), "op", - hub, + scopes, null, options, null @@ -46,7 +46,7 @@ class SpanTest { } fun getRootSut(options: TransactionOptions = TransactionOptions()): Span { - return SentryTracer(TransactionContext("name", "op"), hub, options).root + return SentryTracer(TransactionContext("name", "op"), scopes, options).root } } @@ -106,10 +106,10 @@ class SpanTest { parentSpanId, SentryTracer( TransactionContext("name", "op", TracesSamplingDecision(true)), - fixture.hub + fixture.scopes ), "op", - fixture.hub + fixture.scopes ) val sentryTrace = span.toSentryTrace() @@ -163,17 +163,17 @@ class SpanTest { } @Test - fun `when span has throwable set set, it assigns itself to throwable on the Hub`() { + fun `when span has throwable set set, it assigns itself to throwable on the Scopes`() { val transaction = SentryTracer( TransactionContext("name", "op"), - fixture.hub + fixture.scopes ) val span = transaction.startChild("op") val ex = RuntimeException() span.throwable = ex span.finish() - verify(fixture.hub).setSpanContext(ex, span, "name") + verify(fixture.scopes).setSpanContext(ex, span, "name") } @Test @@ -188,7 +188,7 @@ class SpanTest { span.finish(SpanStatus.UNKNOWN_ERROR) // call only once - verify(fixture.hub).setSpanContext(any(), any(), any()) + verify(fixture.scopes).setSpanContext(any(), any(), any()) assertEquals(SpanStatus.OK, span.status) assertEquals(timestamp, span.finishDate) } @@ -496,7 +496,7 @@ class SpanTest { } private fun getTransaction(transactionContext: TransactionContext = TransactionContext("name", "op")): SentryTracer { - return SentryTracer(transactionContext, fixture.hub) + return SentryTracer(transactionContext, fixture.scopes) } private fun startChildFromSpan(): Span { diff --git a/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt b/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt index e79e5ebf8c..0847c9448f 100644 --- a/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt +++ b/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt @@ -54,10 +54,10 @@ class TraceContextSerializationTest { private fun createTraceContext(sRate: Double): TraceContext { val baggage = Baggage(fixture.logger) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) baggage.setValuesFromTransaction( - SentryTracer(TransactionContext("name", "op"), hub), + SentryTracer(TransactionContext("name", "op"), scopes), User().apply { id = "user-id" others = mapOf("segment" to "pro") diff --git a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt index 01353d5ac0..ec366e1901 100644 --- a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt @@ -30,7 +30,7 @@ class UncaughtExceptionHandlerIntegrationTest { val defaultHandler = mock() val thread = mock() val throwable = Throwable("test") - val hub = mock() + val scopes = mock() val options = SentryOptions() val logger = mock() @@ -63,17 +63,17 @@ class UncaughtExceptionHandlerIntegrationTest { fun `when uncaughtException is called, sentry captures exception`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test fun `when register is called, current handler is not lost`() { val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) verify(fixture.defaultHandler).uncaughtException(fixture.thread, fixture.throwable) @@ -81,7 +81,7 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when uncaughtException is called, exception captured has handled=false`() { - whenever(fixture.hub.captureException(any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureException(any())).thenAnswer { invocation -> val e = invocation.getArgument(1) assertNotNull(e) assertNotNull(e.exceptionMechanism) @@ -91,22 +91,22 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test - fun `when hub is closed, integrations should be closed`() { + fun `when scopes is closed, integrations should be closed`() { val integrationMock = mock() val options = SentryOptions() options.dsn = "https://key@sentry.io/proj" options.addIntegration(integrationMock) options.cacheDirPath = fixture.file.absolutePath options.setSerializer(mock()) - val hub = Hub(options) - hub.close() + val scopes = Hub(options) + scopes.close() verify(integrationMock).close() } @@ -117,7 +117,7 @@ class UncaughtExceptionHandlerIntegrationTest { isPrintUncaughtStackTrace = false ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.handler, never()).defaultUncaughtExceptionHandler = any() } @@ -126,7 +126,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is enabled, should install Sentry UncaughtExceptionHandler`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.handler).defaultUncaughtExceptionHandler = argWhere { it is UncaughtExceptionHandlerIntegration } @@ -136,7 +136,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is set and integration is closed, default uncaught exception handler is reset to previous handler`() { val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) whenever(fixture.handler.defaultUncaughtExceptionHandler) .thenReturn(sut) sut.close() @@ -148,7 +148,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is not set and integration is closed, default uncaught exception handler is reset to null`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) whenever(fixture.handler.defaultUncaughtExceptionHandler) .thenReturn(sut) sut.close() @@ -165,7 +165,7 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(isPrintUncaughtStackTrace = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, RuntimeException("This should be printed!")) assertTrue( @@ -185,7 +185,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `waits for event to flush on disk`() { val capturedEventId = SentryId() - whenever(fixture.hub.captureEvent(any(), any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureEvent(any(), any())).thenAnswer { invocation -> val hint = HintUtils.getSentrySdkHint(invocation.getArgument(1)) as DiskFlushNotification thread { @@ -197,10 +197,10 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(flushTimeoutMillis = 5000) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // shouldn't fall into timed out state, because we marked event as flushed on another thread verify(fixture.logger, never()).log( any(), @@ -211,14 +211,14 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `does not block flushing when the event was dropped`() { - whenever(fixture.hub.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) + whenever(fixture.scopes.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // we do not call markFlushed, hence it should time out waiting for flush, but because // we drop the event, it should not even come to this if-check verify(fixture.logger, never()).log( @@ -231,17 +231,17 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `waits for event to flush on disk if it was dropped by multithreaded deduplicator`() { val hintCaptor = argumentCaptor() - whenever(fixture.hub.captureEvent(any(), hintCaptor.capture())).thenAnswer { + whenever(fixture.scopes.captureEvent(any(), hintCaptor.capture())).thenAnswer { HintUtils.setEventDropReason(hintCaptor.firstValue, MULTITHREADED_DEDUPLICATION) return@thenAnswer SentryId.EMPTY_ID } val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // we do not call markFlushed, even though we dropped the event, the reason was // MULTITHREADED_DEDUPLICATION, so it should time out verify(fixture.logger).log( @@ -254,15 +254,15 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when there is no active transaction on scope, sets current event id as flushable`() { val eventCaptor = argumentCaptor() - whenever(fixture.hub.captureEvent(eventCaptor.capture(), any())) + whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any())) .thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { (HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint) @@ -274,16 +274,16 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when there is active transaction on scope, does not set current event id as flushable`() { val eventCaptor = argumentCaptor() - whenever(fixture.hub.transaction).thenReturn(mock()) - whenever(fixture.hub.captureEvent(eventCaptor.capture(), any())) + whenever(fixture.scopes.transaction).thenReturn(mock()) + whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any())) .thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { !(HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint) diff --git a/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt b/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt index c010c97238..cf234574bf 100644 --- a/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt +++ b/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt @@ -1,6 +1,6 @@ package io.sentry.backpressure -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryOptions import io.sentry.backpressure.BackpressureMonitor.MAX_DOWNSAMPLE_FACTOR @@ -17,13 +17,13 @@ class BackpressureMonitorTest { class Fixture { val options = SentryOptions() - val hub = mock() + val scopes = mock() val executor = mock() fun getSut(): BackpressureMonitor { options.executorService = executor whenever(executor.isClosed).thenReturn(false) whenever(executor.schedule(any(), any())).thenReturn(mock>()) - return BackpressureMonitor(options, hub) + return BackpressureMonitor(options, scopes) } } @@ -38,7 +38,7 @@ class BackpressureMonitorTest { @Test fun `downsampleFactor increases with negative health checks up to max`() { val sut = fixture.getSut() - whenever(fixture.hub.isHealthy).thenReturn(false) + whenever(fixture.scopes.isHealthy).thenReturn(false) assertEquals(0, sut.downsampleFactor) (1..MAX_DOWNSAMPLE_FACTOR).forEach { i -> @@ -54,13 +54,13 @@ class BackpressureMonitorTest { @Test fun `downsampleFactor goes back to 0 after positive health check`() { val sut = fixture.getSut() - whenever(fixture.hub.isHealthy).thenReturn(false) + whenever(fixture.scopes.isHealthy).thenReturn(false) assertEquals(0, sut.downsampleFactor) sut.checkHealth() assertEquals(1, sut.downsampleFactor) - whenever(fixture.hub.isHealthy).thenReturn(true) + whenever(fixture.scopes.isHealthy).thenReturn(true) sut.checkHealth() assertEquals(0, sut.downsampleFactor) } diff --git a/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt b/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt index b4615cac76..d27e5c0228 100644 --- a/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt +++ b/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt @@ -7,7 +7,7 @@ import io.sentry.DataCategory import io.sentry.DateUtils import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.NoOpLogger import io.sentry.ProfilingTraceData import io.sentry.Sentry @@ -47,9 +47,9 @@ class ClientReportTest { @Test fun `lost envelope can be recorded`() { givenClientReportRecorder() - val hub = mock() - whenever(hub.options).thenReturn(opts) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val scopes = mock() + whenever(scopes.options).thenReturn(opts) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val lostClientReport = ClientReport( DateUtils.getCurrentDateTime(), diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt index 00c89f27be..f6271b5b5e 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.util.PlatformTestManipulator @@ -20,25 +20,25 @@ class FileIOSpanManagerTest { @Test fun `startSpan uses transaction on Android platform`() { - val hub = mock() + val scopes = mock() val transaction = mock() - whenever(hub.transaction).thenReturn(transaction) + whenever(scopes.transaction).thenReturn(transaction) PlatformTestManipulator.pretendIsAndroid(true) - FileIOSpanManager.startSpan(hub, "op.read") + FileIOSpanManager.startSpan(scopes, "op.read") verify(transaction).startChild(any()) } @Test fun `startSpan uses last span on non-Android platforms`() { - val hub = mock() + val scopes = mock() val span = mock() - whenever(hub.span).thenReturn(span) + whenever(scopes.span).thenReturn(span) PlatformTestManipulator.pretendIsAndroid(false) - FileIOSpanManager.startSpan(hub, "op.read") + FileIOSpanManager.startSpan(scopes, "op.read") verify(span).startChild(any()) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt index 5e27eb451d..063b6d6428 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class SentryFileInputStreamTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer private val options = SentryOptions() @@ -40,21 +40,21 @@ class SentryFileInputStreamTest { sendDefaultPii: Boolean = false ): SentryFileInputStream { tmpFile?.writeText("Text") - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( options.apply { isSendDefaultPii = sendDefaultPii mainThreadChecker = MainThreadChecker.getInstance() addInAppInclude("org.junit") } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return if (fileDescriptor == null) { - SentryFileInputStream(tmpFile, hub) + SentryFileInputStream(tmpFile, scopes) } else { - SentryFileInputStream(fileDescriptor, hub) + SentryFileInputStream(fileDescriptor, scopes) } } @@ -62,13 +62,13 @@ class SentryFileInputStreamTest { tmpFile: File? = null, delegate: FileInputStream ): SentryFileInputStream { - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) + whenever(scopes.span).thenReturn(sentryTracer) return SentryFileInputStream.Factory.create( delegate, tmpFile, - hub + scopes ) as SentryFileInputStream } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt index f6a09830c2..8b175adc2d 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -24,7 +24,7 @@ import kotlin.test.assertTrue class SentryFileOutputStreamTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -32,17 +32,17 @@ class SentryFileOutputStreamTest { activeTransaction: Boolean = true, append: Boolean = false ): SentryFileOutputStream { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() addInAppInclude("org.junit") } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileOutputStream(tmpFile, append, hub) + return SentryFileOutputStream(tmpFile, append, scopes) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt index 2485579e7a..38781d718b 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -17,7 +17,7 @@ import kotlin.test.assertEquals class SentryFileReaderTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -25,16 +25,16 @@ class SentryFileReaderTest { activeTransaction: Boolean = true ): SentryFileReader { tmpFile.writeText("TEXT") - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileReader(tmpFile, hub) + return SentryFileReader(tmpFile, scopes) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt index f0738d8725..8f3d96b4d7 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -17,7 +17,7 @@ import kotlin.test.assertEquals class SentryFileWriterTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -25,16 +25,16 @@ class SentryFileWriterTest { activeTransaction: Boolean = true, append: Boolean = false ): SentryFileWriter { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileWriter(tmpFile, append, hub) + return SentryFileWriter(tmpFile, append, scopes) } } diff --git a/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt b/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt index 73aa339f26..0a91bec562 100644 --- a/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.internal -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryOptions.BeforeEnvelopeCallback import io.sentry.SpotlightIntegration @@ -19,7 +19,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertNull(options.beforeEnvelopeCallback) } @@ -33,7 +33,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals(envelopeCallback, options.beforeEnvelopeCallback) } @@ -45,7 +45,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals(options.beforeEnvelopeCallback, spotlight) spotlight.close() @@ -71,7 +71,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals("http://example.com:1234/stream", spotlight.spotlightConnectionUrl) } diff --git a/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt b/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt index 27499be0a0..d67b0186be 100644 --- a/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt +++ b/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt @@ -1,6 +1,6 @@ package io.sentry.protocol -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLongDate import io.sentry.SentryTracer import io.sentry.Span @@ -19,7 +19,7 @@ class SentrySpanTest { val span = Span( TransactionContext("name", "op"), mock(), - mock(), + mock(), SentryLongDate(1000000), SpanOptions() ) diff --git a/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt b/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt index d9e217b0ac..2b1be98611 100644 --- a/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt +++ b/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt @@ -4,7 +4,7 @@ import io.sentry.Attachment import io.sentry.CheckIn import io.sentry.CheckInStatus import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISerializer import io.sentry.NoOpLogger import io.sentry.ProfilingTraceData @@ -80,10 +80,10 @@ class RateLimiterTest { fun `parse X-Sentry-Rate-Limit and set its values and retry after should be true`() { val rateLimiter = fixture.getSUT() whenever(fixture.currentDateProvider.currentTimeMillis).thenReturn(0) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), hub)) + val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), scopes)) val transactionItem = SentryEnvelopeItem.fromEvent(fixture.serializer, transaction) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, transactionItem)) @@ -97,10 +97,10 @@ class RateLimiterTest { fun `parse X-Sentry-Rate-Limit and set its values and retry after should be false`() { val rateLimiter = fixture.getSUT() whenever(fixture.currentDateProvider.currentTimeMillis).thenReturn(0, 0, 1001) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), hub)) + val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), scopes)) val transactionItem = SentryEnvelopeItem.fromEvent(fixture.serializer, transaction) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, transactionItem)) @@ -191,9 +191,9 @@ class RateLimiterTest { it.setName("John Me") } ) - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val sessionItem = SentryEnvelopeItem.fromSession(fixture.serializer, Session("123", User(), "env", "release")) val attachmentItem = SentryEnvelopeItem.fromAttachment(fixture.serializer, NoOpLogger.getInstance(), Attachment("{ \"number\": 10 }".toByteArray(), "log.json"), 1000) @@ -219,8 +219,8 @@ class RateLimiterTest { @Test fun `records only dropped items as lost`() { val rateLimiter = fixture.getSUT() - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) val userFeedbackItem = SentryEnvelopeItem.fromUserFeedback( @@ -233,7 +233,7 @@ class RateLimiterTest { it.setName("John Me") } ) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val profileItem = SentryEnvelopeItem.fromProfilingTrace(ProfilingTraceData(File(""), transaction), 1000, fixture.serializer) val sessionItem = SentryEnvelopeItem.fromSession(fixture.serializer, Session("123", User(), "env", "release")) val attachmentItem = SentryEnvelopeItem.fromAttachment(fixture.serializer, NoOpLogger.getInstance(), Attachment("{ \"number\": 10 }".toByteArray(), "log.json"), 1000) @@ -253,12 +253,12 @@ class RateLimiterTest { @Test fun `drop profile items as lost`() { val rateLimiter = fixture.getSUT() - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) val f = File.createTempFile("test", "trace") - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val profileItem = SentryEnvelopeItem.fromProfilingTrace(ProfilingTraceData(f, transaction), 1000, fixture.serializer) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, profileItem)) diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index a285cd5832..9f330348b0 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -1,7 +1,8 @@ package io.sentry.util import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.MonitorConfig import io.sentry.MonitorSchedule import io.sentry.MonitorScheduleUnit @@ -56,30 +57,31 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val returnValue = CheckInUtils.withCheckIn("monitor-1") { return@withCheckIn "test1" } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -87,8 +89,9 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with exception`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) try { CheckInUtils.withCheckIn("monitor-1") { @@ -99,22 +102,22 @@ class CheckInUtilsTest { assertEquals("thrown on purpose", e.message) } - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -122,32 +125,33 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with upsert`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)) val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) { "test1" } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertSame(monitorConfig, it.monitorConfig) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -155,9 +159,10 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with upsert and thresholds`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)).apply { failureIssueThreshold = 10 recoveryThreshold = 20 @@ -167,23 +172,23 @@ class CheckInUtilsTest { } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertSame(monitorConfig, it.monitorConfig) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -191,9 +196,10 @@ class CheckInUtilsTest { @Test fun `sets defaults for MonitorConfig from SentryOptions`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn( + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { defaultCheckinMargin = 20 @@ -218,9 +224,10 @@ class CheckInUtilsTest { @Test fun `defaults for MonitorConfig from SentryOptions can be overridden`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn( + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { defaultCheckinMargin = 20 diff --git a/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt b/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt index e38410641e..b3f640aeec 100644 --- a/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt @@ -1,7 +1,7 @@ package io.sentry.util import io.sentry.Baggage -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.NoOpSpan import io.sentry.Scope import io.sentry.ScopeCallback @@ -29,18 +29,18 @@ class TracingUtilsTest { val options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val scope = Scope(options) lateinit var span: Span val preExistingBaggage = listOf("some-baggage-key=some-baggage-value") fun setup() { - whenever(hub.options).thenReturn(options) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) span = Span( TransactionContext("name", "op", TracesSamplingDecision(true)), - SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), hub), - hub, + SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), scopes), + scopes, null, SpanOptions() ) @@ -53,7 +53,7 @@ class TracingUtilsTest { fun `returns headers if allowed from scope without span`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -68,7 +68,7 @@ class TracingUtilsTest { fun `returns headers if allowed from scope if span is noop`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, NoOpSpan.getInstance()) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, NoOpSpan.getInstance()) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -83,7 +83,7 @@ class TracingUtilsTest { fixture.scope.propagationContext.baggage = Baggage.fromHeader("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=2722d9f6ec019ade60c776169d9a8904,sentry-transaction=HTTP%20GET").also { it.freeze() } fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -98,7 +98,7 @@ class TracingUtilsTest { fun `returns headers if allowed from span`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -112,7 +112,7 @@ class TracingUtilsTest { fixture.options.isTraceSampling = false fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNull(headers) } @@ -122,7 +122,7 @@ class TracingUtilsTest { fixture.options.isTraceSampling = false fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNull(headers) } @@ -132,7 +132,7 @@ class TracingUtilsTest { fixture.options.setTracePropagationTargets(listOf("some-host-that-does-not-exist")) fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNull(headers) } @@ -142,7 +142,7 @@ class TracingUtilsTest { fixture.options.setTracePropagationTargets(listOf("some-host-that-does-not-exist")) fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNull(headers) } @@ -153,7 +153,7 @@ class TracingUtilsTest { val propagationContextBefore = fixture.scope.propagationContext - TracingUtils.startNewTrace(fixture.hub) + TracingUtils.startNewTrace(fixture.scopes) assertNotEquals(propagationContextBefore.traceId, fixture.scope.propagationContext.traceId) assertNotEquals(propagationContextBefore.spanId, fixture.scope.propagationContext.spanId) From 95f5e1b1cd14bde66225c89446d89e7b9cde7156 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 13:53:29 +0200 Subject: [PATCH 02/57] Add Scopes --- sentry/api/sentry.api | 76 ++ sentry/src/main/java/io/sentry/IScope.java | 11 + sentry/src/main/java/io/sentry/NoOpScope.java | 17 + sentry/src/main/java/io/sentry/Scope.java | 28 + sentry/src/main/java/io/sentry/Scopes.java | 1093 +++++++++++++++++ sentry/src/main/java/io/sentry/Sentry.java | 8 +- 6 files changed, 1231 insertions(+), 2 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/Scopes.java diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 52eb5df888..24935be274 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -661,10 +661,12 @@ public abstract interface class io/sentry/IScope { public abstract fun endSession ()Lio/sentry/Session; public abstract fun getAttachments ()Ljava/util/List; public abstract fun getBreadcrumbs ()Ljava/util/Queue; + public abstract fun getClient ()Lio/sentry/ISentryClient; public abstract fun getContexts ()Lio/sentry/protocol/Contexts; public abstract fun getEventProcessors ()Ljava/util/List; public abstract fun getExtras ()Ljava/util/Map; public abstract fun getFingerprint ()Ljava/util/List; + public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; public abstract fun getLevel ()Lio/sentry/SentryLevel; public abstract fun getOptions ()Lio/sentry/SentryOptions; public abstract fun getPropagationContext ()Lio/sentry/PropagationContext; @@ -679,6 +681,7 @@ public abstract interface class io/sentry/IScope { public abstract fun removeContexts (Ljava/lang/String;)V public abstract fun removeExtra (Ljava/lang/String;)V public abstract fun removeTag (Ljava/lang/String;)V + public abstract fun setClient (Lio/sentry/ISentryClient;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -688,6 +691,7 @@ public abstract interface class io/sentry/IScope { public abstract fun setContexts (Ljava/lang/String;[Ljava/lang/Object;)V public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public abstract fun setFingerprint (Ljava/util/List;)V + public abstract fun setLastEventId (Lio/sentry/protocol/SentryId;)V public abstract fun setLevel (Lio/sentry/SentryLevel;)V public abstract fun setPropagationContext (Lio/sentry/PropagationContext;)V public abstract fun setRequest (Lio/sentry/protocol/Request;)V @@ -1286,11 +1290,13 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun endSession ()Lio/sentry/Session; public fun getAttachments ()Ljava/util/List; public fun getBreadcrumbs ()Ljava/util/Queue; + public fun getClient ()Lio/sentry/ISentryClient; public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEventProcessors ()Ljava/util/List; public fun getExtras ()Ljava/util/Map; public fun getFingerprint ()Ljava/util/List; public static fun getInstance ()Lio/sentry/NoOpScope; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; public fun getPropagationContext ()Lio/sentry/PropagationContext; @@ -1305,6 +1311,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V + public fun setClient (Lio/sentry/ISentryClient;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1314,6 +1321,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun setContexts (Ljava/lang/String;[Ljava/lang/Object;)V public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public fun setFingerprint (Ljava/util/List;)V + public fun setLastEventId (Lio/sentry/protocol/SentryId;)V public fun setLevel (Lio/sentry/SentryLevel;)V public fun setPropagationContext (Lio/sentry/PropagationContext;)V public fun setRequest (Lio/sentry/protocol/Request;)V @@ -1708,10 +1716,12 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun endSession ()Lio/sentry/Session; public fun getAttachments ()Ljava/util/List; public fun getBreadcrumbs ()Ljava/util/Queue; + public fun getClient ()Lio/sentry/ISentryClient; public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEventProcessors ()Ljava/util/List; public fun getExtras ()Ljava/util/Map; public fun getFingerprint ()Ljava/util/List; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; public fun getPropagationContext ()Lio/sentry/PropagationContext; @@ -1726,6 +1736,7 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V + public fun setClient (Lio/sentry/ISentryClient;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1735,6 +1746,7 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun setContexts (Ljava/lang/String;[Ljava/lang/Object;)V public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public fun setFingerprint (Ljava/util/List;)V + public fun setLastEventId (Lio/sentry/protocol/SentryId;)V public fun setLevel (Lio/sentry/SentryLevel;)V public fun setPropagationContext (Lio/sentry/PropagationContext;)V public fun setRequest (Lio/sentry/protocol/Request;)V @@ -1780,6 +1792,70 @@ public abstract class io/sentry/ScopeObserverAdapter : io/sentry/IScopeObserver public fun setUser (Lio/sentry/protocol/User;)V } +public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/MetricsApi$IMetricsInterface { + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/Scopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/Scopes; + public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getCreator ()Ljava/lang/String; + public fun getDefaultTagsForMetrics ()Ljava/util/Map; + public fun getGlobalScope ()Lio/sentry/IScope; + public fun getIsolationScope ()Lio/sentry/IScope; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator; + public fun getMetricsAggregator ()Lio/sentry/IMetricsAggregator; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getParent ()Lio/sentry/Scopes; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isAncestorOf (Lio/sentry/Scopes;)Z + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startSpanForMetric (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan; + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V diff --git a/sentry/src/main/java/io/sentry/IScope.java b/sentry/src/main/java/io/sentry/IScope.java index 3842fb2c3a..3bc25ce8e9 100644 --- a/sentry/src/main/java/io/sentry/IScope.java +++ b/sentry/src/main/java/io/sentry/IScope.java @@ -2,6 +2,7 @@ import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; +import io.sentry.protocol.SentryId; import io.sentry.protocol.User; import java.util.Collection; import java.util.List; @@ -370,4 +371,14 @@ public interface IScope { */ @NotNull IScope clone(); + + void setLastEventId(final @NotNull SentryId lastEventId); + + @NotNull + SentryId getLastEventId(); + + void setClient(final @NotNull ISentryClient client); + + @NotNull + ISentryClient getClient(); } diff --git a/sentry/src/main/java/io/sentry/NoOpScope.java b/sentry/src/main/java/io/sentry/NoOpScope.java index c756fb49a3..f7336d6edb 100644 --- a/sentry/src/main/java/io/sentry/NoOpScope.java +++ b/sentry/src/main/java/io/sentry/NoOpScope.java @@ -2,6 +2,7 @@ import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; +import io.sentry.protocol.SentryId; import io.sentry.protocol.User; import java.util.ArrayDeque; import java.util.ArrayList; @@ -236,6 +237,9 @@ public void setPropagationContext(@NotNull PropagationContext propagationContext return new PropagationContext(); } + @Override + public void setLastEventId(@NotNull SentryId lastEventId) {} + /** * Clones the Scope * @@ -245,4 +249,17 @@ public void setPropagationContext(@NotNull PropagationContext propagationContext public @NotNull IScope clone() { return NoOpScope.getInstance(); } + + @Override + public @NotNull SentryId getLastEventId() { + return SentryId.EMPTY_ID; + } + + @Override + public void setClient(@NotNull ISentryClient client) {} + + @Override + public @NotNull ISentryClient getClient() { + return NoOpSentryClient.getInstance(); + } } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index 91c9fcd8cf..aa35ccfd7a 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -3,6 +3,7 @@ import io.sentry.protocol.App; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; +import io.sentry.protocol.SentryId; import io.sentry.protocol.TransactionNameSource; import io.sentry.protocol.User; import io.sentry.util.CollectionUtils; @@ -22,6 +23,8 @@ /** Scope data to be sent with the event */ public final class Scope implements IScope { + private volatile @NotNull SentryId lastEventId; + /** Scope's SentryLevel */ private @Nullable SentryLevel level; @@ -80,6 +83,8 @@ public final class Scope implements IScope { private @NotNull PropagationContext propagationContext; + private @NotNull ISentryClient client = NoOpSentryClient.getInstance(); + /** * Scope's ctor * @@ -89,6 +94,7 @@ public Scope(final @NotNull SentryOptions options) { this.options = Objects.requireNonNull(options, "SentryOptions is required."); this.breadcrumbs = createBreadcrumbsList(this.options.getMaxBreadcrumbs()); this.propagationContext = new PropagationContext(); + this.lastEventId = SentryId.EMPTY_ID; } private Scope(final @NotNull Scope scope) { @@ -97,6 +103,8 @@ private Scope(final @NotNull Scope scope) { this.session = scope.session; this.options = scope.options; this.level = scope.level; + // TODO should we do this? didn't do it for Hub + this.lastEventId = scope.getLastEventId(); final User userRef = scope.user; this.user = userRef != null ? new User(userRef) : null; @@ -945,6 +953,26 @@ public void setPropagationContext(final @NotNull PropagationContext propagationC return new Scope(this); } + @Override + public void setLastEventId(@NotNull SentryId lastEventId) { + this.lastEventId = lastEventId; + } + + @Override + public @NotNull SentryId getLastEventId() { + return lastEventId; + } + + @Override + public void setClient(@NotNull ISentryClient client) { + this.client = client; + } + + @Override + public @NotNull ISentryClient getClient() { + return client; + } + /** The IWithTransaction callback */ @ApiStatus.Internal public interface IWithTransaction { diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java new file mode 100644 index 0000000000..618864b649 --- /dev/null +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -0,0 +1,1093 @@ +package io.sentry; + +import io.sentry.clientreport.DiscardReason; +import io.sentry.hints.SessionEndHint; +import io.sentry.hints.SessionStartHint; +import io.sentry.metrics.LocalMetricsAggregator; +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import io.sentry.util.ExceptionUtils; +import io.sentry.util.HintUtils; +import io.sentry.util.Objects; +import io.sentry.util.Pair; +import io.sentry.util.TracingUtils; +import java.io.Closeable; +import java.io.IOException; +import java.lang.ref.WeakReference; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { + + private final @NotNull IScope scope; + private final @NotNull IScope isolationScope; + // TODO just for debugging + @SuppressWarnings("UnusedVariable") + private final @Nullable Scopes parentScopes; + + private final @NotNull String creator; + + // TODO should this be set on all scopes (global, isolation, current)? + private final @NotNull SentryOptions options; + private volatile boolean isEnabled; + private final @NotNull TracesSampler tracesSampler; + + // TODO should this go on global scope? + private final @NotNull Map, String>> throwableToSpan = + Collections.synchronizedMap(new WeakHashMap<>()); + private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; + private final @NotNull MetricsApi metricsApi; + + Scopes( + final @NotNull IScope scope, + final @NotNull IScope isolationScope, + final @NotNull SentryOptions options, + final @NotNull String creator) { + this(scope, isolationScope, null, options, creator); + } + + private Scopes( + final @NotNull IScope scope, + final @NotNull IScope isolationScope, + final @Nullable Scopes parentScopes, + final @NotNull SentryOptions options, + final @NotNull String creator) { + validateOptions(options); + + this.scope = scope; + this.isolationScope = isolationScope; + this.parentScopes = parentScopes; + this.creator = creator; + this.options = options; + this.tracesSampler = new TracesSampler(options); + this.transactionPerformanceCollector = options.getTransactionPerformanceCollector(); + + this.isEnabled = true; + + this.metricsApi = new MetricsApi(this); + } + + public @NotNull String getCreator() { + return creator; + } + + // TODO add to IScopes interface + public @NotNull IScope getScope() { + return scope; + } + + // TODO add to IScopes interface + public @NotNull IScope getIsolationScope() { + return isolationScope; + } + + // TODO add to IScopes interface? + public @Nullable Scopes getParent() { + return parentScopes; + } + + // TODO add to IScopes interface? + public boolean isAncestorOf(final @Nullable Scopes otherScopes) { + if (otherScopes == null) { + return false; + } + + if (this == otherScopes) { + return true; + } + + final @Nullable Scopes parent = otherScopes.getParent(); + if (parent != null) { + return isAncestorOf(parent); + } + + return false; + } + + // TODO add to IScopes interface + public @NotNull Scopes forkedScopes(final @NotNull String creator) { + return new Scopes(scope.clone(), isolationScope.clone(), this, options, creator); + } + + // TODO add to IScopes interface + public @NotNull Scopes forkedCurrentScope(final @NotNull String creator) { + return new Scopes(scope.clone(), isolationScope, this, options, creator); + } + + // // TODO in Sentry.init? + // public static Scopes forkedRoots(final @NotNull SentryOptions options, final @NotNull String + // creator) { + // return new Scopes(ROOT_SCOPE.clone(), ROOT_ISOLATION_SCOPE.clone(), options, creator); + // } + + // TODO always read from root scope? + @Override + public boolean isEnabled() { + return isEnabled; + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return captureEventInternal(event, hint, null); + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return captureEventInternal(event, hint, callback); + } + + private @NotNull SentryId captureEventInternal( + final @NotNull SentryEvent event, + final @Nullable Hint hint, + final @Nullable ScopeCallback scopeCallback) { + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, "Instance is disabled and this 'captureEvent' call is a no-op."); + } else if (event == null) { + options.getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter."); + } else { + try { + assignTraceContext(event); + final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback); + + sentryId = getClient().captureEvent(event, localScope, hint); + updateLastEventId(sentryId); + } catch (Throwable e) { + options + .getLogger() + .log( + SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e); + } + } + return sentryId; + } + + private @NotNull ISentryClient getClient() { + return getCombinedScopeView().getClient(); + } + + private void assignTraceContext(final @NotNull SentryEvent event) { + if (options.isTracingEnabled() && event.getThrowable() != null) { + final Pair, String> pair = + throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable())); + if (pair != null) { + final WeakReference spanWeakRef = pair.getFirst(); + if (event.getContexts().getTrace() == null && spanWeakRef != null) { + final ISpan span = spanWeakRef.get(); + if (span != null) { + event.getContexts().setTrace(span.getSpanContext()); + } + } + final String transactionName = pair.getSecond(); + if (event.getTransaction() == null && transactionName != null) { + event.setTransaction(transactionName); + } + } + } + } + + private IScope buildLocalScope( + final @NotNull IScope parentScope, final @Nullable ScopeCallback callback) { + if (callback != null) { + try { + final IScope localScope = parentScope.clone(); + callback.run(localScope); + return localScope; + } catch (Throwable t) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t); + } + } + return parentScope; + } + + @Override + public @NotNull SentryId captureMessage( + final @NotNull String message, final @NotNull SentryLevel level) { + return captureMessageInternal(message, level, null); + } + + @Override + public @NotNull SentryId captureMessage( + final @NotNull String message, + final @NotNull SentryLevel level, + final @NotNull ScopeCallback callback) { + return captureMessageInternal(message, level, callback); + } + + private @NotNull SentryId captureMessageInternal( + final @NotNull String message, + final @NotNull SentryLevel level, + final @Nullable ScopeCallback scopeCallback) { + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureMessage' call is a no-op."); + } else if (message == null) { + options.getLogger().log(SentryLevel.WARNING, "captureMessage called with null parameter."); + } else { + try { + final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback); + + sentryId = getClient().captureMessage(message, level, localScope); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error while capturing message: " + message, e); + } + } + updateLastEventId(sentryId); + return sentryId; + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureEnvelope( + final @NotNull SentryEnvelope envelope, final @Nullable Hint hint) { + Objects.requireNonNull(envelope, "SentryEnvelope is required."); + + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureEnvelope' call is a no-op."); + } else { + try { + final SentryId capturedEnvelopeId = getClient().captureEnvelope(envelope, hint); + if (capturedEnvelopeId != null) { + sentryId = capturedEnvelopeId; + } + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e); + } + } + return sentryId; + } + + @Override + public @NotNull SentryId captureException( + final @NotNull Throwable throwable, final @Nullable Hint hint) { + return captureExceptionInternal(throwable, hint, null); + } + + @Override + public @NotNull SentryId captureException( + final @NotNull Throwable throwable, + final @Nullable Hint hint, + final @NotNull ScopeCallback callback) { + + return captureExceptionInternal(throwable, hint, callback); + } + + private @NotNull SentryId captureExceptionInternal( + final @NotNull Throwable throwable, + final @Nullable Hint hint, + final @Nullable ScopeCallback scopeCallback) { + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureException' call is a no-op."); + } else if (throwable == null) { + options.getLogger().log(SentryLevel.WARNING, "captureException called with null parameter."); + } else { + try { + final SentryEvent event = new SentryEvent(throwable); + assignTraceContext(event); + + final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback); + + sentryId = getClient().captureEvent(event, localScope, hint); + } catch (Throwable e) { + options + .getLogger() + .log( + SentryLevel.ERROR, "Error while capturing exception: " + throwable.getMessage(), e); + } + } + updateLastEventId(sentryId); + return sentryId; + } + + @Override + public void captureUserFeedback(final @NotNull UserFeedback userFeedback) { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureUserFeedback' call is a no-op."); + } else { + try { + getClient().captureUserFeedback(userFeedback); + } catch (Throwable e) { + options + .getLogger() + .log( + SentryLevel.ERROR, + "Error while capturing captureUserFeedback: " + userFeedback.toString(), + e); + } + } + } + + @Override + public void startSession() { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op."); + } else { + final Scope.SessionPair pair = getCombinedScopeView().startSession(); + if (pair != null) { + // TODO: add helper overload `captureSessions` to pass a list of sessions and submit a + // single envelope + // Or create the envelope here with both items and call `captureEnvelope` + if (pair.getPrevious() != null) { + final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint()); + + getClient().captureSession(pair.getPrevious(), hint); + } + + final Hint hint = HintUtils.createWithTypeCheckHint(new SessionStartHint()); + + getClient().captureSession(pair.getCurrent(), hint); + } else { + options.getLogger().log(SentryLevel.WARNING, "Session could not be started."); + } + } + } + + @Override + public void endSession() { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op."); + } else { + final Session previousSession = getCombinedScopeView().endSession(); + if (previousSession != null) { + final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint()); + + getClient().captureSession(previousSession, hint); + } + } + } + + private IScope getCombinedScopeView() { + // TODO combine global, isolation and current scope + return scope; + } + + @Override + public void close() { + close(false); + } + + @Override + @SuppressWarnings("FutureReturnValueIgnored") + public void close(final boolean isRestarting) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'close' call is a no-op."); + } else { + try { + for (Integration integration : options.getIntegrations()) { + if (integration instanceof Closeable) { + try { + ((Closeable) integration).close(); + } catch (IOException e) { + options + .getLogger() + .log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e); + } + } + } + + // TODO which scopes do we call this on? isolation and current scope? + configureScope(scope -> scope.clear()); + options.getTransactionProfiler().close(); + options.getTransactionPerformanceCollector().close(); + final @NotNull ISentryExecutorService executorService = options.getExecutorService(); + if (isRestarting) { + executorService.submit(() -> executorService.close(options.getShutdownTimeoutMillis())); + } else { + executorService.close(options.getShutdownTimeoutMillis()); + } + + // TODO: should we end session before closing client? + getClient().close(isRestarting); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e); + } + isEnabled = false; + } + } + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'addBreadcrumb' call is a no-op."); + } else if (breadcrumb == null) { + options.getLogger().log(SentryLevel.WARNING, "addBreadcrumb called with null parameter."); + } else { + getDefaultWriteScope().addBreadcrumb(breadcrumb, hint); + } + } + + private IScope getDefaultConfigureScope() { + // TODO configurable default scope via SentryOptions, Android = global or isolation, backend = + // isolation + return scope; + } + + private IScope getDefaultWriteScope() { + // TODO configurable default scope via SentryOptions, Android = global or isolation, backend = + // isolation + return getIsolationScope(); + } + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { + addBreadcrumb(breadcrumb, new Hint()); + } + + @Override + public void setLevel(final @Nullable SentryLevel level) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op."); + } else { + getDefaultWriteScope().setLevel(level); + } + } + + @Override + public void setTransaction(final @Nullable String transaction) { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'setTransaction' call is a no-op."); + } else if (transaction != null) { + getDefaultWriteScope().setTransaction(transaction); + } else { + options.getLogger().log(SentryLevel.WARNING, "Transaction cannot be null"); + } + } + + @Override + public void setUser(final @Nullable User user) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op."); + } else { + getDefaultWriteScope().setUser(user); + } + } + + @Override + public void setFingerprint(final @NotNull List fingerprint) { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'setFingerprint' call is a no-op."); + } else if (fingerprint == null) { + options.getLogger().log(SentryLevel.WARNING, "setFingerprint called with null parameter."); + } else { + getDefaultWriteScope().setFingerprint(fingerprint); + } + } + + @Override + public void clearBreadcrumbs() { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'clearBreadcrumbs' call is a no-op."); + } else { + getDefaultWriteScope().clearBreadcrumbs(); + } + } + + @Override + public void setTag(final @NotNull String key, final @NotNull String value) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'setTag' call is a no-op."); + } else if (key == null || value == null) { + options.getLogger().log(SentryLevel.WARNING, "setTag called with null parameter."); + } else { + getDefaultWriteScope().setTag(key, value); + } + } + + @Override + public void removeTag(final @NotNull String key) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'removeTag' call is a no-op."); + } else if (key == null) { + options.getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter."); + } else { + getDefaultWriteScope().removeTag(key); + } + } + + @Override + public void setExtra(final @NotNull String key, final @NotNull String value) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'setExtra' call is a no-op."); + } else if (key == null || value == null) { + options.getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter."); + } else { + getDefaultWriteScope().setExtra(key, value); + } + } + + @Override + public void removeExtra(final @NotNull String key) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'removeExtra' call is a no-op."); + } else if (key == null) { + options.getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter."); + } else { + getDefaultWriteScope().removeExtra(key); + } + } + + private void updateLastEventId(final @NotNull SentryId lastEventId) { + scope.setLastEventId(lastEventId); + isolationScope.setLastEventId(lastEventId); + getGlobalScope().setLastEventId(lastEventId); + } + + // TODO add to IScopes interface + public @NotNull IScope getGlobalScope() { + // TODO return singleton global scope here + return scope; + } + + @Override + public @NotNull SentryId getLastEventId() { + // TODO read all scopes here / read default scope? + // returning scope.lastEventId isn't ideal because changed to child scope are not stored in + // there + return getGlobalScope().getLastEventId(); + } + + // TODO needs to be deprecated because there's no more stack + // TODO needs to return a lifecycle token + @Override + public void pushScope() { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op."); + } else { + // Scopes scopes = this.forkedScopes("pushScope"); + // return scopes.makeCurrent(); + } + } + + // public SentryLifecycleToken makeCurrent() { + // // TODO store.set(this); + // } + + // TODO needs to be deprecated because there's no more stack + @Override + public void popScope() { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op."); + } else { + // TODO how to remove fork? + // TODO getParentScopes().makeCurrent()? + } + } + + // TODO lots of testing required to see how ThreadLocal is affected + @Override + public void withScope(final @NotNull ScopeCallback callback) { + if (!isEnabled()) { + try { + callback.run(NoOpScope.getInstance()); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + } + + } else { + Scopes forkedScopes = forkedScopes("withScope"); + // TODO should forkedScopes be made current inside callback? + // TODO forkedScopes.makeCurrent()? + try { + callback.run(forkedScopes.getScope()); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + } + } + } + + @Override + public void configureScope(final @NotNull ScopeCallback callback) { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'configureScope' call is a no-op."); + } else { + try { + callback.run(getDefaultConfigureScope()); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); + } + } + } + + @Override + public void bindClient(final @NotNull ISentryClient client) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'bindClient' call is a no-op."); + } else { + if (client != null) { + options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); + getDefaultWriteScope().setClient(client); + } else { + options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); + getDefaultWriteScope().setClient(NoOpSentryClient.getInstance()); + } + } + } + + @Override + public boolean isHealthy() { + return getClient().isHealthy(); + } + + @Override + public void flush(long timeoutMillis) { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'flush' call is a no-op."); + } else { + try { + getClient().flush(timeoutMillis); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e); + } + } + } + + @Override + @SuppressWarnings("deprecation") + public @NotNull IHub clone() { + if (!isEnabled()) { + options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); + } + return new HubScopesWrapper(forkedScopes("scopes clone")); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureTransaction( + final @NotNull SentryTransaction transaction, + final @Nullable TraceContext traceContext, + final @Nullable Hint hint, + final @Nullable ProfilingTraceData profilingTraceData) { + Objects.requireNonNull(transaction, "transaction is required"); + + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureTransaction' call is a no-op."); + } else { + if (!transaction.isFinished()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Transaction: %s is not finished and this 'captureTransaction' call is a no-op.", + transaction.getEventId()); + } else { + if (!Boolean.TRUE.equals(transaction.isSampled())) { + options + .getLogger() + .log( + SentryLevel.DEBUG, + "Transaction %s was dropped due to sampling decision.", + transaction.getEventId()); + if (options.getBackpressureMonitor().getDownsampleFactor() > 0) { + options + .getClientReportRecorder() + .recordLostEvent(DiscardReason.BACKPRESSURE, DataCategory.Transaction); + } else { + options + .getClientReportRecorder() + .recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Transaction); + } + } else { + try { + sentryId = + getClient() + .captureTransaction( + transaction, + traceContext, + getCombinedScopeView(), + hint, + profilingTraceData); + } catch (Throwable e) { + options + .getLogger() + .log( + SentryLevel.ERROR, + "Error while capturing transaction with id: " + transaction.getEventId(), + e); + } + } + } + } + return sentryId; + } + + @Override + public @NotNull ITransaction startTransaction( + final @NotNull TransactionContext transactionContext, + final @NotNull TransactionOptions transactionOptions) { + return createTransaction(transactionContext, transactionOptions); + } + + private @NotNull ITransaction createTransaction( + final @NotNull TransactionContext transactionContext, + final @NotNull TransactionOptions transactionOptions) { + Objects.requireNonNull(transactionContext, "transactionContext is required"); + + ITransaction transaction; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'startTransaction' returns a no-op."); + transaction = NoOpTransaction.getInstance(); + } else if (!options.getInstrumenter().equals(transactionContext.getInstrumenter())) { + options + .getLogger() + .log( + SentryLevel.DEBUG, + "Returning no-op for instrumenter %s as the SDK has been configured to use instrumenter %s", + transactionContext.getInstrumenter(), + options.getInstrumenter()); + transaction = NoOpTransaction.getInstance(); + } else if (!options.isTracingEnabled()) { + options + .getLogger() + .log( + SentryLevel.INFO, "Tracing is disabled and this 'startTransaction' returns a no-op."); + transaction = NoOpTransaction.getInstance(); + } else { + final SamplingContext samplingContext = + new SamplingContext(transactionContext, transactionOptions.getCustomSamplingContext()); + @NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext); + transactionContext.setSamplingDecision(samplingDecision); + + transaction = + new SentryTracer( + transactionContext, this, transactionOptions, transactionPerformanceCollector); + + // The listener is called only if the transaction exists, as the transaction is needed to + // stop it + if (samplingDecision.getSampled() && samplingDecision.getProfileSampled()) { + final ITransactionProfiler transactionProfiler = options.getTransactionProfiler(); + // If the profiler is not running, we start and bind it here. + if (!transactionProfiler.isRunning()) { + transactionProfiler.start(); + transactionProfiler.bindTransaction(transaction); + } else if (transactionOptions.isAppStartTransaction()) { + // If the profiler is running and the current transaction is the app start, we bind it. + transactionProfiler.bindTransaction(transaction); + } + } + } + if (transactionOptions.isBindToScope()) { + configureScope(scope -> scope.setTransaction(transaction)); + } + return transaction; + } + + @Deprecated + @SuppressWarnings("InlineMeSuggester") + @Override + public @Nullable SentryTraceHeader traceHeaders() { + return getTraceparent(); + } + + @Override + @ApiStatus.Internal + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan span, + final @NotNull String transactionName) { + Objects.requireNonNull(throwable, "throwable is required"); + Objects.requireNonNull(span, "span is required"); + Objects.requireNonNull(transactionName, "transactionName is required"); + // to match any cause, span context is always attached to the root cause of the exception + final Throwable rootCause = ExceptionUtils.findRootCause(throwable); + // the most inner span should be assigned to a throwable + if (!throwableToSpan.containsKey(rootCause)) { + throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName)); + } + } + + // TODO this seems unused + @Nullable + SpanContext getSpanContext(final @NotNull Throwable throwable) { + Objects.requireNonNull(throwable, "throwable is required"); + final Throwable rootCause = ExceptionUtils.findRootCause(throwable); + final Pair, String> pair = this.throwableToSpan.get(rootCause); + if (pair != null) { + final WeakReference spanWeakRef = pair.getFirst(); + if (spanWeakRef != null) { + final ISpan span = spanWeakRef.get(); + if (span != null) { + return span.getSpanContext(); + } + } + } + return null; + } + + @Override + public @Nullable ISpan getSpan() { + ISpan span = null; + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'getSpan' call is a no-op."); + } else { + span = getScope().getSpan(); + } + return span; + } + + @Override + @ApiStatus.Internal + public @Nullable ITransaction getTransaction() { + ITransaction span = null; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'getTransaction' call is a no-op."); + } else { + span = getScope().getTransaction(); + } + return span; + } + + @Override + public @NotNull SentryOptions getOptions() { + return options; + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return SentryCrashLastRunState.getInstance() + .isCrashedLastRun(options.getCacheDirPath(), !options.isEnableAutoSessionTracking()); + } + + @Override + public void reportFullyDisplayed() { + if (options.isEnableTimeToFullDisplayTracing()) { + options.getFullyDisplayedReporter().reportFullyDrawn(); + } + } + + @Override + public @Nullable TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { + @NotNull + PropagationContext propagationContext = + PropagationContext.fromHeaders(getOptions().getLogger(), sentryTrace, baggageHeaders); + // TODO should this go on isolation scope? + configureScope( + (scope) -> { + scope.setPropagationContext(propagationContext); + }); + if (options.isTracingEnabled()) { + return TransactionContext.fromPropagationContext(propagationContext); + } else { + return null; + } + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'getTraceparent' call is a no-op."); + } else { + final @Nullable TracingUtils.TracingHeaders headers = + TracingUtils.trace(this, null, getSpan()); + if (headers != null) { + return headers.getSentryTraceHeader(); + } + } + + return null; + } + + @Override + public @Nullable BaggageHeader getBaggage() { + if (!isEnabled()) { + options + .getLogger() + .log(SentryLevel.WARNING, "Instance is disabled and this 'getBaggage' call is a no-op."); + } else { + final @Nullable TracingUtils.TracingHeaders headers = + TracingUtils.trace(this, null, getSpan()); + if (headers != null) { + return headers.getBaggageHeader(); + } + } + + return null; + } + + @Override + @ApiStatus.Experimental + public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { + SentryId sentryId = SentryId.EMPTY_ID; + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'captureCheckIn' call is a no-op."); + } else { + try { + sentryId = getClient().captureCheckIn(checkIn, getCombinedScopeView(), null); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error while capturing check-in for slug", e); + } + } + updateLastEventId(sentryId); + return sentryId; + } + + @ApiStatus.Internal + @Override + public @Nullable RateLimiter getRateLimiter() { + return getClient().getRateLimiter(); + } + + @Override + public @NotNull MetricsApi metrics() { + return metricsApi; + } + + @Override + public @NotNull IMetricsAggregator getMetricsAggregator() { + return getClient().getMetricsAggregator(); + } + + @Override + public @NotNull Map getDefaultTagsForMetrics() { + if (!options.isEnableDefaultTagsForMetrics()) { + return Collections.emptyMap(); + } + + final @NotNull Map tags = new HashMap<>(); + final @Nullable String release = options.getRelease(); + if (release != null) { + tags.put("release", release); + } + + final @Nullable String environment = options.getEnvironment(); + if (environment != null) { + tags.put("environment", environment); + } + + final @Nullable String txnName = getCombinedScopeView().getTransactionName(); + if (txnName != null) { + tags.put("transaction", txnName); + } + return Collections.unmodifiableMap(tags); + } + + @Override + public @Nullable ISpan startSpanForMetric(@NotNull String op, @NotNull String description) { + final @Nullable ISpan span = getSpan(); + if (span != null) { + return span.startChild(op, description); + } + return null; + } + + @Override + public @Nullable LocalMetricsAggregator getLocalMetricsAggregator() { + if (!options.isEnableSpanLocalMetricAggregation()) { + return null; + } + final @Nullable ISpan span = getSpan(); + if (span != null) { + return span.getLocalMetricsAggregator(); + } + return null; + } + + private static void validateOptions(final @NotNull SentryOptions options) { + Objects.requireNonNull(options, "SentryOptions is required."); + if (options.getDsn() == null || options.getDsn().isEmpty()) { + throw new IllegalArgumentException( + "Hub requires a DSN to be instantiated. Considering using the NoOpHub if no DSN is available."); + } + } +} diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 206ffd8d20..f4996bb8ad 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -254,8 +254,9 @@ private static synchronized void init( Sentry.globalHubMode = globalHubMode; final IScopes hub = getCurrentScopes(); - // TODO new Scopes() - mainScopes = new Hub(options); + final IScope rootScope = new Scope(options); + final IScope rootIsolationScope = new Scope(options); + mainScopes = new Scopes(rootScope, rootIsolationScope, options, "Sentry.init"); currentScopes.set(mainScopes); @@ -800,6 +801,7 @@ public static void removeExtra(final @NotNull String key) { public static void pushScope() { // pushScope is no-op in global hub mode if (!globalHubMode) { + // TODO this might have to behave differently from Scopes.pushScope getCurrentScopes().pushScope(); } } @@ -808,6 +810,7 @@ public static void pushScope() { public static void popScope() { // popScope is no-op in global hub mode if (!globalHubMode) { + // TODO this might have to behave differently from Scopes.popScope getCurrentScopes().popScope(); } } @@ -818,6 +821,7 @@ public static void popScope() { * @param callback the callback */ public static void withScope(final @NotNull ScopeCallback callback) { + // TODO this might have to behave differently from Scopes.withScope getCurrentScopes().withScope(callback); } From 27f2398fd678b0d5554e171d729a69f9b93a33d3 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:00:21 +0200 Subject: [PATCH 03/57] Introduce `IScopes` interface. --- sentry/api/sentry.api | 333 +++++++--- sentry/src/main/java/io/sentry/Hub.java | 1 + .../src/main/java/io/sentry/HubAdapter.java | 22 +- .../main/java/io/sentry/HubScopesWrapper.java | 279 ++++++++ sentry/src/main/java/io/sentry/IHub.java | 595 +----------------- sentry/src/main/java/io/sentry/IScopes.java | 594 +++++++++++++++++ sentry/src/main/java/io/sentry/NoOpHub.java | 7 + .../src/main/java/io/sentry/NoOpScopes.java | 243 +++++++ .../main/java/io/sentry/ScopesAdapter.java | 286 +++++++++ sentry/src/main/java/io/sentry/Sentry.java | 176 +++--- 10 files changed, 1778 insertions(+), 758 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/HubScopesWrapper.java create mode 100644 sentry/src/main/java/io/sentry/IScopes.java create mode 100644 sentry/src/main/java/io/sentry/NoOpScopes.java create mode 100644 sentry/src/main/java/io/sentry/ScopesAdapter.java diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index bcfc1c9194..52eb5df888 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -293,7 +293,7 @@ public final class io/sentry/EnvelopeReader : io/sentry/IEnvelopeReader { } public final class io/sentry/EnvelopeSender : io/sentry/IEnvelopeSender { - public fun (Lio/sentry/IHub;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V + public fun (Lio/sentry/IScopes;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V public synthetic fun processDirectory (Ljava/io/File;)V public fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } @@ -520,6 +520,59 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public fun withScope (Lio/sentry/ScopeCallback;)V } +public final class io/sentry/HubScopesWrapper : io/sentry/IHub { + public fun (Lio/sentry/IScopes;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public abstract interface class io/sentry/IConnectionStatusProvider { public abstract fun addConnectionStatusObserver (Lio/sentry/IConnectionStatusProvider$IConnectionStatusObserver;)Z public abstract fun getConnectionStatus ()Lio/sentry/IConnectionStatusProvider$ConnectionStatus; @@ -548,71 +601,7 @@ public abstract interface class io/sentry/IEnvelopeSender { public abstract fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } -public abstract interface class io/sentry/IHub { - public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;)V - public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V - public fun addBreadcrumb (Ljava/lang/String;)V - public fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun bindClient (Lio/sentry/ISentryClient;)V - public abstract fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; - public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; - public abstract fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;)Lio/sentry/protocol/SentryId; - public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;)Lio/sentry/protocol/SentryId; - public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; - public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public abstract fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; - public abstract fun captureUserFeedback (Lio/sentry/UserFeedback;)V - public abstract fun clearBreadcrumbs ()V - public abstract fun clone ()Lio/sentry/IHub; - public abstract fun close ()V - public abstract fun close (Z)V - public abstract fun configureScope (Lio/sentry/ScopeCallback;)V - public abstract fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; - public abstract fun endSession ()V - public abstract fun flush (J)V - public abstract fun getBaggage ()Lio/sentry/BaggageHeader; - public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; - public abstract fun getOptions ()Lio/sentry/SentryOptions; - public abstract fun getRateLimiter ()Lio/sentry/transport/RateLimiter; - public abstract fun getSpan ()Lio/sentry/ISpan; - public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader; - public abstract fun getTransaction ()Lio/sentry/ITransaction; - public abstract fun isCrashedLastRun ()Ljava/lang/Boolean; - public abstract fun isEnabled ()Z - public abstract fun isHealthy ()Z - public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; - public abstract fun popScope ()V - public abstract fun pushScope ()V - public abstract fun removeExtra (Ljava/lang/String;)V - public abstract fun removeTag (Ljava/lang/String;)V - public fun reportFullDisplayed ()V - public abstract fun reportFullyDisplayed ()V - public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun setFingerprint (Ljava/util/List;)V - public abstract fun setLevel (Lio/sentry/SentryLevel;)V - public abstract fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V - public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V - public abstract fun setTransaction (Ljava/lang/String;)V - public abstract fun setUser (Lio/sentry/protocol/User;)V - public abstract fun startSession ()V - public fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; - public abstract fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; - public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction; - public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; - public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader; - public abstract fun withScope (Lio/sentry/ScopeCallback;)V +public abstract interface class io/sentry/IHub : io/sentry/IScopes { } public abstract interface class io/sentry/ILogger { @@ -731,6 +720,74 @@ public abstract interface class io/sentry/IScopeObserver { public abstract fun setUser (Lio/sentry/protocol/User;)V } +public abstract interface class io/sentry/IScopes { + public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun addBreadcrumb (Ljava/lang/String;)V + public fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun bindClient (Lio/sentry/ISentryClient;)V + public abstract fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; + public abstract fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;)Lio/sentry/protocol/SentryId; + public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;)Lio/sentry/protocol/SentryId; + public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public abstract fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public abstract fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public abstract fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public abstract fun clearBreadcrumbs ()V + public abstract fun clone ()Lio/sentry/IHub; + public abstract fun close ()V + public abstract fun close (Z)V + public abstract fun configureScope (Lio/sentry/ScopeCallback;)V + public abstract fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public abstract fun endSession ()V + public abstract fun flush (J)V + public abstract fun getBaggage ()Lio/sentry/BaggageHeader; + public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; + public abstract fun getOptions ()Lio/sentry/SentryOptions; + public abstract fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public abstract fun getSpan ()Lio/sentry/ISpan; + public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public abstract fun getTransaction ()Lio/sentry/ITransaction; + public abstract fun isCrashedLastRun ()Ljava/lang/Boolean; + public abstract fun isEnabled ()Z + public abstract fun isHealthy ()Z + public fun isNoOp ()Z + public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; + public abstract fun popScope ()V + public abstract fun pushScope ()V + public abstract fun removeExtra (Ljava/lang/String;)V + public abstract fun removeTag (Ljava/lang/String;)V + public fun reportFullDisplayed ()V + public abstract fun reportFullyDisplayed ()V + public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun setFingerprint (Ljava/util/List;)V + public abstract fun setLevel (Lio/sentry/SentryLevel;)V + public abstract fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public abstract fun setTransaction (Ljava/lang/String;)V + public abstract fun setUser (Lio/sentry/protocol/User;)V + public abstract fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; + public abstract fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction; + public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public abstract fun withScope (Lio/sentry/ScopeCallback;)V +} + public abstract interface class io/sentry/ISentryClient { public abstract fun captureCheckIn (Lio/sentry/CheckIn;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; @@ -853,7 +910,7 @@ public final class io/sentry/Instrumenter : java/lang/Enum { } public abstract interface class io/sentry/Integration { - public abstract fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public abstract fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/IpAddressUtils { @@ -1187,6 +1244,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun isNoOp ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V public fun pushScope ()V @@ -1270,6 +1328,60 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun withTransaction (Lio/sentry/Scope$IWithTransaction;)V } +public final class io/sentry/NoOpScopes : io/sentry/IScopes { + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public static fun getInstance ()Lio/sentry/NoOpScopes; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun isNoOp ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public final class io/sentry/NoOpSpan : io/sentry/ISpan { public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V @@ -1403,7 +1515,7 @@ public final class io/sentry/OptionsContainer { } public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender { - public fun (Lio/sentry/IHub;Lio/sentry/IEnvelopeReader;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V + public fun (Lio/sentry/IScopes;Lio/sentry/IEnvelopeReader;Lio/sentry/ISerializer;Lio/sentry/ILogger;JI)V public synthetic fun processDirectory (Ljava/io/File;)V public fun processEnvelopeFile (Ljava/lang/String;Lio/sentry/Hint;)V } @@ -1668,11 +1780,64 @@ public abstract class io/sentry/ScopeObserverAdapter : io/sentry/IScopeObserver public fun setUser (Lio/sentry/protocol/User;)V } +public final class io/sentry/ScopesAdapter : io/sentry/IScopes { + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; + public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; + public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; + public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; + public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; + public fun captureUserFeedback (Lio/sentry/UserFeedback;)V + public fun clearBreadcrumbs ()V + public fun clone ()Lio/sentry/IHub; + public synthetic fun clone ()Ljava/lang/Object; + public fun close ()V + public fun close (Z)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; + public fun endSession ()V + public fun flush (J)V + public fun getBaggage ()Lio/sentry/BaggageHeader; + public static fun getInstance ()Lio/sentry/ScopesAdapter; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTraceparent ()Lio/sentry/SentryTraceHeader; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun isCrashedLastRun ()Ljava/lang/Boolean; + public fun isEnabled ()Z + public fun isHealthy ()Z + public fun metrics ()Lio/sentry/metrics/MetricsApi; + public fun popScope ()V + public fun pushScope ()V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun reportFullyDisplayed ()V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()V + public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; + public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withScope (Lio/sentry/ScopeCallback;)V +} + public final class io/sentry/SendCachedEnvelopeFireAndForgetIntegration : io/sentry/IConnectionStatusProvider$IConnectionStatusObserver, io/sentry/Integration, java/io/Closeable { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory;)V public fun close ()V public fun onConnectionStatusChanged (Lio/sentry/IConnectionStatusProvider$ConnectionStatus;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget { @@ -1684,19 +1849,19 @@ public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegra } public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { - public abstract fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public abstract fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; public fun hasValidPath (Ljava/lang/String;Lio/sentry/ILogger;)Z public fun processDir (Lio/sentry/DirectoryProcessor;Ljava/lang/String;Lio/sentry/ILogger;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/SendFireAndForgetEnvelopeSender : io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetDirPath;)V - public fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/SendFireAndForgetOutboxSender : io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory { public fun (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetDirPath;)V - public fun create (Lio/sentry/IHub;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; + public fun create (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget; } public final class io/sentry/Sentry { @@ -1721,7 +1886,7 @@ public final class io/sentry/Sentry { public static fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; public static fun captureUserFeedback (Lio/sentry/UserFeedback;)V public static fun clearBreadcrumbs ()V - public static fun cloneMainHub ()Lio/sentry/IHub; + public static fun cloneMainHub ()Lio/sentry/IScopes; public static fun close ()V public static fun configureScope (Lio/sentry/ScopeCallback;)V public static fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; @@ -1729,6 +1894,7 @@ public final class io/sentry/Sentry { public static fun flush (J)V public static fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getCurrentHub ()Lio/sentry/IHub; + public static fun getCurrentScopes ()Lio/sentry/IScopes; public static fun getLastEventId ()Lio/sentry/protocol/SentryId; public static fun getSpan ()Lio/sentry/ISpan; public static fun getTraceparent ()Lio/sentry/SentryTraceHeader; @@ -1750,6 +1916,7 @@ public final class io/sentry/Sentry { public static fun reportFullDisplayed ()V public static fun reportFullyDisplayed ()V public static fun setCurrentHub (Lio/sentry/IHub;)V + public static fun setCurrentScopes (Lio/sentry/IScopes;)V public static fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public static fun setFingerprint (Ljava/util/List;)V public static fun setLevel (Lio/sentry/SentryLevel;)V @@ -2500,8 +2667,8 @@ public final class io/sentry/SentryTraceHeader { } public final class io/sentry/SentryTracer : io/sentry/ITransaction { - public fun (Lio/sentry/TransactionContext;Lio/sentry/IHub;)V - public fun (Lio/sentry/TransactionContext;Lio/sentry/IHub;Lio/sentry/TransactionOptions;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/IScopes;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/IScopes;Lio/sentry/TransactionOptions;)V public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V @@ -2630,11 +2797,11 @@ public final class io/sentry/ShutdownHookIntegration : io/sentry/Integration, ja public fun ()V public fun (Ljava/lang/Runtime;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/Span : io/sentry/ISpan { - public fun (Lio/sentry/TransactionContext;Lio/sentry/SentryTracer;Lio/sentry/IHub;Lio/sentry/SentryDate;Lio/sentry/SpanOptions;)V + public fun (Lio/sentry/TransactionContext;Lio/sentry/SentryTracer;Lio/sentry/IScopes;Lio/sentry/SentryDate;Lio/sentry/SpanOptions;)V public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V @@ -2815,7 +2982,7 @@ public final class io/sentry/SpotlightIntegration : io/sentry/Integration, io/se public fun close ()V public fun execute (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)V public fun getSpotlightConnectionUrl ()Ljava/lang/String; - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/SystemOutLogger : io/sentry/ILogger { @@ -2975,7 +3142,7 @@ public final class io/sentry/TypeCheckHint { public final class io/sentry/UncaughtExceptionHandlerIntegration : io/sentry/Integration, java/io/Closeable, java/lang/Thread$UncaughtExceptionHandler { public fun ()V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V public fun uncaughtException (Ljava/lang/Thread;Ljava/lang/Throwable;)V } @@ -3016,7 +3183,7 @@ public final class io/sentry/UserFeedback$JsonKeys { } public final class io/sentry/backpressure/BackpressureMonitor : io/sentry/backpressure/IBackpressureMonitor, java/lang/Runnable { - public fun (Lio/sentry/SentryOptions;Lio/sentry/IHub;)V + public fun (Lio/sentry/SentryOptions;Lio/sentry/IScopes;)V public fun getDownsampleFactor ()I public fun run ()V public fun start ()V @@ -5102,9 +5269,9 @@ public final class io/sentry/util/StringUtils { public final class io/sentry/util/TracingUtils { public fun ()V public static fun maybeUpdateBaggage (Lio/sentry/IScope;Lio/sentry/SentryOptions;)Lio/sentry/PropagationContext; - public static fun startNewTrace (Lio/sentry/IHub;)V - public static fun trace (Lio/sentry/IHub;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; - public static fun traceIfAllowed (Lio/sentry/IHub;Ljava/lang/String;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; + public static fun startNewTrace (Lio/sentry/IScopes;)V + public static fun trace (Lio/sentry/IScopes;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; + public static fun traceIfAllowed (Lio/sentry/IScopes;Ljava/lang/String;Ljava/util/List;Lio/sentry/ISpan;)Lio/sentry/util/TracingUtils$TracingHeaders; } public final class io/sentry/util/TracingUtils$TracingHeaders { diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 6b6da88f09..6a98bb2367 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +@Deprecated public final class Hub implements IHub, MetricsApi.IMetricsInterface { private volatile @NotNull SentryId lastEventId; diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index b31d853192..746d51f0cc 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -10,6 +10,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * @deprecated use {@link ScopesAdapter} instead + */ +@Deprecated public final class HubAdapter implements IHub { private static final HubAdapter INSTANCE = new HubAdapter(); @@ -50,7 +54,7 @@ public boolean isEnabled() { @ApiStatus.Internal @Override public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { - return Sentry.getCurrentHub().captureEnvelope(envelope, hint); + return Sentry.getCurrentScopes().captureEnvelope(envelope, hint); } @Override @@ -186,7 +190,7 @@ public void flush(long timeoutMillis) { @Override public @NotNull IHub clone() { - return Sentry.getCurrentHub().clone(); + return Sentry.getCurrentScopes().clone(); } @Override @@ -195,7 +199,7 @@ public void flush(long timeoutMillis) { @Nullable TraceContext traceContext, @Nullable Hint hint, @Nullable ProfilingTraceData profilingTraceData) { - return Sentry.getCurrentHub() + return Sentry.getCurrentScopes() .captureTransaction(transaction, traceContext, hint, profilingTraceData); } @@ -217,23 +221,23 @@ public void setSpanContext( final @NotNull Throwable throwable, final @NotNull ISpan span, final @NotNull String transactionName) { - Sentry.getCurrentHub().setSpanContext(throwable, span, transactionName); + Sentry.getCurrentScopes().setSpanContext(throwable, span, transactionName); } @Override public @Nullable ISpan getSpan() { - return Sentry.getCurrentHub().getSpan(); + return Sentry.getCurrentScopes().getSpan(); } @Override @ApiStatus.Internal public @Nullable ITransaction getTransaction() { - return Sentry.getCurrentHub().getTransaction(); + return Sentry.getCurrentScopes().getTransaction(); } @Override public @NotNull SentryOptions getOptions() { - return Sentry.getCurrentHub().getOptions(); + return Sentry.getCurrentScopes().getOptions(); } @Override @@ -271,11 +275,11 @@ public void reportFullyDisplayed() { @ApiStatus.Internal @Override public @Nullable RateLimiter getRateLimiter() { - return Sentry.getCurrentHub().getRateLimiter(); + return Sentry.getCurrentScopes().getRateLimiter(); } @Override public @NotNull MetricsApi metrics() { - return Sentry.getCurrentHub().metrics(); + return Sentry.getCurrentScopes().metrics(); } } diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java new file mode 100644 index 0000000000..9b294d05b7 --- /dev/null +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -0,0 +1,279 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@SuppressWarnings("deprecation") +@Deprecated +public final class HubScopesWrapper implements IHub { + + private final IScopes scopes; + + public HubScopesWrapper(final @NotNull IScopes scopes) { + this.scopes = scopes; + } + + @Override + public boolean isEnabled() { + return scopes.isEnabled(); + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return scopes.captureEvent(event, hint); + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return scopes.captureEvent(event, hint, callback); + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return scopes.captureMessage(message, level); + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return scopes.captureMessage(message, level, callback); + } + + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return scopes.captureEnvelope(envelope, hint); + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return scopes.captureException(throwable, hint); + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return scopes.captureException(throwable, hint, callback); + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) { + scopes.captureUserFeedback(userFeedback); + } + + @Override + public void startSession() { + scopes.startSession(); + } + + @Override + public void endSession() { + scopes.endSession(); + } + + @Override + public void close() { + scopes.close(); + } + + @Override + public void close(boolean isRestarting) { + scopes.close(isRestarting); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { + scopes.addBreadcrumb(breadcrumb, hint); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb) { + scopes.addBreadcrumb(breadcrumb); + } + + @Override + public void setLevel(@Nullable SentryLevel level) { + scopes.setLevel(level); + } + + @Override + public void setTransaction(@Nullable String transaction) { + scopes.setTransaction(transaction); + } + + @Override + public void setUser(@Nullable User user) { + scopes.setUser(user); + } + + @Override + public void setFingerprint(@NotNull List fingerprint) { + scopes.setFingerprint(fingerprint); + } + + @Override + public void clearBreadcrumbs() { + scopes.clearBreadcrumbs(); + } + + @Override + public void setTag(@NotNull String key, @NotNull String value) { + scopes.setTag(key, value); + } + + @Override + public void removeTag(@NotNull String key) { + scopes.removeTag(key); + } + + @Override + public void setExtra(@NotNull String key, @NotNull String value) { + scopes.setExtra(key, value); + } + + @Override + public void removeExtra(@NotNull String key) { + scopes.removeExtra(key); + } + + @Override + public @NotNull SentryId getLastEventId() { + return scopes.getLastEventId(); + } + + @Override + public void pushScope() { + scopes.pushScope(); + } + + @Override + public void popScope() { + scopes.popScope(); + } + + @Override + public void withScope(@NotNull ScopeCallback callback) { + scopes.withScope(callback); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) { + scopes.configureScope(callback); + } + + @Override + public void bindClient(@NotNull ISentryClient client) { + scopes.bindClient(client); + } + + @Override + public boolean isHealthy() { + return scopes.isHealthy(); + } + + @Override + public void flush(long timeoutMillis) { + scopes.flush(timeoutMillis); + } + + @Override + public @NotNull IHub clone() { + return scopes.clone(); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData) { + return scopes.captureTransaction(transaction, traceContext, hint, profilingTraceData); + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return scopes.startTransaction(transactionContext, transactionOptions); + } + + @Override + public @Nullable SentryTraceHeader traceHeaders() { + return scopes.traceHeaders(); + } + + @ApiStatus.Internal + @Override + public void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) { + scopes.setSpanContext(throwable, span, transactionName); + } + + @Override + public @Nullable ISpan getSpan() { + return scopes.getSpan(); + } + + @ApiStatus.Internal + @Override + public @Nullable ITransaction getTransaction() { + return scopes.getTransaction(); + } + + @Override + public @NotNull SentryOptions getOptions() { + return scopes.getOptions(); + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return scopes.isCrashedLastRun(); + } + + @Override + public void reportFullyDisplayed() { + scopes.reportFullyDisplayed(); + } + + @Override + public @Nullable TransactionContext continueTrace( + @Nullable String sentryTrace, @Nullable List baggageHeaders) { + return scopes.continueTrace(sentryTrace, baggageHeaders); + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return scopes.getTraceparent(); + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return scopes.getBaggage(); + } + + @ApiStatus.Experimental + @Override + public @NotNull SentryId captureCheckIn(@NotNull CheckIn checkIn) { + return scopes.captureCheckIn(checkIn); + } + + @ApiStatus.Internal + @Override + public @Nullable RateLimiter getRateLimiter() { + return scopes.getRateLimiter(); + } + + @ApiStatus.Experimental + @Override + public @NotNull MetricsApi metrics() { + return scopes.metrics(); + } +} diff --git a/sentry/src/main/java/io/sentry/IHub.java b/sentry/src/main/java/io/sentry/IHub.java index 684d8ec528..23a7bed7de 100644 --- a/sentry/src/main/java/io/sentry/IHub.java +++ b/sentry/src/main/java/io/sentry/IHub.java @@ -1,590 +1,9 @@ package io.sentry; -import io.sentry.metrics.MetricsApi; -import io.sentry.protocol.SentryId; -import io.sentry.protocol.SentryTransaction; -import io.sentry.protocol.User; -import io.sentry.transport.RateLimiter; -import java.util.List; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** SDK API contract which combines a client and scope management */ -public interface IHub { - - /** - * Check if the Hub is enabled/active. - * - * @return true if its enabled or false otherwise. - */ - boolean isEnabled(); - - /** - * Captures the event. - * - * @param event the event - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint); - - /** - * Captures the event. - * - * @param event the event - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEvent(@NotNull SentryEvent event) { - return captureEvent(event, new Hint()); - } - - /** - * Captures the event. - * - * @param event the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEvent( - @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { - return captureEvent(event, new Hint(), callback); - } - - /** - * Captures the event. - * - * @param event the event - * @param hint SDK specific but provides high level information about the origin of the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEvent( - final @NotNull SentryEvent event, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback); - - /** - * Captures the message. - * - * @param message The message to send. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureMessage(@NotNull String message) { - return captureMessage(message, SentryLevel.INFO); - } - - /** - * Captures the message. - * - * @param message The message to send. - * @param level The message level. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level); - - /** - * Captures the message. - * - * @param message The message to send. - * @param level The message level. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureMessage( - @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback); - - /** - * Captures the message. - * - * @param message The message to send. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureMessage( - @NotNull String message, @NotNull ScopeCallback callback) { - return captureMessage(message, SentryLevel.INFO, callback); - } - - /** - * Captures an envelope. - * - * @param envelope the SentryEnvelope to send. - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint); - - /** - * Captures an envelope. - * - * @param envelope the SentryEnvelope to send. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope) { - return captureEnvelope(envelope, new Hint()); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param hint SDK specific but provides high level information about the origin of the event - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint); - - /** - * Captures the exception. - * - * @param throwable The exception. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureException(@NotNull Throwable throwable) { - return captureException(throwable, new Hint()); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - default @NotNull SentryId captureException( - @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { - return captureException(throwable, new Hint(), callback); - } - - /** - * Captures the exception. - * - * @param throwable The exception. - * @param hint SDK specific but provides high level information about the origin of the event - * @param callback The callback to configure the scope for a single invocation. - * @return The Id (SentryId object) of the event - */ - @NotNull - SentryId captureException( - final @NotNull Throwable throwable, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback); - - /** - * Captures a manually created user feedback and sends it to Sentry. - * - * @param userFeedback The user feedback to send to Sentry. - */ - void captureUserFeedback(@NotNull UserFeedback userFeedback); - - /** Starts a new session. If there's a running session, it ends it before starting the new one. */ - void startSession(); - - /** Ends the current session */ - void endSession(); - - /** Flushes out the queue for up to timeout seconds and disable the Hub. */ - void close(); - - /** - * Flushes out the queue for up to timeout seconds and disable the Hub. - * - * @param isRestarting if true, avoids locking the main thread when finishing the queue. - */ - void close(boolean isRestarting); - - /** - * Adds a breadcrumb to the current Scope - * - * @param breadcrumb the breadcrumb - * @param hint SDK specific but provides high level information about the origin of the event - */ - void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint); - - /** - * Adds a breadcrumb to the current Scope - * - * @param breadcrumb the breadcrumb - */ - void addBreadcrumb(@NotNull Breadcrumb breadcrumb); - - /** - * Adds a breadcrumb to the current Scope - * - * @param message rendered as text and the whitespace is preserved. - */ - default void addBreadcrumb(@NotNull String message) { - addBreadcrumb(new Breadcrumb(message)); - } - - /** - * Adds a breadcrumb to the current Scope - * - * @param message rendered as text and the whitespace is preserved. - * @param category Categories are dotted strings that indicate what the crumb is or where it comes - * from. - */ - default void addBreadcrumb(@NotNull String message, @NotNull String category) { - Breadcrumb breadcrumb = new Breadcrumb(message); - breadcrumb.setCategory(category); - addBreadcrumb(breadcrumb); - } - - /** - * Sets the level of all events sent within current Scope - * - * @param level the Sentry level - */ - void setLevel(@Nullable SentryLevel level); - - /** - * Sets the name of the current transaction to the current Scope. - * - * @param transaction the transaction - */ - void setTransaction(@Nullable String transaction); - - /** - * Shallow merges user configuration (email, username, etc) to the current Scope. - * - * @param user the user - */ - void setUser(@Nullable User user); - - /** - * Sets the fingerprint to group specific events together to the current Scope. - * - * @param fingerprint the fingerprints - */ - void setFingerprint(@NotNull List fingerprint); - - /** Deletes current breadcrumbs from the current scope. */ - void clearBreadcrumbs(); - - /** - * Sets the tag to a string value to the current Scope, overwriting a potential previous value - * - * @param key the key - * @param value the value - */ - void setTag(@NotNull String key, @NotNull String value); - - /** - * Removes the tag to a string value to the current Scope - * - * @param key the key - */ - void removeTag(@NotNull String key); - - /** - * Sets the extra key to an arbitrary value to the current Scope, overwriting a potential previous - * value - * - * @param key the key - * @param value the value - */ - void setExtra(@NotNull String key, @NotNull String value); - - /** - * Removes the extra key to an arbitrary value to the current Scope - * - * @param key the key - */ - void removeExtra(@NotNull String key); - - /** - * Last event id recorded in the current scope - * - * @return last SentryId - */ - @NotNull - SentryId getLastEventId(); - - /** Pushes a new scope while inheriting the current scope's data. */ - void pushScope(); - - /** Removes the first scope */ - void popScope(); - - /** - * Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry - * SDK in globalHubMode (defaults to true on Android) {@link - * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope - * changes may be dropped when executed in parallel. Use {@link - * IHub#configureScope(ScopeCallback)} instead. - * - * @param callback the callback - */ - void withScope(@NotNull ScopeCallback callback); - - /** - * Configures the scope through the callback. - * - * @param callback The configure scope callback. - */ - void configureScope(@NotNull ScopeCallback callback); - - /** - * Binds a different client to the hub - * - * @param client the client. - */ - void bindClient(@NotNull ISentryClient client); - - /** - * Whether the transport is healthy. - * - * @return true if the transport is healthy - */ - boolean isHealthy(); - - /** - * Flushes events queued up, but keeps the Hub enabled. Not implemented yet. - * - * @param timeoutMillis time in milliseconds - */ - void flush(long timeoutMillis); - - /** - * Clones the Hub - * - * @return the cloned Hub - */ - @NotNull - IHub clone(); - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @param hint the hints - * @param profilingTraceData the profiling trace data - * @return transaction's id - */ - @ApiStatus.Internal - @NotNull - SentryId captureTransaction( - @NotNull SentryTransaction transaction, - @Nullable TraceContext traceContext, - @Nullable Hint hint, - @Nullable ProfilingTraceData profilingTraceData); - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @param hint the hints - * @return transaction's id - */ - @ApiStatus.Internal - @NotNull - default SentryId captureTransaction( - @NotNull SentryTransaction transaction, - @Nullable TraceContext traceContext, - @Nullable Hint hint) { - return captureTransaction(transaction, traceContext, hint, null); - } - - @ApiStatus.Internal - @NotNull - default SentryId captureTransaction(@NotNull SentryTransaction transaction, @Nullable Hint hint) { - return captureTransaction(transaction, null, hint); - } - - /** - * Captures the transaction and enqueues it for sending to Sentry server. - * - * @param transaction the transaction - * @param traceContext the trace context - * @return transaction's id - */ - @ApiStatus.Internal - default @NotNull SentryId captureTransaction( - @NotNull SentryTransaction transaction, @Nullable TraceContext traceContext) { - return captureTransaction(transaction, traceContext, null); - } - - /** - * Creates a Transaction and returns the instance. - * - * @param transactionContexts the transaction contexts - * @return created transaction - */ - default @NotNull ITransaction startTransaction(@NotNull TransactionContext transactionContexts) { - return startTransaction(transactionContexts, new TransactionOptions()); - } - - /** - * Creates a Transaction and returns the instance. Based on the {@link - * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by - * {@link TracesSampler}. - * - * @param name the transaction name - * @param operation the operation - * @return created transaction - */ - default @NotNull ITransaction startTransaction( - final @NotNull String name, final @NotNull String operation) { - return startTransaction(name, operation, new TransactionOptions()); - } - - /** - * Creates a Transaction and returns the instance. Based on the {@link - * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by - * {@link TracesSampler}. - * - * @param name the transaction name - * @param operation the operation - * @param transactionOptions the transaction options - * @return created transaction - */ - default @NotNull ITransaction startTransaction( - final @NotNull String name, - final @NotNull String operation, - final @NotNull TransactionOptions transactionOptions) { - return startTransaction(new TransactionContext(name, operation), transactionOptions); - } - - /** - * Creates a Transaction and returns the instance. Based on the passed transaction context and - * transaction options the decision if transaction is sampled will be taken by {@link - * TracesSampler}. - * - * @param transactionContext the transaction context - * @param transactionOptions the transaction options - * @return created transaction. - */ - @NotNull - ITransaction startTransaction( - final @NotNull TransactionContext transactionContext, - final @NotNull TransactionOptions transactionOptions); - - /** - * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. - * - * @deprecated please use {@link IHub#getTraceparent()} instead. - * @return sentry trace header or null - */ - @Deprecated - @Nullable - SentryTraceHeader traceHeaders(); - - /** - * Associates {@link ISpan} and the transaction name with the {@link Throwable}. Used to determine - * in which trace the exception has been thrown in framework integrations. - * - * @param throwable the throwable - * @param span the span context - * @param transactionName the transaction name - */ - @ApiStatus.Internal - void setSpanContext( - @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName); - - /** - * Gets the current active transaction or span. - * - * @return the active span or null when no active transaction is running - */ - @Nullable - ISpan getSpan(); - - /** - * Returns the transaction. - * - * @return the transaction or null when no active transaction is running. - */ - @ApiStatus.Internal - @Nullable - ITransaction getTransaction(); - - /** - * Gets the {@link SentryOptions} attached to current scope. - * - * @return the options attached to current scope. - */ - @NotNull - SentryOptions getOptions(); - - /** - * Returns if the App has crashed (Process has terminated) during the last run. It only returns - * true or false if offline caching {{@link SentryOptions#getCacheDirPath()} } is set with a valid - * dir. - * - *

If the call to this method is early in the App lifecycle and the SDK could not check if the - * App has crashed in the background, the check is gonna do IO in the calling thread. - * - * @return true if App has crashed, false otherwise, and null if not evaluated yet - */ - @Nullable - Boolean isCrashedLastRun(); - - /** - * Report a screen has been fully loaded. That means all data needed by the UI was loaded. If - * time-to-full-display tracing {{@link SentryOptions#isEnableTimeToFullDisplayTracing()} } is - * disabled this call is ignored. - * - *

This method is safe to be called multiple times. If the time-to-full-display span is already - * finished, this call will be ignored. - */ - void reportFullyDisplayed(); - - /** - * @deprecated See {@link IHub#reportFullyDisplayed()}. - */ - @Deprecated - default void reportFullDisplayed() { - reportFullyDisplayed(); - } - - /** - * Continue a trace based on HTTP header values. If no "sentry-trace" header is provided a random - * trace ID and span ID is created. - * - * @param sentryTrace "sentry-trace" header - * @param baggageHeaders "baggage" headers - * @return a transaction context for starting a transaction or null if performance is disabled - */ - @Nullable - TransactionContext continueTrace( - final @Nullable String sentryTrace, final @Nullable List baggageHeaders); - - /** - * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. - * - * @return sentry trace header or null - */ - @Nullable - SentryTraceHeader getTraceparent(); - - /** - * Returns the "baggage" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getTraceparent()}. - * - * @return baggage header or null - */ - @Nullable - BaggageHeader getBaggage(); - - @ApiStatus.Experimental - @NotNull - SentryId captureCheckIn(final @NotNull CheckIn checkIn); - - @ApiStatus.Internal - @Nullable - RateLimiter getRateLimiter(); - - @ApiStatus.Experimental - @NotNull - MetricsApi metrics(); -} +/** + * SDK API contract which combines a client and scope management + * + * @deprecated please use {@link IScopes} instead + */ +@Deprecated +public interface IHub extends IScopes {} diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java new file mode 100644 index 0000000000..03662457e4 --- /dev/null +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -0,0 +1,594 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface IScopes { + + /** + * Check if the Hub is enabled/active. + * + * @return true if its enabled or false otherwise. + */ + boolean isEnabled(); + + /** + * Captures the event. + * + * @param event the event + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint); + + /** + * Captures the event. + * + * @param event the event + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEvent(@NotNull SentryEvent event) { + return captureEvent(event, new Hint()); + } + + /** + * Captures the event. + * + * @param event the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEvent( + @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { + return captureEvent(event, new Hint(), callback); + } + + /** + * Captures the event. + * + * @param event the event + * @param hint SDK specific but provides high level information about the origin of the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEvent( + final @NotNull SentryEvent event, + final @Nullable Hint hint, + final @NotNull ScopeCallback callback); + + /** + * Captures the message. + * + * @param message The message to send. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureMessage(@NotNull String message) { + return captureMessage(message, SentryLevel.INFO); + } + + /** + * Captures the message. + * + * @param message The message to send. + * @param level The message level. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level); + + /** + * Captures the message. + * + * @param message The message to send. + * @param level The message level. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback); + + /** + * Captures the message. + * + * @param message The message to send. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureMessage( + @NotNull String message, @NotNull ScopeCallback callback) { + return captureMessage(message, SentryLevel.INFO, callback); + } + + /** + * Captures an envelope. + * + * @param envelope the SentryEnvelope to send. + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint); + + /** + * Captures an envelope. + * + * @param envelope the SentryEnvelope to send. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope) { + return captureEnvelope(envelope, new Hint()); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param hint SDK specific but provides high level information about the origin of the event + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint); + + /** + * Captures the exception. + * + * @param throwable The exception. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureException(@NotNull Throwable throwable) { + return captureException(throwable, new Hint()); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + default @NotNull SentryId captureException( + @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { + return captureException(throwable, new Hint(), callback); + } + + /** + * Captures the exception. + * + * @param throwable The exception. + * @param hint SDK specific but provides high level information about the origin of the event + * @param callback The callback to configure the scope for a single invocation. + * @return The Id (SentryId object) of the event + */ + @NotNull + SentryId captureException( + final @NotNull Throwable throwable, + final @Nullable Hint hint, + final @NotNull ScopeCallback callback); + + /** + * Captures a manually created user feedback and sends it to Sentry. + * + * @param userFeedback The user feedback to send to Sentry. + */ + void captureUserFeedback(@NotNull UserFeedback userFeedback); + + /** Starts a new session. If there's a running session, it ends it before starting the new one. */ + void startSession(); + + /** Ends the current session */ + void endSession(); + + /** Flushes out the queue for up to timeout seconds and disable the Hub. */ + void close(); + + /** + * Flushes out the queue for up to timeout seconds and disable the Hub. + * + * @param isRestarting if true, avoids locking the main thread when finishing the queue. + */ + void close(boolean isRestarting); + + /** + * Adds a breadcrumb to the current Scope + * + * @param breadcrumb the breadcrumb + * @param hint SDK specific but provides high level information about the origin of the event + */ + void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint); + + /** + * Adds a breadcrumb to the current Scope + * + * @param breadcrumb the breadcrumb + */ + void addBreadcrumb(@NotNull Breadcrumb breadcrumb); + + /** + * Adds a breadcrumb to the current Scope + * + * @param message rendered as text and the whitespace is preserved. + */ + default void addBreadcrumb(@NotNull String message) { + addBreadcrumb(new Breadcrumb(message)); + } + + /** + * Adds a breadcrumb to the current Scope + * + * @param message rendered as text and the whitespace is preserved. + * @param category Categories are dotted strings that indicate what the crumb is or where it comes + * from. + */ + default void addBreadcrumb(@NotNull String message, @NotNull String category) { + Breadcrumb breadcrumb = new Breadcrumb(message); + breadcrumb.setCategory(category); + addBreadcrumb(breadcrumb); + } + + /** + * Sets the level of all events sent within current Scope + * + * @param level the Sentry level + */ + void setLevel(@Nullable SentryLevel level); + + /** + * Sets the name of the current transaction to the current Scope. + * + * @param transaction the transaction + */ + void setTransaction(@Nullable String transaction); + + /** + * Shallow merges user configuration (email, username, etc) to the current Scope. + * + * @param user the user + */ + void setUser(@Nullable User user); + + /** + * Sets the fingerprint to group specific events together to the current Scope. + * + * @param fingerprint the fingerprints + */ + void setFingerprint(@NotNull List fingerprint); + + /** Deletes current breadcrumbs from the current scope. */ + void clearBreadcrumbs(); + + /** + * Sets the tag to a string value to the current Scope, overwriting a potential previous value + * + * @param key the key + * @param value the value + */ + void setTag(@NotNull String key, @NotNull String value); + + /** + * Removes the tag to a string value to the current Scope + * + * @param key the key + */ + void removeTag(@NotNull String key); + + /** + * Sets the extra key to an arbitrary value to the current Scope, overwriting a potential previous + * value + * + * @param key the key + * @param value the value + */ + void setExtra(@NotNull String key, @NotNull String value); + + /** + * Removes the extra key to an arbitrary value to the current Scope + * + * @param key the key + */ + void removeExtra(@NotNull String key); + + /** + * Last event id recorded in the current scope + * + * @return last SentryId + */ + @NotNull + SentryId getLastEventId(); + + /** Pushes a new scope while inheriting the current scope's data. */ + void pushScope(); + + /** Removes the first scope */ + void popScope(); + + /** + * Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry + * SDK in globalHubMode (defaults to true on Android) {@link + * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope + * changes may be dropped when executed in parallel. Use {@link + * IHub#configureScope(ScopeCallback)} instead. + * + * @param callback the callback + */ + void withScope(@NotNull ScopeCallback callback); + + /** + * Configures the scope through the callback. + * + * @param callback The configure scope callback. + */ + void configureScope(@NotNull ScopeCallback callback); + + /** + * Binds a different client to the hub + * + * @param client the client. + */ + void bindClient(@NotNull ISentryClient client); + + /** + * Whether the transport is healthy. + * + * @return true if the transport is healthy + */ + boolean isHealthy(); + + /** + * Flushes events queued up, but keeps the Hub enabled. Not implemented yet. + * + * @param timeoutMillis time in milliseconds + */ + void flush(long timeoutMillis); + + /** + * Clones the Hub + * + * @return the cloned Hub + */ + @NotNull + @Deprecated + IHub clone(); + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @param hint the hints + * @param profilingTraceData the profiling trace data + * @return transaction's id + */ + @ApiStatus.Internal + @NotNull + SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData); + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @param hint the hints + * @return transaction's id + */ + @ApiStatus.Internal + @NotNull + default SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint) { + return captureTransaction(transaction, traceContext, hint, null); + } + + @ApiStatus.Internal + @NotNull + default SentryId captureTransaction(@NotNull SentryTransaction transaction, @Nullable Hint hint) { + return captureTransaction(transaction, null, hint); + } + + /** + * Captures the transaction and enqueues it for sending to Sentry server. + * + * @param transaction the transaction + * @param traceContext the trace context + * @return transaction's id + */ + @ApiStatus.Internal + default @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, @Nullable TraceContext traceContext) { + return captureTransaction(transaction, traceContext, null); + } + + /** + * Creates a Transaction and returns the instance. + * + * @param transactionContexts the transaction contexts + * @return created transaction + */ + default @NotNull ITransaction startTransaction(@NotNull TransactionContext transactionContexts) { + return startTransaction(transactionContexts, new TransactionOptions()); + } + + /** + * Creates a Transaction and returns the instance. Based on the {@link + * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by + * {@link TracesSampler}. + * + * @param name the transaction name + * @param operation the operation + * @return created transaction + */ + default @NotNull ITransaction startTransaction( + final @NotNull String name, final @NotNull String operation) { + return startTransaction(name, operation, new TransactionOptions()); + } + + /** + * Creates a Transaction and returns the instance. Based on the {@link + * SentryOptions#getTracesSampleRate()} the decision if transaction is sampled will be taken by + * {@link TracesSampler}. + * + * @param name the transaction name + * @param operation the operation + * @param transactionOptions the transaction options + * @return created transaction + */ + default @NotNull ITransaction startTransaction( + final @NotNull String name, + final @NotNull String operation, + final @NotNull TransactionOptions transactionOptions) { + return startTransaction(new TransactionContext(name, operation), transactionOptions); + } + + /** + * Creates a Transaction and returns the instance. Based on the passed transaction context and + * transaction options the decision if transaction is sampled will be taken by {@link + * TracesSampler}. + * + * @param transactionContext the transaction context + * @param transactionOptions the transaction options + * @return created transaction. + */ + @NotNull + ITransaction startTransaction( + final @NotNull TransactionContext transactionContext, + final @NotNull TransactionOptions transactionOptions); + + /** + * Returns the "sentry-trace" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * + * @deprecated please use {@link IHub#getTraceparent()} instead. + * @return sentry trace header or null + */ + @Deprecated + @Nullable + SentryTraceHeader traceHeaders(); + + /** + * Associates {@link ISpan} and the transaction name with the {@link Throwable}. Used to determine + * in which trace the exception has been thrown in framework integrations. + * + * @param throwable the throwable + * @param span the span context + * @param transactionName the transaction name + */ + @ApiStatus.Internal + void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName); + + /** + * Gets the current active transaction or span. + * + * @return the active span or null when no active transaction is running + */ + @Nullable + ISpan getSpan(); + + /** + * Returns the transaction. + * + * @return the transaction or null when no active transaction is running. + */ + @ApiStatus.Internal + @Nullable + ITransaction getTransaction(); + + /** + * Gets the {@link SentryOptions} attached to current scope. + * + * @return the options attached to current scope. + */ + @NotNull + SentryOptions getOptions(); + + /** + * Returns if the App has crashed (Process has terminated) during the last run. It only returns + * true or false if offline caching {{@link SentryOptions#getCacheDirPath()} } is set with a valid + * dir. + * + *

If the call to this method is early in the App lifecycle and the SDK could not check if the + * App has crashed in the background, the check is gonna do IO in the calling thread. + * + * @return true if App has crashed, false otherwise, and null if not evaluated yet + */ + @Nullable + Boolean isCrashedLastRun(); + + /** + * Report a screen has been fully loaded. That means all data needed by the UI was loaded. If + * time-to-full-display tracing {{@link SentryOptions#isEnableTimeToFullDisplayTracing()} } is + * disabled this call is ignored. + * + *

This method is safe to be called multiple times. If the time-to-full-display span is already + * finished, this call will be ignored. + */ + void reportFullyDisplayed(); + + /** + * @deprecated See {@link IHub#reportFullyDisplayed()}. + */ + @Deprecated + default void reportFullDisplayed() { + reportFullyDisplayed(); + } + + /** + * Continue a trace based on HTTP header values. If no "sentry-trace" header is provided a random + * trace ID and span ID is created. + * + * @param sentryTrace "sentry-trace" header + * @param baggageHeaders "baggage" headers + * @return a transaction context for starting a transaction or null if performance is disabled + */ + @Nullable + TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders); + + /** + * Returns the "sentry-trace" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * + * @return sentry trace header or null + */ + @Nullable + SentryTraceHeader getTraceparent(); + + /** + * Returns the "baggage" header that allows tracing across services. Can also be used in + * <meta> HTML tags. Also see {@link IHub#getTraceparent()}. + * + * @return baggage header or null + */ + @Nullable + BaggageHeader getBaggage(); + + @ApiStatus.Experimental + @NotNull + SentryId captureCheckIn(final @NotNull CheckIn checkIn); + + @ApiStatus.Internal + @Nullable + RateLimiter getRateLimiter(); + + @ApiStatus.Experimental + @NotNull + MetricsApi metrics(); + + default boolean isNoOp() { + return false; + } +} diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index e51cea8d2d..704bd2b44a 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +@Deprecated public final class NoOpHub implements IHub { private static final NoOpHub instance = new NoOpHub(); @@ -21,6 +22,7 @@ public final class NoOpHub implements IHub { private NoOpHub() {} + @Deprecated public static NoOpHub getInstance() { return instance; } @@ -234,4 +236,9 @@ public void reportFullyDisplayed() {} public @NotNull MetricsApi metrics() { return metricsApi; } + + @Override + public boolean isNoOp() { + return true; + } } diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java new file mode 100644 index 0000000000..6fef262944 --- /dev/null +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -0,0 +1,243 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.metrics.NoopMetricsAggregator; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class NoOpScopes implements IScopes { + + private static final NoOpScopes instance = new NoOpScopes(); + + private final @NotNull SentryOptions emptyOptions = SentryOptions.empty(); + private final @NotNull MetricsApi metricsApi = + new MetricsApi(NoopMetricsAggregator.getInstance()); + + private NoOpScopes() {} + + public static NoOpScopes getInstance() { + return instance; + } + + @Override + public boolean isEnabled() { + return false; + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return SentryId.EMPTY_ID; + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) {} + + @Override + public void startSession() {} + + @Override + public void endSession() {} + + @Override + public void close() {} + + @Override + public void close(final boolean isRestarting) {} + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) {} + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) {} + + @Override + public void setLevel(@Nullable SentryLevel level) {} + + @Override + public void setTransaction(@Nullable String transaction) {} + + @Override + public void setUser(@Nullable User user) {} + + @Override + public void setFingerprint(@NotNull List fingerprint) {} + + @Override + public void clearBreadcrumbs() {} + + @Override + public void setTag(@NotNull String key, @NotNull String value) {} + + @Override + public void removeTag(@NotNull String key) {} + + @Override + public void setExtra(@NotNull String key, @NotNull String value) {} + + @Override + public void removeExtra(@NotNull String key) {} + + @Override + public @NotNull SentryId getLastEventId() { + return SentryId.EMPTY_ID; + } + + @Override + public void pushScope() {} + + @Override + public void popScope() {} + + @Override + public void withScope(@NotNull ScopeCallback callback) { + callback.run(NoOpScope.getInstance()); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) {} + + @Override + public void bindClient(@NotNull ISentryClient client) {} + + @Override + public boolean isHealthy() { + return true; + } + + @Override + public void flush(long timeoutMillis) {} + + @Deprecated + @Override + public @NotNull IHub clone() { + return NoOpHub.getInstance(); + } + + @Override + public @NotNull SentryId captureTransaction( + final @NotNull SentryTransaction transaction, + final @Nullable TraceContext traceContext, + final @Nullable Hint hint, + final @Nullable ProfilingTraceData profilingTraceData) { + return SentryId.EMPTY_ID; + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return NoOpTransaction.getInstance(); + } + + @Override + @Deprecated + @SuppressWarnings("InlineMeSuggester") + public @NotNull SentryTraceHeader traceHeaders() { + return new SentryTraceHeader(SentryId.EMPTY_ID, SpanId.EMPTY_ID, true); + } + + @Override + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan spanContext, + final @NotNull String transactionName) {} + + @Override + public @Nullable ISpan getSpan() { + return null; + } + + @Override + public @Nullable ITransaction getTransaction() { + return null; + } + + @Override + public @NotNull SentryOptions getOptions() { + return emptyOptions; + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return null; + } + + @Override + public void reportFullyDisplayed() {} + + @Override + public @Nullable TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { + return null; + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return null; + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return null; + } + + @Override + @ApiStatus.Experimental + public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { + return SentryId.EMPTY_ID; + } + + @Override + public @Nullable RateLimiter getRateLimiter() { + return null; + } + + @Override + public @NotNull MetricsApi metrics() { + return metricsApi; + } + + @Override + public boolean isNoOp() { + return true; + } +} diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java new file mode 100644 index 0000000000..1ecd31e248 --- /dev/null +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -0,0 +1,286 @@ +package io.sentry; + +import io.sentry.metrics.MetricsApi; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.SentryTransaction; +import io.sentry.protocol.User; +import io.sentry.transport.RateLimiter; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class ScopesAdapter implements IScopes { + + private static final ScopesAdapter INSTANCE = new ScopesAdapter(); + + private ScopesAdapter() {} + + public static ScopesAdapter getInstance() { + return INSTANCE; + } + + @Override + public boolean isEnabled() { + return Sentry.isEnabled(); + } + + @Override + public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) { + return Sentry.captureEvent(event, hint); + } + + @Override + public @NotNull SentryId captureEvent( + @NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return Sentry.captureEvent(event, hint, callback); + } + + @Override + public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) { + return Sentry.captureMessage(message, level); + } + + @Override + public @NotNull SentryId captureMessage( + @NotNull String message, @NotNull SentryLevel level, @NotNull ScopeCallback callback) { + return Sentry.captureMessage(message, level, callback); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint hint) { + return Sentry.getCurrentScopes().captureEnvelope(envelope, hint); + } + + @Override + public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Hint hint) { + return Sentry.captureException(throwable, hint); + } + + @Override + public @NotNull SentryId captureException( + @NotNull Throwable throwable, @Nullable Hint hint, @NotNull ScopeCallback callback) { + return Sentry.captureException(throwable, hint, callback); + } + + @Override + public void captureUserFeedback(@NotNull UserFeedback userFeedback) { + Sentry.captureUserFeedback(userFeedback); + } + + @Override + public void startSession() { + Sentry.startSession(); + } + + @Override + public void endSession() { + Sentry.endSession(); + } + + @Override + public void close(final boolean isRestarting) { + Sentry.close(); + } + + @Override + public void close() { + Sentry.close(); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { + Sentry.addBreadcrumb(breadcrumb, hint); + } + + @Override + public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { + addBreadcrumb(breadcrumb, new Hint()); + } + + @Override + public void setLevel(@Nullable SentryLevel level) { + Sentry.setLevel(level); + } + + @Override + public void setTransaction(@Nullable String transaction) { + Sentry.setTransaction(transaction); + } + + @Override + public void setUser(@Nullable User user) { + Sentry.setUser(user); + } + + @Override + public void setFingerprint(@NotNull List fingerprint) { + Sentry.setFingerprint(fingerprint); + } + + @Override + public void clearBreadcrumbs() { + Sentry.clearBreadcrumbs(); + } + + @Override + public void setTag(@NotNull String key, @NotNull String value) { + Sentry.setTag(key, value); + } + + @Override + public void removeTag(@NotNull String key) { + Sentry.removeTag(key); + } + + @Override + public void setExtra(@NotNull String key, @NotNull String value) { + Sentry.setExtra(key, value); + } + + @Override + public void removeExtra(@NotNull String key) { + Sentry.removeExtra(key); + } + + @Override + public @NotNull SentryId getLastEventId() { + return Sentry.getLastEventId(); + } + + @Override + public void pushScope() { + Sentry.pushScope(); + } + + @Override + public void popScope() { + Sentry.popScope(); + } + + @Override + public void withScope(@NotNull ScopeCallback callback) { + Sentry.withScope(callback); + } + + @Override + public void configureScope(@NotNull ScopeCallback callback) { + Sentry.configureScope(callback); + } + + @Override + public void bindClient(@NotNull ISentryClient client) { + Sentry.bindClient(client); + } + + @Override + public boolean isHealthy() { + return Sentry.isHealthy(); + } + + @Override + public void flush(long timeoutMillis) { + Sentry.flush(timeoutMillis); + } + + @Override + @SuppressWarnings("deprecation") + public @NotNull IHub clone() { + return Sentry.getCurrentScopes().clone(); + } + + @ApiStatus.Internal + @Override + public @NotNull SentryId captureTransaction( + @NotNull SentryTransaction transaction, + @Nullable TraceContext traceContext, + @Nullable Hint hint, + @Nullable ProfilingTraceData profilingTraceData) { + return Sentry.getCurrentScopes() + .captureTransaction(transaction, traceContext, hint, profilingTraceData); + } + + @Override + public @NotNull ITransaction startTransaction( + @NotNull TransactionContext transactionContext, + @NotNull TransactionOptions transactionOptions) { + return Sentry.startTransaction(transactionContext, transactionOptions); + } + + @Deprecated + @Override + @SuppressWarnings("deprecation") + public @Nullable SentryTraceHeader traceHeaders() { + return Sentry.traceHeaders(); + } + + @ApiStatus.Internal + @Override + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan span, + final @NotNull String transactionName) { + Sentry.getCurrentScopes().setSpanContext(throwable, span, transactionName); + } + + @Override + public @Nullable ISpan getSpan() { + return Sentry.getCurrentScopes().getSpan(); + } + + @Override + @ApiStatus.Internal + public @Nullable ITransaction getTransaction() { + return Sentry.getCurrentScopes().getTransaction(); + } + + @Override + public @NotNull SentryOptions getOptions() { + return Sentry.getCurrentScopes().getOptions(); + } + + @Override + public @Nullable Boolean isCrashedLastRun() { + return Sentry.isCrashedLastRun(); + } + + @Override + public void reportFullyDisplayed() { + Sentry.reportFullyDisplayed(); + } + + @Override + public @Nullable TransactionContext continueTrace( + final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { + return Sentry.continueTrace(sentryTrace, baggageHeaders); + } + + @Override + public @Nullable SentryTraceHeader getTraceparent() { + return Sentry.getTraceparent(); + } + + @Override + public @Nullable BaggageHeader getBaggage() { + return Sentry.getBaggage(); + } + + @Override + @ApiStatus.Experimental + public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { + return Sentry.captureCheckIn(checkIn); + } + + @ApiStatus.Internal + @Override + public @Nullable RateLimiter getRateLimiter() { + return Sentry.getCurrentScopes().getRateLimiter(); + } + + @ApiStatus.Experimental + @Override + public @NotNull MetricsApi metrics() { + return Sentry.getCurrentScopes().metrics(); + } +} diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 5196d0f31a..206ffd8d20 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -43,11 +43,11 @@ public final class Sentry { private Sentry() {} - /** Holds Hubs per thread or only mainHub if globalHubMode is enabled. */ - private static final @NotNull ThreadLocal currentHub = new ThreadLocal<>(); + /** Holds Hubs per thread or only mainScopes if globalHubMode is enabled. */ + private static final @NotNull ThreadLocal currentScopes = new ThreadLocal<>(); /** The Main Hub or NoOp if Sentry is disabled. */ - private static volatile @NotNull IHub mainHub = NoOpHub.getInstance(); + private static volatile @NotNull IScopes mainScopes = NoOpScopes.getInstance(); /** Default value for globalHubMode is false */ private static final boolean GLOBAL_HUB_DEFAULT_MODE = false; @@ -66,40 +66,58 @@ private Sentry() {} private static final long classCreationTimestamp = System.currentTimeMillis(); /** - * Returns the current (threads) hub, if none, clones the mainHub and returns it. + * Returns the current (threads) hub, if none, clones the mainScopes and returns it. * * @return the hub */ @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @SuppressWarnings("deprecation") + @Deprecated public static @NotNull IHub getCurrentHub() { + return new HubScopesWrapper(getCurrentScopes()); + } + + @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @SuppressWarnings("deprecation") + public static @NotNull IScopes getCurrentScopes() { if (globalHubMode) { - return mainHub; + return mainScopes; } - IHub hub = currentHub.get(); - if (hub == null || hub instanceof NoOpHub) { - hub = mainHub.clone(); - currentHub.set(hub); + IScopes hub = currentScopes.get(); + if (hub == null || hub.isNoOp()) { + // TODO fork instead + hub = mainScopes.clone(); + currentScopes.set(hub); } return hub; } /** - * Returns a new hub which is cloned from the mainHub. + * Returns a new hub which is cloned from the mainScopes. * * @return the hub */ @ApiStatus.Internal @ApiStatus.Experimental - public static @NotNull IHub cloneMainHub() { + @SuppressWarnings("deprecation") + public static @NotNull IScopes cloneMainHub() { if (globalHubMode) { - return mainHub; + return mainScopes; } - return mainHub.clone(); + // TODO fork instead + return mainScopes.clone(); } @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @Deprecated + @SuppressWarnings("deprecation") public static void setCurrentHub(final @NotNull IHub hub) { - currentHub.set(hub); + currentScopes.set(hub); + } + + @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + public static void setCurrentScopes(final @NotNull IScopes scopes) { + currentScopes.set(scopes); } /** @@ -108,7 +126,7 @@ public static void setCurrentHub(final @NotNull IHub hub) { * @return true if its enabled or false otherwise. */ public static boolean isEnabled() { - return getCurrentHub().isEnabled(); + return getCurrentScopes().isEnabled(); } /** Initializes the SDK */ @@ -217,6 +235,7 @@ public static void init(final @NotNull SentryOptions options) { * @param options options the SentryOptions * @param globalHubMode the globalHubMode */ + @SuppressWarnings("deprecation") private static synchronized void init( final @NotNull SentryOptions options, final boolean globalHubMode) { if (isEnabled()) { @@ -234,10 +253,11 @@ private static synchronized void init( options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode)); Sentry.globalHubMode = globalHubMode; - final IHub hub = getCurrentHub(); - mainHub = new Hub(options); + final IScopes hub = getCurrentScopes(); + // TODO new Scopes() + mainScopes = new Hub(options); - currentHub.set(mainHub); + currentScopes.set(mainScopes); hub.close(true); @@ -252,12 +272,12 @@ private static synchronized void init( // and hub was still NoOp. // Registering integrations here make sure that Hub is already created. for (final Integration integration : options.getIntegrations()) { - integration.register(HubAdapter.getInstance(), options); + integration.register(ScopesAdapter.getInstance(), options); } notifyOptionsObservers(options); - finalizePreviousSession(options, HubAdapter.getInstance()); + finalizePreviousSession(options, ScopesAdapter.getInstance()); handleAppStartProfilingConfig(options, options.getExecutorService()); } @@ -327,12 +347,12 @@ private static void handleAppStartProfilingConfig( @SuppressWarnings("FutureReturnValueIgnored") private static void finalizePreviousSession( - final @NotNull SentryOptions options, final @NotNull IHub hub) { + final @NotNull SentryOptions options, final @NotNull IScopes scopes) { // enqueue a task to finalize previous session. Since the executor // is single-threaded, this task will be enqueued sequentially after all integrations that have // to modify the previous session have done their work, even if they do that async. try { - options.getExecutorService().submit(new PreviousSessionFinalizer(options, hub)); + options.getExecutorService().submit(new PreviousSessionFinalizer(options, scopes)); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to finalize previous session.", e); } @@ -475,7 +495,7 @@ private static boolean initConfigurations(final @NotNull SentryOptions options) } if (options.isEnableBackpressureHandling()) { - options.setBackpressureMonitor(new BackpressureMonitor(options, HubAdapter.getInstance())); + options.setBackpressureMonitor(new BackpressureMonitor(options, ScopesAdapter.getInstance())); options.getBackpressureMonitor().start(); } @@ -484,11 +504,11 @@ private static boolean initConfigurations(final @NotNull SentryOptions options) /** Close the SDK */ public static synchronized void close() { - final IHub hub = getCurrentHub(); - mainHub = NoOpHub.getInstance(); + final IScopes scopes = getCurrentScopes(); + mainScopes = NoOpScopes.getInstance(); // remove thread local to avoid memory leak - currentHub.remove(); - hub.close(false); + currentScopes.remove(); + scopes.close(false); } /** @@ -498,7 +518,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureEvent(final @NotNull SentryEvent event) { - return getCurrentHub().captureEvent(event); + return getCurrentScopes().captureEvent(event); } /** @@ -510,7 +530,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureEvent( final @NotNull SentryEvent event, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureEvent(event, callback); + return getCurrentScopes().captureEvent(event, callback); } /** @@ -522,7 +542,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureEvent( final @NotNull SentryEvent event, final @Nullable Hint hint) { - return getCurrentHub().captureEvent(event, hint); + return getCurrentScopes().captureEvent(event, hint); } /** @@ -537,7 +557,7 @@ public static synchronized void close() { final @NotNull SentryEvent event, final @Nullable Hint hint, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureEvent(event, hint, callback); + return getCurrentScopes().captureEvent(event, hint, callback); } /** @@ -547,7 +567,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureMessage(final @NotNull String message) { - return getCurrentHub().captureMessage(message); + return getCurrentScopes().captureMessage(message); } /** @@ -559,7 +579,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureMessage( final @NotNull String message, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureMessage(message, callback); + return getCurrentScopes().captureMessage(message, callback); } /** @@ -571,7 +591,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureMessage( final @NotNull String message, final @NotNull SentryLevel level) { - return getCurrentHub().captureMessage(message, level); + return getCurrentScopes().captureMessage(message, level); } /** @@ -586,7 +606,7 @@ public static synchronized void close() { final @NotNull String message, final @NotNull SentryLevel level, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureMessage(message, level, callback); + return getCurrentScopes().captureMessage(message, level, callback); } /** @@ -596,7 +616,7 @@ public static synchronized void close() { * @return The Id (SentryId object) of the event */ public static @NotNull SentryId captureException(final @NotNull Throwable throwable) { - return getCurrentHub().captureException(throwable); + return getCurrentScopes().captureException(throwable); } /** @@ -608,7 +628,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureException( final @NotNull Throwable throwable, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureException(throwable, callback); + return getCurrentScopes().captureException(throwable, callback); } /** @@ -620,7 +640,7 @@ public static synchronized void close() { */ public static @NotNull SentryId captureException( final @NotNull Throwable throwable, final @Nullable Hint hint) { - return getCurrentHub().captureException(throwable, hint); + return getCurrentScopes().captureException(throwable, hint); } /** @@ -635,7 +655,7 @@ public static synchronized void close() { final @NotNull Throwable throwable, final @Nullable Hint hint, final @NotNull ScopeCallback callback) { - return getCurrentHub().captureException(throwable, hint, callback); + return getCurrentScopes().captureException(throwable, hint, callback); } /** @@ -644,7 +664,7 @@ public static synchronized void close() { * @param userFeedback The user feedback to send to Sentry. */ public static void captureUserFeedback(final @NotNull UserFeedback userFeedback) { - getCurrentHub().captureUserFeedback(userFeedback); + getCurrentScopes().captureUserFeedback(userFeedback); } /** @@ -655,7 +675,7 @@ public static void captureUserFeedback(final @NotNull UserFeedback userFeedback) */ public static void addBreadcrumb( final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) { - getCurrentHub().addBreadcrumb(breadcrumb, hint); + getCurrentScopes().addBreadcrumb(breadcrumb, hint); } /** @@ -664,7 +684,7 @@ public static void addBreadcrumb( * @param breadcrumb the breadcrumb */ public static void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { - getCurrentHub().addBreadcrumb(breadcrumb); + getCurrentScopes().addBreadcrumb(breadcrumb); } /** @@ -673,7 +693,7 @@ public static void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { * @param message rendered as text and the whitespace is preserved. */ public static void addBreadcrumb(final @NotNull String message) { - getCurrentHub().addBreadcrumb(message); + getCurrentScopes().addBreadcrumb(message); } /** @@ -684,7 +704,7 @@ public static void addBreadcrumb(final @NotNull String message) { * from. */ public static void addBreadcrumb(final @NotNull String message, final @NotNull String category) { - getCurrentHub().addBreadcrumb(message, category); + getCurrentScopes().addBreadcrumb(message, category); } /** @@ -693,7 +713,7 @@ public static void addBreadcrumb(final @NotNull String message, final @NotNull S * @param level the Sentry level */ public static void setLevel(final @Nullable SentryLevel level) { - getCurrentHub().setLevel(level); + getCurrentScopes().setLevel(level); } /** @@ -702,7 +722,7 @@ public static void setLevel(final @Nullable SentryLevel level) { * @param transaction the transaction */ public static void setTransaction(final @Nullable String transaction) { - getCurrentHub().setTransaction(transaction); + getCurrentScopes().setTransaction(transaction); } /** @@ -711,7 +731,7 @@ public static void setTransaction(final @Nullable String transaction) { * @param user the user */ public static void setUser(final @Nullable User user) { - getCurrentHub().setUser(user); + getCurrentScopes().setUser(user); } /** @@ -720,12 +740,12 @@ public static void setUser(final @Nullable User user) { * @param fingerprint the fingerprints */ public static void setFingerprint(final @NotNull List fingerprint) { - getCurrentHub().setFingerprint(fingerprint); + getCurrentScopes().setFingerprint(fingerprint); } /** Deletes current breadcrumbs from the current scope. */ public static void clearBreadcrumbs() { - getCurrentHub().clearBreadcrumbs(); + getCurrentScopes().clearBreadcrumbs(); } /** @@ -735,7 +755,7 @@ public static void clearBreadcrumbs() { * @param value the value */ public static void setTag(final @NotNull String key, final @NotNull String value) { - getCurrentHub().setTag(key, value); + getCurrentScopes().setTag(key, value); } /** @@ -744,7 +764,7 @@ public static void setTag(final @NotNull String key, final @NotNull String value * @param key the key */ public static void removeTag(final @NotNull String key) { - getCurrentHub().removeTag(key); + getCurrentScopes().removeTag(key); } /** @@ -755,7 +775,7 @@ public static void removeTag(final @NotNull String key) { * @param value the value */ public static void setExtra(final @NotNull String key, final @NotNull String value) { - getCurrentHub().setExtra(key, value); + getCurrentScopes().setExtra(key, value); } /** @@ -764,7 +784,7 @@ public static void setExtra(final @NotNull String key, final @NotNull String val * @param key the key */ public static void removeExtra(final @NotNull String key) { - getCurrentHub().removeExtra(key); + getCurrentScopes().removeExtra(key); } /** @@ -773,14 +793,14 @@ public static void removeExtra(final @NotNull String key) { * @return last SentryId */ public static @NotNull SentryId getLastEventId() { - return getCurrentHub().getLastEventId(); + return getCurrentScopes().getLastEventId(); } /** Pushes a new scope while inheriting the current scope's data. */ public static void pushScope() { // pushScope is no-op in global hub mode if (!globalHubMode) { - getCurrentHub().pushScope(); + getCurrentScopes().pushScope(); } } @@ -788,7 +808,7 @@ public static void pushScope() { public static void popScope() { // popScope is no-op in global hub mode if (!globalHubMode) { - getCurrentHub().popScope(); + getCurrentScopes().popScope(); } } @@ -798,7 +818,7 @@ public static void popScope() { * @param callback the callback */ public static void withScope(final @NotNull ScopeCallback callback) { - getCurrentHub().withScope(callback); + getCurrentScopes().withScope(callback); } /** @@ -807,7 +827,7 @@ public static void withScope(final @NotNull ScopeCallback callback) { * @param callback The configure scope callback. */ public static void configureScope(final @NotNull ScopeCallback callback) { - getCurrentHub().configureScope(callback); + getCurrentScopes().configureScope(callback); } /** @@ -816,11 +836,11 @@ public static void configureScope(final @NotNull ScopeCallback callback) { * @param client the client. */ public static void bindClient(final @NotNull ISentryClient client) { - getCurrentHub().bindClient(client); + getCurrentScopes().bindClient(client); } public static boolean isHealthy() { - return getCurrentHub().isHealthy(); + return getCurrentScopes().isHealthy(); } /** @@ -829,17 +849,17 @@ public static boolean isHealthy() { * @param timeoutMillis time in milliseconds */ public static void flush(final long timeoutMillis) { - getCurrentHub().flush(timeoutMillis); + getCurrentScopes().flush(timeoutMillis); } /** Starts a new session. If there's a running session, it ends it before starting the new one. */ public static void startSession() { - getCurrentHub().startSession(); + getCurrentScopes().startSession(); } /** Ends the current session */ public static void endSession() { - getCurrentHub().endSession(); + getCurrentScopes().endSession(); } /** @@ -851,7 +871,7 @@ public static void endSession() { */ public static @NotNull ITransaction startTransaction( final @NotNull String name, final @NotNull String operation) { - return getCurrentHub().startTransaction(name, operation); + return getCurrentScopes().startTransaction(name, operation); } /** @@ -866,7 +886,7 @@ public static void endSession() { final @NotNull String name, final @NotNull String operation, final @NotNull TransactionOptions transactionOptions) { - return getCurrentHub().startTransaction(name, operation, transactionOptions); + return getCurrentScopes().startTransaction(name, operation, transactionOptions); } /** @@ -884,7 +904,7 @@ public static void endSession() { final @Nullable String description, final @NotNull TransactionOptions transactionOptions) { final ITransaction transaction = - getCurrentHub().startTransaction(name, operation, transactionOptions); + getCurrentScopes().startTransaction(name, operation, transactionOptions); transaction.setDescription(description); return transaction; } @@ -897,7 +917,7 @@ public static void endSession() { */ public static @NotNull ITransaction startTransaction( final @NotNull TransactionContext transactionContexts) { - return getCurrentHub().startTransaction(transactionContexts); + return getCurrentScopes().startTransaction(transactionContexts); } /** @@ -910,7 +930,7 @@ public static void endSession() { public static @NotNull ITransaction startTransaction( final @NotNull TransactionContext transactionContext, final @NotNull TransactionOptions transactionOptions) { - return getCurrentHub().startTransaction(transactionContext, transactionOptions); + return getCurrentScopes().startTransaction(transactionContext, transactionOptions); } /** @@ -923,7 +943,7 @@ public static void endSession() { @Deprecated @SuppressWarnings("InlineMeSuggester") public static @Nullable SentryTraceHeader traceHeaders() { - return getCurrentHub().traceHeaders(); + return getCurrentScopes().traceHeaders(); } /** @@ -935,9 +955,9 @@ public static void endSession() { */ public static @Nullable ISpan getSpan() { if (globalHubMode && Platform.isAndroid()) { - return getCurrentHub().getTransaction(); + return getCurrentScopes().getTransaction(); } else { - return getCurrentHub().getSpan(); + return getCurrentScopes().getSpan(); } } @@ -952,7 +972,7 @@ public static void endSession() { * @return true if App has crashed, false otherwise, and null if not evaluated yet */ public static @Nullable Boolean isCrashedLastRun() { - return getCurrentHub().isCrashedLastRun(); + return getCurrentScopes().isCrashedLastRun(); } /** @@ -964,7 +984,7 @@ public static void endSession() { * finished, this call will be ignored. */ public static void reportFullyDisplayed() { - getCurrentHub().reportFullyDisplayed(); + getCurrentScopes().reportFullyDisplayed(); } /** @@ -980,7 +1000,7 @@ public static void reportFullDisplayed() { @NotNull @ApiStatus.Experimental public static MetricsApi metrics() { - return getCurrentHub().metrics(); + return getCurrentScopes().metrics(); } /** @@ -1009,7 +1029,7 @@ public interface OptionsConfiguration { // return TransactionContext (if performance enabled) or null (if performance disabled) public static @Nullable TransactionContext continueTrace( final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { - return getCurrentHub().continueTrace(sentryTrace, baggageHeaders); + return getCurrentScopes().continueTrace(sentryTrace, baggageHeaders); } /** @@ -1019,7 +1039,7 @@ public interface OptionsConfiguration { * @return sentry trace header or null */ public static @Nullable SentryTraceHeader getTraceparent() { - return getCurrentHub().getTraceparent(); + return getCurrentScopes().getTraceparent(); } /** @@ -1029,11 +1049,11 @@ public interface OptionsConfiguration { * @return baggage header or null */ public static @Nullable BaggageHeader getBaggage() { - return getCurrentHub().getBaggage(); + return getCurrentScopes().getBaggage(); } @ApiStatus.Experimental public static @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { - return getCurrentHub().captureCheckIn(checkIn); + return getCurrentScopes().captureCheckIn(checkIn); } } From ce3c14f33bbba4c19caa82d82c7c2749facbc626 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:11:40 +0200 Subject: [PATCH 04/57] Replace `IHub` with `IScopes` in core --- sentry/src/main/java/io/sentry/Baggage.java | 4 +- .../java/io/sentry/DirectoryProcessor.java | 8 +- .../main/java/io/sentry/EnvelopeSender.java | 10 +- .../src/main/java/io/sentry/Integration.java | 4 +- .../main/java/io/sentry/MonitorConfig.java | 2 +- .../src/main/java/io/sentry/OutboxSender.java | 14 +- .../io/sentry/PreviousSessionFinalizer.java | 8 +- ...achedEnvelopeFireAndForgetIntegration.java | 20 +- .../SendFireAndForgetEnvelopeSender.java | 6 +- .../sentry/SendFireAndForgetOutboxSender.java | 6 +- .../src/main/java/io/sentry/SentryTracer.java | 85 ++-- .../main/java/io/sentry/SentryWrapper.java | 21 +- .../io/sentry/ShutdownHookIntegration.java | 6 +- sentry/src/main/java/io/sentry/Span.java | 30 +- .../java/io/sentry/SpotlightIntegration.java | 2 +- .../UncaughtExceptionHandlerIntegration.java | 12 +- .../backpressure/BackpressureMonitor.java | 11 +- .../file/FileIOSpanManager.java | 6 +- .../file/SentryFileInputStream.java | 42 +- .../file/SentryFileOutputStream.java | 44 +- .../file/SentryFileReader.java | 7 +- .../file/SentryFileWriter.java | 6 +- .../java/io/sentry/util/CheckInUtils.java | 15 +- .../java/io/sentry/util/TracingUtils.java | 18 +- ...aultTransactionPerformanceCollectorTest.kt | 8 +- .../java/io/sentry/DirectoryProcessorTest.kt | 16 +- .../test/java/io/sentry/EnvelopeSenderTest.kt | 20 +- .../src/test/java/io/sentry/HubAdapterTest.kt | 92 ++-- sentry/src/test/java/io/sentry/HubTest.kt | 430 +++++++++--------- .../test/java/io/sentry/JsonSerializerTest.kt | 8 +- .../java/io/sentry/MainEventProcessorTest.kt | 6 +- .../test/java/io/sentry/OutboxSenderTest.kt | 28 +- .../io/sentry/PreviousSessionFinalizerTest.kt | 22 +- sentry/src/test/java/io/sentry/ScopeTest.kt | 24 +- .../test/java/io/sentry/ScopesAdapterTest.kt | 265 +++++++++++ ...hedEnvelopeFireAndForgetIntegrationTest.kt | 34 +- .../test/java/io/sentry/SentryClientTest.kt | 28 +- sentry/src/test/java/io/sentry/SentryTest.kt | 112 ++--- .../test/java/io/sentry/SentryWrapperTest.kt | 32 +- .../io/sentry/ShutdownHookIntegrationTest.kt | 22 +- sentry/src/test/java/io/sentry/SpanTest.kt | 24 +- .../sentry/TraceContextSerializationTest.kt | 6 +- ...UncaughtExceptionHandlerIntegrationTest.kt | 62 +-- .../backpressure/BackpressureMonitorTest.kt | 12 +- .../sentry/clientreport/ClientReportTest.kt | 8 +- .../file/FileIOSpanManagerTest.kt | 14 +- .../file/SentryFileInputStreamTest.kt | 22 +- .../file/SentryFileOutputStreamTest.kt | 12 +- .../file/SentryFileReaderTest.kt | 12 +- .../file/SentryFileWriterTest.kt | 12 +- .../internal/SpotlightIntegrationTest.kt | 10 +- .../java/io/sentry/protocol/SentrySpanTest.kt | 4 +- .../io/sentry/transport/RateLimiterTest.kt | 32 +- .../java/io/sentry/util/CheckInUtilsTest.kt | 91 ++-- .../java/io/sentry/util/TracingUtilsTest.kt | 30 +- 55 files changed, 1092 insertions(+), 793 deletions(-) create mode 100644 sentry/src/test/java/io/sentry/ScopesAdapterTest.kt diff --git a/sentry/src/main/java/io/sentry/Baggage.java b/sentry/src/main/java/io/sentry/Baggage.java index 8e19fceaf8..4a637bacdf 100644 --- a/sentry/src/main/java/io/sentry/Baggage.java +++ b/sentry/src/main/java/io/sentry/Baggage.java @@ -39,13 +39,13 @@ public final class Baggage { @NotNull public static Baggage fromHeader(final @Nullable String headerValue) { return Baggage.fromHeader( - headerValue, false, HubAdapter.getInstance().getOptions().getLogger()); + headerValue, false, ScopesAdapter.getInstance().getOptions().getLogger()); } @NotNull public static Baggage fromHeader(final @Nullable List headerValues) { return Baggage.fromHeader( - headerValues, false, HubAdapter.getInstance().getOptions().getLogger()); + headerValues, false, ScopesAdapter.getInstance().getOptions().getLogger()); } @ApiStatus.Internal diff --git a/sentry/src/main/java/io/sentry/DirectoryProcessor.java b/sentry/src/main/java/io/sentry/DirectoryProcessor.java index 5d60feba60..a6bb258f30 100644 --- a/sentry/src/main/java/io/sentry/DirectoryProcessor.java +++ b/sentry/src/main/java/io/sentry/DirectoryProcessor.java @@ -19,17 +19,17 @@ abstract class DirectoryProcessor { private static final long ENVELOPE_PROCESSING_DELAY = 100L; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ILogger logger; private final long flushTimeoutMillis; private final Queue processedEnvelopes; DirectoryProcessor( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - this.hub = hub; + this.scopes = scopes; this.logger = logger; this.flushTimeoutMillis = flushTimeoutMillis; this.processedEnvelopes = @@ -86,7 +86,7 @@ public void processDirectory(final @NotNull File directory) { } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { logger.log(SentryLevel.INFO, "DirectoryProcessor, rate limiting active."); return; diff --git a/sentry/src/main/java/io/sentry/EnvelopeSender.java b/sentry/src/main/java/io/sentry/EnvelopeSender.java index 598caad280..3a157f59d3 100644 --- a/sentry/src/main/java/io/sentry/EnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/EnvelopeSender.java @@ -17,18 +17,18 @@ @ApiStatus.Internal public final class EnvelopeSender extends DirectoryProcessor implements IEnvelopeSender { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ISerializer serializer; private final @NotNull ILogger logger; public EnvelopeSender( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ISerializer serializer, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - super(hub, logger, flushTimeoutMillis, maxQueueSize); - this.hub = Objects.requireNonNull(hub, "Hub is required."); + super(scopes, logger, flushTimeoutMillis, maxQueueSize); + this.scopes = Objects.requireNonNull(scopes, "Hub is required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); } @@ -60,7 +60,7 @@ protected void processFile(final @NotNull File file, final @NotNull Hint hint) { logger.log( SentryLevel.ERROR, "Failed to deserialize cached envelope %s", file.getAbsolutePath()); } else { - hub.captureEnvelope(envelope, hint); + scopes.captureEnvelope(envelope, hint); } HintUtils.runIfHasTypeLogIfNot( diff --git a/sentry/src/main/java/io/sentry/Integration.java b/sentry/src/main/java/io/sentry/Integration.java index 54b17e4d51..1b1a520473 100644 --- a/sentry/src/main/java/io/sentry/Integration.java +++ b/sentry/src/main/java/io/sentry/Integration.java @@ -10,8 +10,8 @@ public interface Integration { /** * Registers an integration * - * @param hub the Hub + * @param scopes the Scopes * @param options the options */ - void register(@NotNull IHub hub, @NotNull SentryOptions options); + void register(@NotNull IScopes scopes, @NotNull SentryOptions options); } diff --git a/sentry/src/main/java/io/sentry/MonitorConfig.java b/sentry/src/main/java/io/sentry/MonitorConfig.java index d954a50466..763e3b65a4 100644 --- a/sentry/src/main/java/io/sentry/MonitorConfig.java +++ b/sentry/src/main/java/io/sentry/MonitorConfig.java @@ -21,7 +21,7 @@ public final class MonitorConfig implements JsonUnknown, JsonSerializable { public MonitorConfig(final @NotNull MonitorSchedule schedule) { this.schedule = schedule; - final SentryOptions.Cron defaultCron = HubAdapter.getInstance().getOptions().getCron(); + final SentryOptions.Cron defaultCron = ScopesAdapter.getInstance().getOptions().getCron(); if (defaultCron != null) { this.checkinMargin = defaultCron.getDefaultCheckinMargin(); this.maxRuntime = defaultCron.getDefaultMaxRuntime(); diff --git a/sentry/src/main/java/io/sentry/OutboxSender.java b/sentry/src/main/java/io/sentry/OutboxSender.java index 709cbb8580..f80bf030c8 100644 --- a/sentry/src/main/java/io/sentry/OutboxSender.java +++ b/sentry/src/main/java/io/sentry/OutboxSender.java @@ -36,20 +36,20 @@ public final class OutboxSender extends DirectoryProcessor implements IEnvelopeS @SuppressWarnings("CharsetObjectCanBeUsed") private static final Charset UTF_8 = Charset.forName("UTF-8"); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull IEnvelopeReader envelopeReader; private final @NotNull ISerializer serializer; private final @NotNull ILogger logger; public OutboxSender( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull IEnvelopeReader envelopeReader, final @NotNull ISerializer serializer, final @NotNull ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) { - super(hub, logger, flushTimeoutMillis, maxQueueSize); - this.hub = Objects.requireNonNull(hub, "Hub is required."); + super(scopes, logger, flushTimeoutMillis, maxQueueSize); + this.scopes = Objects.requireNonNull(scopes, "Hub is required."); this.envelopeReader = Objects.requireNonNull(envelopeReader, "Envelope reader is required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); @@ -144,7 +144,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN logUnexpectedEventId(envelope, event.getEventId(), currentItem); continue; } - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); logItemCaptured(currentItem); if (!waitFlush(hint)) { @@ -181,7 +181,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN .getTrace() .setSamplingDecision(extractSamplingDecision(traceContext)); } - hub.captureTransaction(transaction, traceContext, hint); + scopes.captureTransaction(transaction, traceContext, hint); logItemCaptured(currentItem); if (!waitFlush(hint)) { @@ -197,7 +197,7 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN final SentryEnvelope newEnvelope = new SentryEnvelope( envelope.getHeader().getEventId(), envelope.getHeader().getSdkVersion(), item); - hub.captureEnvelope(newEnvelope, hint); + scopes.captureEnvelope(newEnvelope, hint); logger.log( SentryLevel.DEBUG, "%s item %d is being captured.", diff --git a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java index 458c6532ba..bda2a14477 100644 --- a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java +++ b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java @@ -33,11 +33,11 @@ final class PreviousSessionFinalizer implements Runnable { private final @NotNull SentryOptions options; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - PreviousSessionFinalizer(final @NotNull SentryOptions options, final @NotNull IHub hub) { + PreviousSessionFinalizer(final @NotNull SentryOptions options, final @NotNull IScopes scopes) { this.options = options; - this.hub = hub; + this.scopes = scopes; } @Override @@ -116,7 +116,7 @@ public void run() { // SdkVersion will be outdated. final SentryEnvelope fromSession = SentryEnvelope.from(serializer, session, options.getSdkVersion()); - hub.captureEnvelope(fromSession); + scopes.captureEnvelope(fromSession); } } catch (Throwable e) { options.getLogger().log(SentryLevel.ERROR, "Error processing previous session.", e); diff --git a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java index bc813fdd1e..2160d1607e 100644 --- a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java +++ b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java @@ -18,7 +18,7 @@ public final class SendCachedEnvelopeFireAndForgetIntegration private final @NotNull SendFireAndForgetFactory factory; private @Nullable IConnectionStatusProvider connectionStatusProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryOptions options; private @Nullable SendFireAndForget sender; private final AtomicBoolean isInitialized = new AtomicBoolean(false); @@ -35,7 +35,7 @@ public interface SendFireAndForgetDirPath { public interface SendFireAndForgetFactory { @Nullable - SendFireAndForget create(@NotNull IHub hub, @NotNull SentryOptions options); + SendFireAndForget create(@NotNull IScopes scopes, @NotNull SentryOptions options); default boolean hasValidPath(final @Nullable String dirPath, final @NotNull ILogger logger) { if (dirPath == null || dirPath.isEmpty()) { @@ -66,8 +66,8 @@ public SendCachedEnvelopeFireAndForgetIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Hub is required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); final String cachedDir = options.getCacheDirPath(); @@ -81,7 +81,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio .log(SentryLevel.DEBUG, "SendCachedEventFireAndForgetIntegration installed."); addIntegrationToSdkVersion(getClass()); - sendCachedEnvelopes(hub, options); + sendCachedEnvelopes(scopes, options); } @Override @@ -95,14 +95,14 @@ public void close() throws IOException { @Override public void onConnectionStatusChanged( final @NotNull IConnectionStatusProvider.ConnectionStatus status) { - if (hub != null && options != null) { - sendCachedEnvelopes(hub, options); + if (scopes != null && options != null) { + sendCachedEnvelopes(scopes, options); } } @SuppressWarnings({"FutureReturnValueIgnored", "NullAway"}) private synchronized void sendCachedEnvelopes( - final @NotNull IHub hub, final @NotNull SentryOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { try { options .getExecutorService() @@ -122,7 +122,7 @@ private synchronized void sendCachedEnvelopes( connectionStatusProvider = options.getConnectionStatusProvider(); connectionStatusProvider.addConnectionStatusObserver(this); - sender = factory.create(hub, options); + sender = factory.create(scopes, options); } // skip run only if we're certainly disconnected @@ -138,7 +138,7 @@ private synchronized void sendCachedEnvelopes( } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { options .getLogger() diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java index e44d18a8d6..155946b95a 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java @@ -21,8 +21,8 @@ public SendFireAndForgetEnvelopeSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( - final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Hub is required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); @@ -33,7 +33,7 @@ public SendFireAndForgetEnvelopeSender( final EnvelopeSender envelopeSender = new EnvelopeSender( - hub, + scopes, options.getSerializer(), options.getLogger(), options.getFlushTimeoutMillis(), diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java index fda41610fd..e7913b5b2f 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java @@ -21,8 +21,8 @@ public SendFireAndForgetOutboxSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( - final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Hub is required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); @@ -33,7 +33,7 @@ public SendFireAndForgetOutboxSender( final OutboxSender outboxSender = new OutboxSender( - hub, + scopes, options.getEnvelopeReader(), options.getSerializer(), options.getLogger(), diff --git a/sentry/src/main/java/io/sentry/SentryTracer.java b/sentry/src/main/java/io/sentry/SentryTracer.java index bc3b5eb531..7d82bbbc4a 100644 --- a/sentry/src/main/java/io/sentry/SentryTracer.java +++ b/sentry/src/main/java/io/sentry/SentryTracer.java @@ -26,7 +26,7 @@ public final class SentryTracer implements ITransaction { private final @NotNull SentryId eventId = new SentryId(); private final @NotNull Span root; private final @NotNull List children = new CopyOnWriteArrayList<>(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @NotNull String name; /** @@ -52,31 +52,31 @@ public final class SentryTracer implements ITransaction { private final @Nullable TransactionPerformanceCollector transactionPerformanceCollector; private final @NotNull TransactionOptions transactionOptions; - public SentryTracer(final @NotNull TransactionContext context, final @NotNull IHub hub) { - this(context, hub, new TransactionOptions(), null); + public SentryTracer(final @NotNull TransactionContext context, final @NotNull IScopes scopes) { + this(context, scopes, new TransactionOptions(), null); } public SentryTracer( final @NotNull TransactionContext context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionOptions transactionOptions) { - this(context, hub, transactionOptions, null); + this(context, scopes, transactionOptions, null); } SentryTracer( final @NotNull TransactionContext context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionOptions transactionOptions, final @Nullable TransactionPerformanceCollector transactionPerformanceCollector) { Objects.requireNonNull(context, "context is required"); - Objects.requireNonNull(hub, "hub is required"); + Objects.requireNonNull(scopes, "scopes are required"); this.root = - new Span(context, this, hub, transactionOptions.getStartTimestamp(), transactionOptions); + new Span(context, this, scopes, transactionOptions.getStartTimestamp(), transactionOptions); this.name = context.getName(); this.instrumenter = context.getInstrumenter(); - this.hub = hub; + this.scopes = scopes; this.transactionPerformanceCollector = transactionPerformanceCollector; this.transactionNameSource = context.getTransactionNameSource(); this.transactionOptions = transactionOptions; @@ -84,7 +84,7 @@ public SentryTracer( if (context.getBaggage() != null) { this.baggage = context.getBaggage(); } else { - this.baggage = new Baggage(hub.getOptions().getLogger()); + this.baggage = new Baggage(scopes.getOptions().getLogger()); } // We are currently sending the performance data only in profiles, so there's no point in @@ -122,7 +122,8 @@ public void run() { try { timer.schedule(idleTimeoutTask, idleTimeout); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to schedule finish timer", e); // if we failed to schedule the finish timer for some reason, we finish it here right @@ -156,7 +157,7 @@ private void onDeadlineTimeoutReached() { return; } - final @NotNull SentryDate finishTimestamp = hub.getOptions().getDateProvider().now(); + final @NotNull SentryDate finishTimestamp = scopes.getOptions().getDateProvider().now(); // abort all child-spans first, this ensures the transaction can be finished, // even if waitForChildren is true @@ -186,7 +187,7 @@ public void finish( // if it's not set -> fallback to the current time if (finishTimestamp == null) { - finishTimestamp = hub.getOptions().getDateProvider().now(); + finishTimestamp = scopes.getOptions().getDateProvider().now(); } // auto-finish any idle spans first @@ -207,9 +208,10 @@ public void finish( ProfilingTraceData profilingTraceData = null; if (Boolean.TRUE.equals(isSampled()) && Boolean.TRUE.equals(isProfileSampled())) { profilingTraceData = - hub.getOptions() + scopes + .getOptions() .getTransactionProfiler() - .onTransactionFinish(this, performanceCollectionData, hub.getOptions()); + .onTransactionFinish(this, performanceCollectionData, scopes.getOptions()); } if (performanceCollectionData != null) { performanceCollectionData.clear(); @@ -222,7 +224,7 @@ public void finish( root.finish(finishStatus.spanStatus, finishTimestamp); - hub.configureScope( + scopes.configureScope( scope -> { scope.withTransaction( transaction -> { @@ -251,7 +253,8 @@ public void finish( if (dropIfNoChildren && children.isEmpty() && transactionOptions.getIdleTimeout() != null) { // if it's an idle transaction which has no children, we drop it to save user's quota - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -261,7 +264,7 @@ public void finish( } transaction.getMeasurements().putAll(root.getMeasurements()); - hub.captureTransaction(transaction, traceContext(), hint, profilingTraceData); + scopes.captureTransaction(transaction, traceContext(), hint, profilingTraceData); } } @@ -292,7 +295,8 @@ public void run() { try { timer.schedule(deadlineTimeoutTask, deadlineTimeOut); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to schedule finish timer", e); // if we failed to schedule the finish timer for some reason, we finish it here right @@ -418,7 +422,7 @@ private ISpan createChild( return NoOpSpan.getInstance(); } - if (children.size() < hub.getOptions().getMaxSpans()) { + if (children.size() < scopes.getOptions().getMaxSpans()) { Objects.requireNonNull(parentSpanId, "parentSpanId is required"); Objects.requireNonNull(operation, "operation is required"); cancelIdleTimer(); @@ -428,7 +432,7 @@ private ISpan createChild( parentSpanId, this, operation, - this.hub, + this.scopes, timestamp, spanOptions, finishingSpan -> { @@ -451,7 +455,7 @@ private ISpan createChild( span.setData(SpanDataConvention.THREAD_ID, String.valueOf(Thread.currentThread().getId())); span.setData( SpanDataConvention.THREAD_NAME, - hub.getOptions().getMainThreadChecker().isMainThread() + scopes.getOptions().getMainThreadChecker().isMainThread() ? "main" : Thread.currentThread().getName()); this.children.add(span); @@ -460,7 +464,8 @@ private ISpan createChild( } return span; } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -529,10 +534,11 @@ private ISpan createChild( return NoOpSpan.getInstance(); } - if (children.size() < hub.getOptions().getMaxSpans()) { + if (children.size() < scopes.getOptions().getMaxSpans()) { return root.startChild(operation, description, timestamp, instrumenter, spanOptions); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -567,7 +573,7 @@ public void finish(@Nullable SpanStatus status, @Nullable SentryDate finishDate) @Override public @Nullable TraceContext traceContext() { - if (hub.getOptions().isTraceSampling()) { + if (scopes.getOptions().isTraceSampling()) { updateBaggageValues(); return baggage.toTraceContext(); } else { @@ -579,12 +585,12 @@ private void updateBaggageValues() { synchronized (this) { if (baggage.isMutable()) { final AtomicReference userAtomicReference = new AtomicReference<>(); - hub.configureScope( + scopes.configureScope( scope -> { userAtomicReference.set(scope.getUser()); }); baggage.setValuesFromTransaction( - this, userAtomicReference.get(), hub.getOptions(), this.getSamplingDecision()); + this, userAtomicReference.get(), scopes.getOptions(), this.getSamplingDecision()); baggage.freeze(); } } @@ -592,7 +598,7 @@ private void updateBaggageValues() { @Override public @Nullable BaggageHeader toBaggageHeader(@Nullable List thirdPartyBaggageHeaders) { - if (hub.getOptions().isTraceSampling()) { + if (scopes.getOptions().isTraceSampling()) { updateBaggageValues(); return BaggageHeader.fromBaggageAndOutgoingHeader(baggage, thirdPartyBaggageHeaders); @@ -616,7 +622,8 @@ private boolean hasAllChildrenFinished() { @Override public void setOperation(final @NotNull String operation) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -636,7 +643,8 @@ public void setOperation(final @NotNull String operation) { @Override public void setDescription(final @Nullable String description) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -656,7 +664,8 @@ public void setDescription(final @Nullable String description) { @Override public void setStatus(final @Nullable SpanStatus status) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -676,7 +685,8 @@ public void setStatus(final @Nullable SpanStatus status) { @Override public void setThrowable(final @Nullable Throwable throwable) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "The transaction is already finished. Throwable cannot be set"); return; @@ -698,7 +708,8 @@ public void setThrowable(final @Nullable Throwable throwable) { @Override public void setTag(final @NotNull String key, final @NotNull String value) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "The transaction is already finished. Tag %s cannot be set", key); return; @@ -720,7 +731,8 @@ public boolean isFinished() { @Override public void setData(@NotNull String key, @NotNull Object value) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, "The transaction is already finished. Data %s cannot be set", key); @@ -795,7 +807,8 @@ public void setName(@NotNull String name) { @Override public void setName(@NotNull String name, @NotNull TransactionNameSource transactionNameSource) { if (root.isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index 1a39adee99..165ace7c83 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -27,16 +27,18 @@ public final class SentryWrapper { * @return the wrapped {@link Callable} * @param - the result type of the {@link Callable} */ + @SuppressWarnings("deprecation") public static Callable wrapCallable(final @NotNull Callable callable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO replace with forking + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { return callable.call(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } @@ -51,17 +53,18 @@ public static Callable wrapCallable(final @NotNull Callable callable) * @return the wrapped {@link Supplier} * @param - the result type of the {@link Supplier} */ + @SuppressWarnings("deprecation") public static Supplier wrapSupplier(final @NotNull Supplier supplier) { - - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO replace with forking + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { return supplier.get(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java index b144f2d88a..d957a87ccf 100644 --- a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java +++ b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java @@ -27,12 +27,12 @@ public ShutdownHookIntegration() { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); if (options.isEnableShutdownHook()) { - thread = new Thread(() -> hub.flush(options.getFlushTimeoutMillis())); + thread = new Thread(() -> scopes.flush(options.getFlushTimeoutMillis())); runtime.addShutdownHook(thread); options.getLogger().log(SentryLevel.DEBUG, "ShutdownHookIntegration installed."); addIntegrationToSdkVersion(getClass()); diff --git a/sentry/src/main/java/io/sentry/Span.java b/sentry/src/main/java/io/sentry/Span.java index 850276dac3..1c9d180b29 100644 --- a/sentry/src/main/java/io/sentry/Span.java +++ b/sentry/src/main/java/io/sentry/Span.java @@ -35,7 +35,7 @@ public final class Span implements ISpan { /** A throwable thrown during the execution of the span. */ private @Nullable Throwable throwable; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull AtomicBoolean finished = new AtomicBoolean(false); @@ -55,8 +55,8 @@ public final class Span implements ISpan { final @Nullable SpanId parentSpanId, final @NotNull SentryTracer transaction, final @NotNull String operation, - final @NotNull IHub hub) { - this(traceId, parentSpanId, transaction, operation, hub, null, new SpanOptions(), null); + final @NotNull IScopes scopes) { + this(traceId, parentSpanId, transaction, operation, scopes, null, new SpanOptions(), null); } Span( @@ -64,7 +64,7 @@ public final class Span implements ISpan { final @Nullable SpanId parentSpanId, final @NotNull SentryTracer transaction, final @NotNull String operation, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable SentryDate startTimestamp, final @NotNull SpanOptions options, final @Nullable SpanFinishedCallback spanFinishedCallback) { @@ -72,30 +72,30 @@ public final class Span implements ISpan { new SpanContext( traceId, new SpanId(), operation, parentSpanId, transaction.getSamplingDecision()); this.transaction = Objects.requireNonNull(transaction, "transaction is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = options; this.spanFinishedCallback = spanFinishedCallback; if (startTimestamp != null) { this.startTimestamp = startTimestamp; } else { - this.startTimestamp = hub.getOptions().getDateProvider().now(); + this.startTimestamp = scopes.getOptions().getDateProvider().now(); } } public Span( final @NotNull TransactionContext context, final @NotNull SentryTracer sentryTracer, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable SentryDate startTimestamp, final @NotNull SpanOptions options) { this.context = Objects.requireNonNull(context, "context is required"); this.transaction = Objects.requireNonNull(sentryTracer, "sentryTracer is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.spanFinishedCallback = null; if (startTimestamp != null) { this.startTimestamp = startTimestamp; } else { - this.startTimestamp = hub.getOptions().getDateProvider().now(); + this.startTimestamp = scopes.getOptions().getDateProvider().now(); } this.options = options; } @@ -180,7 +180,7 @@ public void finish() { @Override public void finish(@Nullable SpanStatus status) { - finish(status, hub.getOptions().getDateProvider().now()); + finish(status, scopes.getOptions().getDateProvider().now()); } /** @@ -197,7 +197,7 @@ public void finish(final @Nullable SpanStatus status, final @Nullable SentryDate } this.context.setStatus(status); - this.timestamp = timestamp == null ? hub.getOptions().getDateProvider().now() : timestamp; + this.timestamp = timestamp == null ? scopes.getOptions().getDateProvider().now() : timestamp; if (options.isTrimStart() || options.isTrimEnd()) { @Nullable SentryDate minChildStart = null; @Nullable SentryDate maxChildEnd = null; @@ -230,7 +230,7 @@ public void finish(final @Nullable SpanStatus status, final @Nullable SentryDate } if (throwable != null) { - hub.setSpanContext(throwable, this, this.transaction.getName()); + scopes.setSpanContext(throwable, this, this.transaction.getName()); } if (spanFinishedCallback != null) { spanFinishedCallback.execute(this); @@ -343,7 +343,8 @@ public void setData(final @NotNull String key, final @NotNull Object value) { @Override public void setMeasurement(final @NotNull String name, final @NotNull Number value) { if (isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -365,7 +366,8 @@ public void setMeasurement( final @NotNull Number value, final @NotNull MeasurementUnit unit) { if (isFinished()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry/src/main/java/io/sentry/SpotlightIntegration.java b/sentry/src/main/java/io/sentry/SpotlightIntegration.java index 6d488bcbce..0b69ae79be 100644 --- a/sentry/src/main/java/io/sentry/SpotlightIntegration.java +++ b/sentry/src/main/java/io/sentry/SpotlightIntegration.java @@ -26,7 +26,7 @@ public final class SpotlightIntegration private @NotNull ISentryExecutorService executorService = NoOpSentryExecutorService.getInstance(); @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = options; this.logger = options.getLogger(); diff --git a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java index 33e1a4a815..47ceaa084e 100644 --- a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java +++ b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java @@ -28,7 +28,7 @@ public final class UncaughtExceptionHandlerIntegration /** Reference to the pre-existing uncaught exception handler. */ private @Nullable Thread.UncaughtExceptionHandler defaultExceptionHandler; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryOptions options; private boolean registered = false; @@ -43,7 +43,7 @@ public UncaughtExceptionHandlerIntegration() { } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { if (registered) { options .getLogger() @@ -54,7 +54,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions } registered = true; - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Hub is required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); this.options @@ -89,7 +89,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions @Override public void uncaughtException(Thread thread, Throwable thrown) { - if (options != null && hub != null) { + if (options != null && scopes != null) { options.getLogger().log(SentryLevel.INFO, "Uncaught exception received."); try { @@ -99,14 +99,14 @@ public void uncaughtException(Thread thread, Throwable thrown) { final SentryEvent event = new SentryEvent(throwable); event.setLevel(SentryLevel.FATAL); - final ITransaction transaction = hub.getTransaction(); + final ITransaction transaction = scopes.getTransaction(); if (transaction == null && event.getEventId() != null) { // if there's no active transaction on scope, this event can trigger flush notification exceptionHint.setFlushable(event.getEventId()); } final Hint hint = HintUtils.createWithTypeCheckHint(exceptionHint); - final @NotNull SentryId sentryId = hub.captureEvent(event, hint); + final @NotNull SentryId sentryId = scopes.captureEvent(event, hint); final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID); final EventDropReason eventDropReason = HintUtils.getEventDropReason(hint); // in case the event has been dropped by multithreaded deduplicator, the other threads will diff --git a/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java b/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java index 2008a38c76..6541a7586a 100644 --- a/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java +++ b/sentry/src/main/java/io/sentry/backpressure/BackpressureMonitor.java @@ -1,6 +1,6 @@ package io.sentry.backpressure; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISentryExecutorService; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -12,12 +12,13 @@ public final class BackpressureMonitor implements IBackpressureMonitor, Runnable private static final int CHECK_INTERVAL_IN_MS = 10 * 1000; private final @NotNull SentryOptions sentryOptions; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private int downsampleFactor = 0; - public BackpressureMonitor(final @NotNull SentryOptions sentryOptions, final @NotNull IHub hub) { + public BackpressureMonitor( + final @NotNull SentryOptions sentryOptions, final @NotNull IScopes scopes) { this.sentryOptions = sentryOptions; - this.hub = hub; + this.scopes = scopes; } @Override @@ -66,6 +67,6 @@ private void reschedule(final int delay) { } private boolean isHealthy() { - return hub.isHealthy(); + return scopes.isHealthy(); } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java b/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java index 956996ce04..52963413b6 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -28,8 +28,8 @@ final class FileIOSpanManager { private final @NotNull SentryStackTraceFactory stackTraceFactory; - static @Nullable ISpan startSpan(final @NotNull IHub hub, final @NotNull String op) { - final ISpan parent = Platform.isAndroid() ? hub.getTransaction() : hub.getSpan(); + static @Nullable ISpan startSpan(final @NotNull IScopes scopes, final @NotNull String op) { + final ISpan parent = Platform.isAndroid() ? scopes.getTransaction() : scopes.getSpan(); return parent != null ? parent.startChild(op) : null; } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java index 04bb87ae7c..ea7d7f09a5 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java @@ -1,8 +1,8 @@ package io.sentry.instrumentation.file; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; @@ -25,24 +25,24 @@ public final class SentryFileInputStream extends FileInputStream { private final @NotNull FileIOSpanManager spanManager; public SentryFileInputStream(final @Nullable String name) throws FileNotFoundException { - this(name != null ? new File(name) : null, HubAdapter.getInstance()); + this(name != null ? new File(name) : null, ScopesAdapter.getInstance()); } public SentryFileInputStream(final @Nullable File file) throws FileNotFoundException { - this(file, HubAdapter.getInstance()); + this(file, ScopesAdapter.getInstance()); } public SentryFileInputStream(final @NotNull FileDescriptor fdObj) { - this(fdObj, HubAdapter.getInstance()); + this(fdObj, ScopesAdapter.getInstance()); } - SentryFileInputStream(final @Nullable File file, final @NotNull IHub hub) + SentryFileInputStream(final @Nullable File file, final @NotNull IScopes scopes) throws FileNotFoundException { - this(init(file, null, hub)); + this(init(file, null, scopes)); } - SentryFileInputStream(final @NotNull FileDescriptor fdObj, final @NotNull IHub hub) { - this(init(fdObj, null, hub), fdObj); + SentryFileInputStream(final @NotNull FileDescriptor fdObj, final @NotNull IScopes scopes) { + this(init(fdObj, null, scopes), fdObj); } private SentryFileInputStream( @@ -60,24 +60,24 @@ private SentryFileInputStream(final @NotNull FileInputStreamInitData data) } private static FileInputStreamInitData init( - final @Nullable File file, @Nullable FileInputStream delegate, final @NotNull IHub hub) + final @Nullable File file, @Nullable FileInputStream delegate, final @NotNull IScopes scopes) throws FileNotFoundException { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.read"); + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.read"); if (delegate == null) { delegate = new FileInputStream(file); } - return new FileInputStreamInitData(file, span, delegate, hub.getOptions()); + return new FileInputStreamInitData(file, span, delegate, scopes.getOptions()); } private static FileInputStreamInitData init( final @NotNull FileDescriptor fd, @Nullable FileInputStream delegate, - final @NotNull IHub hub) { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.read"); + final @NotNull IScopes scopes) { + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.read"); if (delegate == null) { delegate = new FileInputStream(fd); } - return new FileInputStreamInitData(null, span, delegate, hub.getOptions()); + return new FileInputStreamInitData(null, span, delegate, scopes.getOptions()); } @Override @@ -128,25 +128,27 @@ public static FileInputStream create( final @NotNull FileInputStream delegate, final @Nullable String name) throws FileNotFoundException { return new SentryFileInputStream( - init(name != null ? new File(name) : null, delegate, HubAdapter.getInstance())); + init(name != null ? new File(name) : null, delegate, ScopesAdapter.getInstance())); } public static FileInputStream create( final @NotNull FileInputStream delegate, final @Nullable File file) throws FileNotFoundException { - return new SentryFileInputStream(init(file, delegate, HubAdapter.getInstance())); + return new SentryFileInputStream(init(file, delegate, ScopesAdapter.getInstance())); } public static FileInputStream create( final @NotNull FileInputStream delegate, final @NotNull FileDescriptor descriptor) { return new SentryFileInputStream( - init(descriptor, delegate, HubAdapter.getInstance()), descriptor); + init(descriptor, delegate, ScopesAdapter.getInstance()), descriptor); } static FileInputStream create( - final @NotNull FileInputStream delegate, final @Nullable File file, final @NotNull IHub hub) + final @NotNull FileInputStream delegate, + final @Nullable File file, + final @NotNull IScopes scopes) throws FileNotFoundException { - return new SentryFileInputStream(init(file, delegate, hub)); + return new SentryFileInputStream(init(file, delegate, scopes)); } } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java index 9424710d71..4ef5022e1c 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java @@ -1,8 +1,8 @@ package io.sentry.instrumentation.file; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -24,30 +24,31 @@ public final class SentryFileOutputStream extends FileOutputStream { private final @NotNull FileIOSpanManager spanManager; public SentryFileOutputStream(final @Nullable String name) throws FileNotFoundException { - this(name != null ? new File(name) : null, false, HubAdapter.getInstance()); + this(name != null ? new File(name) : null, false, ScopesAdapter.getInstance()); } public SentryFileOutputStream(final @Nullable String name, final boolean append) throws FileNotFoundException { - this(init(name != null ? new File(name) : null, append, null, HubAdapter.getInstance())); + this(init(name != null ? new File(name) : null, append, null, ScopesAdapter.getInstance())); } public SentryFileOutputStream(final @Nullable File file) throws FileNotFoundException { - this(file, false, HubAdapter.getInstance()); + this(file, false, ScopesAdapter.getInstance()); } public SentryFileOutputStream(final @Nullable File file, final boolean append) throws FileNotFoundException { - this(init(file, append, null, HubAdapter.getInstance())); + this(init(file, append, null, ScopesAdapter.getInstance())); } public SentryFileOutputStream(final @NotNull FileDescriptor fdObj) { - this(init(fdObj, null, HubAdapter.getInstance()), fdObj); + this(init(fdObj, null, ScopesAdapter.getInstance()), fdObj); } - SentryFileOutputStream(final @Nullable File file, final boolean append, final @NotNull IHub hub) + SentryFileOutputStream( + final @Nullable File file, final boolean append, final @NotNull IScopes scopes) throws FileNotFoundException { - this(init(file, append, null, hub)); + this(init(file, append, null, scopes)); } private SentryFileOutputStream( @@ -68,22 +69,24 @@ private static FileOutputStreamInitData init( final @Nullable File file, final boolean append, @Nullable FileOutputStream delegate, - @NotNull IHub hub) + @NotNull IScopes scopes) throws FileNotFoundException { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.write"); + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write"); if (delegate == null) { delegate = new FileOutputStream(file, append); } - return new FileOutputStreamInitData(file, append, span, delegate, hub.getOptions()); + return new FileOutputStreamInitData(file, append, span, delegate, scopes.getOptions()); } private static FileOutputStreamInitData init( - final @NotNull FileDescriptor fd, @Nullable FileOutputStream delegate, @NotNull IHub hub) { - final ISpan span = FileIOSpanManager.startSpan(hub, "file.write"); + final @NotNull FileDescriptor fd, + @Nullable FileOutputStream delegate, + @NotNull IScopes scopes) { + final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write"); if (delegate == null) { delegate = new FileOutputStream(fd); } - return new FileOutputStreamInitData(null, false, span, delegate, hub.getOptions()); + return new FileOutputStreamInitData(null, false, span, delegate, scopes.getOptions()); } @Override @@ -132,31 +135,32 @@ public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable String name) throws FileNotFoundException { return new SentryFileOutputStream( - init(name != null ? new File(name) : null, false, delegate, HubAdapter.getInstance())); + init(name != null ? new File(name) : null, false, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable String name, final boolean append) throws FileNotFoundException { return new SentryFileOutputStream( - init(name != null ? new File(name) : null, append, delegate, HubAdapter.getInstance())); + init( + name != null ? new File(name) : null, append, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable File file) throws FileNotFoundException { - return new SentryFileOutputStream(init(file, false, delegate, HubAdapter.getInstance())); + return new SentryFileOutputStream(init(file, false, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @Nullable File file, final boolean append) throws FileNotFoundException { - return new SentryFileOutputStream(init(file, append, delegate, HubAdapter.getInstance())); + return new SentryFileOutputStream(init(file, append, delegate, ScopesAdapter.getInstance())); } public static FileOutputStream create( final @NotNull FileOutputStream delegate, final @NotNull FileDescriptor fdObj) { - return new SentryFileOutputStream(init(fdObj, delegate, HubAdapter.getInstance()), fdObj); + return new SentryFileOutputStream(init(fdObj, delegate, ScopesAdapter.getInstance()), fdObj); } } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java index 0a225e65a5..38a83c7ff6 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -20,7 +20,8 @@ public SentryFileReader(final @NotNull FileDescriptor fd) { super(new SentryFileInputStream(fd)); } - SentryFileReader(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException { - super(new SentryFileInputStream(file, hub)); + SentryFileReader(final @NotNull File file, final @NotNull IScopes scopes) + throws FileNotFoundException { + super(new SentryFileInputStream(file, scopes)); } } diff --git a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java index 9588984612..93c901ec6c 100644 --- a/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java +++ b/sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file; -import io.sentry.IHub; +import io.sentry.IScopes; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; @@ -30,8 +30,8 @@ public SentryFileWriter(final @NotNull FileDescriptor fd) { super(new SentryFileOutputStream(fd)); } - SentryFileWriter(final @NotNull File file, final boolean append, final @NotNull IHub hub) + SentryFileWriter(final @NotNull File file, final boolean append, final @NotNull IScopes scopes) throws FileNotFoundException { - super(new SentryFileOutputStream(file, append, hub)); + super(new SentryFileOutputStream(file, append, scopes)); } } diff --git a/sentry/src/main/java/io/sentry/util/CheckInUtils.java b/sentry/src/main/java/io/sentry/util/CheckInUtils.java index e15603adaf..6719e24839 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -3,7 +3,7 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.MonitorConfig; import io.sentry.Sentry; import io.sentry.protocol.SentryId; @@ -30,18 +30,19 @@ public static U withCheckIn( final @Nullable MonitorConfig monitorConfig, final @NotNull Callable callable) throws Exception { - final @NotNull IHub hub = Sentry.getCurrentHub(); + final @NotNull IScopes scopes = Sentry.getCurrentScopes(); final long startTime = System.currentTimeMillis(); boolean didError = false; - hub.pushScope(); - TracingUtils.startNewTrace(hub); + // TODO fork instead + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); if (monitorConfig != null) { inProgressCheckIn.setMonitorConfig(monitorConfig); } - @Nullable SentryId checkInId = hub.captureCheckIn(inProgressCheckIn); + @Nullable SentryId checkInId = scopes.captureCheckIn(inProgressCheckIn); try { return callable.call(); } catch (Throwable t) { @@ -51,8 +52,8 @@ public static U withCheckIn( final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry/src/main/java/io/sentry/util/TracingUtils.java b/sentry/src/main/java/io/sentry/util/TracingUtils.java index 2aeb613f2d..67f6459660 100644 --- a/sentry/src/main/java/io/sentry/util/TracingUtils.java +++ b/sentry/src/main/java/io/sentry/util/TracingUtils.java @@ -2,8 +2,8 @@ import io.sentry.Baggage; import io.sentry.BaggageHeader; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.PropagationContext; import io.sentry.SentryOptions; @@ -14,8 +14,8 @@ public final class TracingUtils { - public static void startNewTrace(final @NotNull IHub hub) { - hub.configureScope( + public static void startNewTrace(final @NotNull IScopes scopes) { + scopes.configureScope( scope -> { scope.withPropagationContext( propagationContext -> { @@ -25,30 +25,30 @@ public static void startNewTrace(final @NotNull IHub hub) { } public static @Nullable TracingHeaders traceIfAllowed( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull String requestUrl, @Nullable List thirdPartyBaggageHeaders, final @Nullable ISpan span) { - final @NotNull SentryOptions sentryOptions = hub.getOptions(); + final @NotNull SentryOptions sentryOptions = scopes.getOptions(); if (sentryOptions.isTraceSampling() && shouldAttachTracingHeaders(requestUrl, sentryOptions)) { - return trace(hub, thirdPartyBaggageHeaders, span); + return trace(scopes, thirdPartyBaggageHeaders, span); } return null; } public static @Nullable TracingHeaders trace( - final @NotNull IHub hub, + final @NotNull IScopes scopes, @Nullable List thirdPartyBaggageHeaders, final @Nullable ISpan span) { - final @NotNull SentryOptions sentryOptions = hub.getOptions(); + final @NotNull SentryOptions sentryOptions = scopes.getOptions(); if (span != null && !span.isNoOp()) { return new TracingHeaders( span.toSentryTrace(), span.toBaggageHeader(thirdPartyBaggageHeaders)); } else { final @NotNull PropagationContextHolder returnValue = new PropagationContextHolder(); - hub.configureScope( + scopes.configureScope( (scope) -> { returnValue.propagationContext = maybeUpdateBaggage(scope, sentryOptions); }); diff --git a/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt b/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt index c0445efb23..1416fbbe3f 100644 --- a/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/DefaultTransactionPerformanceCollectorTest.kt @@ -33,7 +33,7 @@ class DefaultTransactionPerformanceCollectorTest { private class Fixture { lateinit var transaction1: ITransaction lateinit var transaction2: ITransaction - val hub: IHub = mock() + val scopes: IScopes = mock() val options = SentryOptions() var mockTimer: Timer? = null val deferredExecutorService = DeferredExecutorService() @@ -47,7 +47,7 @@ class DefaultTransactionPerformanceCollectorTest { } init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(memoryCollector: IPerformanceSnapshotCollector? = JavaMemoryCollector(), cpuCollector: IPerformanceSnapshotCollector? = mockCpuCollector, executorService: ISentryExecutorService = deferredExecutorService): TransactionPerformanceCollector { @@ -59,8 +59,8 @@ class DefaultTransactionPerformanceCollectorTest { if (memoryCollector != null) { options.addPerformanceCollector(memoryCollector) } - transaction1 = SentryTracer(TransactionContext("", ""), hub) - transaction2 = SentryTracer(TransactionContext("", ""), hub) + transaction1 = SentryTracer(TransactionContext("", ""), scopes) + transaction2 = SentryTracer(TransactionContext("", ""), scopes) val collector = DefaultTransactionPerformanceCollector(options) val timer: Timer = collector.getProperty("timer") ?: Timer(true) mockTimer = spy(timer) diff --git a/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt b/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt index e87f4256d5..0507b8499d 100644 --- a/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt +++ b/sentry/src/test/java/io/sentry/DirectoryProcessorTest.kt @@ -27,7 +27,7 @@ class DirectoryProcessorTest { private class Fixture { - var hub: IHub = mock() + var scopes: IScopes = mock() var envelopeReader: IEnvelopeReader = mock() var serializer: ISerializer = mock() var logger: ILogger = mock() @@ -40,7 +40,7 @@ class DirectoryProcessorTest { fun getSut(isRetryable: Boolean = false, isRateLimitingActive: Boolean = false): OutboxSender { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).then { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).then { HintUtils.runIfHasType( hintCaptor.firstValue, Enqueable::class.java @@ -52,7 +52,7 @@ class DirectoryProcessorTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(hub.rateLimiter).thenReturn(rateLimiter) + whenever(scopes.rateLimiter).thenReturn(rateLimiter) } } HintUtils.runIfHasType( @@ -62,7 +62,7 @@ class DirectoryProcessorTest { retryable.isRetry = isRetryable } } - return OutboxSender(hub, envelopeReader, serializer, logger, 500, 30) + return OutboxSender(scopes, envelopeReader, serializer, logger, 500, 30) } } @@ -91,7 +91,7 @@ class DirectoryProcessorTest { whenever(fixture.serializer.deserialize(any(), eq(SentryEvent::class.java))).thenReturn(event) fixture.getSut().processDirectory(file) - verify(fixture.hub).captureEvent(any(), argWhere { !HintUtils.hasType(it, ApplyScopeData::class.java) }) + verify(fixture.scopes).captureEvent(any(), argWhere { !HintUtils.hasType(it, ApplyScopeData::class.java) }) } @Test @@ -100,7 +100,7 @@ class DirectoryProcessorTest { dir.mkdirs() assertTrue(dir.exists()) // sanity check fixture.getSut().processDirectory(file) - verify(fixture.hub, never()).captureEnvelope(any(), any()) + verify(fixture.scopes, never()).captureEnvelope(any(), any()) } @Test @@ -121,7 +121,7 @@ class DirectoryProcessorTest { sut.processDirectory(file) // should only capture once - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @Test @@ -139,7 +139,7 @@ class DirectoryProcessorTest { sut.processDirectory(file) // should only capture once - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } private fun getTempEnvelope(fileName: String): String { diff --git a/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt b/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt index 6f0ea9cb8a..d63ee81854 100644 --- a/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt +++ b/sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt @@ -23,7 +23,7 @@ import kotlin.test.assertFalse class EnvelopeSenderTest { private class Fixture { - var hub: IHub? = mock() + var scopes: IScopes? = mock() var logger: ILogger? = mock() var serializer: ISerializer? = mock() var options = SentryOptions().noFlushTimeout() @@ -35,7 +35,7 @@ class EnvelopeSenderTest { fun getSut(): EnvelopeSender { return EnvelopeSender( - hub!!, + scopes!!, serializer!!, logger!!, options.flushTimeoutMillis, @@ -62,7 +62,7 @@ class EnvelopeSenderTest { val sut = fixture.getSut() sut.processDirectory(File("i don't exist")) verify(fixture.logger)!!.log(eq(SentryLevel.WARNING), eq("Directory '%s' doesn't exist. No cached events to send."), any()) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -72,7 +72,7 @@ class EnvelopeSenderTest { testFile.deleteOnExit() sut.processDirectory(testFile) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq("Cache dir %s is not a directory."), any()) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -82,11 +82,11 @@ class EnvelopeSenderTest { sut.processDirectory(File(tempDirectory.toUri())) testFile.deleteOnExit() verify(fixture.logger)!!.log(eq(SentryLevel.DEBUG), eq("File '%s' doesn't match extension expected."), any()) - verify(fixture.hub, never())!!.captureEnvelope(any(), anyOrNull()) + verify(fixture.scopes, never())!!.captureEnvelope(any(), anyOrNull()) } @Test - fun `when directory has event files, processDirectory captures with hub`() { + fun `when directory has event files, processDirectory captures with scopes`() { val event = SentryEvent() val envelope = SentryEnvelope.from(fixture.serializer!!, event, null) whenever(fixture.serializer!!.deserializeEnvelope(any())).thenReturn(envelope) @@ -94,7 +94,7 @@ class EnvelopeSenderTest { val testFile = File(Files.createTempFile(tempDirectory, "send-cached-event-test", EnvelopeCache.SUFFIX_ENVELOPE_FILE).toUri()) testFile.deleteOnExit() sut.processDirectory(File(tempDirectory.toUri())) - verify(fixture.hub)!!.captureEnvelope(eq(envelope), any()) + verify(fixture.scopes)!!.captureEnvelope(eq(envelope), any()) } @Test @@ -108,12 +108,12 @@ class EnvelopeSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processFile(testFile, hints) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq(expected), eq("Failed to capture cached envelope %s"), eq(testFile.absolutePath)) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) assertFalse(testFile.exists()) } @Test - fun `when hub throws, file gets deleted`() { + fun `when scopes throws, file gets deleted`() { val expected = RuntimeException() whenever(fixture.serializer!!.deserializeEnvelope(any())).doThrow(expected) val sut = fixture.getSut() @@ -121,6 +121,6 @@ class EnvelopeSenderTest { testFile.deleteOnExit() sut.processFile(testFile, Hint()) verify(fixture.logger)!!.log(eq(SentryLevel.ERROR), eq(expected), eq("Failed to capture cached envelope %s"), eq(testFile.absolutePath)) - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } } diff --git a/sentry/src/test/java/io/sentry/HubAdapterTest.kt b/sentry/src/test/java/io/sentry/HubAdapterTest.kt index 9686250d20..0e7e1d0f77 100644 --- a/sentry/src/test/java/io/sentry/HubAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/HubAdapterTest.kt @@ -13,11 +13,11 @@ import kotlin.test.Test class HubAdapterTest { - val hub: Hub = mock() + val scopes: IScopes = mock() @BeforeTest fun `set up`() { - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) } @AfterTest @@ -27,7 +27,7 @@ class HubAdapterTest { @Test fun `isEnabled calls Hub`() { HubAdapter.getInstance().isEnabled - verify(hub).isEnabled + verify(scopes).isEnabled } @Test fun `captureEvent calls Hub`() { @@ -35,27 +35,27 @@ class HubAdapterTest { val hint = mock() val scopeCallback = mock() HubAdapter.getInstance().captureEvent(event, hint) - verify(hub).captureEvent(eq(event), eq(hint)) + verify(scopes).captureEvent(eq(event), eq(hint)) HubAdapter.getInstance().captureEvent(event, hint, scopeCallback) - verify(hub).captureEvent(eq(event), eq(hint), eq(scopeCallback)) + verify(scopes).captureEvent(eq(event), eq(hint), eq(scopeCallback)) } @Test fun `captureMessage calls Hub`() { val scopeCallback = mock() val sentryLevel = mock() HubAdapter.getInstance().captureMessage("message", sentryLevel) - verify(hub).captureMessage(eq("message"), eq(sentryLevel)) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel)) HubAdapter.getInstance().captureMessage("message", sentryLevel, scopeCallback) - verify(hub).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) } @Test fun `captureEnvelope calls Hub`() { val envelope = mock() val hint = mock() HubAdapter.getInstance().captureEnvelope(envelope, hint) - verify(hub).captureEnvelope(eq(envelope), eq(hint)) + verify(scopes).captureEnvelope(eq(envelope), eq(hint)) } @Test fun `captureException calls Hub`() { @@ -63,145 +63,145 @@ class HubAdapterTest { val hint = mock() val scopeCallback = mock() HubAdapter.getInstance().captureException(throwable, hint) - verify(hub).captureException(eq(throwable), eq(hint)) + verify(scopes).captureException(eq(throwable), eq(hint)) HubAdapter.getInstance().captureException(throwable, hint, scopeCallback) - verify(hub).captureException(eq(throwable), eq(hint), eq(scopeCallback)) + verify(scopes).captureException(eq(throwable), eq(hint), eq(scopeCallback)) } @Test fun `captureUserFeedback calls Hub`() { val userFeedback = mock() HubAdapter.getInstance().captureUserFeedback(userFeedback) - verify(hub).captureUserFeedback(eq(userFeedback)) + verify(scopes).captureUserFeedback(eq(userFeedback)) } @Test fun `captureCheckIn calls Hub`() { val checkIn = mock() HubAdapter.getInstance().captureCheckIn(checkIn) - verify(hub).captureCheckIn(eq(checkIn)) + verify(scopes).captureCheckIn(eq(checkIn)) } @Test fun `startSession calls Hub`() { HubAdapter.getInstance().startSession() - verify(hub).startSession() + verify(scopes).startSession() } @Test fun `endSession calls Hub`() { HubAdapter.getInstance().endSession() - verify(hub).endSession() + verify(scopes).endSession() } @Test fun `close calls Hub`() { HubAdapter.getInstance().close() - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `close with isRestarting true calls Hub with isRestarting false`() { HubAdapter.getInstance().close(true) - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `close with isRestarting false calls Hub with isRestarting false`() { HubAdapter.getInstance().close(false) - verify(hub).close(false) + verify(scopes).close(false) } @Test fun `addBreadcrumb calls Hub`() { val breadcrumb = mock() val hint = mock() HubAdapter.getInstance().addBreadcrumb(breadcrumb, hint) - verify(hub).addBreadcrumb(eq(breadcrumb), eq(hint)) + verify(scopes).addBreadcrumb(eq(breadcrumb), eq(hint)) } @Test fun `setLevel calls Hub`() { val sentryLevel = mock() HubAdapter.getInstance().setLevel(sentryLevel) - verify(hub).setLevel(eq(sentryLevel)) + verify(scopes).setLevel(eq(sentryLevel)) } @Test fun `setTransaction calls Hub`() { HubAdapter.getInstance().setTransaction("transaction") - verify(hub).setTransaction(eq("transaction")) + verify(scopes).setTransaction(eq("transaction")) } @Test fun `setUser calls Hub`() { val user = mock() HubAdapter.getInstance().setUser(user) - verify(hub).setUser(eq(user)) + verify(scopes).setUser(eq(user)) } @Test fun `setFingerprint calls Hub`() { val fingerprint = ArrayList() HubAdapter.getInstance().setFingerprint(fingerprint) - verify(hub).setFingerprint(eq(fingerprint)) + verify(scopes).setFingerprint(eq(fingerprint)) } @Test fun `clearBreadcrumbs calls Hub`() { HubAdapter.getInstance().clearBreadcrumbs() - verify(hub).clearBreadcrumbs() + verify(scopes).clearBreadcrumbs() } @Test fun `setTag calls Hub`() { HubAdapter.getInstance().setTag("key", "value") - verify(hub).setTag(eq("key"), eq("value")) + verify(scopes).setTag(eq("key"), eq("value")) } @Test fun `removeTag calls Hub`() { HubAdapter.getInstance().removeTag("key") - verify(hub).removeTag(eq("key")) + verify(scopes).removeTag(eq("key")) } @Test fun `setExtra calls Hub`() { HubAdapter.getInstance().setExtra("key", "value") - verify(hub).setExtra(eq("key"), eq("value")) + verify(scopes).setExtra(eq("key"), eq("value")) } @Test fun `removeExtra calls Hub`() { HubAdapter.getInstance().removeExtra("key") - verify(hub).removeExtra(eq("key")) + verify(scopes).removeExtra(eq("key")) } @Test fun `getLastEventId calls Hub`() { HubAdapter.getInstance().lastEventId - verify(hub).lastEventId + verify(scopes).lastEventId } @Test fun `pushScope calls Hub`() { HubAdapter.getInstance().pushScope() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `popScope calls Hub`() { HubAdapter.getInstance().popScope() - verify(hub).popScope() + verify(scopes).popScope() } @Test fun `withScope calls Hub`() { val scopeCallback = mock() HubAdapter.getInstance().withScope(scopeCallback) - verify(hub).withScope(eq(scopeCallback)) + verify(scopes).withScope(eq(scopeCallback)) } @Test fun `configureScope calls Hub`() { val scopeCallback = mock() HubAdapter.getInstance().configureScope(scopeCallback) - verify(hub).configureScope(eq(scopeCallback)) + verify(scopes).configureScope(eq(scopeCallback)) } @Test fun `bindClient calls Hub`() { val client = mock() HubAdapter.getInstance().bindClient(client) - verify(hub).bindClient(eq(client)) + verify(scopes).bindClient(eq(client)) } @Test fun `flush calls Hub`() { HubAdapter.getInstance().flush(1) - verify(hub).flush(eq(1)) + verify(scopes).flush(eq(1)) } @Test fun `clone calls Hub`() { HubAdapter.getInstance().clone() - verify(hub).clone() + verify(scopes).clone() } @Test fun `captureTransaction calls Hub`() { @@ -210,7 +210,7 @@ class HubAdapterTest { val hint = mock() val profilingTraceData = mock() HubAdapter.getInstance().captureTransaction(transaction, traceContext, hint, profilingTraceData) - verify(hub).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) + verify(scopes).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) } @Test fun `startTransaction calls Hub`() { @@ -218,48 +218,48 @@ class HubAdapterTest { val samplingContext = mock() val transactionOptions = mock() HubAdapter.getInstance().startTransaction(transactionContext) - verify(hub).startTransaction(eq(transactionContext), any()) + verify(scopes).startTransaction(eq(transactionContext), any()) - reset(hub) + reset(scopes) HubAdapter.getInstance().startTransaction(transactionContext, transactionOptions) - verify(hub).startTransaction(eq(transactionContext), eq(transactionOptions)) + verify(scopes).startTransaction(eq(transactionContext), eq(transactionOptions)) } @Test fun `traceHeaders calls Hub`() { HubAdapter.getInstance().traceHeaders() - verify(hub).traceHeaders() + verify(scopes).traceHeaders() } @Test fun `setSpanContext calls Hub`() { val throwable = mock() val span = mock() HubAdapter.getInstance().setSpanContext(throwable, span, "transactionName") - verify(hub).setSpanContext(eq(throwable), eq(span), eq("transactionName")) + verify(scopes).setSpanContext(eq(throwable), eq(span), eq("transactionName")) } @Test fun `getSpan calls Hub`() { HubAdapter.getInstance().span - verify(hub).span + verify(scopes).span } @Test fun `getTransaction calls Hub`() { HubAdapter.getInstance().transaction - verify(hub).transaction + verify(scopes).transaction } @Test fun `getOptions calls Hub`() { HubAdapter.getInstance().options - verify(hub).options + verify(scopes).options } @Test fun `isCrashedLastRun calls Hub`() { HubAdapter.getInstance().isCrashedLastRun - verify(hub).isCrashedLastRun + verify(scopes).isCrashedLastRun } @Test fun `reportFullyDisplayed calls Hub`() { HubAdapter.getInstance().reportFullyDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } } diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt index 8fac963e70..f30fd0a966 100644 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ b/sentry/src/test/java/io/sentry/HubTest.kt @@ -72,7 +72,7 @@ class HubTest { } @Test - fun `when hub is cloned, integrations are not registered`() { + fun `when scopes is cloned, integrations are not registered`() { val integrationMock = mock() val options = SentryOptions() options.cacheDirPath = file.absolutePath @@ -80,36 +80,36 @@ class HubTest { options.setSerializer(mock()) options.addIntegration(integrationMock) // val expected = HubAdapter.getInstance() - val hub = Hub(options) + val scopes = Hub(options) // verify(integrationMock).register(expected, options) - hub.clone() + scopes.clone() verifyNoMoreInteractions(integrationMock) } @Test - fun `when hub is cloned, scope changes are isolated`() { + fun `when scopes is cloned, scope changes are isolated`() { val options = SentryOptions() options.cacheDirPath = file.absolutePath options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) - val hub = Hub(options) + val scopes = Hub(options) var firstScope: IScope? = null - hub.configureScope { + scopes.configureScope { firstScope = it - it.setTag("hub", "a") + it.setTag("scopes", "a") } var cloneScope: IScope? = null - val clone = hub.clone() + val clone = scopes.clone() clone.configureScope { cloneScope = it - it.setTag("hub", "b") + it.setTag("scopes", "b") } - assertEquals("a", firstScope!!.tags["hub"]) - assertEquals("b", cloneScope!!.tags["hub"]) + assertEquals("a", firstScope!!.tags["scopes"]) + assertEquals("b", cloneScope!!.tags["scopes"]) } @Test - fun `when hub is initialized, breadcrumbs are capped as per options`() { + fun `when scopes is initialized, breadcrumbs are capped as per options`() { val options = SentryOptions() options.cacheDirPath = file.absolutePath options.maxBreadcrumbs = 5 @@ -288,7 +288,7 @@ class HubTest { } @Test - fun `when captureEvent is called on disabled hub, lastEventId does not get overwritten`() { + fun `when captureEvent is called on disabled scopes, lastEventId does not get overwritten`() { val (sut, mockClient) = getEnabledHub() whenever(mockClient.captureEvent(any(), any(), anyOrNull())).thenReturn(SentryId(UUID.randomUUID())) val event = SentryEvent() @@ -827,14 +827,14 @@ class HubTest { @Test fun `when withScope throws an exception then it should be caught`() { - val (hub, _, logger) = getEnabledHub() + val (scopes, _, logger) = getEnabledHub() val exception = Exception("scope callback exception") val scopeCallback = ScopeCallback { throw exception } - hub.withScope(scopeCallback) + scopes.withScope(scopeCallback) verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) } @@ -864,25 +864,25 @@ class HubTest { @Test fun `when configureScope throws an exception then it should be caught`() { - val (hub, _, logger) = getEnabledHub() + val (scopes, _, logger) = getEnabledHub() val exception = Exception("scope callback exception") val scopeCallback = ScopeCallback { throw exception } - hub.configureScope(scopeCallback) + scopes.configureScope(scopeCallback) verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) } //endregion @Test - fun `when integration is registered, hub is enabled`() { + fun `when integration is registered, scopes is enabled`() { val mock = mock() var options: SentryOptions? = null - // init main hub and make it enabled + // init main scopes and make it enabled Sentry.init { it.addIntegration(mock) it.dsn = "https://key@sentry.io/proj" @@ -892,8 +892,8 @@ class HubTest { } doAnswer { - val hub = it.arguments[0] as IHub - assertTrue(hub.isEnabled) + val scopes = it.arguments[0] as IScopes + assertTrue(scopes.isEnabled) }.whenever(mock).register(any(), eq(options!!)) verify(mock).register(any(), eq(options!!)) @@ -902,26 +902,26 @@ class HubTest { //region setLevel tests @Test fun `when setLevel is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setLevel(SentryLevel.INFO) + scopes.setLevel(SentryLevel.INFO) assertNull(scope?.level) } @Test fun `when setLevel is called, level is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setLevel(SentryLevel.INFO) + scopes.setLevel(SentryLevel.INFO) assertEquals(SentryLevel.INFO, scope?.level) } //endregion @@ -929,74 +929,74 @@ class HubTest { //region setTransaction tests @Test fun `when setTransaction is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setTransaction("test") + scopes.setTransaction("test") assertNull(scope?.transactionName) } @Test fun `when setTransaction is called, and transaction is not set, transaction name is changed`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setTransaction("test") + scopes.setTransaction("test") assertEquals("test", scope?.transactionName) } @Test fun `when setTransaction is called, and transaction is set, transaction name is changed`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - val tx = hub.startTransaction("test", "op") - hub.configureScope { it.setTransaction(tx) } + val tx = scopes.startTransaction("test", "op") + scopes.configureScope { it.setTransaction(tx) } assertEquals("test", scope?.transactionName) } @Test fun `when startTransaction is called with different instrumenter, no-op is returned`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } val transactionOptions = TransactionOptions() - val tx = hub.startTransaction(transactionContext, transactionOptions) + val tx = scopes.startTransaction(transactionContext, transactionOptions) assertTrue(tx is NoOpTransaction) } @Test fun `when startTransaction is called with different instrumenter, no-op is returned 2`() { - val hub = generateHub() { + val scopes = generateHub() { it.instrumenter = Instrumenter.OTEL } - val tx = hub.startTransaction("test", "op") + val tx = scopes.startTransaction("test", "op") assertTrue(tx is NoOpTransaction) } @Test fun `when startTransaction is called with configured instrumenter, it works`() { - val hub = generateHub() { + val scopes = generateHub() { it.instrumenter = Instrumenter.OTEL } val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } val transactionOptions = TransactionOptions() - val tx = hub.startTransaction(transactionContext, transactionOptions) + val tx = scopes.startTransaction(transactionContext, transactionOptions) assertFalse(tx is NoOpTransaction) } @@ -1005,27 +1005,27 @@ class HubTest { //region setUser tests @Test fun `when setUser is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setUser(User()) + scopes.setUser(User()) assertNull(scope?.user) } @Test fun `when setUser is called, user is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } val user = User() - hub.setUser(user) + scopes.setUser(user) assertEquals(user, scope?.user) } //endregion @@ -1033,40 +1033,40 @@ class HubTest { //region setFingerprint tests @Test fun `when setFingerprint is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() val fingerprint = listOf("abc") - hub.setFingerprint(fingerprint) + scopes.setFingerprint(fingerprint) assertEquals(0, scope?.fingerprint?.count()) } @Test fun `when setFingerprint is called with null parameter, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setFingerprint", List::class.java, null) + scopes.callMethod("setFingerprint", List::class.java, null) assertEquals(0, scope?.fingerprint?.count()) } @Test fun `when setFingerprint is called, fingerprint is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } val fingerprint = listOf("abc") - hub.setFingerprint(fingerprint) + scopes.setFingerprint(fingerprint) assertEquals(1, scope?.fingerprint?.count()) } //endregion @@ -1074,30 +1074,30 @@ class HubTest { //region clearBreadcrumbs tests @Test fun `when clearBreadcrumbs is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.addBreadcrumb(Breadcrumb()) + scopes.addBreadcrumb(Breadcrumb()) assertEquals(1, scope?.breadcrumbs?.count()) - hub.close() + scopes.close() assertEquals(0, scope?.breadcrumbs?.count()) } @Test fun `when clearBreadcrumbs is called, clear breadcrumbs`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.addBreadcrumb(Breadcrumb()) + scopes.addBreadcrumb(Breadcrumb()) assertEquals(1, scope?.breadcrumbs?.count()) - hub.clearBreadcrumbs() + scopes.clearBreadcrumbs() assertEquals(0, scope?.breadcrumbs?.count()) } //endregion @@ -1105,38 +1105,38 @@ class HubTest { //region setTag tests @Test fun `when setTag is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setTag("test", "test") + scopes.setTag("test", "test") assertEquals(0, scope?.tags?.count()) } @Test fun `when setTag is called with null parameters, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + scopes.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) assertEquals(0, scope?.tags?.count()) } @Test fun `when setTag is called, tag is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setTag("test", "test") + scopes.setTag("test", "test") assertEquals(1, scope?.tags?.count()) } //endregion @@ -1144,38 +1144,38 @@ class HubTest { //region setExtra tests @Test fun `when setExtra is called on disabled client, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.close() + scopes.close() - hub.setExtra("test", "test") + scopes.setExtra("test", "test") assertEquals(0, scope?.extras?.count()) } @Test fun `when setExtra is called with null parameters, do nothing`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + scopes.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) assertEquals(0, scope?.extras?.count()) } @Test fun `when setExtra is called, extra is set`() { - val hub = generateHub() + val scopes = generateHub() var scope: IScope? = null - hub.configureScope { + scopes.configureScope { scope = it } - hub.setExtra("test", "test") + scopes.setExtra("test", "test") assertEquals(1, scope?.extras?.count()) } //endregion @@ -1488,19 +1488,19 @@ class HubTest { val mockTransactionProfiler = mock() val mockClient = mock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val hub = generateHub { + val scopes = generateHub { it.setTransactionProfiler(mockTransactionProfiler) } - hub.bindClient(mockClient) + scopes.bindClient(mockClient) // Transaction is not sampled, so it should not be profiled val contexts = TransactionContext("name", "op", TracesSamplingDecision(false, null, true, null)) - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) transaction.finish() verify(mockClient, never()).captureEnvelope(any()) // Transaction is sampled, so it should be profiled val sampledContexts = TransactionContext("name", "op", TracesSamplingDecision(true, null, true, null)) - val sampledTransaction = hub.startTransaction(sampledContexts) + val sampledTransaction = scopes.startTransaction(sampledContexts) sampledTransaction.finish() verify(mockClient).captureEnvelope(any()) } @@ -1510,13 +1510,13 @@ class HubTest { val mockTransactionProfiler = mock() val mockClient = mock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 0.0 it.setTransactionProfiler(mockTransactionProfiler) } - hub.bindClient(mockClient) + scopes.bindClient(mockClient) val contexts = TransactionContext("name", "op") - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) transaction.finish() verify(mockClient, never()).captureEnvelope(any()) } @@ -1525,12 +1525,12 @@ class HubTest { fun `when profiler is running and isAppStartTransaction is false, startTransaction does not interact with profiler`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) verify(mockTransactionProfiler, never()).start() verify(mockTransactionProfiler, never()).bindTransaction(any()) } @@ -1539,12 +1539,12 @@ class HubTest { fun `when profiler is running and isAppStartTransaction is true, startTransaction binds current profile`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - val transaction = hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) verify(mockTransactionProfiler, never()).start() verify(mockTransactionProfiler).bindTransaction(eq(transaction)) } @@ -1553,12 +1553,12 @@ class HubTest { fun `when profiler is not running, startTransaction starts and binds current profile`() { val mockTransactionProfiler = mock() whenever(mockTransactionProfiler.isRunning).thenReturn(false) - val hub = generateHub { + val scopes = generateHub { it.profilesSampleRate = 1.0 it.setTransactionProfiler(mockTransactionProfiler) } val context = TransactionContext("name", "op") - val transaction = hub.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) verify(mockTransactionProfiler).start() verify(mockTransactionProfiler).bindTransaction(eq(transaction)) } @@ -1567,75 +1567,75 @@ class HubTest { //region startTransaction tests @Test fun `when startTransaction, creates transaction`() { - val hub = generateHub() + val scopes = generateHub() val contexts = TransactionContext("name", "op") - val transaction = hub.startTransaction(contexts) + val transaction = scopes.startTransaction(contexts) assertTrue(transaction is SentryTracer) assertEquals(contexts, transaction.root.spanContext) } @Test fun `when startTransaction with bindToScope set to false, transaction is not attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - hub.startTransaction("name", "op", TransactionOptions()) + scopes.startTransaction("name", "op", TransactionOptions()) - hub.configureScope { + scopes.configureScope { assertNull(it.span) } } @Test fun `when startTransaction without bindToScope set, transaction is not attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - hub.startTransaction("name", "op") + scopes.startTransaction("name", "op") - hub.configureScope { + scopes.configureScope { assertNull(it.span) } } @Test fun `when startTransaction with bindToScope set to true, transaction is attached to the scope`() { - val hub = generateHub() + val scopes = generateHub() - val transaction = hub.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) + val transaction = scopes.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) - hub.configureScope { + scopes.configureScope { assertEquals(transaction, it.span) } } @Test fun `when startTransaction and no tracing sampling is configured, event is not sampled`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = 0.0 } - val transaction = hub.startTransaction("name", "op") + val transaction = scopes.startTransaction("name", "op") assertFalse(transaction.isSampled!!) } @Test fun `when startTransaction and no profile sampling is configured, profile is not sampled`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = 1.0 it.profilesSampleRate = 0.0 } - val transaction = hub.startTransaction("name", "op") + val transaction = scopes.startTransaction("name", "op") assertTrue(transaction.isSampled!!) assertFalse(transaction.isProfileSampled!!) } @Test fun `when startTransaction with parent sampled and no traces sampler provided, transaction inherits sampling decision`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("name", "op") transactionContext.parentSampled = true - val transaction = hub.startTransaction(transactionContext) + val transaction = scopes.startTransaction(transactionContext) assertNotNull(transaction) assertNotNull(transaction.isSampled) assertTrue(transaction.isSampled!!) @@ -1643,10 +1643,10 @@ class HubTest { @Test fun `when startTransaction with parent profile sampled and no profile sampler provided, transaction inherits profile sampling decision`() { - val hub = generateHub() + val scopes = generateHub() val transactionContext = TransactionContext("name", "op") transactionContext.setParentSampled(true, true) - val transaction = hub.startTransaction(transactionContext) + val transaction = scopes.startTransaction(transactionContext) assertTrue(transaction.isProfileSampled!!) } @@ -1717,11 +1717,11 @@ class HubTest { @Test fun `when tracesSampleRate and tracesSampler are not set on SentryOptions, startTransaction returns NoOp`() { - val hub = generateHub { + val scopes = generateHub { it.tracesSampleRate = null it.tracesSampler = null } - val transaction = hub.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) + val transaction = scopes.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) assertTrue(transaction is NoOpTransaction) } //endregion @@ -1729,81 +1729,81 @@ class HubTest { //region startTransaction tests @Test fun `when traceHeaders and no transaction is active, traceHeaders are generated from scope`() { - val hub = generateHub() + val scopes = generateHub() var spanId: SpanId? = null - hub.configureScope { spanId = it.propagationContext.spanId } + scopes.configureScope { spanId = it.propagationContext.spanId } - val traceHeader = hub.traceHeaders() + val traceHeader = scopes.traceHeaders() assertNotNull(traceHeader) assertEquals(spanId, traceHeader.spanId) } @Test fun `when traceHeaders and there is an active transaction, traceHeaders are not null`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.setTransaction(tx) } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } - assertNotNull(hub.traceHeaders()) + assertNotNull(scopes.traceHeaders()) } //endregion //region getSpan tests @Test fun `when there is no active transaction, getSpan returns null`() { - val hub = generateHub() - assertNull(hub.span) + val scopes = generateHub() + assertNull(scopes.span) } @Test fun `when there is no active transaction, getTransaction returns null`() { - val hub = generateHub() - assertNull(hub.transaction) + val scopes = generateHub() + assertNull(scopes.transaction) } @Test fun `when there is active transaction bound to the scope, getTransaction and getSpan return active transaction`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.transaction = tx } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.transaction = tx } - assertEquals(tx, hub.transaction) - assertEquals(tx, hub.span) + assertEquals(tx, scopes.transaction) + assertEquals(tx, scopes.span) } @Test - fun `when there is a transaction but the hub is closed, getTransaction returns null`() { - val hub = generateHub() - hub.startTransaction("name", "op") - hub.close() + fun `when there is a transaction but the scopes is closed, getTransaction returns null`() { + val scopes = generateHub() + scopes.startTransaction("name", "op") + scopes.close() - assertNull(hub.transaction) + assertNull(scopes.transaction) } @Test fun `when there is active span within a transaction bound to the scope, getSpan returns active span`() { - val hub = generateHub() - val tx = hub.startTransaction("aTransaction", "op") - hub.configureScope { it.setTransaction(tx) } - hub.configureScope { it.setTransaction(tx) } + val scopes = generateHub() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } + scopes.configureScope { it.setTransaction(tx) } val span = tx.startChild("op") - assertEquals(tx, hub.transaction) - assertEquals(span, hub.span) + assertEquals(tx, scopes.transaction) + assertEquals(span, scopes.span) } // endregion //region setSpanContext @Test fun `associates span context with throwable`() { - val (hub, mockClient) = getEnabledHub() - val transaction = hub.startTransaction("aTransaction", "op") + val (scopes, mockClient) = getEnabledHub() + val transaction = scopes.startTransaction("aTransaction", "op") val span = transaction.startChild("op") val exception = RuntimeException() - hub.setSpanContext(exception, span, "tx-name") - hub.captureEvent(SentryEvent(exception)) + scopes.setSpanContext(exception, span, "tx-name") + scopes.captureEvent(SentryEvent(exception)) verify(mockClient).captureEvent( check { @@ -1816,8 +1816,8 @@ class HubTest { @Test fun `returns null when no span context associated with throwable`() { - val hub = generateHub() as Hub - assertNull(hub.getSpanContext(RuntimeException())) + val scopes = generateHub() as Hub + assertNull(scopes.getSpanContext(RuntimeException())) } // endregion @@ -1826,9 +1826,9 @@ class HubTest { val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) nativeMarker.mkdirs() nativeMarker.createNewFile() - val hub = generateHub() as Hub + val scopes = generateHub() as Hub - assertTrue(hub.isCrashedLastRun!!) + assertTrue(scopes.isCrashedLastRun!!) assertTrue(nativeMarker.exists()) } @@ -1837,69 +1837,69 @@ class HubTest { val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) nativeMarker.mkdirs() nativeMarker.createNewFile() - val hub = generateHub { + val scopes = generateHub { it.isEnableAutoSessionTracking = false } - assertTrue(hub.isCrashedLastRun!!) + assertTrue(scopes.isCrashedLastRun!!) assertFalse(nativeMarker.exists()) } @Test fun `reportFullyDisplayed is ignored if TimeToFullDisplayTracing is disabled`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertFalse(called) } @Test fun `reportFullyDisplayed calls FullyDisplayedReporter if TimeToFullDisplayTracing is enabled`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.isEnableTimeToFullDisplayTracing = true it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) } @Test fun `reportFullyDisplayed calls FullyDisplayedReporter only once`() { var called = false - val hub = generateHub { + val scopes = generateHub { it.isEnableTimeToFullDisplayTracing = true it.fullyDisplayedReporter.registerFullyDrawnListener { called = !called } } - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) - hub.reportFullyDisplayed() + scopes.reportFullyDisplayed() assertTrue(called) } @Test fun `reportFullDisplayed calls reportFullyDisplayed`() { - val hub = spy(generateHub()) - hub.reportFullDisplayed() - verify(hub).reportFullyDisplayed() + val scopes = spy(generateHub()) + scopes.reportFullDisplayed() + verify(scopes).reportFullyDisplayed() } @Test fun `continueTrace creates propagation context from headers and returns transaction context if performance enabled`() { - val hub = generateHub() + val scopes = generateHub() val traceId = SentryId() val parentSpanId = SpanId() - val transactionContext = hub.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertEquals(traceId, scope.propagationContext.traceId) assertEquals(parentSpanId, scope.propagationContext.parentSpanId) } @@ -1910,16 +1910,16 @@ class HubTest { @Test fun `continueTrace creates new propagation context if header invalid and returns transaction context if performance enabled`() { - val hub = generateHub() + val scopes = generateHub() val traceId = SentryId() var propagationContextHolder = AtomicReference() - hub.configureScope { propagationContextHolder.set(it.propagationContext) } + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } val propagationContextAtStart = propagationContextHolder.get()!! - val transactionContext = hub.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) @@ -1932,12 +1932,12 @@ class HubTest { @Test fun `continueTrace creates propagation context from headers and returns null if performance disabled`() { - val hub = generateHub { it.enableTracing = false } + val scopes = generateHub { it.enableTracing = false } val traceId = SentryId() val parentSpanId = SpanId() - val transactionContext = hub.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertEquals(traceId, scope.propagationContext.traceId) assertEquals(parentSpanId, scope.propagationContext.parentSpanId) } @@ -1947,16 +1947,16 @@ class HubTest { @Test fun `continueTrace creates new propagation context if header invalid and returns null if performance disabled`() { - val hub = generateHub { it.enableTracing = false } + val scopes = generateHub { it.enableTracing = false } val traceId = SentryId() var propagationContextHolder = AtomicReference() - hub.configureScope { propagationContextHolder.set(it.propagationContext) } + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } val propagationContextAtStart = propagationContextHolder.get()!! - val transactionContext = hub.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - hub.configureScope { scope -> + scopes.configureScope { scope -> assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) @@ -1966,32 +1966,32 @@ class HubTest { } @Test - fun `hub provides no tags for metrics, if metric option is disabled`() { - val hub = generateHub { + fun `scopes provides no tags for metrics, if metric option is disabled`() { + val scopes = generateHub { it.isEnableMetrics = false it.isEnableDefaultTagsForMetrics = true } as Hub assertTrue( - hub.defaultTagsForMetrics.isEmpty() + scopes.defaultTagsForMetrics.isEmpty() ) } @Test - fun `hub provides no tags for metrics, if default tags option is disabled`() { - val hub = generateHub { + fun `scopes provides no tags for metrics, if default tags option is disabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = false } as Hub assertTrue( - hub.defaultTagsForMetrics.isEmpty() + scopes.defaultTagsForMetrics.isEmpty() ) } @Test - fun `hub provides minimum default tags for metrics, if nothing is set up`() { - val hub = generateHub { + fun `scopes provides minimum default tags for metrics, if nothing is set up`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = true } as Hub @@ -2000,19 +2000,19 @@ class HubTest { mapOf( "environment" to "production" ), - hub.defaultTagsForMetrics + scopes.defaultTagsForMetrics ) } @Test - fun `hub provides default tags for metrics, based on options and running transaction`() { - val hub = generateHub { + fun `scopes provides default tags for metrics, based on options and running transaction`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableDefaultTagsForMetrics = true it.environment = "test" it.release = "1.0" } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } @@ -2024,72 +2024,72 @@ class HubTest { "release" to "1.0", "transaction" to "name" ), - hub.defaultTagsForMetrics + scopes.defaultTagsForMetrics ) } @Test - fun `hub provides no local metric aggregator if metrics feature is disabled`() { - val hub = generateHub { + fun `scopes provides no local metric aggregator if metrics feature is disabled`() { + val scopes = generateHub { it.isEnableMetrics = false it.isEnableSpanLocalMetricAggregation = true } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNull(hub.localMetricsAggregator) + assertNull(scopes.localMetricsAggregator) } @Test - fun `hub provides no local metric aggregator if local aggregation feature is disabled`() { - val hub = generateHub { + fun `scopes provides no local metric aggregator if local aggregation feature is disabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = false } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNull(hub.localMetricsAggregator) + assertNull(scopes.localMetricsAggregator) } @Test - fun `hub provides local metric aggregator if feature is enabled`() { - val hub = generateHub { + fun `scopes provides local metric aggregator if feature is enabled`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = true } as Hub - hub.startTransaction( + scopes.startTransaction( "name", "op", TransactionOptions().apply { isBindToScope = true } ) - assertNotNull(hub.localMetricsAggregator) + assertNotNull(scopes.localMetricsAggregator) } @Test - fun `hub startSpanForMetric starts a child span`() { - val hub = generateHub { + fun `scopes startSpanForMetric starts a child span`() { + val scopes = generateHub { it.isEnableMetrics = true it.isEnableSpanLocalMetricAggregation = true it.sampleRate = 1.0 } as Hub - val txn = hub.startTransaction( + val txn = scopes.startTransaction( "name.txn", "op.txn", TransactionOptions().apply { isBindToScope = true } ) - val span = hub.startSpanForMetric("op", "key")!! + val span = scopes.startSpanForMetric("op", "key")!! assertEquals("op", span.spanContext.op) assertEquals("key", span.spanContext.description) @@ -2098,7 +2098,7 @@ class HubTest { private val dsnTest = "https://key@sentry.io/proj" - private fun generateHub(optionsConfiguration: Sentry.OptionsConfiguration? = null): IHub { + private fun generateHub(optionsConfiguration: Sentry.OptionsConfiguration? = null): IScopes { val options = SentryOptions().apply { dsn = dsnTest cacheDirPath = file.absolutePath diff --git a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt index 8dc4f804bc..bd3a3c2cff 100644 --- a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt @@ -43,7 +43,7 @@ class JsonSerializerTest { private class Fixture { val logger: ILogger = mock() val serializer: ISerializer - val hub = mock() + val scopes = mock() val traceFile = Files.createTempFile("test", "here").toFile() val options = SentryOptions() @@ -51,7 +51,7 @@ class JsonSerializerTest { options.dsn = "https://key@sentry.io/proj" options.setLogger(logger) options.isDebug = true - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) serializer = JsonSerializer(options) options.setSerializer(serializer) options.setEnvelopeReader(EnvelopeReader(serializer)) @@ -830,7 +830,7 @@ class JsonSerializerTest { trace.status = SpanStatus.OK trace.setTag("myTag", "myValue") trace.sampled = true - val tracer = SentryTracer(trace, fixture.hub) + val tracer = SentryTracer(trace, fixture.scopes) tracer.setData("dataKey", "dataValue") val span = tracer.startChild("child") span.finish(SpanStatus.OK) @@ -1300,7 +1300,7 @@ class JsonSerializerTest { status = SpanStatus.OK setTag("myTag", "myValue") } - val tracer = SentryTracer(trace, fixture.hub) + val tracer = SentryTracer(trace, fixture.scopes) val span = tracer.startChild("child") span.setMeasurement("test_measurement", 1, MeasurementUnit.Custom("test")) span.finish(SpanStatus.OK) diff --git a/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt b/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt index ec932ebc86..682626f08c 100644 --- a/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt +++ b/sentry/src/test/java/io/sentry/MainEventProcessorTest.kt @@ -33,7 +33,7 @@ class MainEventProcessorTest { dist = "dist" sdkVersion = SdkVersion("test", "1.2.3") } - val hub = mock() + val scopes = mock() val getLocalhost = mock() lateinit var sentryTracer: SentryTracer private val hostnameCacheMock = Mockito.mockStatic(HostnameCache::class.java) @@ -72,8 +72,8 @@ class MainEventProcessorTest { } host } - whenever(hub.options).thenReturn(sentryOptions) - sentryTracer = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(sentryOptions) + sentryTracer = SentryTracer(TransactionContext("", ""), scopes) val hostnameCache = HostnameCache(hostnameCacheDuration) { getLocalhost } hostnameCacheMock.`when` { HostnameCache.getInstance() }.thenReturn(hostnameCache) diff --git a/sentry/src/test/java/io/sentry/OutboxSenderTest.kt b/sentry/src/test/java/io/sentry/OutboxSenderTest.kt index ab50e054d0..9274ed4400 100644 --- a/sentry/src/test/java/io/sentry/OutboxSenderTest.kt +++ b/sentry/src/test/java/io/sentry/OutboxSenderTest.kt @@ -30,7 +30,7 @@ class OutboxSenderTest { private class Fixture { val options = mock() - val hub = mock() + val scopes = mock() var envelopeReader = mock() val serializer = mock() val logger = mock() @@ -39,11 +39,11 @@ class OutboxSenderTest { whenever(options.dsn).thenReturn("https://key@sentry.io/proj") whenever(options.dateProvider).thenReturn(SentryNanotimeDateProvider()) whenever(options.mainThreadChecker).thenReturn(NoOpMainThreadChecker.getInstance()) - whenever(hub.options).thenReturn(this.options) + whenever(scopes.options).thenReturn(this.options) } fun getSut(): OutboxSender { - return OutboxSender(hub, envelopeReader, serializer, logger, 15000, 30) + return OutboxSender(scopes, envelopeReader, serializer, logger, 15000, 30) } } @@ -83,7 +83,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEvent(eq(expected), any()) + verify(fixture.scopes).captureEvent(eq(expected), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -94,7 +94,7 @@ class OutboxSenderTest { fun `when parser is EnvelopeReader and serializer return SentryTransaction, transaction captured, transactions sampled, file is deleted`() { fixture.envelopeReader = EnvelopeReader(JsonSerializer(fixture.options)) whenever(fixture.options.maxSpans).thenReturn(1000) - whenever(fixture.hub.options).thenReturn(fixture.options) + whenever(fixture.scopes.options).thenReturn(fixture.options) whenever(fixture.options.transactionProfiler).thenReturn(NoOpTransactionProfiler.getInstance()) val transactionContext = TransactionContext("fixture-name", "http") @@ -102,7 +102,7 @@ class OutboxSenderTest { transactionContext.status = SpanStatus.OK transactionContext.setTag("fixture-tag", "fixture-value") - val sentryTracer = SentryTracer(transactionContext, fixture.hub) + val sentryTracer = SentryTracer(transactionContext, fixture.scopes) val span = sentryTracer.startChild("child") span.finish(SpanStatus.OK) sentryTracer.finish() @@ -120,7 +120,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(expected, it) assertTrue(it.isSampled) @@ -139,7 +139,7 @@ class OutboxSenderTest { fun `restores sampleRate`() { fixture.envelopeReader = EnvelopeReader(JsonSerializer(fixture.options)) whenever(fixture.options.maxSpans).thenReturn(1000) - whenever(fixture.hub.options).thenReturn(fixture.options) + whenever(fixture.scopes.options).thenReturn(fixture.options) whenever(fixture.options.transactionProfiler).thenReturn(NoOpTransactionProfiler.getInstance()) val transactionContext = TransactionContext("fixture-name", "http") @@ -148,7 +148,7 @@ class OutboxSenderTest { transactionContext.setTag("fixture-tag", "fixture-value") transactionContext.samplingDecision = TracesSamplingDecision(true, 0.00000021) - val sentryTracer = SentryTracer(transactionContext, fixture.hub) + val sentryTracer = SentryTracer(transactionContext, fixture.scopes) val span = sentryTracer.startChild("child") span.finish(SpanStatus.OK) sentryTracer.finish() @@ -166,7 +166,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(expected, it) assertTrue(it.isSampled) @@ -207,7 +207,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -225,7 +225,7 @@ class OutboxSenderTest { val hints = HintUtils.createWithTypeCheckHint(mock()) sut.processEnvelopeFile(path, hints) - verify(fixture.hub).captureEnvelope(any(), any()) + verify(fixture.scopes).captureEnvelope(any(), any()) assertFalse(File(path).exists()) // Additionally make sure we have no errors logged verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -245,7 +245,7 @@ class OutboxSenderTest { // Additionally make sure we have no errors logged verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) assertFalse(File(path).exists()) } @@ -263,7 +263,7 @@ class OutboxSenderTest { // Additionally make sure we have no errors logged verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) assertFalse(File(path).exists()) } diff --git a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt index 239e90905e..87aa5e6715 100644 --- a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt +++ b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt @@ -21,7 +21,7 @@ class PreviousSessionFinalizerTest { class Fixture { val options = SentryOptions() - val hub = mock() + val scopes = mock() val logger = mock() lateinit var sessionFile: File @@ -61,7 +61,7 @@ class PreviousSessionFinalizerTest { nativeCrashMarker.writeText(nativeCrashTimestamp.toString()) } } - return PreviousSessionFinalizer(options, hub) + return PreviousSessionFinalizer(options, scopes) } fun sessionFromEnvelope(envelope: SentryEnvelope): Session { @@ -80,7 +80,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(null) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -88,7 +88,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = false) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -96,7 +96,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = true, session = null) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -107,7 +107,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -133,7 +133,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -156,7 +156,7 @@ class PreviousSessionFinalizerTest { ) finalizer.run() - verify(fixture.hub).captureEnvelope( + verify(fixture.scopes).captureEnvelope( argThat { val session = fixture.sessionFromEnvelope(this) session.release == "io.sentry.sample@1.0" && @@ -170,7 +170,7 @@ class PreviousSessionFinalizerTest { val finalizer = fixture.getSut(tmpDir, sessionFileExists = true) finalizer.run() - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) assertFalse(fixture.sessionFile.exists()) } @@ -189,7 +189,7 @@ class PreviousSessionFinalizerTest { argThat { startsWith("Timed out waiting to flush previous session to its own file in session finalizer.") }, any() ) - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } @Test @@ -202,6 +202,6 @@ class PreviousSessionFinalizerTest { argThat { startsWith("Timed out waiting to flush previous session to its own file in session finalizer.") }, any() ) - verify(fixture.hub, never()).captureEnvelope(any()) + verify(fixture.scopes, never()).captureEnvelope(any()) } } diff --git a/sentry/src/test/java/io/sentry/ScopeTest.kt b/sentry/src/test/java/io/sentry/ScopeTest.kt index 906c897c62..86794d7b19 100644 --- a/sentry/src/test/java/io/sentry/ScopeTest.kt +++ b/sentry/src/test/java/io/sentry/ScopeTest.kt @@ -114,7 +114,7 @@ class ScopeTest { scope.setExtra("extra", "extra") val transaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val attachment = Attachment("path/log.txt") @@ -192,7 +192,7 @@ class ScopeTest { scope.setTransaction( SentryTracer( TransactionContext("newTransaction", "op"), - NoOpHub.getInstance() + NoOpScopes.getInstance() ) ) @@ -265,7 +265,7 @@ class ScopeTest { fun `clear scope resets scope to default state`() { val scope = Scope(SentryOptions()) scope.level = SentryLevel.WARNING - scope.setTransaction(SentryTracer(TransactionContext("", "op"), NoOpHub.getInstance())) + scope.setTransaction(SentryTracer(TransactionContext("", "op"), NoOpScopes.getInstance())) scope.user = User() scope.request = Request() scope.fingerprint = mutableListOf("finger") @@ -822,7 +822,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the transaction if there is no active span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction assertEquals(transaction, scope.span) } @@ -830,7 +830,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the current span if there is an unfinished span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") assertEquals(span, scope.span) @@ -839,7 +839,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the current span if there is a finished span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") span.finish() @@ -849,7 +849,7 @@ class ScopeTest { @Test fun `Scope getTransaction returns the latest span if there is a list of active span`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction val span = transaction.startChild("op") val innerSpan = span.startChild("op") @@ -859,7 +859,7 @@ class ScopeTest { @Test fun `Scope setTransaction sets transaction name`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction scope.setTransaction("new-name") assertNotNull(scope.transaction) { @@ -871,7 +871,7 @@ class ScopeTest { @Test fun `Scope setTransaction with null does not clear transaction`() { val scope = Scope(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), NoOpHub.getInstance()) + val transaction = SentryTracer(TransactionContext("name", "op"), NoOpScopes.getInstance()) scope.transaction = transaction scope.callMethod("setTransaction", String::class.java, null) assertNotNull(scope.transaction) @@ -936,7 +936,7 @@ class ScopeTest { fun `when transaction is started, sets transaction name on the transaction object`() { val scope = Scope(SentryOptions()) val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.transaction = sentryTransaction assertEquals("transaction-name", scope.transactionName) scope.setTransaction("new-name") @@ -950,7 +950,7 @@ class ScopeTest { val scope = Scope(SentryOptions()) scope.setTransaction("transaction-a") val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.setTransaction(sentryTransaction) assertEquals("transaction-name", scope.transactionName) scope.clearTransaction() @@ -961,7 +961,7 @@ class ScopeTest { fun `withTransaction returns the current Transaction bound to the Scope`() { val scope = Scope(SentryOptions()) val sentryTransaction = - SentryTracer(TransactionContext("transaction-name", "op"), NoOpHub.getInstance()) + SentryTracer(TransactionContext("transaction-name", "op"), NoOpScopes.getInstance()) scope.setTransaction(sentryTransaction) scope.withTransaction { diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt new file mode 100644 index 0000000000..85a0b6ef75 --- /dev/null +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -0,0 +1,265 @@ +package io.sentry + +import io.sentry.protocol.SentryTransaction +import io.sentry.protocol.User +import org.mockito.kotlin.any +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.reset +import org.mockito.kotlin.verify +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +class ScopesAdapterTest { + + val scopes: IScopes = mock() + + @BeforeTest + fun `set up`() { + Sentry.setCurrentScopes(scopes) + } + + @AfterTest + fun shutdown() { + Sentry.close() + } + + @Test fun `isEnabled calls Hub`() { + ScopesAdapter.getInstance().isEnabled + verify(scopes).isEnabled + } + + @Test fun `captureEvent calls Hub`() { + val event = mock() + val hint = mock() + val scopeCallback = mock() + ScopesAdapter.getInstance().captureEvent(event, hint) + verify(scopes).captureEvent(eq(event), eq(hint)) + + ScopesAdapter.getInstance().captureEvent(event, hint, scopeCallback) + verify(scopes).captureEvent(eq(event), eq(hint), eq(scopeCallback)) + } + + @Test fun `captureMessage calls Hub`() { + val scopeCallback = mock() + val sentryLevel = mock() + ScopesAdapter.getInstance().captureMessage("message", sentryLevel) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel)) + + ScopesAdapter.getInstance().captureMessage("message", sentryLevel, scopeCallback) + verify(scopes).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) + } + + @Test fun `captureEnvelope calls Hub`() { + val envelope = mock() + val hint = mock() + ScopesAdapter.getInstance().captureEnvelope(envelope, hint) + verify(scopes).captureEnvelope(eq(envelope), eq(hint)) + } + + @Test fun `captureException calls Hub`() { + val throwable = mock() + val hint = mock() + val scopeCallback = mock() + ScopesAdapter.getInstance().captureException(throwable, hint) + verify(scopes).captureException(eq(throwable), eq(hint)) + + ScopesAdapter.getInstance().captureException(throwable, hint, scopeCallback) + verify(scopes).captureException(eq(throwable), eq(hint), eq(scopeCallback)) + } + + @Test fun `captureUserFeedback calls Hub`() { + val userFeedback = mock() + ScopesAdapter.getInstance().captureUserFeedback(userFeedback) + verify(scopes).captureUserFeedback(eq(userFeedback)) + } + + @Test fun `captureCheckIn calls Hub`() { + val checkIn = mock() + ScopesAdapter.getInstance().captureCheckIn(checkIn) + verify(scopes).captureCheckIn(eq(checkIn)) + } + + @Test fun `startSession calls Hub`() { + ScopesAdapter.getInstance().startSession() + verify(scopes).startSession() + } + + @Test fun `endSession calls Hub`() { + ScopesAdapter.getInstance().endSession() + verify(scopes).endSession() + } + + @Test fun `close calls Hub`() { + ScopesAdapter.getInstance().close() + verify(scopes).close(false) + } + + @Test fun `close with isRestarting true calls Hub with isRestarting false`() { + ScopesAdapter.getInstance().close(true) + verify(scopes).close(false) + } + + @Test fun `close with isRestarting false calls Hub with isRestarting false`() { + ScopesAdapter.getInstance().close(false) + verify(scopes).close(false) + } + + @Test fun `addBreadcrumb calls Hub`() { + val breadcrumb = mock() + val hint = mock() + ScopesAdapter.getInstance().addBreadcrumb(breadcrumb, hint) + verify(scopes).addBreadcrumb(eq(breadcrumb), eq(hint)) + } + + @Test fun `setLevel calls Hub`() { + val sentryLevel = mock() + ScopesAdapter.getInstance().setLevel(sentryLevel) + verify(scopes).setLevel(eq(sentryLevel)) + } + + @Test fun `setTransaction calls Hub`() { + ScopesAdapter.getInstance().setTransaction("transaction") + verify(scopes).setTransaction(eq("transaction")) + } + + @Test fun `setUser calls Hub`() { + val user = mock() + ScopesAdapter.getInstance().setUser(user) + verify(scopes).setUser(eq(user)) + } + + @Test fun `setFingerprint calls Hub`() { + val fingerprint = ArrayList() + ScopesAdapter.getInstance().setFingerprint(fingerprint) + verify(scopes).setFingerprint(eq(fingerprint)) + } + + @Test fun `clearBreadcrumbs calls Hub`() { + ScopesAdapter.getInstance().clearBreadcrumbs() + verify(scopes).clearBreadcrumbs() + } + + @Test fun `setTag calls Hub`() { + ScopesAdapter.getInstance().setTag("key", "value") + verify(scopes).setTag(eq("key"), eq("value")) + } + + @Test fun `removeTag calls Hub`() { + ScopesAdapter.getInstance().removeTag("key") + verify(scopes).removeTag(eq("key")) + } + + @Test fun `setExtra calls Hub`() { + ScopesAdapter.getInstance().setExtra("key", "value") + verify(scopes).setExtra(eq("key"), eq("value")) + } + + @Test fun `removeExtra calls Hub`() { + ScopesAdapter.getInstance().removeExtra("key") + verify(scopes).removeExtra(eq("key")) + } + + @Test fun `getLastEventId calls Hub`() { + ScopesAdapter.getInstance().lastEventId + verify(scopes).lastEventId + } + + @Test fun `pushScope calls Hub`() { + ScopesAdapter.getInstance().pushScope() + verify(scopes).pushScope() + } + + @Test fun `popScope calls Hub`() { + ScopesAdapter.getInstance().popScope() + verify(scopes).popScope() + } + + @Test fun `withScope calls Hub`() { + val scopeCallback = mock() + ScopesAdapter.getInstance().withScope(scopeCallback) + verify(scopes).withScope(eq(scopeCallback)) + } + + @Test fun `configureScope calls Hub`() { + val scopeCallback = mock() + ScopesAdapter.getInstance().configureScope(scopeCallback) + verify(scopes).configureScope(eq(scopeCallback)) + } + + @Test fun `bindClient calls Hub`() { + val client = mock() + ScopesAdapter.getInstance().bindClient(client) + verify(scopes).bindClient(eq(client)) + } + + @Test fun `flush calls Hub`() { + ScopesAdapter.getInstance().flush(1) + verify(scopes).flush(eq(1)) + } + + @Test fun `clone calls Hub`() { + ScopesAdapter.getInstance().clone() + verify(scopes).clone() + } + + @Test fun `captureTransaction calls Hub`() { + val transaction = mock() + val traceContext = mock() + val hint = mock() + val profilingTraceData = mock() + ScopesAdapter.getInstance().captureTransaction(transaction, traceContext, hint, profilingTraceData) + verify(scopes).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) + } + + @Test fun `startTransaction calls Hub`() { + val transactionContext = mock() + val samplingContext = mock() + val transactionOptions = mock() + ScopesAdapter.getInstance().startTransaction(transactionContext) + verify(scopes).startTransaction(eq(transactionContext), any()) + + reset(scopes) + + ScopesAdapter.getInstance().startTransaction(transactionContext, transactionOptions) + verify(scopes).startTransaction(eq(transactionContext), eq(transactionOptions)) + } + + @Test fun `traceHeaders calls Hub`() { + ScopesAdapter.getInstance().traceHeaders() + verify(scopes).traceHeaders() + } + + @Test fun `setSpanContext calls Hub`() { + val throwable = mock() + val span = mock() + ScopesAdapter.getInstance().setSpanContext(throwable, span, "transactionName") + verify(scopes).setSpanContext(eq(throwable), eq(span), eq("transactionName")) + } + + @Test fun `getSpan calls Hub`() { + ScopesAdapter.getInstance().span + verify(scopes).span + } + + @Test fun `getTransaction calls Hub`() { + ScopesAdapter.getInstance().transaction + verify(scopes).transaction + } + + @Test fun `getOptions calls Hub`() { + ScopesAdapter.getInstance().options + verify(scopes).options + } + + @Test fun `isCrashedLastRun calls Hub`() { + ScopesAdapter.getInstance().isCrashedLastRun + verify(scopes).isCrashedLastRun + } + + @Test fun `reportFullyDisplayed calls Hub`() { + ScopesAdapter.getInstance().reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() + } +} diff --git a/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt b/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt index 78623f90a7..beed31a198 100644 --- a/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegrationTest.kt @@ -19,7 +19,7 @@ import kotlin.test.assertTrue class SendCachedEnvelopeFireAndForgetIntegrationTest { private class Fixture { - var hub: IHub = mock() + var scopes: IScopes = mock() var logger: ILogger = mock() var options = SentryOptions() val sender = mock() @@ -45,7 +45,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fun `when cacheDirPath returns null, register logs and exit`() { fixture.options.cacheDirPath = null val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("No cache dir path is defined in options.")) verify(fixture.sender, never()).send() } @@ -73,7 +73,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { val sut = SendCachedEnvelopeFireAndForgetIntegration(CustomFactory()) fixture.options.cacheDirPath = "abc" fixture.options.executorService = ImmediateExecutorService() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("SendFireAndForget factory is null.")) verify(fixture.sender, never()).send() } @@ -85,7 +85,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { mock() ) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(fixture.options.sdkVersion) assert(fixture.options.sdkVersion!!.integrationSet.contains("SendCachedEnvelopeFireAndForget")) } @@ -96,7 +96,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService.close(0) whenever(fixture.callback.create(any(), any())).thenReturn(mock()) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.logger).log(eq(SentryLevel.ERROR), eq("Failed to call the executor. Cached events will not be sent. Did you call Sentry.close()?"), any()) } @@ -108,7 +108,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(connectionStatusProvider).addConnectionStatusObserver(any()) } @@ -122,9 +122,9 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() } @@ -139,7 +139,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender).send() } @@ -155,7 +155,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // when there's no connection no factory create call should be done verify(fixture.sender, never()).send() @@ -183,9 +183,9 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(fixture.hub.rateLimiter).thenReturn(rateLimiter) + whenever(fixture.scopes.rateLimiter).thenReturn(rateLimiter) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // no factory call should be done if there's rate limiting active verify(fixture.sender, never()).send() @@ -196,8 +196,8 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = ImmediateExecutorService() fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) - verify(fixture.callback).create(eq(fixture.hub), eq(fixture.options)) + sut.register(fixture.scopes, fixture.options) + verify(fixture.callback).create(eq(fixture.scopes), eq(fixture.options)) } @Test @@ -205,7 +205,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = mock() fixture.options.cacheDirPath = "cache" val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.callback, never()).create(any(), any()) } @@ -215,7 +215,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { fixture.options.executorService = deferredExecutorService val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() sut.close() @@ -224,7 +224,7 @@ class SendCachedEnvelopeFireAndForgetIntegrationTest { } private class CustomFactory : SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory { - override fun create(hub: IHub, options: SentryOptions): SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget? { + override fun create(scopes: IScopes, options: SentryOptions): SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget? { return null } } diff --git a/sentry/src/test/java/io/sentry/SentryClientTest.kt b/sentry/src/test/java/io/sentry/SentryClientTest.kt index de540bf90c..eac2b0be29 100644 --- a/sentry/src/test/java/io/sentry/SentryClientTest.kt +++ b/sentry/src/test/java/io/sentry/SentryClientTest.kt @@ -70,7 +70,7 @@ class SentryClientTest { var transport = mock() var factory = mock() val maxAttachmentSize: Long = (5 * 1024 * 1024).toLong() - val hub = mock() + val scopes = mock() val sentryTracer: SentryTracer var sentryOptions: SentryOptions = SentryOptions().apply { @@ -88,8 +88,8 @@ class SentryClientTest { init { whenever(factory.create(any(), any())).thenReturn(transport) - whenever(hub.options).thenReturn(sentryOptions) - sentryTracer = SentryTracer(TransactionContext("a-transaction", "op"), hub) + whenever(scopes.options).thenReturn(sentryOptions) + sentryTracer = SentryTracer(TransactionContext("a-transaction", "op"), scopes) } var attachment = Attachment("hello".toByteArray(), "hello.txt", "text/plain", true) @@ -1456,9 +1456,9 @@ class SentryClientTest { @Test fun `when captureTransaction with scope, transaction should use user data`() { - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("tx", "op"), hub)) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) + val transaction = SentryTransaction(SentryTracer(TransactionContext("tx", "op"), scopes)) val scope = createScope() val sut = fixture.getSut() @@ -1487,7 +1487,7 @@ class SentryClientTest { val event = SentryEvent() val sut = fixture.getSut() val scope = createScope() - val transaction = SentryTracer(TransactionContext("a-transaction", "op"), fixture.hub) + val transaction = SentryTracer(TransactionContext("a-transaction", "op"), fixture.scopes) scope.setTransaction(transaction) val span = transaction.startChild("op") sut.captureEvent(event, scope) @@ -1558,7 +1558,7 @@ class SentryClientTest { fixture.sentryOptions.release = "optionsRelease" fixture.sentryOptions.environment = "optionsEnvironment" val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.release = "transactionRelease" transaction.environment = "transactionEnvironment" @@ -1571,7 +1571,7 @@ class SentryClientTest { fun `when transaction does not have SDK version set, and the SDK version is set on options, options values are applied to transactions`() { fixture.sentryOptions.sdkVersion = SdkVersion("sdk.name", "version") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals(fixture.sentryOptions.sdkVersion, transaction.sdk) @@ -1581,7 +1581,7 @@ class SentryClientTest { fun `when transaction has SDK version set, and the SDK version is set on options, options values are not applied to transactions`() { fixture.sentryOptions.sdkVersion = SdkVersion("sdk.name", "version") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) val sdkVersion = SdkVersion("transaction.sdk.name", "version") transaction.sdk = sdkVersion @@ -1593,7 +1593,7 @@ class SentryClientTest { fun `when transaction does not have tags, and tags are set on options, options values are applied to transactions`() { fixture.sentryOptions.setTag("tag1", "value1") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals(mapOf("tag1" to "value1"), transaction.tags) @@ -1604,7 +1604,7 @@ class SentryClientTest { fixture.sentryOptions.setTag("tag1", "value1") fixture.sentryOptions.setTag("tag2", "value2") val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.setTag("tag3", "value3") transaction.setTag("tag2", "transaction-tag") @@ -1618,7 +1618,7 @@ class SentryClientTest { @Test fun `captured transactions without a platform, have the default platform set`() { val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) sut.captureTransaction(transaction, sentryTracer.traceContext()) assertEquals("java", transaction.platform) @@ -1627,7 +1627,7 @@ class SentryClientTest { @Test fun `captured transactions with a platform, do not get the platform overwritten`() { val sut = fixture.getSut() - val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), fixture.scopes) val transaction = SentryTransaction(sentryTracer) transaction.platform = "abc" sut.captureTransaction(transaction, sentryTracer.traceContext()) diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 0f4966b44a..70728d2900 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -63,27 +63,27 @@ class SentryTest { } @Test - fun `init multiple times calls hub close with isRestarting true`() { - val hub = mock() + fun `init multiple times calls scopes close with isRestarting true`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.init { it.dsn = dsn } - verify(hub).close(eq(true)) + verify(scopes).close(eq(true)) } @Test - fun `close calls hub close with isRestarting false`() { - val hub = mock() + fun `close calls scopes close with isRestarting false`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.close() - verify(hub).close(eq(false)) + verify(scopes).close(eq(false)) } @Test @@ -213,7 +213,7 @@ class SentryTest { Sentry.init { it.isEnableExternalConfiguration = true } - assertTrue(HubAdapter.getInstance().isEnabled) + assertTrue(ScopesAdapter.getInstance().isEnabled) } finally { temporaryFolder.delete() } @@ -229,10 +229,10 @@ class SentryTest { Sentry.setTag("none", "shouldNotExist") var value: String? = null - Sentry.getCurrentHub().configureScope { + Sentry.getCurrentScopes().configureScope { value = it.tags[value] } - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) assertNull(value) } @@ -245,10 +245,10 @@ class SentryTest { Sentry.setTag("none", "shouldNotExist") var value: String? = null - Sentry.getCurrentHub().configureScope { + Sentry.getCurrentScopes().configureScope { value = it.tags[value] } - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) assertNull(value) } @@ -267,7 +267,7 @@ class SentryTest { Sentry.init { it.dsn = dsn } val client = mock() - Sentry.getCurrentHub().bindClient(client) + Sentry.getCurrentScopes().bindClient(client) val userFeedback = UserFeedback(SentryId.EMPTY_ID) Sentry.captureUserFeedback(userFeedback) @@ -369,11 +369,11 @@ class SentryTest { } @Test - fun `using sentry before calling init creates NoOpHub but after init Sentry uses a new clone`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `using sentry before calling init creates NoOpScopes but after init Sentry uses a new clone`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.captureMessage("noop caused") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) // init Sentry in another thread val thread = Thread() { @@ -387,18 +387,18 @@ class SentryTest { Sentry.captureMessage("should work now") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(scopes.isNoOp) } @Test - fun `main hub can be cloned and does not share scope with current hub`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `main scopes can be cloned and does not share scope with current scopes`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.addBreadcrumb("breadcrumbNoOp") Sentry.captureMessage("messageNoOp") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) val capturedEvents = mutableListOf() @@ -418,14 +418,14 @@ class SentryTest { Sentry.addBreadcrumb("breadcrumbCurrent") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(Sentry.getCurrentScopes().isNoOp) val newMainHubClone = Sentry.cloneMainHub() newMainHubClone.addBreadcrumb("breadcrumbMainClone") - hub.captureMessage("messageCurrent") + scopes.captureMessage("messageCurrent") newMainHubClone.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) @@ -444,12 +444,12 @@ class SentryTest { } @Test - fun `main hub is not cloned in global hub mode and shares scope with current hub`() { - // noop as not yet initialized, caches NoOpHub in ThreadLocal + fun `main scopes is not cloned in global scopes mode and shares scope with current scopes`() { + // noop as not yet initialized, caches NoOpScopes in ThreadLocal Sentry.addBreadcrumb("breadcrumbNoOp") Sentry.captureMessage("messageNoOp") - assertTrue(Sentry.getCurrentHub() is NoOpHub) + assertTrue(Sentry.getCurrentScopes().isNoOp) val capturedEvents = mutableListOf() @@ -469,14 +469,14 @@ class SentryTest { Sentry.addBreadcrumb("breadcrumbCurrent") - val hub = Sentry.getCurrentHub() - assertNotNull(hub) - assertFalse(hub is NoOpHub) + val scopes = Sentry.getCurrentScopes() + assertNotNull(scopes) + assertFalse(scopes.isNoOp) val newMainHubClone = Sentry.cloneMainHub() newMainHubClone.addBreadcrumb("breadcrumbMainClone") - hub.captureMessage("messageCurrent") + scopes.captureMessage("messageCurrent") newMainHubClone.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) @@ -669,25 +669,25 @@ class SentryTest { } @Test - fun `reportFullyDisplayed calls hub reportFullyDisplayed`() { - val hub = mock() + fun `reportFullyDisplayed calls scopes reportFullyDisplayed`() { + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.reportFullyDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } @Test fun `reportFullDisplayed calls reportFullyDisplayed`() { - val hub = mock() + val scopes = mock() Sentry.init { it.dsn = dsn } - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.reportFullDisplayed() - verify(hub).reportFullyDisplayed() + verify(scopes).reportFullyDisplayed() } @Test @@ -806,11 +806,11 @@ class SentryTest { it.serializer.deserialize(previousSessionFile.bufferedReader(), Session::class.java)!!.environment ) - it.addIntegration { hub, _ -> + it.addIntegration { scopes, _ -> // this is just a hack to trigger the previousSessionFlush latch, so the finalizer // does not time out waiting. We have to do it as integration, because this is where - // the hub is already initialized - hub.startSession() + // the scopes is already initialized + scopes.startSession() } } @@ -861,7 +861,7 @@ class SentryTest { Sentry.init { it.dsn = dsn } val client = mock() - Sentry.getCurrentHub().bindClient(client) + Sentry.getCurrentScopes().bindClient(client) val checkIn = CheckIn("some_slug", CheckInStatus.OK) Sentry.captureCheckIn(checkIn) @@ -910,18 +910,18 @@ class SentryTest { } @Test - fun `getSpan calls hub getSpan`() { - val hub = mock() + fun `getSpan calls scopes getSpan`() { + val scopes = mock() Sentry.init({ it.dsn = dsn }, false) - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.getSpan() - verify(hub).span + verify(scopes).span } @Test - fun `getSpan calls returns root span if globalhub mode is enabled on Android`() { + fun `getSpan calls returns root span if globalscopes mode is enabled on Android`() { PlatformTestManipulator.pretendIsAndroid(true) Sentry.init({ it.dsn = dsn @@ -938,7 +938,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalhub mode is enabled, but the platform is not Android`() { + fun `getSpan calls returns child span if globalscopes mode is enabled, but the platform is not Android`() { PlatformTestManipulator.pretendIsAndroid(false) Sentry.init({ it.dsn = dsn @@ -954,7 +954,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalhub mode is disabled`() { + fun `getSpan calls returns child span if globalscopes mode is disabled`() { Sentry.init({ it.dsn = dsn it.enableTracing = true @@ -1140,15 +1140,15 @@ class SentryTest { } @Test - fun `metrics calls hub getMetrics`() { - val hub = mock() + fun `metrics calls scopes getMetrics`() { + val scopes = mock() Sentry.init({ it.dsn = dsn }, false) - Sentry.setCurrentHub(hub) + Sentry.setCurrentScopes(scopes) Sentry.metrics() - verify(hub).metrics() + verify(scopes).metrics() } private class InMemoryOptionsObserver : IOptionsObserver { diff --git a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt index 7f6d449eac..2fb9b38566 100644 --- a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt +++ b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt @@ -35,20 +35,20 @@ class SentryWrapperTest { } } - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = CompletableFuture.supplyAsync( SentryWrapper.wrapSupplier { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) "Result 1" }, executor @@ -57,8 +57,8 @@ class SentryWrapperTest { callableFuture.join() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } @@ -169,20 +169,20 @@ class SentryWrapperTest { it.dsn = dsn } - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( SentryWrapper.wrapCallable { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) "Result 1" } ) @@ -190,8 +190,8 @@ class SentryWrapperTest { callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt index d6ce0ff043..428a34635f 100644 --- a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt @@ -16,7 +16,7 @@ class ShutdownHookIntegrationTest { private class Fixture { val runtime = mock() val options = SentryOptions() - val hub = mock() + val scopes = mock() fun getSut(): ShutdownHookIntegration { return ShutdownHookIntegration(runtime) @@ -29,7 +29,7 @@ class ShutdownHookIntegrationTest { fun `registration attaches shutdown hook to runtime`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.runtime).addShutdownHook(any()) } @@ -39,7 +39,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() fixture.options.isEnableShutdownHook = false - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.runtime, never()).addShutdownHook(any()) } @@ -48,7 +48,7 @@ class ShutdownHookIntegrationTest { fun `registration removes shutdown hook from runtime`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) integration.close() verify(fixture.runtime).removeShutdownHook(any()) @@ -58,13 +58,13 @@ class ShutdownHookIntegrationTest { fun `hook calls flush`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertNotNull(integration.hook) { it.start() it.join() } - verify(fixture.hub).flush(any()) + verify(fixture.scopes).flush(any()) } @Test @@ -72,13 +72,13 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() fixture.options.flushTimeoutMillis = 10000 - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertNotNull(integration.hook) { it.start() it.join() } - verify(fixture.hub).flush(eq(10000)) + verify(fixture.scopes).flush(eq(10000)) } @Test @@ -86,7 +86,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException("Shutdown in progress")) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) integration.close() verify(fixture.runtime).removeShutdownHook(any()) @@ -97,7 +97,7 @@ class ShutdownHookIntegrationTest { val integration = fixture.getSut() whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException()) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertFails { integration.close() @@ -110,7 +110,7 @@ class ShutdownHookIntegrationTest { fun `Integration adds itself to integration list`() { val integration = fixture.getSut() - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.integrationSet.contains("ShutdownHook") diff --git a/sentry/src/test/java/io/sentry/SpanTest.kt b/sentry/src/test/java/io/sentry/SpanTest.kt index ae9d9bd07f..fd36c31933 100644 --- a/sentry/src/test/java/io/sentry/SpanTest.kt +++ b/sentry/src/test/java/io/sentry/SpanTest.kt @@ -21,10 +21,10 @@ import kotlin.test.assertTrue class SpanTest { private class Fixture { - val hub = mock() + val scopes = mock() init { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" isTraceSampling = true @@ -36,9 +36,9 @@ class SpanTest { return Span( SentryId(), SpanId(), - SentryTracer(TransactionContext("name", "op"), hub), + SentryTracer(TransactionContext("name", "op"), scopes), "op", - hub, + scopes, null, options, null @@ -46,7 +46,7 @@ class SpanTest { } fun getRootSut(options: TransactionOptions = TransactionOptions()): Span { - return SentryTracer(TransactionContext("name", "op"), hub, options).root + return SentryTracer(TransactionContext("name", "op"), scopes, options).root } } @@ -106,10 +106,10 @@ class SpanTest { parentSpanId, SentryTracer( TransactionContext("name", "op", TracesSamplingDecision(true)), - fixture.hub + fixture.scopes ), "op", - fixture.hub + fixture.scopes ) val sentryTrace = span.toSentryTrace() @@ -163,17 +163,17 @@ class SpanTest { } @Test - fun `when span has throwable set set, it assigns itself to throwable on the Hub`() { + fun `when span has throwable set set, it assigns itself to throwable on the Scopes`() { val transaction = SentryTracer( TransactionContext("name", "op"), - fixture.hub + fixture.scopes ) val span = transaction.startChild("op") val ex = RuntimeException() span.throwable = ex span.finish() - verify(fixture.hub).setSpanContext(ex, span, "name") + verify(fixture.scopes).setSpanContext(ex, span, "name") } @Test @@ -188,7 +188,7 @@ class SpanTest { span.finish(SpanStatus.UNKNOWN_ERROR) // call only once - verify(fixture.hub).setSpanContext(any(), any(), any()) + verify(fixture.scopes).setSpanContext(any(), any(), any()) assertEquals(SpanStatus.OK, span.status) assertEquals(timestamp, span.finishDate) } @@ -496,7 +496,7 @@ class SpanTest { } private fun getTransaction(transactionContext: TransactionContext = TransactionContext("name", "op")): SentryTracer { - return SentryTracer(transactionContext, fixture.hub) + return SentryTracer(transactionContext, fixture.scopes) } private fun startChildFromSpan(): Span { diff --git a/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt b/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt index e79e5ebf8c..0847c9448f 100644 --- a/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt +++ b/sentry/src/test/java/io/sentry/TraceContextSerializationTest.kt @@ -54,10 +54,10 @@ class TraceContextSerializationTest { private fun createTraceContext(sRate: Double): TraceContext { val baggage = Baggage(fixture.logger) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) baggage.setValuesFromTransaction( - SentryTracer(TransactionContext("name", "op"), hub), + SentryTracer(TransactionContext("name", "op"), scopes), User().apply { id = "user-id" others = mapOf("segment" to "pro") diff --git a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt index 01353d5ac0..ec366e1901 100644 --- a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt @@ -30,7 +30,7 @@ class UncaughtExceptionHandlerIntegrationTest { val defaultHandler = mock() val thread = mock() val throwable = Throwable("test") - val hub = mock() + val scopes = mock() val options = SentryOptions() val logger = mock() @@ -63,17 +63,17 @@ class UncaughtExceptionHandlerIntegrationTest { fun `when uncaughtException is called, sentry captures exception`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test fun `when register is called, current handler is not lost`() { val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) verify(fixture.defaultHandler).uncaughtException(fixture.thread, fixture.throwable) @@ -81,7 +81,7 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when uncaughtException is called, exception captured has handled=false`() { - whenever(fixture.hub.captureException(any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureException(any())).thenAnswer { invocation -> val e = invocation.getArgument(1) assertNotNull(e) assertNotNull(e.exceptionMechanism) @@ -91,22 +91,22 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test - fun `when hub is closed, integrations should be closed`() { + fun `when scopes is closed, integrations should be closed`() { val integrationMock = mock() val options = SentryOptions() options.dsn = "https://key@sentry.io/proj" options.addIntegration(integrationMock) options.cacheDirPath = fixture.file.absolutePath options.setSerializer(mock()) - val hub = Hub(options) - hub.close() + val scopes = Hub(options) + scopes.close() verify(integrationMock).close() } @@ -117,7 +117,7 @@ class UncaughtExceptionHandlerIntegrationTest { isPrintUncaughtStackTrace = false ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.handler, never()).defaultUncaughtExceptionHandler = any() } @@ -126,7 +126,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is enabled, should install Sentry UncaughtExceptionHandler`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.handler).defaultUncaughtExceptionHandler = argWhere { it is UncaughtExceptionHandlerIntegration } @@ -136,7 +136,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is set and integration is closed, default uncaught exception handler is reset to previous handler`() { val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) whenever(fixture.handler.defaultUncaughtExceptionHandler) .thenReturn(sut) sut.close() @@ -148,7 +148,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `When defaultUncaughtExceptionHandler is not set and integration is closed, default uncaught exception handler is reset to null`() { val sut = fixture.getSut(isPrintUncaughtStackTrace = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) whenever(fixture.handler.defaultUncaughtExceptionHandler) .thenReturn(sut) sut.close() @@ -165,7 +165,7 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(isPrintUncaughtStackTrace = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, RuntimeException("This should be printed!")) assertTrue( @@ -185,7 +185,7 @@ class UncaughtExceptionHandlerIntegrationTest { fun `waits for event to flush on disk`() { val capturedEventId = SentryId() - whenever(fixture.hub.captureEvent(any(), any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureEvent(any(), any())).thenAnswer { invocation -> val hint = HintUtils.getSentrySdkHint(invocation.getArgument(1)) as DiskFlushNotification thread { @@ -197,10 +197,10 @@ class UncaughtExceptionHandlerIntegrationTest { val sut = fixture.getSut(flushTimeoutMillis = 5000) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // shouldn't fall into timed out state, because we marked event as flushed on another thread verify(fixture.logger, never()).log( any(), @@ -211,14 +211,14 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `does not block flushing when the event was dropped`() { - whenever(fixture.hub.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) + whenever(fixture.scopes.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // we do not call markFlushed, hence it should time out waiting for flush, but because // we drop the event, it should not even come to this if-check verify(fixture.logger, never()).log( @@ -231,17 +231,17 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `waits for event to flush on disk if it was dropped by multithreaded deduplicator`() { val hintCaptor = argumentCaptor() - whenever(fixture.hub.captureEvent(any(), hintCaptor.capture())).thenAnswer { + whenever(fixture.scopes.captureEvent(any(), hintCaptor.capture())).thenAnswer { HintUtils.setEventDropReason(hintCaptor.firstValue, MULTITHREADED_DEDUPLICATION) return@thenAnswer SentryId.EMPTY_ID } val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) // we do not call markFlushed, even though we dropped the event, the reason was // MULTITHREADED_DEDUPLICATION, so it should time out verify(fixture.logger).log( @@ -254,15 +254,15 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when there is no active transaction on scope, sets current event id as flushable`() { val eventCaptor = argumentCaptor() - whenever(fixture.hub.captureEvent(eventCaptor.capture(), any())) + whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any())) .thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { (HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint) @@ -274,16 +274,16 @@ class UncaughtExceptionHandlerIntegrationTest { @Test fun `when there is active transaction on scope, does not set current event id as flushable`() { val eventCaptor = argumentCaptor() - whenever(fixture.hub.transaction).thenReturn(mock()) - whenever(fixture.hub.captureEvent(eventCaptor.capture(), any())) + whenever(fixture.scopes.transaction).thenReturn(mock()) + whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any())) .thenReturn(SentryId.EMPTY_ID) val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.uncaughtException(fixture.thread, fixture.throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { !(HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint) diff --git a/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt b/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt index c010c97238..cf234574bf 100644 --- a/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt +++ b/sentry/src/test/java/io/sentry/backpressure/BackpressureMonitorTest.kt @@ -1,6 +1,6 @@ package io.sentry.backpressure -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryOptions import io.sentry.backpressure.BackpressureMonitor.MAX_DOWNSAMPLE_FACTOR @@ -17,13 +17,13 @@ class BackpressureMonitorTest { class Fixture { val options = SentryOptions() - val hub = mock() + val scopes = mock() val executor = mock() fun getSut(): BackpressureMonitor { options.executorService = executor whenever(executor.isClosed).thenReturn(false) whenever(executor.schedule(any(), any())).thenReturn(mock>()) - return BackpressureMonitor(options, hub) + return BackpressureMonitor(options, scopes) } } @@ -38,7 +38,7 @@ class BackpressureMonitorTest { @Test fun `downsampleFactor increases with negative health checks up to max`() { val sut = fixture.getSut() - whenever(fixture.hub.isHealthy).thenReturn(false) + whenever(fixture.scopes.isHealthy).thenReturn(false) assertEquals(0, sut.downsampleFactor) (1..MAX_DOWNSAMPLE_FACTOR).forEach { i -> @@ -54,13 +54,13 @@ class BackpressureMonitorTest { @Test fun `downsampleFactor goes back to 0 after positive health check`() { val sut = fixture.getSut() - whenever(fixture.hub.isHealthy).thenReturn(false) + whenever(fixture.scopes.isHealthy).thenReturn(false) assertEquals(0, sut.downsampleFactor) sut.checkHealth() assertEquals(1, sut.downsampleFactor) - whenever(fixture.hub.isHealthy).thenReturn(true) + whenever(fixture.scopes.isHealthy).thenReturn(true) sut.checkHealth() assertEquals(0, sut.downsampleFactor) } diff --git a/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt b/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt index b4615cac76..d27e5c0228 100644 --- a/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt +++ b/sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt @@ -7,7 +7,7 @@ import io.sentry.DataCategory import io.sentry.DateUtils import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.NoOpLogger import io.sentry.ProfilingTraceData import io.sentry.Sentry @@ -47,9 +47,9 @@ class ClientReportTest { @Test fun `lost envelope can be recorded`() { givenClientReportRecorder() - val hub = mock() - whenever(hub.options).thenReturn(opts) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val scopes = mock() + whenever(scopes.options).thenReturn(opts) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val lostClientReport = ClientReport( DateUtils.getCurrentDateTime(), diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt index 00c89f27be..f6271b5b5e 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/FileIOSpanManagerTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.util.PlatformTestManipulator @@ -20,25 +20,25 @@ class FileIOSpanManagerTest { @Test fun `startSpan uses transaction on Android platform`() { - val hub = mock() + val scopes = mock() val transaction = mock() - whenever(hub.transaction).thenReturn(transaction) + whenever(scopes.transaction).thenReturn(transaction) PlatformTestManipulator.pretendIsAndroid(true) - FileIOSpanManager.startSpan(hub, "op.read") + FileIOSpanManager.startSpan(scopes, "op.read") verify(transaction).startChild(any()) } @Test fun `startSpan uses last span on non-Android platforms`() { - val hub = mock() + val scopes = mock() val span = mock() - whenever(hub.span).thenReturn(span) + whenever(scopes.span).thenReturn(span) PlatformTestManipulator.pretendIsAndroid(false) - FileIOSpanManager.startSpan(hub, "op.read") + FileIOSpanManager.startSpan(scopes, "op.read") verify(span).startChild(any()) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt index 5e27eb451d..063b6d6428 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class SentryFileInputStreamTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer private val options = SentryOptions() @@ -40,21 +40,21 @@ class SentryFileInputStreamTest { sendDefaultPii: Boolean = false ): SentryFileInputStream { tmpFile?.writeText("Text") - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( options.apply { isSendDefaultPii = sendDefaultPii mainThreadChecker = MainThreadChecker.getInstance() addInAppInclude("org.junit") } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return if (fileDescriptor == null) { - SentryFileInputStream(tmpFile, hub) + SentryFileInputStream(tmpFile, scopes) } else { - SentryFileInputStream(fileDescriptor, hub) + SentryFileInputStream(fileDescriptor, scopes) } } @@ -62,13 +62,13 @@ class SentryFileInputStreamTest { tmpFile: File? = null, delegate: FileInputStream ): SentryFileInputStream { - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) + whenever(scopes.span).thenReturn(sentryTracer) return SentryFileInputStream.Factory.create( delegate, tmpFile, - hub + scopes ) as SentryFileInputStream } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt index f6a09830c2..8b175adc2d 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -24,7 +24,7 @@ import kotlin.test.assertTrue class SentryFileOutputStreamTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -32,17 +32,17 @@ class SentryFileOutputStreamTest { activeTransaction: Boolean = true, append: Boolean = false ): SentryFileOutputStream { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() addInAppInclude("org.junit") } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileOutputStream(tmpFile, append, hub) + return SentryFileOutputStream(tmpFile, append, scopes) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt index 2485579e7a..38781d718b 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -17,7 +17,7 @@ import kotlin.test.assertEquals class SentryFileReaderTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -25,16 +25,16 @@ class SentryFileReaderTest { activeTransaction: Boolean = true ): SentryFileReader { tmpFile.writeText("TEXT") - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileReader(tmpFile, hub) + return SentryFileReader(tmpFile, scopes) } } diff --git a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt index f0738d8725..8f3d96b4d7 100644 --- a/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt +++ b/sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt @@ -1,6 +1,6 @@ package io.sentry.instrumentation.file -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention @@ -17,7 +17,7 @@ import kotlin.test.assertEquals class SentryFileWriterTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var sentryTracer: SentryTracer internal fun getSut( @@ -25,16 +25,16 @@ class SentryFileWriterTest { activeTransaction: Boolean = true, append: Boolean = false ): SentryFileWriter { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { mainThreadChecker = MainThreadChecker.getInstance() } ) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (activeTransaction) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SentryFileWriter(tmpFile, append, hub) + return SentryFileWriter(tmpFile, append, scopes) } } diff --git a/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt b/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt index 73aa339f26..0a91bec562 100644 --- a/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/internal/SpotlightIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.internal -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryOptions.BeforeEnvelopeCallback import io.sentry.SpotlightIntegration @@ -19,7 +19,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertNull(options.beforeEnvelopeCallback) } @@ -33,7 +33,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals(envelopeCallback, options.beforeEnvelopeCallback) } @@ -45,7 +45,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals(options.beforeEnvelopeCallback, spotlight) spotlight.close() @@ -71,7 +71,7 @@ class SpotlightIntegrationTest { } val spotlight = SpotlightIntegration() - spotlight.register(mock(), options) + spotlight.register(mock(), options) assertEquals("http://example.com:1234/stream", spotlight.spotlightConnectionUrl) } diff --git a/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt b/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt index 27499be0a0..d67b0186be 100644 --- a/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt +++ b/sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt @@ -1,6 +1,6 @@ package io.sentry.protocol -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLongDate import io.sentry.SentryTracer import io.sentry.Span @@ -19,7 +19,7 @@ class SentrySpanTest { val span = Span( TransactionContext("name", "op"), mock(), - mock(), + mock(), SentryLongDate(1000000), SpanOptions() ) diff --git a/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt b/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt index d9e217b0ac..2b1be98611 100644 --- a/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt +++ b/sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt @@ -4,7 +4,7 @@ import io.sentry.Attachment import io.sentry.CheckIn import io.sentry.CheckInStatus import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISerializer import io.sentry.NoOpLogger import io.sentry.ProfilingTraceData @@ -80,10 +80,10 @@ class RateLimiterTest { fun `parse X-Sentry-Rate-Limit and set its values and retry after should be true`() { val rateLimiter = fixture.getSUT() whenever(fixture.currentDateProvider.currentTimeMillis).thenReturn(0) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), hub)) + val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), scopes)) val transactionItem = SentryEnvelopeItem.fromEvent(fixture.serializer, transaction) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, transactionItem)) @@ -97,10 +97,10 @@ class RateLimiterTest { fun `parse X-Sentry-Rate-Limit and set its values and retry after should be false`() { val rateLimiter = fixture.getSUT() whenever(fixture.currentDateProvider.currentTimeMillis).thenReturn(0, 0, 1001) - val hub: IHub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes: IScopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) - val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), hub)) + val transaction = SentryTransaction(SentryTracer(TransactionContext("name", "op"), scopes)) val transactionItem = SentryEnvelopeItem.fromEvent(fixture.serializer, transaction) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, transactionItem)) @@ -191,9 +191,9 @@ class RateLimiterTest { it.setName("John Me") } ) - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val sessionItem = SentryEnvelopeItem.fromSession(fixture.serializer, Session("123", User(), "env", "release")) val attachmentItem = SentryEnvelopeItem.fromAttachment(fixture.serializer, NoOpLogger.getInstance(), Attachment("{ \"number\": 10 }".toByteArray(), "log.json"), 1000) @@ -219,8 +219,8 @@ class RateLimiterTest { @Test fun `records only dropped items as lost`() { val rateLimiter = fixture.getSUT() - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) val userFeedbackItem = SentryEnvelopeItem.fromUserFeedback( @@ -233,7 +233,7 @@ class RateLimiterTest { it.setName("John Me") } ) - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val profileItem = SentryEnvelopeItem.fromProfilingTrace(ProfilingTraceData(File(""), transaction), 1000, fixture.serializer) val sessionItem = SentryEnvelopeItem.fromSession(fixture.serializer, Session("123", User(), "env", "release")) val attachmentItem = SentryEnvelopeItem.fromAttachment(fixture.serializer, NoOpLogger.getInstance(), Attachment("{ \"number\": 10 }".toByteArray(), "log.json"), 1000) @@ -253,12 +253,12 @@ class RateLimiterTest { @Test fun `drop profile items as lost`() { val rateLimiter = fixture.getSUT() - val hub = mock() - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + whenever(scopes.options).thenReturn(SentryOptions()) val eventItem = SentryEnvelopeItem.fromEvent(fixture.serializer, SentryEvent()) val f = File.createTempFile("test", "trace") - val transaction = SentryTracer(TransactionContext("name", "op"), hub) + val transaction = SentryTracer(TransactionContext("name", "op"), scopes) val profileItem = SentryEnvelopeItem.fromProfilingTrace(ProfilingTraceData(f, transaction), 1000, fixture.serializer) val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(eventItem, profileItem)) diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index a285cd5832..9f330348b0 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -1,7 +1,8 @@ package io.sentry.util import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.MonitorConfig import io.sentry.MonitorSchedule import io.sentry.MonitorScheduleUnit @@ -56,30 +57,31 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val returnValue = CheckInUtils.withCheckIn("monitor-1") { return@withCheckIn "test1" } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -87,8 +89,9 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with exception`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) try { CheckInUtils.withCheckIn("monitor-1") { @@ -99,22 +102,22 @@ class CheckInUtilsTest { assertEquals("thrown on purpose", e.message) } - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -122,32 +125,33 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with upsert`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)) val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) { "test1" } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertSame(monitorConfig, it.monitorConfig) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -155,9 +159,10 @@ class CheckInUtilsTest { @Test fun `sends check-in for wrapped supplier with upsert and thresholds`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn(SentryOptions()) + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)).apply { failureIssueThreshold = 10 recoveryThreshold = 20 @@ -167,23 +172,23 @@ class CheckInUtilsTest { } assertEquals("test1", returnValue) - inOrder(hub) { - verify(hub).pushScope() - verify(hub).configureScope(any()) - verify(hub).captureCheckIn( + inOrder(scopes) { + verify(scopes).pushScope() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertSame(monitorConfig, it.monitorConfig) assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) } ) - verify(hub).captureCheckIn( + verify(scopes).captureCheckIn( check { assertEquals("monitor-1", it.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(hub).popScope() + verify(scopes).popScope() } } } @@ -191,9 +196,10 @@ class CheckInUtilsTest { @Test fun `sets defaults for MonitorConfig from SentryOptions`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn( + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { defaultCheckinMargin = 20 @@ -218,9 +224,10 @@ class CheckInUtilsTest { @Test fun `defaults for MonitorConfig from SentryOptions can be overridden`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> - val hub = mock() - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) - whenever(hub.options).thenReturn( + val scopes = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { defaultCheckinMargin = 20 diff --git a/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt b/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt index e38410641e..b3f640aeec 100644 --- a/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/TracingUtilsTest.kt @@ -1,7 +1,7 @@ package io.sentry.util import io.sentry.Baggage -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.NoOpSpan import io.sentry.Scope import io.sentry.ScopeCallback @@ -29,18 +29,18 @@ class TracingUtilsTest { val options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val scope = Scope(options) lateinit var span: Span val preExistingBaggage = listOf("some-baggage-key=some-baggage-value") fun setup() { - whenever(hub.options).thenReturn(options) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) span = Span( TransactionContext("name", "op", TracesSamplingDecision(true)), - SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), hub), - hub, + SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), scopes), + scopes, null, SpanOptions() ) @@ -53,7 +53,7 @@ class TracingUtilsTest { fun `returns headers if allowed from scope without span`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -68,7 +68,7 @@ class TracingUtilsTest { fun `returns headers if allowed from scope if span is noop`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, NoOpSpan.getInstance()) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, NoOpSpan.getInstance()) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -83,7 +83,7 @@ class TracingUtilsTest { fixture.scope.propagationContext.baggage = Baggage.fromHeader("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=2722d9f6ec019ade60c776169d9a8904,sentry-transaction=HTTP%20GET").also { it.freeze() } fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -98,7 +98,7 @@ class TracingUtilsTest { fun `returns headers if allowed from span`() { fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNotNull(headers) assertNotNull(headers.baggageHeader) @@ -112,7 +112,7 @@ class TracingUtilsTest { fixture.options.isTraceSampling = false fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNull(headers) } @@ -122,7 +122,7 @@ class TracingUtilsTest { fixture.options.isTraceSampling = false fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNull(headers) } @@ -132,7 +132,7 @@ class TracingUtilsTest { fixture.options.setTracePropagationTargets(listOf("some-host-that-does-not-exist")) fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, null) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, null) assertNull(headers) } @@ -142,7 +142,7 @@ class TracingUtilsTest { fixture.options.setTracePropagationTargets(listOf("some-host-that-does-not-exist")) fixture.setup() - val headers = TracingUtils.traceIfAllowed(fixture.hub, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) + val headers = TracingUtils.traceIfAllowed(fixture.scopes, "https://sentry.io/hello", fixture.preExistingBaggage, fixture.span) assertNull(headers) } @@ -153,7 +153,7 @@ class TracingUtilsTest { val propagationContextBefore = fixture.scope.propagationContext - TracingUtils.startNewTrace(fixture.hub) + TracingUtils.startNewTrace(fixture.scopes) assertNotEquals(propagationContextBefore.traceId, fixture.scope.propagationContext.traceId) assertNotEquals(propagationContextBefore.spanId, fixture.scope.propagationContext.spanId) From ce615f4274bb37d731af4acf68e9d92a56660489 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:15:56 +0200 Subject: [PATCH 05/57] Replace `IHub` with `IScopes` in android core --- .../api/sentry-android-core.api | 28 ++--- .../core/ActivityBreadcrumbsIntegration.java | 17 +-- .../core/ActivityLifecycleIntegration.java | 32 +++--- .../core/AndroidTransactionProfiler.java | 12 +- .../sentry/android/core/AnrIntegration.java | 19 +-- .../sentry/android/core/AnrV2Integration.java | 14 +-- .../AppComponentsBreadcrumbsIntegration.java | 16 +-- .../android/core/AppLifecycleIntegration.java | 14 +-- .../core/CurrentActivityIntegration.java | 4 +- .../core/EnvelopeFileObserverIntegration.java | 14 ++- .../android/core/InternalSentrySdk.java | 24 ++-- .../sentry/android/core/LifecycleWatcher.java | 22 ++-- .../sentry/android/core/NdkIntegration.java | 9 +- .../core/NetworkBreadcrumbsIntegration.java | 21 ++-- .../PhoneStateBreadcrumbsIntegration.java | 20 ++-- .../core/SendCachedEnvelopeIntegration.java | 20 ++-- .../io/sentry/android/core/SentryAndroid.java | 11 +- .../SystemEventsBreadcrumbsIntegration.java | 20 ++-- .../TempSensorBreadcrumbsIntegration.java | 12 +- .../core/UserInteractionIntegration.java | 12 +- .../gestures/SentryGestureListener.java | 18 +-- .../core/AndroidTransactionProfilerTest.kt | 12 +- .../sentry/android/core/AnrIntegrationTest.kt | 28 ++--- .../android/core/AnrV2IntegrationTest.kt | 90 +++++++-------- ...AppComponentsBreadcrumbsIntegrationTest.kt | 48 ++++---- .../core/AppLifecycleIntegrationTest.kt | 16 +-- .../core/CurrentActivityIntegrationTest.kt | 6 +- .../core/DefaultAndroidEventProcessorTest.kt | 8 +- .../EnvelopeFileObserverIntegrationTest.kt | 20 ++-- .../android/core/LifecycleWatcherTest.kt | 48 ++++---- .../sentry/android/core/NdkIntegrationTest.kt | 22 ++-- .../core/NetworkBreadcrumbsIntegrationTest.kt | 108 +++++++++--------- .../PerformanceAndroidEventProcessorTest.kt | 20 ++-- .../PhoneStateBreadcrumbsIntegrationTest.kt | 38 +++--- .../core/SendCachedEnvelopeIntegrationTest.kt | 30 ++--- .../SystemEventsBreadcrumbsIntegrationTest.kt | 24 ++-- .../TempSensorBreadcrumbsIntegrationTest.kt | 32 +++--- .../SentryGestureListenerClickTest.kt | 22 ++-- .../SentryGestureListenerScrollTest.kt | 26 ++--- .../SentryGestureListenerTracingTest.kt | 58 +++++----- 40 files changed, 511 insertions(+), 504 deletions(-) diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 8eb017346d..2afe788ef8 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -8,7 +8,7 @@ public final class io/sentry/android/core/ActivityBreadcrumbsIntegration : andro public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ActivityFramesTracker { @@ -33,7 +33,7 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AndroidCpuCollector : io/sentry/IPerformanceSnapshotCollector { @@ -87,7 +87,7 @@ public class io/sentry/android/core/AndroidProfiler$ProfileStartData { public final class io/sentry/android/core/AnrIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AnrIntegrationFactory { @@ -104,7 +104,7 @@ public final class io/sentry/android/core/AnrV2EventProcessor : io/sentry/Backfi public class io/sentry/android/core/AnrV2Integration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AnrV2Integration$AnrV2Hint : io/sentry/hints/BlockingFlushHint, io/sentry/hints/AbnormalExit, io/sentry/hints/Backfillable { @@ -123,13 +123,13 @@ public final class io/sentry/android/core/AppComponentsBreadcrumbsIntegration : public fun onConfigurationChanged (Landroid/content/res/Configuration;)V public fun onLowMemory ()V public fun onTrimMemory (I)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AppLifecycleIntegration : io/sentry/Integration, java/io/Closeable { public fun ()V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/AppState { @@ -177,7 +177,7 @@ public final class io/sentry/android/core/CurrentActivityIntegration : android/a public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/DeviceInfoUtil { @@ -193,7 +193,7 @@ public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : i public fun ()V public fun close ()V public static fun getOutboxFileObserver ()Lio/sentry/android/core/EnvelopeFileObserverIntegration; - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public abstract interface class io/sentry/android/core/IDebugImagesLoader { @@ -219,19 +219,19 @@ public final class io/sentry/android/core/NdkIntegration : io/sentry/Integration public static final field SENTRY_NDK_CLASS_NAME Ljava/lang/String; public fun (Ljava/lang/Class;)V public fun close ()V - public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/NetworkBreadcrumbsIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/ILogger;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/PhoneStateBreadcrumbsIntegration : io/sentry/Integration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor { @@ -360,7 +360,7 @@ public final class io/sentry/android/core/SystemEventsBreadcrumbsIntegration : i public fun (Landroid/content/Context;)V public fun (Landroid/content/Context;Ljava/util/List;)V public fun close ()V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/TempSensorBreadcrumbsIntegration : android/hardware/SensorEventListener, io/sentry/Integration, java/io/Closeable { @@ -368,7 +368,7 @@ public final class io/sentry/android/core/TempSensorBreadcrumbsIntegration : and public fun close ()V public fun onAccuracyChanged (Landroid/hardware/Sensor;I)V public fun onSensorChanged (Landroid/hardware/SensorEvent;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/UserInteractionIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/Integration, java/io/Closeable { @@ -381,7 +381,7 @@ public final class io/sentry/android/core/UserInteractionIntegration : android/a public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentry/EventProcessor { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java index dc03abe808..4a5ca63717 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityBreadcrumbsIntegration.java @@ -8,7 +8,7 @@ import android.os.Bundle; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -23,7 +23,7 @@ public final class ActivityBreadcrumbsIntegration implements Integration, Closeable, Application.ActivityLifecycleCallbacks { private final @NotNull Application application; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private boolean enabled; public ActivityBreadcrumbsIntegration(final @NotNull Application application) { @@ -31,13 +31,13 @@ public ActivityBreadcrumbsIntegration(final @NotNull Application application) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { final SentryAndroidOptions androidOptions = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.enabled = androidOptions.isEnableActivityLifecycleBreadcrumbs(); options .getLogger() @@ -54,8 +54,9 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio public void close() throws IOException { if (enabled) { application.unregisterActivityLifecycleCallbacks(this); - if (hub != null) { - hub.getOptions() + if (scopes != null) { + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "ActivityBreadcrumbsIntegration removed."); } @@ -100,7 +101,7 @@ public synchronized void onActivityDestroyed(final @NotNull Activity activity) { } private void addBreadcrumb(final @NotNull Activity activity, final @NotNull String state) { - if (hub == null) { + if (scopes == null) { return; } @@ -114,7 +115,7 @@ private void addBreadcrumb(final @NotNull Activity activity, final @NotNull Stri final Hint hint = new Hint(); hint.set(ANDROID_ACTIVITY, activity); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } private @NotNull String getActivityName(final @NotNull Activity activity) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java index 1121a6bfe7..76bedae5d0 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java @@ -12,8 +12,8 @@ import android.view.View; import androidx.annotation.NonNull; import io.sentry.FullyDisplayedReporter; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.Instrumenter; @@ -60,7 +60,7 @@ public final class ActivityLifecycleIntegration private final @NotNull Application application; private final @NotNull BuildInfoProvider buildInfoProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private boolean performanceEnabled = false; @@ -102,13 +102,13 @@ public ActivityLifecycleIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); performanceEnabled = isPerformanceEnabled(this.options); fullyDisplayedReporter = this.options.getFullyDisplayedReporter(); @@ -150,10 +150,10 @@ private void stopPreviousTransactions() { private void startTracing(final @NotNull Activity activity) { WeakReference weakActivity = new WeakReference<>(activity); - if (hub != null && !isRunningTransactionOrTrace(activity)) { + if (scopes != null && !isRunningTransactionOrTrace(activity)) { if (!performanceEnabled) { activitiesWithOngoingTransactions.put(activity, NoOpTransaction.getInstance()); - TracingUtils.startNewTrace(hub); + TracingUtils.startNewTrace(scopes); } else { // as we allow a single transaction running on the bound Scope, we finish the previous ones stopPreviousTransactions(); @@ -225,7 +225,7 @@ private void startTracing(final @NotNull Activity activity) { // we can only bind to the scope if there's no running transaction ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext( activityName, TransactionNameSource.COMPONENT, @@ -278,7 +278,7 @@ private void startTracing(final @NotNull Activity activity) { } // lets bind to the scope so other integrations can pick it up - hub.configureScope( + scopes.configureScope( scope -> { applyScope(scope, transaction); }); @@ -356,10 +356,10 @@ private void finishTransaction( status = SpanStatus.OK; } transaction.finish(status); - if (hub != null) { + if (scopes != null) { // make sure to remove the transaction from scope, as it may contain running children, // therefore `finish` method will not remove it from scope - hub.configureScope( + scopes.configureScope( scope -> { clearScope(scope, transaction); }); @@ -371,9 +371,9 @@ private void finishTransaction( public synchronized void onActivityCreated( final @NotNull Activity activity, final @Nullable Bundle savedInstanceState) { setColdStart(savedInstanceState); - if (hub != null) { + if (scopes != null) { final @Nullable String activityClassName = ClassUtil.getClassName(activity); - hub.configureScope(scope -> scope.setScreen(activityClassName)); + scopes.configureScope(scope -> scope.setScreen(activityClassName)); } startTracing(activity); final @Nullable ISpan ttfdSpan = ttfdSpanMap.get(activity); @@ -429,10 +429,10 @@ public void onActivityPrePaused(@NonNull Activity activity) { // well // this ensures any newly launched activity will not use the app start timestamp as txn start firstActivityCreated = true; - if (hub == null) { + if (scopes == null) { lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime(); } else { - lastPausedTime = hub.getOptions().getDateProvider().now(); + lastPausedTime = scopes.getOptions().getDateProvider().now(); } } } @@ -445,10 +445,10 @@ public synchronized void onActivityPaused(final @NotNull Activity activity) { // well // this ensures any newly launched activity will not use the app start timestamp as txn start firstActivityCreated = true; - if (hub == null) { + if (scopes == null) { lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime(); } else { - lastPausedTime = hub.getOptions().getDateProvider().now(); + lastPausedTime = scopes.getOptions().getDateProvider().now(); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java index 3abfe1306a..0d232a6ada 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java @@ -9,15 +9,15 @@ import android.os.Build; import android.os.Process; import android.os.SystemClock; -import io.sentry.HubAdapter; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.ISentryExecutorService; import io.sentry.ITransaction; import io.sentry.ITransactionProfiler; import io.sentry.PerformanceCollectionData; import io.sentry.ProfilingTraceData; import io.sentry.ProfilingTransactionData; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.android.core.internal.util.CpuInfoUtils; @@ -46,8 +46,8 @@ final class AndroidTransactionProfiler implements ITransactionProfiler { private long profileStartCpuMillis; /** - * @deprecated please use a constructor that doesn't takes a {@link IHub} instead, as it would be - * ignored anyway. + * @deprecated please use a constructor that doesn't takes a {@link IScopes} instead, as it would + * be ignored anyway. */ @Deprecated public AndroidTransactionProfiler( @@ -55,7 +55,7 @@ public AndroidTransactionProfiler( final @NotNull SentryAndroidOptions sentryAndroidOptions, final @NotNull BuildInfoProvider buildInfoProvider, final @NotNull SentryFrameMetricsCollector frameMetricsCollector, - final @NotNull IHub hub) { + final @NotNull IScopes scopes) { this(context, sentryAndroidOptions, buildInfoProvider, frameMetricsCollector); } @@ -311,7 +311,7 @@ public void close() { currentProfilingTransactionData.getTraceId(), true, null, - HubAdapter.getInstance().getOptions()); + ScopesAdapter.getInstance().getOptions()); } else if (transactionsCounter != 0) { // in case the app start profiling is running, and it's not bound to a transaction, we still // stop profiling, but we also have to manually update the counter. diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java index 0ad2c242da..90d53a3c9b 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java @@ -5,7 +5,7 @@ import android.annotation.SuppressLint; import android.content.Context; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -48,12 +48,13 @@ public AnrIntegration(final @NotNull Context context) { private static final @NotNull Object watchDogLock = new Object(); @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { this.options = Objects.requireNonNull(options, "SentryOptions is required"); - register(hub, (SentryAndroidOptions) options); + register(scopes, (SentryAndroidOptions) options); } - private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + private void register( + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { options .getLogger() .log(SentryLevel.DEBUG, "AnrIntegration enabled: %s", options.isAnrEnabled()); @@ -67,7 +68,7 @@ private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptio () -> { synchronized (startLock) { if (!isClosed) { - startAnrWatchdog(hub, options); + startAnrWatchdog(scopes, options); } } }); @@ -80,7 +81,7 @@ private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptio } private void startAnrWatchdog( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { synchronized (watchDogLock) { if (anrWatchDog == null) { options @@ -94,7 +95,7 @@ private void startAnrWatchdog( new ANRWatchDog( options.getAnrTimeoutIntervalMillis(), options.isAnrReportInDebug(), - error -> reportANR(hub, options, error), + error -> reportANR(scopes, options, error), options.getLogger(), context); anrWatchDog.start(); @@ -106,7 +107,7 @@ private void startAnrWatchdog( @TestOnly void reportANR( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options, final @NotNull ApplicationNotResponding error) { options.getLogger().log(SentryLevel.INFO, "ANR triggered with message: %s", error.getMessage()); @@ -122,7 +123,7 @@ void reportANR( final AnrHint anrHint = new AnrHint(isAppInBackground); final Hint hint = HintUtils.createWithTypeCheckHint(anrHint); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } private @NotNull Throwable buildAnrThrowable( diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java index 669233bb09..152ceed322 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java @@ -9,8 +9,8 @@ import io.sentry.Attachment; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -69,7 +69,7 @@ public AnrV2Integration(final @NotNull Context context) { @SuppressLint("NewApi") // we do the check in the AnrIntegrationFactory @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -90,7 +90,7 @@ public void register(@NotNull IHub hub, @NotNull SentryOptions options) { try { options .getExecutorService() - .submit(new AnrProcessor(context, hub, this.options, dateProvider)); + .submit(new AnrProcessor(context, scopes, this.options, dateProvider)); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to start AnrProcessor.", e); } @@ -109,17 +109,17 @@ public void close() throws IOException { static class AnrProcessor implements Runnable { private final @NotNull Context context; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryAndroidOptions options; private final long threshold; AnrProcessor( final @NotNull Context context, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options, final @NotNull ICurrentDateProvider dateProvider) { this.context = context; - this.hub = hub; + this.scopes = scopes; this.options = options; this.threshold = dateProvider.getCurrentTimeMillis() - NINETY_DAYS_THRESHOLD; } @@ -277,7 +277,7 @@ private void reportAsSentryEvent( } } - final @NotNull SentryId sentryId = hub.captureEvent(event, hint); + final @NotNull SentryId sentryId = scopes.captureEvent(event, hint); final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID); if (!isEventDropped) { // Block until the event is flushed to disk and the last_reported_anr marker is updated diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java index eef4707683..0d20dc7a90 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java @@ -8,7 +8,7 @@ import android.content.res.Configuration; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -25,7 +25,7 @@ public final class AppComponentsBreadcrumbsIntegration implements Integration, Closeable, ComponentCallbacks2 { private final @NotNull Context context; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { @@ -33,8 +33,8 @@ public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -84,7 +84,7 @@ public void close() throws IOException { @SuppressWarnings("deprecation") @Override public void onConfigurationChanged(@NotNull Configuration newConfig) { - if (hub != null) { + if (scopes != null) { final Device.DeviceOrientation deviceOrientation = DeviceOrientations.getOrientation(context.getResources().getConfiguration().orientation); @@ -104,7 +104,7 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) { final Hint hint = new Hint(); hint.set(ANDROID_CONFIGURATION, newConfig); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } @@ -119,7 +119,7 @@ public void onTrimMemory(final int level) { } private void createLowMemoryBreadcrumb(final @Nullable Integer level) { - if (hub != null) { + if (scopes != null) { final Breadcrumb breadcrumb = new Breadcrumb(); if (level != null) { // only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or @@ -143,7 +143,7 @@ private void createLowMemoryBreadcrumb(final @Nullable Integer level) { breadcrumb.setMessage("Low memory"); breadcrumb.setData("action", "LOW_MEMORY"); breadcrumb.setLevel(SentryLevel.WARNING); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java index 3e8fe6383f..8614a60061 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java @@ -3,7 +3,7 @@ import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; import androidx.lifecycle.ProcessLifecycleOwner; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -32,8 +32,8 @@ public AppLifecycleIntegration() { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -59,11 +59,11 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio Class.forName("androidx.lifecycle.DefaultLifecycleObserver"); Class.forName("androidx.lifecycle.ProcessLifecycleOwner"); if (AndroidMainThreadChecker.getInstance().isMainThread()) { - addObserver(hub); + addObserver(scopes); } else { // some versions of the androidx lifecycle-process require this to be executed on the main // thread. - handler.post(() -> addObserver(hub)); + handler.post(() -> addObserver(scopes)); } } catch (ClassNotFoundException e) { options @@ -80,7 +80,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } } - private void addObserver(final @NotNull IHub hub) { + private void addObserver(final @NotNull IScopes scopes) { // this should never happen, check added to avoid warnings from NullAway if (this.options == null) { return; @@ -88,7 +88,7 @@ private void addObserver(final @NotNull IHub hub) { watcher = new LifecycleWatcher( - hub, + scopes, this.options.getSessionTrackingIntervalMillis(), this.options.isEnableAutoSessionTracking(), this.options.isEnableAppLifecycleBreadcrumbs()); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java index b4c5f1ed02..0b618636d3 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/CurrentActivityIntegration.java @@ -4,7 +4,7 @@ import android.app.Application; import android.os.Bundle; import androidx.annotation.NonNull; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryOptions; import io.sentry.util.Objects; @@ -25,7 +25,7 @@ public CurrentActivityIntegration(final @NotNull Application application) { } @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { application.registerActivityLifecycleCallbacks(this); } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java index f99294584b..6e821e5be7 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java @@ -1,7 +1,7 @@ package io.sentry.android.core; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.OutboxSender; import io.sentry.SentryLevel; @@ -24,8 +24,8 @@ public abstract class EnvelopeFileObserverIntegration implements Integration, Cl } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); logger = options.getLogger(); @@ -46,7 +46,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions () -> { synchronized (startLock) { if (!isClosed) { - startOutboxSender(hub, options, path); + startOutboxSender(scopes, options, path); } } }); @@ -60,10 +60,12 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions } private void startOutboxSender( - final @NotNull IHub hub, final @NotNull SentryOptions options, final @NotNull String path) { + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull String path) { final OutboxSender outboxSender = new OutboxSender( - hub, + scopes, options.getEnvelopeReader(), options.getSerializer(), options.getLogger(), diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index 9bdbe86a77..692d8562f8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -4,12 +4,12 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; import io.sentry.ILogger; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ISerializer; import io.sentry.ObjectWriter; +import io.sentry.ScopesAdapter; import io.sentry.SentryEnvelope; import io.sentry.SentryEnvelopeItem; import io.sentry.SentryEvent; @@ -39,12 +39,12 @@ public final class InternalSentrySdk { /** - * @return a copy of the current hub's topmost scope, or null in case the hub is disabled + * @return a copy of the current scopes's topmost scope, or null in case the scopes is disabled */ @Nullable public static IScope getCurrentScope() { final @NotNull AtomicReference scopeRef = new AtomicReference<>(); - HubAdapter.getInstance() + ScopesAdapter.getInstance() .configureScope( scope -> { scopeRef.set(scope.clone()); @@ -134,8 +134,8 @@ public static Map serializeScope( } /** - * Captures the provided envelope. Compared to {@link IHub#captureEvent(SentryEvent)} this method - *
+ * Captures the provided envelope. Compared to {@link IScopes#captureEvent(SentryEvent)} this + * method
* - will not enrich events with additional data (e.g. scope)
* - will not execute beforeSend: it's up to the caller to take care of this
* - will not perform any sampling: it's up to the caller to take care of this
@@ -147,8 +147,8 @@ public static Map serializeScope( */ @Nullable public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { - final @NotNull IHub hub = HubAdapter.getInstance(); - final @NotNull SentryOptions options = hub.getOptions(); + final @NotNull IScopes scopes = ScopesAdapter.getInstance(); + final @NotNull SentryOptions options = scopes.getOptions(); try (final InputStream envelopeInputStream = new ByteArrayInputStream(envelopeData)) { final @NotNull ISerializer serializer = options.getSerializer(); @@ -178,7 +178,7 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { } // update session and add it to envelope if necessary - final @Nullable Session session = updateSession(hub, options, status, crashedOrErrored); + final @Nullable Session session = updateSession(scopes, options, status, crashedOrErrored); if (session != null) { final SentryEnvelopeItem sessionItem = SentryEnvelopeItem.fromSession(serializer, session); envelopeItems.add(sessionItem); @@ -186,7 +186,7 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { final SentryEnvelope repackagedEnvelope = new SentryEnvelope(envelope.getHeader(), envelopeItems); - return hub.captureEnvelope(repackagedEnvelope); + return scopes.captureEnvelope(repackagedEnvelope); } catch (Throwable t) { options.getLogger().log(SentryLevel.ERROR, "Failed to capture envelope", t); } @@ -195,12 +195,12 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) { @Nullable private static Session updateSession( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryOptions options, final @Nullable Session.State status, final boolean crashedOrErrored) { final @NotNull AtomicReference sessionRef = new AtomicReference<>(); - hub.configureScope( + scopes.configureScope( scope -> { final @Nullable Session session = scope.getSession(); if (session != null) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java b/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java index 7b38bcd9c2..a32fa51d3f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/LifecycleWatcher.java @@ -3,7 +3,7 @@ import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.LifecycleOwner; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.Session; import io.sentry.android.core.internal.util.BreadcrumbFactory; @@ -25,19 +25,19 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { private @Nullable TimerTask timerTask; private final @Nullable Timer timer; private final @NotNull Object timerLock = new Object(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final boolean enableSessionTracking; private final boolean enableAppLifecycleBreadcrumbs; private final @NotNull ICurrentDateProvider currentDateProvider; LifecycleWatcher( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final long sessionIntervalMillis, final boolean enableSessionTracking, final boolean enableAppLifecycleBreadcrumbs) { this( - hub, + scopes, sessionIntervalMillis, enableSessionTracking, enableAppLifecycleBreadcrumbs, @@ -45,7 +45,7 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { } LifecycleWatcher( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final long sessionIntervalMillis, final boolean enableSessionTracking, final boolean enableAppLifecycleBreadcrumbs, @@ -53,7 +53,7 @@ final class LifecycleWatcher implements DefaultLifecycleObserver { this.sessionIntervalMillis = sessionIntervalMillis; this.enableSessionTracking = enableSessionTracking; this.enableAppLifecycleBreadcrumbs = enableAppLifecycleBreadcrumbs; - this.hub = hub; + this.scopes = scopes; this.currentDateProvider = currentDateProvider; if (enableSessionTracking) { timer = new Timer(true); @@ -79,7 +79,7 @@ private void startSession() { final long currentTimeMillis = currentDateProvider.getCurrentTimeMillis(); - hub.configureScope( + scopes.configureScope( scope -> { if (lastUpdatedSession.get() == 0L) { final @Nullable Session currentSession = scope.getSession(); @@ -93,7 +93,7 @@ private void startSession() { if (lastUpdatedSession == 0L || (lastUpdatedSession + sessionIntervalMillis) <= currentTimeMillis) { addSessionBreadcrumb("start"); - hub.startSession(); + scopes.startSession(); } this.lastUpdatedSession.set(currentTimeMillis); } @@ -123,7 +123,7 @@ private void scheduleEndSession() { @Override public void run() { addSessionBreadcrumb("end"); - hub.endSession(); + scopes.endSession(); } }; @@ -148,13 +148,13 @@ private void addAppBreadcrumb(final @NotNull String state) { breadcrumb.setData("state", state); breadcrumb.setCategory("app.lifecycle"); breadcrumb.setLevel(SentryLevel.INFO); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } private void addSessionBreadcrumb(final @NotNull String state) { final Breadcrumb breadcrumb = BreadcrumbFactory.forSession(state); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } @TestOnly diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java index 3a4a91498e..dc464303c6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java @@ -2,7 +2,7 @@ import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -28,8 +28,8 @@ public NdkIntegration(final @Nullable Class sentryNdkClass) { } @Override - public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -38,7 +38,8 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions final boolean enabled = this.options.isEnableNdk(); this.options.getLogger().log(SentryLevel.DEBUG, "NdkIntegration enabled: %s", enabled); - // Note: `hub` isn't used here because the NDK integration writes files to disk which are picked + // Note: `scopes` isn't used here because the NDK integration writes files to disk which are + // picked // up by another integration (EnvelopeFileObserverIntegration). if (enabled && sentryNdkClass != null) { final String cachedDir = this.options.getCacheDirPath(); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java index 1cd42e9dab..9f1dd3ecf6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/NetworkBreadcrumbsIntegration.java @@ -13,8 +13,8 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryDateProvider; import io.sentry.SentryLevel; @@ -50,8 +50,8 @@ public NetworkBreadcrumbsIntegration( @SuppressLint("NewApi") @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); SentryAndroidOptions androidOptions = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -72,7 +72,8 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } networkCallback = - new NetworkBreadcrumbsNetworkCallback(hub, buildInfoProvider, options.getDateProvider()); + new NetworkBreadcrumbsNetworkCallback( + scopes, buildInfoProvider, options.getDateProvider()); final boolean registered = AndroidConnectionStatusProvider.registerNetworkCallback( context, logger, buildInfoProvider, networkCallback); @@ -101,7 +102,7 @@ public void close() throws IOException { @SuppressLint("ObsoleteSdkInt") @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager.NetworkCallback { - final @NotNull IHub hub; + final @NotNull IScopes scopes; final @NotNull BuildInfoProvider buildInfoProvider; @Nullable Network currentNetwork = null; @@ -111,10 +112,10 @@ static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager final @NotNull SentryDateProvider dateProvider; NetworkBreadcrumbsNetworkCallback( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull BuildInfoProvider buildInfoProvider, final @NotNull SentryDateProvider dateProvider) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.buildInfoProvider = Objects.requireNonNull(buildInfoProvider, "BuildInfoProvider is required"); this.dateProvider = Objects.requireNonNull(dateProvider, "SentryDateProvider is required"); @@ -126,7 +127,7 @@ public void onAvailable(final @NonNull Network network) { return; } final Breadcrumb breadcrumb = createBreadcrumb("NETWORK_AVAILABLE"); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); currentNetwork = network; lastCapabilities = null; } @@ -156,7 +157,7 @@ public void onCapabilitiesChanged( } Hint hint = new Hint(); hint.set(TypeCheckHint.ANDROID_NETWORK_CAPABILITIES, connectionDetail); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } @Override @@ -165,7 +166,7 @@ public void onLost(final @NonNull Network network) { return; } final Breadcrumb breadcrumb = createBreadcrumb("NETWORK_LOST"); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); currentNetwork = null; lastCapabilities = null; } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java index c10d25b057..cae1492d3e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegration.java @@ -6,7 +6,7 @@ import android.content.Context; import android.telephony.TelephonyManager; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -32,8 +32,8 @@ public PhoneStateBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -55,7 +55,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio () -> { synchronized (startLock) { if (!isClosed) { - startTelephonyListener(hub, options); + startTelephonyListener(scopes, options); } } }); @@ -72,11 +72,11 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio @SuppressWarnings("deprecation") private void startTelephonyListener( - final @NotNull IHub hub, final @NotNull SentryOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryOptions options) { telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { try { - listener = new PhoneStateChangeListener(hub); + listener = new PhoneStateChangeListener(scopes); telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE); options.getLogger().log(SentryLevel.DEBUG, "PhoneStateBreadcrumbsIntegration installed."); @@ -110,10 +110,10 @@ public void close() throws IOException { @SuppressWarnings("deprecation") static final class PhoneStateChangeListener extends android.telephony.PhoneStateListener { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - PhoneStateChangeListener(final @NotNull IHub hub) { - this.hub = hub; + PhoneStateChangeListener(final @NotNull IScopes scopes) { + this.scopes = scopes; } @SuppressWarnings("deprecation") @@ -128,7 +128,7 @@ public void onCallStateChanged(int state, String incomingNumber) { breadcrumb.setData("action", "CALL_STATE_RINGING"); breadcrumb.setMessage("Device ringing"); breadcrumb.setLevel(SentryLevel.INFO); - hub.addBreadcrumb(breadcrumb); + scopes.addBreadcrumb(breadcrumb); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java index 66e534bb7d..64f1cab362 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java @@ -2,7 +2,7 @@ import io.sentry.DataCategory; import io.sentry.IConnectionStatusProvider; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SendCachedEnvelopeFireAndForgetIntegration; import io.sentry.SentryLevel; @@ -28,7 +28,7 @@ final class SendCachedEnvelopeIntegration private final @NotNull LazyEvaluator startupCrashMarkerEvaluator; private final AtomicBoolean startupCrashHandled = new AtomicBoolean(false); private @Nullable IConnectionStatusProvider connectionStatusProvider; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget sender; private final AtomicBoolean isInitialized = new AtomicBoolean(false); @@ -42,8 +42,8 @@ public SendCachedEnvelopeIntegration( } @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -55,7 +55,7 @@ public void register(@NotNull IHub hub, @NotNull SentryOptions options) { return; } - sendCachedEnvelopes(hub, this.options); + sendCachedEnvelopes(scopes, this.options); } @Override @@ -69,14 +69,14 @@ public void close() throws IOException { @Override public void onConnectionStatusChanged( final @NotNull IConnectionStatusProvider.ConnectionStatus status) { - if (hub != null && options != null) { - sendCachedEnvelopes(hub, options); + if (scopes != null && options != null) { + sendCachedEnvelopes(scopes, options); } } @SuppressWarnings({"NullAway"}) private synchronized void sendCachedEnvelopes( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { try { final Future future = options @@ -97,7 +97,7 @@ private synchronized void sendCachedEnvelopes( connectionStatusProvider = options.getConnectionStatusProvider(); connectionStatusProvider.addConnectionStatusObserver(this); - sender = factory.create(hub, options); + sender = factory.create(scopes, options); } if (connectionStatusProvider != null @@ -110,7 +110,7 @@ private synchronized void sendCachedEnvelopes( } // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = hub.getRateLimiter(); + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { options diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index af68a026fb..424de4d82e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -4,8 +4,8 @@ import android.content.Context; import android.os.Process; import android.os.SystemClock; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.OptionsContainer; import io.sentry.Sentry; @@ -144,10 +144,11 @@ public static synchronized void init( }, true); - final @NotNull IHub hub = Sentry.getCurrentHub(); - if (hub.getOptions().isEnableAutoSessionTracking() && ContextUtils.isForegroundImportance()) { - hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start")); - hub.startSession(); + final @NotNull IScopes scopes = Sentry.getCurrentScopes(); + if (scopes.getOptions().isEnableAutoSessionTracking() + && ContextUtils.isForegroundImportance()) { + scopes.addBreadcrumb(BreadcrumbFactory.forSession("session.start")); + scopes.startSession(); } } catch (IllegalAccessException e) { logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java index 1c22a7dcc8..333ece2148 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java @@ -40,8 +40,8 @@ import android.os.Bundle; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.ILogger; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -80,8 +80,8 @@ public SystemEventsBreadcrumbsIntegration( } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -103,7 +103,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio () -> { synchronized (startLock) { if (!isClosed) { - startSystemEventsReceiver(hub, (SentryAndroidOptions) options); + startSystemEventsReceiver(scopes, (SentryAndroidOptions) options); } } }); @@ -119,8 +119,8 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio } private void startSystemEventsReceiver( - final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) { - receiver = new SystemEventsBroadcastReceiver(hub, options.getLogger()); + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { + receiver = new SystemEventsBroadcastReceiver(scopes, options.getLogger()); final IntentFilter filter = new IntentFilter(); for (String item : actions) { filter.addAction(item); @@ -204,11 +204,11 @@ public void close() throws IOException { static final class SystemEventsBroadcastReceiver extends BroadcastReceiver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull ILogger logger; - SystemEventsBroadcastReceiver(final @NotNull IHub hub, final @NotNull ILogger logger) { - this.hub = hub; + SystemEventsBroadcastReceiver(final @NotNull IScopes scopes, final @NotNull ILogger logger) { + this.scopes = scopes; this.logger = logger; } @@ -249,7 +249,7 @@ public void onReceive(Context context, Intent intent) { final Hint hint = new Hint(); hint.set(ANDROID_INTENT, intent); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java index eaf5c64991..4d0e9c7e60 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/TempSensorBreadcrumbsIntegration.java @@ -11,7 +11,7 @@ import android.hardware.SensorManager; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -26,7 +26,7 @@ public final class TempSensorBreadcrumbsIntegration implements Integration, Closeable, SensorEventListener { private final @NotNull Context context; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; @TestOnly @Nullable SensorManager sensorManager; @@ -38,8 +38,8 @@ public TempSensorBreadcrumbsIntegration(final @NotNull Context context) { } @Override - public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) { - this.hub = Objects.requireNonNull(hub, "Hub is required"); + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -121,7 +121,7 @@ public void onSensorChanged(final @NotNull SensorEvent event) { return; } - if (hub != null) { + if (scopes != null) { final Breadcrumb breadcrumb = new Breadcrumb(); breadcrumb.setType("system"); breadcrumb.setCategory("device.event"); @@ -134,7 +134,7 @@ public void onSensorChanged(final @NotNull SensorEvent event) { final Hint hint = new Hint(); hint.set(ANDROID_SENSOR_EVENT, event); - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java index c361529671..712651b460 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/UserInteractionIntegration.java @@ -6,7 +6,7 @@ import android.app.Application; import android.os.Bundle; import android.view.Window; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Integration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -23,7 +23,7 @@ public final class UserInteractionIntegration implements Integration, Closeable, Application.ActivityLifecycleCallbacks { private final @NotNull Application application; - private @Nullable IHub hub; + private @Nullable IScopes scopes; private @Nullable SentryAndroidOptions options; private final boolean isAndroidXAvailable; @@ -44,14 +44,14 @@ private void startTracking(final @NotNull Activity activity) { return; } - if (hub != null && options != null) { + if (scopes != null && options != null) { Window.Callback delegate = window.getCallback(); if (delegate == null) { delegate = new NoOpWindowCallback(); } final SentryGestureListener gestureListener = - new SentryGestureListener(activity, hub, options); + new SentryGestureListener(activity, scopes, options); window.setCallback(new SentryWindowCallback(delegate, activity, gestureListener, options)); } } @@ -102,13 +102,13 @@ public void onActivitySaveInstanceState(@NotNull Activity activity, @NotNull Bun public void onActivityDestroyed(@NotNull Activity activity) {} @Override - public void register(@NotNull IHub hub, @NotNull SentryOptions options) { + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, "SentryAndroidOptions is required"); - this.hub = Objects.requireNonNull(hub, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); final boolean integrationEnabled = this.options.isEnableUserInteractionBreadcrumbs() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java index 0ec0d83258..9154f3e7c6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryGestureListener.java @@ -10,8 +10,8 @@ import android.view.Window; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.SentryLevel; import io.sentry.SpanStatus; @@ -43,7 +43,7 @@ private enum GestureType { private static final String TRACE_ORIGIN = "auto.ui.gesture_listener"; private final @NotNull WeakReference activityRef; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryAndroidOptions options; private @Nullable UiElement activeUiElement = null; @@ -54,10 +54,10 @@ private enum GestureType { public SentryGestureListener( final @NotNull Activity currentActivity, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { this.activityRef = new WeakReference<>(currentActivity); - this.hub = hub; + this.scopes = scopes; this.options = options; } @@ -185,7 +185,7 @@ private void addBreadcrumb( hint.set(ANDROID_MOTION_EVENT, motionEvent); hint.set(ANDROID_VIEW, target.getView()); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.userInteraction( type, target.getResourceName(), target.getClassName(), target.getTag(), additionalData), hint); @@ -202,7 +202,7 @@ private void startTracing(final @NotNull UiElement target, final @NotNull Gestur if (!(options.isTracingEnabled() && options.isEnableUserInteractionTracing())) { if (isNewInteraction) { - TracingUtils.startNewTrace(hub); + TracingUtils.startNewTrace(scopes); activeUiElement = target; activeEventType = eventType; } @@ -253,12 +253,12 @@ private void startTracing(final @NotNull UiElement target, final @NotNull Gestur transactionOptions.setTrimEnd(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(name, TransactionNameSource.COMPONENT, op), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN + "." + target.getOrigin()); - hub.configureScope( + scopes.configureScope( scope -> { applyScope(scope, transaction); }); @@ -278,7 +278,7 @@ void stopTracing(final @NotNull SpanStatus status) { activeTransaction.finish(); } } - hub.configureScope( + scopes.configureScope( scope -> { // avoid method refs on Android due to some issues with older AGP setups // noinspection Convert2MethodRef diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt index 405aa6dc98..436c3ee6f9 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt @@ -5,8 +5,8 @@ import android.os.Build import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.CpuCollectionData -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.MemoryCollectionData import io.sentry.PerformanceCollectionData @@ -89,7 +89,7 @@ class AndroidTransactionProfilerTest { executorService = mockExecutorService } - val hub: IHub = mock() + val scopes: IScopes = mock() val frameMetricsCollector: SentryFrameMetricsCollector = mock() lateinit var transaction1: SentryTracer @@ -97,10 +97,10 @@ class AndroidTransactionProfilerTest { lateinit var transaction3: SentryTracer fun getSut(context: Context, buildInfoProvider: BuildInfoProvider = buildInfo): AndroidTransactionProfiler { - whenever(hub.options).thenReturn(options) - transaction1 = SentryTracer(TransactionContext("", ""), hub) - transaction2 = SentryTracer(TransactionContext("", ""), hub) - transaction3 = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(options) + transaction1 = SentryTracer(TransactionContext("", ""), scopes) + transaction2 = SentryTracer(TransactionContext("", ""), scopes) + transaction3 = SentryTracer(TransactionContext("", ""), scopes) return AndroidTransactionProfiler(context, options, buildInfoProvider, frameMetricsCollector) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt index cceabc9774..1a74a47ae1 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrIntegrationTest.kt @@ -2,7 +2,7 @@ package io.sentry.android.core import android.content.Context import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.android.core.AnrIntegration.AnrHint import io.sentry.exception.ExceptionMechanismException @@ -24,7 +24,7 @@ class AnrIntegrationTest { private class Fixture { val context = mock() - val hub = mock() + val scopes = mock() var options: SentryAndroidOptions = SentryAndroidOptions().apply { setLogger(mock()) } @@ -49,7 +49,7 @@ class AnrIntegrationTest { fixture.options.executorService = ImmediateExecutorService() val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.anrWatchDog) assertTrue((sut.anrWatchDog as ANRWatchDog).isAlive) @@ -60,7 +60,7 @@ class AnrIntegrationTest { fixture.options.executorService = mock() val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) } @@ -70,7 +70,7 @@ class AnrIntegrationTest { val sut = fixture.getSut() fixture.options.isAnrEnabled = false - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) } @@ -79,9 +79,9 @@ class AnrIntegrationTest { fun `When ANR watch dog is triggered, it should capture an error event with AnrHint`() { val sut = fixture.getSut() - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.ERROR, it.level) }, @@ -97,7 +97,7 @@ class AnrIntegrationTest { val sut = fixture.getSut() fixture.options.executorService = ImmediateExecutorService() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.anrWatchDog) @@ -107,11 +107,11 @@ class AnrIntegrationTest { } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut() fixture.options.executorService = deferredExecutorService - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.anrWatchDog) sut.close() deferredExecutorService.runAll() @@ -122,9 +122,9 @@ class AnrIntegrationTest { fun `When ANR watch dog is triggered, constructs exception with proper mechanism and snapshot flag`() { val sut = fixture.getSut() - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val ex = it.throwableMechanism as ExceptionMechanismException assertTrue(ex.isSnapshot) @@ -139,9 +139,9 @@ class AnrIntegrationTest { val sut = fixture.getSut() AppState.getInstance().setInBackground(true) - sut.reportANR(fixture.hub, fixture.options, getApplicationNotResponding()) + sut.reportANR(fixture.scopes, fixture.options, getApplicationNotResponding()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val message = it.throwable?.message assertTrue(message?.startsWith("Background") == true) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt index 885ad22c8f..1abcd43719 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt @@ -6,8 +6,8 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hint -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryEnvelope import io.sentry.SentryLevel import io.sentry.android.core.AnrV2Integration.AnrV2Hint @@ -59,7 +59,7 @@ class AnrV2IntegrationTest { lateinit var lastReportedAnrFile: File val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val logger = mock() fun getSut( @@ -93,7 +93,7 @@ class AnrV2IntegrationTest { lastReportedAnrFile = File(cacheDir, AndroidEnvelopeCache.LAST_ANR_REPORT) lastReportedAnrFile.writeText(lastReportedAnrTimestamp.toString()) } - whenever(hub.captureEvent(any(), anyOrNull())).thenReturn(lastEventId) + whenever(scopes.captureEvent(any(), anyOrNull())).thenReturn(lastEventId) return AnrV2Integration(context) } @@ -170,7 +170,7 @@ class AnrV2IntegrationTest { fun `when cacheDir is not set, does not process historical exits`() { val integration = fixture.getSut(null, useImmediateExecutorService = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.options.executorService, never()).submit(any()) } @@ -180,7 +180,7 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, isAnrEnabled = false, useImmediateExecutorService = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.options.executorService, never()).submit(any()) } @@ -189,9 +189,9 @@ class AnrV2IntegrationTest { fun `when historical exit list is empty, does not process historical exits`() { val integration = fixture.getSut(tmpDir) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -199,9 +199,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(reason = null) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -212,9 +212,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -222,9 +222,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @Test @@ -232,9 +232,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = null) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @Test @@ -242,9 +242,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(newTimestamp, it.timestamp.time) assertEquals(SentryLevel.FATAL, it.level) @@ -291,9 +291,9 @@ class AnrV2IntegrationTest { importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -311,7 +311,7 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - whenever(fixture.hub.captureEvent(any(), any())).thenAnswer { invocation -> + whenever(fixture.scopes.captureEvent(any(), any())).thenAnswer { invocation -> val hint = HintUtils.getSentrySdkHint(invocation.getArgument(1)) as DiskFlushNotification thread { @@ -321,9 +321,9 @@ class AnrV2IntegrationTest { SentryId() } - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) // shouldn't fall into timed out state, because we marked event as flushed on another thread verify(fixture.logger, never()).log( any(), @@ -341,9 +341,9 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent(any(), anyOrNull()) + verify(fixture.scopes).captureEvent(any(), anyOrNull()) // we do not call markFlushed, hence it should time out waiting for flush, but because // we drop the event, it should not even come to this if-check verify(fixture.logger, never()).log( @@ -360,9 +360,9 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, times(2)).captureEvent( + verify(fixture.scopes, times(2)).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -382,10 +382,10 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // only the latest anr is reported which should be enrichable - verify(fixture.hub, atMost(1)).captureEvent( + verify(fixture.scopes, atMost(1)).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -402,20 +402,20 @@ class AnrV2IntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - TimeUnit.DAYS.toMillis(1)) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // the order is reverse here, so the oldest ANR will be reported first to keep track of // last reported ANR in a marker file - inOrder(fixture.hub) { - verify(fixture.hub).captureEvent( + inOrder(fixture.scopes) { + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp - TimeUnit.DAYS.toMillis(2) }, anyOrNull() ) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp - TimeUnit.DAYS.toMillis(1) }, anyOrNull() ) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { timestamp.time == newTimestamp }, anyOrNull() ) @@ -427,9 +427,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -443,9 +443,9 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), argThat { val hint = HintUtils.getSentrySdkHint(this) @@ -472,7 +472,7 @@ class AnrV2IntegrationTest { ) } - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) // we store envelope with StartSessionHint on different thread after some delay, which // triggers the previous session flush, so no timeout @@ -493,14 +493,14 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.logger, never()).log( any(), argThat { startsWith("Timed out waiting to flush previous session to its own file.") }, any() ) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -512,7 +512,7 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) verify(fixture.logger).log( any(), @@ -532,9 +532,9 @@ class AnrV2IntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { assertNotNull(it.threadDump) @@ -547,8 +547,8 @@ class AnrV2IntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedAnrTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addTrace = false) - integration.register(fixture.hub, fixture.options) + integration.register(fixture.scopes, fixture.options) - verify(fixture.hub, never()).captureEvent(any(), anyOrNull()) + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt index 15a6d690e5..5f8792c850 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegrationTest.kt @@ -5,7 +5,7 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import org.junit.runner.RunWith import org.mockito.kotlin.any @@ -37,8 +37,8 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) verify(fixture.context).registerComponentCallbacks(any()) } @@ -46,10 +46,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is enabled, but ComponentCallbacks is not ready, do not throw`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) whenever(fixture.context.registerComponentCallbacks(any())).thenThrow(NullPointerException()) - sut.register(hub, options) + sut.register(scopes, options) assertFalse(options.isEnableAppComponentBreadcrumbs) } @@ -59,8 +59,8 @@ class AppComponentsBreadcrumbsIntegrationTest { val options = SentryAndroidOptions().apply { isEnableAppComponentBreadcrumbs = false } - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) verify(fixture.context, never()).registerComponentCallbacks(any()) } @@ -68,8 +68,8 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When AppComponentsBreadcrumbsIntegrationTest is closed, it should unregister the callback`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.close() verify(fixture.context).unregisterComponentCallbacks(any()) } @@ -78,10 +78,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When app components breadcrumb is closed, but ComponentCallbacks is not ready, do not throw`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() whenever(fixture.context.registerComponentCallbacks(any())).thenThrow(NullPointerException()) whenever(fixture.context.unregisterComponentCallbacks(any())).thenThrow(NullPointerException()) - sut.register(hub, options) + sut.register(scopes, options) sut.close() } @@ -89,10 +89,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When low memory event, a breadcrumb with type, category and level should be set`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onLowMemory() - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -105,10 +105,10 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When trim memory event with level, a breadcrumb with type, category and level should be set`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -121,20 +121,20 @@ class AppComponentsBreadcrumbsIntegrationTest { fun `When trim memory event with level not so high, do not add a breadcrumb`() { val sut = fixture.getSut() val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } @Test fun `When device orientation event, a breadcrumb with type, category and level should be set`() { val sut = AppComponentsBreadcrumbsIntegration(ApplicationProvider.getApplicationContext()) val options = SentryAndroidOptions() - val hub = mock() - sut.register(hub, options) + val scopes = mock() + sut.register(scopes, options) sut.onConfigurationChanged(mock()) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.orientation", it.category) assertEquals("navigation", it.type) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt index ed8d53227c..733aefa8d6 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt @@ -2,7 +2,7 @@ package io.sentry.android.core import android.os.Looper import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.IHub +import io.sentry.IScopes import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.mock @@ -17,7 +17,7 @@ import kotlin.test.assertNull class AppLifecycleIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var handler: MainLooperHandler val options = SentryAndroidOptions() @@ -33,7 +33,7 @@ class AppLifecycleIntegrationTest { fun `When AppLifecycleIntegration is added, lifecycle watcher should be started`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) } @@ -46,7 +46,7 @@ class AppLifecycleIntegrationTest { isEnableAutoSessionTracking = false } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.watcher) } @@ -55,7 +55,7 @@ class AppLifecycleIntegrationTest { fun `When AppLifecycleIntegration is closed, lifecycle watcher should be closed`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) @@ -70,7 +70,7 @@ class AppLifecycleIntegrationTest { val latch = CountDownLatch(1) Thread { - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) latch.countDown() }.start() @@ -84,7 +84,7 @@ class AppLifecycleIntegrationTest { val sut = fixture.getSut() val latch = CountDownLatch(1) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) @@ -103,7 +103,7 @@ class AppLifecycleIntegrationTest { val sut = fixture.getSut(mockHandler = false) val latch = CountDownLatch(1) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNotNull(sut.watcher) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt index 6330623121..ecdbff5104 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/CurrentActivityIntegrationTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.app.Activity import android.app.Application import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.IHub +import io.sentry.IScopes import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.mock @@ -19,7 +19,7 @@ class CurrentActivityIntegrationTest { private class Fixture { val application = mock() val activity = mock() - val hub = mock() + val scopes = mock() val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" @@ -27,7 +27,7 @@ class CurrentActivityIntegrationTest { fun getSut(): CurrentActivityIntegration { val integration = CurrentActivityIntegration(application) - integration.register(hub, options) + integration.register(scopes, options) return integration } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt index 80954f67a5..c4fef01cc2 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt @@ -7,7 +7,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.DiagnosticLogger import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.SentryTracer @@ -62,13 +62,13 @@ class DefaultAndroidEventProcessorTest { sdkVersion = SdkVersion("test", "1.2.3") } - val hub: IHub = mock() + val scopes: IScopes = mock() lateinit var sentryTracer: SentryTracer fun getSut(context: Context): DefaultAndroidEventProcessor { - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("", ""), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("", ""), scopes) return DefaultAndroidEventProcessor(context, buildInfo, options) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt index 699fa2d2f2..192565f101 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt @@ -2,8 +2,8 @@ package io.sentry.android.core import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hub -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.test.DeferredExecutorService @@ -24,7 +24,7 @@ import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) class EnvelopeFileObserverIntegrationTest { inner class Fixture { - val hub: IHub = mock() + val scopes: IScopes = mock() private lateinit var options: SentryAndroidOptions val logger = mock() @@ -33,7 +33,7 @@ class EnvelopeFileObserverIntegrationTest { options.setLogger(logger) options.isDebug = true optionConfiguration(options) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return object : EnvelopeFileObserverIntegration() { override fun getPath(options: SentryOptions): String? = file.absolutePath @@ -65,7 +65,7 @@ class EnvelopeFileObserverIntegrationTest { } @Test - fun `when hub is closed, integrations should be closed`() { + fun `when scopes is closed, integrations should be closed`() { val integrationMock = mock() val options = SentryOptions() options.dsn = "https://key@sentry.io/proj" @@ -73,19 +73,19 @@ class EnvelopeFileObserverIntegrationTest { options.addIntegration(integrationMock) options.setSerializer(mock()) // val expected = HubAdapter.getInstance() - val hub = Hub(options) + val scopes = Hub(options) // verify(integrationMock).register(expected, options) - hub.close() + scopes.close() verify(integrationMock).close() } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val integration = fixture.getSut { it.executorService = deferredExecutorService } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) integration.close() deferredExecutorService.runAll() verify(fixture.logger, never()).log(eq(SentryLevel.DEBUG), eq("EnvelopeFileObserverIntegration installed.")) @@ -96,7 +96,7 @@ class EnvelopeFileObserverIntegrationTest { val integration = fixture.getSut { it.executorService = mock() } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) verify(fixture.logger).log( eq(SentryLevel.DEBUG), eq("Registering EnvelopeFileObserverIntegration for path: %s"), @@ -110,7 +110,7 @@ class EnvelopeFileObserverIntegrationTest { val integration = fixture.getSut { it.executorService = ImmediateExecutorService() } - integration.register(fixture.hub, fixture.hub.options) + integration.register(fixture.scopes, fixture.scopes.options) verify(fixture.logger).log( eq(SentryLevel.DEBUG), eq("Registering EnvelopeFileObserverIntegration for path: %s"), diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt index be30993142..73571a5ad4 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt @@ -3,8 +3,8 @@ package io.sentry.android.core import androidx.lifecycle.LifecycleOwner import io.sentry.Breadcrumb import io.sentry.DateUtils -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.ScopeCallback import io.sentry.SentryLevel import io.sentry.Session @@ -32,7 +32,7 @@ class LifecycleWatcherTest { private class Fixture { val ownerMock = mock() - val hub = mock() + val scopes = mock() val dateProvider = mock() fun getSUT( @@ -44,12 +44,12 @@ class LifecycleWatcherTest { val argumentCaptor: ArgumentCaptor = ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = mock() whenever(scope.session).thenReturn(session) - whenever(hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } return LifecycleWatcher( - hub, + scopes, sessionIntervalMillis, enableAutoSessionTracking, enableAppLifecycleBreadcrumbs, @@ -69,7 +69,7 @@ class LifecycleWatcherTest { fun `if last started session is 0, start new session`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test @@ -78,7 +78,7 @@ class LifecycleWatcherTest { whenever(fixture.dateProvider.currentTimeMillis).thenReturn(1L, 2L) watcher.onStart(fixture.ownerMock) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, times(2)).startSession() + verify(fixture.scopes, times(2)).startSession() } @Test @@ -87,7 +87,7 @@ class LifecycleWatcherTest { whenever(fixture.dateProvider.currentTimeMillis).thenReturn(2L, 1L) watcher.onStart(fixture.ownerMock) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test @@ -95,7 +95,7 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, timeout(10000)).endSession() + verify(fixture.scopes, timeout(10000)).endSession() } @Test @@ -109,14 +109,14 @@ class LifecycleWatcherTest { watcher.onStart(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).endSession() + verify(fixture.scopes, never()).endSession() } @Test fun `When session tracking is disabled, do not start session`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).startSession() + verify(fixture.scopes, never()).startSession() } @Test @@ -124,14 +124,14 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).endSession() + verify(fixture.scopes, never()).endSession() } @Test fun `When session tracking is enabled, add breadcrumb on start`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("session", it.type) @@ -145,8 +145,8 @@ class LifecycleWatcherTest { fun `When session tracking is enabled, add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, timeout(10000)).endSession() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes, timeout(10000)).endSession() + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("session", it.type) @@ -160,7 +160,7 @@ class LifecycleWatcherTest { fun `When session tracking is disabled, do not add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -168,14 +168,14 @@ class LifecycleWatcherTest { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) assertNull(watcher.timerTask) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("navigation", it.type) @@ -189,14 +189,14 @@ class LifecycleWatcherTest { fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on start`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("app.lifecycle", it.category) assertEquals("navigation", it.type) @@ -210,7 +210,7 @@ class LifecycleWatcherTest { fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on stop`() { val watcher = fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false) watcher.onStop(fixture.ownerMock) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -226,7 +226,7 @@ class LifecycleWatcherTest { } @Test - fun `if the hub has already a fresh session running, don't start new one`() { + fun `if the scopes has already a fresh session running, don't start new one`() { val watcher = fixture.getSUT( enableAppLifecycleBreadcrumbs = false, session = Session( @@ -248,11 +248,11 @@ class LifecycleWatcherTest { ) watcher.onStart(fixture.ownerMock) - verify(fixture.hub, never()).startSession() + verify(fixture.scopes, never()).startSession() } @Test - fun `if the hub has a long running session, start new one`() { + fun `if the scopes has a long running session, start new one`() { val watcher = fixture.getSUT( enableAppLifecycleBreadcrumbs = false, session = Session( @@ -274,7 +274,7 @@ class LifecycleWatcherTest { ) watcher.onStart(fixture.ownerMock) - verify(fixture.hub).startSession() + verify(fixture.scopes).startSession() } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt index e86de06814..e282ad7141 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/NdkIntegrationTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.core -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.SentryLevel import org.mockito.kotlin.any import org.mockito.kotlin.eq @@ -15,7 +15,7 @@ import kotlin.test.assertTrue class NdkIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() val logger = mock() fun getSut(clazz: Class<*>? = SentryNdk::class.java): NdkIntegration { @@ -31,7 +31,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) assertTrue(options.isEnableNdk) @@ -44,7 +44,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) assertTrue(options.isEnableNdk) assertTrue(options.isEnableScopeSync) @@ -62,7 +62,7 @@ class NdkIntegrationTest { val options = getOptions(enableNdk = false) - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -76,7 +76,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger, never()).log(eq(SentryLevel.ERROR), any(), any()) @@ -90,7 +90,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) @@ -104,7 +104,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) assertTrue(options.isEnableNdk) assertTrue(options.isEnableScopeSync) @@ -122,7 +122,7 @@ class NdkIntegrationTest { val options = getOptions() - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any(), any()) @@ -136,7 +136,7 @@ class NdkIntegrationTest { val options = getOptions(cacheDir = null) - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any()) @@ -150,7 +150,7 @@ class NdkIntegrationTest { val options = getOptions(cacheDir = "") - integration.register(fixture.hub, options) + integration.register(fixture.scopes, options) verify(fixture.logger).log(eq(SentryLevel.ERROR), any()) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt index 146f229fdf..c28cf64085 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/NetworkBreadcrumbsIntegrationTest.kt @@ -8,7 +8,7 @@ import android.net.NetworkCapabilities import android.os.Build import io.sentry.Breadcrumb import io.sentry.DateUtils -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryDateProvider import io.sentry.SentryLevel import io.sentry.SentryNanotimeDate @@ -39,7 +39,7 @@ class NetworkBreadcrumbsIntegrationTest { private class Fixture { val context = mock() var options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val mockBuildInfoProvider = mock() val connectivityManager = mock() var nowMs: Long = 0 @@ -68,7 +68,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When network events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager).registerDefaultNetworkCallback(any()) assertNotNull(sut.networkCallback) @@ -78,7 +78,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut(enableNetworkEventBreadcrumbs = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager, never()).registerDefaultNetworkCallback(any()) assertNull(sut.networkCallback) @@ -90,7 +90,7 @@ class NetworkBreadcrumbsIntegrationTest { whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.M) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.connectivityManager, never()).registerDefaultNetworkCallback(any()) assertNull(sut.networkCallback) @@ -100,7 +100,7 @@ class NetworkBreadcrumbsIntegrationTest { fun `When NetworkBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.connectivityManager).unregisterNetworkCallback(any()) @@ -114,7 +114,7 @@ class NetworkBreadcrumbsIntegrationTest { val sut = fixture.getSut(buildInfo = buildInfo) assertNull(sut.networkCallback) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.connectivityManager, never()).unregisterNetworkCallback(any()) @@ -124,12 +124,12 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When connected to a new network, a breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(mock()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -142,27 +142,27 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When connected to the same network without disconnecting from the previous one, only one breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) callback.onAvailable(fixture.network) - verify(fixture.hub, times(1)).addBreadcrumb(any()) + verify(fixture.scopes, times(1)).addBreadcrumb(any()) } @Test fun `When disconnected from a network, a breadcrumb is captured`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) callback.onLost(fixture.network) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -175,12 +175,12 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When disconnected from a network, a breadcrumb is captured only if previously connected to that network`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) // callback.onAvailable(network) was not called, so no breadcrumb should be captured callback.onLost(mock()) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -188,7 +188,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -204,7 +204,7 @@ class NetworkBreadcrumbsIntegrationTest { isCellular = false ) ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("system", it.type) assertEquals("network.event", it.category) @@ -223,18 +223,18 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `When a network connection detail changes, a breadcrumb is captured only if previously connected to that network`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) // callback.onAvailable(network) was not called, so no breadcrumb should be captured onCapabilitiesChanged(callback, mock()) - verify(fixture.hub, never()).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, never()).addBreadcrumb(any(), anyOrNull()) } @Test fun `When a network connection detail changes, a new breadcrumb is captured if vpn flag changes`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -245,17 +245,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1) onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertFalse(it.isVpn) } verifyBreadcrumbInOrder { assertTrue(it.isVpn) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `When a network connection detail changes, a new breadcrumb is captured if type changes`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -266,10 +266,10 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1) onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals("wifi", it.type) } verifyBreadcrumbInOrder { assertEquals("cellular", it.type) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -278,7 +278,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -289,10 +289,10 @@ class NetworkBreadcrumbsIntegrationTest { // A change of signal strength of 5 doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details2) onCapabilitiesChanged(callback, details3) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(50, it.signalStrength) } verifyBreadcrumbInOrder { assertEquals(56, it.signalStrength) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -301,7 +301,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -317,11 +317,11 @@ class NetworkBreadcrumbsIntegrationTest { // A change of download bandwidth of 10% (more than 1000) doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details4) onCapabilitiesChanged(callback, details5) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1000, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(20000, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(22001, it.downBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -330,7 +330,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -346,18 +346,18 @@ class NetworkBreadcrumbsIntegrationTest { // A change of upload bandwidth of 10% (more than 1000) doesn't trigger a new breadcrumb onCapabilitiesChanged(callback, details4) onCapabilitiesChanged(callback, details5) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1000, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(20000, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(22001, it.upBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `signal strength is 0 if not on Android Q+`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -371,7 +371,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -384,7 +384,7 @@ class NetworkBreadcrumbsIntegrationTest { @Test fun `A breadcrumb is captured when vpn status changes, regardless of the timestamp`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -392,17 +392,17 @@ class NetworkBreadcrumbsIntegrationTest { val details2 = createConnectionDetail(isVpn = true) onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertFalse(it.isVpn) } verifyBreadcrumbInOrder { assertTrue(it.isVpn) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when connection type changes, regardless of the timestamp`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -412,11 +412,11 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 0) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals("wifi", it.type) } verifyBreadcrumbInOrder { assertEquals("cellular", it.type) } verifyBreadcrumbInOrder { assertEquals("ethernet", it.type) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @@ -425,7 +425,7 @@ class NetworkBreadcrumbsIntegrationTest { val buildInfo = mock() whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.Q) val sut = fixture.getSut(buildInfo = buildInfo) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -435,17 +435,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.signalStrength) } verifyBreadcrumbInOrder { assertEquals(51, it.signalStrength) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when downBandwidth changes at most once every 5 seconds`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -455,17 +455,17 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.downBandwidth) } verifyBreadcrumbInOrder { assertEquals(2001, it.downBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } @Test fun `A breadcrumb is captured when upBandwidth changes at most once every 5 seconds`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val callback = sut.networkCallback assertNotNull(callback) callback.onAvailable(fixture.network) @@ -475,15 +475,15 @@ class NetworkBreadcrumbsIntegrationTest { onCapabilitiesChanged(callback, details1, 0) onCapabilitiesChanged(callback, details2, 0) onCapabilitiesChanged(callback, details3, 5000) - inOrder(fixture.hub) { + inOrder(fixture.scopes) { verifyBreadcrumbInOrder { assertEquals(1, it.upBandwidth) } verifyBreadcrumbInOrder { assertEquals(2001, it.upBandwidth) } - verify(fixture.hub, never()).addBreadcrumb(any(), any()) + verify(fixture.scopes, never()).addBreadcrumb(any(), any()) } } private fun KInOrder.verifyBreadcrumbInOrder(check: (detail: NetworkBreadcrumbConnectionDetail) -> Unit) { - verify(fixture.hub, times(1)).addBreadcrumb( + verify(fixture.scopes, times(1)).addBreadcrumb( any(), check { val connectionDetail = it[TypeCheckHint.ANDROID_NETWORK_CAPABILITIES] as NetworkBreadcrumbConnectionDetail @@ -493,7 +493,7 @@ class NetworkBreadcrumbsIntegrationTest { } private fun verifyBreadcrumb(check: (detail: NetworkBreadcrumbConnectionDetail) -> Unit) { - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( any(), check { val connectionDetail = it[TypeCheckHint.ANDROID_NETWORK_CAPABILITIES] as NetworkBreadcrumbConnectionDetail diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt index 4c23691e63..e1593e600f 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.content.ContentProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.MeasurementUnit import io.sentry.SentryTracer import io.sentry.SpanContext @@ -35,7 +35,7 @@ class PerformanceAndroidEventProcessorTest { private class Fixture { val options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() val context = TransactionContext("name", "op", TracesSamplingDecision(true)) lateinit var tracer: SentryTracer val activityFramesTracker = mock() @@ -46,8 +46,8 @@ class PerformanceAndroidEventProcessorTest { ): PerformanceAndroidEventProcessor { options.tracesSampleRate = tracesSampleRate options.isEnablePerformanceV2 = enablePerformanceV2 - whenever(hub.options).thenReturn(options) - tracer = SentryTracer(context, hub) + whenever(scopes.options).thenReturn(options) + tracer = SentryTracer(context, scopes) return PerformanceAndroidEventProcessor(options, activityFramesTracker) } } @@ -181,7 +181,7 @@ class PerformanceAndroidEventProcessorTest { fun `add slow and frozen frames if auto transaction`() { val sut = fixture.getSut() val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val metrics = mapOf( @@ -227,7 +227,7 @@ class PerformanceAndroidEventProcessorTest { // when an activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) // and it contains an app.start.cold span @@ -297,7 +297,7 @@ class PerformanceAndroidEventProcessorTest { // when an activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) // then the app start metrics should not be attached @@ -326,7 +326,7 @@ class PerformanceAndroidEventProcessorTest { // when the first activity transaction is created val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, @@ -377,7 +377,7 @@ class PerformanceAndroidEventProcessorTest { val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, @@ -424,7 +424,7 @@ class PerformanceAndroidEventProcessorTest { val sut = fixture.getSut(enablePerformanceV2 = true) val context = TransactionContext("Activity", UI_LOAD_OP) - val tracer = SentryTracer(context, fixture.hub) + val tracer = SentryTracer(context, fixture.scopes) var tr = SentryTransaction(tracer) val appStartSpan = SentrySpan( 0.0, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt index 2b6ca801da..c764d11c2d 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PhoneStateBreadcrumbsIntegrationTest.kt @@ -4,7 +4,7 @@ import android.content.Context import android.telephony.PhoneStateListener import android.telephony.TelephonyManager import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.test.DeferredExecutorService @@ -41,8 +41,8 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) verify(fixture.manager).listen(any(), eq(PhoneStateListener.LISTEN_CALL_STATE)) assertNotNull(sut.listener) } @@ -50,8 +50,8 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `Phone state callback is registered in the executorService`() { val sut = fixture.getSut(mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.listener) } @@ -59,9 +59,9 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut() - val hub = mock() + val scopes = mock() sut.register( - hub, + scopes, fixture.options.apply { isEnableSystemEventBreadcrumbs = false } @@ -73,15 +73,15 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When ActivityBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.close() verify(fixture.manager).listen(any(), eq(PhoneStateListener.LISTEN_NONE)) assertNull(sut.listener) } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) sut.register(mock(), fixture.options) @@ -94,11 +94,11 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When on call state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -111,18 +111,18 @@ class PhoneStateBreadcrumbsIntegrationTest { @Test fun `When on idle state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_IDLE, null) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } @Test fun `When on offhook state received, added breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.listener!!.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt index 403f40ee70..f1e345eefc 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt @@ -2,8 +2,8 @@ package io.sentry.android.core import io.sentry.IConnectionStatusProvider import io.sentry.IConnectionStatusProvider.ConnectionStatus -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget import io.sentry.SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory @@ -28,7 +28,7 @@ import kotlin.test.Test class SendCachedEnvelopeIntegrationTest { private class Fixture { - val hub: IHub = mock() + val scopes: IScopes = mock() val options = SentryAndroidOptions() val logger = mock() val factory = mock() @@ -74,7 +74,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when cacheDirPath is not set, does nothing`() { val sut = fixture.getSut(cacheDirPath = null) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory, never()).create(any(), any()) } @@ -83,7 +83,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when factory returns null, does nothing`() { val sut = fixture.getSut(hasSender = false, mockExecutorService = ImmediateExecutorService()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory).create(any(), any()) verify(fixture.sender, never()).send() @@ -93,7 +93,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when has factory and cacheDirPath set, submits task into queue`() { val sut = fixture.getSut(mockExecutorService = ImmediateExecutorService()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) await.untilFalse(fixture.flag) verify(fixture.sender).send() @@ -102,7 +102,7 @@ class SendCachedEnvelopeIntegrationTest { @Test fun `when executorService is fake, does nothing`() { val sut = fixture.getSut(mockExecutorService = mock()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory, never()).create(any(), any()) verify(fixture.sender, never()).send() @@ -112,7 +112,7 @@ class SendCachedEnvelopeIntegrationTest { fun `when has startup crash marker, awaits the task on the calling thread`() { val sut = fixture.getSut(hasStartupCrashMarker = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // we do not need to await here, because it's executed synchronously verify(fixture.sender).send() @@ -123,7 +123,7 @@ class SendCachedEnvelopeIntegrationTest { val sut = fixture.getSut(hasStartupCrashMarker = true, delaySend = 1000) fixture.options.startupCrashFlushTimeoutMillis = 100 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // first wait until synchronous send times out and check that the logger was hit in the catch block await.atLeast(500, MILLISECONDS) @@ -144,7 +144,7 @@ class SendCachedEnvelopeIntegrationTest { val connectionStatusProvider = mock() fixture.options.connectionStatusProvider = connectionStatusProvider - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(connectionStatusProvider).addConnectionStatusObserver(any()) } @@ -159,7 +159,7 @@ class SendCachedEnvelopeIntegrationTest { ConnectionStatus.DISCONNECTED ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() } @@ -174,7 +174,7 @@ class SendCachedEnvelopeIntegrationTest { ConnectionStatus.UNKNOWN ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.factory).create(any(), any()) } @@ -187,7 +187,7 @@ class SendCachedEnvelopeIntegrationTest { whenever(connectionStatusProvider.connectionStatus).thenReturn( ConnectionStatus.DISCONNECTED ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // when there's no connection no factory create call should be done verify(fixture.sender, never()).send() @@ -215,9 +215,9 @@ class SendCachedEnvelopeIntegrationTest { val rateLimiter = mock { whenever(mock.isActiveForCategory(any())).thenReturn(true) } - whenever(fixture.hub.rateLimiter).thenReturn(rateLimiter) + whenever(fixture.scopes.rateLimiter).thenReturn(rateLimiter) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // no factory call should be done if there's rate limiting active verify(fixture.sender, never()).send() @@ -228,7 +228,7 @@ class SendCachedEnvelopeIntegrationTest { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(mockExecutorService = deferredExecutorService) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.sender, never()).send() sut.close() diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt index f8293f9b87..146abb617e 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.core import android.content.Context import android.content.Intent import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.test.DeferredExecutorService @@ -26,7 +26,7 @@ class SystemEventsBreadcrumbsIntegrationTest { private class Fixture { val context = mock() var options = SentryAndroidOptions() - val hub = mock() + val scopes = mock() fun getSut(enableSystemEventBreadcrumbs: Boolean = true, executorService: ISentryExecutorService = ImmediateExecutorService()): SystemEventsBreadcrumbsIntegration { options = SentryAndroidOptions().apply { @@ -43,7 +43,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.context).registerReceiver(any(), any()) assertNotNull(sut.receiver) @@ -52,8 +52,8 @@ class SystemEventsBreadcrumbsIntegrationTest { @Test fun `system events callback is registered in the executorService`() { val sut = fixture.getSut(executorService = mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.receiver) } @@ -62,7 +62,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When system events breadcrumb is disabled, it doesn't register callback`() { val sut = fixture.getSut(enableSystemEventBreadcrumbs = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.context, never()).registerReceiver(any(), any()) assertNull(sut.receiver) @@ -72,7 +72,7 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When ActivityBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.context).unregisterReceiver(any()) @@ -80,10 +80,10 @@ class SystemEventsBreadcrumbsIntegrationTest { } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertNull(sut.receiver) sut.close() deferredExecutorService.runAll() @@ -94,13 +94,13 @@ class SystemEventsBreadcrumbsIntegrationTest { fun `When broadcast received, added breadcrumb with type and category`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val intent = Intent().apply { action = Intent.ACTION_TIME_CHANGED } sut.receiver!!.onReceive(fixture.context, intent) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -116,7 +116,7 @@ class SystemEventsBreadcrumbsIntegrationTest { val sut = fixture.getSut() whenever(fixture.context.registerReceiver(any(), any())).thenThrow(SecurityException()) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertFalse(fixture.options.isEnableSystemEventBreadcrumbs) } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt index d443b1e345..5d049e3dad 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/TempSensorBreadcrumbsIntegrationTest.kt @@ -7,7 +7,7 @@ import android.hardware.SensorEventListener import android.hardware.SensorManager import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.SentryLevel import io.sentry.TypeCheckHint @@ -47,8 +47,8 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is enabled, it registers callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) verify(fixture.manager).registerListener(any(), any(), eq(SensorManager.SENSOR_DELAY_NORMAL)) assertNotNull(sut.sensorManager) } @@ -56,8 +56,8 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `temp sensor listener is registered in the executorService`() { val sut = fixture.getSut(executorService = mock()) - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) assertNull(sut.sensorManager) } @@ -65,9 +65,9 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When system events breadcrumb is disabled, it should not register a callback`() { val sut = fixture.getSut() - val hub = mock() + val scopes = mock() sut.register( - hub, + scopes, fixture.options.apply { isEnableSystemEventBreadcrumbs = false } @@ -79,15 +79,15 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When TempSensorBreadcrumbsIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) sut.close() verify(fixture.manager).unregisterListener(any()) assertNull(sut.sensorManager) } @Test - fun `when hub is closed right after start, integration is not registered`() { + fun `when scopes is closed right after start, integration is not registered`() { val deferredExecutorService = DeferredExecutorService() val sut = fixture.getSut(executorService = deferredExecutorService) sut.register(mock(), fixture.options) @@ -100,14 +100,14 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When onSensorChanged received, add a breadcrumb with type and category`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) val sensorCtor = "android.hardware.SensorEvent".getDeclaredCtor(emptyArray()) val sensorEvent: SensorEvent = sensorCtor.newInstance() as SensorEvent sensorEvent.injectForField("values", FloatArray(2) { 1F }) sut.onSensorChanged(sensorEvent) - verify(hub).addBreadcrumb( + verify(scopes).addBreadcrumb( check { assertEquals("device.event", it.category) assertEquals("system", it.type) @@ -122,12 +122,12 @@ class TempSensorBreadcrumbsIntegrationTest { @Test fun `When onSensorChanged received and null values, do not add a breadcrumb`() { val sut = fixture.getSut() - val hub = mock() - sut.register(hub, fixture.options) + val scopes = mock() + sut.register(scopes, fixture.options) val event = mock() assertNull(event.values) sut.onSensorChanged(event) - verify(hub, never()).addBreadcrumb(any()) + verify(scopes, never()).addBreadcrumb(any()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt index 1e6652276a..74edfb4302 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerClickTest.kt @@ -10,8 +10,8 @@ import android.view.Window import android.widget.CheckBox import android.widget.RadioButton import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.Scope.IWithPropagationContext import io.sentry.ScopeCallback @@ -40,7 +40,7 @@ class SentryGestureListenerClickTest { gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val scope = mock() val propagationContext = PropagationContext() lateinit var target: View @@ -86,11 +86,11 @@ class SentryGestureListenerClickTest { whenever(context.resources).thenReturn(resources) whenever(this.target.context).thenReturn(context) whenever(activity.window).thenReturn(window) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) doAnswer { (it.arguments[0] as IWithPropagationContext).accept(propagationContext); propagationContext; }.whenever(scope).withPropagationContext(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -123,7 +123,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.click", it.category) assertEquals("user", it.type) @@ -146,7 +146,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("radio_button", it.data["view.id"]) assertEquals("android.widget.RadioButton", it.data["view.class"]) @@ -166,7 +166,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("check_box", it.data["view.id"]) assertEquals("android.widget.CheckBox", it.data["view.class"]) @@ -185,7 +185,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -198,7 +198,7 @@ class SentryGestureListenerClickTest { val sut = fixture.getSut(event, "decor_view", targetOverride = decorView) sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(decorView.javaClass.canonicalName, it.data["view.class"]) assertEquals("decor_view", it.data["view.id"]) @@ -214,7 +214,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -230,7 +230,7 @@ class SentryGestureListenerClickTest { sut.onSingleTapUp(event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.target.javaClass.simpleName, it.data["view.class"]) }, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt index 5d39b64753..e5a9623c4d 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerScrollTest.kt @@ -11,8 +11,8 @@ import android.widget.AbsListView import android.widget.ListAdapter import androidx.core.view.ScrollingView import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.Scope import io.sentry.ScopeCallback @@ -44,7 +44,7 @@ class SentryGestureListenerScrollTest { isEnableUserInteractionTracing = true gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) } - val hub = mock() + val scopes = mock() val scope = mock() val propagationContext = PropagationContext() @@ -77,11 +77,11 @@ class SentryGestureListenerScrollTest { endEvent.mockDirection(firstEvent, direction) } whenever(activity.window).thenReturn(window) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) doAnswer { (it.arguments[0] as Scope.IWithPropagationContext).accept(propagationContext); propagationContext }.whenever(scope).withPropagationContext(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -99,7 +99,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.scroll", it.category) assertEquals("user", it.type) @@ -122,7 +122,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -143,8 +143,8 @@ class SentryGestureListenerScrollTest { sut.onFling(fixture.firstEvent, fixture.endEvent, 1.0f, 1.0f) sut.onUp(fixture.endEvent) - inOrder(fixture.hub) { - verify(fixture.hub).addBreadcrumb( + inOrder(fixture.scopes) { + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.swipe", it.category) assertEquals("user", it.type) @@ -155,8 +155,8 @@ class SentryGestureListenerScrollTest { }, anyOrNull() ) - verify(fixture.hub).configureScope(anyOrNull()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).configureScope(anyOrNull()) + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.swipe", it.category) assertEquals("user", it.type) @@ -168,7 +168,7 @@ class SentryGestureListenerScrollTest { anyOrNull() ) } - verifyNoMoreInteractions(fixture.hub) + verifyNoMoreInteractions(fixture.scopes) } @Test @@ -177,7 +177,7 @@ class SentryGestureListenerScrollTest { sut.onUp(fixture.firstEvent) sut.onDown(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -190,7 +190,7 @@ class SentryGestureListenerScrollTest { } sut.onUp(fixture.endEvent) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt index c7ada69c88..d3f6647c2a 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/internal/gestures/SentryGestureListenerTracingTest.kt @@ -9,8 +9,8 @@ import android.view.ViewGroup import android.view.Window import android.widget.AbsListView import android.widget.ListAdapter -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryTracer @@ -46,7 +46,7 @@ class SentryGestureListenerTracingTest { val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" } - val hub = mock() + val scopes = mock() val event = mock() val scope = mock() lateinit var target: View @@ -64,9 +64,9 @@ class SentryGestureListenerTracingTest { options.isEnableUserInteractionBreadcrumbs = true options.gestureTargetLocators = listOf(AndroidViewGestureTargetLocator(true)) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - this.transaction = transaction ?: SentryTracer(TransactionContext("name", "op"), hub) + this.transaction = transaction ?: SentryTracer(TransactionContext("name", "op"), scopes) target = mockView(event = event, clickable = true, context = context) window.mockDecorView(event = event, context = context) { @@ -86,13 +86,13 @@ class SentryGestureListenerTracingTest { whenever(activity.window).thenReturn(window) - whenever(hub.startTransaction(any(), any())) + whenever(scopes.startTransaction(any(), any())) .thenReturn(this.transaction) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) return SentryGestureListener( activity, - hub, + scopes, options ) } @@ -106,7 +106,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -118,7 +118,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -130,7 +130,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -140,7 +140,7 @@ class SentryGestureListenerTracingTest { fun `when transaction is created, set transaction to the bound Scope`() { val sut = fixture.getSut() - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) sut.applyScope(scope, fixture.transaction) @@ -155,9 +155,9 @@ class SentryGestureListenerTracingTest { fun `when transaction is created, do not overwrite transaction already bound to the Scope`() { val sut = fixture.getSut() - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) - val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.scopes) scope.transaction = previousTransaction sut.applyScope(scope, fixture.transaction) @@ -173,14 +173,14 @@ class SentryGestureListenerTracingTest { val sut = fixture.getSut() val expectedStatus = SpanStatus.CANCELLED - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) sut.applyScope(scope, fixture.transaction) } sut.onSingleTapUp(fixture.event) - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) scope.transaction = fixture.transaction @@ -199,7 +199,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -214,7 +214,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { transactionOptions -> assertEquals(fixture.options.idleTimeout, transactionOptions.idleTimeout) @@ -232,7 +232,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("ui.action.click", it.operation) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -248,7 +248,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -256,7 +256,7 @@ class SentryGestureListenerTracingTest { any() ) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) // second view interaction with another view val newTarget = mockView(event = fixture.event, clickable = true, context = fixture.context) val newContext = mock() @@ -269,16 +269,16 @@ class SentryGestureListenerTracingTest { whenever(it.getChildAt(0)).thenReturn(newTarget) } - whenever(fixture.hub.startTransaction(any(), any())) + whenever(fixture.scopes.startTransaction(any(), any())) .thenAnswer { // verify that the active transaction gets finished when a new one appears assertEquals(true, fixture.transaction.isFinished) - SentryTracer(TransactionContext("name", "op"), fixture.hub) + SentryTracer(TransactionContext("name", "op"), fixture.scopes) } sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_checkbox", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -293,7 +293,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_scroll_view", it.name) assertEquals("ui.action.click", it.operation) @@ -302,20 +302,20 @@ class SentryGestureListenerTracingTest { any() ) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) // second view interaction with a different interaction type (scroll) - whenever(fixture.hub.startTransaction(any(), any())) + whenever(fixture.scopes.startTransaction(any(), any())) .thenAnswer { // verify that the active transaction gets finished when a new one appears assertEquals(true, fixture.transaction.isFinished) - SentryTracer(TransactionContext("name", "op"), fixture.hub) + SentryTracer(TransactionContext("name", "op"), fixture.scopes) } sut.onScroll(fixture.event, mock(), 10.0f, 0f) sut.onUp(mock()) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity.test_scroll_view", it.name) assertEquals("ui.action.scroll", it.operation) @@ -340,7 +340,7 @@ class SentryGestureListenerTracingTest { sut.onSingleTapUp(fixture.event) // then two transaction should be captured - verify(fixture.hub, times(2)).startTransaction( + verify(fixture.scopes, times(2)).startTransaction( check { assertEquals("Activity.test_button", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) From 22ddc005c07f64752b137e2babe96ac9dfbc9f50 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:23:32 +0200 Subject: [PATCH 06/57] Replace `IHub` with `IScopes` in android integrations --- .../api/sentry-android-fragment.api | 8 +-- .../fragment/FragmentLifecycleIntegration.kt | 10 ++-- .../SentryFragmentLifecycleCallbacks.kt | 18 +++---- .../FragmentLifecycleIntegrationTest.kt | 16 +++--- .../SentryFragmentLifecycleCallbacksTest.kt | 14 ++--- .../android/mockservers/RelayAsserter.kt | 2 +- .../api/sentry-android-navigation.api | 10 ++-- .../navigation/SentryNavigationListener.kt | 24 ++++----- .../SentryNavigationListenerTest.kt | 46 ++++++++-------- .../api/sentry-android-okhttp.api | 18 +++---- .../okhttp/SentryOkHttpEventListener.kt | 22 ++++---- .../android/okhttp/SentryOkHttpInterceptor.kt | 16 +++--- .../android/sqlite/SQLiteSpanManager.kt | 12 ++--- .../android/sqlite/SQLiteSpanManagerTest.kt | 12 ++--- .../sqlite/SentrySupportSQLiteDatabaseTest.kt | 12 ++--- .../SentrySupportSQLiteStatementTest.kt | 12 ++--- .../api/sentry-android-timber.api | 4 +- .../android/timber/SentryTimberIntegration.kt | 6 +-- .../sentry/android/timber/SentryTimberTree.kt | 8 +-- .../timber/SentryTimberIntegrationTest.kt | 18 +++---- .../android/timber/SentryTimberTreeTest.kt | 52 +++++++++---------- 21 files changed, 170 insertions(+), 170 deletions(-) diff --git a/sentry-android-fragment/api/sentry-android-fragment.api b/sentry-android-fragment/api/sentry-android-fragment.api index 4b3487c36e..f2f3334280 100644 --- a/sentry-android-fragment/api/sentry-android-fragment.api +++ b/sentry-android-fragment/api/sentry-android-fragment.api @@ -18,7 +18,7 @@ public final class io/sentry/android/fragment/FragmentLifecycleIntegration : and public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/fragment/FragmentLifecycleState : java/lang/Enum { @@ -40,9 +40,9 @@ public final class io/sentry/android/fragment/FragmentLifecycleState : java/lang public final class io/sentry/android/fragment/SentryFragmentLifecycleCallbacks : androidx/fragment/app/FragmentManager$FragmentLifecycleCallbacks { public static final field Companion Lio/sentry/android/fragment/SentryFragmentLifecycleCallbacks$Companion; public static final field FRAGMENT_LOAD_OP Ljava/lang/String; - public fun (Lio/sentry/IHub;Ljava/util/Set;Z)V - public synthetic fun (Lio/sentry/IHub;Ljava/util/Set;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;ZZ)V + public fun (Lio/sentry/IScopes;Ljava/util/Set;Z)V + public synthetic fun (Lio/sentry/IScopes;Ljava/util/Set;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;ZZ)V public fun (ZZ)V public synthetic fun (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getEnableAutoFragmentLifecycleTracing ()Z diff --git a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt index 4129ea4356..d2fd393200 100644 --- a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt +++ b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/FragmentLifecycleIntegration.kt @@ -5,7 +5,7 @@ import android.app.Application import android.app.Application.ActivityLifecycleCallbacks import android.os.Bundle import androidx.fragment.app.FragmentActivity -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Integration import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel.DEBUG @@ -40,11 +40,11 @@ class FragmentLifecycleIntegration( enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) - private lateinit var hub: IHub + private lateinit var scopes: IScopes private lateinit var options: SentryOptions - override fun register(hub: IHub, options: SentryOptions) { - this.hub = hub + override fun register(scopes: IScopes, options: SentryOptions) { + this.scopes = scopes this.options = options application.registerActivityLifecycleCallbacks(this) @@ -66,7 +66,7 @@ class FragmentLifecycleIntegration( ?.supportFragmentManager ?.registerFragmentLifecycleCallbacks( SentryFragmentLifecycleCallbacks( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = filterFragmentLifecycleBreadcrumbs, enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ), diff --git a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt index 78f45474e2..64ecb21d41 100644 --- a/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt +++ b/sentry-android-fragment/src/main/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacks.kt @@ -8,9 +8,9 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryLevel.INFO import io.sentry.SpanStatus import io.sentry.TypeCheckHint.ANDROID_FRAGMENT @@ -20,17 +20,17 @@ private const val TRACE_ORIGIN = "auto.ui.fragment" @Suppress("TooManyFunctions") class SentryFragmentLifecycleCallbacks( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), val filterFragmentLifecycleBreadcrumbs: Set, val enableAutoFragmentLifecycleTracing: Boolean ) : FragmentLifecycleCallbacks() { constructor( - hub: IHub, + scopes: IScopes, enableFragmentLifecycleBreadcrumbs: Boolean, enableAutoFragmentLifecycleTracing: Boolean ) : this( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = FragmentLifecycleState.values().toSet() .takeIf { enableFragmentLifecycleBreadcrumbs } .orEmpty(), @@ -41,14 +41,14 @@ class SentryFragmentLifecycleCallbacks( enableFragmentLifecycleBreadcrumbs: Boolean = true, enableAutoFragmentLifecycleTracing: Boolean = false ) : this( - hub = HubAdapter.getInstance(), + scopes = ScopesAdapter.getInstance(), filterFragmentLifecycleBreadcrumbs = FragmentLifecycleState.values().toSet() .takeIf { enableFragmentLifecycleBreadcrumbs } .orEmpty(), enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) - private val isPerformanceEnabled get() = hub.options.isTracingEnabled && enableAutoFragmentLifecycleTracing + private val isPerformanceEnabled get() = scopes.options.isTracingEnabled && enableAutoFragmentLifecycleTracing private val fragmentsWithOngoingTransactions = WeakHashMap() @@ -141,7 +141,7 @@ class SentryFragmentLifecycleCallbacks( val hint = Hint() .also { it.set(ANDROID_FRAGMENT, fragment) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun getFragmentName(fragment: Fragment): String { @@ -157,7 +157,7 @@ class SentryFragmentLifecycleCallbacks( } var transaction: ISpan? = null - hub.configureScope { + scopes.configureScope { transaction = it.transaction } diff --git a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt index 032aef58e1..84286503b9 100644 --- a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt +++ b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/FragmentLifecycleIntegrationTest.kt @@ -4,7 +4,7 @@ import android.app.Activity import android.app.Application import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentManager -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import org.mockito.kotlin.check import org.mockito.kotlin.doReturn @@ -24,14 +24,14 @@ class FragmentLifecycleIntegrationTest { val fragmentActivity = mock { on { supportFragmentManager } doReturn fragmentManager } - val hub = mock() + val scopes = mock() val options = SentryOptions() fun getSut( enableFragmentLifecycleBreadcrumbs: Boolean = true, enableAutoFragmentLifecycleTracing: Boolean = false ): FragmentLifecycleIntegration { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return FragmentLifecycleIntegration( application = application, enableFragmentLifecycleBreadcrumbs = enableFragmentLifecycleBreadcrumbs, @@ -46,7 +46,7 @@ class FragmentLifecycleIntegrationTest { fun `When register, it should register activity lifecycle callbacks`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application).registerActivityLifecycleCallbacks(sut) } @@ -55,7 +55,7 @@ class FragmentLifecycleIntegrationTest { fun `When close, it should unregister lifecycle callbacks`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() verify(fixture.application).unregisterActivityLifecycleCallbacks(sut) @@ -69,7 +69,7 @@ class FragmentLifecycleIntegrationTest { on { supportFragmentManager } doReturn fragmentManager } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(fragmentActivity, savedInstanceState = null) verify(fragmentManager).registerFragmentLifecycleCallbacks( @@ -84,7 +84,7 @@ class FragmentLifecycleIntegrationTest { fun `When FragmentActivity is created, it should register fragment lifecycle callbacks with passed config`() { val sut = fixture.getSut(enableFragmentLifecycleBreadcrumbs = false, enableAutoFragmentLifecycleTracing = true) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(fixture.fragmentActivity, savedInstanceState = null) verify(fixture.fragmentManager).registerFragmentLifecycleCallbacks( @@ -102,7 +102,7 @@ class FragmentLifecycleIntegrationTest { val sut = fixture.getSut() val activity = mock() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, savedInstanceState = null) } diff --git a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt index 9b7fd5c5f2..26cb5b211a 100644 --- a/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt +++ b/sentry-android-fragment/src/test/java/io/sentry/android/fragment/SentryFragmentLifecycleCallbacksTest.kt @@ -5,8 +5,8 @@ import android.os.Bundle import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import io.sentry.Breadcrumb -import io.sentry.Hub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.ScopeCallback @@ -32,7 +32,7 @@ class SentryFragmentLifecycleCallbacksTest { private class Fixture { val fragmentManager = mock() - val hub = mock() + val scopes = mock() val fragment = mock() val context = mock() val scope = mock() @@ -45,7 +45,7 @@ class SentryFragmentLifecycleCallbacksTest { tracesSampleRate: Double? = 1.0, isAdded: Boolean = true ): SentryFragmentLifecycleCallbacks { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { setTracesSampleRate(tracesSampleRate) } @@ -55,12 +55,12 @@ class SentryFragmentLifecycleCallbacksTest { ) whenever(transaction.startChild(any(), any())).thenReturn(span) whenever(scope.transaction).thenReturn(transaction) - whenever(hub.configureScope(any())).thenAnswer { + whenever(scopes.configureScope(any())).thenAnswer { (it.arguments[0] as ScopeCallback).run(scope) } whenever(fragment.isAdded).thenReturn(isAdded) return SentryFragmentLifecycleCallbacks( - hub = hub, + scopes = scopes, filterFragmentLifecycleBreadcrumbs = loggedFragmentLifecycleStates, enableAutoFragmentLifecycleTracing = enableAutoFragmentLifecycleTracing ) @@ -272,7 +272,7 @@ class SentryFragmentLifecycleCallbacksTest { } private fun verifyBreadcrumbAdded(expectedState: String) { - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { breadcrumb: Breadcrumb -> assertEquals("ui.fragment.lifecycle", breadcrumb.category) assertEquals("navigation", breadcrumb.type) @@ -285,6 +285,6 @@ class SentryFragmentLifecycleCallbacksTest { } private fun verifyBreadcrumbAddedCount(count: Int) { - verify(fixture.hub, times(count)).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, times(count)).addBreadcrumb(any(), anyOrNull()) } } diff --git a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt index e956978086..f685e84874 100644 --- a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt +++ b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/RelayAsserter.kt @@ -93,7 +93,7 @@ class RelayAsserter( /** Request parsed as envelope. */ val envelope: SentryEnvelope? by lazy { try { - EnvelopeReader(Sentry.getCurrentHub().options.serializer) + EnvelopeReader(Sentry.getCurrentScopes().options.serializer) .read(GZIPInputStream(request.body.inputStream())) } catch (e: IOException) { null diff --git a/sentry-android-navigation/api/sentry-android-navigation.api b/sentry-android-navigation/api/sentry-android-navigation.api index 79151bb3fb..03a46d8b87 100644 --- a/sentry-android-navigation/api/sentry-android-navigation.api +++ b/sentry-android-navigation/api/sentry-android-navigation.api @@ -10,11 +10,11 @@ public final class io/sentry/android/navigation/SentryNavigationListener : andro public static final field Companion Lio/sentry/android/navigation/SentryNavigationListener$Companion; public static final field NAVIGATION_OP Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Z)V - public fun (Lio/sentry/IHub;ZZ)V - public fun (Lio/sentry/IHub;ZZLjava/lang/String;)V - public synthetic fun (Lio/sentry/IHub;ZZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Z)V + public fun (Lio/sentry/IScopes;ZZ)V + public fun (Lio/sentry/IScopes;ZZLjava/lang/String;)V + public synthetic fun (Lio/sentry/IScopes;ZZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun onDestinationChanged (Landroidx/navigation/NavController;Landroidx/navigation/NavDestination;Landroid/os/Bundle;)V } diff --git a/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt b/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt index 8fdf8b0df8..bb06d66b3c 100644 --- a/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt +++ b/sentry-android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt @@ -6,9 +6,9 @@ import androidx.navigation.NavController import androidx.navigation.NavDestination import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel.DEBUG import io.sentry.SentryLevel.INFO @@ -34,7 +34,7 @@ private const val TRACE_ORIGIN = "auto.navigation" * with [SentryOptions.idleTimeout] for navigation events. */ class SentryNavigationListener @JvmOverloads constructor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val enableNavigationBreadcrumbs: Boolean = true, private val enableNavigationTracing: Boolean = true, private val traceOriginAppendix: String? = null @@ -43,7 +43,7 @@ class SentryNavigationListener @JvmOverloads constructor( private var previousDestinationRef: WeakReference? = null private var previousArgs: Bundle? = null - private val isPerformanceEnabled get() = hub.options.isTracingEnabled && enableNavigationTracing + private val isPerformanceEnabled get() = scopes.options.isTracingEnabled && enableNavigationTracing private var activeTransaction: ITransaction? = null @@ -91,7 +91,7 @@ class SentryNavigationListener @JvmOverloads constructor( } val hint = Hint() hint.set(TypeCheckHint.ANDROID_NAV_DESTINATION, destination) - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun startTracing( @@ -100,7 +100,7 @@ class SentryNavigationListener @JvmOverloads constructor( arguments: Map ) { if (!isPerformanceEnabled) { - TracingUtils.startNewTrace(hub) + TracingUtils.startNewTrace(scopes) return } @@ -111,7 +111,7 @@ class SentryNavigationListener @JvmOverloads constructor( if (destination.navigatorName == "activity") { // we do not trace navigation between activities to avoid clashing with activity lifecycle tracing - hub.options.logger.log( + scopes.options.logger.log( DEBUG, "Navigating to activity destination, no transaction captured." ) @@ -122,7 +122,7 @@ class SentryNavigationListener @JvmOverloads constructor( var name = destination.route ?: try { controller.context.resources.getResourceEntryName(destination.id) } catch (e: NotFoundException) { - hub.options.logger.log( + scopes.options.logger.log( DEBUG, "Destination id cannot be retrieved from Resources, no transaction captured." ) @@ -134,12 +134,12 @@ class SentryNavigationListener @JvmOverloads constructor( val transactionOptions = TransactionOptions().also { it.isWaitForChildren = true - it.idleTimeout = hub.options.idleTimeout + it.idleTimeout = scopes.options.idleTimeout it.deadlineTimeout = TransactionOptions.DEFAULT_DEADLINE_TIMEOUT_AUTO_TRANSACTION it.isTrimEnd = true } - val transaction = hub.startTransaction( + val transaction = scopes.startTransaction( TransactionContext(name, TransactionNameSource.ROUTE, NAVIGATION_OP), transactionOptions ) @@ -151,7 +151,7 @@ class SentryNavigationListener @JvmOverloads constructor( if (arguments.isNotEmpty()) { transaction.setData("arguments", arguments) } - hub.configureScope { scope -> + scopes.configureScope { scope -> scope.withTransaction { tx -> if (tx == null) { scope.transaction = transaction @@ -166,7 +166,7 @@ class SentryNavigationListener @JvmOverloads constructor( activeTransaction?.finish(status) // clear transaction from scope so others can bind to it - hub.configureScope { scope -> + scopes.configureScope { scope -> scope.withTransaction { tx -> if (tx == activeTransaction) { scope.clearTransaction() diff --git a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt index 76c57159c3..b37133410f 100644 --- a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt +++ b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt @@ -7,8 +7,8 @@ import androidx.navigation.NavController import androidx.navigation.NavDestination import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Scope.IWithTransaction import io.sentry.ScopeCallback @@ -39,7 +39,7 @@ import kotlin.test.assertNull class SentryNavigationListenerTest { class Fixture { - val hub = mock() + val scopes = mock() val destination = mock() val navController = mock() @@ -67,20 +67,20 @@ class SentryNavigationListenerTest { tracesSampleRate ) } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) this.transaction = transaction ?: SentryTracer( TransactionContext( "/$toRoute", SentryNavigationListener.NAVIGATION_OP ), - hub + scopes ) - whenever(hub.startTransaction(any(), any())) + whenever(scopes.startTransaction(any(), any())) .thenReturn(this.transaction) - whenever(hub.configureScope(any())).thenAnswer { + whenever(scopes.configureScope(any())).thenAnswer { (it.arguments[0] as ScopeCallback).run(scope) } @@ -96,7 +96,7 @@ class SentryNavigationListenerTest { whenever(navController.context).thenReturn(context) whenever(destination.route).thenReturn(toRoute) return SentryNavigationListener( - hub, + scopes, enableBreadcrumbs, enableTracing, traceOriginAppendix @@ -112,7 +112,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("navigation", it.type) assertEquals("navigation", it.category) @@ -133,7 +133,7 @@ class SentryNavigationListenerTest { bundleOf("arg1" to "foo", "arg2" to "bar") ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("/route", it.data["to"]) assertEquals(mapOf("arg1" to "foo", "arg2" to "bar"), it.data["to_arguments"]) @@ -152,7 +152,7 @@ class SentryNavigationListenerTest { bundleOf() ) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("/route", it.data["to"]) assertNull(it.data["to_arguments"]) @@ -180,7 +180,7 @@ class SentryNavigationListenerTest { bundleOf("to_arg1" to "to_foo") ) val captor = argumentCaptor() - verify(fixture.hub, times(2)).addBreadcrumb(captor.capture(), any()) + verify(fixture.scopes, times(2)).addBreadcrumb(captor.capture(), any()) captor.secondValue.let { assertEquals("/route_from", it.data["from"]) assertEquals(mapOf("from_arg1" to "from_foo"), it.data["from_arguments"]) @@ -196,7 +196,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test @@ -205,7 +205,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -217,7 +217,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -230,7 +230,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -242,7 +242,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) @@ -254,7 +254,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/route", it.name) assertEquals(SentryNavigationListener.NAVIGATION_OP, it.operation) @@ -270,7 +270,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/github", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) @@ -285,7 +285,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/destination-id-1", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) @@ -304,7 +304,7 @@ class SentryNavigationListenerTest { bundleOf("user_id" to 123, "per_page" to 10) ) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("/github", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) @@ -365,13 +365,13 @@ class SentryNavigationListenerTest { ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = Scope(fixture.options) val propagationContextAtStart = scope.propagationContext - whenever(fixture.hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(fixture.scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).configureScope(any()) + verify(fixture.scopes).configureScope(any()) assertNotSame(propagationContextAtStart, scope.propagationContext) } @@ -399,7 +399,7 @@ class SentryNavigationListenerTest { sut.onDestinationChanged(fixture.navController, fixture.destination, null) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { options -> assertEquals(TransactionOptions.DEFAULT_DEADLINE_TIMEOUT_AUTO_TRANSACTION, options.deadlineTimeout) diff --git a/sentry-android-okhttp/api/sentry-android-okhttp.api b/sentry-android-okhttp/api/sentry-android-okhttp.api index a1ad9114a2..35f42842dd 100644 --- a/sentry-android-okhttp/api/sentry-android-okhttp.api +++ b/sentry-android-okhttp/api/sentry-android-okhttp.api @@ -8,12 +8,12 @@ public final class io/sentry/android/okhttp/BuildConfig { public final class io/sentry/android/okhttp/SentryOkHttpEventListener : okhttp3/EventListener { public fun ()V - public fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V - public synthetic fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V + public synthetic fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lokhttp3/EventListener$Factory;)V public fun (Lokhttp3/EventListener;)V public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V @@ -49,9 +49,9 @@ public final class io/sentry/android/okhttp/SentryOkHttpEventListener : okhttp3/ public final class io/sentry/android/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/android/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response; } diff --git a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt index 7ca5313d8f..f99106e8d9 100644 --- a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt +++ b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEventListener.kt @@ -1,7 +1,7 @@ package io.sentry.android.okhttp -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import okhttp3.Call import okhttp3.Connection import okhttp3.EventListener @@ -42,35 +42,35 @@ import java.net.Proxy ) @Suppress("TooManyFunctions") class SentryOkHttpEventListener( - hub: IHub = HubAdapter.getInstance(), + scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerCreator: ((call: Call) -> EventListener)? = null ) : EventListener() { constructor() : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = null ) constructor(originalEventListener: EventListener) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListener } ) constructor(originalEventListenerFactory: Factory) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this( - hub, + constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListener: EventListener) : this( + scopes, originalEventListenerCreator = { originalEventListener } ) - constructor(hub: IHub = HubAdapter.getInstance(), originalEventListenerFactory: Factory) : this( - hub, + constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerFactory: Factory) : this( + scopes, originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - private val delegate = io.sentry.okhttp.SentryOkHttpEventListener(hub, originalEventListenerCreator) + private val delegate = io.sentry.okhttp.SentryOkHttpEventListener(scopes, originalEventListenerCreator) override fun cacheConditionalHit(call: Call, cachedResponse: Response) { delegate.cacheConditionalHit(call, cachedResponse) diff --git a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt index 971af925ff..3c7e590fe1 100644 --- a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt @@ -1,9 +1,9 @@ package io.sentry.android.okhttp import io.sentry.HttpStatusCodeRange -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.android.okhttp.SentryOkHttpInterceptor.BeforeSpanCallback @@ -17,7 +17,7 @@ import okhttp3.Response * out of the active span bound to the scope for each HTTP Request. * If [captureFailedRequests] is enabled, the SDK will capture HTTP Client errors as well. * - * @param hub The [IHub], internal and only used for testing. + * @param scopes The [IScopes], internal and only used for testing. * @param beforeSpan The [ISpan] can be customized or dropped with the [BeforeSpanCallback]. * @param captureFailedRequests The SDK will only capture HTTP Client errors if it is enabled, * Defaults to false. @@ -31,7 +31,7 @@ import okhttp3.Response ReplaceWith("SentryOkHttpInterceptor", "io.sentry.okhttp.SentryOkHttpInterceptor") ) class SentryOkHttpInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = true, private val failedRequestStatusCodes: List = listOf( @@ -39,7 +39,7 @@ class SentryOkHttpInterceptor( ), private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) ) : Interceptor by io.sentry.okhttp.SentryOkHttpInterceptor( - hub, + scopes, { span, request, response -> beforeSpan ?: return@SentryOkHttpInterceptor span beforeSpan.execute(span, request, response) @@ -49,9 +49,9 @@ class SentryOkHttpInterceptor( failedRequestTargets ) { - constructor() : this(HubAdapter.getInstance()) - constructor(hub: IHub) : this(hub, null) - constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + constructor() : this(ScopesAdapter.getInstance()) + constructor(scopes: IScopes) : this(scopes, null) + constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) diff --git a/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt b/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt index 3bfa855d53..2e39bf76ec 100644 --- a/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt +++ b/sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt @@ -1,8 +1,8 @@ package io.sentry.android.sqlite import android.database.SQLException -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryStackTraceFactory import io.sentry.SpanDataConvention @@ -11,10 +11,10 @@ import io.sentry.SpanStatus private const val TRACE_ORIGIN = "auto.db.sqlite" internal class SQLiteSpanManager( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val databaseName: String? = null ) { - private val stackTraceFactory = SentryStackTraceFactory(hub.options) + private val stackTraceFactory = SentryStackTraceFactory(scopes.options) init { SentryIntegrationPackageStorage.getInstance().addIntegration("SQLite") @@ -30,7 +30,7 @@ internal class SQLiteSpanManager( @Suppress("TooGenericExceptionCaught") @Throws(SQLException::class) fun performSql(sql: String, operation: () -> T): T { - val span = hub.span?.startChild("db.sql.query", sql) + val span = scopes.span?.startChild("db.sql.query", sql) span?.spanContext?.origin = TRACE_ORIGIN return try { val result = operation() @@ -42,7 +42,7 @@ internal class SQLiteSpanManager( throw e } finally { span?.apply { - val isMainThread: Boolean = hub.options.mainThreadChecker.isMainThread + val isMainThread: Boolean = scopes.options.mainThreadChecker.isMainThread setData(SpanDataConvention.BLOCKED_MAIN_THREAD_KEY, isMainThread) if (isMainThread) { setData(SpanDataConvention.CALL_STACK_KEY, stackTraceFactory.inAppCallStack) diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt index e2fa0c2e4d..9265cd260a 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.sqlite import android.database.SQLException -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -22,7 +22,7 @@ import kotlin.test.assertTrue class SQLiteSpanManagerTest { private class Fixture { - private val hub = mock() + private val scopes = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -30,13 +30,13 @@ class SQLiteSpanManagerTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } - return SQLiteSpanManager(hub, databaseName) + return SQLiteSpanManager(scopes, databaseName) } } diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt index cf22c3b0ec..99e1d5f4a0 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabaseTest.kt @@ -3,7 +3,7 @@ package io.sentry.android.sqlite import android.database.Cursor import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteQuery -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -23,8 +23,8 @@ import kotlin.test.assertTrue class SentrySupportSQLiteDatabaseTest { private class Fixture { - private val hub = mock() - private val spanManager = SQLiteSpanManager(hub) + private val scopes = mock() + private val spanManager = SQLiteSpanManager(scopes) val mockDatabase = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -37,11 +37,11 @@ class SentrySupportSQLiteDatabaseTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return SentrySupportSQLiteDatabase(mockDatabase, spanManager) diff --git a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt index 9078ba8b08..4b6292bd27 100644 --- a/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt +++ b/sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SentrySupportSQLiteStatementTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.sqlite import androidx.sqlite.db.SupportSQLiteStatement -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -18,8 +18,8 @@ import kotlin.test.assertTrue class SentrySupportSQLiteStatementTest { private class Fixture { - private val hub = mock() - private val spanManager = SQLiteSpanManager(hub) + private val scopes = mock() + private val spanManager = SQLiteSpanManager(scopes) val mockStatement = mock() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -28,11 +28,11 @@ class SentrySupportSQLiteStatementTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" } - whenever(hub.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(options) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } return SentrySupportSQLiteStatement(mockStatement, spanManager, sql) } diff --git a/sentry-android-timber/api/sentry-android-timber.api b/sentry-android-timber/api/sentry-android-timber.api index 808e91bf10..2d71f67570 100644 --- a/sentry-android-timber/api/sentry-android-timber.api +++ b/sentry-android-timber/api/sentry-android-timber.api @@ -14,11 +14,11 @@ public final class io/sentry/android/timber/SentryTimberIntegration : io/sentry/ public fun close ()V public final fun getMinBreadcrumbLevel ()Lio/sentry/SentryLevel; public final fun getMinEventLevel ()Lio/sentry/SentryLevel; - public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V } public final class io/sentry/android/timber/SentryTimberTree : timber/log/Timber$Tree { - public fun (Lio/sentry/IHub;Lio/sentry/SentryLevel;Lio/sentry/SentryLevel;)V + public fun (Lio/sentry/IScopes;Lio/sentry/SentryLevel;Lio/sentry/SentryLevel;)V public fun d (Ljava/lang/String;[Ljava/lang/Object;)V public fun d (Ljava/lang/Throwable;)V public fun d (Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/Object;)V diff --git a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt index d043faa5f6..334146a218 100644 --- a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt +++ b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberIntegration.kt @@ -1,7 +1,7 @@ package io.sentry.android.timber -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.Integration import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel @@ -21,10 +21,10 @@ class SentryTimberIntegration( private lateinit var tree: SentryTimberTree private lateinit var logger: ILogger - override fun register(hub: IHub, options: SentryOptions) { + override fun register(scopes: IScopes, options: SentryOptions) { logger = options.logger - tree = SentryTimberTree(hub, minEventLevel, minBreadcrumbLevel) + tree = SentryTimberTree(scopes, minEventLevel, minBreadcrumbLevel) Timber.plant(tree) logger.log(SentryLevel.DEBUG, "SentryTimberIntegration installed.") diff --git a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt index f3a0f599a9..dddab75133 100644 --- a/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt +++ b/sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt @@ -2,7 +2,7 @@ package io.sentry.android.timber import android.util.Log import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.protocol.Message @@ -13,7 +13,7 @@ import timber.log.Timber */ @Suppress("TooManyFunctions") // we have to override all methods to be able to tweak logging class SentryTimberTree( - private val hub: IHub, + private val scopes: IScopes, private val minEventLevel: SentryLevel, private val minBreadcrumbLevel: SentryLevel ) : Timber.Tree() { @@ -269,7 +269,7 @@ class SentryTimberTree( logger = "Timber" } - hub.captureEvent(sentryEvent) + scopes.captureEvent(sentryEvent) } } @@ -296,7 +296,7 @@ class SentryTimberTree( else -> null } - breadCrumb?.let { hub.addBreadcrumb(it) } + breadCrumb?.let { scopes.addBreadcrumb(it) } } } diff --git a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt index a57853e059..8bb85aa085 100644 --- a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt +++ b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.android.timber -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.protocol.SdkVersion @@ -16,7 +16,7 @@ import kotlin.test.assertTrue class SentryTimberIntegrationTest { private class Fixture { - val hub = mock() + val scopes = mock() val options = SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") } @@ -41,7 +41,7 @@ class SentryTimberIntegrationTest { @Test fun `Integrations plants a tree into Timber on register`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(1, Timber.treeCount()) @@ -53,16 +53,16 @@ class SentryTimberIntegrationTest { @Test fun `Integrations plants the SentryTimberTree tree`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) Timber.e(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Integrations removes a tree from Timber on close integration`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(1, Timber.treeCount()) @@ -84,7 +84,7 @@ class SentryTimberIntegrationTest { minEventLevel = SentryLevel.INFO, minBreadcrumbLevel = SentryLevel.DEBUG ) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertEquals(sut.minEventLevel, SentryLevel.INFO) assertEquals(sut.minBreadcrumbLevel, SentryLevel.DEBUG) @@ -93,7 +93,7 @@ class SentryTimberIntegrationTest { @Test fun `Integration adds itself to the package list`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.packageSet.any { @@ -106,7 +106,7 @@ class SentryTimberIntegrationTest { @Test fun `Integration adds itself to the integration list`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) assertTrue( fixture.options.sdkVersion!!.integrationSet.contains("Timber") diff --git a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt index 3d82b139ec..2ab7ff64db 100644 --- a/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt +++ b/sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt @@ -1,7 +1,7 @@ package io.sentry.android.timber import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryLevel import io.sentry.getExc import org.mockito.kotlin.any @@ -19,13 +19,13 @@ import kotlin.test.assertNull class SentryTimberTreeTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut( minEventLevel: SentryLevel = SentryLevel.ERROR, minBreadcrumbLevel: SentryLevel = SentryLevel.INFO ): SentryTimberTree { - return SentryTimberTree(hub, minEventLevel, minBreadcrumbLevel) + return SentryTimberTree(scopes, minEventLevel, minBreadcrumbLevel) } } @@ -40,28 +40,28 @@ class SentryTimberTreeTest { fun `Tree captures an event if min level is equal`() { val sut = fixture.getSut() sut.e(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Tree captures an event if min level is higher`() { val sut = fixture.getSut() sut.wtf(Throwable()) - verify(fixture.hub).captureEvent(any()) + verify(fixture.scopes).captureEvent(any()) } @Test fun `Tree won't capture an event if min level is lower`() { val sut = fixture.getSut() sut.d(Throwable()) - verify(fixture.hub, never()).captureEvent(any()) + verify(fixture.scopes, never()).captureEvent(any()) } @Test fun `Tree captures debug level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.d(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.DEBUG, it.level) } @@ -72,7 +72,7 @@ class SentryTimberTreeTest { fun `Tree captures info level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.i(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.INFO, it.level) } @@ -83,7 +83,7 @@ class SentryTimberTreeTest { fun `Tree captures warning level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.w(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.WARNING, it.level) } @@ -94,7 +94,7 @@ class SentryTimberTreeTest { fun `Tree captures error level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.e(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.ERROR, it.level) } @@ -105,7 +105,7 @@ class SentryTimberTreeTest { fun `Tree captures fatal level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.wtf(Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.FATAL, it.level) } @@ -116,7 +116,7 @@ class SentryTimberTreeTest { fun `Tree captures unknown as debug level event`() { val sut = fixture.getSut(SentryLevel.DEBUG) sut.log(15, Throwable()) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(SentryLevel.DEBUG, it.level) } @@ -128,7 +128,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() val throwable = Throwable() sut.e(throwable) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(throwable, it.getExc()) } @@ -139,7 +139,7 @@ class SentryTimberTreeTest { fun `Tree captures an event without an exception`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNull(it.getExc()) } @@ -150,7 +150,7 @@ class SentryTimberTreeTest { fun `Tree captures an event and sets Timber as a logger`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals("Timber", it.logger) } @@ -164,7 +164,7 @@ class SentryTimberTreeTest { // only available thru static class Timber.tag("tag") Timber.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals("tag", it.getTag("TimberTag")) } @@ -176,7 +176,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() Timber.plant(sut) Timber.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNull(it.getTag("TimberTag")) } @@ -187,7 +187,7 @@ class SentryTimberTreeTest { fun `Tree captures an event with given message`() { val sut = fixture.getSut() sut.e("message") - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNotNull(it.message) { message -> assertEquals("message", message.message) @@ -200,7 +200,7 @@ class SentryTimberTreeTest { fun `Tree captures an event with formatted message and arguments, when provided`() { val sut = fixture.getSut() sut.e("test count: %d", 32) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertNotNull(it.message) { message -> assertEquals("test count: %d", message.message) @@ -216,7 +216,7 @@ class SentryTimberTreeTest { val sut = fixture.getSut() sut.e("test count: %d", 32) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("test count: 32", it.message) } @@ -227,28 +227,28 @@ class SentryTimberTreeTest { fun `Tree adds a breadcrumb if min level is equal`() { val sut = fixture.getSut() sut.i(Throwable("test")) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) } @Test fun `Tree adds a breadcrumb if min level is higher`() { val sut = fixture.getSut() sut.e(Throwable("test")) - verify(fixture.hub).addBreadcrumb(any()) + verify(fixture.scopes).addBreadcrumb(any()) } @Test fun `Tree won't add a breadcrumb if min level is lower`() { val sut = fixture.getSut(minBreadcrumbLevel = SentryLevel.ERROR) sut.i(Throwable("test")) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test fun `Tree adds an info breadcrumb`() { val sut = fixture.getSut() sut.i("message") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("Timber", it.category) assertEquals(SentryLevel.INFO, it.level) @@ -261,7 +261,7 @@ class SentryTimberTreeTest { fun `Tree adds an error breadcrumb`() { val sut = fixture.getSut() sut.e(Throwable("test")) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("exception", it.category) assertEquals(SentryLevel.ERROR, it.level) @@ -274,7 +274,7 @@ class SentryTimberTreeTest { fun `Tree does not add a breadcrumb, if no message provided`() { val sut = fixture.getSut() sut.e(Throwable()) - verify(fixture.hub, never()).addBreadcrumb(any()) + verify(fixture.scopes, never()).addBreadcrumb(any()) } @Test From 305c217401c21fb47ea48d9b20bef1a433c80120 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:26:37 +0200 Subject: [PATCH 07/57] Replace `IHub` with `IScopes` in apollo integrations --- sentry-apollo-3/api/sentry-apollo-3.api | 20 +++++----- .../apollo3/SentryApollo3HttpInterceptor.kt | 34 ++++++++-------- .../apollo3/SentryApolloBuilderExtensions.kt | 10 ++--- .../SentryApollo3InterceptorClientErrors.kt | 40 +++++++++---------- .../apollo3/SentryApollo3InterceptorTest.kt | 40 +++++++++---------- ...ntryApollo3InterceptorWithVariablesTest.kt | 22 +++++----- sentry-apollo/api/sentry-apollo.api | 6 +-- .../sentry/apollo/SentryApolloInterceptor.kt | 20 +++++----- .../apollo/SentryApolloInterceptorTest.kt | 38 +++++++++--------- 9 files changed, 115 insertions(+), 115 deletions(-) diff --git a/sentry-apollo-3/api/sentry-apollo-3.api b/sentry-apollo-3/api/sentry-apollo-3.api index 1c80e1950b..e106585156 100644 --- a/sentry-apollo-3/api/sentry-apollo-3.api +++ b/sentry-apollo-3/api/sentry-apollo-3.api @@ -17,11 +17,11 @@ public final class io/sentry/apollo3/SentryApollo3HttpInterceptor : com/apollogr public static final field SENTRY_APOLLO_3_OPERATION_TYPE Ljava/lang/String; public static final field SENTRY_APOLLO_3_VARIABLES Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;Z)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;Z)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ZLjava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun dispose ()V public fun intercept (Lcom/apollographql/apollo3/api/http/HttpRequest;Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } @@ -40,12 +40,12 @@ public final class io/sentry/apollo3/SentryApollo3Interceptor : com/apollographq public final class io/sentry/apollo3/SentryApolloBuilderExtensionsKt { public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; public static final fun sentryTracing (Lcom/apollographql/apollo3/ApolloClient$Builder;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IHub;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lio/sentry/IScopes;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; public static synthetic fun sentryTracing$default (Lcom/apollographql/apollo3/ApolloClient$Builder;ZLjava/util/List;Lio/sentry/apollo3/SentryApollo3HttpInterceptor$BeforeSpanCallback;ILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; } diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt index 08cab179a5..52219cb8e1 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt @@ -11,9 +11,9 @@ import com.apollographql.apollo3.network.http.HttpInterceptorChain import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryEvent import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel @@ -41,7 +41,7 @@ import java.util.Locale private const val TRACE_ORIGIN = "auto.graphql.apollo3" class SentryApollo3HttpInterceptor @JvmOverloads constructor( - @ApiStatus.Internal private val hub: IHub = HubAdapter.getInstance(), + @ApiStatus.Internal private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = DEFAULT_CAPTURE_FAILED_REQUESTS, private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) @@ -65,7 +65,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( request: HttpRequest, chain: HttpInterceptorChain ): HttpResponse { - val activeSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val activeSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span val operationName = getHeader(HEADER_APOLLO_OPERATION_NAME, request.headers) val operationType = decodeHeaderValue(request, SENTRY_APOLLO_3_OPERATION_TYPE) @@ -77,7 +77,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( span = startChild(request, activeSpan, operationName, operationType, operationId) } - val modifiedRequest = maybeAddTracingHeaders(hub, request, span) + val modifiedRequest = maybeAddTracingHeaders(scopes, request, span) var httpResponse: HttpResponse? = null var statusCode: Int? = null @@ -117,10 +117,10 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( } } - private fun maybeAddTracingHeaders(hub: IHub, request: HttpRequest, span: ISpan?): HttpRequest { + private fun maybeAddTracingHeaders(scopes: IScopes, request: HttpRequest, span: ISpan?): HttpRequest { var cleanedHeaders = removeSentryInternalHeaders(request.headers).toMutableList() - TracingUtils.traceIfAllowed(hub, request.url, request.headers.filter { it.name == BaggageHeader.BAGGAGE_HEADER }.map { it.value }, span)?.let { + TracingUtils.traceIfAllowed(scopes, request.url, request.headers.filter { it.name == BaggageHeader.BAGGAGE_HEADER }.map { it.value }, span)?.let { cleanedHeaders.add(HttpHeader(it.sentryTraceHeader.name, it.sentryTraceHeader.value)) it.baggageHeader?.let { baggageHeader -> cleanedHeaders = cleanedHeaders.filterNot { it.name == BaggageHeader.BAGGAGE_HEADER }.toMutableList().apply { @@ -179,7 +179,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( try { String(Base64.decode(it, Base64.NO_WRAP)) } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error decoding internal apolloHeader $headerName", e @@ -218,7 +218,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( span.spanContext.sampled = false } } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e @@ -256,7 +256,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( hint.set(APOLLO_RESPONSE, httpResponse) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } // Extensions @@ -273,7 +273,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( private fun getHeaders(headers: List): MutableMap? { // Headers are only sent if isSendDefaultPii is enabled due to PII - if (!hub.options.isSendDefaultPii) { + if (!scopes.options.isSendDefaultPii) { return null } @@ -311,7 +311,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( val body = try { response.body?.peek()?.readUtf8() ?: "" } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error reading the response body.", e @@ -368,7 +368,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( urlDetails.applyToRequest(this) // Cookie is only sent if isSendDefaultPii is enabled cookies = - if (hub.options.isSendDefaultPii) getHeader("Cookie", request.headers) else null + if (scopes.options.isSendDefaultPii) getHeader("Cookie", request.headers) else null method = request.method.name headers = getHeaders(request.headers) apiTarget = "graphql" @@ -382,7 +382,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( it.writeTo(buffer) data = buffer.readUtf8() } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error reading the request body.", e @@ -396,7 +396,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( val sentryResponse = Response().apply { // Set-Cookie is only sent if isSendDefaultPii is enabled due to PII - cookies = if (hub.options.isSendDefaultPii) { + cookies = if (scopes.options.isSendDefaultPii) { getHeader( "Set-Cookie", response.headers @@ -419,9 +419,9 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( event.contexts.setResponse(sentryResponse) event.fingerprints = fingerprints - hub.captureEvent(event, hint) + scopes.captureEvent(event, hint) } catch (e: Throwable) { - hub.options.logger.log( + scopes.options.logger.log( SentryLevel.ERROR, "Error capturing the GraphQL error.", e diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt index 2cdbc148fb..b40b1c183d 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt @@ -1,14 +1,14 @@ package io.sentry.apollo3 import com.apollographql.apollo3.ApolloClient -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.apollo3.SentryApollo3HttpInterceptor.Companion.DEFAULT_CAPTURE_FAILED_REQUESTS @JvmOverloads fun ApolloClient.Builder.sentryTracing( - hub: IHub = HubAdapter.getInstance(), + scopes: IScopes = ScopesAdapter.getInstance(), captureFailedRequests: Boolean = DEFAULT_CAPTURE_FAILED_REQUESTS, failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS), beforeSpan: SentryApollo3HttpInterceptor.BeforeSpanCallback? = null @@ -16,7 +16,7 @@ fun ApolloClient.Builder.sentryTracing( addInterceptor(SentryApollo3Interceptor()) addHttpInterceptor( SentryApollo3HttpInterceptor( - hub = hub, + scopes = scopes, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, beforeSpan = beforeSpan @@ -31,7 +31,7 @@ fun ApolloClient.Builder.sentryTracing( beforeSpan: SentryApollo3HttpInterceptor.BeforeSpanCallback? = null ): ApolloClient.Builder { return sentryTracing( - hub = HubAdapter.getInstance(), + scopes = ScopesAdapter.getInstance(), captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, beforeSpan = beforeSpan diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt index 40406b77b5..b3f8b6d57e 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt @@ -5,7 +5,7 @@ import com.apollographql.apollo3.api.http.HttpRequest import com.apollographql.apollo3.api.http.HttpResponse import com.apollographql.apollo3.exception.ApolloException import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS @@ -35,7 +35,7 @@ import kotlin.test.assertTrue class SentryApollo3InterceptorClientErrors { class Fixture { val server = MockWebServer() - lateinit var hub: IHub + lateinit var scopes: IScopes private val responseBodyOk = """{ @@ -75,7 +75,7 @@ class SentryApollo3InterceptorClientErrors { ): ApolloClient { SentryIntegrationPackageStorage.getInstance().clearStorage() - hub = mock().apply { + scopes = mock().apply { whenever(options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" @@ -84,7 +84,7 @@ class SentryApollo3InterceptorClientErrors { } ) } - whenever(hub.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) + whenever(scopes.captureEvent(any(), any())).thenReturn(SentryId.EMPTY_ID) val response = MockResponse() .setBody(responseBody) @@ -102,7 +102,7 @@ class SentryApollo3InterceptorClientErrors { val builder = ApolloClient.Builder() .serverUrl(server.url("?myQuery=query#myFragment").toString()) .sentryTracing( - hub = hub, + scopes = scopes, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets ) @@ -123,7 +123,7 @@ class SentryApollo3InterceptorClientErrors { val sut = fixture.getSut(captureFailedRequests = false, responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -132,7 +132,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } // endregion @@ -165,7 +165,7 @@ class SentryApollo3InterceptorClientErrors { ) executeQuery(sut) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -174,7 +174,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } // endregion @@ -187,7 +187,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertEquals("SentryApollo3Interceptor", throwable.exceptionMechanism.type) @@ -202,7 +202,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertEquals("GraphQL Request failed, name: LaunchDetails, type: query", throwable.throwable.message) @@ -217,7 +217,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val throwable = (it.throwableMechanism as ExceptionMechanismException) assertTrue(throwable.isSnapshot) @@ -238,7 +238,7 @@ class SentryApollo3InterceptorClientErrors { {"operationName":"LaunchDetails","variables":{"id":"83"},"query":"query LaunchDetails($escapeDolar: ID!) { launch(id: $escapeDolar) { id site mission { name missionPatch(size: LARGE) } rocket { name type } } }"} """.trimIndent() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val request = it.request!! @@ -262,7 +262,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val request = it.request!! @@ -280,7 +280,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val response = it.contexts.response!! @@ -300,7 +300,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val response = it.contexts.response!! @@ -318,7 +318,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { assertEquals(listOf("LaunchDetails", "query", "200"), it.fingerprints) }, @@ -337,7 +337,7 @@ class SentryApollo3InterceptorClientErrors { executeQuery(sut) // HttpInterceptor does not throw for >= 400 - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -345,7 +345,7 @@ class SentryApollo3InterceptorClientErrors { val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk) - whenever(fixture.hub.captureEvent(any(), any())).thenThrow(RuntimeException()) + whenever(fixture.scopes.captureEvent(any(), any())).thenThrow(RuntimeException()) executeQuery(sut) } @@ -360,7 +360,7 @@ class SentryApollo3InterceptorClientErrors { fixture.getSut(responseBody = fixture.responseBodyNotOk) executeQuery(sut) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { val request = it.get(TypeCheckHint.APOLLO_REQUEST) diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt index 44d8bfd624..3b836f45b9 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt @@ -9,7 +9,7 @@ import com.apollographql.apollo3.network.http.HttpInterceptor import com.apollographql.apollo3.network.http.HttpInterceptorChain import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.Scope import io.sentry.ScopeCallback @@ -57,11 +57,11 @@ class SentryApollo3InterceptorTest { sdkVersion = SdkVersion("test", "1.2.3") } val scope = Scope(options) - val hub = mock().also { + val scopes = mock().also { whenever(it.options).thenReturn(options) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(it).configureScope(any()) } - private var httpInterceptor = SentryApollo3HttpInterceptor(hub, captureFailedRequests = false) + private var httpInterceptor = SentryApollo3HttpInterceptor(scopes, captureFailedRequests = false) @SuppressWarnings("LongParameterList") fun getSut( @@ -93,7 +93,7 @@ class SentryApollo3InterceptorTest { ) if (beforeSpan != null) { - httpInterceptor = SentryApollo3HttpInterceptor(hub, beforeSpan, captureFailedRequests = false) + httpInterceptor = SentryApollo3HttpInterceptor(scopes, beforeSpan, captureFailedRequests = false) } val builder = ApolloClient.Builder() @@ -124,7 +124,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 200) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -139,7 +139,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 403) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -159,7 +159,7 @@ class SentryApollo3InterceptorTest { } executeQuery(fixture.getSut(interceptor = failingInterceptor)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = 404, contentLength = null) assertEquals("POST", it.spans.first().data?.get(SpanDataConvention.HTTP_METHOD_KEY)) @@ -176,7 +176,7 @@ class SentryApollo3InterceptorTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it, httpStatusCode = null, contentLength = null) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -241,7 +241,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) val httpClientSpan = it.spans.first() @@ -261,7 +261,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(0, it.spans.size) }, @@ -281,7 +281,7 @@ class SentryApollo3InterceptorTest { ) ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) }, @@ -294,7 +294,7 @@ class SentryApollo3InterceptorTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) // response_body_size is added but mock webserver returns 0 always @@ -309,9 +309,9 @@ class SentryApollo3InterceptorTest { @Test fun `sets SDKVersion Info`() { - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("Apollo3")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo-3" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("Apollo3")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo-3" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } @@ -320,14 +320,14 @@ class SentryApollo3InterceptorTest { fun `attaches to root transaction on Android`() { Apollo3PlatformTestManipulator.pretendIsAndroid(true) executeQuery(fixture.getSut()) - verify(fixture.hub).transaction + verify(fixture.scopes).transaction } @Test fun `attaches to child span on non-Android`() { Apollo3PlatformTestManipulator.pretendIsAndroid(false) executeQuery(fixture.getSut()) - verify(fixture.hub).span + verify(fixture.scopes).span } private fun assertTransactionDetails(it: SentryTransaction, httpStatusCode: Int? = 200, contentLength: Long? = 0L) { @@ -350,9 +350,9 @@ class SentryApollo3InterceptorTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true, id: String = "83") = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.transaction).thenReturn(tx) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.transaction).thenReturn(tx) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt index 81775efc18..3ac3d80d7d 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt @@ -3,7 +3,7 @@ package io.sentry.apollo3 import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.exception.ApolloException import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -32,7 +32,7 @@ class SentryApollo3InterceptorWithVariablesTest { class Fixture { val server = MockWebServer() - val hub = mock() + val scopes = mock() @SuppressWarnings("LongParameterList") fun getSut( @@ -54,7 +54,7 @@ class SentryApollo3InterceptorWithVariablesTest { socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, beforeSpan: BeforeSpanCallback? = null ): ApolloClient { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "http://key@localhost/proj" } @@ -68,7 +68,7 @@ class SentryApollo3InterceptorWithVariablesTest { ) return ApolloClient.Builder().serverUrl(server.url("/").toString()) - .sentryTracing(hub = hub, beforeSpan = beforeSpan, captureFailedRequests = false) + .sentryTracing(scopes = scopes, beforeSpan = beforeSpan, captureFailedRequests = false) .build() } } @@ -79,7 +79,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -94,7 +94,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -109,7 +109,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -124,7 +124,7 @@ class SentryApollo3InterceptorWithVariablesTest { fun `handles non-ascii header values correctly`() { executeQuery(id = "á") - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -138,7 +138,7 @@ class SentryApollo3InterceptorWithVariablesTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) // response_body_size is added but mock webserver returns 0 always @@ -173,8 +173,8 @@ class SentryApollo3InterceptorWithVariablesTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true, id: String = "83") = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { diff --git a/sentry-apollo/api/sentry-apollo.api b/sentry-apollo/api/sentry-apollo.api index 8c18bce06e..63eac6a193 100644 --- a/sentry-apollo/api/sentry-apollo.api +++ b/sentry-apollo/api/sentry-apollo.api @@ -5,9 +5,9 @@ public final class io/sentry/apollo/BuildConfig { public final class io/sentry/apollo/SentryApolloInterceptor : com/apollographql/apollo/interceptor/ApolloInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V public fun dispose ()V public fun interceptAsync (Lcom/apollographql/apollo/interceptor/ApolloInterceptor$InterceptorRequest;Lcom/apollographql/apollo/interceptor/ApolloInterceptorChain;Ljava/util/concurrent/Executor;Lcom/apollographql/apollo/interceptor/ApolloInterceptor$CallBack;)V diff --git a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt index faa8a549a9..fe5a6a4762 100644 --- a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt +++ b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt @@ -15,9 +15,9 @@ import com.apollographql.apollo.request.RequestHeaders import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryLevel import io.sentry.SpanDataConvention @@ -32,12 +32,12 @@ import java.util.concurrent.Executor private const val TRACE_ORIGIN = "auto.graphql.apollo" class SentryApolloInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null ) : ApolloInterceptor { - constructor(hub: IHub) : this(hub, null) - constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + constructor(scopes: IScopes) : this(scopes, null) + constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) @@ -45,7 +45,7 @@ class SentryApolloInterceptor( } override fun interceptAsync(request: InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: CallBack) { - val activeSpan = if (io.sentry.util.Platform.isAndroid()) hub.transaction else hub.span + val activeSpan = if (io.sentry.util.Platform.isAndroid()) scopes.transaction else scopes.span if (activeSpan == null) { val headers = addTracingHeaders(request, null) val modifiedRequest = request.toBuilder().requestHeaders(headers).build() @@ -115,10 +115,10 @@ class SentryApolloInterceptor( private fun addTracingHeaders(request: InterceptorRequest, span: ISpan?): RequestHeaders { val requestHeaderBuilder = request.requestHeaders.toBuilder() - if (hub.options.isTraceSampling) { + if (scopes.options.isTraceSampling) { // we have no access to URI, no way to verify tracing origins TracingUtils.trace( - hub, + scopes, listOf(request.requestHeaders.headerValue(BaggageHeader.BAGGAGE_HEADER)), span )?.let { tracingHeaders -> @@ -154,7 +154,7 @@ class SentryApolloInterceptor( try { newSpan = beforeSpan.execute(span, request, response) } catch (e: Exception) { - hub.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) + scopes.options.logger.log(SentryLevel.ERROR, "An error occurred while executing beforeSpan on ApolloInterceptor", e) } } if (newSpan == null) { @@ -182,7 +182,7 @@ class SentryApolloInterceptor( set(APOLLO_REQUEST, httpRequest) set(APOLLO_RESPONSE, httpResponse) } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } } } diff --git a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt index d22c2fd3e5..b1b118c334 100644 --- a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt +++ b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt @@ -5,7 +5,7 @@ import com.apollographql.apollo.coroutines.await import com.apollographql.apollo.exception.ApolloException import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransaction import io.sentry.Scope import io.sentry.ScopeCallback @@ -48,13 +48,13 @@ class SentryApolloInterceptorTest { sdkVersion = SdkVersion("test", "1.2.3") } val scope = Scope(options) - val hub = mock().also { + val scopes = mock().also { whenever(it.options).thenReturn(options) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(it).configureScope( any() ) } - private var interceptor = SentryApolloInterceptor(hub) + private var interceptor = SentryApolloInterceptor(scopes) @SuppressWarnings("LongParameterList") fun getSut( @@ -84,7 +84,7 @@ class SentryApolloInterceptorTest { ) if (beforeSpan != null) { - interceptor = SentryApolloInterceptor(hub, beforeSpan) + interceptor = SentryApolloInterceptor(scopes, beforeSpan) } return ApolloClient.builder() .serverUrl(server.url("/")) @@ -104,7 +104,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the successful request`() { executeQuery() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.OK, it.spans.first().status) @@ -120,7 +120,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the failed request`() { executeQuery(fixture.getSut(httpStatusCode = 403)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.PERMISSION_DENIED, it.spans.first().status) @@ -138,7 +138,7 @@ class SentryApolloInterceptorTest { fun `creates a span around the request failing with network error`() { executeQuery(fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_DURING_REQUEST_BODY)) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTransactionDetails(it) assertEquals(SpanStatus.INTERNAL_ERROR, it.spans.first().status) @@ -176,7 +176,7 @@ class SentryApolloInterceptorTest { } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) val httpClientSpan = it.spans.first() @@ -196,7 +196,7 @@ class SentryApolloInterceptorTest { } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertTrue(it.spans.isEmpty()) }, @@ -212,7 +212,7 @@ class SentryApolloInterceptorTest { fixture.getSut { _, _, _ -> throw RuntimeException() } ) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) }, @@ -225,7 +225,7 @@ class SentryApolloInterceptorTest { @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(280L, it.data["response_body_size"]) @@ -237,9 +237,9 @@ class SentryApolloInterceptorTest { @Test fun `sets SDKVersion Info`() { - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("Apollo")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("Apollo")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-apollo" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } @@ -248,14 +248,14 @@ class SentryApolloInterceptorTest { fun `attaches to root transaction on Android`() { ApolloPlatformTestManipulator.pretendIsAndroid(true) executeQuery(fixture.getSut()) - verify(fixture.hub).transaction + verify(fixture.scopes).transaction } @Test fun `attaches to child span on non-Android`() { ApolloPlatformTestManipulator.pretendIsAndroid(false) executeQuery(fixture.getSut()) - verify(fixture.hub).span + verify(fixture.scopes).span } private fun assertTransactionDetails(it: SentryTransaction) { @@ -273,9 +273,9 @@ class SentryApolloInterceptorTest { private fun executeQuery(sut: ApolloClient = fixture.getSut(), isSpanActive: Boolean = true) = runBlocking { var tx: ITransaction? = null if (isSpanActive) { - tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub) - whenever(fixture.hub.transaction).thenReturn(tx) - whenever(fixture.hub.span).thenReturn(tx) + tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.transaction).thenReturn(tx) + whenever(fixture.scopes.span).thenReturn(tx) } val coroutine = launch { From da927bcca927a31ed5b5a7d186790adb7b857d67 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:30:11 +0200 Subject: [PATCH 08/57] Replace `IHub` with `IScopes` in okhttp integration --- sentry-okhttp/api/sentry-okhttp.api | 18 +++---- .../io/sentry/okhttp/SentryOkHttpEvent.kt | 16 +++--- .../okhttp/SentryOkHttpEventListener.kt | 24 ++++----- .../sentry/okhttp/SentryOkHttpInterceptor.kt | 22 ++++---- .../io/sentry/okhttp/SentryOkHttpUtils.kt | 18 +++---- .../okhttp/SentryOkHttpEventListenerTest.kt | 28 +++++----- .../io/sentry/okhttp/SentryOkHttpEventTest.kt | 52 +++++++++---------- .../okhttp/SentryOkHttpInterceptorTest.kt | 50 +++++++++--------- .../io/sentry/okhttp/SentryOkHttpUtilsTest.kt | 22 ++++---- 9 files changed, 125 insertions(+), 125 deletions(-) diff --git a/sentry-okhttp/api/sentry-okhttp.api b/sentry-okhttp/api/sentry-okhttp.api index 3095659c88..9cb875ff34 100644 --- a/sentry-okhttp/api/sentry-okhttp.api +++ b/sentry-okhttp/api/sentry-okhttp.api @@ -6,12 +6,12 @@ public final class io/sentry/okhttp/BuildConfig { public class io/sentry/okhttp/SentryOkHttpEventListener : okhttp3/EventListener { public static final field Companion Lio/sentry/okhttp/SentryOkHttpEventListener$Companion; public fun ()V - public fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V - public synthetic fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V + public synthetic fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lokhttp3/EventListener$Factory;)V public fun (Lokhttp3/EventListener;)V public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V @@ -50,9 +50,9 @@ public final class io/sentry/okhttp/SentryOkHttpEventListener$Companion { public class io/sentry/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response; } diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt index 00cc26e754..153499210c 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryDate import io.sentry.SentryLevel @@ -30,7 +30,7 @@ private const val RESPONSE_BODY_TIMEOUT_MILLIS = 800L internal const val TRACE_ORIGIN = "auto.http.okhttp" @Suppress("TooManyFunctions") -internal class SentryOkHttpEvent(private val hub: IHub, private val request: Request) { +internal class SentryOkHttpEvent(private val scopes: IScopes, private val request: Request) { private val eventSpans: MutableMap = ConcurrentHashMap() private val breadcrumb: Breadcrumb internal val callRootSpan: ISpan? @@ -47,7 +47,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req val method: String = request.method // We start the call span that will contain all the others - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span callRootSpan = parentSpan?.startChild("http.client", "$method $url") callRootSpan?.spanContext?.origin = TRACE_ORIGIN urlDetails.applyToSpan(callRootSpan) @@ -149,13 +149,13 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) } // We send the breadcrumb even without spans. - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) // No span is created (e.g. no transaction is running) if (callRootSpan == null) { // We report the client error even without spans. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } return } @@ -173,7 +173,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req // We report the client error here, after all sub-spans finished, so that it will be bound // to the root call span. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } if (finishDate != null) { callRootSpan.finish(callRootSpan.status, finishDate) @@ -204,7 +204,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req fun scheduleFinish(timestamp: SentryDate) { try { - hub.options.executorService.schedule({ + scopes.options.executorService.schedule({ if (!isReadingResponseBody.get() && (eventSpans.values.all { it.isFinished } || callRootSpan?.isFinished != true) ) { @@ -212,7 +212,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req } }, RESPONSE_BODY_TIMEOUT_MILLIS) } catch (e: RejectedExecutionException) { - hub.options + scopes.options .logger .log( SentryLevel.ERROR, diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt index 67a8cd8b56..f20e2ef0cb 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SpanDataConvention import io.sentry.SpanStatus import okhttp3.Call @@ -41,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap */ @Suppress("TooManyFunctions") public open class SentryOkHttpEventListener( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val originalEventListenerCreator: ((call: Call) -> EventListener)? = null ) : EventListener() { @@ -62,27 +62,27 @@ public open class SentryOkHttpEventListener( } public constructor() : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = null ) public constructor(originalEventListener: EventListener) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListener } ) public constructor(originalEventListenerFactory: Factory) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListener: EventListener) : this( + scopes, originalEventListenerCreator = { originalEventListener } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListenerFactory: Factory) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerFactory: Factory) : this( + scopes, originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) @@ -92,7 +92,7 @@ public open class SentryOkHttpEventListener( // If the wrapped EventListener is ours, we can just delegate the calls, // without creating other events that would create duplicates if (canCreateEventSpan()) { - eventMap[call] = SentryOkHttpEvent(hub, call.request()) + eventMap[call] = SentryOkHttpEvent(scopes, call.request()) } } @@ -318,7 +318,7 @@ public open class SentryOkHttpEventListener( it.status = SpanStatus.fromHttpStatusCode(response.code) } } - okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: hub.options.dateProvider.now()) + okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: scopes.options.dateProvider.now()) } override fun responseBodyStart(call: Call) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt index efa472963d..a079864612 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt @@ -4,9 +4,9 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.SpanDataConvention @@ -29,7 +29,7 @@ import java.io.IOException * out of the active span bound to the scope for each HTTP Request. * If [captureFailedRequests] is enabled, the SDK will capture HTTP Client errors as well. * - * @param hub The [IHub], internal and only used for testing. + * @param scopes The [IScopes], internal and only used for testing. * @param beforeSpan The [ISpan] can be customized or dropped with the [BeforeSpanCallback]. * @param captureFailedRequests The SDK will only capture HTTP Client errors if it is enabled, * Defaults to false. @@ -39,7 +39,7 @@ import java.io.IOException * is a match for any of the defined targets. */ public open class SentryOkHttpInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = true, private val failedRequestStatusCodes: List = listOf( @@ -48,9 +48,9 @@ public open class SentryOkHttpInterceptor( private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) ) : Interceptor { - public constructor() : this(HubAdapter.getInstance()) - public constructor(hub: IHub) : this(hub, null) - public constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + public constructor() : this(ScopesAdapter.getInstance()) + public constructor(scopes: IScopes) : this(scopes, null) + public constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) @@ -76,7 +76,7 @@ public open class SentryOkHttpInterceptor( } else { // read the span from the bound scope okHttpEvent = null - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span span = parentSpan?.startChild("http.client", "$method $url") } @@ -92,7 +92,7 @@ public open class SentryOkHttpInterceptor( val requestBuilder = request.newBuilder() TracingUtils.traceIfAllowed( - hub, + scopes, request.url.toString(), request.headers(BaggageHeader.BAGGAGE_HEADER), span @@ -121,7 +121,7 @@ public open class SentryOkHttpInterceptor( if (isFromEventListener && okHttpEvent != null) { okHttpEvent.setClientErrorResponse(response) } else { - SentryOkHttpUtils.captureClientError(hub, request, response) + SentryOkHttpUtils.captureClientError(scopes, request, response) } } @@ -157,7 +157,7 @@ public open class SentryOkHttpInterceptor( hint[OKHTTP_RESPONSE] = it } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun finishSpan(span: ISpan?, request: Request, response: Response?, isFromEventListener: Boolean) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt index 0cfc1c5a75..eea35ca22e 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.TypeCheckHint import io.sentry.exception.ExceptionMechanismException @@ -15,7 +15,7 @@ import okhttp3.Response internal object SentryOkHttpUtils { - internal fun captureClientError(hub: IHub, request: Request, response: Response) { + internal fun captureClientError(scopes: IScopes, request: Request, response: Response) { // not possible to get a parameterized url, but we remove at least the // query string and the fragment. // url example: https://api.github.com/users/getsentry/repos/#fragment?query=query @@ -40,9 +40,9 @@ internal object SentryOkHttpUtils { val sentryRequest = io.sentry.protocol.Request().apply { urlDetails.applyToRequest(this) // Cookie is only sent if isSendDefaultPii is enabled - cookies = if (hub.options.isSendDefaultPii) request.headers["Cookie"] else null + cookies = if (scopes.options.isSendDefaultPii) request.headers["Cookie"] else null method = request.method - headers = getHeaders(hub, request.headers) + headers = getHeaders(scopes, request.headers) request.body?.contentLength().ifHasValidLength { bodySize = it @@ -51,8 +51,8 @@ internal object SentryOkHttpUtils { val sentryResponse = io.sentry.protocol.Response().apply { // Set-Cookie is only sent if isSendDefaultPii is enabled due to PII - cookies = if (hub.options.isSendDefaultPii) response.headers["Set-Cookie"] else null - headers = getHeaders(hub, response.headers) + cookies = if (scopes.options.isSendDefaultPii) response.headers["Set-Cookie"] else null + headers = getHeaders(scopes, response.headers) statusCode = response.code response.body?.contentLength().ifHasValidLength { @@ -63,7 +63,7 @@ internal object SentryOkHttpUtils { event.request = sentryRequest event.contexts.setResponse(sentryResponse) - hub.captureEvent(event, hint) + scopes.captureEvent(event, hint) } private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { @@ -72,9 +72,9 @@ internal object SentryOkHttpUtils { } } - private fun getHeaders(hub: IHub, requestHeaders: Headers): MutableMap? { + private fun getHeaders(scopes: IScopes, requestHeaders: Headers): MutableMap? { // Headers are only sent if isSendDefaultPii is enabled due to PII - if (!hub.options.isSendDefaultPii) { + if (!scopes.options.isSendDefaultPii) { return null } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt index 1d90da70fe..3a90a4c05c 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.BaggageHeader -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTraceHeader import io.sentry.SentryTracer @@ -36,7 +36,7 @@ import kotlin.test.assertTrue class SentryOkHttpEventListenerTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val mockEventListener = mock() val mockEventListenerFactory = mock() @@ -63,12 +63,12 @@ class SentryOkHttpEventListenerTest { isSendDefaultPii = sendDefaultPii configureOptions(this) } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -80,12 +80,12 @@ class SentryOkHttpEventListenerTest { val builder = OkHttpClient.Builder() if (useInterceptor) { - builder.addInterceptor(SentryOkHttpInterceptor(hub)) + builder.addInterceptor(SentryOkHttpInterceptor(scopes)) } sentryOkHttpEventListener = when { - eventListenerFactory != null -> SentryOkHttpEventListener(hub, eventListenerFactory) - eventListener != null -> SentryOkHttpEventListener(hub, eventListener) - else -> SentryOkHttpEventListener(hub) + eventListenerFactory != null -> SentryOkHttpEventListener(scopes, eventListenerFactory) + eventListener != null -> SentryOkHttpEventListener(scopes, eventListener) + else -> SentryOkHttpEventListener(scopes) } return builder.eventListener(sentryOkHttpEventListener).build() } @@ -276,7 +276,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -288,7 +288,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener factory passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListenerFactory = { originalListener }) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -300,7 +300,7 @@ class SentryOkHttpEventListenerTest { @Test fun `does not duplicated spans if an SentryOkHttpEventListener is passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val request = postRequest(body = "requestBody") val call = sut.newCall(request) @@ -363,8 +363,8 @@ class SentryOkHttpEventListenerTest { @Test fun `responseHeadersEnd schedules event finish`() { - val listener = SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener) - whenever(fixture.hub.options).thenReturn(SentryOptions()) + val listener = SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener) + whenever(fixture.scopes.options).thenReturn(SentryOptions()) val call = mock() whenever(call.request()).thenReturn(getRequest()) val okHttpEvent = mock() diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt index 337f722848..4d9f005143 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.ISpan import io.sentry.SentryDate @@ -50,14 +50,14 @@ import kotlin.test.assertTrue class SentryOkHttpEventTest { private class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val span: ISpan val mockRequest: Request val response: Response init { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -65,8 +65,8 @@ class SentryOkHttpEventTest { span = Span( TransactionContext("name", "op", TracesSamplingDecision(true)), - SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), hub), - hub, + SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), scopes), + scopes, null, SpanOptions() ) @@ -86,7 +86,7 @@ class SentryOkHttpEventTest { } fun getSut(currentSpan: ISpan? = span, requestUrl: String ? = null): SentryOkHttpEvent { - whenever(hub.span).thenReturn(currentSpan) + whenever(scopes.span).thenReturn(currentSpan) val request = if (requestUrl == null) { mockRequest } else { @@ -96,7 +96,7 @@ class SentryOkHttpEventTest { .url(server.url(requestUrl)) .build() } - return SentryOkHttpEvent(hub, request) + return SentryOkHttpEvent(scopes, request) } } @@ -126,7 +126,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut(currentSpan = null) assertNull(sut.callRootSpan) sut.finishEvent() - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test @@ -240,7 +240,7 @@ class SentryOkHttpEventTest { fun `when finishEvent, a breadcrumb is captured with request in the hint`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.mockRequest.url.toString(), it.data["url"]) assertEquals(fixture.mockRequest.url.host, it.data["host"]) @@ -258,7 +258,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut() sut.finishEvent() sut.finishEvent() - verify(fixture.hub, times(1)).addBreadcrumb(any(), any()) + verify(fixture.scopes, times(1)).addBreadcrumb(any(), any()) } @Test @@ -283,7 +283,7 @@ class SentryOkHttpEventTest { assertEquals(fixture.response.code, sut.callRootSpan?.getData(SpanDataConvention.HTTP_STATUS_CODE_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.response.protocol.name, it.data["protocol"]) assertEquals(fixture.response.code, it.data["status_code"]) @@ -300,7 +300,7 @@ class SentryOkHttpEventTest { sut.setProtocol("protocol") assertEquals("protocol", sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("protocol", it.data["protocol"]) }, @@ -314,7 +314,7 @@ class SentryOkHttpEventTest { sut.setProtocol(null) assertNull(sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["protocol"]) }, @@ -328,7 +328,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(10) assertEquals(10L, sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["request_content_length"]) }, @@ -342,7 +342,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(-1) assertNull(sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["request_content_length"]) }, @@ -356,7 +356,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(10) assertEquals(10L, sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["response_content_length"]) }, @@ -370,7 +370,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(-1) assertNull(sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["response_content_length"]) }, @@ -384,7 +384,7 @@ class SentryOkHttpEventTest { sut.setError("errorMessage") assertEquals("errorMessage", sut.callRootSpan?.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("errorMessage", it.data["error_message"]) }, @@ -399,7 +399,7 @@ class SentryOkHttpEventTest { assertNotNull(sut.callRootSpan) assertNull(sut.callRootSpan.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["error_message"]) }, @@ -532,7 +532,7 @@ class SentryOkHttpEventTest { @Test fun `scheduleFinish schedules finishEvent and finish running spans to specific timestamp`() { - fixture.hub.options.executorService = ImmediateExecutorService() + fixture.scopes.options.executorService = ImmediateExecutorService() val sut = spy(fixture.getSut()) val timestamp = mock() sut.startSpan(CONNECTION_EVENT) @@ -554,7 +554,7 @@ class SentryOkHttpEventTest { fun `scheduleFinish does not throw if executor is shut down`() { val executorService = mock() whenever(executorService.schedule(any(), any())).thenThrow(RejectedExecutionException()) - whenever(fixture.hub.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) + whenever(fixture.scopes.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) val sut = fixture.getSut() sut.scheduleFinish(mock()) } @@ -565,10 +565,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNotNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -586,10 +586,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -605,7 +605,7 @@ class SentryOkHttpEventTest { fun `when setClientErrorResponse is not called, no client error is captured`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } /** Retrieve all the spans started in the event using reflection. */ diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt index fce16d9220..f40b2c4cb5 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt @@ -6,8 +6,8 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -47,7 +47,7 @@ import kotlin.test.fail class SentryOkHttpInterceptorTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -82,13 +82,13 @@ class SentryOkHttpInterceptorTest { isSendDefaultPii = sendDefaultPii } scope = Scope(options) - whenever(hub.options).thenReturn(options) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -100,14 +100,14 @@ class SentryOkHttpInterceptorTest { val interceptor = when (captureFailedRequests) { null -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, failedRequestTargets = failedRequestTargets, failedRequestStatusCodes = failedRequestStatusCodes ) else -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, @@ -281,7 +281,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls succeeds`() { val sut = fixture.getSut(responseBody = "response body") sut.newCall(postRequest()).execute() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(13L, it.data[SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY]) @@ -296,7 +296,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls results in exception`() { // to setup mocks fixture.getSut() - val interceptor = SentryOkHttpInterceptor(fixture.hub) + val interceptor = SentryOkHttpInterceptor(fixture.scopes) val chain = mock() whenever(chain.call()).thenReturn(mock()) whenever(chain.proceed(any())).thenThrow(IOException()) @@ -308,7 +308,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) }, @@ -385,7 +385,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -396,7 +396,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -406,7 +406,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -417,7 +417,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -429,7 +429,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -440,7 +440,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { assertNotNull(it.get(TypeCheckHint.OKHTTP_REQUEST)) @@ -462,7 +462,7 @@ class SentryOkHttpInterceptorTest { val request = getRequest(url = "/hello?myQuery=myValue#myFragment") val response = sut.newCall(request).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("http://localhost:${fixture.server.port}/hello", sentryRequest.url) @@ -503,7 +503,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(postRequest(body = body)).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals(body.contentLength(), sentryRequest.bodySize) @@ -522,7 +522,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("myValue", sentryRequest.headers!!["myHeader"]) @@ -540,7 +540,7 @@ class SentryOkHttpInterceptorTest { // to setup mocks fixture.getSut() val interceptor = SentryOkHttpInterceptor( - fixture.hub, + fixture.scopes, captureFailedRequests = true ) val chain = mock() @@ -554,7 +554,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -565,7 +565,7 @@ class SentryOkHttpInterceptorTest { call.execute() val httpClientSpan = fixture.sentryTracer.children.firstOrNull() assertNull(httpClientSpan) - verify(fixture.hub, never()).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, never()).addBreadcrumb(any(), anyOrNull()) } @Test @@ -573,7 +573,7 @@ class SentryOkHttpInterceptorTest { val sut = fixture.getSut(captureFailedRequests = true, httpStatusCode = 500) val call = sut.newCall(getRequest()) call.execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -582,6 +582,6 @@ class SentryOkHttpInterceptorTest { val call = sut.newCall(getRequest()) SentryOkHttpEventListener.eventMap[call] = mock() call.execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt index ec19454327..c7194e5994 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.TransactionContext @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class SentryOkHttpUtilsTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() fun getSut( @@ -43,11 +43,11 @@ class SentryOkHttpUtilsTest { setTracePropagationTargets(listOf(server.hostName)) isSendDefaultPii = sendDefaultPii } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) server.enqueue( MockResponse() @@ -78,8 +78,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -103,8 +103,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -127,8 +127,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response From 82792768eaff84e41c66b238e654d482cdd071bb Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:33:01 +0200 Subject: [PATCH 09/57] Replace `IHub` with `IScopes` in graphql integration --- sentry-graphql/api/sentry-graphql.api | 20 +++++---- .../io/sentry/graphql/ExceptionReporter.java | 43 +++++++++++-------- .../graphql/NoOpSubscriptionHandler.java | 4 +- .../SentryDataFetcherExceptionHandler.java | 14 +++--- ...tryGenericDataFetcherExceptionHandler.java | 4 +- .../sentry/graphql/SentryInstrumentation.java | 38 ++++++++-------- .../graphql/SentrySubscriptionHandler.java | 4 +- .../sentry/graphql/ExceptionReporterTest.kt | 32 +++++++------- .../SentryDataFetcherExceptionHandlerTest.kt | 8 ++-- ...yGenericDataFetcherExceptionHandlerTest.kt | 6 +-- .../SentryInstrumentationAnotherTest.kt | 40 +++++++++-------- .../graphql/SentryInstrumentationTest.kt | 36 ++++++++-------- 12 files changed, 132 insertions(+), 117 deletions(-) diff --git a/sentry-graphql/api/sentry-graphql.api b/sentry-graphql/api/sentry-graphql.api index 57c253e23c..d119256010 100644 --- a/sentry-graphql/api/sentry-graphql.api +++ b/sentry-graphql/api/sentry-graphql.api @@ -9,10 +9,11 @@ public final class io/sentry/graphql/ExceptionReporter { } public final class io/sentry/graphql/ExceptionReporter$ExceptionDetails { - public fun (Lio/sentry/IHub;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V - public fun (Lio/sentry/IHub;Lgraphql/schema/DataFetchingEnvironment;Z)V - public fun getHub ()Lio/sentry/IHub; + public fun (Lio/sentry/IScopes;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V + public fun (Lio/sentry/IScopes;Lgraphql/schema/DataFetchingEnvironment;Z)V + public fun getHub ()Lio/sentry/IScopes; public fun getQuery ()Ljava/lang/String; + public fun getScopes ()Lio/sentry/IScopes; public fun getVariables ()Ljava/util/Map; public fun isSubscription ()Z } @@ -26,19 +27,19 @@ public final class io/sentry/graphql/GraphqlStringUtils { public final class io/sentry/graphql/NoOpSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public static fun getInstance ()Lio/sentry/graphql/NoOpSubscriptionHandler; - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/graphql/SentryDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler { public fun (Lgraphql/execution/DataFetcherExceptionHandler;)V - public fun (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V + public fun (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture; public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult; } public final class io/sentry/graphql/SentryGenericDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler { public fun (Lgraphql/execution/DataFetcherExceptionHandler;)V - public fun (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V + public fun (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture; public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult; } @@ -51,9 +52,10 @@ public final class io/sentry/graphql/SentryGraphqlExceptionHandler { public final class io/sentry/graphql/SentryInstrumentation : graphql/execution/instrumentation/SimpleInstrumentation { public static final field SENTRY_EXCEPTIONS_CONTEXT_KEY Ljava/lang/String; public static final field SENTRY_HUB_CONTEXT_KEY Ljava/lang/String; + public static final field SENTRY_SCOPES_CONTEXT_KEY Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Lio/sentry/graphql/ExceptionReporter;Ljava/util/List;)V public fun (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Z)V @@ -71,6 +73,6 @@ public abstract interface class io/sentry/graphql/SentryInstrumentation$BeforeSp } public abstract interface class io/sentry/graphql/SentrySubscriptionHandler { - public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java b/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java index 30ccb21425..843ca77494 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java @@ -5,7 +5,7 @@ import graphql.language.AstPrinter; import graphql.schema.DataFetchingEnvironment; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -33,7 +33,7 @@ public void captureThrowable( final @NotNull Throwable throwable, final @NotNull ExceptionDetails exceptionDetails, final @Nullable ExecutionResult result) { - final @NotNull IHub hub = exceptionDetails.getHub(); + final @NotNull IScopes scopes = exceptionDetails.getScopes(); final @NotNull Mechanism mechanism = new Mechanism(); mechanism.setType(MECHANISM_TYPE); mechanism.setHandled(false); @@ -43,44 +43,44 @@ public void captureThrowable( event.setLevel(SentryLevel.FATAL); final @NotNull Hint hint = new Hint(); - setRequestDetailsOnEvent(hub, exceptionDetails, event); + setRequestDetailsOnEvent(scopes, exceptionDetails, event); - if (result != null && isAllowedToAttachBody(hub)) { + if (result != null && isAllowedToAttachBody(scopes)) { final @NotNull Response response = new Response(); final @NotNull Map responseBody = result.toSpecification(); response.setData(responseBody); event.getContexts().setResponse(response); } - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } - private boolean isAllowedToAttachBody(final @NotNull IHub hub) { - final @NotNull SentryOptions options = hub.getOptions(); + private boolean isAllowedToAttachBody(final @NotNull IScopes scopes) { + final @NotNull SentryOptions options = scopes.getOptions(); return options.isSendDefaultPii() && !SentryOptions.RequestSize.NONE.equals(options.getMaxRequestBodySize()); } private void setRequestDetailsOnEvent( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionDetails exceptionDetails, final @NotNull SentryEvent event) { - hub.configureScope( + scopes.configureScope( (scope) -> { final @Nullable Request scopeRequest = scope.getRequest(); final @NotNull Request request = scopeRequest == null ? new Request() : scopeRequest; - setDetailsOnRequest(hub, exceptionDetails, request); + setDetailsOnRequest(scopes, exceptionDetails, request); event.setRequest(request); }); } private void setDetailsOnRequest( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionDetails exceptionDetails, final @NotNull Request request) { request.setApiTarget("graphql"); - if (isAllowedToAttachBody(hub) + if (isAllowedToAttachBody(scopes) && (exceptionDetails.isSubscription() || captureRequestBodyForNonSubscriptions)) { final @NotNull Map data = new HashMap<>(); @@ -99,27 +99,27 @@ private void setDetailsOnRequest( public static final class ExceptionDetails { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters; private final @Nullable DataFetchingEnvironment dataFetchingEnvironment; private final boolean isSubscription; public ExceptionDetails( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters, final boolean isSubscription) { - this.hub = hub; + this.scopes = scopes; this.instrumentationExecutionParameters = instrumentationExecutionParameters; dataFetchingEnvironment = null; this.isSubscription = isSubscription; } public ExceptionDetails( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable DataFetchingEnvironment dataFetchingEnvironment, final boolean isSubscription) { - this.hub = hub; + this.scopes = scopes; this.dataFetchingEnvironment = dataFetchingEnvironment; instrumentationExecutionParameters = null; this.isSubscription = isSubscription; @@ -149,8 +149,13 @@ public boolean isSubscription() { return isSubscription; } - public @NotNull IHub getHub() { - return hub; + @Deprecated + public @NotNull IScopes getHub() { + return scopes; + } + + public @NotNull IScopes getScopes() { + return scopes; } } } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java index df241ce35b..839f413719 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import org.jetbrains.annotations.NotNull; public final class NoOpSubscriptionHandler implements SentrySubscriptionHandler { @@ -17,7 +17,7 @@ private NoOpSubscriptionHandler() {} @Override public @NotNull Object onSubscriptionResult( @NotNull Object result, - @NotNull IHub hub, + @NotNull IScopes scopes, @NotNull ExceptionReporter exceptionReporter, @NotNull InstrumentationFieldFetchParameters parameters) { return result; diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java index c0467c0089..0813aab851 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java @@ -6,8 +6,8 @@ import graphql.execution.DataFetcherExceptionHandlerParameters; import graphql.execution.DataFetcherExceptionHandlerResult; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.util.Objects; import java.util.concurrent.CompletableFuture; @@ -24,18 +24,18 @@ */ @Deprecated public final class SentryDataFetcherExceptionHandler implements DataFetcherExceptionHandler { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull DataFetcherExceptionHandler delegate; public SentryDataFetcherExceptionHandler( - final @NotNull IHub hub, final @NotNull DataFetcherExceptionHandler delegate) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.delegate = Objects.requireNonNull(delegate, "delegate is required"); SentryIntegrationPackageStorage.getInstance().addIntegration("GrahQLLegacyExceptionHandler"); } public SentryDataFetcherExceptionHandler(final @NotNull DataFetcherExceptionHandler delegate) { - this(HubAdapter.getInstance(), delegate); + this(ScopesAdapter.getInstance(), delegate); } @Override @@ -44,7 +44,7 @@ public CompletableFuture handleException( final Hint hint = new Hint(); hint.set(GRAPHQL_HANDLER_PARAMETERS, handlerParameters); - hub.captureException(handlerParameters.getException(), hint); + scopes.captureException(handlerParameters.getException(), hint); return delegate.handleException(handlerParameters); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java index 6251d00779..1287d38caa 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java @@ -3,7 +3,7 @@ import graphql.execution.DataFetcherExceptionHandler; import graphql.execution.DataFetcherExceptionHandlerParameters; import graphql.execution.DataFetcherExceptionHandlerResult; -import io.sentry.IHub; +import io.sentry.IScopes; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import org.jetbrains.annotations.NotNull; @@ -17,7 +17,7 @@ public final class SentryGenericDataFetcherExceptionHandler implements DataFetch private final @NotNull SentryGraphqlExceptionHandler handler; public SentryGenericDataFetcherExceptionHandler( - final @Nullable IHub hub, final @NotNull DataFetcherExceptionHandler delegate) { + final @Nullable IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) { this.handler = new SentryGraphqlExceptionHandler(delegate); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java index d2d62c99d8..e4f85d12a2 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java @@ -18,9 +18,9 @@ import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLOutputType; import io.sentry.Breadcrumb; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SpanStatus; @@ -46,7 +46,11 @@ public final class SentryInstrumentation "INTERNAL", // Netflix DGS "DataFetchingException" // raw graphql-java ); - public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = "sentry.hub"; + public static final @NotNull String SENTRY_SCOPES_CONTEXT_KEY = "sentry.scopes"; + + @Deprecated + public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = SENTRY_SCOPES_CONTEXT_KEY; + public static final @NotNull String SENTRY_EXCEPTIONS_CONTEXT_KEY = "sentry.exceptions"; private static final String TRACE_ORIGIN = "auto.graphql.graphql"; private final @Nullable BeforeSpanCallback beforeSpan; @@ -70,7 +74,7 @@ public SentryInstrumentation() { */ @Deprecated @SuppressWarnings("InlineMeSuggester") - public SentryInstrumentation(final @Nullable IHub hub) { + public SentryInstrumentation(final @Nullable IScopes scopes) { this(null, NoOpSubscriptionHandler.getInstance(), true); } @@ -89,7 +93,7 @@ public SentryInstrumentation(final @Nullable BeforeSpanCallback beforeSpan) { @Deprecated @SuppressWarnings("InlineMeSuggester") public SentryInstrumentation( - final @Nullable IHub hub, final @Nullable BeforeSpanCallback beforeSpan) { + final @Nullable IScopes scopes, final @Nullable BeforeSpanCallback beforeSpan) { this(beforeSpan, NoOpSubscriptionHandler.getInstance(), true); } @@ -172,9 +176,9 @@ public SentryInstrumentation( public @NotNull InstrumentationContext beginExecution( final @NotNull InstrumentationExecutionParameters parameters) { final TracingState tracingState = parameters.getInstrumentationState(); - final @NotNull IHub currentHub = Sentry.getCurrentHub(); - tracingState.setTransaction(currentHub.getSpan()); - parameters.getGraphQLContext().put(SENTRY_HUB_CONTEXT_KEY, currentHub); + final @NotNull IScopes currentScopes = Sentry.getCurrentScopes(); + tracingState.setTransaction(currentScopes.getSpan()); + parameters.getGraphQLContext().put(SENTRY_SCOPES_CONTEXT_KEY, currentScopes); return super.beginExecution(parameters); } @@ -195,7 +199,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( throwable, new ExceptionReporter.ExceptionDetails( - hubFromContext(graphQLContext), parameters, false), + scopesFromContext(graphQLContext), parameters, false), result); } } @@ -207,7 +211,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( new RuntimeException(error.getMessage()), new ExceptionReporter.ExceptionDetails( - hubFromContext(graphQLContext), parameters, false), + scopesFromContext(graphQLContext), parameters, false), result); } } @@ -217,7 +221,7 @@ public CompletableFuture instrumentExecutionResult( exceptionReporter.captureThrowable( exception, new ExceptionReporter.ExceptionDetails( - hubFromContext(parameters.getGraphQLContext()), parameters, false), + scopesFromContext(parameters.getGraphQLContext()), parameters, false), null); } }); @@ -262,7 +266,7 @@ private boolean isIgnored(final @Nullable String errorType) { operationDefinition.getOperation(); final @Nullable String operationType = operation == null ? null : operation.name().toLowerCase(Locale.ROOT); - hubFromContext(parameters.getExecutionContext().getGraphQLContext()) + scopesFromContext(parameters.getExecutionContext().getGraphQLContext()) .addBreadcrumb( Breadcrumb.graphqlOperation( operationDefinition.getName(), @@ -273,11 +277,11 @@ private boolean isIgnored(final @Nullable String errorType) { return super.beginExecuteOperation(parameters); } - private @NotNull IHub hubFromContext(final @Nullable GraphQLContext context) { + private @NotNull IScopes scopesFromContext(final @Nullable GraphQLContext context) { if (context == null) { - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } - return context.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return context.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } @Override @@ -293,7 +297,7 @@ private boolean isIgnored(final @Nullable String errorType) { return environment -> { final @Nullable ExecutionStepInfo executionStepInfo = environment.getExecutionStepInfo(); if (executionStepInfo != null) { - hubFromContext(parameters.getExecutionContext().getGraphQLContext()) + scopesFromContext(parameters.getExecutionContext().getGraphQLContext()) .addBreadcrumb( Breadcrumb.graphqlDataFetcher( StringUtils.toString(executionStepInfo.getPath()), @@ -351,7 +355,7 @@ private boolean isIgnored(final @Nullable String errorType) { environment.getOperationDefinition().getOperation())) { return subscriptionHandler.onSubscriptionResult( tmpResult, - hubFromContext(environment.getGraphQlContext()), + scopesFromContext(environment.getGraphQlContext()), exceptionReporter, parameters); } diff --git a/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java b/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java index bfc962b501..0a5538ce22 100644 --- a/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java +++ b/sentry-graphql/src/main/java/io/sentry/graphql/SentrySubscriptionHandler.java @@ -1,14 +1,14 @@ package io.sentry.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import org.jetbrains.annotations.NotNull; public interface SentrySubscriptionHandler { @NotNull Object onSubscriptionResult( @NotNull Object result, - @NotNull IHub hub, + @NotNull IScopes scopes, @NotNull ExceptionReporter exceptionReporter, @NotNull InstrumentationFieldFetchParameters parameters); } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt index a2b2b0f101..3a798a2f86 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/ExceptionReporterTest.kt @@ -12,8 +12,8 @@ import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLScalarType import graphql.schema.GraphQLSchema import io.sentry.Hint -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,7 +39,7 @@ class ExceptionReporterTest { it.maxRequestBodySize = SentryOptions.RequestSize.ALWAYS } val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() lateinit var instrumentationExecutionParameters: InstrumentationExecutionParameters lateinit var executionResult: ExecutionResult lateinit var scope: IScope @@ -47,7 +47,7 @@ class ExceptionReporterTest { val variables = mapOf("variableA" to "value a") fun getSut(options: SentryOptions = defaultOptions, captureRequestBodyForNonSubscriptions: Boolean = true): ExceptionReporter { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) scope = Scope(options) val exceptionReporter = ExceptionReporter(captureRequestBodyForNonSubscriptions) executionResult = ExecutionResultImpl.newExecutionResult() @@ -77,7 +77,7 @@ class ExceptionReporterTest { ).build() val instrumentationState = SentryInstrumentation.TracingState() instrumentationExecutionParameters = InstrumentationExecutionParameters(executionInput, schema, instrumentationState) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) return exceptionReporter } @@ -88,9 +88,9 @@ class ExceptionReporterTest { @Test fun `captures throwable`() { val exceptionReporter = fixture.getSut() - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -112,9 +112,9 @@ class ExceptionReporterTest { val exceptionReporter = fixture.getSut() val headers = mapOf("some-header" to "some-header-value") fixture.scope.request = Request().also { it.headers = headers } - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -136,9 +136,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if spring`() { val exceptionReporter = fixture.getSut(captureRequestBodyForNonSubscriptions = false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -156,9 +156,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if no max body size is set`() { val exceptionReporter = fixture.getSut(SentryOptions().also { it.isSendDefaultPii = true }, false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -176,9 +176,9 @@ class ExceptionReporterTest { @Test fun `does not attach query or variables if sendDefaultPii is false`() { val exceptionReporter = fixture.getSut(SentryOptions().also { it.maxRequestBodySize = SentryOptions.RequestSize.ALWAYS }, false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, false), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, false), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) @@ -196,9 +196,9 @@ class ExceptionReporterTest { @Test fun `attaches query and variables if spring and subscription`() { val exceptionReporter = fixture.getSut(captureRequestBodyForNonSubscriptions = false) - exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.hub, fixture.instrumentationExecutionParameters, true), fixture.executionResult) + exceptionReporter.captureThrowable(fixture.exception, ExceptionReporter.ExceptionDetails(fixture.scopes, fixture.instrumentationExecutionParameters, true), fixture.executionResult) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( org.mockito.kotlin.check { val ex = it.throwableMechanism as ExceptionMechanismException assertFalse(ex.exceptionMechanism.isHandled!!) diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt index b571fa8218..de51abac21 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryDataFetcherExceptionHandlerTest.kt @@ -3,7 +3,7 @@ package io.sentry.graphql import graphql.execution.DataFetcherExceptionHandler import graphql.execution.DataFetcherExceptionHandlerParameters import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq import org.mockito.kotlin.mock @@ -14,15 +14,15 @@ class SentryDataFetcherExceptionHandlerTest { @Test fun `passes exception to Sentry and invokes delegate`() { - val hub = mock() + val scopes = mock() val delegate = mock() - val handler = SentryDataFetcherExceptionHandler(hub, delegate) + val handler = SentryDataFetcherExceptionHandler(scopes, delegate) val exception = RuntimeException() val parameters = DataFetcherExceptionHandlerParameters.newExceptionParameters().exception(exception).build() handler.onException(parameters) - verify(hub).captureException(eq(exception), anyOrNull()) + verify(scopes).captureException(eq(exception), anyOrNull()) verify(delegate).handleException(parameters) } } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt index 6d643baf01..88e2f5df55 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryGenericDataFetcherExceptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.GraphQLContext import graphql.execution.DataFetcherExceptionHandler import graphql.execution.DataFetcherExceptionHandlerParameters import graphql.schema.DataFetchingEnvironmentImpl -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import kotlin.test.Test @@ -15,10 +15,10 @@ class SentryGenericDataFetcherExceptionHandlerTest { @Test fun `collects exception into GraphQLContext and invokes delegate`() { - val hub = mock() + val scopes = mock() val delegate = mock() val handler = SentryGenericDataFetcherExceptionHandler( - hub, + scopes, delegate ) diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt index e30bbc9415..087a1dfa72 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt @@ -28,7 +28,8 @@ import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLScalarType import graphql.schema.GraphQLSchema import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -52,7 +53,7 @@ import kotlin.test.assertSame class SentryInstrumentationAnotherTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var activeSpan: SentryTracer lateinit var dataFetcher: DataFetcher lateinit var fieldFetchParameters: InstrumentationFieldFetchParameters @@ -70,17 +71,17 @@ class SentryInstrumentationAnotherTest { val variables = mapOf("variableA" to "value a") fun getSut(isTransactionActive: Boolean = true, operation: OperationDefinition.Operation = OperationDefinition.Operation.QUERY, graphQLContextParam: Map? = null, addTransactionToTracingState: Boolean = true, ignoredErrors: List = emptyList()): SentryInstrumentation { - whenever(hub.options).thenReturn(SentryOptions()) - activeSpan = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(SentryOptions()) + activeSpan = SentryTracer(TransactionContext("name", "op"), scopes) if (isTransactionActive) { - whenever(hub.span).thenReturn(activeSpan) + whenever(scopes.span).thenReturn(activeSpan) } else { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) } val defaultGraphQLContext = mapOf( - SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY to hub + SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY to scopes ) val mergedField = MergedField.newMergedField().addField(Field.newField("myFieldName").build()).build() @@ -165,7 +166,7 @@ class SentryInstrumentationAnotherTest { val result = instrumentedDataFetcher.get(fixture.environment) assertEquals("result modified by subscription handler", result) - verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.hub), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) + verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.scopes), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) } @Test @@ -175,7 +176,7 @@ class SentryInstrumentationAnotherTest { val result = instrumentedDataFetcher.get(fixture.environment) assertEquals("result modified by subscription handler", result) - verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.hub), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) + verify(fixture.subscriptionHandler).onSubscriptionResult(eq("raw result"), same(fixture.scopes), same(fixture.exceptionReporter), same(fixture.fieldFetchParameters)) } @Test @@ -222,7 +223,7 @@ class SentryInstrumentationAnotherTest { fun `adds a breadcrumb for operation`() { val instrumentation = fixture.getSut() instrumentation.beginExecuteOperation(fixture.instrumentationExecuteOperationParameters) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( org.mockito.kotlin.check { breadcrumb -> assertEquals("graphql", breadcrumb.type) assertEquals("query", breadcrumb.category) @@ -237,7 +238,7 @@ class SentryInstrumentationAnotherTest { fun `adds a breadcrumb for data fetcher`() { val instrumentation = fixture.getSut() instrumentation.instrumentDataFetcher(fixture.dataFetcher, fixture.fieldFetchParameters).get(fixture.environment) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( org.mockito.kotlin.check { breadcrumb -> assertEquals("graphql", breadcrumb.type) assertEquals("graphql.fetcher", breadcrumb.category) @@ -250,11 +251,11 @@ class SentryInstrumentationAnotherTest { } @Test - fun `stores hub in context and adds transaction to state`() { + fun `stores scopes in context and adds transaction to state`() { val instrumentation = fixture.getSut(isTransactionActive = true, operation = OperationDefinition.Operation.MUTATION, graphQLContextParam = emptyMap(), addTransactionToTracingState = false) - withMockHub { + withMockScopes { instrumentation.beginExecution(fixture.instrumentationExecutionParameters) - assertSame(fixture.hub, fixture.instrumentationExecutionParameters.graphQLContext.get(SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY)) + assertSame(fixture.scopes, fixture.instrumentationExecutionParameters.graphQLContext.get(SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY)) assertNotNull(fixture.instrumentationState.transaction) } } @@ -276,7 +277,7 @@ class SentryInstrumentationAnotherTest { assertEquals("exception message", it.message) }, org.mockito.kotlin.check { - assertSame(fixture.hub, it.hub) + assertSame(fixture.scopes, it.scopes) assertSame(fixture.query, it.query) assertEquals(false, it.isSubscription) assertEquals(fixture.variables, it.variables) @@ -293,7 +294,7 @@ class SentryInstrumentationAnotherTest { val instrumentation = fixture.getSut( graphQLContextParam = mapOf( SENTRY_EXCEPTIONS_CONTEXT_KEY to listOf(exception), - SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY to fixture.hub + SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY to fixture.scopes ) ) val executionResult = ExecutionResultImpl.newExecutionResult() @@ -305,7 +306,7 @@ class SentryInstrumentationAnotherTest { assertSame(exception, it) }, org.mockito.kotlin.check { - assertSame(fixture.hub, it.hub) + assertSame(fixture.scopes, it.scopes) assertSame(fixture.query, it.query) assertEquals(false, it.isSubscription) assertEquals(fixture.variables, it.variables) @@ -356,8 +357,9 @@ class SentryInstrumentationAnotherTest { assertSame(executionResult, result) } - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt index 8a579e2687..2bb46f79fc 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt @@ -18,7 +18,8 @@ import graphql.schema.GraphQLScalarType import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaGenerator import graphql.schema.idl.SchemaParser -import io.sentry.IHub +import io.sentry.HubScopesWrapper +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -39,12 +40,12 @@ import kotlin.test.assertTrue class SentryInstrumentationTest { class Fixture { - val hub = mock() + val scopes = mock() lateinit var activeSpan: SentryTracer fun getSut(isTransactionActive: Boolean = true, dataFetcherThrows: Boolean = false, beforeSpan: SentryInstrumentation.BeforeSpanCallback? = null): GraphQL { - whenever(hub.options).thenReturn(SentryOptions()) - activeSpan = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(SentryOptions()) + activeSpan = SentryTracer(TransactionContext("name", "op"), scopes) val schema = """ type Query { shows: [Show] @@ -61,9 +62,9 @@ class SentryInstrumentationTest { .build() if (isTransactionActive) { - whenever(hub.span).thenReturn(activeSpan) + whenever(scopes.span).thenReturn(activeSpan) } else { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) } return graphQL @@ -87,7 +88,7 @@ class SentryInstrumentationTest { fun `when transaction is active, creates inner spans`() { val sut = fixture.getSut() - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -105,7 +106,7 @@ class SentryInstrumentationTest { fun `when transaction is active, and data fetcher throws, creates inner spans`() { val sut = fixture.getSut(dataFetcherThrows = true) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isNotEmpty()) @@ -122,7 +123,7 @@ class SentryInstrumentationTest { fun `when transaction is not active, does not create spans`() { val sut = fixture.getSut(isTransactionActive = false) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -134,7 +135,7 @@ class SentryInstrumentationTest { fun `beforeSpan can drop spans`() { val sut = fixture.getSut(beforeSpan = SentryInstrumentation.BeforeSpanCallback { _, _, _ -> null }) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -152,7 +153,7 @@ class SentryInstrumentationTest { fun `beforeSpan can modify spans`() { val sut = fixture.getSut(beforeSpan = SentryInstrumentation.BeforeSpanCallback { span, _, _ -> span.apply { description = "changed" } }) - withMockHub { + withMockScopes { val result = sut.execute("{ shows { id } }") assertTrue(result.errors.isEmpty()) @@ -208,19 +209,20 @@ class SentryInstrumentationTest { @Test fun `Integration adds itself to integration and package list`() { - withMockHub { + withMockScopes { val sut = fixture.getSut() - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("GraphQL")) + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("GraphQL")) val packageInfo = - fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-graphql" } + fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-graphql" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } } - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } From 9bfc0863bc61b6d4107b0ffa6801302fb92b6e26 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:36:44 +0200 Subject: [PATCH 10/57] Replace `IHub` with `IScopes` in logging integrations --- .../java/io/sentry/jul/SentryHandler.java | 6 +++--- sentry-log4j2/api/sentry-log4j2.api | 2 +- .../java/io/sentry/log4j2/SentryAppender.java | 20 +++++++++---------- .../io/sentry/log4j2/SentryAppenderTest.kt | 6 +++--- .../io/sentry/logback/SentryAppender.java | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java b/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java index 2ca775572b..c52b4706f2 100644 --- a/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java +++ b/sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java @@ -6,7 +6,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -210,9 +210,9 @@ SentryEvent createEvent(final @NotNull LogRecord record) { mdcProperties = CollectionUtils.filterMapEntries(mdcProperties, entry -> entry.getValue() != null); if (!mdcProperties.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = HubAdapter.getInstance().getOptions().getContextTags(); + final List contextTags = ScopesAdapter.getInstance().getOptions().getContextTags(); if (!contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag diff --git a/sentry-log4j2/api/sentry-log4j2.api b/sentry-log4j2/api/sentry-log4j2.api index 76aa3e823e..b7fe8b3273 100644 --- a/sentry-log4j2/api/sentry-log4j2.api +++ b/sentry-log4j2/api/sentry-log4j2.api @@ -5,7 +5,7 @@ public final class io/sentry/log4j2/BuildConfig { public class io/sentry/log4j2/SentryAppender : org/apache/logging/log4j/core/appender/AbstractAppender { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/Boolean;Lio/sentry/ITransportFactory;Lio/sentry/IHub;[Ljava/lang/String;)V + public fun (Ljava/lang/String;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/Boolean;Lio/sentry/ITransportFactory;Lio/sentry/IScopes;[Ljava/lang/String;)V public fun append (Lorg/apache/logging/log4j/core/LogEvent;)V public static fun createAppender (Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/String;Ljava/lang/Boolean;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;)Lio/sentry/log4j2/SentryAppender; protected fun createBreadcrumb (Lorg/apache/logging/log4j/core/LogEvent;)Lio/sentry/Breadcrumb; diff --git a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java index 4cf4ad4a86..4ee07ab7b9 100644 --- a/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java +++ b/sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java @@ -7,9 +7,9 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -50,7 +50,7 @@ public class SentryAppender extends AbstractAppender { private @NotNull Level minimumBreadcrumbLevel = Level.INFO; private @NotNull Level minimumEventLevel = Level.ERROR; private final @Nullable Boolean debug; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable List contextTags; public SentryAppender( @@ -61,7 +61,7 @@ public SentryAppender( final @Nullable Level minimumEventLevel, final @Nullable Boolean debug, final @Nullable ITransportFactory transportFactory, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable String[] contextTags) { super(name, filter, null, true, null); this.dsn = dsn; @@ -73,7 +73,7 @@ public SentryAppender( } this.debug = debug; this.transportFactory = transportFactory; - this.hub = hub; + this.scopes = scopes; this.contextTags = contextTags != null ? Arrays.asList(contextTags) : null; } @@ -110,7 +110,7 @@ public SentryAppender( minimumEventLevel, debug, null, - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), contextTags != null ? contextTags.split(",") : null); } @@ -149,13 +149,13 @@ public void append(final @NotNull LogEvent eventObject) { final Hint hint = new Hint(); hint.set(SENTRY_SYNTHETIC_EXCEPTION, eventObject); - hub.captureEvent(createEvent(eventObject), hint); + scopes.captureEvent(createEvent(eventObject), hint); } if (eventObject.getLevel().isMoreSpecificThan(minimumBreadcrumbLevel)) { final Hint hint = new Hint(); hint.set(LOG4J_LOG_EVENT, eventObject); - hub.addBreadcrumb(createBreadcrumb(eventObject), hint); + scopes.addBreadcrumb(createBreadcrumb(eventObject), hint); } } @@ -199,9 +199,9 @@ public void append(final @NotNull LogEvent eventObject) { CollectionUtils.filterMapEntries( loggingEvent.getContextData().toMap(), entry -> entry.getValue() != null); if (!contextData.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = hub.getOptions().getContextTags(); + final List contextTags = scopes.getOptions().getContextTags(); if (contextTags != null && !contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag diff --git a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt index b6f004232c..3786555a61 100644 --- a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt +++ b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt @@ -1,7 +1,7 @@ package io.sentry.log4j2 -import io.sentry.HubAdapter import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.SentryLevel import io.sentry.checkEvent @@ -49,7 +49,7 @@ class SentryAppenderTest { } loggerContext.start() val config: Configuration = loggerContext.configuration - val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, debug, this.transportFactory, HubAdapter.getInstance(), contextTags?.toTypedArray()) + val appender = SentryAppender("sentry", null, "http://key@localhost/proj", minimumBreadcrumbLevel, minimumEventLevel, debug, this.transportFactory, ScopesAdapter.getInstance(), contextTags?.toTypedArray()) config.addAppender(appender) val ref = AppenderRef.createAppenderRef("sentry", null, null) @@ -445,6 +445,6 @@ class SentryAppenderTest { @Test fun `sets the debug mode`() { fixture.getSut(debug = true) - assertTrue(HubAdapter.getInstance().options.isDebug) + assertTrue(ScopesAdapter.getInstance().options.isDebug) } } diff --git a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java index d0be108149..56db0b4dbc 100644 --- a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java +++ b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java @@ -12,8 +12,8 @@ import io.sentry.Breadcrumb; import io.sentry.DateUtils; import io.sentry.Hint; -import io.sentry.HubAdapter; import io.sentry.ITransportFactory; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryEvent; import io.sentry.SentryIntegrationPackageStorage; @@ -134,9 +134,9 @@ protected void append(@NotNull ILoggingEvent eventObject) { CollectionUtils.filterMapEntries( loggingEvent.getMDCPropertyMap(), entry -> entry.getValue() != null); if (!mdcProperties.isEmpty()) { - // get tags from HubAdapter options to allow getting the correct tags if Sentry has been + // get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been // initialized somewhere else - final List contextTags = HubAdapter.getInstance().getOptions().getContextTags(); + final List contextTags = ScopesAdapter.getInstance().getOptions().getContextTags(); if (!contextTags.isEmpty()) { for (final String contextTag : contextTags) { // if mdc tag is listed in SentryOptions, apply as event tag From b998e50993d6f012f4d8087382c9de6a77eacaa2 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:40:25 +0200 Subject: [PATCH 11/57] Replace `IHub` with `IScopes` in more integrations --- sentry-jdbc/api/sentry-jdbc.api | 2 +- .../sentry/jdbc/SentryJdbcEventListener.java | 14 +++++----- .../jdbc/SentryJdbcEventListenerTest.kt | 16 +++++------ .../api/sentry-kotlin-extensions.api | 8 +++--- .../java/io/sentry/kotlin/SentryContext.kt | 28 +++++++++++-------- .../io/sentry/kotlin/SentryContextTest.kt | 6 ++-- sentry-openfeign/api/sentry-openfeign.api | 4 +-- .../io/sentry/openfeign/SentryCapability.java | 17 +++++------ .../sentry/openfeign/SentryFeignClient.java | 14 +++++----- .../sentry/openfeign/SentryFeignClientTest.kt | 22 +++++++-------- sentry-quartz/api/sentry-quartz.api | 2 +- .../io/sentry/quartz/SentryJobListener.java | 28 ++++++++++--------- .../api/sentry-servlet-jakarta.api | 2 +- .../jakarta/SentryServletRequestListener.java | 20 ++++++------- .../SentryServletRequestListenerTest.kt | 12 ++++---- sentry-servlet/api/sentry-servlet.api | 2 +- .../servlet/SentryServletRequestListener.java | 20 ++++++------- .../SentryServletRequestListenerTest.kt | 12 ++++---- .../api/sentry-test-support.api | 1 + .../main/kotlin/io/sentry/test/Reflection.kt | 11 ++++++-- 20 files changed, 129 insertions(+), 112 deletions(-) diff --git a/sentry-jdbc/api/sentry-jdbc.api b/sentry-jdbc/api/sentry-jdbc.api index cff0f37fd2..700cbb2d69 100644 --- a/sentry-jdbc/api/sentry-jdbc.api +++ b/sentry-jdbc/api/sentry-jdbc.api @@ -16,7 +16,7 @@ public final class io/sentry/jdbc/DatabaseUtils$DatabaseDetails { public class io/sentry/jdbc/SentryJdbcEventListener : com/p6spy/engine/event/SimpleJdbcEventListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun onAfterAnyExecute (Lcom/p6spy/engine/common/StatementInformation;JLjava/sql/SQLException;)V public fun onBeforeAnyExecute (Lcom/p6spy/engine/common/StatementInformation;)V } diff --git a/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java b/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java index 0346d2d0b9..4cb21188e4 100644 --- a/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java +++ b/sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java @@ -6,9 +6,9 @@ import com.jakewharton.nopen.annotation.Open; import com.p6spy.engine.common.StatementInformation; import com.p6spy.engine.event.SimpleJdbcEventListener; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.Span; import io.sentry.SpanStatus; @@ -21,24 +21,24 @@ @Open public class SentryJdbcEventListener extends SimpleJdbcEventListener { private static final String TRACE_ORIGIN = "auto.db.jdbc"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private static final @NotNull ThreadLocal CURRENT_SPAN = new ThreadLocal<>(); private volatile @Nullable DatabaseUtils.DatabaseDetails cachedDatabaseDetails = null; private final @NotNull Object databaseDetailsLock = new Object(); - public SentryJdbcEventListener(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryJdbcEventListener(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); addPackageAndIntegrationInfo(); } public SentryJdbcEventListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void onBeforeAnyExecute(final @NotNull StatementInformation statementInformation) { - final ISpan parent = hub.getSpan(); + final ISpan parent = scopes.getSpan(); if (parent != null && !parent.isNoOp()) { final ISpan span = parent.startChild("db.query", statementInformation.getSql()); CURRENT_SPAN.set(span); diff --git a/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt b/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt index 78c5d4cf12..00ce03de41 100644 --- a/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt +++ b/sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt @@ -2,7 +2,7 @@ package io.sentry.jdbc import com.p6spy.engine.common.StatementInformation import com.p6spy.engine.spy.P6DataSource -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.SpanDataConvention.DB_NAME_KEY @@ -26,7 +26,7 @@ import kotlin.test.assertTrue class SentryJdbcEventListenerTest { class Fixture { - val hub = mock().apply { + val scopes = mock().apply { whenever(options).thenReturn( SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") @@ -37,9 +37,9 @@ class SentryJdbcEventListenerTest { val actualDataSource = JDBCDataSource() fun getSut(withRunningTransaction: Boolean = true, existingRow: Int? = null): DataSource { - tx = SentryTracer(TransactionContext("name", "op"), hub) + tx = SentryTracer(TransactionContext("name", "op"), scopes) if (withRunningTransaction) { - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) } actualDataSource.setURL("jdbc:hsqldb:mem:testdb") @@ -54,7 +54,7 @@ class SentryJdbcEventListenerTest { } } - val sentryQueryExecutionListener = SentryJdbcEventListener(hub) + val sentryQueryExecutionListener = SentryJdbcEventListener(scopes) val p6spyDataSource = P6DataSource(actualDataSource) p6spyDataSource.setJdbcEventListenerFactory { sentryQueryExecutionListener } return p6spyDataSource @@ -131,9 +131,9 @@ class SentryJdbcEventListenerTest { @Test fun `sets SDKVersion Info`() { val sut = fixture.getSut() - assertNotNull(fixture.hub.options.sdkVersion) - assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("JDBC")) - val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" } + assertNotNull(fixture.scopes.options.sdkVersion) + assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("JDBC")) + val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" } assertNotNull(packageInfo) assert(packageInfo.version == BuildConfig.VERSION_NAME) } diff --git a/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api b/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api index d501240a3a..7e3be67279 100644 --- a/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api +++ b/sentry-kotlin-extensions/api/sentry-kotlin-extensions.api @@ -1,16 +1,16 @@ public final class io/sentry/kotlin/SentryContext : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/CopyableThreadContextElement { public fun ()V - public fun (Lio/sentry/IHub;)V - public synthetic fun (Lio/sentry/IHub;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public synthetic fun (Lio/sentry/IScopes;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun copyForChild ()Lkotlinx/coroutines/CopyableThreadContextElement; public fun fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element; public fun mergeForChild (Lkotlin/coroutines/CoroutineContext$Element;)Lkotlin/coroutines/CoroutineContext; public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext; public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext; - public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IHub;)V + public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IScopes;)V public synthetic fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V - public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IHub; + public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IScopes; public synthetic fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object; } diff --git a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt index 3cf22a20da..4c814f2805 100644 --- a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt +++ b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt @@ -1,6 +1,6 @@ package io.sentry.kotlin -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import kotlinx.coroutines.CopyableThreadContextElement import kotlin.coroutines.AbstractCoroutineContextElement @@ -9,26 +9,32 @@ import kotlin.coroutines.CoroutineContext /** * Sentry context element for [CoroutineContext]. */ -public class SentryContext(private val hub: IHub = Sentry.getCurrentHub().clone()) : - CopyableThreadContextElement, AbstractCoroutineContextElement(Key) { +@SuppressWarnings("deprecation") +// TODO fork instead +public class SentryContext(private val scopes: IScopes = Sentry.getCurrentScopes().clone()) : + CopyableThreadContextElement, AbstractCoroutineContextElement(Key) { private companion object Key : CoroutineContext.Key - override fun copyForChild(): CopyableThreadContextElement { - return SentryContext(hub.clone()) + @SuppressWarnings("deprecation") + override fun copyForChild(): CopyableThreadContextElement { + // TODO fork instead + return SentryContext(scopes.clone()) } + @SuppressWarnings("deprecation") override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext { - return overwritingElement[Key] ?: SentryContext(hub.clone()) + // TODO fork instead? + return overwritingElement[Key] ?: SentryContext(scopes.clone()) } - override fun updateThreadContext(context: CoroutineContext): IHub { - val oldState = Sentry.getCurrentHub() - Sentry.setCurrentHub(hub) + override fun updateThreadContext(context: CoroutineContext): IScopes { + val oldState = Sentry.getCurrentScopes() + Sentry.setCurrentScopes(scopes) return oldState } - override fun restoreThreadContext(context: CoroutineContext, oldState: IHub) { - Sentry.setCurrentHub(oldState) + override fun restoreThreadContext(context: CoroutineContext, oldState: IScopes) { + Sentry.setCurrentScopes(oldState) } } diff --git a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt index b54ceabc51..578b610267 100644 --- a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt +++ b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt @@ -119,7 +119,7 @@ class SentryContextTest { val c2 = launch( SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) @@ -145,7 +145,7 @@ class SentryContextTest { @Test fun `mergeForChild returns copy of initial context if Key not present`() { val initialContextElement = SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) @@ -158,7 +158,7 @@ class SentryContextTest { @Test fun `mergeForChild returns passed context`() { val initialContextElement = SentryContext( - Sentry.getCurrentHub().clone().also { + Sentry.getCurrentScopes().clone().also { it.setTag("cloned", "clonedValue") } ) diff --git a/sentry-openfeign/api/sentry-openfeign.api b/sentry-openfeign/api/sentry-openfeign.api index beb15c9e02..4ab65a5ca4 100644 --- a/sentry-openfeign/api/sentry-openfeign.api +++ b/sentry-openfeign/api/sentry-openfeign.api @@ -1,12 +1,12 @@ public final class io/sentry/openfeign/SentryCapability : feign/Capability { public fun ()V - public fun (Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V + public fun (Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun (Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun enrich (Lfeign/Client;)Lfeign/Client; } public final class io/sentry/openfeign/SentryFeignClient : feign/Client { - public fun (Lfeign/Client;Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V + public fun (Lfeign/Client;Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V public fun execute (Lfeign/Request;Lfeign/Request$Options;)Lfeign/Response; } diff --git a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java index b65685c3fd..1ad6b1f274 100644 --- a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java +++ b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java @@ -2,33 +2,34 @@ import feign.Capability; import feign.Client; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** Adds Sentry tracing capability to Feign clients. */ public final class SentryCapability implements Capability { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan; public SentryCapability( - final @NotNull IHub hub, final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { - this.hub = hub; + final @NotNull IScopes scopes, + final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { + this.scopes = scopes; this.beforeSpan = beforeSpan; } public SentryCapability(final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) { - this(HubAdapter.getInstance(), beforeSpan); + this(ScopesAdapter.getInstance(), beforeSpan); } public SentryCapability() { - this(HubAdapter.getInstance(), null); + this(ScopesAdapter.getInstance(), null); } @Override public @NotNull Client enrich(final @NotNull Client client) { - return new SentryFeignClient(client, hub, beforeSpan); + return new SentryFeignClient(client, scopes, beforeSpan); } } diff --git a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java index cb8aa3d9e0..037768c7ad 100644 --- a/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java +++ b/sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java @@ -9,7 +9,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -30,15 +30,15 @@ public final class SentryFeignClient implements Client { private static final String TRACE_ORIGIN = "auto.http.openfeign"; private final @NotNull Client delegate; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @Nullable BeforeSpanCallback beforeSpan; public SentryFeignClient( final @NotNull Client delegate, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @Nullable BeforeSpanCallback beforeSpan) { this.delegate = Objects.requireNonNull(delegate, "delegate is required"); - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.beforeSpan = beforeSpan; } @@ -47,7 +47,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O throws IOException { Response response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { final @NotNull Request modifiedRequest = maybeAddTracingHeaders(request, null); @@ -102,7 +102,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url(), (requestBaggageHeaders != null ? new ArrayList<>(requestBaggageHeaders) : null), span); @@ -139,7 +139,7 @@ private void addBreadcrumb(final @NotNull Request request, final @Nullable Respo hint.set(OPEN_FEIGN_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } static final class RequestWrapper { diff --git a/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt b/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt index 65e56ab02b..959b890d46 100644 --- a/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt +++ b/sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt @@ -7,7 +7,7 @@ import feign.HeaderMap import feign.RequestLine import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentryFeignClientTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val sentryTracer: SentryTracer val sentryOptions = SentryOptions().apply { @@ -46,9 +46,9 @@ class SentryFeignClientTest { val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) } fun getSut( @@ -59,7 +59,7 @@ class SentryFeignClientTest { beforeSpan: SentryFeignClient.BeforeSpanCallback? = null ): MockApi { if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -70,12 +70,12 @@ class SentryFeignClientTest { return if (!networkError) { Feign.builder() - .addCapability(SentryCapability(hub, beforeSpan)) + .addCapability(SentryCapability(scopes, beforeSpan)) } else { val mockClient = mock() whenever(mockClient.execute(any(), any())).thenThrow(RuntimeException::class.java) Feign.builder() - .client(SentryFeignClient(mockClient, hub, beforeSpan)) + .client(SentryFeignClient(mockClient, scopes, beforeSpan)) }.target(MockApi::class.java, server.url("/").toUrl().toString()) } } @@ -201,7 +201,7 @@ class SentryFeignClientTest { fun `adds breadcrumb when http calls succeeds`() { val sut = fixture.getSut(responseBody = "response body") sut.postWithBody("request-body") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(13, it.data["response_body_size"]) @@ -215,7 +215,7 @@ class SentryFeignClientTest { fun `adds breadcrumb when http calls succeeds even though response body is null`() { val sut = fixture.getSut(responseBody = "") sut.postWithBody("request-body") - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(0, it.data["response_body_size"]) @@ -236,7 +236,7 @@ class SentryFeignClientTest { } catch (e: Exception) { // ignore me } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) }, diff --git a/sentry-quartz/api/sentry-quartz.api b/sentry-quartz/api/sentry-quartz.api index ff32280dc5..23fce49e7d 100644 --- a/sentry-quartz/api/sentry-quartz.api +++ b/sentry-quartz/api/sentry-quartz.api @@ -7,7 +7,7 @@ public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener { public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String; public static final field SENTRY_SLUG_KEY Ljava/lang/String; public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun getName ()Ljava/lang/String; public fun jobExecutionVetoed (Lorg/quartz/JobExecutionContext;)V public fun jobToBeExecuted (Lorg/quartz/JobExecutionContext;)V diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index 28a0e51200..f9c22022cc 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -3,8 +3,8 @@ import io.sentry.BuildConfig; import io.sentry.CheckIn; import io.sentry.CheckInStatus; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; @@ -24,14 +24,14 @@ public final class SentryJobListener implements JobListener { public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; public static final String SENTRY_SLUG_KEY = "sentry-slug"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryJobListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryJobListener(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryJobListener(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); SentryIntegrationPackageStorage.getInstance().addIntegration("Quartz"); SentryIntegrationPackageStorage.getInstance() .addPackage("maven:io.sentry:sentry-quartz", BuildConfig.VERSION_NAME); @@ -49,15 +49,16 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { if (maybeSlug == null) { return; } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); final @NotNull String slug = maybeSlug; final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); - final @NotNull SentryId checkInId = hub.captureCheckIn(checkIn); + final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn); context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); context.put(SENTRY_SLUG_KEY, slug); } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobToBeExecuted.", t); } @@ -94,14 +95,15 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo if (slug != null) { final boolean isFailed = jobException != null; final @NotNull CheckInStatus status = isFailed ? CheckInStatus.ERROR : CheckInStatus.OK; - hub.captureCheckIn(new CheckIn(checkInId, slug, status)); + scopes.captureCheckIn(new CheckIn(checkInId, slug, status)); } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); } finally { - hub.popScope(); + scopes.popScope(); } } } diff --git a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api index d0367e5195..a5421e7453 100644 --- a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api +++ b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api @@ -10,7 +10,7 @@ public class io/sentry/servlet/jakarta/SentryServletContainerInitializer : jakar public class io/sentry/servlet/jakarta/SentryServletRequestListener : jakarta/servlet/ServletRequestListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljakarta/servlet/ServletRequestEvent;)V public fun requestInitialized (Ljakarta/servlet/ServletRequestEvent;)V } diff --git a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java index e3811157f8..54775386fd 100644 --- a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java +++ b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java @@ -5,8 +5,8 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.util.Objects; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletRequestEvent; @@ -21,24 +21,24 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - private final IHub hub; + private final IScopes scopes; - public SentryServletRequestListener(@NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryServletRequestListener(@NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public SentryServletRequestListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - hub.popScope(); + scopes.popScope(); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - hub.pushScope(); + scopes.pushScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); if (servletRequest instanceof HttpServletRequest) { @@ -47,10 +47,10 @@ public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) final Hint hint = new Hint(); hint.set(SERVLET_REQUEST, httpRequest); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.http(httpRequest.getRequestURI(), httpRequest.getMethod()), hint); - hub.configureScope( + scopes.configureScope( scope -> { scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(httpRequest)); }); diff --git a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt index b87ea218a2..3be76d1cd2 100644 --- a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt +++ b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.servlet.jakarta import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import jakarta.servlet.ServletRequestEvent import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check @@ -13,9 +13,9 @@ import kotlin.test.assertEquals class SentryServletRequestListenerTest { private class Fixture { - val hub = mock() + val scopes = mock() val listener = - SentryServletRequestListener(hub) + SentryServletRequestListener(scopes) val request = mockRequest( url = "http://localhost:8080/some-uri", method = "POST" @@ -33,14 +33,14 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test fun `adds breadcrumb when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> assertEquals("/some-uri", it.getData("url")) assertEquals("POST", it.getData("method")) @@ -54,6 +54,6 @@ class SentryServletRequestListenerTest { fun `pops scope when request gets destroyed`() { fixture.listener.requestDestroyed(fixture.event) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-servlet/api/sentry-servlet.api b/sentry-servlet/api/sentry-servlet.api index a0a2a1e0d2..fd7aee819b 100644 --- a/sentry-servlet/api/sentry-servlet.api +++ b/sentry-servlet/api/sentry-servlet.api @@ -10,7 +10,7 @@ public class io/sentry/servlet/SentryServletContainerInitializer : javax/servlet public class io/sentry/servlet/SentryServletRequestListener : javax/servlet/ServletRequestListener { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljavax/servlet/ServletRequestEvent;)V public fun requestInitialized (Ljavax/servlet/ServletRequestEvent;)V } diff --git a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java index 9b981676c4..97c37e1133 100644 --- a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java +++ b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java @@ -5,8 +5,8 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.util.Objects; import javax.servlet.ServletRequest; import javax.servlet.ServletRequestEvent; @@ -21,24 +21,24 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - private final IHub hub; + private final IScopes scopes; - public SentryServletRequestListener(@NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryServletRequestListener(@NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public SentryServletRequestListener() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - hub.popScope(); + scopes.popScope(); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - hub.pushScope(); + scopes.pushScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); if (servletRequest instanceof HttpServletRequest) { @@ -47,10 +47,10 @@ public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) final Hint hint = new Hint(); hint.set(SERVLET_REQUEST, httpRequest); - hub.addBreadcrumb( + scopes.addBreadcrumb( Breadcrumb.http(httpRequest.getRequestURI(), httpRequest.getMethod()), hint); - hub.configureScope( + scopes.configureScope( scope -> { scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(httpRequest)); }); diff --git a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt index b94e73f2ef..bfa216f738 100644 --- a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt +++ b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.servlet import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import org.assertj.core.api.Assertions.assertThat import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check @@ -14,8 +14,8 @@ import kotlin.test.Test class SentryServletRequestListenerTest { private class Fixture { - val hub = mock() - val listener = SentryServletRequestListener(hub) + val scopes = mock() + val listener = SentryServletRequestListener(scopes) val request = MockHttpServletRequest() val event = mock() @@ -32,14 +32,14 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test fun `adds breadcrumb when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") assertThat(it.getData("method")).isEqualTo("POST") @@ -53,6 +53,6 @@ class SentryServletRequestListenerTest { fun `pops scope when request gets destroyed`() { fixture.listener.requestDestroyed(fixture.event) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-test-support/api/sentry-test-support.api b/sentry-test-support/api/sentry-test-support.api index ffce23a516..dd1a4b69d3 100644 --- a/sentry-test-support/api/sentry-test-support.api +++ b/sentry-test-support/api/sentry-test-support.api @@ -32,6 +32,7 @@ public final class io/sentry/test/ImmediateExecutorService : io/sentry/ISentryEx } public final class io/sentry/test/ReflectionKt { + public static final fun collectInterfaceHierarchy (Ljava/lang/Class;)Ljava/util/List; public static final fun containsMethod (Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Class;)Z public static final fun containsMethod (Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Z public static final fun getCtor (Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor; diff --git a/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt b/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt index 690dcc8725..19d11676e1 100644 --- a/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt +++ b/sentry-test-support/src/main/kotlin/io/sentry/test/Reflection.kt @@ -12,16 +12,23 @@ inline fun T.callMethod(name: String, parameterTypes: Class<*> val declaredMethod = try { T::class.java.getDeclaredMethod(name, parameterTypes) } catch (e: NoSuchMethodException) { - T::class.java.interfaces.first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, parameterTypes) + collectInterfaceHierarchy(T::class.java).first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, parameterTypes) } return declaredMethod.invoke(this, value) } +fun collectInterfaceHierarchy(clazz: Class<*>): List> { + if (clazz.interfaces.isEmpty()) { + return listOf(clazz) + } + return clazz.interfaces.flatMap { iface -> collectInterfaceHierarchy(iface) }.also { it.toMutableList().add(clazz) } +} + inline fun T.callMethod(name: String, parameterTypes: Array>, vararg value: Any?): Any? { val declaredMethod = try { T::class.java.getDeclaredMethod(name, *parameterTypes) } catch (e: NoSuchMethodException) { - T::class.java.interfaces.first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, *parameterTypes) + collectInterfaceHierarchy(T::class.java).first { it.containsMethod(name, parameterTypes) }.getDeclaredMethod(name, *parameterTypes) } return declaredMethod.invoke(this, *value) } From 739827ab98f30b4b7b06567167e9e84aa2857092 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:43:12 +0200 Subject: [PATCH 12/57] Replace `IHub` with `IScopes` in OTel integration --- .../OpenTelemetryLinkErrorEventProcessor.java | 26 ++++++---- .../opentelemetry/SentryPropagator.java | 24 +++++---- .../opentelemetry/SentrySpanProcessor.java | 50 +++++++++++-------- .../test/kotlin/SentrySpanProcessorTest.kt | 46 ++++++++--------- 4 files changed, 82 insertions(+), 64 deletions(-) diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java index 1e373ece9c..bfc4cd05f1 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java @@ -5,10 +5,10 @@ import io.opentelemetry.api.trace.TraceId; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.Instrumenter; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentrySpanStorage; @@ -20,21 +20,21 @@ public final class OpenTelemetryLinkErrorEventProcessor implements EventProcessor { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); public OpenTelemetryLinkErrorEventProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @TestOnly - OpenTelemetryLinkErrorEventProcessor(final @NotNull IHub hub) { - this.hub = hub; + OpenTelemetryLinkErrorEventProcessor(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override public @Nullable SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) { - final @NotNull Instrumenter instrumenter = hub.getOptions().getInstrumenter(); + final @NotNull Instrumenter instrumenter = scopes.getOptions().getInstrumenter(); if (Instrumenter.OTEL.equals(instrumenter)) { @NotNull final Span otelSpan = Span.current(); @NotNull final String traceId = otelSpan.getSpanContext().getTraceId(); @@ -55,7 +55,8 @@ public OpenTelemetryLinkErrorEventProcessor() { null); event.getContexts().setTrace(spanContext); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -64,7 +65,8 @@ public OpenTelemetryLinkErrorEventProcessor() { spanId, traceId); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -74,7 +76,8 @@ public OpenTelemetryLinkErrorEventProcessor() { traceId); } } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -84,7 +87,8 @@ public OpenTelemetryLinkErrorEventProcessor() { spanId); } } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java index 14ac12323b..ed3e243f4d 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentryPropagator.java @@ -10,9 +10,9 @@ import io.opentelemetry.context.propagation.TextMapSetter; import io.sentry.Baggage; import io.sentry.BaggageHeader; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.SentrySpanStorage; import io.sentry.SentryTraceHeader; @@ -29,14 +29,14 @@ public final class SentryPropagator implements TextMapPropagator { private static final @NotNull List FIELDS = Arrays.asList(SentryTraceHeader.SENTRY_TRACE_HEADER, BaggageHeader.BAGGAGE_HEADER); private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryPropagator() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryPropagator(final @NotNull IHub hub) { - this.hub = hub; + SentryPropagator(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override @@ -49,7 +49,8 @@ public void inject(final Context context, final C carrier, final TextMapSett final @NotNull Span otelSpan = Span.fromContext(context); final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext(); if (!otelSpanContext.isValid()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -58,7 +59,8 @@ public void inject(final Context context, final C carrier, final TextMapSett } final @Nullable ISpan sentrySpan = spanStorage.get(otelSpanContext.getSpanId()); if (sentrySpan == null || sentrySpan.isNoOp()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -106,13 +108,15 @@ public Context extract( Span wrappedSpan = Span.wrap(otelSpanContext); modifiedContext = modifiedContext.with(wrappedSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.DEBUG, "Continuing Sentry trace %s", sentryTraceHeader.getTraceId()); return modifiedContext; } catch (InvalidSentryTraceHeaderException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.ERROR, diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java index a9e70f66a0..6b7797153b 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySpanProcessor.java @@ -13,12 +13,12 @@ import io.opentelemetry.semconv.SemanticAttributes; import io.sentry.Baggage; import io.sentry.DsnUtil; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.Instrumenter; import io.sentry.PropagationContext; +import io.sentry.ScopesAdapter; import io.sentry.SentryDate; import io.sentry.SentryLevel; import io.sentry.SentryLongDate; @@ -46,14 +46,14 @@ public final class SentrySpanProcessor implements SpanProcessor { private final @NotNull SpanDescriptionExtractor spanDescriptionExtractor = new SpanDescriptionExtractor(); private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentrySpanProcessor(final @NotNull IHub hub) { - this.hub = hub; + SentrySpanProcessor(final @NotNull IScopes scopes) { + this.scopes = scopes; } @Override @@ -65,7 +65,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri final @NotNull TraceData traceData = getTraceData(otelSpan, parentContext); if (isSentryRequest(otelSpan)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -78,7 +79,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri traceData.getParentSpanId() == null ? null : spanStorage.get(traceData.getParentSpanId()); if (sentryParentSpan != null) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -94,7 +96,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri sentryChildSpan.getSpanContext().setOrigin(TRACE_ORIGN); spanStorage.store(traceData.getSpanId(), sentryChildSpan); } else { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -123,7 +126,7 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri transactionOptions.setStartTimestamp( new SentryLongDate(otelSpan.toSpanData().getStartEpochNanos())); - ISpan sentryTransaction = hub.startTransaction(transactionContext, transactionOptions); + ISpan sentryTransaction = scopes.startTransaction(transactionContext, transactionOptions); sentryTransaction.getSpanContext().setOrigin(TRACE_ORIGN); spanStorage.store(traceData.getSpanId(), sentryTransaction); } @@ -144,7 +147,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { final @Nullable ISpan sentrySpan = spanStorage.removeAndGet(traceData.getSpanId()); if (sentrySpan == null) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -155,7 +159,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { } if (isSentryRequest(otelSpan)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -168,7 +173,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { if (sentrySpan instanceof ITransaction) { final @NotNull ITransaction sentryTransaction = (ITransaction) sentrySpan; updateTransactionWithOtelData(sentryTransaction, otelSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -178,7 +184,8 @@ public void onEnd(final @NotNull ReadableSpan otelSpan) { traceData.getTraceId()); } else { updateSpanWithOtelData(sentrySpan, otelSpan); - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -201,7 +208,8 @@ public boolean isEndRequired() { private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { if (!hasSentryBeenInitialized()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -209,9 +217,10 @@ private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { return false; } - final @NotNull Instrumenter instrumenter = hub.getOptions().getInstrumenter(); + final @NotNull Instrumenter instrumenter = scopes.getOptions().getInstrumenter(); if (!Instrumenter.OTEL.equals(instrumenter)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -222,7 +231,8 @@ private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) { final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext(); if (!otelSpanContext.isValid()) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.DEBUG, @@ -241,7 +251,7 @@ private boolean isSentryRequest(final @NotNull ReadableSpan otelSpan) { } final @Nullable String httpUrl = otelSpan.getAttribute(SemanticAttributes.HTTP_URL); - return DsnUtil.urlContainsDsnHost(hub.getOptions(), httpUrl); + return DsnUtil.urlContainsDsnHost(scopes.getOptions(), httpUrl); } private @NotNull TraceData getTraceData( @@ -334,7 +344,7 @@ private SpanStatus mapOtelStatus(final @NotNull ReadableSpan otelSpan) { } private boolean hasSentryBeenInitialized() { - return hub.isEnabled(); + return scopes.isEnabled(); } private @NotNull Map toMapWithStringKeys(final @Nullable Attributes attributes) { diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt index 5ed757ba16..50d70f34f5 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt @@ -21,7 +21,7 @@ import io.opentelemetry.semconv.SemanticAttributes import io.sentry.Baggage import io.sentry.BaggageHeader import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.Instrumenter @@ -65,7 +65,7 @@ class SentrySpanProcessorTest { it.dsn = "https://key@sentry.io/proj" it.instrumenter = Instrumenter.OTEL } - val hub = mock() + val scopes = mock() val transaction = mock() val span = mock() val spanContext = mock() @@ -75,9 +75,9 @@ class SentrySpanProcessorTest { val baggage = Baggage.fromHeader(BAGGAGE_HEADER_STRING) fun setup() { - whenever(hub.isEnabled).thenReturn(true) - whenever(hub.options).thenReturn(options) - whenever(hub.startTransaction(any(), any())).thenReturn(transaction) + whenever(scopes.isEnabled).thenReturn(true) + whenever(scopes.options).thenReturn(options) + whenever(scopes.startTransaction(any(), any())).thenReturn(transaction) whenever(spanContext.operation).thenReturn("spanContextOp") whenever(spanContext.parentSpanId).thenReturn(io.sentry.SpanId("cedf5b7571cb4972")) @@ -94,7 +94,7 @@ class SentrySpanProcessorTest { whenever(transaction.startChild(any(), anyOrNull(), anyOrNull(), eq(Instrumenter.OTEL))).thenReturn(span) val sdkTracerProvider = SdkTracerProvider.builder() - .addSpanProcessor(SentrySpanProcessor(hub)) + .addSpanProcessor(SentrySpanProcessor(scopes)) .build() openTelemetry = OpenTelemetrySdk.builder() @@ -146,13 +146,13 @@ class SentrySpanProcessorTest { val context = mock() val span = mock() - whenever(fixture.hub.isEnabled).thenReturn(false) + whenever(fixture.scopes.isEnabled).thenReturn(false) - SentrySpanProcessor(fixture.hub).onStart(context, span) + SentrySpanProcessor(fixture.scopes).onStart(context, span) - verify(fixture.hub).isEnabled - verify(fixture.hub).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).options + verifyNoMoreInteractions(fixture.scopes) verifyNoInteractions(context, span) } @@ -161,13 +161,13 @@ class SentrySpanProcessorTest { fixture.setup() val span = mock() - whenever(fixture.hub.isEnabled).thenReturn(false) + whenever(fixture.scopes.isEnabled).thenReturn(false) - SentrySpanProcessor(fixture.hub).onEnd(span) + SentrySpanProcessor(fixture.scopes).onEnd(span) - verify(fixture.hub).isEnabled - verify(fixture.hub).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).options + verifyNoMoreInteractions(fixture.scopes) verifyNoInteractions(span) } @@ -178,7 +178,7 @@ class SentrySpanProcessorTest { val mockSpanContext = mock() whenever(mockSpanContext.spanId).thenReturn(SpanId.getInvalid()) whenever(mockSpan.spanContext).thenReturn(mockSpanContext) - SentrySpanProcessor(fixture.hub).onStart(Context.current(), mockSpan) + SentrySpanProcessor(fixture.scopes).onStart(Context.current(), mockSpan) thenNoTransactionIsStarted() } @@ -190,7 +190,7 @@ class SentrySpanProcessorTest { whenever(mockSpanContext.spanId).thenReturn(SpanId.fromBytes("seed".toByteArray())) whenever(mockSpanContext.traceId).thenReturn(TraceId.getInvalid()) whenever(mockSpan.spanContext).thenReturn(mockSpanContext) - SentrySpanProcessor(fixture.hub).onStart(Context.current(), mockSpan) + SentrySpanProcessor(fixture.scopes).onStart(Context.current(), mockSpan) thenNoTransactionIsStarted() } @@ -342,7 +342,7 @@ class SentrySpanProcessorTest { thenTransactionIsStarted(otelSpan, isContinued = true) otelSpan.makeCurrent().use { _ -> - val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.hub).process(SentryEvent(), Hint()) + val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.scopes).process(SentryEvent(), Hint()) val traceContext = processedEvent!!.contexts.trace!! assertEquals("2722d9f6ec019ade60c776169d9a8904", traceContext.traceId.toString()) @@ -361,7 +361,7 @@ class SentrySpanProcessorTest { fixture.options.instrumenter = Instrumenter.SENTRY fixture.setup() - val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.hub).process(SentryEvent(), Hint()) + val processedEvent = OpenTelemetryLinkErrorEventProcessor(fixture.scopes).process(SentryEvent(), Hint()) thenNoTraceContextHasBeenAddedToEvent(processedEvent) } @@ -393,7 +393,7 @@ class SentrySpanProcessorTest { private fun thenTransactionIsStarted(otelSpan: Span, isContinued: Boolean = false, continuesWithFilledBaggage: Boolean = true) { if (isContinued) { - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("testspan", it.name) assertEquals(TransactionNameSource.CUSTOM, it.transactionNameSource) @@ -423,7 +423,7 @@ class SentrySpanProcessorTest { } ) } else { - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("testspan", it.name) assertEquals(TransactionNameSource.CUSTOM, it.transactionNameSource) @@ -451,7 +451,7 @@ class SentrySpanProcessorTest { } private fun thenNoTransactionIsStarted() { - verify(fixture.hub, never()).startTransaction( + verify(fixture.scopes, never()).startTransaction( any(), any() ) From 69f2d63dac76b396c44f09e6103685d5a45d59f5 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:51:42 +0200 Subject: [PATCH 13/57] Replace `IHub` with `IScopes` in Spring 5 / Spring Boot 2 integrations --- sentry-spring-boot/api/sentry-spring-boot.api | 4 +- .../spring/boot/SentryAutoConfiguration.java | 39 ++++---- .../SentrySpanRestTemplateCustomizer.java | 6 +- .../boot/SentrySpanWebClientCustomizer.java | 6 +- .../boot/SentryWebfluxAutoConfiguration.java | 15 +-- .../boot/SentryAutoConfigurationTest.kt | 12 +-- .../SentrySpanRestTemplateCustomizerTest.kt | 22 ++--- .../boot/SentrySpanWebClientCustomizerTest.kt | 22 ++--- .../boot/it/SentrySpringIntegrationTest.kt | 6 +- sentry-spring/api/sentry-spring.api | 37 ++++---- .../spring/SentryExceptionResolver.java | 10 +- .../io/sentry/spring/SentryHubRegistrar.java | 4 +- .../spring/SentryInitBeanPostProcessor.java | 16 ++-- .../sentry/spring/SentryRequestResolver.java | 16 ++-- .../io/sentry/spring/SentrySpringFilter.java | 38 ++++---- .../io/sentry/spring/SentryTaskDecorator.java | 14 +-- .../io/sentry/spring/SentryUserFilter.java | 12 +-- .../spring/checkin/SentryCheckInAdvice.java | 28 +++--- ...SentryCaptureExceptionParameterAdvice.java | 14 +-- .../graphql/SentryBatchLoaderRegistry.java | 16 ++-- .../graphql/SentryDgsSubscriptionHandler.java | 6 +- .../SentrySpringSubscriptionHandler.java | 6 +- .../spring/tracing/SentrySpanAdvice.java | 14 +-- ...entrySpanClientHttpRequestInterceptor.java | 14 +-- .../SentrySpanClientWebRequestFilter.java | 14 +-- .../spring/tracing/SentryTracingFilter.java | 31 +++--- .../tracing/SentryTransactionAdvice.java | 20 ++-- .../spring/webflux/SentryRequestResolver.java | 13 +-- .../spring/webflux/SentryScheduleHook.java | 12 ++- .../webflux/SentryWebExceptionHandler.java | 10 +- .../spring/webflux/SentryWebFilter.java | 33 ++++--- .../io/sentry/spring/EnableSentryTest.kt | 6 +- .../sentry/spring/SentryCheckInAdviceTest.kt | 94 +++++++++---------- .../spring/SentryExceptionResolverTest.kt | 28 +++--- .../spring/SentryInitBeanPostProcessorTest.kt | 10 +- ...yRequestHttpServletRequestProcessorTest.kt | 6 +- .../sentry/spring/SentrySpringFilterTest.kt | 20 ++-- .../sentry/spring/SentryTaskDecoratorTest.kt | 16 ++-- .../io/sentry/spring/SentryUserFilterTest.kt | 20 ++-- ...ntryCaptureExceptionParameterAdviceTest.kt | 16 ++-- .../SentrySpringSubscriptionHandlerTest.kt | 14 +-- .../spring/mvc/SentrySpringIntegrationTest.kt | 24 ++--- .../spring/tracing/SentrySpanAdviceTest.kt | 40 ++++---- .../spring/tracing/SentryTracingFilterTest.kt | 52 +++++----- .../tracing/SentryTransactionAdviceTest.kt | 38 ++++---- .../spring/webflux/SentryScheduleHookTest.kt | 14 +-- .../webflux/SentryWebFluxTracingFilterTest.kt | 93 +++++++++--------- .../webflux/SentryWebfluxIntegrationTest.kt | 10 +- 48 files changed, 516 insertions(+), 495 deletions(-) diff --git a/sentry-spring-boot/api/sentry-spring-boot.api b/sentry-spring-boot/api/sentry-spring-boot.api index 32d7cc8c60..d97e2e1111 100644 --- a/sentry-spring-boot/api/sentry-spring-boot.api +++ b/sentry-spring-boot/api/sentry-spring-boot.api @@ -53,8 +53,8 @@ public class io/sentry/spring/boot/SentryProperties$Logging { public class io/sentry/spring/boot/SentryWebfluxAutoConfiguration { public fun ()V public fun sentryScheduleHookApplicationRunner ()Lorg/springframework/boot/ApplicationRunner; - public fun sentryWebExceptionHandler (Lio/sentry/IHub;)Lio/sentry/spring/webflux/SentryWebExceptionHandler; - public fun sentryWebFilter (Lio/sentry/IHub;)Lio/sentry/spring/webflux/SentryWebFilter; + public fun sentryWebExceptionHandler (Lio/sentry/IScopes;)Lio/sentry/spring/webflux/SentryWebExceptionHandler; + public fun sentryWebFilter (Lio/sentry/IScopes;)Lio/sentry/spring/webflux/SentryWebFilter; } public class io/sentry/spring/boot/graphql/SentryGraphqlAutoConfiguration { diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java index edbfee271d..7d6a6a6fc4 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java @@ -3,10 +3,10 @@ import com.jakewharton.nopen.annotation.Open; import graphql.GraphQLError; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -114,7 +114,7 @@ static class HubConfiguration { } @Bean - public @NotNull IHub sentryHub( + public @NotNull IScopes sentryHub( final @NotNull List> optionsConfigurations, final @NotNull SentryProperties options, final @NotNull ObjectProvider gitProperties) { @@ -137,7 +137,7 @@ static class HubConfiguration { // here we make sure that only classes that extend throwable are set on this field options.getIgnoredExceptionsForType().removeIf(it -> !Throwable.class.isAssignableFrom(it)); Sentry.init(options); - return HubAdapter.getInstance(); + return ScopesAdapter.getInstance(); } @Configuration(proxyBeanMethods = false) @@ -237,7 +237,7 @@ static class SentrySecurityConfiguration { * HttpServletRequest#getUserPrincipal()}. If Spring Security is auto-configured, its order is * set to run after Spring Security. * - * @param hub the Sentry hub + * @param scopes the Sentry scopes * @param sentryProperties the Sentry properties * @param sentryUserProvider the user provider * @return {@link SentryUserFilter} registration bean @@ -245,11 +245,11 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnBean(SentryUserProvider.class) public @NotNull FilterRegistrationBean sentryUserFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryProperties sentryProperties, final @NotNull List sentryUserProvider) { final FilterRegistrationBean filter = new FilterRegistrationBean<>(); - filter.setFilter(new SentryUserFilter(hub, sentryUserProvider)); + filter.setFilter(new SentryUserFilter(scopes, sentryUserProvider)); filter.setOrder(resolveUserFilterOrder(sentryProperties)); return filter; } @@ -261,8 +261,8 @@ static class SentrySecurityConfiguration { } @Bean - public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IHub hub) { - return new SentryRequestResolver(hub); + public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IScopes scopes) { + return new SentryRequestResolver(scopes); } @Bean @@ -274,12 +274,12 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentrySpringFilter") public @NotNull FilterRegistrationBean sentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = new FilterRegistrationBean<>( - new SentrySpringFilter(hub, requestResolver, transactionNameProvider)); + new SentrySpringFilter(scopes, requestResolver, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE); return filter; } @@ -287,9 +287,10 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentryTracingFilter") public FilterRegistrationBean sentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = - new FilterRegistrationBean<>(new SentryTracingFilter(hub, transactionNameProvider)); + new FilterRegistrationBean<>(new SentryTracingFilter(scopes, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE + 1); // must run after SentrySpringFilter return filter; } @@ -298,11 +299,11 @@ public FilterRegistrationBean sentryTracingFilter( @ConditionalOnMissingBean @ConditionalOnClass(HandlerExceptionResolver.class) public @NotNull SentryExceptionResolver sentryExceptionResolver( - final @NotNull IHub sentryHub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final @NotNull SentryProperties options) { return new SentryExceptionResolver( - sentryHub, transactionNameProvider, options.getExceptionResolverOrder()); + scopes, transactionNameProvider, options.getExceptionResolverOrder()); } } @@ -348,8 +349,8 @@ static class SentrySpanPointcutAutoConfiguration {} @Open static class SentryPerformanceRestTemplateConfiguration { @Bean - public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hub) { - return new SentrySpanRestTemplateCustomizer(hub); + public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IScopes scopes) { + return new SentrySpanRestTemplateCustomizer(scopes); } } @@ -359,8 +360,8 @@ public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hu @Open static class SentryPerformanceWebClientConfiguration { @Bean - public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IHub hub) { - return new SentrySpanWebClientCustomizer(hub); + public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IScopes scopes) { + return new SentrySpanWebClientCustomizer(scopes); } } diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java index bd311c55f1..2a5e4f1be5 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanRestTemplateCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.ArrayList; import java.util.List; @@ -14,8 +14,8 @@ class SentrySpanRestTemplateCustomizer implements RestTemplateCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestTemplateCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub); + public SentrySpanRestTemplateCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes); } @Override diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java index 0b8aa4055c..79e59f1cf0 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentrySpanWebClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientWebRequestFilter; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanWebClientCustomizer implements WebClientCustomizer { private final @NotNull SentrySpanClientWebRequestFilter filter; - public SentrySpanWebClientCustomizer(final @NotNull IHub hub) { - this.filter = new SentrySpanClientWebRequestFilter(hub); + public SentrySpanWebClientCustomizer(final @NotNull IScopes scopes) { + this.filter = new SentrySpanClientWebRequestFilter(scopes); } @Override diff --git a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java index 3d507f1b6b..e7f6a444b8 100644 --- a/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java +++ b/sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryWebfluxAutoConfiguration.java @@ -1,8 +1,8 @@ package io.sentry.spring.boot; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.spring.webflux.SentryScheduleHook; import io.sentry.spring.webflux.SentryWebExceptionHandler; import io.sentry.spring.webflux.SentryWebFilter; @@ -21,14 +21,14 @@ /** Configures Sentry integration for Spring Webflux and Project Reactor. */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) -@ConditionalOnBean(IHub.class) +@ConditionalOnBean(IScopes.class) @ConditionalOnClass(Schedulers.class) @Open @ApiStatus.Experimental public class SentryWebfluxAutoConfiguration { private static final int SENTRY_SPRING_FILTER_PRECEDENCE = Ordered.HIGHEST_PRECEDENCE; - /** Configures hook that sets correct hub on the executing thread. */ + /** Configures hook that sets correct scopes on the executing thread. */ @Bean public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() { return args -> { @@ -39,13 +39,14 @@ public class SentryWebfluxAutoConfiguration { /** Configures a filter that sets up Sentry {@link IScope} for each request. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) - public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IHub hub) { - return new SentryWebFilter(hub); + public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IScopes scopes) { + return new SentryWebFilter(scopes); } /** Configures exception handler that handles unhandled exceptions and sends them to Sentry. */ @Bean - public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler(final @NotNull IHub hub) { - return new SentryWebExceptionHandler(hub); + public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler( + final @NotNull IScopes scopes) { + return new SentryWebExceptionHandler(scopes); } } diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt index c7fd177ec0..e1fe220aea 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt @@ -5,7 +5,7 @@ import io.sentry.AsyncHttpTransportFactory import io.sentry.Breadcrumb import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.NoOpTransportFactory @@ -76,18 +76,18 @@ class SentryAutoConfigurationTest { .withConfiguration(AutoConfigurations.of(SentryAutoConfiguration::class.java, WebMvcAutoConfiguration::class.java)) @Test - fun `hub is not created when auto-configuration dsn is not set`() { + fun `scopes is not created when auto-configuration dsn is not set`() { contextRunner .run { - assertThat(it).doesNotHaveBean(IHub::class.java) + assertThat(it).doesNotHaveBean(IScopes::class.java) } } @Test - fun `hub is created when dsn is provided`() { + fun `scopes is created when dsn is provided`() { contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj") .run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } @@ -943,7 +943,7 @@ class SentryAutoConfigurationTest { } class CustomIntegration : Integration { - override fun register(hub: IHub, options: SentryOptions) {} + override fun register(scopes: IScopes, options: SentryOptions) {} } @Configuration(proxyBeanMethods = false) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt index 0d675b6841..33d7974d8a 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,23 +37,23 @@ import kotlin.test.assertTrue class SentrySpanRestTemplateCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restTemplate = RestTemplateBuilder() .setConnectTimeout(Duration.ofSeconds(2)) .setReadTimeout(Duration.ofSeconds(2)) .build() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestTemplateCustomizer(hub) + internal val customizer = SentrySpanRestTemplateCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope( + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope( any() ) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, includeMockServerInTracingOrigins: Boolean = true): RestTemplate { @@ -76,7 +76,7 @@ class SentrySpanRestTemplateCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restTemplate @@ -211,7 +211,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = true).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -229,7 +229,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = true, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -242,7 +242,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is not active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = false).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -260,7 +260,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = false, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt index f925435fd3..4f1f70d75e 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentrySpanWebClientCustomizerTest.kt @@ -2,8 +2,8 @@ package io.sentry.spring.boot import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,10 +39,10 @@ class SentrySpanWebClientCustomizerTest { class Fixture { lateinit var sentryOptions: SentryOptions lateinit var scope: IScope - val hub = mock() + val scopes = mock() var mockServer = MockWebServer() lateinit var transaction: SentryTracer - private val customizer = SentrySpanWebClientCustomizer(hub) + private val customizer = SentrySpanWebClientCustomizer(scopes) fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, throwIOException: Boolean = false, includeMockServerInTracingOrigins: Boolean = true): WebClient { sentryOptions = SentryOptions().apply { @@ -54,11 +54,11 @@ class SentrySpanWebClientCustomizerTest { dsn = "http://key@localhost/proj" } scope = Scope(sentryOptions) - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope( + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope( any() ) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) val webClientBuilder = WebClient.builder() customizer.customize(webClientBuilder) val webClient = webClientBuilder.build() @@ -66,7 +66,7 @@ class SentrySpanWebClientCustomizerTest { if (isTransactionActive) { val scope = Scope(sentryOptions) scope.transaction = transaction - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } val dispatcher: Dispatcher = object : Dispatcher() { @@ -238,7 +238,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -261,7 +261,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -281,7 +281,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -304,7 +304,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt index eb6d159a7c..3bbcb2c3e7 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.boot.it -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.checkEvent @@ -60,7 +60,7 @@ class SentrySpringIntegrationTest { lateinit var transport: ITransport @SpyBean - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -188,7 +188,7 @@ class SentrySpringIntegrationTest { restTemplate.getForEntity("http://localhost:$port/throws-handled", String::class.java) - verify(hub, never()).captureEvent(any()) + verify(scopes, never()).captureEvent(any()) } @Test diff --git a/sentry-spring/api/sentry-spring.api b/sentry-spring/api/sentry-spring.api index 9ef6b5bb3a..58de26098f 100644 --- a/sentry-spring/api/sentry-spring.api +++ b/sentry-spring/api/sentry-spring.api @@ -22,7 +22,7 @@ public final class io/sentry/spring/HttpServletRequestSentryUserProvider : io/se public class io/sentry/spring/SentryExceptionResolver : org/springframework/core/Ordered, org/springframework/web/servlet/HandlerExceptionResolver { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;Lio/sentry/spring/tracing/TransactionNameProvider;I)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/tracing/TransactionNameProvider;I)V protected fun createEvent (Ljavax/servlet/http/HttpServletRequest;Ljava/lang/Exception;)Lio/sentry/SentryEvent; protected fun createHint (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lio/sentry/Hint; public fun getOrder ()I @@ -47,14 +47,14 @@ public class io/sentry/spring/SentryRequestHttpServletRequestProcessor : io/sent } public class io/sentry/spring/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Ljavax/servlet/http/HttpServletRequest;)Lio/sentry/protocol/Request; } public class io/sentry/spring/SentrySpringFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/SentryRequestResolver;Lio/sentry/spring/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/SentryRequestResolver;Lio/sentry/spring/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V } @@ -69,7 +69,7 @@ public final class io/sentry/spring/SentryTaskDecorator : org/springframework/co } public class io/sentry/spring/SentryUserFilter : org/springframework/web/filter/OncePerRequestFilter { - public fun (Lio/sentry/IHub;Ljava/util/List;)V + public fun (Lio/sentry/IScopes;Ljava/util/List;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V public fun getSentryUserProviders ()Ljava/util/List; } @@ -96,7 +96,7 @@ public abstract interface annotation class io/sentry/spring/checkin/SentryCheckI public class io/sentry/spring/checkin/SentryCheckInAdvice : org/aopalliance/intercept/MethodInterceptor, org/springframework/context/EmbeddedValueResolverAware { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; public fun setEmbeddedValueResolver (Lorg/springframework/util/StringValueResolver;)V } @@ -127,7 +127,7 @@ public abstract interface annotation class io/sentry/spring/exception/SentryCapt public class io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -169,7 +169,7 @@ public final class io/sentry/spring/graphql/SentryDataFetcherExceptionResolverAd public final class io/sentry/spring/graphql/SentryDgsSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/spring/graphql/SentryGraphqlBeanPostProcessor : org/springframework/beans/factory/config/BeanPostProcessor, org/springframework/core/PriorityOrdered { @@ -188,7 +188,7 @@ public class io/sentry/spring/graphql/SentryGraphqlConfiguration { public final class io/sentry/spring/graphql/SentrySpringSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public class io/sentry/spring/tracing/SentryAdviceConfiguration { @@ -207,17 +207,17 @@ public abstract interface annotation class io/sentry/spring/tracing/SentrySpan : public class io/sentry/spring/tracing/SentrySpanAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } public class io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor : org/springframework/http/client/ClientHttpRequestInterceptor { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun intercept (Lorg/springframework/http/HttpRequest;[BLorg/springframework/http/client/ClientHttpRequestExecution;)Lorg/springframework/http/client/ClientHttpResponse; } public class io/sentry/spring/tracing/SentrySpanClientWebRequestFilter : org/springframework/web/reactive/function/client/ExchangeFilterFunction { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/reactive/function/client/ClientRequest;Lorg/springframework/web/reactive/function/client/ExchangeFunction;)Lreactor/core/publisher/Mono; } @@ -232,8 +232,8 @@ public class io/sentry/spring/tracing/SentryTracingConfiguration { public class io/sentry/spring/tracing/SentryTracingFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/FilterChain;)V } @@ -245,7 +245,7 @@ public abstract interface annotation class io/sentry/spring/tracing/SentryTransa public class io/sentry/spring/tracing/SentryTransactionAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -266,7 +266,7 @@ public abstract interface class io/sentry/spring/tracing/TransactionNameProvider } public class io/sentry/spring/webflux/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/protocol/Request; } @@ -278,13 +278,14 @@ public final class io/sentry/spring/webflux/SentryScheduleHook : java/util/funct public final class io/sentry/spring/webflux/SentryWebExceptionHandler : org/springframework/web/server/WebExceptionHandler { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun handle (Lorg/springframework/web/server/ServerWebExchange;Ljava/lang/Throwable;)Lreactor/core/publisher/Mono; } public final class io/sentry/spring/webflux/SentryWebFilter : org/springframework/web/server/WebFilter { public static final field SENTRY_HUB_KEY Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public static final field SENTRY_SCOPES_KEY Ljava/lang/String; + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java index fc9da87933..ba0586eade 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java @@ -5,7 +5,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -29,15 +29,15 @@ public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered { public static final String MECHANISM_TYPE = "Spring5ExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull TransactionNameProvider transactionNameProvider; private final int order; public SentryExceptionResolver( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final int order) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); this.order = order; @@ -53,7 +53,7 @@ public SentryExceptionResolver( final SentryEvent event = createEvent(request, ex); final Hint hint = createHint(request, response); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); // null = run other HandlerExceptionResolvers to actually handle the exception return null; diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java b/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java index e597d175b6..195a88e277 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryHubRegistrar.java @@ -1,7 +1,7 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; import io.sentry.protocol.SdkVersion; @@ -60,7 +60,7 @@ private void registerSentryOptions( private void registerSentryHubBean(final @NotNull BeanDefinitionRegistry registry) { final BeanDefinitionBuilder builder = - BeanDefinitionBuilder.genericBeanDefinition(HubAdapter.class); + BeanDefinitionBuilder.genericBeanDefinition(ScopesAdapter.class); builder.setInitMethodName("getInstance"); registry.registerBeanDefinition("sentryHub", builder.getBeanDefinition()); diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java b/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java index 9e455dfb04..ca431aae14 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java @@ -2,10 +2,10 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryOptions; import io.sentry.SentryOptions.TracesSamplerCallback; @@ -27,15 +27,15 @@ public class SentryInitBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, DisposableBean { private @Nullable ApplicationContext applicationContext; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryInitBeanPostProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryInitBeanPostProcessor(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.hub = hub; + SentryInitBeanPostProcessor(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.scopes = scopes; } @Override @@ -86,6 +86,6 @@ public void setApplicationContext(final @NotNull ApplicationContext applicationC @Override public void destroy() { - hub.close(); + scopes.close(); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java index 442644fcac..2d9e2996f7 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; @@ -20,11 +20,11 @@ @Open public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private volatile @Nullable List extraSecurityCookies; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } // httpRequest.getRequestURL() returns StringBuffer which is considered an obsolete class. @@ -40,7 +40,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { extractSecurityCookieNamesOrUseCached(httpRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest, additionalSecurityCookieNames)); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String cookieName = HttpUtils.COOKIE_HEADER_NAME; final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( @@ -57,7 +57,8 @@ Map resolveHeadersMap( final Map headersMap = new HashMap<>(); for (String headerName : Collections.list(request.getHeaderNames())) { // do not copy personal information identifiable headers - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( request.getHeaders(headerName), headerName, additionalSecurityCookieNames); @@ -94,7 +95,8 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to extract session cookie name from request.", t); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 8fd8180941..7695545f04 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -8,8 +8,8 @@ import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -29,26 +29,26 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; public SentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentrySpringFilter(final @NotNull IHub hub) { - this(hub, new SentryRequestResolver(hub), new SpringMvcTransactionNameProvider()); + public SentrySpringFilter(final @NotNull IScopes scopes) { + this(scopes, new SentryRequestResolver(scopes), new SpringMvcTransactionNameProvider()); } public SentrySpringFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override @@ -57,20 +57,20 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - hub.pushScope(); + scopes.pushScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - hub.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); configureScope(request); filterChain.doFilter(request, response); } finally { - hub.popScope(); + scopes.popScope(); } } else { filterChain.doFilter(servletRequest, response); @@ -79,7 +79,7 @@ protected void doFilterInternal( private void configureScope(HttpServletRequest request) { try { - hub.configureScope( + scopes.configureScope( scope -> { // set basic request information on the scope scope.setRequest(requestResolver.resolveSentryRequest(request)); @@ -92,11 +92,12 @@ private void configureScope(HttpServletRequest request) { // request processing if (request instanceof CachedBodyHttpServletRequest) { scope.addEventProcessor( - new RequestBodyExtractingEventProcessor(request, hub.getOptions())); + new RequestBodyExtractingEventProcessor(request, scopes.getOptions())); } }); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Failed to set scope for HTTP request", e); } @@ -104,12 +105,13 @@ private void configureScope(HttpServletRequest request) { private @NotNull HttpServletRequest resolveHttpServletRequest( final @NotNull HttpServletRequest request) { - if (hub.getOptions().isSendDefaultPii() - && qualifiesForCaching(request, hub.getOptions().getMaxRequestBodySize())) { + if (scopes.getOptions().isSendDefaultPii() + && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { return new CachedBodyHttpServletRequest(request); } catch (IOException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index cb4a700696..88d205a57e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -1,6 +1,6 @@ package io.sentry.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -9,21 +9,23 @@ /** * Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate - * the current {@link IHub} on the thread executing async task - like MVC controller methods + * the current {@link IScopes} on the thread executing async task - like MVC controller methods * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override + @SuppressWarnings("deprecation") public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java index 55c5826d40..e0b4e9c1ba 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.IpAddressUtils; import io.sentry.protocol.User; import io.sentry.util.Objects; @@ -26,12 +26,12 @@ */ @Open public class SentryUserFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull List sentryUserProviders; public SentryUserFilter( - final @NotNull IHub hub, final @NotNull List sentryUserProviders) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull List sentryUserProviders) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.sentryUserProviders = Objects.requireNonNull(sentryUserProviders, "sentryUserProviders list is required"); } @@ -46,13 +46,13 @@ protected void doFilterInternal( for (final SentryUserProvider provider : sentryUserProviders) { apply(user, provider.provideUser()); } - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { if (IpAddressUtils.isDefault(user.getIpAddress())) { // unset {{auto}} as it would set the server's ip address as a user ip address user.setIpAddress(null); } } - hub.setUser(user); + scopes.setUser(user); chain.doFilter(request, response); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index c29ad44900..edae74c2ac 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -4,8 +4,8 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; @@ -30,16 +30,16 @@ @ApiStatus.Experimental @Open public class SentryCheckInAdvice implements MethodInterceptor, EmbeddedValueResolverAware { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @Nullable StringValueResolver resolver; public SentryCheckInAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCheckInAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCheckInAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -69,7 +69,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl // Sentry should alert the user about missed checkins in this case since the monitor slug // won't match // what is configured in Sentry. - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -79,7 +80,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } if (ObjectUtils.isEmpty(monitorSlug)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -87,8 +89,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); @@ -96,7 +98,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { if (!isHeartbeatOnly) { - checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); } return invocation.proceed(); } catch (Throwable e) { @@ -106,8 +108,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java index bababbce77..119f39ba6c 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/exception/SentryCaptureExceptionParameterAdvice.java @@ -1,8 +1,8 @@ package io.sentry.spring.exception; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.exception.ExceptionMechanismException; import io.sentry.protocol.Mechanism; import io.sentry.util.Objects; @@ -22,14 +22,14 @@ @Open public class SentryCaptureExceptionParameterAdvice implements MethodInterceptor { private static final String MECHANISM_TYPE = "SentrySpring5CaptureExceptionParameterAdvice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryCaptureExceptionParameterAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -58,6 +58,6 @@ private void captureException(final @NotNull Throwable throwable) { mechanism.setHandled(true); final Throwable mechanismException = new ExceptionMechanismException(mechanism, throwable, Thread.currentThread()); - hub.captureException(mechanismException); + scopes.captureException(mechanismException); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java index 62a8669f89..f1d8717598 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryBatchLoaderRegistry.java @@ -1,11 +1,11 @@ package io.sentry.spring.graphql; -import static io.sentry.graphql.SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY; +import static io.sentry.graphql.SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY; import graphql.GraphQLContext; import io.sentry.Breadcrumb; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import java.util.List; import java.util.Map; import java.util.Set; @@ -89,7 +89,7 @@ public BatchLoaderRegistry.RegistrationSpec withOptions(DataLoaderOptions public void registerBatchLoader(BiFunction, BatchLoaderEnvironment, Flux> loader) { delegate.registerBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); @@ -100,20 +100,20 @@ public void registerMappedBatchLoader( BiFunction, BatchLoaderEnvironment, Mono>> loader) { delegate.registerMappedBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); } - private @NotNull IHub hubFromContext(final @NotNull BatchLoaderEnvironment environment) { + private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) { Object context = environment.getContext(); if (context instanceof GraphQLContext) { GraphQLContext graphqlContext = (GraphQLContext) context; - return graphqlContext.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java index fb4e09e889..eef5fdae69 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentryDgsSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; @@ -17,7 +17,7 @@ public SentryDgsSubscriptionHandler() { @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -25,7 +25,7 @@ public SentryDgsSubscriptionHandler() { return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); exceptionReporter.captureThrowable(throwable, exceptionDetails, null); }); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java index a7809eb230..b3f0b9830e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/graphql/SentrySpringSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; import org.jetbrains.annotations.NotNull; @@ -13,7 +13,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -21,7 +21,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); if (throwable instanceof SubscriptionPublisherException && throwable.getCause() != null) { exceptionReporter.captureThrowable(throwable.getCause(), exceptionDetails, null); diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java index 96c4275836..c1b7305d4f 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.util.Objects; import java.lang.reflect.Method; @@ -22,20 +22,20 @@ @Open public class SentrySpanAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentrySpanAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @Override public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null || activeSpan.isNoOp()) { // there is no active transaction, we do not start new span diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java index 4067c69c8c..bdf8417642 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java @@ -8,7 +8,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -27,10 +27,10 @@ @Open public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private static final String TRACE_ORIGIN = "auto.http.spring.resttemplate"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientHttpRequestInterceptor(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -42,7 +42,7 @@ public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { Integer responseStatusCode = null; ClientHttpResponse response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { maybeAddTracingHeaders(request, null); return execution.execute(request, body); @@ -83,7 +83,7 @@ private void maybeAddTracingHeaders( final @NotNull HttpRequest request, final @Nullable ISpan span) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.getURI().toString(), request.getHeaders().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -120,6 +120,6 @@ private void addBreadcrumb( hint.set(SPRING_REQUEST_INTERCEPTOR_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java index 00ad91bbb0..c526cb64cc 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientWebRequestFilter.java @@ -7,7 +7,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -26,16 +26,16 @@ @Open public class SentrySpanClientWebRequestFilter implements ExchangeFilterFunction { private static final String TRACE_ORIGIN = "auto.http.spring.webclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientWebRequestFilter(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override public @NotNull Mono filter( final @NotNull ClientRequest request, final @NotNull ExchangeFunction next) { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { addBreadcrumb(request, null); return next.exchange(maybeAddHeaders(request, null)); @@ -76,7 +76,7 @@ private ClientRequest maybeAddHeaders( final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url().toString(), request.headers().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -113,6 +113,6 @@ private void addBreadcrumb( hint.set(SPRING_EXCHANGE_FILTER_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java index 5af329f600..50cdb8dc3a 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java @@ -3,9 +3,9 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.BaggageHeader; import io.sentry.CustomSamplingContext; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; import io.sentry.TransactionContext; @@ -35,7 +35,7 @@ public class SentryTracingFilter extends OncePerRequestFilter { private static final String TRACE_ORIGIN = "auto.http.spring.webmvc"; private final @NotNull TransactionNameProvider transactionNameProvider; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; /** * Creates filter that resolves transaction name using {@link SpringMvcTransactionNameProvider}. @@ -46,25 +46,26 @@ public class SentryTracingFilter extends OncePerRequestFilter { * javax.servlet.Filter}. */ public SentryTracingFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } /** * Creates filter that resolves transaction name using transaction name provider given by * parameter. * - * @param hub - the hub + * @param scopes - the scopes * @param transactionNameProvider - transaction name provider. */ public SentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { + this.scopes = Objects.requireNonNull(scopes, "scopes is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentryTracingFilter(final @NotNull IHub hub) { - this(hub, new SpringMvcTransactionNameProvider()); + public SentryTracingFilter(final @NotNull IScopes scopes) { + this(scopes, new SpringMvcTransactionNameProvider()); } @Override @@ -74,15 +75,15 @@ protected void doFilterInternal( final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { final @Nullable String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER)); final @Nullable TransactionContext transactionContext = - hub.continueTrace(sentryTraceHeader, baggageHeader); + scopes.continueTrace(sentryTraceHeader, baggageHeader); - if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { + if (scopes.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext); } else { filterChain.doFilter(httpRequest, httpResponse); @@ -129,7 +130,7 @@ private void doFilterWithTransaction( } private boolean shouldTraceRequest(final @NotNull HttpServletRequest request) { - return hub.getOptions().isTraceOptionsRequests() + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.name().equals(request.getMethod()); } @@ -151,14 +152,14 @@ private ITransaction startTransaction( transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, "http.server"), transactionOptions); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java index c417f14a14..8f4f5bbdfc 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.TransactionContext; import io.sentry.TransactionOptions; @@ -27,14 +27,14 @@ @Open public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryTransactionAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryTransactionAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryTransactionAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -67,11 +67,11 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - hub.pushScope(); + scopes.pushScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(nameAndSource.name, nameAndSource.source, operation), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN); @@ -85,7 +85,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - hub.popScope(); + scopes.popScope(); } } } @@ -105,7 +105,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } private boolean isTransactionActive() { - return hub.getSpan() != null; + return scopes.getSpan() != null; } private static class TransactionNameAndSource { diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java index a710438c46..76e50985e5 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; @@ -20,10 +20,10 @@ @Open @ApiStatus.Experimental public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public @NotNull Request resolveSentryRequest(final @NotNull ServerHttpRequest httpRequest) { @@ -36,7 +36,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { urlDetails.applyToRequest(sentryRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest.getHeaders())); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String headerName = HttpUtils.COOKIE_HEADER_NAME; sentryRequest.setCookies( toString( @@ -52,7 +52,8 @@ Map resolveHeadersMap(final HttpHeaders request) { for (Map.Entry> entry : request.entrySet()) { // do not copy personal information identifiable headers String headerName = entry.getKey(); - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { headersMap.put( headerName, toString( diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java index 7775d4e482..20f494168d 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java @@ -1,6 +1,6 @@ package io.sentry.spring.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -13,16 +13,18 @@ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override + @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java index 98d3855a04..042d1d48ec 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebExceptionHandler.java @@ -5,7 +5,7 @@ import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_RESPONSE; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -26,10 +26,10 @@ @ApiStatus.Experimental public final class SentryWebExceptionHandler implements WebExceptionHandler { public static final String MECHANISM_TYPE = "Spring5WebFluxExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryWebExceptionHandler(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryWebExceptionHandler(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -50,7 +50,7 @@ public SentryWebExceptionHandler(final @NotNull IHub hub) { hint.set(WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse()); hint.set(WEBFLUX_EXCEPTION_HANDLER_EXCHANGE, serverWebExchange); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } return Mono.error(ex); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index f68a87ae0f..3bb7de5ae4 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -7,10 +7,10 @@ import io.sentry.Breadcrumb; import io.sentry.CustomSamplingContext; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; @@ -34,22 +34,24 @@ /** Manages {@link IScope} in Webflux request processing. */ @ApiStatus.Experimental public final class SentryWebFilter implements WebFilter { - public static final String SENTRY_HUB_KEY = "sentry-hub"; + public static final String SENTRY_SCOPES_KEY = "sentry-scopes"; + @Deprecated public static final String SENTRY_HUB_KEY = SENTRY_SCOPES_KEY; private static final String TRANSACTION_OP = "http.server"; private static final String TRACE_ORIGIN = "auto.spring.webflux"; private final @NotNull SentryRequestResolver sentryRequestResolver; - public SentryWebFilter(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.sentryRequestResolver = new SentryRequestResolver(hub); + public SentryWebFilter(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.sentryRequestResolver = new SentryRequestResolver(scopes); } @Override public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IHub requestHub = Sentry.cloneMainHub(); + @NotNull IScopes requestHub = Sentry.cloneMainHub(); + // TODO do not push / pop, use fork instead if (!requestHub.isEnabled()) { return webFilterChain.filter(serverWebExchange); } @@ -80,7 +82,8 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) finishTransaction(serverWebExchange, transaction); } requestHub.popScope(); - Sentry.setCurrentHub(NoOpHub.getInstance()); + // TODO token based cleanup instead? + Sentry.setCurrentScopes(NoOpScopes.getInstance()); }) .doOnError( e -> { @@ -91,8 +94,8 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) }) .doFirst( () -> { - serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); - Sentry.setCurrentHub(requestHub); + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); + Sentry.setCurrentScopes(requestHub); requestHub.pushScope(); final ServerHttpResponse response = serverWebExchange.getResponse(); @@ -109,13 +112,13 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) } private boolean shouldTraceRequest( - final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { - return hub.getOptions().isTraceOptionsRequests() + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request) { + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.equals(request.getMethod()); } private @NotNull ITransaction startTransaction( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request, final @Nullable TransactionContext transactionContext) { final @NotNull String name = request.getMethod() + " " + request.getURI().getPath(); @@ -131,10 +134,10 @@ private boolean shouldTraceRequest( transactionContext.setTransactionNameSource(TransactionNameSource.URL); transactionContext.setOperation(TRANSACTION_OP); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, TRANSACTION_OP), transactionOptions); } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt index 5a8fec3053..5182309416 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/EnableSentryTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.EventProcessor -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.Sentry @@ -65,9 +65,9 @@ class EnableSentryTest { } @Test - fun `creates Sentry Hub`() { + fun `creates Sentry Scopes`() { contextRunner.run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt index 4f94fd7ce0..23807e1fd9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring import io.sentry.CheckIn import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -55,19 +55,19 @@ class SentryCheckInAdviceTest { lateinit var sampleServiceSpringProperties: SampleServiceSpringProperties @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.options).thenReturn(SentryOptions()) + reset(scopes) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleService.hello() assertEquals(1, result) assertEquals(2, checkInCaptor.allValues.size) @@ -80,17 +80,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleService.oops() } @@ -104,17 +104,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceHeartbeat.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -124,17 +124,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleServiceHeartbeat.oops() } @@ -145,31 +145,31 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn but slug is missing, does not create check-in`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(hub, never()).pushScope() - verify(hub, never()).captureCheckIn(any()) - verify(hub, never()).popScope() + verify(scopes, never()).pushScope() + verify(scopes, never()).captureCheckIn(any()) + verify(scopes, never()).popScope() } @Test fun `when @SentryCheckIn is passed a spring property it is resolved correctly`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -179,17 +179,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that does not exist, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloUnresolvedProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -199,17 +199,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that causes an exception, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloExceptionProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -219,10 +219,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Configuration @@ -243,10 +243,10 @@ class SentryCheckInAdviceTest { open fun sampleServiceSpringProperties() = SampleServiceSpringProperties() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } companion object { diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt index f3e4c32fb0..048166107b 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryExceptionResolverTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.exception.ExceptionMechanismException @@ -17,7 +17,7 @@ import javax.servlet.http.HttpServletResponse import kotlin.test.Test class SentryExceptionResolverTest { - private val hub = mock() + private val scopes = mock() private val transactionNameProvider = mock() private val request = mock() @@ -26,10 +26,10 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets wrapped exception for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) val expectedCause = RuntimeException("test") - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, expectedCause) assertThat(eventCaptor.firstValue.throwable).isEqualTo(expectedCause) @@ -46,9 +46,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets fatal level for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.level).isEqualTo(SentryLevel.FATAL) @@ -59,9 +59,9 @@ class SentryExceptionResolverTest { val expectedTransactionName = "test-transaction" whenever(transactionNameProvider.provideTransactionName(any())).thenReturn(expectedTransactionName) val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.transaction).isEqualTo(expectedTransactionName) @@ -71,9 +71,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, provides spring resolver hint`() { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) with(hintCaptor.firstValue) { @@ -86,8 +86,8 @@ class SentryExceptionResolverTest { fun `when custom create event method provided, uses it to capture event`() { val expectedEvent = SentryEvent() val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createEvent(request: HttpServletRequest, ex: Exception) = expectedEvent } @@ -100,8 +100,8 @@ class SentryExceptionResolverTest { fun `when custom create hint method provided, uses it to capture event`() { val expectedHint = Hint() val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createHint(request: HttpServletRequest, response: HttpServletResponse) = expectedHint } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt index 78ebf961c9..dc4a14e656 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryInitBeanPostProcessorTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -13,18 +13,18 @@ class SentryInitBeanPostProcessorTest { @Test fun closesSentryOnApplicationContextDestroy() { val ctx = AnnotationConfigApplicationContext(TestConfig::class.java) - val hub = ctx.getBean(IHub::class.java) + val scopes = ctx.getBean(IScopes::class.java) ctx.close() - verify(hub).close() + verify(scopes).close() } @Configuration open class TestConfig { @Bean(destroyMethod = "") - open fun hub() = mock() + open fun scopes() = mock() @Bean - open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(hub()) + open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(scopes()) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt index 3fe9b60743..c7277a2240 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryRequestHttpServletRequestProcessorTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryOptions import io.sentry.spring.tracing.SpringMvcTransactionNameProvider @@ -19,10 +19,10 @@ import kotlin.test.assertNotNull class SentryRequestHttpServletRequestProcessorTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut(request: HttpServletRequest, options: SentryOptions = SentryOptions()): SentryRequestHttpServletRequestProcessor { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return SentryRequestHttpServletRequestProcessor(SpringMvcTransactionNameProvider(), request) } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt index ce83c4b9b7..c6ac952531 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val response = MockHttpServletResponse() val chain = mock() lateinit var scope: IScope @@ -45,15 +45,15 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) - whenever(hub.options).thenReturn(options) - whenever(hub.isEnabled).thenReturn(true) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + whenever(scopes.isEnabled).thenReturn(true) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(hub) + return SentrySpringFilter(scopes) } } @@ -64,7 +64,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test @@ -72,7 +72,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> Assertions.assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") Assertions.assertThat(it.getData("method")).isEqualTo("POST") @@ -87,7 +87,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } @Test @@ -99,7 +99,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt index e202deca86..3f34ab9d9d 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt @@ -32,28 +32,28 @@ class SentryTaskDecoratorTest { val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt index 2dac9a57ed..7f9be1ba8b 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryUserFilterTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.protocol.User import org.mockito.kotlin.check @@ -16,7 +16,7 @@ import kotlin.test.assertNull class SentryUserFilterTest { class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -25,8 +25,8 @@ class SentryUserFilterTest { val options = SentryOptions().apply { this.isSendDefaultPii = isSendDefaultPii } - whenever(hub.options).thenReturn(options) - return SentryUserFilter(hub, userProviders) + whenever(scopes.options).thenReturn(options) + return SentryUserFilter(scopes, userProviders) } } @@ -52,7 +52,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -72,7 +72,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -92,7 +92,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -118,7 +118,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(mapOf("key" to "value", "new-key" to "new-value"), it.others) } @@ -140,7 +140,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals("192.168.0.1", it.ipAddress) } @@ -162,7 +162,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertNull(it.ipAddress) } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt index 39bc66fabb..dca1c4c592 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/exception/SentryCaptureExceptionParameterAdviceTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.exception import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.exception.ExceptionMechanismException import org.junit.runner.RunWith @@ -30,18 +30,18 @@ class SentryCaptureExceptionParameterAdviceTest { lateinit var sampleService: SampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) + reset(scopes) } @Test fun `captures exception passed to method annotated with @SentryCaptureException`() { val exception = RuntimeException("test exception") sampleService.methodTakingAnException(exception) - verify(hub).captureException( + verify(scopes).captureException( check { assertTrue(it is ExceptionMechanismException) assertEquals(exception, it.throwable) @@ -60,10 +60,10 @@ class SentryCaptureExceptionParameterAdviceTest { open fun sampleService() = SampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt index df2df5b3ef..1aa97cd262 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/graphql/SentrySpringSubscriptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchPar import graphql.language.Document import graphql.language.OperationDefinition import graphql.schema.DataFetchingEnvironment -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.graphql.ExceptionReporter import org.junit.jupiter.api.assertThrows import org.mockito.kotlin.anyOrNull @@ -23,7 +23,7 @@ class SentrySpringSubscriptionHandlerTest { @Test fun `reports exception`() { val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -32,7 +32,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -41,7 +41,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() @@ -52,7 +52,7 @@ class SentrySpringSubscriptionHandlerTest { fun `unwraps SubscriptionPublisherException and reports cause`() { val exception = IllegalStateException("some exception") val wrappedException = SubscriptionPublisherException(emptyList(), exception) - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -61,7 +61,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -70,7 +70,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt index 998b27c7e8..ad9734def6 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/mvc/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.mvc -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.SentryOptions @@ -104,7 +104,7 @@ class SentrySpringIntegrationTest { lateinit var anotherService: AnotherService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -260,7 +260,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -276,7 +276,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodWithInnerSpanThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -370,20 +370,20 @@ open class App { open fun springSecuritySentryUserProvider(sentryOptions: SentryOptions) = SpringSecuritySentryUserProvider(sentryOptions) @Bean - open fun sentryUserFilter(hub: IHub, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { - this.filter = SentryUserFilter(hub, sentryUserProviders) + open fun sentryUserFilter(scopes: IScopes, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { + this.filter = SentryUserFilter(scopes, sentryUserProviders) this.order = Ordered.LOWEST_PRECEDENCE } @Bean - open fun sentrySpringFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentrySpringFilter(hub) + open fun sentrySpringFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentrySpringFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE } @Bean - open fun sentryTracingFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentryTracingFilter(hub) + open fun sentryTracingFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentryTracingFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE + 1 // must run after SentrySpringFilter } @@ -391,13 +391,13 @@ open class App { open fun sentryTaskDecorator() = SentryTaskDecorator() @Bean - open fun webClient(hub: IHub): WebClient { + open fun webClient(scopes: IScopes): WebClient { return WebClient.builder() .filter( ExchangeFilterFunctions .basicAuthentication("user", "password") ) - .filter(SentrySpanClientWebRequestFilter(hub)).build() + .filter(SentrySpanClientWebRequestFilter(scopes)).build() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt index dc6f00eb46..a6f59971c9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentrySpanAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Sentry import io.sentry.SentryOptions @@ -37,20 +37,20 @@ class SentrySpanAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - whenever(hub.options).thenReturn(SentryOptions()) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when class is annotated with @SentrySpan, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -62,10 +62,10 @@ class SentrySpanAdviceTest { @Test fun `when class is annotated with @SentrySpan with operation set, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedWithOperationSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -76,10 +76,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan with properties set, attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -90,10 +90,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan without properties set, attaches span to existing transaction and sets Span description as className dot methodName`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithoutSpanDescriptionSet() assertEquals(2, result) assertEquals(1, tx.spans.size) @@ -104,10 +104,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and returns, attached span has status OK`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) sampleService.methodWithSpanDescriptionSet() assertEquals(SpanStatus.OK, tx.spans.first().status) } @@ -115,10 +115,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and throws exception, attached span has throwable set and INTERNAL_ERROR status`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) var throwable: Throwable? = null try { sampleService.methodThrowingException() @@ -131,7 +131,7 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and there is no active transaction, span is not created and method is executed`() { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) } @@ -151,10 +151,10 @@ class SentrySpanAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt index 6c4c06ebff..aca54809cc 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTracingFilterTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.tracing -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -38,7 +38,7 @@ import kotlin.test.fail class SentryTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -50,7 +50,7 @@ class SentryTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: Int = 200, sentryTraceHeader: String? = null, baggageHeaders: List? = null): SentryTracingFilter { @@ -61,16 +61,16 @@ class SentryTracingFilterTest { whenever(transactionNameProvider.provideTransactionSource()).thenReturn(TransactionNameSource.CUSTOM) if (sentryTraceHeader != null) { request.addHeader("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { request.addHeader("baggage", baggageHeaders) } response.status = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) - whenever(hub.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryTracingFilter(hub, transactionNameProvider) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) + whenever(scopes.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryTracingFilter(scopes, transactionNameProvider) } } @@ -82,7 +82,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -95,7 +95,7 @@ class SentryTracingFilterTest { } ) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -114,7 +114,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -130,7 +130,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -146,7 +146,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -163,7 +163,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -174,15 +174,15 @@ class SentryTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) filter.doFilter(fixture.request, fixture.response, fixture.chain) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -196,7 +196,7 @@ class SentryTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -216,10 +216,10 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -233,7 +233,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -253,7 +253,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -275,9 +275,9 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt index 0fa9abbb29..f53acde8aa 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -44,13 +44,13 @@ class SentryTransactionAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.options).thenReturn( + reset(scopes) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -60,7 +60,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction around method annotated with @SentryTransaction`() { sampleService.methodWithTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("customName") assertThat(it.contexts.trace!!.operation).isEqualTo("bean") @@ -76,7 +76,7 @@ class SentryTransactionAdviceTest { @Test fun `when method annotated with @SentryTransaction throws exception, sets error status on transaction`() { assertThrows { sampleService.methodThrowingException() } - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -89,7 +89,7 @@ class SentryTransactionAdviceTest { @Test fun `when @SentryTransaction has no name set, sets transaction name as className dot methodName`() { sampleService.methodWithoutTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("SampleService.methodWithoutTransactionNameSet") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -102,18 +102,18 @@ class SentryTransactionAdviceTest { @Test fun `when transaction is already active, does not start new transaction`() { - whenever(hub.options).thenReturn(SentryOptions()) - whenever(hub.span).then { SentryTracer(TransactionContext("aTransaction", "op"), hub) } + whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.span).then { SentryTracer(TransactionContext("aTransaction", "op"), scopes) } sampleService.methodWithTransactionNameSet() - verify(hub, times(0)).captureTransaction(any(), any()) + verify(scopes, times(0)).captureTransaction(any(), any()) } @Test fun `creates transaction around method in class annotated with @SentryTransaction`() { classAnnotatedSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -127,7 +127,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction with operation set around method in class annotated with @SentryTransaction`() { classAnnotatedWithOperationSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedWithOperationSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("my-op") @@ -141,13 +141,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(hub).popScope() + verify(scopes).popScope() } @Configuration @@ -165,10 +165,10 @@ class SentryTransactionAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt index b09011d544..7a8b2993f9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt @@ -33,28 +33,28 @@ class SentryScheduleHookTest { val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { Sentry.setCurrentHub(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt index be56a8391d..2113c748ee 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,8 +2,9 @@ package io.sentry.spring.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.HubScopesWrapper import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.ScopeCallback import io.sentry.Sentry @@ -17,7 +18,7 @@ import io.sentry.TransactionOptions import io.sentry.protocol.SentryId import io.sentry.protocol.SentryTransaction import io.sentry.protocol.TransactionNameSource -import io.sentry.spring.webflux.SentryWebFilter.SENTRY_HUB_KEY +import io.sentry.spring.webflux.SentryWebFilter.SENTRY_SCOPES_KEY import org.assertj.core.api.Assertions.assertThat import org.mockito.Mockito import org.mockito.kotlin.any @@ -47,7 +48,7 @@ import kotlin.test.fail class SentryWebFluxTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var request: MockServerHttpRequest lateinit var exchange: MockServerWebExchange val chain = mock() @@ -58,35 +59,37 @@ class SentryWebFluxTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: HttpStatus = HttpStatus.OK, sentryTraceHeader: String? = null, baggageHeaders: List? = null, method: HttpMethod = HttpMethod.POST): SentryWebFilter { var requestBuilder = MockServerHttpRequest.method(method, "/product/{id}", 12) if (sentryTraceHeader != null) { requestBuilder = requestBuilder.header("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { requestBuilder = requestBuilder.header("baggage", *baggageHeaders.toTypedArray()) } request = requestBuilder.build() exchange = MockServerWebExchange.builder(request).build() - exchange.attributes.put(SENTRY_HUB_KEY, hub) + exchange.attributes.put(SENTRY_SCOPES_KEY, scopes) exchange.attributes.put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, PathPatternParser().parse("/product/{id}")) exchange.response.statusCode = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) whenever(chain.filter(any())).thenReturn(Mono.create { s -> s.success() }) - whenever(hub.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryWebFilter(hub) + whenever(scopes.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryWebFilter(scopes) } } private val fixture = Fixture() - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) + it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) closure.invoke() } @@ -94,10 +97,10 @@ class SentryWebFluxTracingFilterTest { fun `creates transaction around the request`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -110,7 +113,7 @@ class SentryWebFluxTracingFilterTest { } ) verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -129,10 +132,10 @@ class SentryWebFluxTracingFilterTest { fun `sets correct span status based on the response status`() { val filter = fixture.getSut(status = HttpStatus.INTERNAL_SERVER_ERROR) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) assertThat(it.contexts.response!!.statusCode).isEqualTo(500) @@ -148,10 +151,10 @@ class SentryWebFluxTracingFilterTest { fun `does not set span status for response status that dont match predefined span statuses`() { val filter = fixture.getSut(status = HttpStatus.FOUND) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -166,10 +169,10 @@ class SentryWebFluxTracingFilterTest { fun `when sentry trace is not present, transaction does not have parentSpanId set`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -185,10 +188,10 @@ class SentryWebFluxTracingFilterTest { val parentSpanId = SpanId() val filter = fixture.getSut(sentryTraceHeader = "${SentryId()}-$parentSpanId-1") - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -200,16 +203,16 @@ class SentryWebFluxTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) } } @@ -217,7 +220,7 @@ class SentryWebFluxTracingFilterTest { fun `sets status to internal server error when chain throws exception`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { whenever(fixture.chain.filter(any())).thenReturn(Mono.error(RuntimeException("error"))) try { @@ -225,7 +228,7 @@ class SentryWebFluxTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -240,21 +243,21 @@ class SentryWebFluxTracingFilterTest { fun `does not track OPTIONS request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub).pushScope() - verify(fixture.hub).addBreadcrumb(any(), any()) - verify(fixture.hub).configureScope(any()) - verify(fixture.hub).popScope() - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes).pushScope() + verify(fixture.scopes).addBreadcrumb(any(), any()) + verify(fixture.scopes).configureScope(any()) + verify(fixture.scopes).popScope() + verifyNoMoreInteractions(fixture.scopes) } } @@ -262,14 +265,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks OPTIONS request with traceOptionsRequests=true`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = true filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -284,14 +287,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks POST request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.POST) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -310,19 +313,19 @@ class SentryWebFluxTracingFilterTest { fixture.options.enableTracing = false val filter = fixture.getSut(sentryTraceHeader = sentryTraceHeaderString, baggageHeaders = baggageHeaderStrings) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull() ) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) } } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt index 0d4346c5ae..8c5aeb1c0a 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebfluxIntegrationTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.webflux -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.checkEvent import io.sentry.checkTransaction @@ -160,13 +160,13 @@ open class App { open fun mockTransport() = transport @Bean - open fun hub() = HubAdapter.getInstance() + open fun scopes() = ScopesAdapter.getInstance() @Bean - open fun sentryFilter(hub: IHub) = SentryWebFilter(hub) + open fun sentryFilter(scopes: IScopes) = SentryWebFilter(scopes) @Bean - open fun sentryWebExceptionHandler(hub: IHub) = SentryWebExceptionHandler(hub) + open fun sentryWebExceptionHandler(scopes: IScopes) = SentryWebExceptionHandler(scopes) @Bean open fun sentryScheduleHookRegistrar() = ApplicationRunner { From 792d482570aca7ef1d321e0cf46dc543db431c96 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:53:48 +0200 Subject: [PATCH 14/57] Replace `IHub` with `IScopes` in Spring 6 / Spring Boot 3 integrations --- .../api/sentry-spring-boot-jakarta.api | 2 +- .../boot/jakarta/SentryAutoConfiguration.java | 43 ++++----- .../SentrySpanRestClientCustomizer.java | 6 +- .../SentrySpanRestTemplateCustomizer.java | 6 +- .../SentrySpanWebClientCustomizer.java | 6 +- .../SentryWebfluxAutoConfiguration.java | 21 +++-- .../jakarta/SentryAutoConfigurationTest.kt | 12 +-- .../SentrySpanRestClientCustomizerTest.kt | 22 ++--- .../SentrySpanRestTemplateCustomizerTest.kt | 22 ++--- .../SentrySpanWebClientCustomizerTest.kt | 22 ++--- .../jakarta/it/SentrySpringIntegrationTest.kt | 6 +- .../api/sentry-spring-jakarta.api | 61 ++++++------ .../sentry/spring/jakarta/EnableSentry.java | 2 +- .../jakarta/SentryExceptionResolver.java | 10 +- .../spring/jakarta/SentryHubRegistrar.java | 4 +- .../jakarta/SentryInitBeanPostProcessor.java | 16 ++-- .../spring/jakarta/SentryRequestResolver.java | 16 ++-- .../spring/jakarta/SentrySpringFilter.java | 38 ++++---- .../spring/jakarta/SentryTaskDecorator.java | 18 ++-- .../spring/jakarta/SentryUserFilter.java | 12 +-- .../jakarta/checkin/SentryCheckInAdvice.java | 28 +++--- ...SentryCaptureExceptionParameterAdvice.java | 14 +-- .../graphql/SentryBatchLoaderRegistry.java | 16 ++-- .../graphql/SentryDgsSubscriptionHandler.java | 6 +- .../SentrySpringSubscriptionHandler.java | 6 +- .../jakarta/tracing/SentrySpanAdvice.java | 14 +-- ...entrySpanClientHttpRequestInterceptor.java | 18 ++-- .../SentrySpanClientWebRequestFilter.java | 14 +-- .../jakarta/tracing/SentryTracingFilter.java | 31 +++--- .../tracing/SentryTransactionAdvice.java | 20 ++-- .../webflux/AbstractSentryWebFilter.java | 35 +++---- .../spring/jakarta/webflux/ReactorUtils.java | 35 ++++--- .../SentryReactorThreadLocalAccessor.java | 18 ++-- .../webflux/SentryRequestResolver.java | 13 +-- .../jakarta/webflux/SentryScheduleHook.java | 12 ++- .../webflux/SentryWebExceptionHandler.java | 18 ++-- .../jakarta/webflux/SentryWebFilter.java | 16 ++-- ...entryWebFilterWithThreadLocalAccessor.java | 13 +-- .../sentry/spring/jakarta/EnableSentryTest.kt | 4 +- .../spring/jakarta/SentryCheckInAdviceTest.kt | 94 +++++++++---------- .../jakarta/SentryExceptionResolverTest.kt | 28 +++--- .../SentryInitBeanPostProcessorTest.kt | 10 +- ...yRequestHttpServletRequestProcessorTest.kt | 6 +- .../spring/jakarta/SentrySpringFilterTest.kt | 20 ++-- .../spring/jakarta/SentryTaskDecoratorTest.kt | 16 ++-- .../spring/jakarta/SentryUserFilterTest.kt | 20 ++-- ...ntryCaptureExceptionParameterAdviceTest.kt | 16 ++-- .../SentrySpringSubscriptionHandlerTest.kt | 14 +-- .../mvc/SentrySpringIntegrationTest.kt | 24 ++--- .../jakarta/tracing/SentrySpanAdviceTest.kt | 40 ++++---- .../tracing/SentryTracingFilterTest.kt | 52 +++++----- .../tracing/SentryTransactionAdviceTest.kt | 38 ++++---- .../jakarta/webflux/ReactorUtilsTest.kt | 45 ++++----- .../jakarta/webflux/SentryScheduleHookTest.kt | 16 ++-- .../webflux/SentryWebFluxTracingFilterTest.kt | 93 +++++++++--------- .../webflux/SentryWebfluxIntegrationTest.kt | 10 +- 56 files changed, 623 insertions(+), 595 deletions(-) diff --git a/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api b/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api index a392ff75ee..009036082e 100644 --- a/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api +++ b/sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api @@ -62,7 +62,7 @@ public class io/sentry/spring/boot/jakarta/SentryProperties$Reactive { public class io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration { public fun ()V - public fun sentryWebExceptionHandler (Lio/sentry/IHub;)Lio/sentry/spring/jakarta/webflux/SentryWebExceptionHandler; + public fun sentryWebExceptionHandler (Lio/sentry/IScopes;)Lio/sentry/spring/jakarta/webflux/SentryWebExceptionHandler; } public class io/sentry/spring/boot/jakarta/graphql/SentryGraphqlAutoConfiguration { diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java index fbf3162280..b2c8df213c 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java @@ -3,10 +3,10 @@ import com.jakewharton.nopen.annotation.Open; import graphql.GraphQLError; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; @@ -116,7 +116,7 @@ static class HubConfiguration { } @Bean - public @NotNull IHub sentryHub( + public @NotNull IScopes sentryHub( final @NotNull List> optionsConfigurations, final @NotNull SentryProperties options, final @NotNull ObjectProvider gitProperties) { @@ -139,7 +139,7 @@ static class HubConfiguration { // here we make sure that only classes that extend throwable are set on this field options.getIgnoredExceptionsForType().removeIf(it -> !Throwable.class.isAssignableFrom(it)); Sentry.init(options); - return HubAdapter.getInstance(); + return ScopesAdapter.getInstance(); } @Configuration(proxyBeanMethods = false) @@ -239,7 +239,7 @@ static class SentrySecurityConfiguration { * HttpServletRequest#getUserPrincipal()}. If Spring Security is auto-configured, its order is * set to run after Spring Security. * - * @param hub the Sentry hub + * @param scopes the Sentry scopes * @param sentryProperties the Sentry properties * @param sentryUserProvider the user provider * @return {@link SentryUserFilter} registration bean @@ -247,11 +247,11 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnBean(SentryUserProvider.class) public @NotNull FilterRegistrationBean sentryUserFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryProperties sentryProperties, final @NotNull List sentryUserProvider) { final FilterRegistrationBean filter = new FilterRegistrationBean<>(); - filter.setFilter(new SentryUserFilter(hub, sentryUserProvider)); + filter.setFilter(new SentryUserFilter(scopes, sentryUserProvider)); filter.setOrder(resolveUserFilterOrder(sentryProperties)); return filter; } @@ -263,8 +263,8 @@ static class SentrySecurityConfiguration { } @Bean - public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IHub hub) { - return new SentryRequestResolver(hub); + public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IScopes scopes) { + return new SentryRequestResolver(scopes); } @Bean @@ -276,12 +276,12 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentrySpringFilter") public @NotNull FilterRegistrationBean sentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = new FilterRegistrationBean<>( - new SentrySpringFilter(hub, requestResolver, transactionNameProvider)); + new SentrySpringFilter(scopes, requestResolver, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE); return filter; } @@ -289,9 +289,10 @@ static class SentrySecurityConfiguration { @Bean @ConditionalOnMissingBean(name = "sentryTracingFilter") public FilterRegistrationBean sentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { FilterRegistrationBean filter = - new FilterRegistrationBean<>(new SentryTracingFilter(hub, transactionNameProvider)); + new FilterRegistrationBean<>(new SentryTracingFilter(scopes, transactionNameProvider)); filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE + 1); // must run after SentrySpringFilter return filter; } @@ -300,11 +301,11 @@ public FilterRegistrationBean sentryTracingFilter( @ConditionalOnMissingBean @ConditionalOnClass(HandlerExceptionResolver.class) public @NotNull SentryExceptionResolver sentryExceptionResolver( - final @NotNull IHub sentryHub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final @NotNull SentryProperties options) { return new SentryExceptionResolver( - sentryHub, transactionNameProvider, options.getExceptionResolverOrder()); + scopes, transactionNameProvider, options.getExceptionResolverOrder()); } } @@ -354,8 +355,8 @@ static class SentrySpanPointcutAutoConfiguration {} @Open static class SentryPerformanceRestTemplateConfiguration { @Bean - public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hub) { - return new SentrySpanRestTemplateCustomizer(hub); + public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IScopes scopes) { + return new SentrySpanRestTemplateCustomizer(scopes); } } @@ -365,8 +366,8 @@ public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hu @Open static class SentrySpanRestClientConfiguration { @Bean - public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IHub hub) { - return new SentrySpanRestClientCustomizer(hub); + public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IScopes scopes) { + return new SentrySpanRestClientCustomizer(scopes); } } @@ -376,8 +377,8 @@ public SentrySpanRestClientCustomizer sentrySpanRestClientCustomizer(IHub hub) { @Open static class SentryPerformanceWebClientConfiguration { @Bean - public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IHub hub) { - return new SentrySpanWebClientCustomizer(hub); + public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IScopes scopes) { + return new SentrySpanWebClientCustomizer(scopes); } } diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java index 243a4d1f50..304fb6911e 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.client.RestClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanRestClientCustomizer implements RestClientCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestClientCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub, false); + public SentrySpanRestClientCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes, false); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java index c1ded006e8..4a0faa9875 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.ArrayList; import java.util.List; @@ -14,8 +14,8 @@ class SentrySpanRestTemplateCustomizer implements RestTemplateCustomizer { private final @NotNull SentrySpanClientHttpRequestInterceptor interceptor; - public SentrySpanRestTemplateCustomizer(final @NotNull IHub hub) { - this.interceptor = new SentrySpanClientHttpRequestInterceptor(hub); + public SentrySpanRestTemplateCustomizer(final @NotNull IScopes scopes) { + this.interceptor = new SentrySpanClientHttpRequestInterceptor(scopes); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java index afbe2eb6c4..d349ac4c6e 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizer.java @@ -1,7 +1,7 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientWebRequestFilter; import org.jetbrains.annotations.NotNull; import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; @@ -11,8 +11,8 @@ class SentrySpanWebClientCustomizer implements WebClientCustomizer { private final @NotNull SentrySpanClientWebRequestFilter filter; - public SentrySpanWebClientCustomizer(final @NotNull IHub hub) { - this.filter = new SentrySpanClientWebRequestFilter(hub); + public SentrySpanWebClientCustomizer(final @NotNull IScopes scopes) { + this.filter = new SentrySpanClientWebRequestFilter(scopes); } @Override diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java index d1cda8b4d2..2bc8bc87ed 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java @@ -1,8 +1,8 @@ package io.sentry.spring.boot.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.spring.jakarta.webflux.SentryScheduleHook; import io.sentry.spring.jakarta.webflux.SentryWebExceptionHandler; import io.sentry.spring.jakarta.webflux.SentryWebFilter; @@ -28,7 +28,7 @@ /** Configures Sentry integration for Spring Webflux and Project Reactor. */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) -@ConditionalOnBean(IHub.class) +@ConditionalOnBean(IScopes.class) @ConditionalOnClass(Schedulers.class) @Open @ApiStatus.Experimental @@ -44,14 +44,14 @@ static class SentryWebfluxFilterThreadLocalAccessorConfiguration { * Configures a filter that sets up Sentry {@link IScope} for each request. * *

Makes use of newer reactor-core and context-propagation library feature - * ThreadLocalAccessor to propagate the Sentry hub. + * ThreadLocalAccessor to propagate the Sentry scopes. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) public @NotNull SentryWebFilterWithThreadLocalAccessor sentryWebFilterWithContextPropagation( - final @NotNull IHub hub) { + final @NotNull IScopes scopes) { Hooks.enableAutomaticContextPropagation(); - return new SentryWebFilterWithThreadLocalAccessor(hub); + return new SentryWebFilterWithThreadLocalAccessor(scopes); } } @@ -60,7 +60,7 @@ static class SentryWebfluxFilterThreadLocalAccessorConfiguration { @Open static class SentryWebfluxFilterConfiguration { - /** Configures hook that sets correct hub on the executing thread. */ + /** Configures hook that sets correct scopes on the executing thread. */ @Bean public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() { return args -> { @@ -71,15 +71,16 @@ static class SentryWebfluxFilterConfiguration { /** Configures a filter that sets up Sentry {@link IScope} for each request. */ @Bean @Order(SENTRY_SPRING_FILTER_PRECEDENCE) - public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IHub hub) { - return new SentryWebFilter(hub); + public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IScopes scopes) { + return new SentryWebFilter(scopes); } } /** Configures exception handler that handles unhandled exceptions and sends them to Sentry. */ @Bean - public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler(final @NotNull IHub hub) { - return new SentryWebExceptionHandler(hub); + public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler( + final @NotNull IScopes scopes) { + return new SentryWebExceptionHandler(scopes); } static final class SentryLegacyFilterConfigurationCondition extends AnyNestedCondition { diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt index aecb1b4712..df0fe96cf0 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt @@ -5,7 +5,7 @@ import io.sentry.AsyncHttpTransportFactory import io.sentry.Breadcrumb import io.sentry.EventProcessor import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.NoOpTransportFactory @@ -77,18 +77,18 @@ class SentryAutoConfigurationTest { .withConfiguration(AutoConfigurations.of(SentryAutoConfiguration::class.java, WebMvcAutoConfiguration::class.java)) @Test - fun `hub is not created when auto-configuration dsn is not set`() { + fun `scopes is not created when auto-configuration dsn is not set`() { contextRunner .run { - assertThat(it).doesNotHaveBean(IHub::class.java) + assertThat(it).doesNotHaveBean(IScopes::class.java) } } @Test - fun `hub is created when dsn is provided`() { + fun `scopes is created when dsn is provided`() { contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj") .run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } @@ -961,7 +961,7 @@ class SentryAutoConfigurationTest { } class CustomIntegration : Integration { - override fun register(hub: IHub, options: SentryOptions) {} + override fun register(scopes: IScopes, options: SentryOptions) {} } @Configuration(proxyBeanMethods = false) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt index f9f55ffede..9ba9bf57d2 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestClientCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -36,18 +36,18 @@ import kotlin.test.assertTrue class SentrySpanRestClientCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restClientBuilder = RestClient.builder() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestClientCustomizer(hub) + internal val customizer = SentrySpanRestClientCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut( @@ -75,7 +75,7 @@ class SentrySpanRestClientCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restClientBuilder.apply { @@ -247,7 +247,7 @@ class SentrySpanRestClientCustomizerTest { .body("content") .retrieve() .toEntity(String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -269,7 +269,7 @@ class SentrySpanRestClientCustomizerTest { .toEntity(String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -287,7 +287,7 @@ class SentrySpanRestClientCustomizerTest { .body("content") .retrieve() .toEntity(String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -309,7 +309,7 @@ class SentrySpanRestClientCustomizerTest { .toEntity(String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt index db5b25de44..4ab3205b31 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanRestTemplateCustomizerTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,21 +37,21 @@ import kotlin.test.assertTrue class SentrySpanRestTemplateCustomizerTest { class Fixture { val sentryOptions = SentryOptions() - val hub = mock() + val scopes = mock() val restTemplate = RestTemplateBuilder() .setConnectTimeout(Duration.ofSeconds(2)) .setReadTimeout(Duration.ofSeconds(2)) .build() var mockServer = MockWebServer() val transaction: SentryTracer - internal val customizer = SentrySpanRestTemplateCustomizer(hub) + internal val customizer = SentrySpanRestTemplateCustomizer(scopes) val url = mockServer.url("/test/123").toString() val scope = Scope(sentryOptions) init { - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) } fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN, includeMockServerInTracingOrigins: Boolean = true): RestTemplate { @@ -74,7 +74,7 @@ class SentrySpanRestTemplateCustomizerTest { ) if (isTransactionActive) { - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } return restTemplate @@ -209,7 +209,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = true).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -227,7 +227,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = true, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -240,7 +240,7 @@ class SentrySpanRestTemplateCustomizerTest { @Test fun `when transaction is not active adds breadcrumb when http calls succeeds`() { fixture.getSut(isTransactionActive = false).postForObject(fixture.url, "content", String::class.java) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) @@ -258,7 +258,7 @@ class SentrySpanRestTemplateCustomizerTest { fixture.getSut(isTransactionActive = false, status = HttpStatus.INTERNAL_SERVER_ERROR).getForObject(fixture.url, String::class.java) } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(fixture.url, it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt index 51f0a6cb3d..d3fb5f7d31 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentrySpanWebClientCustomizerTest.kt @@ -2,8 +2,8 @@ package io.sentry.spring.boot.jakarta import io.sentry.BaggageHeader import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,10 +39,10 @@ class SentrySpanWebClientCustomizerTest { class Fixture { lateinit var sentryOptions: SentryOptions lateinit var scope: IScope - val hub = mock() + val scopes = mock() var mockServer = MockWebServer() lateinit var transaction: SentryTracer - private val customizer = SentrySpanWebClientCustomizer(hub) + private val customizer = SentrySpanWebClientCustomizer(scopes) fun getSut(isTransactionActive: Boolean, status: HttpStatus = HttpStatus.OK, throwIOException: Boolean = false, includeMockServerInTracingOrigins: Boolean = true): WebClient { sentryOptions = SentryOptions().apply { @@ -54,9 +54,9 @@ class SentrySpanWebClientCustomizerTest { dsn = "http://key@localhost/proj" } scope = Scope(sentryOptions) - whenever(hub.options).thenReturn(sentryOptions) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) - transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), hub) + whenever(scopes.options).thenReturn(sentryOptions) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) + transaction = SentryTracer(TransactionContext("aTransaction", "op", TracesSamplingDecision(true)), scopes) val webClientBuilder = WebClient.builder() customizer.customize(webClientBuilder) val webClient = webClientBuilder.build() @@ -64,7 +64,7 @@ class SentrySpanWebClientCustomizerTest { if (isTransactionActive) { val scope = Scope(sentryOptions) scope.transaction = transaction - whenever(hub.span).thenReturn(transaction) + whenever(scopes.span).thenReturn(transaction) } val dispatcher: Dispatcher = object : Dispatcher() { @@ -236,7 +236,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -259,7 +259,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -279,7 +279,7 @@ class SentrySpanWebClientCustomizerTest { .retrieve() .bodyToMono(String::class.java) .block() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) @@ -302,7 +302,7 @@ class SentrySpanWebClientCustomizerTest { .block() } catch (e: Throwable) { } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(uri.toString(), it.data["url"]) diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt index 4b8a0c26be..13a207d8eb 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.boot.jakarta.it -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.checkEvent @@ -60,7 +60,7 @@ class SentrySpringIntegrationTest { lateinit var transport: ITransport @SpyBean - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -188,7 +188,7 @@ class SentrySpringIntegrationTest { restTemplate.getForEntity("http://localhost:$port/throws-handled", String::class.java) - verify(hub, never()).captureEvent(any()) + verify(scopes, never()).captureEvent(any()) } @Test diff --git a/sentry-spring-jakarta/api/sentry-spring-jakarta.api b/sentry-spring-jakarta/api/sentry-spring-jakarta.api index cebbd26b4f..13eb6033f9 100644 --- a/sentry-spring-jakarta/api/sentry-spring-jakarta.api +++ b/sentry-spring-jakarta/api/sentry-spring-jakarta.api @@ -22,7 +22,7 @@ public final class io/sentry/spring/jakarta/HttpServletRequestSentryUserProvider public class io/sentry/spring/jakarta/SentryExceptionResolver : org/springframework/core/Ordered, org/springframework/web/servlet/HandlerExceptionResolver { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;I)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;I)V protected fun createEvent (Ljakarta/servlet/http/HttpServletRequest;Ljava/lang/Exception;)Lio/sentry/SentryEvent; protected fun createHint (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;)Lio/sentry/Hint; public fun getOrder ()I @@ -47,14 +47,14 @@ public class io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor : } public class io/sentry/spring/jakarta/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Ljakarta/servlet/http/HttpServletRequest;)Lio/sentry/protocol/Request; } public class io/sentry/spring/jakarta/SentrySpringFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/SentryRequestResolver;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/SentryRequestResolver;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V } @@ -69,7 +69,7 @@ public final class io/sentry/spring/jakarta/SentryTaskDecorator : org/springfram } public class io/sentry/spring/jakarta/SentryUserFilter : org/springframework/web/filter/OncePerRequestFilter { - public fun (Lio/sentry/IHub;Ljava/util/List;)V + public fun (Lio/sentry/IScopes;Ljava/util/List;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V public fun getSentryUserProviders ()Ljava/util/List; } @@ -96,7 +96,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/checkin/Sent public class io/sentry/spring/jakarta/checkin/SentryCheckInAdvice : org/aopalliance/intercept/MethodInterceptor, org/springframework/context/EmbeddedValueResolverAware { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; public fun setEmbeddedValueResolver (Lorg/springframework/util/StringValueResolver;)V } @@ -127,7 +127,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/exception/Se public class io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -169,7 +169,7 @@ public final class io/sentry/spring/jakarta/graphql/SentryDataFetcherExceptionRe public final class io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public final class io/sentry/spring/jakarta/graphql/SentryGraphqlBeanPostProcessor : org/springframework/beans/factory/config/BeanPostProcessor, org/springframework/core/PriorityOrdered { @@ -188,7 +188,7 @@ public class io/sentry/spring/jakarta/graphql/SentryGraphqlConfiguration { public final class io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler { public fun ()V - public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; + public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object; } public class io/sentry/spring/jakarta/tracing/SentryAdviceConfiguration { @@ -207,18 +207,18 @@ public abstract interface annotation class io/sentry/spring/jakarta/tracing/Sent public class io/sentry/spring/jakarta/tracing/SentrySpanAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } public class io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor : org/springframework/http/client/ClientHttpRequestInterceptor { - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Z)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Z)V public fun intercept (Lorg/springframework/http/HttpRequest;[BLorg/springframework/http/client/ClientHttpRequestExecution;)Lorg/springframework/http/client/ClientHttpResponse; } public class io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter : org/springframework/web/reactive/function/client/ExchangeFilterFunction { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/reactive/function/client/ClientRequest;Lorg/springframework/web/reactive/function/client/ExchangeFunction;)Lreactor/core/publisher/Mono; } @@ -233,8 +233,8 @@ public class io/sentry/spring/jakarta/tracing/SentryTracingConfiguration { public class io/sentry/spring/jakarta/tracing/SentryTracingFilter : org/springframework/web/filter/OncePerRequestFilter { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;)V protected fun doFilterInternal (Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljakarta/servlet/FilterChain;)V } @@ -246,7 +246,7 @@ public abstract interface annotation class io/sentry/spring/jakarta/tracing/Sent public class io/sentry/spring/jakarta/tracing/SentryTransactionAdvice : org/aopalliance/intercept/MethodInterceptor { public fun ()V - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun invoke (Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object; } @@ -268,21 +268,22 @@ public abstract interface class io/sentry/spring/jakarta/tracing/TransactionName public abstract class io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter : org/springframework/web/server/WebFilter { public static final field SENTRY_HUB_KEY Ljava/lang/String; - public fun (Lio/sentry/IHub;)V - protected fun doFinally (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IHub;Lio/sentry/ITransaction;)V - protected fun doFirst (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IHub;)V + public static final field SENTRY_SCOPES_KEY Ljava/lang/String; + public fun (Lio/sentry/IScopes;)V + protected fun doFinally (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IScopes;Lio/sentry/ITransaction;)V + protected fun doFirst (Lorg/springframework/web/server/ServerWebExchange;Lio/sentry/IScopes;)V protected fun doOnError (Lio/sentry/ITransaction;Ljava/lang/Throwable;)V - protected fun maybeStartTransaction (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/ITransaction; - protected fun shouldTraceRequest (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Z - protected fun startTransaction (Lio/sentry/IHub;Lorg/springframework/http/server/reactive/ServerHttpRequest;Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; + protected fun maybeStartTransaction (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/ITransaction; + protected fun shouldTraceRequest (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;)Z + protected fun startTransaction (Lio/sentry/IScopes;Lorg/springframework/http/server/reactive/ServerHttpRequest;Lio/sentry/TransactionContext;)Lio/sentry/ITransaction; } public final class io/sentry/spring/jakarta/webflux/ReactorUtils { public fun ()V public static fun withSentry (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentry (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; - public static fun withSentryHub (Lreactor/core/publisher/Flux;Lio/sentry/IHub;)Lreactor/core/publisher/Flux; - public static fun withSentryHub (Lreactor/core/publisher/Mono;Lio/sentry/IHub;)Lreactor/core/publisher/Mono; + public static fun withSentryHub (Lreactor/core/publisher/Flux;Lio/sentry/IScopes;)Lreactor/core/publisher/Flux; + public static fun withSentryHub (Lreactor/core/publisher/Mono;Lio/sentry/IScopes;)Lreactor/core/publisher/Mono; public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; } @@ -290,16 +291,16 @@ public final class io/sentry/spring/jakarta/webflux/ReactorUtils { public final class io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor : io/micrometer/context/ThreadLocalAccessor { public static final field KEY Ljava/lang/String; public fun ()V - public fun getValue ()Lio/sentry/IHub; + public fun getValue ()Lio/sentry/IScopes; public synthetic fun getValue ()Ljava/lang/Object; public fun key ()Ljava/lang/Object; public fun reset ()V - public fun setValue (Lio/sentry/IHub;)V + public fun setValue (Lio/sentry/IScopes;)V public synthetic fun setValue (Ljava/lang/Object;)V } public class io/sentry/spring/jakarta/webflux/SentryRequestResolver { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun resolveSentryRequest (Lorg/springframework/http/server/reactive/ServerHttpRequest;)Lio/sentry/protocol/Request; } @@ -311,18 +312,18 @@ public final class io/sentry/spring/jakarta/webflux/SentryScheduleHook : java/ut public final class io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler : org/springframework/web/server/WebExceptionHandler { public static final field MECHANISM_TYPE Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun handle (Lorg/springframework/web/server/ServerWebExchange;Ljava/lang/Throwable;)Lreactor/core/publisher/Mono; } public class io/sentry/spring/jakarta/webflux/SentryWebFilter : io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter { - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } public final class io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor : io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter { public static final field TRACE_ORIGIN Ljava/lang/String; - public fun (Lio/sentry/IHub;)V + public fun (Lio/sentry/IScopes;)V public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java index 1395e035a1..e8cd91f3f4 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/EnableSentry.java @@ -12,7 +12,7 @@ * *

    *
  • creates bean of type {@link io.sentry.SentryOptions} - *
  • registers {@link io.sentry.IHub} for sending Sentry events + *
  • registers {@link io.sentry.IScopes} for sending Sentry events *
  • registers {@link SentryExceptionResolver} to send Sentry event for any uncaught exception * in Spring MVC flow. *
diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java index 6d4098d624..efa98ef581 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryExceptionResolver.java @@ -5,7 +5,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -29,15 +29,15 @@ public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered { public static final String MECHANISM_TYPE = "Spring6ExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull TransactionNameProvider transactionNameProvider; private final int order; public SentryExceptionResolver( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider, final int order) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); this.order = order; @@ -53,7 +53,7 @@ public SentryExceptionResolver( final SentryEvent event = createEvent(request, ex); final Hint hint = createHint(request, response); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); // null = run other HandlerExceptionResolvers to actually handle the exception return null; diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java index 4a8789c812..9598f0c926 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryHubRegistrar.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; +import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; import io.sentry.protocol.SdkVersion; @@ -60,7 +60,7 @@ private void registerSentryOptions( private void registerSentryHubBean(final @NotNull BeanDefinitionRegistry registry) { final BeanDefinitionBuilder builder = - BeanDefinitionBuilder.genericBeanDefinition(HubAdapter.class); + BeanDefinitionBuilder.genericBeanDefinition(ScopesAdapter.class); builder.setInitMethodName("getInstance"); registry.registerBeanDefinition("sentryHub", builder.getBeanDefinition()); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java index 9937bfbf1a..d33dfca8d8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java @@ -2,10 +2,10 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.EventProcessor; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransportFactory; import io.sentry.Integration; +import io.sentry.ScopesAdapter; import io.sentry.Sentry; import io.sentry.SentryOptions; import io.sentry.SentryOptions.TracesSamplerCallback; @@ -27,15 +27,15 @@ public class SentryInitBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, DisposableBean { private @Nullable ApplicationContext applicationContext; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryInitBeanPostProcessor() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - SentryInitBeanPostProcessor(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.hub = hub; + SentryInitBeanPostProcessor(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "Scopes are required"); + this.scopes = scopes; } @Override @@ -86,6 +86,6 @@ public void setApplicationContext(final @NotNull ApplicationContext applicationC @Override public void destroy() { - hub.close(); + scopes.close(); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java index e1bc45ac64..71f9079f9f 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryLevel; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; @@ -20,11 +20,11 @@ @Open public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private volatile @Nullable List extraSecurityCookies; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "options is required"); } // httpRequest.getRequestURL() returns StringBuffer which is considered an obsolete class. @@ -40,7 +40,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { extractSecurityCookieNamesOrUseCached(httpRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest, additionalSecurityCookieNames)); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String cookieName = HttpUtils.COOKIE_HEADER_NAME; final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( @@ -57,7 +57,8 @@ Map resolveHeadersMap( final Map headersMap = new HashMap<>(); for (String headerName : Collections.list(request.getHeaderNames())) { // do not copy personal information identifiable headers - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { final @Nullable List filteredHeaders = HttpUtils.filterOutSecurityCookiesFromHeader( request.getHeaders(headerName), headerName, additionalSecurityCookieNames); @@ -94,7 +95,8 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to extract session cookie name from request.", t); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index 129ea739d6..be06d3d253 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -8,8 +8,8 @@ import io.sentry.Breadcrumb; import io.sentry.EventProcessor; import io.sentry.Hint; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -29,26 +29,26 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; public SentrySpringFilter( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentrySpringFilter(final @NotNull IHub hub) { - this(hub, new SentryRequestResolver(hub), new SpringMvcTransactionNameProvider()); + public SentrySpringFilter(final @NotNull IScopes scopes) { + this(scopes, new SentryRequestResolver(scopes), new SpringMvcTransactionNameProvider()); } public SentrySpringFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } @Override @@ -57,20 +57,20 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - hub.pushScope(); + scopes.pushScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - hub.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); configureScope(request); filterChain.doFilter(request, response); } finally { - hub.popScope(); + scopes.popScope(); } } else { filterChain.doFilter(servletRequest, response); @@ -79,7 +79,7 @@ protected void doFilterInternal( private void configureScope(HttpServletRequest request) { try { - hub.configureScope( + scopes.configureScope( scope -> { // set basic request information on the scope scope.setRequest(requestResolver.resolveSentryRequest(request)); @@ -92,11 +92,12 @@ private void configureScope(HttpServletRequest request) { // request processing if (request instanceof CachedBodyHttpServletRequest) { scope.addEventProcessor( - new RequestBodyExtractingEventProcessor(request, hub.getOptions())); + new RequestBodyExtractingEventProcessor(request, scopes.getOptions())); } }); } catch (Throwable e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log(SentryLevel.ERROR, "Failed to set scope for HTTP request", e); } @@ -104,12 +105,13 @@ private void configureScope(HttpServletRequest request) { private @NotNull HttpServletRequest resolveHttpServletRequest( final @NotNull HttpServletRequest request) { - if (hub.getOptions().isSendDefaultPii() - && qualifiesForCaching(request, hub.getOptions().getMaxRequestBodySize())) { + if (scopes.getOptions().isSendDefaultPii() + && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { return new CachedBodyHttpServletRequest(request); } catch (IOException e) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java index 5c4f37e521..c99abf3e21 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -8,22 +8,24 @@ import org.springframework.scheduling.annotation.Async; /** - * Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate - * the current {@link IHub} on the thread executing async task - like MVC controller methods - * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. + * Sets a current scopes on a thread running a {@link Runnable} given by parameter. Used to + * propagate the current {@link IScopes} on the thread executing async task - like MVC controller + * methods returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override + @SuppressWarnings("deprecation") public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java index f7b8bc62d6..31cc73a346 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryUserFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.IpAddressUtils; import io.sentry.protocol.User; import io.sentry.util.Objects; @@ -26,12 +26,12 @@ */ @Open public class SentryUserFilter extends OncePerRequestFilter { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull List sentryUserProviders; public SentryUserFilter( - final @NotNull IHub hub, final @NotNull List sentryUserProviders) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull List sentryUserProviders) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); this.sentryUserProviders = Objects.requireNonNull(sentryUserProviders, "sentryUserProviders list is required"); } @@ -46,13 +46,13 @@ protected void doFilterInternal( for (final SentryUserProvider provider : sentryUserProviders) { apply(user, provider.provideUser()); } - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { if (IpAddressUtils.isDefault(user.getIpAddress())) { // unset {{auto}} as it would set the server's ip address as a user ip address user.setIpAddress(null); } } - hub.setUser(user); + scopes.setUser(user); chain.doFilter(request, response); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index 5a4e329fa8..4a366a8b01 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -4,8 +4,8 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.DateUtils; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; @@ -30,16 +30,16 @@ @ApiStatus.Experimental @Open public class SentryCheckInAdvice implements MethodInterceptor, EmbeddedValueResolverAware { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private @Nullable StringValueResolver resolver; public SentryCheckInAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCheckInAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCheckInAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -66,7 +66,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl // expressions. Testing shows this can also happen if properties cannot be resolved (without // an exception being thrown). Sentry should alert the user about missed checkins in this // case since the monitor slug won't match what is configured in Sentry. - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -76,7 +77,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } if (ObjectUtils.isEmpty(monitorSlug)) { - hub.getOptions() + scopes + .getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -84,8 +86,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - hub.pushScope(); - TracingUtils.startNewTrace(hub); + scopes.pushScope(); + TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); @@ -93,7 +95,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { if (!isHeartbeatOnly) { - checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); } return invocation.proceed(); } catch (Throwable e) { @@ -103,8 +105,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - hub.captureCheckIn(checkIn); - hub.popScope(); + scopes.captureCheckIn(checkIn); + scopes.popScope(); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java index f989382045..c6537f853c 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.exception; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; +import io.sentry.ScopesAdapter; import io.sentry.exception.ExceptionMechanismException; import io.sentry.protocol.Mechanism; import io.sentry.util.Objects; @@ -22,14 +22,14 @@ @Open public class SentryCaptureExceptionParameterAdvice implements MethodInterceptor { private static final String MECHANISM_TYPE = "SentrySpring6CaptureExceptionParameterAdvice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryCaptureExceptionParameterAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override @@ -58,6 +58,6 @@ private void captureException(final @NotNull Throwable throwable) { mechanism.setHandled(true); final Throwable mechanismException = new ExceptionMechanismException(mechanism, throwable, Thread.currentThread()); - hub.captureException(mechanismException); + scopes.captureException(mechanismException); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java index 1d5576c596..3e2223b694 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryBatchLoaderRegistry.java @@ -1,11 +1,11 @@ package io.sentry.spring.jakarta.graphql; -import static io.sentry.graphql.SentryInstrumentation.SENTRY_HUB_CONTEXT_KEY; +import static io.sentry.graphql.SentryInstrumentation.SENTRY_SCOPES_CONTEXT_KEY; import graphql.GraphQLContext; import io.sentry.Breadcrumb; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import java.util.List; import java.util.Map; import java.util.Set; @@ -89,7 +89,7 @@ public BatchLoaderRegistry.RegistrationSpec withOptions(DataLoaderOptions public void registerBatchLoader(BiFunction, BatchLoaderEnvironment, Flux> loader) { delegate.registerBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); @@ -100,20 +100,20 @@ public void registerMappedBatchLoader( BiFunction, BatchLoaderEnvironment, Mono>> loader) { delegate.registerMappedBatchLoader( (keys, batchLoaderEnvironment) -> { - hubFromContext(batchLoaderEnvironment) + scopesFromContext(batchLoaderEnvironment) .addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name)); return loader.apply(keys, batchLoaderEnvironment); }); } - private @NotNull IHub hubFromContext(final @NotNull BatchLoaderEnvironment environment) { + private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) { Object context = environment.getContext(); if (context instanceof GraphQLContext) { GraphQLContext graphqlContext = (GraphQLContext) context; - return graphqlContext.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance()); + return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance()); } - return NoOpHub.getInstance(); + return NoOpScopes.getInstance(); } } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java index 83a090954d..a7a6cccd3e 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentryDgsSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; @@ -17,7 +17,7 @@ public SentryDgsSubscriptionHandler() { @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -25,7 +25,7 @@ public SentryDgsSubscriptionHandler() { return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); exceptionReporter.captureThrowable(throwable, exceptionDetails, null); }); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java index 4c51981035..eec86f5e8b 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandler.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.graphql; import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.graphql.ExceptionReporter; import io.sentry.graphql.SentrySubscriptionHandler; import org.jetbrains.annotations.NotNull; @@ -13,7 +13,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription @Override public @NotNull Object onSubscriptionResult( final @NotNull Object result, - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ExceptionReporter exceptionReporter, final @NotNull InstrumentationFieldFetchParameters parameters) { if (result instanceof Flux) { @@ -21,7 +21,7 @@ public final class SentrySpringSubscriptionHandler implements SentrySubscription return flux.doOnError( throwable -> { final @NotNull ExceptionReporter.ExceptionDetails exceptionDetails = - new ExceptionReporter.ExceptionDetails(hub, parameters.getEnvironment(), true); + new ExceptionReporter.ExceptionDetails(scopes, parameters.getEnvironment(), true); if (throwable instanceof SubscriptionPublisherException && throwable.getCause() != null) { exceptionReporter.captureThrowable(throwable.getCause(), exceptionDetails, null); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java index 5f345a4e02..e8de36487f 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.jakarta.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.util.Objects; import java.lang.reflect.Method; @@ -22,20 +22,20 @@ @Open public class SentrySpanAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring_jakarta.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentrySpanAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentrySpanAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @Override public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null || activeSpan.isNoOp()) { // there is no active transaction, we do not start new span diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java index 59c1926ff3..7a787fb29d 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientHttpRequestInterceptor.java @@ -8,7 +8,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -28,16 +28,16 @@ public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private static final String TRACE_ORIGIN_REST_TEMPLATE = "auto.http.spring_jakarta.resttemplate"; private static final String TRACE_ORIGIN_REST_CLIENT = "auto.http.spring_jakarta.restclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; private final @NotNull String traceOrigin; - public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { - this(hub, true); + public SentrySpanClientHttpRequestInterceptor(final @NotNull IScopes scopes) { + this(scopes, true); } public SentrySpanClientHttpRequestInterceptor( - final @NotNull IHub hub, final @NotNull boolean isRestTemplate) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, final @NotNull boolean isRestTemplate) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.traceOrigin = isRestTemplate ? TRACE_ORIGIN_REST_TEMPLATE : TRACE_ORIGIN_REST_CLIENT; } @@ -50,7 +50,7 @@ public SentrySpanClientHttpRequestInterceptor( Integer responseStatusCode = null; ClientHttpResponse response = null; try { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { maybeAddTracingHeaders(request, null); return execution.execute(request, body); @@ -91,7 +91,7 @@ private void maybeAddTracingHeaders( final @NotNull HttpRequest request, final @Nullable ISpan span) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.getURI().toString(), request.getHeaders().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -128,6 +128,6 @@ private void addBreadcrumb( hint.set(SPRING_REQUEST_INTERCEPTOR_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java index 4ac511721e..bc5c0edfab 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentrySpanClientWebRequestFilter.java @@ -7,7 +7,7 @@ import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ISpan; import io.sentry.SpanDataConvention; import io.sentry.SpanStatus; @@ -25,16 +25,16 @@ @Open public class SentrySpanClientWebRequestFilter implements ExchangeFilterFunction { private static final String TRACE_ORIGIN = "auto.http.spring_jakarta.webclient"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentrySpanClientWebRequestFilter(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); } @Override public @NotNull Mono filter( final @NotNull ClientRequest request, final @NotNull ExchangeFunction next) { - final ISpan activeSpan = hub.getSpan(); + final ISpan activeSpan = scopes.getSpan(); if (activeSpan == null) { final @NotNull ClientRequest modifiedRequest = maybeAddTracingHeaders(request, null); addBreadcrumb(modifiedRequest, null); @@ -74,7 +74,7 @@ public SentrySpanClientWebRequestFilter(final @NotNull IHub hub) { final @Nullable TracingUtils.TracingHeaders tracingHeaders = TracingUtils.traceIfAllowed( - hub, + scopes, request.url().toString(), request.headers().get(BaggageHeader.BAGGAGE_HEADER), span); @@ -111,6 +111,6 @@ private void addBreadcrumb( hint.set(SPRING_EXCHANGE_FILTER_RESPONSE, response); } - hub.addBreadcrumb(breadcrumb, hint); + scopes.addBreadcrumb(breadcrumb, hint); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java index ed91f85a29..097528ac44 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java @@ -3,9 +3,9 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.BaggageHeader; import io.sentry.CustomSamplingContext; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; import io.sentry.TransactionContext; @@ -38,7 +38,7 @@ public class SentryTracingFilter extends OncePerRequestFilter { private static final String TRACE_ORIGIN = "auto.http.spring_jakarta.webmvc"; private final @NotNull TransactionNameProvider transactionNameProvider; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; /** * Creates filter that resolves transaction name using {@link SpringMvcTransactionNameProvider}. @@ -49,25 +49,26 @@ public class SentryTracingFilter extends OncePerRequestFilter { * jakarta.servlet.Filter}. */ public SentryTracingFilter() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } /** * Creates filter that resolves transaction name using transaction name provider given by * parameter. * - * @param hub - the hub + * @param scopes - the scopes * @param transactionNameProvider - transaction name provider. */ public SentryTracingFilter( - final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + final @NotNull IScopes scopes, + final @NotNull TransactionNameProvider transactionNameProvider) { + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } - public SentryTracingFilter(final @NotNull IHub hub) { - this(hub, new SpringMvcTransactionNameProvider()); + public SentryTracingFilter(final @NotNull IScopes scopes) { + this(scopes, new SpringMvcTransactionNameProvider()); } @Override @@ -76,14 +77,14 @@ protected void doFilterInternal( final @NotNull HttpServletResponse httpResponse, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (hub.isEnabled()) { + if (scopes.isEnabled()) { final @Nullable String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER)); final @Nullable TransactionContext transactionContext = - hub.continueTrace(sentryTraceHeader, baggageHeader); - if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { + scopes.continueTrace(sentryTraceHeader, baggageHeader); + if (scopes.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) { doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext); } else { filterChain.doFilter(httpRequest, httpResponse); @@ -130,7 +131,7 @@ private void doFilterWithTransaction( } private boolean shouldTraceRequest(final @NotNull HttpServletRequest request) { - return hub.getOptions().isTraceOptionsRequests() + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.name().equals(request.getMethod()); } @@ -152,14 +153,14 @@ private ITransaction startTransaction( transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setCustomSamplingContext(customSamplingContext); transactionOptions.setBindToScope(true); - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, "http.server"), transactionOptions); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java index 5b264defa4..f04b3dd7a6 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java @@ -1,9 +1,9 @@ package io.sentry.spring.jakarta.tracing; import com.jakewharton.nopen.annotation.Open; -import io.sentry.HubAdapter; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.ITransaction; +import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; import io.sentry.TransactionContext; import io.sentry.TransactionOptions; @@ -28,14 +28,14 @@ public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring_jakarta.advice"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; public SentryTransactionAdvice() { - this(HubAdapter.getInstance()); + this(ScopesAdapter.getInstance()); } - public SentryTransactionAdvice(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryTransactionAdvice(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -68,11 +68,11 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - hub.pushScope(); + scopes.pushScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = - hub.startTransaction( + scopes.startTransaction( new TransactionContext(nameAndSource.name, nameAndSource.source, operation), transactionOptions); transaction.getSpanContext().setOrigin(TRACE_ORIGIN); @@ -86,7 +86,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - hub.popScope(); + scopes.popScope(); } } } @@ -106,7 +106,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } private boolean isTransactionActive() { - return hub.getSpan() != null; + return scopes.getSpan() != null; } private static class TransactionNameAndSource { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java index bf25aa5f49..3321874dd8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java @@ -7,10 +7,10 @@ import io.sentry.Breadcrumb; import io.sentry.CustomSamplingContext; import io.sentry.Hint; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; -import io.sentry.NoOpHub; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import io.sentry.SentryTraceHeader; import io.sentry.SpanStatus; @@ -34,16 +34,17 @@ @ApiStatus.Experimental public abstract class AbstractSentryWebFilter implements WebFilter { private final @NotNull SentryRequestResolver sentryRequestResolver; - public static final String SENTRY_HUB_KEY = "sentry-hub"; + public static final String SENTRY_SCOPES_KEY = "sentry-scopes"; + @Deprecated public static final String SENTRY_HUB_KEY = SENTRY_SCOPES_KEY; private static final String TRANSACTION_OP = "http.server"; - public AbstractSentryWebFilter(final @NotNull IHub hub) { - Objects.requireNonNull(hub, "hub is required"); - this.sentryRequestResolver = new SentryRequestResolver(hub); + public AbstractSentryWebFilter(final @NotNull IScopes scopes) { + Objects.requireNonNull(scopes, "scopes are required"); + this.sentryRequestResolver = new SentryRequestResolver(scopes); } protected @Nullable ITransaction maybeStartTransaction( - final @NotNull IHub requestHub, final @NotNull ServerHttpRequest request) { + final @NotNull IScopes requestHub, final @NotNull ServerHttpRequest request) { if (requestHub.isEnabled()) { final @NotNull HttpHeaders headers = request.getHeaders(); final @Nullable String sentryTraceHeader = @@ -62,21 +63,23 @@ public AbstractSentryWebFilter(final @NotNull IHub hub) { protected void doFinally( final @NotNull ServerWebExchange serverWebExchange, - final @NotNull IHub requestHub, + final @NotNull IScopes requestHub, final @Nullable ITransaction transaction) { if (transaction != null) { finishTransaction(serverWebExchange, transaction); } if (requestHub.isEnabled()) { + // TODO close lifecycle token instead of popscope requestHub.popScope(); } - Sentry.setCurrentHub(NoOpHub.getInstance()); + Sentry.setCurrentScopes(NoOpScopes.getInstance()); } protected void doFirst( - final @NotNull ServerWebExchange serverWebExchange, final @NotNull IHub requestHub) { + final @NotNull ServerWebExchange serverWebExchange, final @NotNull IScopes requestHub) { if (requestHub.isEnabled()) { - serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); + // TODO fork instead requestHub.pushScope(); final ServerHttpRequest request = serverWebExchange.getRequest(); final ServerHttpResponse response = serverWebExchange.getResponse(); @@ -100,8 +103,8 @@ protected void doOnError(final @Nullable ITransaction transaction, final @NotNul } protected boolean shouldTraceRequest( - final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { - return hub.getOptions().isTraceOptionsRequests() + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request) { + return scopes.getOptions().isTraceOptionsRequests() || !HttpMethod.OPTIONS.equals(request.getMethod()); } @@ -130,7 +133,7 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact } protected @NotNull ITransaction startTransaction( - final @NotNull IHub hub, + final @NotNull IScopes scopes, final @NotNull ServerHttpRequest request, final @Nullable TransactionContext transactionContext) { final @NotNull String name = request.getMethod() + " " + request.getURI().getPath(); @@ -146,10 +149,10 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact transactionContext.setTransactionNameSource(TransactionNameSource.URL); transactionContext.setOperation(TRANSACTION_OP); - return hub.startTransaction(transactionContext, transactionOptions); + return scopes.startTransaction(transactionContext, transactionOptions); } - return hub.startTransaction( + return scopes.startTransaction( new TransactionContext(name, TransactionNameSource.URL, TRANSACTION_OP), transactionOptions); } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java index 4af641af29..41dd2e4bc0 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -8,11 +8,12 @@ import reactor.core.publisher.Mono; import reactor.util.context.Context; +// TODO deprecate and replace with "withSentryScopes" etc. @ApiStatus.Experimental public final class ReactorUtils { /** - * Writes the current Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the current Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -20,14 +21,16 @@ public final class ReactorUtils { * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental + @SuppressWarnings("deprecation") public static Mono withSentry(final @NotNull Mono mono) { - final @NotNull IHub oldHub = Sentry.getCurrentHub(); - final @NotNull IHub clonedHub = oldHub.clone(); + final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); + // TODO fork + final @NotNull IScopes clonedHub = oldHub.clone(); return withSentryHub(mono, clonedHub); } /** - * Writes a new Sentry {@link IHub} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -36,12 +39,12 @@ public static Mono withSentry(final @NotNull Mono mono) { */ @ApiStatus.Experimental public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) { - final @NotNull IHub hub = Sentry.cloneMainHub(); + final @NotNull IScopes hub = Sentry.cloneMainHub(); return withSentryHub(mono, hub); } /** - * Writes the given Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the given Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -49,7 +52,7 @@ public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental - public static Mono withSentryHub(final @NotNull Mono mono, final @NotNull IHub hub) { + public static Mono withSentryHub(final @NotNull Mono mono, final @NotNull IScopes hub) { /** * WARNING: Cannot set the hub as current. It would be used by others to clone again causing * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in @@ -62,7 +65,7 @@ public static Mono withSentryHub(final @NotNull Mono mono, final @NotN } /** - * Writes the current Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the current Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -70,15 +73,17 @@ public static Mono withSentryHub(final @NotNull Mono mono, final @NotN * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental + @SuppressWarnings("deprecation") public static Flux withSentry(final @NotNull Flux flux) { - final @NotNull IHub oldHub = Sentry.getCurrentHub(); - final @NotNull IHub clonedHub = oldHub.clone(); + final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); + // TODO fork + final @NotNull IScopes clonedHub = oldHub.clone(); return withSentryHub(flux, clonedHub); } /** - * Writes a new Sentry {@link IHub} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -87,12 +92,12 @@ public static Flux withSentry(final @NotNull Flux flux) { */ @ApiStatus.Experimental public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) { - final @NotNull IHub hub = Sentry.cloneMainHub(); + final @NotNull IScopes hub = Sentry.cloneMainHub(); return withSentryHub(flux, hub); } /** - * Writes the given Sentry {@link IHub} to the {@link Context} and uses {@link + * Writes the given Sentry {@link IScopes} to the {@link Context} and uses {@link * io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -100,7 +105,7 @@ public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ @ApiStatus.Experimental - public static Flux withSentryHub(final @NotNull Flux flux, final @NotNull IHub hub) { + public static Flux withSentryHub(final @NotNull Flux flux, final @NotNull IScopes hub) { /** * WARNING: Cannot set the hub as current. It would be used by others to clone again causing * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java index c64cf3d634..9b7e51db73 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java @@ -1,15 +1,15 @@ package io.sentry.spring.jakarta.webflux; import io.micrometer.context.ThreadLocalAccessor; -import io.sentry.IHub; -import io.sentry.NoOpHub; +import io.sentry.IScopes; +import io.sentry.NoOpScopes; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @ApiStatus.Experimental -public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor { +public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor { - public static final String KEY = "sentry-hub"; + public static final String KEY = "sentry-scopes"; @Override public Object key() { @@ -17,18 +17,18 @@ public Object key() { } @Override - public IHub getValue() { - return Sentry.getCurrentHub(); + public IScopes getValue() { + return Sentry.getCurrentScopes(); } @Override - public void setValue(IHub value) { - Sentry.setCurrentHub(value); + public void setValue(IScopes value) { + Sentry.setCurrentScopes(value); } @Override @SuppressWarnings("deprecation") public void reset() { - Sentry.setCurrentHub(NoOpHub.getInstance()); + Sentry.setCurrentScopes(NoOpScopes.getInstance()); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java index 2f41ba93ca..d58291ade6 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryRequestResolver.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.protocol.Request; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; @@ -20,10 +20,10 @@ @Open @ApiStatus.Experimental public class SentryRequestResolver { - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryRequestResolver(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "options is required"); + public SentryRequestResolver(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } public @NotNull Request resolveSentryRequest(final @NotNull ServerHttpRequest httpRequest) { @@ -36,7 +36,7 @@ public SentryRequestResolver(final @NotNull IHub hub) { urlDetails.applyToRequest(sentryRequest); sentryRequest.setHeaders(resolveHeadersMap(httpRequest.getHeaders())); - if (hub.getOptions().isSendDefaultPii()) { + if (scopes.getOptions().isSendDefaultPii()) { String headerName = HttpUtils.COOKIE_HEADER_NAME; sentryRequest.setCookies( toString( @@ -52,7 +52,8 @@ Map resolveHeadersMap(final HttpHeaders request) { for (Map.Entry> entry : request.entrySet()) { // do not copy personal information identifiable headers String headerName = entry.getKey(); - if (hub.getOptions().isSendDefaultPii() || !HttpUtils.containsSensitiveHeader(headerName)) { + if (scopes.getOptions().isSendDefaultPii() + || !HttpUtils.containsSensitiveHeader(headerName)) { headersMap.put( headerName, toString( diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java index 2bf27f246d..882a0b268a 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -13,16 +13,18 @@ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override + @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - final IHub newHub = Sentry.getCurrentHub().clone(); + // TODO fork instead + final IScopes newHub = Sentry.getCurrentScopes().clone(); return () -> { - final IHub oldState = Sentry.getCurrentHub(); - Sentry.setCurrentHub(newHub); + final IScopes oldState = Sentry.getCurrentScopes(); + Sentry.setCurrentScopes(newHub); try { runnable.run(); } finally { - Sentry.setCurrentHub(oldState); + Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java index 24439cd0e9..40b0ed4e87 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java @@ -5,7 +5,7 @@ import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_RESPONSE; import io.sentry.Hint; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.exception.ExceptionMechanismException; @@ -27,18 +27,18 @@ @ApiStatus.Experimental public final class SentryWebExceptionHandler implements WebExceptionHandler { public static final String MECHANISM_TYPE = "Spring6WebFluxExceptionResolver"; - private final @NotNull IHub hub; + private final @NotNull IScopes scopes; - public SentryWebExceptionHandler(final @NotNull IHub hub) { - this.hub = Objects.requireNonNull(hub, "hub is required"); + public SentryWebExceptionHandler(final @NotNull IScopes scopes) { + this.scopes = Objects.requireNonNull(scopes, "scopes are required"); } @Override public @NotNull Mono handle( final @NotNull ServerWebExchange serverWebExchange, final @NotNull Throwable ex) { - final @Nullable IHub requestHub = - serverWebExchange.getAttributeOrDefault(SentryWebFilter.SENTRY_HUB_KEY, null); - final @NotNull IHub hubToUse = requestHub != null ? requestHub : hub; + final @Nullable IScopes requestScopes = + serverWebExchange.getAttributeOrDefault(SentryWebFilter.SENTRY_SCOPES_KEY, null); + final @NotNull IScopes scopesToUse = requestScopes != null ? requestScopes : scopes; return ReactorUtils.withSentryHub( Mono.just(ex) @@ -61,12 +61,12 @@ public SentryWebExceptionHandler(final @NotNull IHub hub) { WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse()); hint.set(WEBFLUX_EXCEPTION_HANDLER_EXCHANGE, serverWebExchange); - hub.captureEvent(event, hint); + scopes.captureEvent(event, hint); } return it; }), - hubToUse) + scopesToUse) .flatMap(it -> Mono.error(ex)); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java index a57a389499..dab985eecf 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.webflux; import com.jakewharton.nopen.annotation.Open; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @@ -20,28 +20,28 @@ public class SentryWebFilter extends AbstractSentryWebFilter { private static final String TRACE_ORIGIN = "auto.spring_jakarta.webflux"; - public SentryWebFilter(final @NotNull IHub hub) { - super(hub); + public SentryWebFilter(final @NotNull IScopes scopes) { + super(scopes); } @Override public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IHub requestHub = Sentry.cloneMainHub(); + @NotNull IScopes requestScopes = Sentry.cloneMainHub(); final ServerHttpRequest request = serverWebExchange.getRequest(); - final @Nullable ITransaction transaction = maybeStartTransaction(requestHub, request); + final @Nullable ITransaction transaction = maybeStartTransaction(requestScopes, request); if (transaction != null) { transaction.getSpanContext().setOrigin(TRACE_ORIGIN); } return webFilterChain .filter(serverWebExchange) - .doFinally(__ -> doFinally(serverWebExchange, requestHub, transaction)) + .doFinally(__ -> doFinally(serverWebExchange, requestScopes, transaction)) .doOnError(e -> doOnError(transaction, e)) .doFirst( () -> { - Sentry.setCurrentHub(requestHub); - doFirst(serverWebExchange, requestHub); + Sentry.setCurrentScopes(requestScopes); + doFirst(serverWebExchange, requestScopes); }); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java index 278c2b8e7e..e760ef8f3e 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.webflux; -import io.sentry.IHub; import io.sentry.IScope; +import io.sentry.IScopes; import io.sentry.ITransaction; import io.sentry.Sentry; import org.jetbrains.annotations.ApiStatus; @@ -17,8 +17,8 @@ public final class SentryWebFilterWithThreadLocalAccessor extends AbstractSentry public static final String TRACE_ORIGIN = "auto.spring_jakarta.webflux"; - public SentryWebFilterWithThreadLocalAccessor(final @NotNull IHub hub) { - super(hub); + public SentryWebFilterWithThreadLocalAccessor(final @NotNull IScopes scopes) { + super(scopes); } @Override @@ -33,14 +33,15 @@ public Mono filter( __ -> doFinally( serverWebExchange, - Sentry.getCurrentHub(), + Sentry.getCurrentScopes(), transactionContainer.transaction)) .doOnError(e -> doOnError(transactionContainer.transaction, e)) .doFirst( () -> { - doFirst(serverWebExchange, Sentry.getCurrentHub()); + doFirst(serverWebExchange, Sentry.getCurrentScopes()); final ITransaction transaction = - maybeStartTransaction(Sentry.getCurrentHub(), serverWebExchange.getRequest()); + maybeStartTransaction( + Sentry.getCurrentScopes(), serverWebExchange.getRequest()); transactionContainer.transaction = transaction; if (transaction != null) { transaction.getSpanContext().setOrigin(TRACE_ORIGIN); diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt index 7b51bbc1e7..37853c3101 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/EnableSentryTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.EventProcessor -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Integration import io.sentry.Sentry @@ -67,7 +67,7 @@ class EnableSentryTest { @Test fun `creates Sentry Hub`() { contextRunner.run { - assertThat(it).hasSingleBean(IHub::class.java) + assertThat(it).hasSingleBean(IScopes::class.java) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt index 05b97ba24c..5d093f50f1 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt @@ -2,7 +2,7 @@ package io.sentry.spring.jakarta import io.sentry.CheckIn import io.sentry.CheckInStatus -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -54,19 +54,19 @@ class SentryCheckInAdviceTest { lateinit var sampleServiceSpringProperties: SampleServiceSpringProperties @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.options).thenReturn(SentryOptions()) + reset(scopes) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleService.hello() assertEquals(1, result) assertEquals(2, checkInCaptor.allValues.size) @@ -79,17 +79,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn, every method call creates two check-ins error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleService.oops() } @@ -103,17 +103,17 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub, times(2)).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes, times(2)).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceHeartbeat.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -123,17 +123,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) assertThrows { sampleServiceHeartbeat.oops() } @@ -144,31 +144,31 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when method is annotated with @SentryCheckIn but slug is missing, does not create check-in`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(hub, never()).pushScope() - verify(hub, never()).captureCheckIn(any()) - verify(hub, never()).popScope() + verify(scopes, never()).pushScope() + verify(scopes, never()).captureCheckIn(any()) + verify(scopes, never()).popScope() } @Test fun `when @SentryCheckIn is passed a spring property it is resolved correctly`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.hello() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -178,17 +178,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that does not exist, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloUnresolvedProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -198,17 +198,17 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Test fun `when @SentryCheckIn is passed a spring property that causes an exception, raw value is used`() { val checkInId = SentryId() val checkInCaptor = argumentCaptor() - whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) + whenever(scopes.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId) val result = sampleServiceSpringProperties.helloExceptionProperty() assertEquals(1, result) assertEquals(1, checkInCaptor.allValues.size) @@ -218,10 +218,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(hub) - order.verify(hub).pushScope() - order.verify(hub).captureCheckIn(any()) - order.verify(hub).popScope() + val order = inOrder(scopes) + order.verify(scopes).pushScope() + order.verify(scopes).captureCheckIn(any()) + order.verify(scopes).popScope() } @Configuration @@ -242,10 +242,10 @@ class SentryCheckInAdviceTest { open fun sampleServiceSpringProperties() = SampleServiceSpringProperties() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } companion object { diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt index 797d5aa5ac..431137aabd 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryExceptionResolverTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryLevel import io.sentry.exception.ExceptionMechanismException @@ -17,7 +17,7 @@ import org.mockito.kotlin.whenever import kotlin.test.Test class SentryExceptionResolverTest { - private val hub = mock() + private val scopes = mock() private val transactionNameProvider = mock() private val request = mock() @@ -26,10 +26,10 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets wrapped exception for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) val expectedCause = RuntimeException("test") - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, expectedCause) assertThat(eventCaptor.firstValue.throwable).isEqualTo(expectedCause) @@ -46,9 +46,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, sets fatal level for event`() { val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.level).isEqualTo(SentryLevel.FATAL) @@ -59,9 +59,9 @@ class SentryExceptionResolverTest { val expectedTransactionName = "test-transaction" whenever(transactionNameProvider.provideTransactionName(any())).thenReturn(expectedTransactionName) val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) assertThat(eventCaptor.firstValue.transaction).isEqualTo(expectedTransactionName) @@ -71,9 +71,9 @@ class SentryExceptionResolverTest { @Test fun `when handles exception, provides spring resolver hint`() { val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - SentryExceptionResolver(hub, transactionNameProvider, 1) + SentryExceptionResolver(scopes, transactionNameProvider, 1) .resolveException(request, response, null, RuntimeException("test")) with(hintCaptor.firstValue) { @@ -86,8 +86,8 @@ class SentryExceptionResolverTest { fun `when custom create event method provided, uses it to capture event`() { val expectedEvent = SentryEvent() val eventCaptor = argumentCaptor() - whenever(hub.captureEvent(eventCaptor.capture(), any())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(eventCaptor.capture(), any())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createEvent(request: HttpServletRequest, ex: Exception) = expectedEvent } @@ -100,8 +100,8 @@ class SentryExceptionResolverTest { fun `when custom create hint method provided, uses it to capture event`() { val expectedHint = Hint() val hintCaptor = argumentCaptor() - whenever(hub.captureEvent(any(), hintCaptor.capture())).thenReturn(null) - val resolver = object : SentryExceptionResolver(hub, transactionNameProvider, 1) { + whenever(scopes.captureEvent(any(), hintCaptor.capture())).thenReturn(null) + val resolver = object : SentryExceptionResolver(scopes, transactionNameProvider, 1) { override fun createHint(request: HttpServletRequest, response: HttpServletResponse) = expectedHint } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt index 4168605823..54b6acb0f3 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryInitBeanPostProcessorTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta -import io.sentry.IHub +import io.sentry.IScopes import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -13,18 +13,18 @@ class SentryInitBeanPostProcessorTest { @Test fun closesSentryOnApplicationContextDestroy() { val ctx = AnnotationConfigApplicationContext(TestConfig::class.java) - val hub = ctx.getBean(IHub::class.java) + val scopes = ctx.getBean(IScopes::class.java) ctx.close() - verify(hub).close() + verify(scopes).close() } @Configuration open class TestConfig { @Bean(destroyMethod = "") - open fun hub() = mock() + open fun scopes() = mock() @Bean - open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(hub()) + open fun sentryInitBeanPostProcessor() = SentryInitBeanPostProcessor(scopes()) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt index 279abce417..8faa243f83 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessorTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.SentryOptions import io.sentry.spring.jakarta.tracing.SpringMvcTransactionNameProvider @@ -19,10 +19,10 @@ import kotlin.test.assertNotNull class SentryRequestHttpServletRequestProcessorTest { private class Fixture { - val hub = mock() + val scopes = mock() fun getSut(request: HttpServletRequest, options: SentryOptions = SentryOptions()): SentryRequestHttpServletRequestProcessor { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return SentryRequestHttpServletRequestProcessor(SpringMvcTransactionNameProvider(), request) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt index 4e1bbb0ee5..b6bce77a0b 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta import io.sentry.Breadcrumb -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -37,7 +37,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val response = MockHttpServletResponse() val chain = mock() lateinit var scope: IScope @@ -45,15 +45,15 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) - whenever(hub.options).thenReturn(options) - whenever(hub.isEnabled).thenReturn(true) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + whenever(scopes.isEnabled).thenReturn(true) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(hub) + return SentrySpringFilter(scopes) } } @@ -64,7 +64,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).pushScope() + verify(fixture.scopes).pushScope() } @Test @@ -72,7 +72,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { it: Breadcrumb -> Assertions.assertThat(it.getData("url")).isEqualTo("http://localhost:8080/some-uri") Assertions.assertThat(it.getData("method")).isEqualTo("POST") @@ -87,7 +87,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } @Test @@ -99,7 +99,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.hub).popScope() + verify(fixture.scopes).popScope() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt index aa809259d1..e5f8704b49 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt @@ -32,28 +32,28 @@ class SentryTaskDecoratorTest { val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt index 2f128cc4bb..b30dc937e5 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryUserFilterTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.protocol.User import jakarta.servlet.FilterChain @@ -16,7 +16,7 @@ import kotlin.test.assertNull class SentryUserFilterTest { class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -25,8 +25,8 @@ class SentryUserFilterTest { val options = SentryOptions().apply { this.isSendDefaultPii = isSendDefaultPii } - whenever(hub.options).thenReturn(options) - return SentryUserFilter(hub, userProviders) + whenever(scopes.options).thenReturn(options) + return SentryUserFilter(scopes, userProviders) } } @@ -52,7 +52,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -72,7 +72,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -92,7 +92,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(sampleUser, it) } @@ -118,7 +118,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals(mapOf("key" to "value", "new-key" to "new-value"), it.others) } @@ -140,7 +140,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertEquals("192.168.0.1", it.ipAddress) } @@ -162,7 +162,7 @@ class SentryUserFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).setUser( + verify(fixture.scopes).setUser( check { assertNull(it.ipAddress) } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt index 0da5047251..3f8371ca3d 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdviceTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.exception import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.exception.ExceptionMechanismException import org.junit.runner.RunWith @@ -30,18 +30,18 @@ class SentryCaptureExceptionParameterAdviceTest { lateinit var sampleService: SampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) + reset(scopes) } @Test fun `captures exception passed to method annotated with @SentryCaptureException`() { val exception = RuntimeException("test exception") sampleService.methodTakingAnException(exception) - verify(hub).captureException( + verify(scopes).captureException( check { assertTrue(it is ExceptionMechanismException) assertEquals(exception, it.throwable) @@ -60,10 +60,10 @@ class SentryCaptureExceptionParameterAdviceTest { open fun sampleService() = SampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt index 3f71eaf23e..c2ebb95e48 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/graphql/SentrySpringSubscriptionHandlerTest.kt @@ -4,7 +4,7 @@ import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchPar import graphql.language.Document import graphql.language.OperationDefinition import graphql.schema.DataFetchingEnvironment -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.graphql.ExceptionReporter import io.sentry.spring.jakarta.graphql.SentrySpringSubscriptionHandler import org.junit.jupiter.api.assertThrows @@ -24,7 +24,7 @@ class SentrySpringSubscriptionHandlerTest { @Test fun `reports exception`() { val exception = IllegalStateException("some exception") - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -33,7 +33,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(exception), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -42,7 +42,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() @@ -53,7 +53,7 @@ class SentrySpringSubscriptionHandlerTest { fun `unwraps SubscriptionPublisherException and reports cause`() { val exception = IllegalStateException("some exception") val wrappedException = SubscriptionPublisherException(emptyList(), exception) - val hub = mock() + val scopes = mock() val exceptionReporter = mock() val parameters = mock() val dataFetchingEnvironment = mock() @@ -62,7 +62,7 @@ class SentrySpringSubscriptionHandlerTest { .build() whenever(dataFetchingEnvironment.document).thenReturn(document) whenever(parameters.environment).thenReturn(dataFetchingEnvironment) - val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), hub, exceptionReporter, parameters) + val resultObject = SentrySpringSubscriptionHandler().onSubscriptionResult(Flux.error(wrappedException), scopes, exceptionReporter, parameters) assertThrows { (resultObject as Flux).blockFirst() } @@ -71,7 +71,7 @@ class SentrySpringSubscriptionHandlerTest { same(exception), org.mockito.kotlin.check { assertEquals(true, it.isSubscription) - assertSame(hub, it.hub) + assertSame(scopes, it.scopes) assertEquals("query testQuery\n", it.query) }, anyOrNull() diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt index f472a75a65..0b4be869d0 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/mvc/SentrySpringIntegrationTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.mvc -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory import io.sentry.Sentry import io.sentry.SentryOptions @@ -104,7 +104,7 @@ class SentrySpringIntegrationTest { lateinit var anotherService: AnotherService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @LocalServerPort var port: Int? = null @@ -260,7 +260,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -276,7 +276,7 @@ class SentrySpringIntegrationTest { try { someService.aMethodWithInnerSpanThrowing() } catch (e: Exception) { - hub.captureException(e) + scopes.captureException(e) } verify(transport).send( checkEvent { @@ -370,20 +370,20 @@ open class App { open fun springSecuritySentryUserProvider(sentryOptions: SentryOptions) = SpringSecuritySentryUserProvider(sentryOptions) @Bean - open fun sentryUserFilter(hub: IHub, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { - this.filter = SentryUserFilter(hub, sentryUserProviders) + open fun sentryUserFilter(scopes: IScopes, @Lazy sentryUserProviders: List) = FilterRegistrationBean().apply { + this.filter = SentryUserFilter(scopes, sentryUserProviders) this.order = Ordered.LOWEST_PRECEDENCE } @Bean - open fun sentrySpringFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentrySpringFilter(hub) + open fun sentrySpringFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentrySpringFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE } @Bean - open fun sentryTracingFilter(hub: IHub) = FilterRegistrationBean().apply { - this.filter = SentryTracingFilter(hub) + open fun sentryTracingFilter(scopes: IScopes) = FilterRegistrationBean().apply { + this.filter = SentryTracingFilter(scopes) this.order = Ordered.HIGHEST_PRECEDENCE + 1 // must run after SentrySpringFilter } @@ -391,13 +391,13 @@ open class App { open fun sentryTaskDecorator() = SentryTaskDecorator() @Bean - open fun webClient(hub: IHub): WebClient { + open fun webClient(scopes: IScopes): WebClient { return WebClient.builder() .filter( ExchangeFilterFunctions .basicAuthentication("user", "password") ) - .filter(SentrySpanClientWebRequestFilter(hub)).build() + .filter(SentrySpanClientWebRequestFilter(scopes)).build() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt index 2910e7aac6..8b74b08fb1 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentrySpanAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Scope import io.sentry.Sentry import io.sentry.SentryOptions @@ -37,20 +37,20 @@ class SentrySpanAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - whenever(hub.options).thenReturn(SentryOptions()) + whenever(scopes.options).thenReturn(SentryOptions()) } @Test fun `when class is annotated with @SentrySpan, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -62,10 +62,10 @@ class SentrySpanAdviceTest { @Test fun `when class is annotated with @SentrySpan with operation set, every method call attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = classAnnotatedWithOperationSampleService.hello() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -76,10 +76,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan with properties set, attaches span to existing transaction`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) assertEquals(1, tx.spans.size) @@ -90,10 +90,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan without properties set, attaches span to existing transaction and sets Span description as className dot methodName`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) val result = sampleService.methodWithoutSpanDescriptionSet() assertEquals(2, result) assertEquals(1, tx.spans.size) @@ -104,10 +104,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and returns, attached span has status OK`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) sampleService.methodWithSpanDescriptionSet() assertEquals(SpanStatus.OK, tx.spans.first().status) } @@ -115,10 +115,10 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and throws exception, attached span has throwable set and INTERNAL_ERROR status`() { val scope = Scope(SentryOptions()) - val tx = SentryTracer(TransactionContext("aTransaction", "op"), hub) + val tx = SentryTracer(TransactionContext("aTransaction", "op"), scopes) scope.setTransaction(tx) - whenever(hub.span).thenReturn(tx) + whenever(scopes.span).thenReturn(tx) var throwable: Throwable? = null try { sampleService.methodThrowingException() @@ -131,7 +131,7 @@ class SentrySpanAdviceTest { @Test fun `when method is annotated with @SentrySpan and there is no active transaction, span is not created and method is executed`() { - whenever(hub.span).thenReturn(null) + whenever(scopes.span).thenReturn(null) val result = sampleService.methodWithSpanDescriptionSet() assertEquals(1, result) } @@ -151,10 +151,10 @@ class SentrySpanAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt index 0424696fad..265d607b70 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt @@ -1,7 +1,7 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -38,7 +38,7 @@ import kotlin.test.fail class SentryTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() val request = MockHttpServletRequest() val response = MockHttpServletResponse() val chain = mock() @@ -50,7 +50,7 @@ class SentryTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: Int = 200, sentryTraceHeader: String? = null, baggageHeaders: List? = null): SentryTracingFilter { @@ -61,16 +61,16 @@ class SentryTracingFilterTest { whenever(transactionNameProvider.provideTransactionSource()).thenReturn(TransactionNameSource.CUSTOM) if (sentryTraceHeader != null) { request.addHeader("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { request.addHeader("baggage", baggageHeaders) } response.status = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) - whenever(hub.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryTracingFilter(hub, transactionNameProvider) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) + whenever(scopes.continueTrace(any(), any())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryTracingFilter(scopes, transactionNameProvider) } } @@ -82,7 +82,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -95,7 +95,7 @@ class SentryTracingFilterTest { } ) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -114,7 +114,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -130,7 +130,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -146,7 +146,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -163,7 +163,7 @@ class SentryTracingFilterTest { filter.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -174,15 +174,15 @@ class SentryTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) filter.doFilter(fixture.request, fixture.response, fixture.chain) verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -196,7 +196,7 @@ class SentryTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -216,10 +216,10 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).isEnabled - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub, times(2)).options - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes).isEnabled + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes, times(2)).options + verifyNoMoreInteractions(fixture.scopes) verify(fixture.transactionNameProvider, never()).provideTransactionName(any()) } @@ -233,7 +233,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -253,7 +253,7 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -275,9 +275,9 @@ class SentryTracingFilterTest { verify(fixture.chain).doFilter(fixture.request, fixture.response) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt index 5d2863310f..390b4d8241 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,6 @@ package io.sentry.spring.jakarta.tracing -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -44,13 +44,13 @@ class SentryTransactionAdviceTest { lateinit var classAnnotatedWithOperationSampleService: ClassAnnotatedWithOperationSampleService @Autowired - lateinit var hub: IHub + lateinit var scopes: IScopes @BeforeTest fun setup() { - reset(hub) - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.options).thenReturn( + reset(scopes) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -60,7 +60,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction around method annotated with @SentryTransaction`() { sampleService.methodWithTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("customName") assertThat(it.contexts.trace!!.operation).isEqualTo("bean") @@ -76,7 +76,7 @@ class SentryTransactionAdviceTest { @Test fun `when method annotated with @SentryTransaction throws exception, sets error status on transaction`() { assertThrows { sampleService.methodThrowingException() } - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -89,7 +89,7 @@ class SentryTransactionAdviceTest { @Test fun `when @SentryTransaction has no name set, sets transaction name as className dot methodName`() { sampleService.methodWithoutTransactionNameSet() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("SampleService.methodWithoutTransactionNameSet") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -102,18 +102,18 @@ class SentryTransactionAdviceTest { @Test fun `when transaction is already active, does not start new transaction`() { - whenever(hub.options).thenReturn(SentryOptions()) - whenever(hub.span).then { SentryTracer(TransactionContext("aTransaction", "op"), hub) } + whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.span).then { SentryTracer(TransactionContext("aTransaction", "op"), scopes) } sampleService.methodWithTransactionNameSet() - verify(hub, times(0)).captureTransaction(any(), any()) + verify(scopes, times(0)).captureTransaction(any(), any()) } @Test fun `creates transaction around method in class annotated with @SentryTransaction`() { classAnnotatedSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("op") @@ -127,7 +127,7 @@ class SentryTransactionAdviceTest { @Test fun `creates transaction with operation set around method in class annotated with @SentryTransaction`() { classAnnotatedWithOperationSampleService.hello() - verify(hub).captureTransaction( + verify(scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("ClassAnnotatedWithOperationSampleService.hello") assertThat(it.contexts.trace!!.operation).isEqualTo("my-op") @@ -141,13 +141,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(hub).pushScope() + verify(scopes).pushScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(hub).popScope() + verify(scopes).popScope() } @Configuration @@ -165,10 +165,10 @@ class SentryTransactionAdviceTest { open fun classAnnotatedWithOperationSampleService() = ClassAnnotatedWithOperationSampleService() @Bean - open fun hub(): IHub { - val hub = mock() - Sentry.setCurrentHub(hub) - return hub + open fun scopes(): IScopes { + val scopes = mock() + Sentry.setCurrentScopes(scopes) + return scopes } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt index ad335333ed..9c851cde11 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt @@ -1,7 +1,8 @@ package io.sentry.spring.jakarta.webflux import io.sentry.IHub -import io.sentry.NoOpHub +import io.sentry.IScopes +import io.sentry.NoOpScopes import io.sentry.Sentry import org.mockito.kotlin.mock import org.mockito.kotlin.verify @@ -26,18 +27,18 @@ class ReactorUtilsTest { @AfterTest fun teardown() { - Sentry.setCurrentHub(NoOpHub.getInstance()) + Sentry.setCurrentScopes(NoOpScopes.getInstance()) } @Test fun `propagates hub inside mono`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val mono = ReactorUtils.withSentryHub( Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it }, hubToUse @@ -49,13 +50,13 @@ class ReactorUtilsTest { @Test fun `propagates hub inside flux`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val flux = ReactorUtils.withSentryHub( Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it }, hubToUse @@ -67,12 +68,12 @@ class ReactorUtilsTest { @Test fun `without reactive utils hub is not propagated to mono`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val mono = Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it } @@ -82,12 +83,12 @@ class ReactorUtilsTest { @Test fun `without reactive utils hub is not propagated to flux`() { - val hubToUse = mock() - var hubInside: IHub? = null + val hubToUse = mock() + var hubInside: IScopes? = null val flux = Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentHub() + hubInside = Sentry.getCurrentScopes() it } @@ -97,21 +98,21 @@ class ReactorUtilsTest { @Test fun `clones hub for mono`() { - val mockHub = mock() - whenever(mockHub.clone()).thenReturn(mock()) - Sentry.setCurrentHub(mockHub) + val mockScopes = mock() + whenever(mockScopes.clone()).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Mono.just("hello")).block() - verify(mockHub).clone() + verify(mockScopes).clone() } @Test fun `clones hub for flux`() { - val mockHub = mock() - whenever(mockHub.clone()).thenReturn(mock()) - Sentry.setCurrentHub(mockHub) + val mockScopes = mock() + whenever(mockScopes.clone()).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Flux.just("hello")).blockFirst() - verify(mockHub).clone() + verify(mockScopes).clone() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt index 1eb06d0afe..5403caa7e0 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt @@ -33,28 +33,28 @@ class SentryScheduleHookTest { val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentHub() - val threadedHub = Sentry.getCurrentHub().clone() + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().clone() executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedHub) }.get() - assertEquals(mainHub, Sentry.getCurrentHub()) + assertEquals(mainHub, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertNotEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentHub()) - assertEquals(threadedHub, Sentry.getCurrentHub()) + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt index eac0b9c60f..e936394039 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,8 +2,9 @@ package io.sentry.spring.jakarta.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.HubScopesWrapper import io.sentry.ILogger +import io.sentry.IScopes import io.sentry.PropagationContext import io.sentry.ScopeCallback import io.sentry.Sentry @@ -17,7 +18,7 @@ import io.sentry.TransactionOptions import io.sentry.protocol.SentryId import io.sentry.protocol.SentryTransaction import io.sentry.protocol.TransactionNameSource -import io.sentry.spring.jakarta.webflux.AbstractSentryWebFilter.SENTRY_HUB_KEY +import io.sentry.spring.jakarta.webflux.AbstractSentryWebFilter.SENTRY_SCOPES_KEY import org.assertj.core.api.Assertions.assertThat import org.mockito.Mockito import org.mockito.kotlin.any @@ -47,7 +48,7 @@ import kotlin.test.fail class SentryWebFluxTracingFilterTest { private class Fixture { - val hub = mock() + val scopes = mock() lateinit var request: MockServerHttpRequest lateinit var exchange: MockServerWebExchange val chain = mock() @@ -58,45 +59,47 @@ class SentryWebFluxTracingFilterTest { val logger = mock() init { - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) } fun getSut(isEnabled: Boolean = true, status: HttpStatus = HttpStatus.OK, sentryTraceHeader: String? = null, baggageHeaders: List? = null, method: HttpMethod = HttpMethod.POST): SentryWebFilter { var requestBuilder = MockServerHttpRequest.method(method, "/product/{id}", 12) if (sentryTraceHeader != null) { requestBuilder = requestBuilder.header("sentry-trace", sentryTraceHeader) - whenever(hub.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } + whenever(scopes.startTransaction(any(), check { it.isBindToScope })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } } if (baggageHeaders != null) { requestBuilder = requestBuilder.header("baggage", *baggageHeaders.toTypedArray()) } request = requestBuilder.build() exchange = MockServerWebExchange.builder(request).build() - exchange.attributes.put(SENTRY_HUB_KEY, hub) + exchange.attributes.put(SENTRY_SCOPES_KEY, scopes) exchange.attributes.put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, PathPatternParser().parse("/product/{id}")) exchange.response.statusCode = status - whenever(hub.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, hub) } - whenever(hub.isEnabled).thenReturn(isEnabled) + whenever(scopes.startTransaction(any(), check { assertTrue(it.isBindToScope) })).thenAnswer { SentryTracer(it.arguments[0] as TransactionContext, scopes) } + whenever(scopes.isEnabled).thenReturn(isEnabled) whenever(chain.filter(any())).thenReturn(Mono.create { s -> s.success() }) - whenever(hub.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } - return SentryWebFilter(hub) + whenever(scopes.continueTrace(anyOrNull(), anyOrNull())).thenAnswer { TransactionContext.fromPropagationContext(PropagationContext.fromHeaders(logger, it.arguments[0] as String?, it.arguments[1] as List?)) } + return SentryWebFilter(scopes) } } private val fixture = Fixture() - fun withMockHub(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.hub) + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { + it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) + it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) + it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) closure.invoke() } @Test fun `creates transaction around the request`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("POST /product/12", it.name) assertEquals(TransactionNameSource.URL, it.transactionNameSource) @@ -109,7 +112,7 @@ class SentryWebFluxTracingFilterTest { } ) verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.transaction).isEqualTo("POST /product/{id}") assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK) @@ -128,10 +131,10 @@ class SentryWebFluxTracingFilterTest { fun `sets correct span status based on the response status`() { val filter = fixture.getSut(status = HttpStatus.INTERNAL_SERVER_ERROR) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR) assertThat(it.contexts.response!!.statusCode).isEqualTo(500) @@ -147,10 +150,10 @@ class SentryWebFluxTracingFilterTest { fun `does not set span status for response status that dont match predefined span statuses`() { val filter = fixture.getSut(status = HttpStatus.FOUND) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.status).isNull() }, @@ -165,10 +168,10 @@ class SentryWebFluxTracingFilterTest { fun `when sentry trace is not present, transaction does not have parentSpanId set`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -184,10 +187,10 @@ class SentryWebFluxTracingFilterTest { val parentSpanId = SpanId() val filter = fixture.getSut(sentryTraceHeader = "${SentryId()}-$parentSpanId-1") - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isEqualTo(parentSpanId) }, @@ -199,16 +202,16 @@ class SentryWebFluxTracingFilterTest { } @Test - fun `when hub is disabled, components are not invoked`() { + fun `when scopes is disabled, components are not invoked`() { val filter = fixture.getSut(isEnabled = false) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, times(3)).isEnabled - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes, times(3)).isEnabled + verifyNoMoreInteractions(fixture.scopes) } } @@ -216,7 +219,7 @@ class SentryWebFluxTracingFilterTest { fun `sets status to internal server error when chain throws exception`() { val filter = fixture.getSut() - withMockHub { + withMockScopes { whenever(fixture.chain.filter(any())).thenReturn(Mono.error(RuntimeException("error"))) try { @@ -224,7 +227,7 @@ class SentryWebFluxTracingFilterTest { fail("filter is expected to rethrow exception") } catch (_: Exception) { } - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) }, @@ -239,21 +242,21 @@ class SentryWebFluxTracingFilterTest { fun `does not track OPTIONS request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, times(3)).isEnabled - verify(fixture.hub, times(2)).options - verify(fixture.hub).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.hub).pushScope() - verify(fixture.hub).addBreadcrumb(any(), any()) - verify(fixture.hub).configureScope(any()) - verify(fixture.hub).popScope() - verifyNoMoreInteractions(fixture.hub) + verify(fixture.scopes, times(3)).isEnabled + verify(fixture.scopes, times(2)).options + verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) + verify(fixture.scopes).pushScope() + verify(fixture.scopes).addBreadcrumb(any(), any()) + verify(fixture.scopes).configureScope(any()) + verify(fixture.scopes).popScope() + verifyNoMoreInteractions(fixture.scopes) } } @@ -261,14 +264,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks OPTIONS request with traceOptionsRequests=true`() { val filter = fixture.getSut(method = HttpMethod.OPTIONS) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = true filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -283,14 +286,14 @@ class SentryWebFluxTracingFilterTest { fun `tracks POST request with traceOptionsRequests=false`() { val filter = fixture.getSut(method = HttpMethod.POST) - withMockHub { + withMockScopes { fixture.options.isTraceOptionsRequests = false filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertThat(it.contexts.trace!!.parentSpanId).isNull() }, @@ -309,19 +312,19 @@ class SentryWebFluxTracingFilterTest { fixture.options.enableTracing = false val filter = fixture.getSut(sentryTraceHeader = sentryTraceHeaderString, baggageHeaders = baggageHeaderStrings) - withMockHub { + withMockScopes { filter.filter(fixture.exchange, fixture.chain).block() verify(fixture.chain).filter(fixture.exchange) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull() ) - verify(fixture.hub).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) + verify(fixture.scopes).continueTrace(eq(sentryTraceHeaderString), eq(baggageHeaderStrings)) } } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt index 3f4628ea3c..59f9a700b6 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebfluxIntegrationTest.kt @@ -1,8 +1,8 @@ package io.sentry.spring.jakarta.webflux -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ITransportFactory +import io.sentry.ScopesAdapter import io.sentry.Sentry import io.sentry.checkEvent import io.sentry.checkTransaction @@ -160,13 +160,13 @@ open class App { open fun mockTransport() = transport @Bean - open fun hub() = HubAdapter.getInstance() + open fun scopes() = ScopesAdapter.getInstance() @Bean - open fun sentryFilter(hub: IHub) = SentryWebFilter(hub) + open fun sentryFilter(scopes: IScopes) = SentryWebFilter(scopes) @Bean - open fun sentryWebExceptionHandler(hub: IHub) = SentryWebExceptionHandler(hub) + open fun sentryWebExceptionHandler(scopes: IScopes) = SentryWebExceptionHandler(scopes) @Bean open fun sentryScheduleHookRegistrar() = ApplicationRunner { From 9bcbce657d392795a7507daea9824d3f1a2e4bdc Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 14:55:42 +0200 Subject: [PATCH 15/57] Replace `IHub` with `IScopes` in samples --- .../java/io/sentry/samples/android/ProfilingActivity.kt | 2 +- .../java/io/sentry/samples/spring/jakarta/AppConfig.java | 6 +++--- .../java/io/sentry/samples/spring/jakarta/WebConfig.java | 8 ++++---- .../src/main/java/io/sentry/samples/spring/AppConfig.java | 6 +++--- .../src/main/java/io/sentry/samples/spring/WebConfig.java | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt index 610fc1534d..a7004deb35 100644 --- a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt +++ b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt @@ -100,7 +100,7 @@ class ProfilingActivity : AppCompatActivity() { val traceData = ProfilingTraceData(profile, t) // Create envelope item from copied profile val item = - SentryEnvelopeItem.fromProfilingTrace(traceData, Long.MAX_VALUE, Sentry.getCurrentHub().options.serializer) + SentryEnvelopeItem.fromProfilingTrace(traceData, Long.MAX_VALUE, Sentry.getCurrentScopes().options.serializer) val itemData = item.data // Compress the envelope item using Gzip diff --git a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java index f78c3f71d5..72ecb14e2f 100644 --- a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java +++ b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/AppConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.SentryUserFilter; import io.sentry.spring.jakarta.SentryUserProvider; import java.util.List; @@ -14,7 +14,7 @@ public class AppConfig { @Bean SentryUserFilter sentryUserFilter( - final IHub hub, final List sentryUserProviders) { - return new SentryUserFilter(hub, sentryUserProviders); + final IScopes scopes, final List sentryUserProviders) { + return new SentryUserFilter(scopes, sentryUserProviders); } } diff --git a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java index 92b48b138c..73d425b286 100644 --- a/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java +++ b/sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/WebConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring.jakarta; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.Collections; import org.springframework.context.annotation.Bean; @@ -20,14 +20,14 @@ public class WebConfig { * Creates a {@link RestTemplate} which calls are intercepted with {@link * SentrySpanClientHttpRequestInterceptor} to create spans around HTTP calls. * - * @param hub - sentry hub + * @param scopes - sentry scopes * @return RestTemplate */ @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } diff --git a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java index 7d46f09fb9..89a968834a 100644 --- a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java +++ b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/AppConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.SentryUserFilter; import io.sentry.spring.SentryUserProvider; import java.util.List; @@ -14,7 +14,7 @@ public class AppConfig { @Bean SentryUserFilter sentryUserFilter( - final IHub hub, final List sentryUserProviders) { - return new SentryUserFilter(hub, sentryUserProviders); + final IScopes scopes, final List sentryUserProviders) { + return new SentryUserFilter(scopes, sentryUserProviders); } } diff --git a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java index e135cbe233..2990ba8a38 100644 --- a/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java +++ b/sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/WebConfig.java @@ -1,6 +1,6 @@ package io.sentry.samples.spring; -import io.sentry.IHub; +import io.sentry.IScopes; import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor; import java.util.Collections; import org.springframework.context.annotation.Bean; @@ -20,14 +20,14 @@ public class WebConfig { * Creates a {@link RestTemplate} which calls are intercepted with {@link * SentrySpanClientHttpRequestInterceptor} to create spans around HTTP calls. * - * @param hub - sentry hub + * @param scopes - sentry scopes * @return RestTemplate */ @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } From d6fb40ad03da82ed6d416aa684a6b7a905b9c738 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 2 Apr 2024 15:49:05 +0200 Subject: [PATCH 16/57] gitscopes -> github --- .../android/navigation/SentryNavigationListenerTest.kt | 8 ++++---- .../io/sentry/apollo3/SentryApollo3HttpInterceptor.kt | 2 +- .../src/test/kotlin/SentrySpanProcessorTest.kt | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt index 7308b6b659..b37133410f 100644 --- a/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt +++ b/sentry-android-navigation/src/test/java/io/sentry/android/navigation/SentryNavigationListenerTest.kt @@ -266,13 +266,13 @@ class SentryNavigationListenerTest { @Test fun `onDestinationChanged strips out route parameters from transaction name`() { - val sut = fixture.getSut(toRoute = "gitscopes/{user_id}?per_page={per_page}") + val sut = fixture.getSut(toRoute = "github/{user_id}?per_page={per_page}") sut.onDestinationChanged(fixture.navController, fixture.destination, null) verify(fixture.scopes).startTransaction( check { - assertEquals("/gitscopes", it.name) + assertEquals("/github", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) }, any() @@ -296,7 +296,7 @@ class SentryNavigationListenerTest { @Test fun `onDestinationChanged captures arguments as additional data for transaction`() { - val sut = fixture.getSut(toRoute = "gitscopes/{user_id}?per_page={per_page}") + val sut = fixture.getSut(toRoute = "github/{user_id}?per_page={per_page}") sut.onDestinationChanged( fixture.navController, @@ -306,7 +306,7 @@ class SentryNavigationListenerTest { verify(fixture.scopes).startTransaction( check { - assertEquals("/gitscopes", it.name) + assertEquals("/github", it.name) assertEquals(TransactionNameSource.ROUTE, it.transactionNameSource) }, any() diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt index 10e56ce7be..52219cb8e1 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt @@ -91,7 +91,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor( return httpResponse } catch (e: Throwable) { - // https://gitscopes.com/apollographql/apollo-kotlin/issues/4711 will change error handling in v4 + // https://github.com/apollographql/apollo-kotlin/issues/4711 will change error handling in v4 when (e) { is ApolloHttpException -> { statusCode = e.statusCode diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt index 625fd54004..50d70f34f5 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySpanProcessorTest.kt @@ -303,7 +303,7 @@ class SentrySpanProcessorTest { thenChildSpanIsStarted() otelChildSpan.setStatus(StatusCode.ERROR) - otelChildSpan.setAttribute(SemanticAttributes.HTTP_URL, "http://gitscopes.com/getsentry/sentry-java") + otelChildSpan.setAttribute(SemanticAttributes.HTTP_URL, "http://github.com/getsentry/sentry-java") otelChildSpan.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, 404L) otelChildSpan.end() From 7752bcc13869a208b2332c5127bbf613735f4212 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 4 Apr 2024 13:53:20 +0200 Subject: [PATCH 17/57] Replace ThreadLocal with ScopesStorage --- sentry/api/sentry.api | 24 +++++++++++ .../java/io/sentry/DefaultScopesStorage.java | 42 +++++++++++++++++++ .../main/java/io/sentry/IScopesStorage.java | 13 ++++++ .../java/io/sentry/ISentryLifecycleToken.java | 8 ++++ .../java/io/sentry/NoOpScopesStorage.java | 40 ++++++++++++++++++ sentry/src/main/java/io/sentry/Sentry.java | 42 +++++++++++-------- 6 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/DefaultScopesStorage.java create mode 100644 sentry/src/main/java/io/sentry/IScopesStorage.java create mode 100644 sentry/src/main/java/io/sentry/ISentryLifecycleToken.java create mode 100644 sentry/src/main/java/io/sentry/NoOpScopesStorage.java diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 24935be274..32ce777a3d 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -259,6 +259,13 @@ public final class io/sentry/DeduplicateMultithreadedEventProcessor : io/sentry/ public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } +public final class io/sentry/DefaultScopesStorage : io/sentry/IScopesStorage { + public fun ()V + public fun close ()V + public fun get ()Lio/sentry/IScopes; + public fun set (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken; +} + public final class io/sentry/DefaultTransactionPerformanceCollector : io/sentry/TransactionPerformanceCollector { public fun (Lio/sentry/SentryOptions;)V public fun close ()V @@ -792,6 +799,12 @@ public abstract interface class io/sentry/IScopes { public abstract fun withScope (Lio/sentry/ScopeCallback;)V } +public abstract interface class io/sentry/IScopesStorage { + public abstract fun close ()V + public abstract fun get ()Lio/sentry/IScopes; + public abstract fun set (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken; +} + public abstract interface class io/sentry/ISentryClient { public abstract fun captureCheckIn (Lio/sentry/CheckIn;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; public fun captureEnvelope (Lio/sentry/SentryEnvelope;)Lio/sentry/protocol/SentryId; @@ -831,6 +844,10 @@ public abstract interface class io/sentry/ISentryExecutorService { public abstract fun submit (Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Future; } +public abstract interface class io/sentry/ISentryLifecycleToken : java/lang/AutoCloseable { + public abstract fun close ()V +} + public abstract interface class io/sentry/ISerializer { public abstract fun deserialize (Ljava/io/Reader;Ljava/lang/Class;)Ljava/lang/Object; public abstract fun deserializeCollection (Ljava/io/Reader;Ljava/lang/Class;Lio/sentry/JsonDeserializer;)Ljava/lang/Object; @@ -1390,6 +1407,13 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun withScope (Lio/sentry/ScopeCallback;)V } +public final class io/sentry/NoOpScopesStorage : io/sentry/IScopesStorage { + public fun close ()V + public fun get ()Lio/sentry/IScopes; + public static fun getInstance ()Lio/sentry/NoOpScopesStorage; + public fun set (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken; +} + public final class io/sentry/NoOpSpan : io/sentry/ISpan { public fun finish ()V public fun finish (Lio/sentry/SpanStatus;)V diff --git a/sentry/src/main/java/io/sentry/DefaultScopesStorage.java b/sentry/src/main/java/io/sentry/DefaultScopesStorage.java new file mode 100644 index 0000000000..12902a1dff --- /dev/null +++ b/sentry/src/main/java/io/sentry/DefaultScopesStorage.java @@ -0,0 +1,42 @@ +package io.sentry; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class DefaultScopesStorage implements IScopesStorage { + + private static final @NotNull ThreadLocal currentScopes = new ThreadLocal<>(); + + @Override + public ISentryLifecycleToken set(@Nullable IScopes scopes) { + final @Nullable IScopes oldScopes = get(); + currentScopes.set(scopes); + return new DefaultScopesLifecycleToken(oldScopes); + } + + @Override + public @Nullable IScopes get() { + return currentScopes.get(); + } + + @Override + public void close() { + // TODO prevent further storing? would this cause problems if singleton, closed and + // re-initialized? + currentScopes.remove(); + } + + static final class DefaultScopesLifecycleToken implements ISentryLifecycleToken { + + private final @Nullable IScopes oldValue; + + DefaultScopesLifecycleToken(final @Nullable IScopes scopes) { + this.oldValue = scopes; + } + + @Override + public void close() { + currentScopes.set(oldValue); + } + } +} diff --git a/sentry/src/main/java/io/sentry/IScopesStorage.java b/sentry/src/main/java/io/sentry/IScopesStorage.java new file mode 100644 index 0000000000..92f6b587c4 --- /dev/null +++ b/sentry/src/main/java/io/sentry/IScopesStorage.java @@ -0,0 +1,13 @@ +package io.sentry; + +import org.jetbrains.annotations.Nullable; + +public interface IScopesStorage { + + ISentryLifecycleToken set(final @Nullable IScopes scopes); + + @Nullable + IScopes get(); + + void close(); +} diff --git a/sentry/src/main/java/io/sentry/ISentryLifecycleToken.java b/sentry/src/main/java/io/sentry/ISentryLifecycleToken.java new file mode 100644 index 0000000000..2d0ad180f7 --- /dev/null +++ b/sentry/src/main/java/io/sentry/ISentryLifecycleToken.java @@ -0,0 +1,8 @@ +package io.sentry; + +public interface ISentryLifecycleToken extends AutoCloseable { + + // overridden to not have a checked exception on the method. + @Override + void close(); +} diff --git a/sentry/src/main/java/io/sentry/NoOpScopesStorage.java b/sentry/src/main/java/io/sentry/NoOpScopesStorage.java new file mode 100644 index 0000000000..fa507987ae --- /dev/null +++ b/sentry/src/main/java/io/sentry/NoOpScopesStorage.java @@ -0,0 +1,40 @@ +package io.sentry; + +import org.jetbrains.annotations.Nullable; + +public final class NoOpScopesStorage implements IScopesStorage { + private static final NoOpScopesStorage instance = new NoOpScopesStorage(); + + private NoOpScopesStorage() {} + + public static NoOpScopesStorage getInstance() { + return instance; + } + + @Override + public ISentryLifecycleToken set(@Nullable IScopes scopes) { + return NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @Nullable IScopes get() { + return NoOpScopes.getInstance(); + } + + @Override + public void close() {} + + static final class NoOpScopesLifecycleToken implements ISentryLifecycleToken { + + private static final NoOpScopesLifecycleToken instance = new NoOpScopesLifecycleToken(); + + private NoOpScopesLifecycleToken() {} + + public static NoOpScopesLifecycleToken getInstance() { + return instance; + } + + @Override + public void close() {} + } +} diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index f4996bb8ad..e01ca2f281 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -43,8 +43,7 @@ public final class Sentry { private Sentry() {} - /** Holds Hubs per thread or only mainScopes if globalHubMode is enabled. */ - private static final @NotNull ThreadLocal currentScopes = new ThreadLocal<>(); + private static volatile @NotNull IScopesStorage scopesStorage = new DefaultScopesStorage(); /** The Main Hub or NoOp if Sentry is disabled. */ private static volatile @NotNull IScopes mainScopes = NoOpScopes.getInstance(); @@ -83,13 +82,17 @@ private Sentry() {} if (globalHubMode) { return mainScopes; } - IScopes hub = currentScopes.get(); - if (hub == null || hub.isNoOp()) { + IScopes scopes = getScopesStorage().get(); + if (scopes == null || scopes.isNoOp()) { // TODO fork instead - hub = mainScopes.clone(); - currentScopes.set(hub); + scopes = mainScopes.clone(); + getScopesStorage().set(scopes); } - return hub; + return scopes; + } + + private static @NotNull IScopesStorage getScopesStorage() { + return scopesStorage; } /** @@ -110,14 +113,15 @@ private Sentry() {} @ApiStatus.Internal // exposed for the coroutines integration in SentryContext @Deprecated - @SuppressWarnings("deprecation") + @SuppressWarnings({"deprecation", "InlineMeSuggester"}) public static void setCurrentHub(final @NotNull IHub hub) { - currentScopes.set(hub); + setCurrentScopes(hub); } @ApiStatus.Internal // exposed for the coroutines integration in SentryContext - public static void setCurrentScopes(final @NotNull IScopes scopes) { - currentScopes.set(scopes); + public static @NotNull ISentryLifecycleToken setCurrentScopes(final @NotNull IScopes scopes) { + return getScopesStorage().set(scopes); + } } /** @@ -253,14 +257,18 @@ private static synchronized void init( options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode)); Sentry.globalHubMode = globalHubMode; - final IScopes hub = getCurrentScopes(); + final IScopes scopes = getCurrentScopes(); final IScope rootScope = new Scope(options); - final IScope rootIsolationScope = new Scope(options); - mainScopes = new Scopes(rootScope, rootIsolationScope, options, "Sentry.init"); + // TODO should use separate isolation scope: + // final IScope rootIsolationScope = new Scope(options); + // TODO should be: + // getGlobalScope().bindClient(new SentryClient(options)); + rootScope.bindClient(new SentryClient(options)); + mainScopes = new Scopes(rootScope, rootScope, options, "Sentry.init"); - currentScopes.set(mainScopes); + getScopesStorage().set(mainScopes); - hub.close(true); + scopes.close(true); // If the executorService passed in the init is the same that was previously closed, we have to // set a new one @@ -508,7 +516,7 @@ public static synchronized void close() { final IScopes scopes = getCurrentScopes(); mainScopes = NoOpScopes.getInstance(); // remove thread local to avoid memory leak - currentScopes.remove(); + getScopesStorage().close(); scopes.close(false); } From 1e329c59b7a63638f67720077f838c8665d83470 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 4 Apr 2024 14:13:51 +0200 Subject: [PATCH 18/57] Move client and throwable to span map to scope --- sentry/api/sentry.api | 12 ++- sentry/src/main/java/io/sentry/IScope.java | 11 ++- sentry/src/main/java/io/sentry/NoOpScope.java | 9 +- sentry/src/main/java/io/sentry/Scope.java | 52 +++++++++++- sentry/src/main/java/io/sentry/Scopes.java | 83 ++++++------------- 5 files changed, 105 insertions(+), 62 deletions(-) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 32ce777a3d..81107f75a5 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -660,6 +660,8 @@ public abstract interface class io/sentry/IScope { public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public abstract fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V public abstract fun addEventProcessor (Lio/sentry/EventProcessor;)V + public abstract fun assignTraceContext (Lio/sentry/SentryEvent;)V + public abstract fun bindClient (Lio/sentry/ISentryClient;)V public abstract fun clear ()V public abstract fun clearAttachments ()V public abstract fun clearBreadcrumbs ()V @@ -688,7 +690,6 @@ public abstract interface class io/sentry/IScope { public abstract fun removeContexts (Ljava/lang/String;)V public abstract fun removeExtra (Ljava/lang/String;)V public abstract fun removeTag (Ljava/lang/String;)V - public abstract fun setClient (Lio/sentry/ISentryClient;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -703,6 +704,7 @@ public abstract interface class io/sentry/IScope { public abstract fun setPropagationContext (Lio/sentry/PropagationContext;)V public abstract fun setRequest (Lio/sentry/protocol/Request;)V public abstract fun setScreen (Ljava/lang/String;)V + public abstract fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V public abstract fun setTransaction (Lio/sentry/ITransaction;)V public abstract fun setTransaction (Ljava/lang/String;)V @@ -1298,6 +1300,8 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V public fun addEventProcessor (Lio/sentry/EventProcessor;)V + public fun assignTraceContext (Lio/sentry/SentryEvent;)V + public fun bindClient (Lio/sentry/ISentryClient;)V public fun clear ()V public fun clearAttachments ()V public fun clearBreadcrumbs ()V @@ -1328,7 +1332,6 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V - public fun setClient (Lio/sentry/ISentryClient;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1343,6 +1346,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun setPropagationContext (Lio/sentry/PropagationContext;)V public fun setRequest (Lio/sentry/protocol/Request;)V public fun setScreen (Ljava/lang/String;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V public fun setTag (Ljava/lang/String;Ljava/lang/String;)V public fun setTransaction (Lio/sentry/ITransaction;)V public fun setTransaction (Ljava/lang/String;)V @@ -1731,6 +1735,8 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V public fun addEventProcessor (Lio/sentry/EventProcessor;)V + public fun assignTraceContext (Lio/sentry/SentryEvent;)V + public fun bindClient (Lio/sentry/ISentryClient;)V public fun clear ()V public fun clearAttachments ()V public fun clearBreadcrumbs ()V @@ -1760,7 +1766,6 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V - public fun setClient (Lio/sentry/ISentryClient;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1775,6 +1780,7 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun setPropagationContext (Lio/sentry/PropagationContext;)V public fun setRequest (Lio/sentry/protocol/Request;)V public fun setScreen (Ljava/lang/String;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V public fun setTag (Ljava/lang/String;Ljava/lang/String;)V public fun setTransaction (Lio/sentry/ITransaction;)V public fun setTransaction (Ljava/lang/String;)V diff --git a/sentry/src/main/java/io/sentry/IScope.java b/sentry/src/main/java/io/sentry/IScope.java index 3bc25ce8e9..d761ccc192 100644 --- a/sentry/src/main/java/io/sentry/IScope.java +++ b/sentry/src/main/java/io/sentry/IScope.java @@ -377,8 +377,17 @@ public interface IScope { @NotNull SentryId getLastEventId(); - void setClient(final @NotNull ISentryClient client); + void bindClient(final @NotNull ISentryClient client); @NotNull ISentryClient getClient(); + + @ApiStatus.Internal + void assignTraceContext(final @NotNull SentryEvent event); + + @ApiStatus.Internal + void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan span, + final @NotNull String transactionName); } diff --git a/sentry/src/main/java/io/sentry/NoOpScope.java b/sentry/src/main/java/io/sentry/NoOpScope.java index f7336d6edb..400a7e739f 100644 --- a/sentry/src/main/java/io/sentry/NoOpScope.java +++ b/sentry/src/main/java/io/sentry/NoOpScope.java @@ -256,10 +256,17 @@ public void setLastEventId(@NotNull SentryId lastEventId) {} } @Override - public void setClient(@NotNull ISentryClient client) {} + public void bindClient(@NotNull ISentryClient client) {} @Override public @NotNull ISentryClient getClient() { return NoOpSentryClient.getInstance(); } + + @Override + public void assignTraceContext(@NotNull SentryEvent event) {} + + @Override + public void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) {} } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index aa35ccfd7a..fcbcd74650 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -7,13 +7,18 @@ import io.sentry.protocol.TransactionNameSource; import io.sentry.protocol.User; import io.sentry.util.CollectionUtils; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; +import io.sentry.util.Pair; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import org.jetbrains.annotations.ApiStatus; @@ -85,6 +90,11 @@ public final class Scope implements IScope { private @NotNull ISentryClient client = NoOpSentryClient.getInstance(); + // TODO intended only for global scope + // TODO test for memory leak + private final @NotNull Map, String>> throwableToSpan = + Collections.synchronizedMap(new WeakHashMap<>()); + /** * Scope's ctor * @@ -103,6 +113,7 @@ private Scope(final @NotNull Scope scope) { this.session = scope.session; this.options = scope.options; this.level = scope.level; + this.client = scope.client; // TODO should we do this? didn't do it for Hub this.lastEventId = scope.getLastEventId(); @@ -964,7 +975,7 @@ public void setLastEventId(@NotNull SentryId lastEventId) { } @Override - public void setClient(@NotNull ISentryClient client) { + public void bindClient(@NotNull ISentryClient client) { this.client = client; } @@ -973,6 +984,45 @@ public void setClient(@NotNull ISentryClient client) { return client; } + @Override + @ApiStatus.Internal + public void assignTraceContext(final @NotNull SentryEvent event) { + if (options.isTracingEnabled() && event.getThrowable() != null) { + final Pair, String> pair = + throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable())); + if (pair != null) { + final WeakReference spanWeakRef = pair.getFirst(); + if (event.getContexts().getTrace() == null && spanWeakRef != null) { + final ISpan span = spanWeakRef.get(); + if (span != null) { + event.getContexts().setTrace(span.getSpanContext()); + } + } + final String transactionName = pair.getSecond(); + if (event.getTransaction() == null && transactionName != null) { + event.setTransaction(transactionName); + } + } + } + } + + @Override + @ApiStatus.Internal + public void setSpanContext( + final @NotNull Throwable throwable, + final @NotNull ISpan span, + final @NotNull String transactionName) { + Objects.requireNonNull(throwable, "throwable is required"); + Objects.requireNonNull(span, "span is required"); + Objects.requireNonNull(transactionName, "transactionName is required"); + // to match any cause, span context is always attached to the root cause of the exception + final Throwable rootCause = ExceptionUtils.findRootCause(throwable); + // the most inner span should be assigned to a throwable + if (!throwableToSpan.containsKey(rootCause)) { + throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName)); + } + } + /** The IWithTransaction callback */ @ApiStatus.Internal public interface IWithTransaction { diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 618864b649..b384d39c05 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -9,19 +9,15 @@ import io.sentry.protocol.SentryTransaction; import io.sentry.protocol.User; import io.sentry.transport.RateLimiter; -import io.sentry.util.ExceptionUtils; import io.sentry.util.HintUtils; import io.sentry.util.Objects; -import io.sentry.util.Pair; import io.sentry.util.TracingUtils; import java.io.Closeable; import java.io.IOException; -import java.lang.ref.WeakReference; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.WeakHashMap; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,10 +36,6 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull SentryOptions options; private volatile boolean isEnabled; private final @NotNull TracesSampler tracesSampler; - - // TODO should this go on global scope? - private final @NotNull Map, String>> throwableToSpan = - Collections.synchronizedMap(new WeakHashMap<>()); private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; private final @NotNull MetricsApi metricsApi; @@ -120,7 +112,10 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { // TODO add to IScopes interface public @NotNull Scopes forkedCurrentScope(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope, this, options, creator); + IScope clone = scope.clone(); + // TODO should use isolation scope + // return new Scopes(clone, isolationScope, this, options, creator); + return new Scopes(clone, clone, this, options, creator); } // // TODO in Sentry.init? @@ -180,23 +175,7 @@ public boolean isEnabled() { } private void assignTraceContext(final @NotNull SentryEvent event) { - if (options.isTracingEnabled() && event.getThrowable() != null) { - final Pair, String> pair = - throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable())); - if (pair != null) { - final WeakReference spanWeakRef = pair.getFirst(); - if (event.getContexts().getTrace() == null && spanWeakRef != null) { - final ISpan span = spanWeakRef.get(); - if (span != null) { - event.getContexts().setTrace(span.getSpanContext()); - } - } - final String transactionName = pair.getSecond(); - if (event.getTransaction() == null && transactionName != null) { - event.setTransaction(transactionName); - } - } - } + Sentry.getGlobalScope().assignTraceContext(event); } private IScope buildLocalScope( @@ -691,10 +670,10 @@ public void bindClient(final @NotNull ISentryClient client) { } else { if (client != null) { options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); - getDefaultWriteScope().setClient(client); + getDefaultWriteScope().bindClient(client); } else { options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); - getDefaultWriteScope().setClient(NoOpSentryClient.getInstance()); + getDefaultWriteScope().bindClient(NoOpSentryClient.getInstance()); } } } @@ -871,34 +850,26 @@ public void setSpanContext( final @NotNull Throwable throwable, final @NotNull ISpan span, final @NotNull String transactionName) { - Objects.requireNonNull(throwable, "throwable is required"); - Objects.requireNonNull(span, "span is required"); - Objects.requireNonNull(transactionName, "transactionName is required"); - // to match any cause, span context is always attached to the root cause of the exception - final Throwable rootCause = ExceptionUtils.findRootCause(throwable); - // the most inner span should be assigned to a throwable - if (!throwableToSpan.containsKey(rootCause)) { - throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName)); - } - } - - // TODO this seems unused - @Nullable - SpanContext getSpanContext(final @NotNull Throwable throwable) { - Objects.requireNonNull(throwable, "throwable is required"); - final Throwable rootCause = ExceptionUtils.findRootCause(throwable); - final Pair, String> pair = this.throwableToSpan.get(rootCause); - if (pair != null) { - final WeakReference spanWeakRef = pair.getFirst(); - if (spanWeakRef != null) { - final ISpan span = spanWeakRef.get(); - if (span != null) { - return span.getSpanContext(); - } - } - } - return null; - } + Sentry.getGlobalScope().setSpanContext(throwable, span, transactionName); + } + + // // TODO this seems unused + // @Nullable + // SpanContext getSpanContext(final @NotNull Throwable throwable) { + // Objects.requireNonNull(throwable, "throwable is required"); + // final Throwable rootCause = ExceptionUtils.findRootCause(throwable); + // final Pair, String> pair = this.throwableToSpan.get(rootCause); + // if (pair != null) { + // final WeakReference spanWeakRef = pair.getFirst(); + // if (spanWeakRef != null) { + // final ISpan span = spanWeakRef.get(); + // if (span != null) { + // return span.getSpanContext(); + // } + // } + // } + // return null; + // } @Override public @Nullable ISpan getSpan() { From b0d89ae630829c1c223bbb0e599af4554261c3dd Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 4 Apr 2024 14:22:16 +0200 Subject: [PATCH 19/57] Add global scope --- sentry/api/sentry.api | 1 + sentry/src/main/java/io/sentry/Scopes.java | 3 ++- sentry/src/main/java/io/sentry/Sentry.java | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 81107f75a5..0d33cccdc4 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2001,6 +2001,7 @@ public final class io/sentry/Sentry { public static fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getCurrentHub ()Lio/sentry/IHub; public static fun getCurrentScopes ()Lio/sentry/IScopes; + public static fun getGlobalScope ()Lio/sentry/IScope; public static fun getLastEventId ()Lio/sentry/protocol/SentryId; public static fun getSpan ()Lio/sentry/ISpan; public static fun getTraceparent ()Lio/sentry/SentryTraceHeader; diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index b384d39c05..d99e0d8bd7 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -579,7 +579,8 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { // TODO add to IScopes interface public @NotNull IScope getGlobalScope() { - // TODO return singleton global scope here + // TODO should be: + // return Sentry.getGlobalScope(); return scope; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index e01ca2f281..76ada357f2 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -47,6 +47,8 @@ private Sentry() {} /** The Main Hub or NoOp if Sentry is disabled. */ private static volatile @NotNull IScopes mainScopes = NoOpScopes.getInstance(); + // TODO cannot pass options here + private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); /** Default value for globalHubMode is false */ private static final boolean GLOBAL_HUB_DEFAULT_MODE = false; @@ -122,6 +124,9 @@ public static void setCurrentHub(final @NotNull IHub hub) { public static @NotNull ISentryLifecycleToken setCurrentScopes(final @NotNull IScopes scopes) { return getScopesStorage().set(scopes); } + + public static @NotNull IScope getGlobalScope() { + return globalScope; } /** @@ -264,6 +269,8 @@ private static synchronized void init( // TODO should be: // getGlobalScope().bindClient(new SentryClient(options)); rootScope.bindClient(new SentryClient(options)); + // TODO shouldn't replace global scope + globalScope = rootScope; mainScopes = new Scopes(rootScope, rootScope, options, "Sentry.init"); getScopesStorage().set(mainScopes); From cdd414aaa9590c284561fd92f0325356f48d002c Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 4 Apr 2024 16:24:34 +0200 Subject: [PATCH 20/57] use global scope in Scopes --- sentry/src/main/java/io/sentry/Scopes.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index d99e0d8bd7..c440b995ec 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -580,8 +580,8 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { // TODO add to IScopes interface public @NotNull IScope getGlobalScope() { // TODO should be: - // return Sentry.getGlobalScope(); - return scope; + return Sentry.getGlobalScope(); + // return scope; } @Override From 98da9ff6e4a1be1d01cfdafdd850ed51e8f07ce1 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 4 Apr 2024 16:28:30 +0200 Subject: [PATCH 21/57] Implement pushScope popScope and withScope for Scopes --- .../webflux/SentryWebFluxTracingFilterTest.kt | 4 +-- .../spring/webflux/SentryWebFilter.java | 4 +-- .../webflux/SentryWebFluxTracingFilterTest.kt | 4 +-- sentry/api/sentry.api | 21 ++++++++-------- sentry/src/main/java/io/sentry/Hub.java | 3 ++- .../src/main/java/io/sentry/HubAdapter.java | 4 +-- .../main/java/io/sentry/HubScopesWrapper.java | 4 +-- sentry/src/main/java/io/sentry/IScopes.java | 3 ++- sentry/src/main/java/io/sentry/NoOpHub.java | 4 ++- .../src/main/java/io/sentry/NoOpScopes.java | 4 ++- sentry/src/main/java/io/sentry/Scopes.java | 25 +++++++++++-------- .../main/java/io/sentry/ScopesAdapter.java | 4 +-- sentry/src/main/java/io/sentry/Sentry.java | 5 ++-- sentry/src/test/java/io/sentry/NoOpHubTest.kt | 4 ++- 14 files changed, 54 insertions(+), 39 deletions(-) diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt index e936394039..ddbbe75817 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt @@ -252,10 +252,10 @@ class SentryWebFluxTracingFilterTest { verify(fixture.scopes, times(3)).isEnabled verify(fixture.scopes, times(2)).options verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushScope() // TODO don't verify(fixture.scopes).addBreadcrumb(any(), any()) verify(fixture.scopes).configureScope(any()) - verify(fixture.scopes).popScope() + verify(fixture.scopes).popScope() // TODO don't verifyNoMoreInteractions(fixture.scopes) } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index 3bb7de5ae4..4d39e092bc 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -81,7 +81,7 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) if (transaction != null) { finishTransaction(serverWebExchange, transaction); } - requestHub.popScope(); + requestHub.popScope(); // TODO don't // TODO token based cleanup instead? Sentry.setCurrentScopes(NoOpScopes.getInstance()); }) @@ -96,7 +96,7 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) () -> { serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); Sentry.setCurrentScopes(requestHub); - requestHub.pushScope(); + requestHub.pushScope(); // TODO don't final ServerHttpResponse response = serverWebExchange.getResponse(); final Hint hint = new Hint(); diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt index 2113c748ee..1a31dcaa10 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt @@ -253,10 +253,10 @@ class SentryWebFluxTracingFilterTest { verify(fixture.scopes).isEnabled verify(fixture.scopes, times(2)).options verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushScope() // TODO don't verify(fixture.scopes).addBreadcrumb(any(), any()) verify(fixture.scopes).configureScope(any()) - verify(fixture.scopes).popScope() + verify(fixture.scopes).popScope() // TODO don't verifyNoMoreInteractions(fixture.scopes) } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 0d33cccdc4..9063972579 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -456,7 +456,7 @@ public final class io/sentry/Hub : io/sentry/IHub, io/sentry/metrics/MetricsApi$ public fun isHealthy ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -510,7 +510,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public fun isHealthy ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -563,7 +563,7 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub { public fun isHealthy ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -780,7 +780,7 @@ public abstract interface class io/sentry/IScopes { public fun isNoOp ()Z public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; public abstract fun popScope ()V - public abstract fun pushScope ()V + public abstract fun pushScope ()Lio/sentry/ISentryLifecycleToken; public abstract fun removeExtra (Ljava/lang/String;)V public abstract fun removeTag (Ljava/lang/String;)V public fun reportFullDisplayed ()V @@ -1270,7 +1270,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun isNoOp ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -1394,7 +1394,7 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun isNoOp ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -1866,9 +1866,10 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -1922,7 +1923,7 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public fun isHealthy ()Z public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V - public fun pushScope ()V + public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V public fun reportFullyDisplayed ()V @@ -2017,13 +2018,13 @@ public final class io/sentry/Sentry { public static fun isHealthy ()Z public static fun metrics ()Lio/sentry/metrics/MetricsApi; public static fun popScope ()V - public static fun pushScope ()V + public static fun pushScope ()Lio/sentry/ISentryLifecycleToken; public static fun removeExtra (Ljava/lang/String;)V public static fun removeTag (Ljava/lang/String;)V public static fun reportFullDisplayed ()V public static fun reportFullyDisplayed ()V public static fun setCurrentHub (Lio/sentry/IHub;)V - public static fun setCurrentScopes (Lio/sentry/IScopes;)V + public static fun setCurrentScopes (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken; public static fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public static fun setFingerprint (Ljava/util/List;)V public static fun setLevel (Lio/sentry/SentryLevel;)V diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 6a98bb2367..d56305be5d 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -526,7 +526,7 @@ public void removeExtra(final @NotNull String key) { } @Override - public void pushScope() { + public @NotNull ISentryLifecycleToken pushScope() { if (!isEnabled()) { options .getLogger() @@ -536,6 +536,7 @@ public void pushScope() { final StackItem newItem = new StackItem(options, item.getClient(), item.getScope().clone()); stack.push(newItem); } + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } @Override diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index 746d51f0cc..f7970200ce 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -154,8 +154,8 @@ public void removeExtra(@NotNull String key) { } @Override - public void pushScope() { - Sentry.pushScope(); + public @NotNull ISentryLifecycleToken pushScope() { + return Sentry.pushScope(); } @Override diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index 9b294d05b7..90c485d7f4 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -149,8 +149,8 @@ public void removeExtra(@NotNull String key) { } @Override - public void pushScope() { - scopes.pushScope(); + public @NotNull ISentryLifecycleToken pushScope() { + return scopes.pushScope(); } @Override diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 03662457e4..29edb6627d 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -306,7 +306,8 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { SentryId getLastEventId(); /** Pushes a new scope while inheriting the current scope's data. */ - void pushScope(); + @NotNull + ISentryLifecycleToken pushScope(); /** Removes the first scope */ void popScope(); diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index 704bd2b44a..1d5134d865 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -124,7 +124,9 @@ public void removeExtra(@NotNull String key) {} } @Override - public void pushScope() {} + public @NotNull ISentryLifecycleToken pushScope() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } @Override public void popScope() {} diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index 6fef262944..edf7ce1ecb 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -122,7 +122,9 @@ public void removeExtra(@NotNull String key) {} } @Override - public void pushScope() {} + public @NotNull ISentryLifecycleToken pushScope() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } @Override public void popScope() {} diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index c440b995ec..5e3de8a855 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -595,20 +595,21 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { // TODO needs to be deprecated because there's no more stack // TODO needs to return a lifecycle token @Override - public void pushScope() { + public ISentryLifecycleToken pushScope() { if (!isEnabled()) { options .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op."); + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } else { - // Scopes scopes = this.forkedScopes("pushScope"); - // return scopes.makeCurrent(); + Scopes scopes = this.forkedCurrentScope("pushScope"); + return scopes.makeCurrent(); } } - // public SentryLifecycleToken makeCurrent() { - // // TODO store.set(this); - // } + public ISentryLifecycleToken makeCurrent() { + return Sentry.setCurrentScopes(this); + } // TODO needs to be deprecated because there's no more stack @Override @@ -618,8 +619,11 @@ public void popScope() { .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op."); } else { - // TODO how to remove fork? - // TODO getParentScopes().makeCurrent()? + final @Nullable Scopes parent = getParent(); + if (parent != null) { + // TODO this is never closed + parent.makeCurrent(); + } } } @@ -634,7 +638,7 @@ public void withScope(final @NotNull ScopeCallback callback) { } } else { - Scopes forkedScopes = forkedScopes("withScope"); + Scopes forkedScopes = forkedCurrentScope("withScope"); // TODO should forkedScopes be made current inside callback? // TODO forkedScopes.makeCurrent()? try { @@ -705,7 +709,8 @@ public void flush(long timeoutMillis) { if (!isEnabled()) { options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); } - return new HubScopesWrapper(forkedScopes("scopes clone")); + // TODO should this fork isolation scope as well? + return new HubScopesWrapper(forkedCurrentScope("scopes clone")); } @ApiStatus.Internal diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index 1ecd31e248..3ffdc01186 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -150,8 +150,8 @@ public void removeExtra(@NotNull String key) { } @Override - public void pushScope() { - Sentry.pushScope(); + public @NotNull ISentryLifecycleToken pushScope() { + return Sentry.pushScope(); } @Override diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 76ada357f2..d9ebaa78be 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -813,12 +813,13 @@ public static void removeExtra(final @NotNull String key) { } /** Pushes a new scope while inheriting the current scope's data. */ - public static void pushScope() { + public static @NotNull ISentryLifecycleToken pushScope() { // pushScope is no-op in global hub mode if (!globalHubMode) { // TODO this might have to behave differently from Scopes.pushScope - getCurrentScopes().pushScope(); + return getCurrentScopes().pushScope(); } + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } /** Removes the first scope */ diff --git a/sentry/src/test/java/io/sentry/NoOpHubTest.kt b/sentry/src/test/java/io/sentry/NoOpHubTest.kt index dbbfb4b4f1..94af1acc9f 100644 --- a/sentry/src/test/java/io/sentry/NoOpHubTest.kt +++ b/sentry/src/test/java/io/sentry/NoOpHubTest.kt @@ -71,7 +71,9 @@ class NoOpHubTest { } @Test - fun `pushScope is no op`() = sut.pushScope() + fun `pushScope is no op`() { + sut.pushScope() + } @Test fun `popScope is no op`() = sut.popScope() From 2d2603384ae80946b24dabaff6dd6d05c6f21a9b Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 08:01:03 +0200 Subject: [PATCH 22/57] Add pushIsolationScope; add fork methods to ISCope --- sentry/src/main/java/io/sentry/Hub.java | 30 +++++++++++++ .../src/main/java/io/sentry/HubAdapter.java | 31 +++++++++++++ .../main/java/io/sentry/HubScopesWrapper.java | 30 +++++++++++++ sentry/src/main/java/io/sentry/IScopes.java | 45 +++++++++++++++++++ sentry/src/main/java/io/sentry/NoOpHub.java | 30 +++++++++++++ .../src/main/java/io/sentry/NoOpScopes.java | 30 +++++++++++++ sentry/src/main/java/io/sentry/Scopes.java | 45 +++++++++++-------- .../main/java/io/sentry/ScopesAdapter.java | 31 +++++++++++++ sentry/src/main/java/io/sentry/Sentry.java | 19 +++++++- 9 files changed, 270 insertions(+), 21 deletions(-) diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index d56305be5d..dd468d1eca 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -539,6 +539,11 @@ public void removeExtra(final @NotNull String key) { return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + @Override public @NotNull SentryOptions getOptions() { return this.stack.peek().getOptions(); @@ -652,6 +657,31 @@ public void flush(long timeoutMillis) { return new Hub(this.options, new Stack(this.stack)); } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return Sentry.forkedScopes(creator); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return Sentry.forkedCurrentScope(creator); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @NotNull IScope getScope() { + return Sentry.getCurrentScopes().getScope(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return Sentry.getCurrentScopes().getIsolationScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index f7970200ce..d813a11391 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -158,6 +158,11 @@ public void removeExtra(@NotNull String key) { return Sentry.pushScope(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return Sentry.pushIsolationScope(); + } + @Override public void popScope() { Sentry.popScope(); @@ -193,6 +198,32 @@ public void flush(long timeoutMillis) { return Sentry.getCurrentScopes().clone(); } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return Sentry.forkedScopes(creator); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return Sentry.forkedCurrentScope(creator); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + // TODO this wouldn't do anything since it replaced the current with the same Scopes + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @NotNull IScope getScope() { + return Sentry.getCurrentScopes().getScope(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return Sentry.getCurrentScopes().getIsolationScope(); + } + @Override public @NotNull SentryId captureTransaction( @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index 90c485d7f4..c3b2b19d80 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -153,6 +153,11 @@ public void removeExtra(@NotNull String key) { return scopes.pushScope(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return scopes.pushIsolationScope(); + } + @Override public void popScope() { scopes.popScope(); @@ -188,6 +193,31 @@ public void flush(long timeoutMillis) { return scopes.clone(); } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return scopes.forkedScopes(creator); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return scopes.forkedCurrentScope(creator); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + return scopes.makeCurrent(); + } + + @Override + public @NotNull IScope getScope() { + return scopes.getScope(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return scopes.getIsolationScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 29edb6627d..1ad2d62887 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -309,6 +309,9 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { @NotNull ISentryLifecycleToken pushScope(); + @NotNull + ISentryLifecycleToken pushIsolationScope(); + /** Removes the first scope */ void popScope(); @@ -354,12 +357,54 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { /** * Clones the Hub * + * @deprecated please use {@link IScopes#forkedScopes(String)} or {@link + * IScopes#forkedCurrentScope(String)} instead. * @return the cloned Hub */ @NotNull @Deprecated IHub clone(); + /** + * Creates a fork of both current and isolation scope. + * + * @param creator debug information to see why scopes where forked + * @return forked Scopes + */ + @NotNull + IScopes forkedScopes(final @NotNull String creator); + + /** + * Creates a fork of current scope without forking isolation scope. + * + * @param creator debug information to see why scopes where forked + * @return forked Scopes + */ + @NotNull + IScopes forkedCurrentScope(final @NotNull String creator); + + /** + * Stores this Scopes in store, making it the current one that is used by static API. + * + * @return a token you should call .close() on when you're done. + */ + @NotNull + ISentryLifecycleToken makeCurrent(); + + /** + * Returns the current scope of this Scopes. + * + * @return scope + */ + public @NotNull IScope getScope(); + + /** + * Returns the isolation scope of this Scopes. + * + * @return isolation scope + */ + public @NotNull IScope getIsolationScope(); + /** * Captures the transaction and enqueues it for sending to Sentry server. * diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index 1d5134d865..890c41d43e 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -128,6 +128,11 @@ public void removeExtra(@NotNull String key) {} return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + @Override public void popScope() {} @@ -155,6 +160,31 @@ public void flush(long timeoutMillis) {} return instance; } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return NoOpScopes.getInstance(); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return NoOpScopes.getInstance(); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @NotNull IScope getScope() { + return NoOpScope.getInstance(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return NoOpScope.getInstance(); + } + @Override public @NotNull SentryId captureTransaction( final @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index edf7ce1ecb..aa2b0fd2f3 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -126,6 +126,11 @@ public void removeExtra(@NotNull String key) {} return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + @Override public void popScope() {} @@ -154,6 +159,31 @@ public void flush(long timeoutMillis) {} return NoOpHub.getInstance(); } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return NoOpScopes.getInstance(); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return NoOpScopes.getInstance(); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @NotNull IScope getScope() { + return NoOpScope.getInstance(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return NoOpScope.getInstance(); + } + @Override public @NotNull SentryId captureTransaction( final @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 5e3de8a855..f844119be3 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -72,12 +72,12 @@ private Scopes( return creator; } - // TODO add to IScopes interface + @Override public @NotNull IScope getScope() { return scope; } - // TODO add to IScopes interface + @Override public @NotNull IScope getIsolationScope() { return isolationScope; } @@ -105,25 +105,16 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { return false; } - // TODO add to IScopes interface - public @NotNull Scopes forkedScopes(final @NotNull String creator) { + @Override + public @NotNull IScopes forkedScopes(final @NotNull String creator) { return new Scopes(scope.clone(), isolationScope.clone(), this, options, creator); } - // TODO add to IScopes interface - public @NotNull Scopes forkedCurrentScope(final @NotNull String creator) { - IScope clone = scope.clone(); - // TODO should use isolation scope - // return new Scopes(clone, isolationScope, this, options, creator); - return new Scopes(clone, clone, this, options, creator); + @Override + public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { + return new Scopes(scope.clone(), isolationScope, this, options, creator); } - // // TODO in Sentry.init? - // public static Scopes forkedRoots(final @NotNull SentryOptions options, final @NotNull String - // creator) { - // return new Scopes(ROOT_SCOPE.clone(), ROOT_ISOLATION_SCOPE.clone(), options, creator); - // } - // TODO always read from root scope? @Override public boolean isEnabled() { @@ -602,12 +593,28 @@ public ISentryLifecycleToken pushScope() { .log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op."); return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } else { - Scopes scopes = this.forkedCurrentScope("pushScope"); + final @NotNull IScopes scopes = this.forkedCurrentScope("pushScope"); return scopes.makeCurrent(); } } - public ISentryLifecycleToken makeCurrent() { + @Override + public ISentryLifecycleToken pushIsolationScope() { + if (!isEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Instance is disabled and this 'pushIsolationScope' call is a no-op."); + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } else { + final @NotNull IScopes scopes = this.forkedScopes("pushIsolationScope"); + return scopes.makeCurrent(); + } + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { return Sentry.setCurrentScopes(this); } @@ -638,7 +645,7 @@ public void withScope(final @NotNull ScopeCallback callback) { } } else { - Scopes forkedScopes = forkedCurrentScope("withScope"); + final @NotNull IScopes forkedScopes = forkedCurrentScope("withScope"); // TODO should forkedScopes be made current inside callback? // TODO forkedScopes.makeCurrent()? try { diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index 3ffdc01186..fe79a42731 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -154,6 +154,11 @@ public void removeExtra(@NotNull String key) { return Sentry.pushScope(); } + @Override + public @NotNull ISentryLifecycleToken pushIsolationScope() { + return Sentry.pushIsolationScope(); + } + @Override public void popScope() { Sentry.popScope(); @@ -190,6 +195,32 @@ public void flush(long timeoutMillis) { return Sentry.getCurrentScopes().clone(); } + @Override + public @NotNull IScopes forkedScopes(@NotNull String creator) { + return Sentry.forkedScopes(creator); + } + + @Override + public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { + return Sentry.forkedCurrentScope(creator); + } + + @Override + public @NotNull ISentryLifecycleToken makeCurrent() { + // TODO this wouldn't do anything since it replaced the current with the same Scopes + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + + @Override + public @NotNull IScope getScope() { + return Sentry.getCurrentScopes().getScope(); + } + + @Override + public @NotNull IScope getIsolationScope() { + return Sentry.getCurrentScopes().getIsolationScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index d9ebaa78be..aac1b9d66d 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -113,6 +113,14 @@ private Sentry() {} return mainScopes.clone(); } + public static @NotNull IScopes forkedScopes(final @NotNull String creator) { + return getCurrentScopes().forkedScopes(creator); + } + + public static @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { + return getCurrentScopes().forkedCurrentScope(creator); + } + @ApiStatus.Internal // exposed for the coroutines integration in SentryContext @Deprecated @SuppressWarnings({"deprecation", "InlineMeSuggester"}) @@ -822,11 +830,19 @@ public static void removeExtra(final @NotNull String key) { return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } + /** Pushes a new isolation and current scope while inheriting the current scope's data. */ + public static @NotNull ISentryLifecycleToken pushIsolationScope() { + // pushScope is no-op in global hub mode + if (!globalHubMode) { + return getCurrentScopes().pushIsolationScope(); + } + return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); + } + /** Removes the first scope */ public static void popScope() { // popScope is no-op in global hub mode if (!globalHubMode) { - // TODO this might have to behave differently from Scopes.popScope getCurrentScopes().popScope(); } } @@ -837,7 +853,6 @@ public static void popScope() { * @param callback the callback */ public static void withScope(final @NotNull ScopeCallback callback) { - // TODO this might have to behave differently from Scopes.withScope getCurrentScopes().withScope(callback); } From bbb6700e0e704ef9340dd04fa3ce82704da6a043 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 09:07:17 +0200 Subject: [PATCH 23/57] Use separate scopes for current, isolation and global scope; rename mainScopes to rootScopes --- sentry/src/main/java/io/sentry/Sentry.java | 41 +++++++++----------- sentry/src/test/java/io/sentry/SentryTest.kt | 10 ++--- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index aac1b9d66d..1a8c7db4b5 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -45,8 +45,8 @@ private Sentry() {} private static volatile @NotNull IScopesStorage scopesStorage = new DefaultScopesStorage(); - /** The Main Hub or NoOp if Sentry is disabled. */ - private static volatile @NotNull IScopes mainScopes = NoOpScopes.getInstance(); + /** The root Scopes or NoOp if Sentry is disabled. */ + private static volatile @NotNull IScopes rootScopes = NoOpScopes.getInstance(); // TODO cannot pass options here private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); @@ -67,7 +67,7 @@ private Sentry() {} private static final long classCreationTimestamp = System.currentTimeMillis(); /** - * Returns the current (threads) hub, if none, clones the mainScopes and returns it. + * Returns the current (threads) hub, if none, clones the rootScopes and returns it. * * @return the hub */ @@ -82,12 +82,11 @@ private Sentry() {} @SuppressWarnings("deprecation") public static @NotNull IScopes getCurrentScopes() { if (globalHubMode) { - return mainScopes; + return rootScopes; } IScopes scopes = getScopesStorage().get(); if (scopes == null || scopes.isNoOp()) { - // TODO fork instead - scopes = mainScopes.clone(); + scopes = rootScopes.forkedScopes("getCurrentScopes"); getScopesStorage().set(scopes); } return scopes; @@ -98,19 +97,18 @@ private Sentry() {} } /** - * Returns a new hub which is cloned from the mainScopes. + * Returns a new Scopes which is cloned from the rootScopes. * * @return the hub */ @ApiStatus.Internal @ApiStatus.Experimental @SuppressWarnings("deprecation") - public static @NotNull IScopes cloneMainHub() { + public static @NotNull IScopes forkedRootScopes(final @NotNull String creator) { if (globalHubMode) { - return mainScopes; + return rootScopes; } - // TODO fork instead - return mainScopes.clone(); + return rootScopes.forkedScopes(creator); } public static @NotNull IScopes forkedScopes(final @NotNull String creator) { @@ -123,9 +121,9 @@ private Sentry() {} @ApiStatus.Internal // exposed for the coroutines integration in SentryContext @Deprecated - @SuppressWarnings({"deprecation", "InlineMeSuggester"}) - public static void setCurrentHub(final @NotNull IHub hub) { - setCurrentScopes(hub); + @SuppressWarnings({"deprecation"}) + public static @NotNull ISentryLifecycleToken setCurrentHub(final @NotNull IHub hub) { + return setCurrentScopes(hub); } @ApiStatus.Internal // exposed for the coroutines integration in SentryContext @@ -272,16 +270,13 @@ private static synchronized void init( final IScopes scopes = getCurrentScopes(); final IScope rootScope = new Scope(options); - // TODO should use separate isolation scope: - // final IScope rootIsolationScope = new Scope(options); - // TODO should be: - // getGlobalScope().bindClient(new SentryClient(options)); - rootScope.bindClient(new SentryClient(options)); + final IScope rootIsolationScope = new Scope(options); // TODO shouldn't replace global scope - globalScope = rootScope; - mainScopes = new Scopes(rootScope, rootScope, options, "Sentry.init"); + globalScope = new Scope(options); + globalScope.bindClient(new SentryClient(options)); + rootScopes = new Scopes(rootScope, rootIsolationScope, options, "Sentry.init"); - getScopesStorage().set(mainScopes); + getScopesStorage().set(rootScopes); scopes.close(true); @@ -529,7 +524,7 @@ private static boolean initConfigurations(final @NotNull SentryOptions options) /** Close the SDK */ public static synchronized void close() { final IScopes scopes = getCurrentScopes(); - mainScopes = NoOpScopes.getInstance(); + rootScopes = NoOpScopes.getInstance(); // remove thread local to avoid memory leak getScopesStorage().close(); scopes.close(false); diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 70728d2900..77d443f909 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -422,7 +422,7 @@ class SentryTest { assertNotNull(scopes) assertFalse(Sentry.getCurrentScopes().isNoOp) - val newMainHubClone = Sentry.cloneMainHub() + val newMainHubClone = Sentry.forkedRootScopes("test") newMainHubClone.addBreadcrumb("breadcrumbMainClone") scopes.captureMessage("messageCurrent") @@ -473,7 +473,7 @@ class SentryTest { assertNotNull(scopes) assertFalse(scopes.isNoOp) - val newMainHubClone = Sentry.cloneMainHub() + val newMainHubClone = Sentry.forkedRootScopes("test") newMainHubClone.addBreadcrumb("breadcrumbMainClone") scopes.captureMessage("messageCurrent") @@ -921,7 +921,7 @@ class SentryTest { } @Test - fun `getSpan calls returns root span if globalscopes mode is enabled on Android`() { + fun `getSpan calls returns root span if globalHubMode is enabled on Android`() { PlatformTestManipulator.pretendIsAndroid(true) Sentry.init({ it.dsn = dsn @@ -938,7 +938,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalscopes mode is enabled, but the platform is not Android`() { + fun `getSpan calls returns child span if globalHubMode is enabled, but the platform is not Android`() { PlatformTestManipulator.pretendIsAndroid(false) Sentry.init({ it.dsn = dsn @@ -954,7 +954,7 @@ class SentryTest { } @Test - fun `getSpan calls returns child span if globalscopes mode is disabled`() { + fun `getSpan calls returns child span if globalHubMode is disabled`() { Sentry.init({ it.dsn = dsn it.enableTracing = true From c714b214dcd17d946248e2898e7fff01105863f0 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 09:22:08 +0200 Subject: [PATCH 24/57] Allow controlling which scope configureScope uses --- .../core/AndroidOptionsInitializer.java | 3 ++ sentry/src/main/java/io/sentry/Hub.java | 3 +- .../src/main/java/io/sentry/HubAdapter.java | 4 +- .../main/java/io/sentry/HubScopesWrapper.java | 4 +- sentry/src/main/java/io/sentry/IScopes.java | 11 +++++- sentry/src/main/java/io/sentry/NoOpHub.java | 2 +- .../src/main/java/io/sentry/NoOpScopes.java | 2 +- sentry/src/main/java/io/sentry/ScopeType.java | 7 ++++ sentry/src/main/java/io/sentry/Scopes.java | 38 +++++++++++++------ .../main/java/io/sentry/ScopesAdapter.java | 4 +- sentry/src/main/java/io/sentry/Sentry.java | 12 +++++- .../main/java/io/sentry/SentryOptions.java | 10 +++++ .../src/test/java/io/sentry/HubAdapterTest.kt | 3 +- .../test/java/io/sentry/ScopesAdapterTest.kt | 3 +- 14 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/ScopeType.java diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 372448b8e7..605de4c0a8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -10,6 +10,7 @@ import io.sentry.ILogger; import io.sentry.ITransactionProfiler; import io.sentry.NoOpConnectionStatusProvider; +import io.sentry.ScopeType; import io.sentry.SendFireAndForgetEnvelopeSender; import io.sentry.SendFireAndForgetOutboxSender; import io.sentry.SentryLevel; @@ -98,6 +99,8 @@ static void loadDefaultAndMetadataOptions( // Firstly set the logger, if `debug=true` configured, logging can start asap. options.setLogger(logger); + options.setDefaultScopeType(ScopeType.CURRENT); + options.setDateProvider(new SentryAndroidDateProvider()); // set a lower flush timeout on Android to avoid ANRs diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index dd468d1eca..35740f4c3e 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -594,7 +594,8 @@ public void withScope(final @NotNull ScopeCallback callback) { } @Override - public void configureScope(final @NotNull ScopeCallback callback) { + public void configureScope( + final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { if (!isEnabled()) { options .getLogger() diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index d813a11391..f0d7335a80 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -174,8 +174,8 @@ public void withScope(@NotNull ScopeCallback callback) { } @Override - public void configureScope(@NotNull ScopeCallback callback) { - Sentry.configureScope(callback); + public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { + Sentry.configureScope(scopeType, callback); } @Override diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index c3b2b19d80..3309e59671 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -169,8 +169,8 @@ public void withScope(@NotNull ScopeCallback callback) { } @Override - public void configureScope(@NotNull ScopeCallback callback) { - scopes.configureScope(callback); + public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { + scopes.configureScope(scopeType, callback); } @Override diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 1ad2d62887..af6f41ae13 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -331,7 +331,16 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { * * @param callback The configure scope callback. */ - void configureScope(@NotNull ScopeCallback callback); + default void configureScope(@NotNull ScopeCallback callback) { + configureScope(null, callback); + } + + /** + * Configures the scope through the callback. + * + * @param callback The configure scope callback. + */ + void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback); /** * Binds a different client to the hub diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index 890c41d43e..ac1c542a94 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -142,7 +142,7 @@ public void withScope(@NotNull ScopeCallback callback) { } @Override - public void configureScope(@NotNull ScopeCallback callback) {} + public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {} @Override public void bindClient(@NotNull ISentryClient client) {} diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index aa2b0fd2f3..de75ff8178 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -140,7 +140,7 @@ public void withScope(@NotNull ScopeCallback callback) { } @Override - public void configureScope(@NotNull ScopeCallback callback) {} + public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {} @Override public void bindClient(@NotNull ISentryClient client) {} diff --git a/sentry/src/main/java/io/sentry/ScopeType.java b/sentry/src/main/java/io/sentry/ScopeType.java new file mode 100644 index 0000000000..d54c2b635c --- /dev/null +++ b/sentry/src/main/java/io/sentry/ScopeType.java @@ -0,0 +1,7 @@ +package io.sentry; + +public enum ScopeType { + CURRENT, + ISOLATION, + GLOBAL; +} diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index f844119be3..c37dddfd31 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -428,16 +428,31 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable } } - private IScope getDefaultConfigureScope() { - // TODO configurable default scope via SentryOptions, Android = global or isolation, backend = - // isolation - return scope; - } + private IScope getSpecificScope(final @Nullable ScopeType scopeType) { + if (scopeType != null) { + switch (scopeType) { + case CURRENT: + return scope; + case ISOLATION: + return isolationScope; + case GLOBAL: + return getGlobalScope(); + default: + break; + } + } - private IScope getDefaultWriteScope() { - // TODO configurable default scope via SentryOptions, Android = global or isolation, backend = - // isolation - return getIsolationScope(); + switch (getOptions().getDefaultScopeType()) { + case CURRENT: + return scope; + case ISOLATION: + return isolationScope; + case GLOBAL: + return getGlobalScope(); + default: + // calm the compiler + return scope; + } } @Override @@ -657,7 +672,8 @@ public void withScope(final @NotNull ScopeCallback callback) { } @Override - public void configureScope(final @NotNull ScopeCallback callback) { + public void configureScope( + final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { if (!isEnabled()) { options .getLogger() @@ -666,7 +682,7 @@ public void configureScope(final @NotNull ScopeCallback callback) { "Instance is disabled and this 'configureScope' call is a no-op."); } else { try { - callback.run(getDefaultConfigureScope()); + callback.run(getSpecificScope(scopeType)); } catch (Throwable e) { options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); } diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index fe79a42731..d3b0f43bf2 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -170,8 +170,8 @@ public void withScope(@NotNull ScopeCallback callback) { } @Override - public void configureScope(@NotNull ScopeCallback callback) { - Sentry.configureScope(callback); + public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { + Sentry.configureScope(scopeType, callback); } @Override diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 1a8c7db4b5..090cba0d66 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -857,7 +857,17 @@ public static void withScope(final @NotNull ScopeCallback callback) { * @param callback The configure scope callback. */ public static void configureScope(final @NotNull ScopeCallback callback) { - getCurrentScopes().configureScope(callback); + configureScope(null, callback); + } + + /** + * Configures the scope through the callback. + * + * @param callback The configure scope callback. + */ + public static void configureScope( + final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { + getCurrentScopes().configureScope(scopeType, callback); } /** diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index b3bf66a1c2..c693907121 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -479,6 +479,8 @@ public class SentryOptions { @ApiStatus.Experimental private @Nullable Cron cron = null; + private @NotNull ScopeType defaultScopeType = ScopeType.ISOLATION; + /** * Adds an event processor * @@ -2385,6 +2387,14 @@ public void setCron(@Nullable Cron cron) { this.cron = cron; } + public void setDefaultScopeType(final @NotNull ScopeType scopeType) { + this.defaultScopeType = scopeType; + } + + public @NotNull ScopeType getDefaultScopeType() { + return defaultScopeType; + } + /** The BeforeSend callback */ public interface BeforeSendCallback { diff --git a/sentry/src/test/java/io/sentry/HubAdapterTest.kt b/sentry/src/test/java/io/sentry/HubAdapterTest.kt index 0e7e1d0f77..c8e17bfb2b 100644 --- a/sentry/src/test/java/io/sentry/HubAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/HubAdapterTest.kt @@ -3,6 +3,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq import org.mockito.kotlin.mock import org.mockito.kotlin.reset @@ -185,7 +186,7 @@ class HubAdapterTest { @Test fun `configureScope calls Hub`() { val scopeCallback = mock() HubAdapter.getInstance().configureScope(scopeCallback) - verify(scopes).configureScope(eq(scopeCallback)) + verify(scopes).configureScope(anyOrNull(), eq(scopeCallback)) } @Test fun `bindClient calls Hub`() { diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt index 85a0b6ef75..7637e6c74e 100644 --- a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -3,6 +3,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq import org.mockito.kotlin.mock import org.mockito.kotlin.reset @@ -185,7 +186,7 @@ class ScopesAdapterTest { @Test fun `configureScope calls Hub`() { val scopeCallback = mock() ScopesAdapter.getInstance().configureScope(scopeCallback) - verify(scopes).configureScope(eq(scopeCallback)) + verify(scopes).configureScope(anyOrNull(), eq(scopeCallback)) } @Test fun `bindClient calls Hub`() { From a4744028342b7b0abf7d93e4a1b387cd09a24404 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 11:32:03 +0200 Subject: [PATCH 25/57] Combine scopes --- .../android/core/InternalSentrySdkTest.kt | 4 +- .../src/main/java/io/sentry/Breadcrumb.java | 8 +- .../java/io/sentry/CombinedContextsView.java | 214 ++++++++ .../java/io/sentry/CombinedScopeView.java | 456 ++++++++++++++++++ sentry/src/main/java/io/sentry/Scopes.java | 46 +- sentry/src/main/java/io/sentry/Sentry.java | 2 +- .../java/io/sentry/protocol/Contexts.java | 4 +- 7 files changed, 702 insertions(+), 32 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/CombinedContextsView.java create mode 100644 sentry/src/main/java/io/sentry/CombinedScopeView.java diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt index a10d8add7b..b34e79991f 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt @@ -118,7 +118,7 @@ class InternalSentrySdkTest { @Test fun `current scope returns obj when hub is active`() { - Sentry.setCurrentHub( + Sentry.setCurrentScopes( Hub( SentryOptions().apply { dsn = "https://key@uri/1234567" @@ -131,7 +131,7 @@ class InternalSentrySdkTest { @Test fun `current scope returns a copy of the scope`() { - Sentry.setCurrentHub( + Sentry.setCurrentScopes( Hub( SentryOptions().apply { dsn = "https://key@uri/1234567" diff --git a/sentry/src/main/java/io/sentry/Breadcrumb.java b/sentry/src/main/java/io/sentry/Breadcrumb.java index fe2055c336..5f43ab6d29 100644 --- a/sentry/src/main/java/io/sentry/Breadcrumb.java +++ b/sentry/src/main/java/io/sentry/Breadcrumb.java @@ -17,7 +17,7 @@ import org.jetbrains.annotations.Nullable; /** Series of application events */ -public final class Breadcrumb implements JsonUnknown, JsonSerializable { +public final class Breadcrumb implements JsonUnknown, JsonSerializable, Comparable { /** A timestamp representing when the breadcrumb occurred. */ private final @NotNull Date timestamp; @@ -660,6 +660,12 @@ public void setUnknown(@Nullable Map unknown) { this.unknown = unknown; } + @Override + @SuppressWarnings("JavaUtilDate") + public int compareTo(@NotNull Breadcrumb o) { + return timestamp.compareTo(o.timestamp); + } + public static final class JsonKeys { public static final String TIMESTAMP = "timestamp"; public static final String MESSAGE = "message"; diff --git a/sentry/src/main/java/io/sentry/CombinedContextsView.java b/sentry/src/main/java/io/sentry/CombinedContextsView.java new file mode 100644 index 0000000000..3362a8ea1e --- /dev/null +++ b/sentry/src/main/java/io/sentry/CombinedContextsView.java @@ -0,0 +1,214 @@ +package io.sentry; + +import io.sentry.protocol.App; +import io.sentry.protocol.Browser; +import io.sentry.protocol.Contexts; +import io.sentry.protocol.Device; +import io.sentry.protocol.Gpu; +import io.sentry.protocol.OperatingSystem; +import io.sentry.protocol.Response; +import io.sentry.protocol.SentryRuntime; +import io.sentry.util.HintUtils; +import java.io.IOException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class CombinedContextsView extends Contexts { + + private static final long serialVersionUID = 3585992094653318439L; + private final @NotNull Contexts globalContexts; + private final @NotNull Contexts isolationContexts; + private final @NotNull Contexts currentContexts; + + private final @NotNull ScopeType defaultScopeType; + + public CombinedContextsView( + final @NotNull Contexts globalContexts, + final @NotNull Contexts isolationContexts, + final @NotNull Contexts currentContexts, + final @NotNull ScopeType defaultScopeType) { + this.globalContexts = globalContexts; + this.isolationContexts = isolationContexts; + this.currentContexts = currentContexts; + this.defaultScopeType = defaultScopeType; + } + + @Override + public @Nullable SpanContext getTrace() { + final @Nullable SpanContext current = currentContexts.getTrace(); + if (current != null) { + return current; + } + final @Nullable SpanContext isolation = isolationContexts.getTrace(); + if (isolation != null) { + return isolation; + } + return globalContexts.getTrace(); + } + + @Override + public void setTrace(@Nullable SpanContext traceContext) { + getDefaultContexts().setTrace(traceContext); + } + + private Contexts getDefaultContexts() { + switch (defaultScopeType) { + case CURRENT: + return currentContexts; + case ISOLATION: + return isolationContexts; + case GLOBAL: + return globalContexts; + default: + return currentContexts; + } + } + + @Override + public @Nullable App getApp() { + final @Nullable App current = currentContexts.getApp(); + if (current != null) { + return current; + } + final @Nullable App isolation = isolationContexts.getApp(); + if (isolation != null) { + return isolation; + } + return globalContexts.getApp(); + } + + @Override + public void setApp(@NotNull App app) { + getDefaultContexts().setApp(app); + } + + @Override + public @Nullable Browser getBrowser() { + final @Nullable Browser current = currentContexts.getBrowser(); + if (current != null) { + return current; + } + final @Nullable Browser isolation = isolationContexts.getBrowser(); + if (isolation != null) { + return isolation; + } + return globalContexts.getBrowser(); + } + + @Override + public void setBrowser(@NotNull Browser browser) { + getDefaultContexts().setBrowser(browser); + } + + @Override + public @Nullable Device getDevice() { + final @Nullable Device current = currentContexts.getDevice(); + if (current != null) { + return current; + } + final @Nullable Device isolation = isolationContexts.getDevice(); + if (isolation != null) { + return isolation; + } + return globalContexts.getDevice(); + } + + @Override + public void setDevice(@NotNull Device device) { + getDefaultContexts().setDevice(device); + } + + @Override + public @Nullable OperatingSystem getOperatingSystem() { + final @Nullable OperatingSystem current = currentContexts.getOperatingSystem(); + if (current != null) { + return current; + } + final @Nullable OperatingSystem isolation = isolationContexts.getOperatingSystem(); + if (isolation != null) { + return isolation; + } + return globalContexts.getOperatingSystem(); + } + + @Override + public void setOperatingSystem(@NotNull OperatingSystem operatingSystem) { + getDefaultContexts().setOperatingSystem(operatingSystem); + } + + @Override + public @Nullable SentryRuntime getRuntime() { + final @Nullable SentryRuntime current = currentContexts.getRuntime(); + if (current != null) { + return current; + } + final @Nullable SentryRuntime isolation = isolationContexts.getRuntime(); + if (isolation != null) { + return isolation; + } + return globalContexts.getRuntime(); + } + + @Override + public void setRuntime(@NotNull SentryRuntime runtime) { + getDefaultContexts().setRuntime(runtime); + } + + @Override + public @Nullable Gpu getGpu() { + final @Nullable Gpu current = currentContexts.getGpu(); + if (current != null) { + return current; + } + final @Nullable Gpu isolation = isolationContexts.getGpu(); + if (isolation != null) { + return isolation; + } + return globalContexts.getGpu(); + } + + @Override + public void setGpu(@NotNull Gpu gpu) { + getDefaultContexts().setGpu(gpu); + } + + @Override + public @Nullable Response getResponse() { + final @Nullable Response current = currentContexts.getResponse(); + if (current != null) { + return current; + } + final @Nullable Response isolation = isolationContexts.getResponse(); + if (isolation != null) { + return isolation; + } + return globalContexts.getResponse(); + } + + @Override + public void withResponse(HintUtils.SentryConsumer callback) { + if (currentContexts.getResponse() != null) { + currentContexts.withResponse(callback); + } else if (isolationContexts.getResponse() != null) { + isolationContexts.withResponse(callback); + } else if (globalContexts.getResponse() != null) { + globalContexts.withResponse(callback); + } else { + getDefaultContexts().withResponse(callback); + } + } + + @Override + public void setResponse(@NotNull Response response) { + getDefaultContexts().setResponse(response); + } + + @Override + public void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException { + final @NotNull Contexts allContexts = new Contexts(); + allContexts.putAll(globalContexts); + allContexts.putAll(isolationContexts); + allContexts.putAll(currentContexts); + allContexts.serialize(writer, logger); + } +} diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java new file mode 100644 index 0000000000..b9f8ab2c70 --- /dev/null +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -0,0 +1,456 @@ +package io.sentry; + +import io.sentry.protocol.Contexts; +import io.sentry.protocol.Request; +import io.sentry.protocol.SentryId; +import io.sentry.protocol.User; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class CombinedScopeView implements IScope { + + private final IScope globalScope; + private final IScope isolationScope; + private final IScope scope; + + public CombinedScopeView( + final @NotNull IScope globalScope, + final @NotNull IScope isolationScope, + final @NotNull IScope scope) { + this.globalScope = globalScope; + this.isolationScope = isolationScope; + this.scope = scope; + } + + @Override + public @Nullable SentryLevel getLevel() { + final @Nullable SentryLevel current = scope.getLevel(); + if (current != null) { + return current; + } + final @Nullable SentryLevel isolation = isolationScope.getLevel(); + if (isolation != null) { + return isolation; + } + return globalScope.getLevel(); + } + + @Override + public void setLevel(@Nullable SentryLevel level) { + getDefaultWriteScope().setLevel(level); + } + + @Override + public @Nullable String getTransactionName() { + final @Nullable String current = scope.getTransactionName(); + if (current != null) { + return current; + } + final @Nullable String isolation = isolationScope.getTransactionName(); + if (isolation != null) { + return isolation; + } + return globalScope.getTransactionName(); + } + + @Override + public void setTransaction(@NotNull String transaction) { + getDefaultWriteScope().setTransaction(transaction); + } + + @Override + public @Nullable ISpan getSpan() { + final @Nullable ISpan current = scope.getSpan(); + if (current != null) { + return current; + } + final @Nullable ISpan isolation = isolationScope.getSpan(); + if (isolation != null) { + return isolation; + } + return globalScope.getSpan(); + } + + @Override + public void setTransaction(@Nullable ITransaction transaction) { + getDefaultWriteScope().setTransaction(transaction); + } + + @Override + public @Nullable User getUser() { + final @Nullable User current = scope.getUser(); + if (current != null) { + return current; + } + final @Nullable User isolation = isolationScope.getUser(); + if (isolation != null) { + return isolation; + } + return globalScope.getUser(); + } + + @Override + public void setUser(@Nullable User user) { + getDefaultWriteScope().setUser(user); + } + + @Override + public @Nullable String getScreen() { + final @Nullable String current = scope.getScreen(); + if (current != null) { + return current; + } + final @Nullable String isolation = isolationScope.getScreen(); + if (isolation != null) { + return isolation; + } + return globalScope.getScreen(); + } + + @Override + public void setScreen(@Nullable String screen) { + getDefaultWriteScope().setScreen(screen); + } + + @Override + public @Nullable Request getRequest() { + final @Nullable Request current = scope.getRequest(); + if (current != null) { + return current; + } + final @Nullable Request isolation = isolationScope.getRequest(); + if (isolation != null) { + return isolation; + } + return globalScope.getRequest(); + } + + @Override + public void setRequest(@Nullable Request request) { + getDefaultWriteScope().setRequest(request); + } + + @Override + public @NotNull List getFingerprint() { + final @Nullable List current = scope.getFingerprint(); + if (!current.isEmpty()) { + return current; + } + final @Nullable List isolation = isolationScope.getFingerprint(); + if (!isolation.isEmpty()) { + return isolation; + } + return globalScope.getFingerprint(); + } + + @Override + public void setFingerprint(@NotNull List fingerprint) { + getDefaultWriteScope().setFingerprint(fingerprint); + } + + @Override + public @NotNull Queue getBreadcrumbs() { + final @NotNull List allBreadcrumbs = new ArrayList<>(); + allBreadcrumbs.addAll(globalScope.getBreadcrumbs()); + allBreadcrumbs.addAll(isolationScope.getBreadcrumbs()); + allBreadcrumbs.addAll(scope.getBreadcrumbs()); + Collections.sort(allBreadcrumbs); + + // TODO test oldest are removed first + final @NotNull Queue breadcrumbs = + createBreadcrumbsList(scope.getOptions().getMaxBreadcrumbs()); + breadcrumbs.addAll(allBreadcrumbs); + + return breadcrumbs; + } + + /** + * Creates a breadcrumb list with the max number of breadcrumbs + * + * @param maxBreadcrumb the max number of breadcrumbs + * @return the breadcrumbs queue + */ + // TODO copied from Scope, should reuse instead + private @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { + return SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb)); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { + getDefaultWriteScope().addBreadcrumb(breadcrumb, hint); + } + + @Override + public void addBreadcrumb(@NotNull Breadcrumb breadcrumb) { + getDefaultWriteScope().addBreadcrumb(breadcrumb); + } + + @Override + public void clearBreadcrumbs() { + getDefaultWriteScope().clearBreadcrumbs(); + } + + @Override + public void clearTransaction() { + getDefaultWriteScope().clearTransaction(); + } + + @Override + public @Nullable ITransaction getTransaction() { + final @Nullable ITransaction current = scope.getTransaction(); + if (current != null) { + return current; + } + final @Nullable ITransaction isolation = isolationScope.getTransaction(); + if (isolation != null) { + return isolation; + } + return globalScope.getTransaction(); + } + + @Override + public void clear() { + getDefaultWriteScope().clear(); + } + + @Override + public @NotNull Map getTags() { + final @NotNull Map allTags = new ConcurrentHashMap<>(); + allTags.putAll(globalScope.getTags()); + allTags.putAll(isolationScope.getTags()); + allTags.putAll(scope.getTags()); + return allTags; + } + + @Override + public void setTag(@NotNull String key, @NotNull String value) { + getDefaultWriteScope().setTag(key, value); + } + + @Override + public void removeTag(@NotNull String key) { + getDefaultWriteScope().removeTag(key); + } + + @Override + public @NotNull Map getExtras() { + final @NotNull Map allTags = new ConcurrentHashMap<>(); + allTags.putAll(globalScope.getExtras()); + allTags.putAll(isolationScope.getExtras()); + allTags.putAll(scope.getExtras()); + return allTags; + } + + @Override + public void setExtra(@NotNull String key, @NotNull String value) { + getDefaultWriteScope().setExtra(key, value); + } + + @Override + public void removeExtra(@NotNull String key) { + getDefaultWriteScope().removeExtra(key); + } + + @Override + public @NotNull Contexts getContexts() { + return new CombinedContextsView( + globalScope.getContexts(), + isolationScope.getContexts(), + scope.getContexts(), + getOptions().getDefaultScopeType()); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Object value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Boolean value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull String value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Number value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Collection value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Object[] value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void setContexts(@NotNull String key, @NotNull Character value) { + getDefaultWriteScope().setContexts(key, value); + } + + @Override + public void removeContexts(@NotNull String key) { + getDefaultWriteScope().removeContexts(key); + } + + private @NotNull IScope getDefaultWriteScope() { + if (ScopeType.CURRENT.equals(getOptions().getDefaultScopeType())) { + return scope; + } + if (ScopeType.ISOLATION.equals(getOptions().getDefaultScopeType())) { + return isolationScope; + } + return globalScope; + } + + @Override + public @NotNull List getAttachments() { + final @NotNull List allAttachments = new CopyOnWriteArrayList<>(); + allAttachments.addAll(globalScope.getAttachments()); + allAttachments.addAll(isolationScope.getAttachments()); + allAttachments.addAll(scope.getAttachments()); + return allAttachments; + } + + @Override + public void addAttachment(@NotNull Attachment attachment) { + getDefaultWriteScope().addAttachment(attachment); + } + + @Override + public void clearAttachments() { + getDefaultWriteScope().clearAttachments(); + } + + @Override + public @NotNull List getEventProcessors() { + // TODO mechanism for ordering event processors + final @NotNull List allEventProcessors = new CopyOnWriteArrayList<>(); + allEventProcessors.addAll(globalScope.getEventProcessors()); + allEventProcessors.addAll(isolationScope.getEventProcessors()); + allEventProcessors.addAll(scope.getEventProcessors()); + return allEventProcessors; + } + + @Override + public void addEventProcessor(@NotNull EventProcessor eventProcessor) { + getDefaultWriteScope().addEventProcessor(eventProcessor); + } + + @Override + public @Nullable Session withSession(Scope.@NotNull IWithSession sessionCallback) { + return getDefaultWriteScope().withSession(sessionCallback); + } + + @Override + public @Nullable Scope.SessionPair startSession() { + return getDefaultWriteScope().startSession(); + } + + @Override + public @Nullable Session endSession() { + return getDefaultWriteScope().endSession(); + } + + @Override + public void withTransaction(Scope.@NotNull IWithTransaction callback) { + getDefaultWriteScope().withTransaction(callback); + } + + @Override + public @NotNull SentryOptions getOptions() { + return scope.getOptions(); + } + + @Override + public @Nullable Session getSession() { + final @Nullable Session current = scope.getSession(); + if (current != null) { + return current; + } + final @Nullable Session isolation = isolationScope.getSession(); + if (isolation != null) { + return isolation; + } + return globalScope.getSession(); + } + + @Override + public void setPropagationContext(@NotNull PropagationContext propagationContext) { + getDefaultWriteScope().setPropagationContext(propagationContext); + } + + @Override + public @NotNull PropagationContext getPropagationContext() { + return getDefaultWriteScope().getPropagationContext(); + } + + @Override + public @NotNull PropagationContext withPropagationContext( + Scope.@NotNull IWithPropagationContext callback) { + return getDefaultWriteScope().withPropagationContext(callback); + } + + @Override + public @NotNull IScope clone() { + // TODO just return a new CombinedScopeView with forked scope? + return getDefaultWriteScope().clone(); + } + + @Override + public void setLastEventId(@NotNull SentryId lastEventId) { + globalScope.setLastEventId(lastEventId); + isolationScope.setLastEventId(lastEventId); + scope.setLastEventId(lastEventId); + } + + @Override + public @NotNull SentryId getLastEventId() { + return globalScope.getLastEventId(); + } + + @Override + public void bindClient(@NotNull ISentryClient client) { + getDefaultWriteScope().bindClient(client); + } + + @Override + public @NotNull ISentryClient getClient() { + // TODO checking for noop here doesn't allow disabling via client, is that ok? + final @Nullable ISentryClient current = scope.getClient(); + if (!(current instanceof NoOpSentryClient)) { + return current; + } + final @Nullable ISentryClient isolation = isolationScope.getClient(); + if (!(isolation instanceof NoOpSentryClient)) { + return isolation; + } + return globalScope.getClient(); + } + + @Override + public void assignTraceContext(@NotNull SentryEvent event) { + globalScope.assignTraceContext(event); + } + + @Override + public void setSpanContext( + @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) { + globalScope.setSpanContext(throwable, span, transactionName); + } +} diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index c37dddfd31..daf143ba66 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -166,7 +166,7 @@ public boolean isEnabled() { } private void assignTraceContext(final @NotNull SentryEvent event) { - Sentry.getGlobalScope().assignTraceContext(event); + getCombinedScopeView().assignTraceContext(event); } private IScope buildLocalScope( @@ -363,8 +363,7 @@ public void endSession() { } private IScope getCombinedScopeView() { - // TODO combine global, isolation and current scope - return scope; + return new CombinedScopeView(getGlobalScope(), isolationScope, scope); } @Override @@ -424,7 +423,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable } else if (breadcrumb == null) { options.getLogger().log(SentryLevel.WARNING, "addBreadcrumb called with null parameter."); } else { - getDefaultWriteScope().addBreadcrumb(breadcrumb, hint); + getCombinedScopeView().addBreadcrumb(breadcrumb, hint); } } @@ -467,7 +466,7 @@ public void setLevel(final @Nullable SentryLevel level) { .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op."); } else { - getDefaultWriteScope().setLevel(level); + getCombinedScopeView().setLevel(level); } } @@ -480,7 +479,7 @@ public void setTransaction(final @Nullable String transaction) { SentryLevel.WARNING, "Instance is disabled and this 'setTransaction' call is a no-op."); } else if (transaction != null) { - getDefaultWriteScope().setTransaction(transaction); + getCombinedScopeView().setTransaction(transaction); } else { options.getLogger().log(SentryLevel.WARNING, "Transaction cannot be null"); } @@ -493,7 +492,7 @@ public void setUser(final @Nullable User user) { .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op."); } else { - getDefaultWriteScope().setUser(user); + getCombinedScopeView().setUser(user); } } @@ -508,7 +507,7 @@ public void setFingerprint(final @NotNull List fingerprint) { } else if (fingerprint == null) { options.getLogger().log(SentryLevel.WARNING, "setFingerprint called with null parameter."); } else { - getDefaultWriteScope().setFingerprint(fingerprint); + getCombinedScopeView().setFingerprint(fingerprint); } } @@ -521,7 +520,7 @@ public void clearBreadcrumbs() { SentryLevel.WARNING, "Instance is disabled and this 'clearBreadcrumbs' call is a no-op."); } else { - getDefaultWriteScope().clearBreadcrumbs(); + getCombinedScopeView().clearBreadcrumbs(); } } @@ -534,7 +533,7 @@ public void setTag(final @NotNull String key, final @NotNull String value) { } else if (key == null || value == null) { options.getLogger().log(SentryLevel.WARNING, "setTag called with null parameter."); } else { - getDefaultWriteScope().setTag(key, value); + getCombinedScopeView().setTag(key, value); } } @@ -547,7 +546,7 @@ public void removeTag(final @NotNull String key) { } else if (key == null) { options.getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter."); } else { - getDefaultWriteScope().removeTag(key); + getCombinedScopeView().removeTag(key); } } @@ -560,7 +559,7 @@ public void setExtra(final @NotNull String key, final @NotNull String value) { } else if (key == null || value == null) { options.getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter."); } else { - getDefaultWriteScope().setExtra(key, value); + getCombinedScopeView().setExtra(key, value); } } @@ -573,14 +572,12 @@ public void removeExtra(final @NotNull String key) { } else if (key == null) { options.getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter."); } else { - getDefaultWriteScope().removeExtra(key); + getCombinedScopeView().removeExtra(key); } } private void updateLastEventId(final @NotNull SentryId lastEventId) { - scope.setLastEventId(lastEventId); - isolationScope.setLastEventId(lastEventId); - getGlobalScope().setLastEventId(lastEventId); + getCombinedScopeView().setLastEventId(lastEventId); } // TODO add to IScopes interface @@ -592,14 +589,9 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { @Override public @NotNull SentryId getLastEventId() { - // TODO read all scopes here / read default scope? - // returning scope.lastEventId isn't ideal because changed to child scope are not stored in - // there - return getGlobalScope().getLastEventId(); + return getCombinedScopeView().getLastEventId(); } - // TODO needs to be deprecated because there's no more stack - // TODO needs to return a lifecycle token @Override public ISentryLifecycleToken pushScope() { if (!isEnabled()) { @@ -698,10 +690,10 @@ public void bindClient(final @NotNull ISentryClient client) { } else { if (client != null) { options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); - getDefaultWriteScope().bindClient(client); + getCombinedScopeView().bindClient(client); } else { options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); - getDefaultWriteScope().bindClient(NoOpSentryClient.getInstance()); + getCombinedScopeView().bindClient(NoOpSentryClient.getInstance()); } } } @@ -879,7 +871,7 @@ public void setSpanContext( final @NotNull Throwable throwable, final @NotNull ISpan span, final @NotNull String transactionName) { - Sentry.getGlobalScope().setSpanContext(throwable, span, transactionName); + getCombinedScopeView().setSpanContext(throwable, span, transactionName); } // // TODO this seems unused @@ -908,7 +900,7 @@ public void setSpanContext( .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'getSpan' call is a no-op."); } else { - span = getScope().getSpan(); + span = getCombinedScopeView().getSpan(); } return span; } @@ -924,7 +916,7 @@ public void setSpanContext( SentryLevel.WARNING, "Instance is disabled and this 'getTransaction' call is a no-op."); } else { - span = getScope().getTransaction(); + span = getCombinedScopeView().getTransaction(); } return span; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 090cba0d66..42ccc3cf63 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -121,7 +121,7 @@ private Sentry() {} @ApiStatus.Internal // exposed for the coroutines integration in SentryContext @Deprecated - @SuppressWarnings({"deprecation"}) + @SuppressWarnings({"deprecation", "InlineMeSuggester"}) public static @NotNull ISentryLifecycleToken setCurrentHub(final @NotNull IHub hub) { return setCurrentScopes(hub); } diff --git a/sentry/src/main/java/io/sentry/protocol/Contexts.java b/sentry/src/main/java/io/sentry/protocol/Contexts.java index 21be9fd8a5..aa157e665e 100644 --- a/sentry/src/main/java/io/sentry/protocol/Contexts.java +++ b/sentry/src/main/java/io/sentry/protocol/Contexts.java @@ -1,5 +1,6 @@ package io.sentry.protocol; +import com.jakewharton.nopen.annotation.Open; import io.sentry.ILogger; import io.sentry.JsonDeserializer; import io.sentry.JsonObjectReader; @@ -17,7 +18,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public final class Contexts extends ConcurrentHashMap implements JsonSerializable { +@Open +public class Contexts extends ConcurrentHashMap implements JsonSerializable { private static final long serialVersionUID = 252445813254943011L; /** Response lock, Ops should be atomic */ From ae93e336ef1ed057a3f58f2f1c019d7d4a1de981 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 13:01:25 +0200 Subject: [PATCH 26/57] Use new API for CRONS integrations --- .../io/sentry/log4j2/SentryAppenderTest.kt | 1 + sentry-quartz/api/sentry-quartz.api | 1 + .../io/sentry/quartz/SentryJobListener.java | 8 ++- .../jakarta/checkin/SentryCheckInAdvice.java | 5 +- .../spring/jakarta/SentryCheckInAdviceTest.kt | 46 +++++++++-------- .../spring/checkin/SentryCheckInAdvice.java | 5 +- .../sentry/spring/SentryCheckInAdviceTest.kt | 50 ++++++++++--------- .../java/io/sentry/util/CheckInUtils.java | 6 +-- .../java/io/sentry/util/CheckInUtilsTest.kt | 45 ++++++++++++----- 9 files changed, 102 insertions(+), 65 deletions(-) diff --git a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt index 3786555a61..2713bb7f17 100644 --- a/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt +++ b/sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt @@ -78,6 +78,7 @@ class SentryAppenderTest { @BeforeTest fun `clear MDC`() { ThreadContext.clearAll() + Sentry.close() } @Test diff --git a/sentry-quartz/api/sentry-quartz.api b/sentry-quartz/api/sentry-quartz.api index 23fce49e7d..21ca2abca6 100644 --- a/sentry-quartz/api/sentry-quartz.api +++ b/sentry-quartz/api/sentry-quartz.api @@ -5,6 +5,7 @@ public final class io/sentry/quartz/BuildConfig { public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener { public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String; + public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public static final field SENTRY_SLUG_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index f9c22022cc..03f1acbbed 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -4,10 +4,12 @@ import io.sentry.CheckIn; import io.sentry.CheckInStatus; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; +import io.sentry.util.LifecycleHelper; import io.sentry.util.Objects; import io.sentry.util.TracingUtils; import org.jetbrains.annotations.ApiStatus; @@ -23,6 +25,7 @@ public final class SentryJobListener implements JobListener { public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; public static final String SENTRY_SLUG_KEY = "sentry-slug"; + public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; private final @NotNull IScopes scopes; @@ -49,13 +52,14 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { if (maybeSlug == null) { return; } - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); TracingUtils.startNewTrace(scopes); final @NotNull String slug = maybeSlug; final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn); context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); context.put(SENTRY_SLUG_KEY, slug); + context.put(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); } catch (Throwable t) { scopes .getOptions() @@ -103,7 +107,7 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); } finally { - scopes.popScope(); + LifecycleHelper.close(context.get(SENTRY_LIFECYCLE_TOKEN_KEY)); } } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index 4a366a8b01..e51647cba8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -5,6 +5,7 @@ import io.sentry.CheckInStatus; import io.sentry.DateUtils; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; @@ -86,7 +87,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; @@ -106,7 +107,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); scopes.captureCheckIn(checkIn); - scopes.popScope(); + lifecycleToken.close(); } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt index 5d093f50f1..e87b5f5b26 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt @@ -3,6 +3,7 @@ package io.sentry.spring.jakarta import io.sentry.CheckIn import io.sentry.CheckInStatus import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -56,10 +57,13 @@ class SentryCheckInAdviceTest { @Autowired lateinit var scopes: IScopes + val lifecycleToken = mock() + @BeforeTest fun setup() { reset(scopes) whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } @Test @@ -79,10 +83,10 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes, times(2)).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -103,10 +107,10 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes, times(2)).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -123,10 +127,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -144,10 +148,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -178,10 +182,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -198,10 +202,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -218,10 +222,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Configuration diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index edae74c2ac..9e59093b16 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -5,6 +5,7 @@ import io.sentry.CheckInStatus; import io.sentry.DateUtils; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; @@ -89,7 +90,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); TracingUtils.startNewTrace(scopes); @Nullable SentryId checkInId = null; @@ -109,7 +110,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); scopes.captureCheckIn(checkIn); - scopes.popScope(); + lifecycleToken.close(); } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt index 23807e1fd9..57bd293756 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt @@ -3,6 +3,7 @@ package io.sentry.spring import io.sentry.CheckIn import io.sentry.CheckInStatus import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.protocol.SentryId @@ -57,10 +58,13 @@ class SentryCheckInAdviceTest { @Autowired lateinit var scopes: IScopes + val lifecycleToken = mock() + @BeforeTest fun setup() { reset(scopes) whenever(scopes.options).thenReturn(SentryOptions()) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } @Test @@ -80,10 +84,10 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes, times(2)).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -104,10 +108,10 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes, times(2)).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -124,10 +128,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -145,10 +149,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -160,9 +164,9 @@ class SentryCheckInAdviceTest { assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(scopes, never()).pushScope() + verify(scopes, never()).pushIsolationScope() verify(scopes, never()).captureCheckIn(any()) - verify(scopes, never()).popScope() + verify(lifecycleToken, never()).close() } @Test @@ -179,10 +183,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -199,10 +203,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Test @@ -219,10 +223,10 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) - val order = inOrder(scopes) - order.verify(scopes).pushScope() + val order = inOrder(scopes, lifecycleToken) + order.verify(scopes).pushIsolationScope() order.verify(scopes).captureCheckIn(any()) - order.verify(scopes).popScope() + order.verify(lifecycleToken).close() } @Configuration diff --git a/sentry/src/main/java/io/sentry/util/CheckInUtils.java b/sentry/src/main/java/io/sentry/util/CheckInUtils.java index 6719e24839..d42cf4edf4 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -4,6 +4,7 @@ import io.sentry.CheckInStatus; import io.sentry.DateUtils; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.MonitorConfig; import io.sentry.Sentry; import io.sentry.protocol.SentryId; @@ -30,12 +31,11 @@ public static U withCheckIn( final @Nullable MonitorConfig monitorConfig, final @NotNull Callable callable) throws Exception { + final @NotNull ISentryLifecycleToken lifecycleToken = Sentry.pushIsolationScope(); final @NotNull IScopes scopes = Sentry.getCurrentScopes(); final long startTime = System.currentTimeMillis(); boolean didError = false; - // TODO fork instead - scopes.pushScope(); TracingUtils.startNewTrace(scopes); CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); @@ -53,7 +53,7 @@ public static U withCheckIn( CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); scopes.captureCheckIn(checkIn); - scopes.popScope(); + lifecycleToken.close(); } } diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index 9f330348b0..7058083ac9 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -3,6 +3,7 @@ package io.sentry.util import io.sentry.CheckInStatus import io.sentry.HubScopesWrapper import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.MonitorConfig import io.sentry.MonitorSchedule import io.sentry.MonitorScheduleUnit @@ -58,16 +59,21 @@ class CheckInUtilsTest { fun `sends check-in for wrapped supplier`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() + val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + sentry.`when` { Sentry.pushIsolationScope() }.then { + scopes.pushIsolationScope() + lifecycleToken + } whenever(scopes.options).thenReturn(SentryOptions()) val returnValue = CheckInUtils.withCheckIn("monitor-1") { return@withCheckIn "test1" } assertEquals("test1", returnValue) - inOrder(scopes) { - verify(scopes).pushScope() + inOrder(scopes, lifecycleToken) { + verify(scopes).pushIsolationScope() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -81,7 +87,7 @@ class CheckInUtilsTest { assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(scopes).popScope() + verify(lifecycleToken).close() } } } @@ -90,8 +96,13 @@ class CheckInUtilsTest { fun `sends check-in for wrapped supplier with exception`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() + val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + sentry.`when` { Sentry.pushIsolationScope() }.then { + scopes.pushIsolationScope() + lifecycleToken + } try { CheckInUtils.withCheckIn("monitor-1") { @@ -102,8 +113,8 @@ class CheckInUtilsTest { assertEquals("thrown on purpose", e.message) } - inOrder(scopes) { - verify(scopes).pushScope() + inOrder(scopes, lifecycleToken) { + verify(scopes).pushIsolationScope() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -117,7 +128,7 @@ class CheckInUtilsTest { assertEquals(CheckInStatus.ERROR.apiName(), it.status) } ) - verify(scopes).popScope() + verify(lifecycleToken).close() } } } @@ -126,8 +137,13 @@ class CheckInUtilsTest { fun `sends check-in for wrapped supplier with upsert`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() + val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + sentry.`when` { Sentry.pushIsolationScope() }.then { + scopes.pushIsolationScope() + lifecycleToken + } whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)) val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) { @@ -135,8 +151,8 @@ class CheckInUtilsTest { } assertEquals("test1", returnValue) - inOrder(scopes) { - verify(scopes).pushScope() + inOrder(scopes, lifecycleToken) { + verify(scopes).pushIsolationScope() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -151,7 +167,7 @@ class CheckInUtilsTest { assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(scopes).popScope() + verify(lifecycleToken).close() } } } @@ -160,8 +176,13 @@ class CheckInUtilsTest { fun `sends check-in for wrapped supplier with upsert and thresholds`() { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() + val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) + sentry.`when` { Sentry.pushIsolationScope() }.then { + scopes.pushIsolationScope() + lifecycleToken + } whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)).apply { failureIssueThreshold = 10 @@ -172,8 +193,8 @@ class CheckInUtilsTest { } assertEquals("test1", returnValue) - inOrder(scopes) { - verify(scopes).pushScope() + inOrder(scopes, lifecycleToken) { + verify(scopes).pushIsolationScope() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -188,7 +209,7 @@ class CheckInUtilsTest { assertEquals(CheckInStatus.OK.apiName(), it.status) } ) - verify(scopes).popScope() + verify(lifecycleToken).close() } } } From b01298b7cb497afaa90a6460204753f90ea03d35 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 14:03:52 +0200 Subject: [PATCH 27/57] Add lifecycle helper --- .../main/java/io/sentry/util/LifecycleHelper.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 sentry/src/main/java/io/sentry/util/LifecycleHelper.java diff --git a/sentry/src/main/java/io/sentry/util/LifecycleHelper.java b/sentry/src/main/java/io/sentry/util/LifecycleHelper.java new file mode 100644 index 0000000000..4a029f620c --- /dev/null +++ b/sentry/src/main/java/io/sentry/util/LifecycleHelper.java @@ -0,0 +1,15 @@ +package io.sentry.util; + +import io.sentry.ISentryLifecycleToken; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class LifecycleHelper { + + public static void close(final @Nullable Object tokenObject) { + if (tokenObject != null && tokenObject instanceof ISentryLifecycleToken) { + final @NotNull ISentryLifecycleToken token = (ISentryLifecycleToken) tokenObject; + token.close(); + } + } +} From b64e6884cf81762062d1b5447bacc5242928d2d8 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 14:05:37 +0200 Subject: [PATCH 28/57] Change spring integrations to use new API --- .../api/sentry-spring-jakarta.api | 8 +-- .../spring/jakarta/SentrySpringFilter.java | 5 +- .../spring/jakarta/SentryTaskDecorator.java | 12 ++-- .../tracing/SentryTransactionAdvice.java | 5 +- .../webflux/AbstractSentryWebFilter.java | 6 -- .../spring/jakarta/webflux/ReactorUtils.java | 65 ++++++++----------- .../jakarta/webflux/SentryScheduleHook.java | 13 ++-- .../webflux/SentryWebExceptionHandler.java | 2 +- .../jakarta/webflux/SentryWebFilter.java | 2 +- ...entryWebFilterWithThreadLocalAccessor.java | 2 +- .../spring/jakarta/SentrySpringFilterTest.kt | 9 ++- .../spring/jakarta/SentryTaskDecoratorTest.kt | 18 ++--- .../tracing/SentryTransactionAdviceTest.kt | 8 ++- .../jakarta/webflux/ReactorUtilsTest.kt | 62 +++++++++--------- .../jakarta/webflux/SentryScheduleHookTest.kt | 18 ++--- .../webflux/SentryWebFluxTracingFilterTest.kt | 8 +-- .../io/sentry/spring/SentrySpringFilter.java | 5 +- .../io/sentry/spring/SentryTaskDecorator.java | 12 ++-- .../tracing/SentryTransactionAdvice.java | 5 +- .../spring/webflux/SentryScheduleHook.java | 13 ++-- .../spring/webflux/SentryWebFilter.java | 5 +- .../sentry/spring/SentrySpringFilterTest.kt | 9 ++- .../sentry/spring/SentryTaskDecoratorTest.kt | 18 ++--- .../tracing/SentryTransactionAdviceTest.kt | 8 ++- .../spring/webflux/SentryScheduleHookTest.kt | 18 ++--- .../webflux/SentryWebFluxTracingFilterTest.kt | 4 +- 26 files changed, 159 insertions(+), 181 deletions(-) diff --git a/sentry-spring-jakarta/api/sentry-spring-jakarta.api b/sentry-spring-jakarta/api/sentry-spring-jakarta.api index 13eb6033f9..156ab1aaee 100644 --- a/sentry-spring-jakarta/api/sentry-spring-jakarta.api +++ b/sentry-spring-jakarta/api/sentry-spring-jakarta.api @@ -282,10 +282,10 @@ public final class io/sentry/spring/jakarta/webflux/ReactorUtils { public fun ()V public static fun withSentry (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentry (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; - public static fun withSentryHub (Lreactor/core/publisher/Flux;Lio/sentry/IScopes;)Lreactor/core/publisher/Flux; - public static fun withSentryHub (Lreactor/core/publisher/Mono;Lio/sentry/IScopes;)Lreactor/core/publisher/Mono; - public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; - public static fun withSentryNewMainHubClone (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; + public static fun withSentryForkedRoots (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; + public static fun withSentryForkedRoots (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; + public static fun withSentryScopes (Lreactor/core/publisher/Flux;Lio/sentry/IScopes;)Lreactor/core/publisher/Flux; + public static fun withSentryScopes (Lreactor/core/publisher/Mono;Lio/sentry/IScopes;)Lreactor/core/publisher/Mono; } public final class io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor : io/micrometer/context/ThreadLocalAccessor { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index be06d3d253..2e3561a982 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -9,6 +9,7 @@ import io.sentry.EventProcessor; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -60,7 +61,7 @@ protected void doFilterInternal( if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); @@ -70,7 +71,7 @@ protected void doFilterInternal( configureScope(request); filterChain.doFilter(request, response); } finally { - scopes.popScope(); + lifecycleToken.close(); } } else { filterChain.doFilter(servletRequest, response); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java index c99abf3e21..943a7cc5ff 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java @@ -1,6 +1,7 @@ package io.sentry.spring.jakarta; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -14,18 +15,13 @@ */ public final class SentryTaskDecorator implements TaskDecorator { @Override - @SuppressWarnings("deprecation") + // TODO should there also be a SentryIsolatedTaskDecorator or similar that uses forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - // TODO fork - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (final @NotNull ISentryLifecycleToken ignored = newScopes.makeCurrent()) { runnable.run(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java index f04b3dd7a6..da781afcd8 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java @@ -2,6 +2,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ITransaction; import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; @@ -68,7 +69,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = @@ -86,7 +87,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - scopes.popScope(); + lifecycleToken.close(); } } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java index 3321874dd8..5995728785 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java @@ -68,10 +68,6 @@ protected void doFinally( if (transaction != null) { finishTransaction(serverWebExchange, transaction); } - if (requestHub.isEnabled()) { - // TODO close lifecycle token instead of popscope - requestHub.popScope(); - } Sentry.setCurrentScopes(NoOpScopes.getInstance()); } @@ -79,8 +75,6 @@ protected void doFirst( final @NotNull ServerWebExchange serverWebExchange, final @NotNull IScopes requestHub) { if (requestHub.isEnabled()) { serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); - // TODO fork instead - requestHub.pushScope(); final ServerHttpRequest request = serverWebExchange.getRequest(); final ServerHttpResponse response = serverWebExchange.getResponse(); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java index 41dd2e4bc0..cc7509129e 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java @@ -8,8 +8,9 @@ import reactor.core.publisher.Mono; import reactor.util.context.Context; -// TODO deprecate and replace with "withSentryScopes" etc. @ApiStatus.Experimental +// TODO do we keep old methods around and deprecate them? +// TODO do we need to offer isolated variants? public final class ReactorUtils { /** @@ -20,27 +21,23 @@ public final class ReactorUtils { * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - @SuppressWarnings("deprecation") public static Mono withSentry(final @NotNull Mono mono) { - final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); - // TODO fork - final @NotNull IScopes clonedHub = oldHub.clone(); - return withSentryHub(mono, clonedHub); + final @NotNull IScopes oldScopes = Sentry.getCurrentScopes(); + final @NotNull IScopes forkedScopes = oldScopes.forkedCurrentScope("reactor.withSentry"); + return withSentryScopes(mono, forkedScopes); } /** - * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main scopes to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) { - final @NotNull IScopes hub = Sentry.cloneMainHub(); - return withSentryHub(mono, hub); + public static Mono withSentryForkedRoots(final @NotNull Mono mono) { + final @NotNull IScopes scopes = Sentry.forkedRootScopes("reactor"); + return withSentryScopes(mono, scopes); } /** @@ -51,17 +48,16 @@ public static Mono withSentryNewMainHubClone(final @NotNull Mono mono) * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - public static Mono withSentryHub(final @NotNull Mono mono, final @NotNull IScopes hub) { + public static Mono withSentryScopes( + final @NotNull Mono mono, final @NotNull IScopes scopes) { /** - * WARNING: Cannot set the hub as current. It would be used by others to clone again causing - * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in - * events. + * WARNING: Cannot set the scopes as current. It would be used by others to clone again causing + * shared scopes and thus leading to issues like unrelated breadcrumbs showing up in events. */ - // Sentry.setCurrentHub(clonedHub); + // Sentry.setCurrentScopes(forkedScopes); return Mono.deferContextual(ctx -> mono) - .contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, hub)); + .contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, scopes)); } /** @@ -72,28 +68,24 @@ public static Mono withSentryHub(final @NotNull Mono mono, final @NotN * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - @SuppressWarnings("deprecation") public static Flux withSentry(final @NotNull Flux flux) { - final @NotNull IScopes oldHub = Sentry.getCurrentScopes(); - // TODO fork - final @NotNull IScopes clonedHub = oldHub.clone(); + final @NotNull IScopes oldScopes = Sentry.getCurrentScopes(); + final @NotNull IScopes forkedScopes = oldScopes.forkedCurrentScope("reactor.withSentry"); - return withSentryHub(flux, clonedHub); + return withSentryScopes(flux, forkedScopes); } /** - * Writes a new Sentry {@link IScopes} cloned from the main hub to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} cloned from the main scopes to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) { - final @NotNull IScopes hub = Sentry.cloneMainHub(); - return withSentryHub(flux, hub); + public static Flux withSentryForkedRoots(final @NotNull Flux flux) { + final @NotNull IScopes scopes = Sentry.forkedRootScopes("reactor"); + return withSentryScopes(flux, scopes); } /** @@ -104,16 +96,15 @@ public static Flux withSentryNewMainHubClone(final @NotNull Flux flux) * enabled - having `io.micrometer:context-propagation:1.0.2+` (provided by Spring Boot 3.0.3+) - * having `io.projectreactor:reactor-core:3.5.3+` (provided by Spring Boot 3.0.3+) */ - @ApiStatus.Experimental - public static Flux withSentryHub(final @NotNull Flux flux, final @NotNull IScopes hub) { + public static Flux withSentryScopes( + final @NotNull Flux flux, final @NotNull IScopes scopes) { /** - * WARNING: Cannot set the hub as current. It would be used by others to clone again causing - * shared hubs and scopes and thus leading to issues like unrelated breadcrumbs showing up in - * events. + * WARNING: Cannot set the scopes as current. It would be used by others to clone again causing + * shared scopes and thus leading to issues like unrelated breadcrumbs showing up in events. */ - // Sentry.setCurrentHub(clonedHub); + // Sentry.setCurrentScopes(forkedScopes); return Flux.deferContextual(ctx -> flux) - .contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, hub)); + .contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, scopes)); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java index 882a0b268a..57a74732ea 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryScheduleHook.java @@ -1,6 +1,7 @@ package io.sentry.spring.jakarta.webflux; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -8,23 +9,17 @@ /** * Hook meant to used with {@link reactor.core.scheduler.Schedulers#onScheduleHook(String, - * Function)} to configure Reactor to copy correct hub into the operating thread. + * Function)} to configure Reactor to copy correct scopes into the operating thread. */ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override - @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - // TODO fork instead - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("spring.scheduleHook"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (final @NotNull ISentryLifecycleToken ignored = newScopes.makeCurrent()) { runnable.run(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java index 40b0ed4e87..15c73ab625 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebExceptionHandler.java @@ -40,7 +40,7 @@ public SentryWebExceptionHandler(final @NotNull IScopes scopes) { serverWebExchange.getAttributeOrDefault(SentryWebFilter.SENTRY_SCOPES_KEY, null); final @NotNull IScopes scopesToUse = requestScopes != null ? requestScopes : scopes; - return ReactorUtils.withSentryHub( + return ReactorUtils.withSentryScopes( Mono.just(ex) .map( it -> { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java index dab985eecf..0a6b767ec4 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilter.java @@ -28,7 +28,7 @@ public SentryWebFilter(final @NotNull IScopes scopes) { public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IScopes requestScopes = Sentry.cloneMainHub(); + @NotNull IScopes requestScopes = Sentry.forkedRootScopes("request.webflux"); final ServerHttpRequest request = serverWebExchange.getRequest(); final @Nullable ITransaction transaction = maybeStartTransaction(requestScopes, request); if (transaction != null) { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java index e760ef8f3e..c38e322731 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java @@ -26,7 +26,7 @@ public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { final @NotNull TransactionContainer transactionContainer = new TransactionContainer(); - return ReactorUtils.withSentryNewMainHubClone( + return ReactorUtils.withSentryForkedRoots( webFilterChain .filter(serverWebExchange) .doFinally( diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt index b6bce77a0b..ac394deb31 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt @@ -3,6 +3,7 @@ package io.sentry.spring.jakarta import io.sentry.Breadcrumb import io.sentry.IScope import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,6 +40,7 @@ class SentrySpringFilterTest { private class Fixture { val scopes = mock() val response = MockHttpServletResponse() + val lifecycleToken = mock() val chain = mock() lateinit var scope: IScope lateinit var request: HttpServletRequest @@ -47,6 +49,7 @@ class SentrySpringFilterTest { scope = Scope(options) whenever(scopes.options).thenReturn(options) whenever(scopes.isEnabled).thenReturn(true) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { @@ -64,7 +67,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushIsolationScope() } @Test @@ -87,7 +90,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).popScope() + verify(fixture.lifecycleToken).close() } @Test @@ -99,7 +102,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.scopes).popScope() + verify(fixture.lifecycleToken).close() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt index e5f8704b49..d44e64f78d 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryTaskDecoratorTest.kt @@ -25,35 +25,35 @@ class SentryTaskDecoratorTest { } @Test - fun `hub is reset to its state within the thread after decoration is done`() { + fun `scopes is reset to its state within the thread after decoration is done`() { Sentry.init { it.dsn = dsn } val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt index 390b4d8241..b0f83782e0 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,7 @@ package io.sentry.spring.jakarta.tracing import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -46,6 +47,8 @@ class SentryTransactionAdviceTest { @Autowired lateinit var scopes: IScopes + val lifecycleToken = mock() + @BeforeTest fun setup() { reset(scopes) @@ -55,6 +58,7 @@ class SentryTransactionAdviceTest { dsn = "https://key@sentry.io/proj" } ) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } @Test @@ -141,13 +145,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(scopes).pushScope() + verify(scopes).pushIsolationScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(scopes).popScope() + verify(lifecycleToken).close() } @Configuration diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt index 9c851cde11..f3bd5d2653 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/ReactorUtilsTest.kt @@ -1,9 +1,9 @@ package io.sentry.spring.jakarta.webflux -import io.sentry.IHub import io.sentry.IScopes import io.sentry.NoOpScopes import io.sentry.Sentry +import org.mockito.kotlin.any import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.whenever @@ -31,88 +31,88 @@ class ReactorUtilsTest { } @Test - fun `propagates hub inside mono`() { - val hubToUse = mock() - var hubInside: IScopes? = null - val mono = ReactorUtils.withSentryHub( + fun `propagates scopes inside mono`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null + val mono = ReactorUtils.withSentryScopes( Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentScopes() + scopesInside = Sentry.getCurrentScopes() it }, - hubToUse + scopesToUse ) assertEquals("hello", mono.block()) - assertSame(hubToUse, hubInside) + assertSame(scopesToUse, scopesInside) } @Test - fun `propagates hub inside flux`() { - val hubToUse = mock() - var hubInside: IScopes? = null - val flux = ReactorUtils.withSentryHub( + fun `propagates scopes inside flux`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null + val flux = ReactorUtils.withSentryScopes( Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentScopes() + scopesInside = Sentry.getCurrentScopes() it }, - hubToUse + scopesToUse ) assertEquals("hello", flux.blockFirst()) - assertSame(hubToUse, hubInside) + assertSame(scopesToUse, scopesInside) } @Test - fun `without reactive utils hub is not propagated to mono`() { - val hubToUse = mock() - var hubInside: IScopes? = null + fun `without reactive utils scopes is not propagated to mono`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null val mono = Mono.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentScopes() + scopesInside = Sentry.getCurrentScopes() it } assertEquals("hello", mono.block()) - assertNotSame(hubToUse, hubInside) + assertNotSame(scopesToUse, scopesInside) } @Test - fun `without reactive utils hub is not propagated to flux`() { - val hubToUse = mock() - var hubInside: IScopes? = null + fun `without reactive utils scopes is not propagated to flux`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null val flux = Flux.just("hello") .publishOn(Schedulers.boundedElastic()) .map { it -> - hubInside = Sentry.getCurrentScopes() + scopesInside = Sentry.getCurrentScopes() it } assertEquals("hello", flux.blockFirst()) - assertNotSame(hubToUse, hubInside) + assertNotSame(scopesToUse, scopesInside) } @Test - fun `clones hub for mono`() { + fun `clones scopes for mono`() { val mockScopes = mock() - whenever(mockScopes.clone()).thenReturn(mock()) + whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Mono.just("hello")).block() - verify(mockScopes).clone() + verify(mockScopes).forkedCurrentScope(any()) } @Test - fun `clones hub for flux`() { + fun `clones scopes for flux`() { val mockScopes = mock() - whenever(mockScopes.clone()).thenReturn(mock()) + whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) Sentry.setCurrentScopes(mockScopes) ReactorUtils.withSentry(Flux.just("hello")).blockFirst() - verify(mockScopes).clone() + verify(mockScopes).forkedCurrentScope(any()) } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt index 5403caa7e0..4b540da1aa 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryScheduleHookTest.kt @@ -26,35 +26,35 @@ class SentryScheduleHookTest { } @Test - fun `hub is reset to its state within the thread after hook is done`() { + fun `scopes is reset to its state within the thread after hook is done`() { Sentry.init { it.dsn = dsn } val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt index ddbbe75817..44f4925c2d 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt @@ -89,7 +89,7 @@ class SentryWebFluxTracingFilterTest { fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) + it.`when` { Sentry.forkedRootScopes(any()) }.thenReturn(fixture.scopes) closure.invoke() } @@ -210,7 +210,7 @@ class SentryWebFluxTracingFilterTest { verify(fixture.chain).filter(fixture.exchange) - verify(fixture.scopes, times(3)).isEnabled + verify(fixture.scopes, times(2)).isEnabled verifyNoMoreInteractions(fixture.scopes) } } @@ -249,13 +249,11 @@ class SentryWebFluxTracingFilterTest { verify(fixture.chain).filter(fixture.exchange) - verify(fixture.scopes, times(3)).isEnabled + verify(fixture.scopes, times(2)).isEnabled verify(fixture.scopes, times(2)).options verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.scopes).pushScope() // TODO don't verify(fixture.scopes).addBreadcrumb(any(), any()) verify(fixture.scopes).configureScope(any()) - verify(fixture.scopes).popScope() // TODO don't verifyNoMoreInteractions(fixture.scopes) } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 7695545f04..252a07910c 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -9,6 +9,7 @@ import io.sentry.EventProcessor; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -60,7 +61,7 @@ protected void doFilterInternal( if (scopes.isEnabled()) { // request may qualify for caching request body, if so resolve cached request final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); try { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); @@ -70,7 +71,7 @@ protected void doFilterInternal( configureScope(request); filterChain.doFilter(request, response); } finally { - scopes.popScope(); + lifecycleToken.close(); } } else { filterChain.doFilter(servletRequest, response); diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index 88d205a57e..761038ece0 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -1,6 +1,7 @@ package io.sentry.spring; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.Sentry; import java.util.concurrent.Callable; import org.jetbrains.annotations.NotNull; @@ -14,18 +15,13 @@ */ public final class SentryTaskDecorator implements TaskDecorator { @Override - @SuppressWarnings("deprecation") + // TODO should there also be a SentryIsolatedTaskDecorator or similar that uses forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - // TODO fork instead - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newHub = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (final @NotNull ISentryLifecycleToken ignored = newHub.makeCurrent()) { runnable.run(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java index 8f4f5bbdfc..a885510fcd 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java @@ -2,6 +2,7 @@ import com.jakewharton.nopen.annotation.Open; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ITransaction; import io.sentry.ScopesAdapter; import io.sentry.SpanStatus; @@ -67,7 +68,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); final TransactionOptions transactionOptions = new TransactionOptions(); transactionOptions.setBindToScope(true); final ITransaction transaction = @@ -85,7 +86,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl throw e; } finally { transaction.finish(); - scopes.popScope(); + lifecycleToken.close(); } } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java index 20f494168d..50839e653e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryScheduleHook.java @@ -1,6 +1,7 @@ package io.sentry.spring.webflux; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.Sentry; import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; @@ -8,23 +9,17 @@ /** * Hook meant to used with {@link reactor.core.scheduler.Schedulers#onScheduleHook(String, - * Function)} to configure Reactor to copy correct hub into the operating thread. + * Function)} to configure Reactor to copy correct scopes into the operating thread. */ @ApiStatus.Experimental public final class SentryScheduleHook implements Function { @Override - @SuppressWarnings("deprecation") public Runnable apply(final @NotNull Runnable runnable) { - // TODO fork instead - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("spring.scheduleHook"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (final @NotNull ISentryLifecycleToken ignored = newScopes.makeCurrent()) { runnable.run(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index 4d39e092bc..10e80ebe8b 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -50,7 +50,7 @@ public SentryWebFilter(final @NotNull IScopes scopes) { public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IScopes requestHub = Sentry.cloneMainHub(); + @NotNull IScopes requestHub = Sentry.forkedRootScopes("request.webflux"); // TODO do not push / pop, use fork instead if (!requestHub.isEnabled()) { return webFilterChain.filter(serverWebExchange); @@ -81,8 +81,6 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) if (transaction != null) { finishTransaction(serverWebExchange, transaction); } - requestHub.popScope(); // TODO don't - // TODO token based cleanup instead? Sentry.setCurrentScopes(NoOpScopes.getInstance()); }) .doOnError( @@ -96,7 +94,6 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) () -> { serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); Sentry.setCurrentScopes(requestHub); - requestHub.pushScope(); // TODO don't final ServerHttpResponse response = serverWebExchange.getResponse(); final Hint hint = new Hint(); diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt index c6ac952531..6037e253c8 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt @@ -3,6 +3,7 @@ package io.sentry.spring import io.sentry.Breadcrumb import io.sentry.IScope import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -39,6 +40,7 @@ class SentrySpringFilterTest { private class Fixture { val scopes = mock() val response = MockHttpServletResponse() + val lifecycleToken = mock() val chain = mock() lateinit var scope: IScope lateinit var request: HttpServletRequest @@ -47,6 +49,7 @@ class SentrySpringFilterTest { scope = Scope(options) whenever(scopes.options).thenReturn(options) whenever(scopes.isEnabled).thenReturn(true) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { @@ -64,7 +67,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushIsolationScope() } @Test @@ -87,7 +90,7 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).popScope() + verify(fixture.lifecycleToken).close() } @Test @@ -99,7 +102,7 @@ class SentrySpringFilterTest { listener.doFilter(fixture.request, fixture.response, fixture.chain) fail() } catch (e: Exception) { - verify(fixture.scopes).popScope() + verify(fixture.lifecycleToken).close() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt index 3f34ab9d9d..4bbce919eb 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryTaskDecoratorTest.kt @@ -25,35 +25,35 @@ class SentryTaskDecoratorTest { } @Test - fun `hub is reset to its state within the thread after decoration is done`() { + fun `scopes is reset to its state within the thread after decoration is done`() { Sentry.init { it.dsn = dsn } val sut = SentryTaskDecorator() - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.decorate { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt index f53acde8aa..8a3d8ee46c 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt @@ -1,6 +1,7 @@ package io.sentry.spring.tracing import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import io.sentry.Sentry import io.sentry.SentryOptions import io.sentry.SentryTracer @@ -46,6 +47,8 @@ class SentryTransactionAdviceTest { @Autowired lateinit var scopes: IScopes + val lifecycleToken = mock() + @BeforeTest fun setup() { reset(scopes) @@ -55,6 +58,7 @@ class SentryTransactionAdviceTest { dsn = "https://key@sentry.io/proj" } ) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } @Test @@ -141,13 +145,13 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(scopes).pushScope() + verify(scopes).pushIsolationScope() } @Test fun `pops the scope when advice finishes`() { classAnnotatedSampleService.hello() - verify(scopes).popScope() + verify(lifecycleToken).close() } @Configuration diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt index 7a8b2993f9..88c33c3695 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryScheduleHookTest.kt @@ -26,35 +26,35 @@ class SentryScheduleHookTest { } @Test - fun `hub is reset to its state within the thread after hook is done`() { + fun `scopes is reset to its state within the thread after hook is done`() { Sentry.init { it.dsn = dsn } val sut = SentryScheduleHook() - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentHub(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( sut.apply { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) } ) callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt index 1a31dcaa10..ff527abd7d 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt @@ -89,7 +89,7 @@ class SentryWebFluxTracingFilterTest { fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) - it.`when` { Sentry.cloneMainHub() }.thenReturn(fixture.scopes) + it.`when` { Sentry.forkedRootScopes(any()) }.thenReturn(fixture.scopes) closure.invoke() } @@ -253,10 +253,8 @@ class SentryWebFluxTracingFilterTest { verify(fixture.scopes).isEnabled verify(fixture.scopes, times(2)).options verify(fixture.scopes).continueTrace(anyOrNull(), anyOrNull()) - verify(fixture.scopes).pushScope() // TODO don't verify(fixture.scopes).addBreadcrumb(any(), any()) verify(fixture.scopes).configureScope(any()) - verify(fixture.scopes).popScope() // TODO don't verifyNoMoreInteractions(fixture.scopes) } } From d06fc50cec696ab6ec812bce48c6b95fd18de696 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 14:36:36 +0200 Subject: [PATCH 29/57] Use new API in servlet integrations --- .../api/sentry-servlet-jakarta.api | 1 + .../jakarta/SentryServletRequestListener.java | 10 ++++++++-- .../jakarta/SentryServletRequestListenerTest.kt | 13 ++++++++++--- sentry-servlet/api/sentry-servlet.api | 1 + .../servlet/SentryServletRequestListener.java | 10 ++++++++-- .../servlet/SentryServletRequestListenerTest.kt | 12 +++++++++--- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api index a5421e7453..adde86fda5 100644 --- a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api +++ b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api @@ -9,6 +9,7 @@ public class io/sentry/servlet/jakarta/SentryServletContainerInitializer : jakar } public class io/sentry/servlet/jakarta/SentryServletRequestListener : jakarta/servlet/ServletRequestListener { + public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljakarta/servlet/ServletRequestEvent;)V diff --git a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java index 54775386fd..f5b3be30b9 100644 --- a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java +++ b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java @@ -6,7 +6,9 @@ import io.sentry.Breadcrumb; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; +import io.sentry.util.LifecycleHelper; import io.sentry.util.Objects; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletRequestEvent; @@ -21,6 +23,8 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { + public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; + private final IScopes scopes; public SentryServletRequestListener(@NotNull IScopes scopes) { @@ -33,14 +37,16 @@ public SentryServletRequestListener() { @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - scopes.popScope(); + final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); + LifecycleHelper.close(servletRequest.getAttribute(SENTRY_LIFECYCLE_TOKEN_KEY)); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); + servletRequest.setAttribute(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); if (servletRequest instanceof HttpServletRequest) { final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; diff --git a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt index 3be76d1cd2..30ef3da1ed 100644 --- a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt +++ b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt @@ -2,10 +2,13 @@ package io.sentry.servlet.jakarta import io.sentry.Breadcrumb import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import jakarta.servlet.ServletRequestEvent import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check +import org.mockito.kotlin.eq import org.mockito.kotlin.mock +import org.mockito.kotlin.same import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import kotlin.test.Test @@ -14,6 +17,7 @@ import kotlin.test.assertEquals class SentryServletRequestListenerTest { private class Fixture { val scopes = mock() + val lifecycleToken = mock() val listener = SentryServletRequestListener(scopes) val request = mockRequest( @@ -24,6 +28,7 @@ class SentryServletRequestListenerTest { init { whenever(event.servletRequest).thenReturn(request) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } } @@ -33,7 +38,7 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushIsolationScope() } @Test @@ -48,12 +53,14 @@ class SentryServletRequestListenerTest { }, anyOrNull() ) + verify(fixture.request).setAttribute(eq("sentry-lifecycle"), same(fixture.lifecycleToken)) } @Test fun `pops scope when request gets destroyed`() { - fixture.listener.requestDestroyed(fixture.event) + whenever(fixture.request.getAttribute(eq("sentry-lifecycle"))).thenReturn(fixture.lifecycleToken) - verify(fixture.scopes).popScope() + fixture.listener.requestDestroyed(fixture.event) + verify(fixture.lifecycleToken).close() } } diff --git a/sentry-servlet/api/sentry-servlet.api b/sentry-servlet/api/sentry-servlet.api index fd7aee819b..63d3cf4b33 100644 --- a/sentry-servlet/api/sentry-servlet.api +++ b/sentry-servlet/api/sentry-servlet.api @@ -9,6 +9,7 @@ public class io/sentry/servlet/SentryServletContainerInitializer : javax/servlet } public class io/sentry/servlet/SentryServletRequestListener : javax/servlet/ServletRequestListener { + public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljavax/servlet/ServletRequestEvent;)V diff --git a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java index 97c37e1133..4587daa655 100644 --- a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java +++ b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java @@ -6,7 +6,9 @@ import io.sentry.Breadcrumb; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryLifecycleToken; import io.sentry.ScopesAdapter; +import io.sentry.util.LifecycleHelper; import io.sentry.util.Objects; import javax.servlet.ServletRequest; import javax.servlet.ServletRequestEvent; @@ -21,6 +23,8 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { + public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; + private final IScopes scopes; public SentryServletRequestListener(@NotNull IScopes scopes) { @@ -33,14 +37,16 @@ public SentryServletRequestListener() { @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { - scopes.popScope(); + final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); + LifecycleHelper.close(servletRequest.getAttribute(SENTRY_LIFECYCLE_TOKEN_KEY)); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - scopes.pushScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); + servletRequest.setAttribute(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); if (servletRequest instanceof HttpServletRequest) { final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; diff --git a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt index bfa216f738..d72b93179f 100644 --- a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt +++ b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt @@ -2,6 +2,7 @@ package io.sentry.servlet import io.sentry.Breadcrumb import io.sentry.IScopes +import io.sentry.ISentryLifecycleToken import org.assertj.core.api.Assertions.assertThat import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check @@ -11,10 +12,12 @@ import org.mockito.kotlin.whenever import org.springframework.mock.web.MockHttpServletRequest import javax.servlet.ServletRequestEvent import kotlin.test.Test +import kotlin.test.assertSame class SentryServletRequestListenerTest { private class Fixture { val scopes = mock() + val lifecycleToken = mock() val listener = SentryServletRequestListener(scopes) val request = MockHttpServletRequest() val event = mock() @@ -23,6 +26,7 @@ class SentryServletRequestListenerTest { request.requestURI = "http://localhost:8080/some-uri" request.method = "post" whenever(event.servletRequest).thenReturn(request) + whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) } } @@ -32,7 +36,7 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.scopes).pushScope() + verify(fixture.scopes).pushIsolationScope() } @Test @@ -47,12 +51,14 @@ class SentryServletRequestListenerTest { }, anyOrNull() ) + assertSame(fixture.lifecycleToken, fixture.request.getAttribute("sentry-lifecycle")) } @Test fun `pops scope when request gets destroyed`() { - fixture.listener.requestDestroyed(fixture.event) + fixture.request.setAttribute("sentry-lifecycle", fixture.lifecycleToken) - verify(fixture.scopes).popScope() + fixture.listener.requestDestroyed(fixture.event) + verify(fixture.lifecycleToken).close() } } From f0af5c3dfc10f97a02b83c4f4154c1a76acc3c45 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 14:52:06 +0200 Subject: [PATCH 30/57] Use new API for kotlin coroutines and wrapers for Supplier/Callable --- .../java/io/sentry/kotlin/SentryContext.kt | 10 +- .../io/sentry/kotlin/SentryContextTest.kt | 138 +++++++++++-- .../main/java/io/sentry/SentryWrapper.java | 41 ++-- .../test/java/io/sentry/SentryWrapperTest.kt | 187 +++++++++++++++++- 4 files changed, 329 insertions(+), 47 deletions(-) diff --git a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt index 4c814f2805..a77281a033 100644 --- a/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt +++ b/sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt @@ -9,23 +9,19 @@ import kotlin.coroutines.CoroutineContext /** * Sentry context element for [CoroutineContext]. */ -@SuppressWarnings("deprecation") -// TODO fork instead -public class SentryContext(private val scopes: IScopes = Sentry.getCurrentScopes().clone()) : +public class SentryContext(private val scopes: IScopes = Sentry.forkedCurrentScope("coroutine")) : CopyableThreadContextElement, AbstractCoroutineContextElement(Key) { private companion object Key : CoroutineContext.Key @SuppressWarnings("deprecation") override fun copyForChild(): CopyableThreadContextElement { - // TODO fork instead - return SentryContext(scopes.clone()) + return SentryContext(scopes.forkedCurrentScope("coroutine.child")) } @SuppressWarnings("deprecation") override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext { - // TODO fork instead? - return overwritingElement[Key] ?: SentryContext(scopes.clone()) + return overwritingElement[Key] ?: SentryContext(scopes.forkedCurrentScope("coroutine.child")) } override fun updateThreadContext(context: CoroutineContext): IScopes { diff --git a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt index 578b610267..bd498846dd 100644 --- a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt +++ b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt @@ -1,5 +1,6 @@ package io.sentry.kotlin +import io.sentry.ScopeType import io.sentry.Sentry import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.joinAll @@ -38,11 +39,40 @@ class SentryContextTest { Sentry.setTag("c2", "c2value") assertEquals("c2value", getTag("c2")) assertEquals("parentValue", getTag("parent")) - assertNull(getTag("c1")) + assertNotNull(getTag("c1")) + } + listOf(c1, c2).joinAll() + assertNotNull(getTag("parent")) + assertNotNull(getTag("c1")) + assertNotNull(getTag("c2")) + return@runBlocking + } + + @Test + fun testContextIsNotPassedByDefaultBetweenCoroutinesCurrentScope() = runBlocking { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("parent", "parentValue") + } + val c1 = launch(SentryContext()) { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c1", "c1value") + } + assertEquals("c1value", getTag("c1", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + } + val c2 = launch(SentryContext()) { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c2", "c2value") + } + assertEquals("c2value", getTag("c2", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c1", ScopeType.CURRENT)) } listOf(c1, c2).joinAll() - assertNull(getTag("c1")) - assertNull(getTag("c2")) + assertNotNull(getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c1", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) } @Test @@ -84,7 +114,7 @@ class SentryContextTest { } @Test - fun testContextIsClonedWhenPassedToChild() = runBlocking { + fun testContextIsClonedWhenPassedToChildCurrentScope() = runBlocking { Sentry.setTag("parent", "parentValue") launch(SentryContext()) { Sentry.setTag("c1", "c1value") @@ -102,10 +132,44 @@ class SentryContextTest { c2.join() assertNotNull(getTag("c1")) - assertNull(getTag("c2")) + assertNotNull(getTag("c2")) + }.join() + assertNotNull(getTag("parent")) + assertNotNull(getTag("c1")) + assertNotNull(getTag("c2")) + return@runBlocking + } + + @Test + fun testContextIsClonedWhenPassedToChild() = runBlocking { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("parent", "parentValue") } - assertNull(getTag("c1")) - assertNull(getTag("c2")) + launch(SentryContext()) { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c1", "c1value") + } + assertEquals("c1value", getTag("c1", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + + val c2 = launch() { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c2", "c2value") + } + assertEquals("c2value", getTag("c2", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNotNull(getTag("c1", ScopeType.CURRENT)) + } + + c2.join() + + assertNotNull(getTag("c1", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + }.join() + assertNotNull(getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c1", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) } @Test @@ -120,7 +184,7 @@ class SentryContextTest { val c2 = launch( SentryContext( Sentry.getCurrentScopes().clone().also { - it.setTag("cloned", "clonedValue") + Sentry.setTag("cloned", "clonedValue") } ) ) { @@ -134,12 +198,56 @@ class SentryContextTest { c2.join() assertNotNull(getTag("c1")) - assertNull(getTag("c2")) - assertNull(getTag("cloned")) + assertNotNull(getTag("c2")) + assertNotNull(getTag("cloned")) + }.join() + + assertNotNull(getTag("c1")) + assertNotNull(getTag("c2")) + assertNotNull(getTag("cloned")) + return@runBlocking + } + + @Test + fun testExplicitlyPassedContextOverridesPropagatedContextCurrentScope() = runBlocking { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("parent", "parentValue") + } + launch(SentryContext()) { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c1", "c1value") + } + assertEquals("c1value", getTag("c1", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + + val c2 = launch( + SentryContext( + Sentry.getCurrentScopes().clone().also { + it.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("cloned", "clonedValue") + } + } + ) + ) { + Sentry.configureScope(ScopeType.CURRENT) { scope -> + scope.setTag("c2", "c2value") + } + assertEquals("c2value", getTag("c2", ScopeType.CURRENT)) + assertEquals("parentValue", getTag("parent", ScopeType.CURRENT)) + assertNotNull(getTag("c1", ScopeType.CURRENT)) + assertNotNull(getTag("cloned", ScopeType.CURRENT)) + } + + c2.join() + + assertNotNull(getTag("c1", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + assertNull(getTag("cloned", ScopeType.CURRENT)) } - assertNull(getTag("c1")) - assertNull(getTag("c2")) - assertNull(getTag("cloned")) + assertNull(getTag("c1", ScopeType.CURRENT)) + assertNull(getTag("c2", ScopeType.CURRENT)) + assertNull(getTag("cloned", ScopeType.CURRENT)) } @Test @@ -167,9 +275,9 @@ class SentryContextTest { assertEquals(initialContextElement, mergedContextElement) } - private fun getTag(tag: String): String? { + private fun getTag(tag: String, scopeType: ScopeType = ScopeType.ISOLATION): String? { var value: String? = null - Sentry.configureScope { + Sentry.configureScope(scopeType) { value = it.tags[tag] } return value diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index 165ace7c83..aba5a4b778 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -27,18 +27,23 @@ public final class SentryWrapper { * @return the wrapped {@link Callable} * @param - the result type of the {@link Callable} */ - @SuppressWarnings("deprecation") + // TODO adapt javadoc public static Callable wrapCallable(final @NotNull Callable callable) { - // TODO replace with forking - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("wrapCallable"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (ISentryLifecycleToken ignored = newScopes.makeCurrent()) { + return callable.call(); + } + }; + } + + public static Callable wrapCallableIsolated(final @NotNull Callable callable) { + final IScopes newScopes = Sentry.getCurrentScopes().forkedScopes("wrapCallable"); + + return () -> { + try (ISentryLifecycleToken ignored = newScopes.makeCurrent()) { return callable.call(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } @@ -53,18 +58,22 @@ public static Callable wrapCallable(final @NotNull Callable callable) * @return the wrapped {@link Supplier} * @param - the result type of the {@link Supplier} */ - @SuppressWarnings("deprecation") public static Supplier wrapSupplier(final @NotNull Supplier supplier) { - // TODO replace with forking - final IScopes newHub = Sentry.getCurrentScopes().clone(); + final IScopes newScopes = Sentry.forkedCurrentScope("wrapSupplier"); + + return () -> { + try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) { + return supplier.get(); + } + }; + } + + public static Supplier wrapSupplierIsolated(final @NotNull Supplier supplier) { + final IScopes newScopes = Sentry.forkedScopes("wrapSupplier"); return () -> { - final IScopes oldState = Sentry.getCurrentScopes(); - Sentry.setCurrentScopes(newHub); - try { + try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) { return supplier.get(); - } finally { - Sentry.setCurrentScopes(oldState); } }; } diff --git a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt index 2fb9b38566..a3511450f0 100644 --- a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt +++ b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt @@ -36,7 +36,7 @@ class SentryWrapperTest { } val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val threadedHub = mainHub.forkedCurrentScope("test") executor.submit { Sentry.setCurrentScopes(threadedHub) @@ -46,7 +46,7 @@ class SentryWrapperTest { val callableFuture = CompletableFuture.supplyAsync( - SentryWrapper.wrapSupplier { + SentryWrapper.wrapSupplierIsolated { assertNotEquals(mainHub, Sentry.getCurrentScopes()) assertNotEquals(threadedHub, Sentry.getCurrentScopes()) "Result 1" @@ -63,7 +63,7 @@ class SentryWrapperTest { } @Test - fun `wrapped supply async isolates Hubs`() { + fun `wrapped supply async does not isolate Scopes`() { val capturedEvents = mutableListOf() Sentry.init { @@ -110,12 +110,12 @@ class SentryWrapperTest { val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } assertEquals(2, mainEvent?.breadcrumbs?.size) - assertEquals(2, clonedEvent?.breadcrumbs?.size) - assertEquals(2, clonedEvent2?.breadcrumbs?.size) + assertEquals(3, clonedEvent?.breadcrumbs?.size) + assertEquals(4, clonedEvent2?.breadcrumbs?.size) } @Test - fun `wrapped callable isolates Hubs`() { + fun `wrapped callable does not isolate Scopes`() { val capturedEvents = mutableListOf() Sentry.init { @@ -159,8 +159,8 @@ class SentryWrapperTest { val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } assertEquals(2, mainEvent?.breadcrumbs?.size) - assertEquals(2, clonedEvent?.breadcrumbs?.size) - assertEquals(2, clonedEvent2?.breadcrumbs?.size) + assertEquals(3, clonedEvent?.breadcrumbs?.size) + assertEquals(4, clonedEvent2?.breadcrumbs?.size) } @Test @@ -170,7 +170,7 @@ class SentryWrapperTest { } val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().clone() + val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { Sentry.setCurrentScopes(threadedHub) @@ -194,4 +194,173 @@ class SentryWrapperTest { assertEquals(threadedHub, Sentry.getCurrentScopes()) }.get() } + + @Test + fun `scopes is reset to its state within the thread after isolated supply is done`() { + Sentry.init { + it.dsn = dsn + it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> + event + } + } + + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") + + executor.submit { + Sentry.setCurrentScopes(threadedHub) + }.get() + + assertEquals(mainHub, Sentry.getCurrentScopes()) + + val callableFuture = + CompletableFuture.supplyAsync( + SentryWrapper.wrapSupplierIsolated { + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + "Result 1" + }, + executor + ) + + callableFuture.join() + + executor.submit { + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) + }.get() + } + + @Test + fun `wrapped supply async isolates Scopes`() { + val capturedEvents = mutableListOf() + + Sentry.init { + it.dsn = dsn + it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> + capturedEvents.add(event) + event + } + } + + Sentry.addBreadcrumb("MyOriginalBreadcrumbBefore") + Sentry.captureMessage("OriginalMessageBefore") + + val callableFuture = + CompletableFuture.supplyAsync( + SentryWrapper.wrapSupplierIsolated { + Thread.sleep(20) + Sentry.addBreadcrumb("MyClonedBreadcrumb") + Sentry.captureMessage("ClonedMessage") + "Result 1" + }, + executor + ) + + val callableFuture2 = + CompletableFuture.supplyAsync( + SentryWrapper.wrapSupplierIsolated { + Thread.sleep(10) + Sentry.addBreadcrumb("MyClonedBreadcrumb2") + Sentry.captureMessage("ClonedMessage2") + "Result 2" + }, + executor + ) + + Sentry.addBreadcrumb("MyOriginalBreadcrumb") + Sentry.captureMessage("OriginalMessage") + + callableFuture.join() + callableFuture2.join() + + val mainEvent = capturedEvents.firstOrNull { it.message?.formatted == "OriginalMessage" } + val clonedEvent = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage" } + val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } + + assertEquals(2, mainEvent?.breadcrumbs?.size) + assertEquals(2, clonedEvent?.breadcrumbs?.size) + assertEquals(2, clonedEvent2?.breadcrumbs?.size) + } + + @Test + fun `wrapped callable isolates Scopes`() { + val capturedEvents = mutableListOf() + + Sentry.init { + it.dsn = dsn + it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> + capturedEvents.add(event) + event + } + } + + Sentry.addBreadcrumb("MyOriginalBreadcrumbBefore") + Sentry.captureMessage("OriginalMessageBefore") + println(Thread.currentThread().name) + + val future1 = executor.submit( + SentryWrapper.wrapCallableIsolated { + Thread.sleep(20) + Sentry.addBreadcrumb("MyClonedBreadcrumb") + Sentry.captureMessage("ClonedMessage") + "Result 1" + } + ) + + val future2 = executor.submit( + SentryWrapper.wrapCallableIsolated { + Thread.sleep(10) + Sentry.addBreadcrumb("MyClonedBreadcrumb2") + Sentry.captureMessage("ClonedMessage2") + "Result 2" + } + ) + + Sentry.addBreadcrumb("MyOriginalBreadcrumb") + Sentry.captureMessage("OriginalMessage") + + future1.get() + future2.get() + + val mainEvent = capturedEvents.firstOrNull { it.message?.formatted == "OriginalMessage" } + val clonedEvent = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage" } + val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } + + assertEquals(2, mainEvent?.breadcrumbs?.size) + assertEquals(2, clonedEvent?.breadcrumbs?.size) + assertEquals(2, clonedEvent2?.breadcrumbs?.size) + } + + @Test + fun `scopes is reset to its state within the thread after isolated callable is done`() { + Sentry.init { + it.dsn = dsn + } + + val mainHub = Sentry.getCurrentScopes() + val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") + + executor.submit { + Sentry.setCurrentScopes(threadedHub) + }.get() + + assertEquals(mainHub, Sentry.getCurrentScopes()) + + val callableFuture = + executor.submit( + SentryWrapper.wrapCallableIsolated { + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + "Result 1" + } + ) + + callableFuture.get() + + executor.submit { + assertNotEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(threadedHub, Sentry.getCurrentScopes()) + }.get() + } } From 2f02001865e316ba97841ff28bcbcedcaaa52b49 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 12 Apr 2024 14:55:14 +0200 Subject: [PATCH 31/57] Discussion TODOs --- build.gradle.kts | 2 +- sentry/api/sentry.api | 179 ++++++++++++++++-- .../src/main/java/io/sentry/Breadcrumb.java | 1 + .../java/io/sentry/CombinedScopeView.java | 4 + sentry/src/main/java/io/sentry/ScopeType.java | 5 +- sentry/src/main/java/io/sentry/Scopes.java | 2 + 6 files changed, 177 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 42acafadb1..acb1fba051 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -99,7 +99,7 @@ allprojects { dependsOn("cleanTest") } withType { - options.compilerArgs.addAll(arrayOf("-Xlint:all", "-Werror", "-Xlint:-classfile", "-Xlint:-processing")) + options.compilerArgs.addAll(arrayOf("-Xlint:all", "-Werror", "-Xlint:-classfile", "-Xlint:-processing", "-Xlint:-try")) } } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 9063972579..6fa2b29423 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -93,10 +93,12 @@ public final class io/sentry/BaggageHeader { public fun getValue ()Ljava/lang/String; } -public final class io/sentry/Breadcrumb : io/sentry/JsonSerializable, io/sentry/JsonUnknown { +public final class io/sentry/Breadcrumb : io/sentry/JsonSerializable, io/sentry/JsonUnknown, java/lang/Comparable { public fun ()V public fun (Ljava/lang/String;)V public fun (Ljava/util/Date;)V + public fun compareTo (Lio/sentry/Breadcrumb;)I + public synthetic fun compareTo (Ljava/lang/Object;)I public static fun debug (Ljava/lang/String;)Lio/sentry/Breadcrumb; public fun equals (Ljava/lang/Object;)Z public static fun error (Ljava/lang/String;)Lio/sentry/Breadcrumb; @@ -206,6 +208,90 @@ public final class io/sentry/CheckInStatus : java/lang/Enum { public static fun values ()[Lio/sentry/CheckInStatus; } +public final class io/sentry/CombinedContextsView : io/sentry/protocol/Contexts { + public fun (Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/ScopeType;)V + public fun getApp ()Lio/sentry/protocol/App; + public fun getBrowser ()Lio/sentry/protocol/Browser; + public fun getDevice ()Lio/sentry/protocol/Device; + public fun getGpu ()Lio/sentry/protocol/Gpu; + public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem; + public fun getResponse ()Lio/sentry/protocol/Response; + public fun getRuntime ()Lio/sentry/protocol/SentryRuntime; + public fun getTrace ()Lio/sentry/SpanContext; + public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V + public fun setApp (Lio/sentry/protocol/App;)V + public fun setBrowser (Lio/sentry/protocol/Browser;)V + public fun setDevice (Lio/sentry/protocol/Device;)V + public fun setGpu (Lio/sentry/protocol/Gpu;)V + public fun setOperatingSystem (Lio/sentry/protocol/OperatingSystem;)V + public fun setResponse (Lio/sentry/protocol/Response;)V + public fun setRuntime (Lio/sentry/protocol/SentryRuntime;)V + public fun setTrace (Lio/sentry/SpanContext;)V + public fun withResponse (Lio/sentry/util/HintUtils$SentryConsumer;)V +} + +public final class io/sentry/CombinedScopeView : io/sentry/IScope { + public fun (Lio/sentry/IScope;Lio/sentry/IScope;Lio/sentry/IScope;)V + public fun addAttachment (Lio/sentry/Attachment;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V + public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V + public fun addEventProcessor (Lio/sentry/EventProcessor;)V + public fun assignTraceContext (Lio/sentry/SentryEvent;)V + public fun bindClient (Lio/sentry/ISentryClient;)V + public fun clear ()V + public fun clearAttachments ()V + public fun clearBreadcrumbs ()V + public fun clearTransaction ()V + public fun clone ()Lio/sentry/IScope; + public synthetic fun clone ()Ljava/lang/Object; + public fun endSession ()Lio/sentry/Session; + public fun getAttachments ()Ljava/util/List; + public fun getBreadcrumbs ()Ljava/util/Queue; + public fun getClient ()Lio/sentry/ISentryClient; + public fun getContexts ()Lio/sentry/protocol/Contexts; + public fun getEventProcessors ()Ljava/util/List; + public fun getExtras ()Ljava/util/Map; + public fun getFingerprint ()Ljava/util/List; + public fun getLastEventId ()Lio/sentry/protocol/SentryId; + public fun getLevel ()Lio/sentry/SentryLevel; + public fun getOptions ()Lio/sentry/SentryOptions; + public fun getPropagationContext ()Lio/sentry/PropagationContext; + public fun getRequest ()Lio/sentry/protocol/Request; + public fun getScreen ()Ljava/lang/String; + public fun getSession ()Lio/sentry/Session; + public fun getSpan ()Lio/sentry/ISpan; + public fun getTags ()Ljava/util/Map; + public fun getTransaction ()Lio/sentry/ITransaction; + public fun getTransactionName ()Ljava/lang/String; + public fun getUser ()Lio/sentry/protocol/User; + public fun removeContexts (Ljava/lang/String;)V + public fun removeExtra (Ljava/lang/String;)V + public fun removeTag (Ljava/lang/String;)V + public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V + public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V + public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V + public fun setContexts (Ljava/lang/String;Ljava/lang/Object;)V + public fun setContexts (Ljava/lang/String;Ljava/lang/String;)V + public fun setContexts (Ljava/lang/String;Ljava/util/Collection;)V + public fun setContexts (Ljava/lang/String;[Ljava/lang/Object;)V + public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V + public fun setFingerprint (Ljava/util/List;)V + public fun setLastEventId (Lio/sentry/protocol/SentryId;)V + public fun setLevel (Lio/sentry/SentryLevel;)V + public fun setPropagationContext (Lio/sentry/PropagationContext;)V + public fun setRequest (Lio/sentry/protocol/Request;)V + public fun setScreen (Ljava/lang/String;)V + public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V + public fun setTag (Ljava/lang/String;Ljava/lang/String;)V + public fun setTransaction (Lio/sentry/ITransaction;)V + public fun setTransaction (Ljava/lang/String;)V + public fun setUser (Lio/sentry/protocol/User;)V + public fun startSession ()Lio/sentry/Scope$SessionPair; + public fun withPropagationContext (Lio/sentry/Scope$IWithPropagationContext;)Lio/sentry/PropagationContext; + public fun withSession (Lio/sentry/Scope$IWithSession;)Lio/sentry/Session; + public fun withTransaction (Lio/sentry/Scope$IWithTransaction;)V +} + public final class io/sentry/CpuCollectionData { public fun (JD)V public fun getCpuUsagePercentage ()D @@ -437,25 +523,31 @@ public final class io/sentry/Hub : io/sentry/IHub, io/sentry/metrics/MetricsApi$ public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public fun getDefaultTagsForMetrics ()Ljava/util/Map; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator; public fun getMetricsAggregator ()Lio/sentry/IMetricsAggregator; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -493,23 +585,29 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getInstance ()Lio/sentry/HubAdapter; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -547,22 +645,28 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub { public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -763,14 +867,19 @@ public abstract interface class io/sentry/IScopes { public abstract fun clone ()Lio/sentry/IHub; public abstract fun close ()V public abstract fun close (Z)V - public abstract fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeCallback;)V + public abstract fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public abstract fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public abstract fun endSession ()V public abstract fun flush (J)V + public abstract fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public abstract fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public abstract fun getBaggage ()Lio/sentry/BaggageHeader; + public abstract fun getIsolationScope ()Lio/sentry/IScope; public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; public abstract fun getOptions ()Lio/sentry/SentryOptions; public abstract fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public abstract fun getScope ()Lio/sentry/IScope; public abstract fun getSpan ()Lio/sentry/ISpan; public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader; public abstract fun getTransaction ()Lio/sentry/ITransaction; @@ -778,8 +887,10 @@ public abstract interface class io/sentry/IScopes { public abstract fun isEnabled ()Z public abstract fun isHealthy ()Z public fun isNoOp ()Z + public abstract fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public abstract fun metrics ()Lio/sentry/metrics/MetricsApi; public abstract fun popScope ()V + public abstract fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public abstract fun pushScope ()Lio/sentry/ISentryLifecycleToken; public abstract fun removeExtra (Ljava/lang/String;)V public abstract fun removeTag (Ljava/lang/String;)V @@ -1252,15 +1363,19 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getInstance ()Lio/sentry/NoOpHub; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; @@ -1268,8 +1383,10 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun isEnabled ()Z public fun isHealthy ()Z public fun isNoOp ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -1376,15 +1493,19 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getInstance ()Lio/sentry/NoOpScopes; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; @@ -1392,8 +1513,10 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun isEnabled ()Z public fun isHealthy ()Z public fun isNoOp ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -1822,6 +1945,14 @@ public abstract class io/sentry/ScopeObserverAdapter : io/sentry/IScopeObserver public fun setUser (Lio/sentry/protocol/User;)V } +public final class io/sentry/ScopeType : java/lang/Enum { + public static final field CURRENT Lio/sentry/ScopeType; + public static final field GLOBAL Lio/sentry/ScopeType; + public static final field ISOLATION Lio/sentry/ScopeType; + public static fun valueOf (Ljava/lang/String;)Lio/sentry/ScopeType; + public static fun values ()[Lio/sentry/ScopeType; +} + public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/MetricsApi$IMetricsInterface { public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V @@ -1841,12 +1972,12 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V - public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/Scopes; - public fun forkedScopes (Ljava/lang/String;)Lio/sentry/Scopes; + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public fun getCreator ()Ljava/lang/String; public fun getDefaultTagsForMetrics ()Ljava/util/Map; @@ -1869,6 +2000,7 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -1906,23 +2038,29 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public synthetic fun clone ()Ljava/lang/Object; public fun close ()V public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeCallback;)V + public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public fun endSession ()V public fun flush (J)V + public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getInstance ()Lio/sentry/ScopesAdapter; + public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; + public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; public fun getTraceparent ()Lio/sentry/SentryTraceHeader; public fun getTransaction ()Lio/sentry/ITransaction; public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z public fun isHealthy ()Z + public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; public fun metrics ()Lio/sentry/metrics/MetricsApi; public fun popScope ()V + public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public fun pushScope ()Lio/sentry/ISentryLifecycleToken; public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V @@ -1993,12 +2131,15 @@ public final class io/sentry/Sentry { public static fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; public static fun captureUserFeedback (Lio/sentry/UserFeedback;)V public static fun clearBreadcrumbs ()V - public static fun cloneMainHub ()Lio/sentry/IScopes; public static fun close ()V public static fun configureScope (Lio/sentry/ScopeCallback;)V + public static fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V public static fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; public static fun endSession ()V public static fun flush (J)V + public static fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public static fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; + public static fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public static fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getCurrentHub ()Lio/sentry/IHub; public static fun getCurrentScopes ()Lio/sentry/IScopes; @@ -2018,12 +2159,13 @@ public final class io/sentry/Sentry { public static fun isHealthy ()Z public static fun metrics ()Lio/sentry/metrics/MetricsApi; public static fun popScope ()V + public static fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; public static fun pushScope ()Lio/sentry/ISentryLifecycleToken; public static fun removeExtra (Ljava/lang/String;)V public static fun removeTag (Ljava/lang/String;)V public static fun reportFullDisplayed ()V public static fun reportFullyDisplayed ()V - public static fun setCurrentHub (Lio/sentry/IHub;)V + public static fun setCurrentHub (Lio/sentry/IHub;)Lio/sentry/ISentryLifecycleToken; public static fun setCurrentScopes (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken; public static fun setExtra (Ljava/lang/String;Ljava/lang/String;)V public static fun setFingerprint (Ljava/util/List;)V @@ -2491,6 +2633,7 @@ public class io/sentry/SentryOptions { public fun getCron ()Lio/sentry/SentryOptions$Cron; public fun getDateProvider ()Lio/sentry/SentryDateProvider; public fun getDebugMetaLoader ()Lio/sentry/internal/debugmeta/IDebugMetaLoader; + public fun getDefaultScopeType ()Lio/sentry/ScopeType; public fun getDiagnosticLevel ()Lio/sentry/SentryLevel; public fun getDist ()Ljava/lang/String; public fun getDistinctId ()Ljava/lang/String; @@ -2601,6 +2744,7 @@ public class io/sentry/SentryOptions { public fun setDateProvider (Lio/sentry/SentryDateProvider;)V public fun setDebug (Z)V public fun setDebugMetaLoader (Lio/sentry/internal/debugmeta/IDebugMetaLoader;)V + public fun setDefaultScopeType (Lio/sentry/ScopeType;)V public fun setDiagnosticLevel (Lio/sentry/SentryLevel;)V public fun setDist (Ljava/lang/String;)V public fun setDistinctId (Ljava/lang/String;)V @@ -2834,7 +2978,9 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction { public final class io/sentry/SentryWrapper { public fun ()V public static fun wrapCallable (Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Callable; + public static fun wrapCallableIsolated (Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Callable; public static fun wrapSupplier (Ljava/util/function/Supplier;)Ljava/util/function/Supplier; + public static fun wrapSupplierIsolated (Ljava/util/function/Supplier;)Ljava/util/function/Supplier; } public final class io/sentry/Session : io/sentry/JsonSerializable, io/sentry/JsonUnknown { @@ -3979,7 +4125,7 @@ public final class io/sentry/protocol/Browser$JsonKeys { public fun ()V } -public final class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMap, io/sentry/JsonSerializable { +public class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMap, io/sentry/JsonSerializable { public fun ()V public fun (Lio/sentry/protocol/Contexts;)V public fun getApp ()Lio/sentry/protocol/App; @@ -5292,6 +5438,11 @@ public abstract interface class io/sentry/util/LazyEvaluator$Evaluator { public abstract fun evaluate ()Ljava/lang/Object; } +public final class io/sentry/util/LifecycleHelper { + public fun ()V + public static fun close (Ljava/lang/Object;)V +} + public final class io/sentry/util/LogUtils { public fun ()V public static fun logNotInstanceOf (Ljava/lang/Class;Ljava/lang/Object;Lio/sentry/ILogger;)V diff --git a/sentry/src/main/java/io/sentry/Breadcrumb.java b/sentry/src/main/java/io/sentry/Breadcrumb.java index 5f43ab6d29..fcd9407993 100644 --- a/sentry/src/main/java/io/sentry/Breadcrumb.java +++ b/sentry/src/main/java/io/sentry/Breadcrumb.java @@ -663,6 +663,7 @@ public void setUnknown(@Nullable Map unknown) { @Override @SuppressWarnings("JavaUtilDate") public int compareTo(@NotNull Breadcrumb o) { + // TODO also use nano time if equal return timestamp.compareTo(o.timestamp); } diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index b9f8ab2c70..19253d1f4a 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -237,6 +237,7 @@ public void setTag(@NotNull String key, @NotNull String value) { @Override public void removeTag(@NotNull String key) { + // TODO should this go to all scopes? getDefaultWriteScope().removeTag(key); } @@ -256,6 +257,7 @@ public void setExtra(@NotNull String key, @NotNull String value) { @Override public void removeExtra(@NotNull String key) { + // TODO should this go to all scopes? getDefaultWriteScope().removeExtra(key); } @@ -305,10 +307,12 @@ public void setContexts(@NotNull String key, @NotNull Character value) { @Override public void removeContexts(@NotNull String key) { + // TODO should this go to all scopes? getDefaultWriteScope().removeContexts(key); } private @NotNull IScope getDefaultWriteScope() { + // TODO use Scopes.getSpecificScope? if (ScopeType.CURRENT.equals(getOptions().getDefaultScopeType())) { return scope; } diff --git a/sentry/src/main/java/io/sentry/ScopeType.java b/sentry/src/main/java/io/sentry/ScopeType.java index d54c2b635c..6f35ce6604 100644 --- a/sentry/src/main/java/io/sentry/ScopeType.java +++ b/sentry/src/main/java/io/sentry/ScopeType.java @@ -3,5 +3,8 @@ public enum ScopeType { CURRENT, ISOLATION, - GLOBAL; + GLOBAL, + + // TODO do we need a combined as well so configureScope + COMBINED; } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index daf143ba66..9319c9b46f 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -363,6 +363,7 @@ public void endSession() { } private IScope getCombinedScopeView() { + // TODO create in ctor? return new CombinedScopeView(getGlobalScope(), isolationScope, scope); } @@ -428,6 +429,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable } private IScope getSpecificScope(final @Nullable ScopeType scopeType) { + // TODO extract and reuse if (scopeType != null) { switch (scopeType) { case CURRENT: From bf4a7bfd7e9c68bfa0bd6df24415385c396daf97 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 15 Apr 2024 13:25:21 +0200 Subject: [PATCH 32/57] Fix breadcrumb ordering --- .../io/sentry/logback/SentryAppenderTest.kt | 1 + .../src/main/java/io/sentry/Breadcrumb.java | 11 ++- .../java/io/sentry/CombinedScopeViewTest.kt | 72 +++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt diff --git a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt index 4217954be1..96c3dad9f1 100644 --- a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt +++ b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt @@ -77,6 +77,7 @@ class SentryAppenderTest { @BeforeTest fun `clear MDC`() { MDC.clear() + Sentry.close() } @Test diff --git a/sentry/src/main/java/io/sentry/Breadcrumb.java b/sentry/src/main/java/io/sentry/Breadcrumb.java index fcd9407993..ddb19e5052 100644 --- a/sentry/src/main/java/io/sentry/Breadcrumb.java +++ b/sentry/src/main/java/io/sentry/Breadcrumb.java @@ -22,6 +22,8 @@ public final class Breadcrumb implements JsonUnknown, JsonSerializable, Comparab /** A timestamp representing when the breadcrumb occurred. */ private final @NotNull Date timestamp; + private final @NotNull Long nanos; + /** If a message is provided, its rendered as text and the whitespace is preserved. */ private @Nullable String message; @@ -46,10 +48,12 @@ public final class Breadcrumb implements JsonUnknown, JsonSerializable, Comparab * @param timestamp the timestamp */ public Breadcrumb(final @NotNull Date timestamp) { + this.nanos = System.nanoTime(); this.timestamp = timestamp; } Breadcrumb(final @NotNull Breadcrumb breadcrumb) { + this.nanos = System.nanoTime(); this.timestamp = breadcrumb.timestamp; this.message = breadcrumb.message; this.type = breadcrumb.type; @@ -663,8 +667,11 @@ public void setUnknown(@Nullable Map unknown) { @Override @SuppressWarnings("JavaUtilDate") public int compareTo(@NotNull Breadcrumb o) { - // TODO also use nano time if equal - return timestamp.compareTo(o.timestamp); + int timestampCompare = timestamp.compareTo(o.timestamp); + if (timestampCompare == 0) { + return nanos.compareTo(o.nanos); + } + return timestampCompare; } public static final class JsonKeys { diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt new file mode 100644 index 0000000000..38023da18e --- /dev/null +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -0,0 +1,72 @@ +package io.sentry + +import kotlin.test.Test +import kotlin.test.assertEquals + +class CombinedScopeViewTest { + + @Test + fun `adds breadcrumbs from all scopes in sorted order`() { + val options = SentryOptions() + val globalScope = Scope(options) + val isolationScope = Scope(options) + val scope = Scope(options) + + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + globalScope.addBreadcrumb(Breadcrumb.info("global 1")) + isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) + scope.addBreadcrumb(Breadcrumb.info("current 1")) + + globalScope.addBreadcrumb(Breadcrumb.info("global 2")) + isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) + scope.addBreadcrumb(Breadcrumb.info("current 2")) + + val breadcrumbs = combined.breadcrumbs + assertEquals("global 1", breadcrumbs.poll().message) + assertEquals("isolation 1", breadcrumbs.poll().message) + assertEquals("current 1", breadcrumbs.poll().message) + assertEquals("global 2", breadcrumbs.poll().message) + assertEquals("isolation 2", breadcrumbs.poll().message) + assertEquals("current 2", breadcrumbs.poll().message) + } + + @Test + fun `oldest breadcrumbs are dropped first`() { + val options = SentryOptions().also { it.maxBreadcrumbs = 5 } + val globalScope = Scope(options) + val isolationScope = Scope(options) + val scope = Scope(options) + + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + globalScope.addBreadcrumb(Breadcrumb.info("global 1")) + isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) + scope.addBreadcrumb(Breadcrumb.info("current 1")) + + globalScope.addBreadcrumb(Breadcrumb.info("global 2")) + isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) + scope.addBreadcrumb(Breadcrumb.info("current 2")) + + val breadcrumbs = combined.breadcrumbs +// assertEquals("global 1", breadcrumbs.poll().message) <-- was dropped + assertEquals("isolation 1", breadcrumbs.poll().message) + assertEquals("current 1", breadcrumbs.poll().message) + assertEquals("global 2", breadcrumbs.poll().message) + assertEquals("isolation 2", breadcrumbs.poll().message) + assertEquals("current 2", breadcrumbs.poll().message) + + scope.addBreadcrumb(Breadcrumb.info("current 3")) + scope.addBreadcrumb(Breadcrumb.info("current 4")) + + val breadcrumbs2 = combined.breadcrumbs +// assertEquals("global 1", breadcrumbs.poll().message) <-- was dropped +// assertEquals("isolation 1", breadcrumbs2.poll().message) <-- dropped +// assertEquals("current 1", breadcrumbs2.poll().message) <-- dropped + assertEquals("global 2", breadcrumbs2.poll().message) + assertEquals("isolation 2", breadcrumbs2.poll().message) + assertEquals("current 2", breadcrumbs2.poll().message) + assertEquals("current 3", breadcrumbs2.poll().message) + assertEquals("current 4", breadcrumbs2.poll().message) + } +} From 62cb91ae794be9ea6dd35b63c67c6f867daf888d Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 15 Apr 2024 14:36:01 +0200 Subject: [PATCH 33/57] Mark TODOS with [HSM] --- .../android/core/ManifestMetadataReader.java | 6 +-- .../spring/jakarta/SentryTaskDecorator.java | 3 +- .../spring/jakarta/webflux/ReactorUtils.java | 4 +- .../io/sentry/spring/SentryTaskDecorator.java | 3 +- .../spring/webflux/SentryWebFilter.java | 1 - .../java/io/sentry/CombinedScopeView.java | 17 +++--- .../java/io/sentry/DefaultScopesStorage.java | 2 +- .../src/main/java/io/sentry/HubAdapter.java | 2 +- sentry/src/main/java/io/sentry/Scope.java | 6 +-- sentry/src/main/java/io/sentry/ScopeType.java | 2 +- sentry/src/main/java/io/sentry/Scopes.java | 52 ++++++------------- .../main/java/io/sentry/ScopesAdapter.java | 2 +- sentry/src/main/java/io/sentry/Sentry.java | 5 +- .../main/java/io/sentry/SentryOptions.java | 2 +- .../main/java/io/sentry/SentryWrapper.java | 2 +- 15 files changed, 44 insertions(+), 65 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java b/sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java index 31e026dd00..b51c4b22a8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java @@ -37,7 +37,7 @@ final class ManifestMetadataReader { static final String SDK_NAME = "io.sentry.sdk.name"; static final String SDK_VERSION = "io.sentry.sdk.version"; - // TODO: remove on 6.x in favor of SESSION_AUTO_TRACKING_ENABLE + // TODO [MAJOR]: remove on 6.x in favor of SESSION_AUTO_TRACKING_ENABLE static final String SESSION_TRACKING_ENABLE = "io.sentry.session-tracking.enable"; static final String AUTO_SESSION_TRACKING_ENABLE = "io.sentry.auto-session-tracking.enable"; @@ -70,7 +70,7 @@ final class ManifestMetadataReader { @ApiStatus.Experimental static final String TRACE_SAMPLING = "io.sentry.traces.trace-sampling"; - // TODO: remove in favor of TRACE_PROPAGATION_TARGETS + // TODO [MAJOR]: remove in favor of TRACE_PROPAGATION_TARGETS @Deprecated static final String TRACING_ORIGINS = "io.sentry.traces.tracing-origins"; static final String TRACE_PROPAGATION_TARGETS = "io.sentry.traces.trace-propagation-targets"; @@ -323,7 +323,7 @@ static void applyMetadata( List tracePropagationTargets = readList(metadata, logger, TRACE_PROPAGATION_TARGETS); - // TODO remove once TRACING_ORIGINS have been removed + // TODO [MAJOR] remove once TRACING_ORIGINS have been removed if (!metadata.containsKey(TRACE_PROPAGATION_TARGETS) && (tracePropagationTargets == null || tracePropagationTargets.isEmpty())) { tracePropagationTargets = readList(metadata, logger, TRACING_ORIGINS); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java index 943a7cc5ff..42c35919d7 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java @@ -15,7 +15,8 @@ */ public final class SentryTaskDecorator implements TaskDecorator { @Override - // TODO should there also be a SentryIsolatedTaskDecorator or similar that uses forkedScopes()? + // TODO [HSM] should there also be a SentryIsolatedTaskDecorator or similar that uses + // forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java index cc7509129e..e160be7ac9 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java @@ -9,8 +9,8 @@ import reactor.util.context.Context; @ApiStatus.Experimental -// TODO do we keep old methods around and deprecate them? -// TODO do we need to offer isolated variants? +// TODO [HSM] do we keep old methods around and deprecate them? +// TODO [HSM] do we need to offer isolated variants? public final class ReactorUtils { /** diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index 761038ece0..8c3b9ac1f4 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -15,7 +15,8 @@ */ public final class SentryTaskDecorator implements TaskDecorator { @Override - // TODO should there also be a SentryIsolatedTaskDecorator or similar that uses forkedScopes()? + // TODO [HSM] should there also be a SentryIsolatedTaskDecorator or similar that uses + // forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { final IScopes newHub = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index 10e80ebe8b..e32ede6947 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -51,7 +51,6 @@ public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { @NotNull IScopes requestHub = Sentry.forkedRootScopes("request.webflux"); - // TODO do not push / pop, use fork instead if (!requestHub.isEnabled()) { return webFilterChain.filter(serverWebExchange); } diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 19253d1f4a..ee6fdad06e 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -164,7 +164,6 @@ public void setFingerprint(@NotNull List fingerprint) { allBreadcrumbs.addAll(scope.getBreadcrumbs()); Collections.sort(allBreadcrumbs); - // TODO test oldest are removed first final @NotNull Queue breadcrumbs = createBreadcrumbsList(scope.getOptions().getMaxBreadcrumbs()); breadcrumbs.addAll(allBreadcrumbs); @@ -178,7 +177,7 @@ public void setFingerprint(@NotNull List fingerprint) { * @param maxBreadcrumb the max number of breadcrumbs * @return the breadcrumbs queue */ - // TODO copied from Scope, should reuse instead + // TODO [HSM] copied from Scope, should reuse instead private @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { return SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb)); } @@ -237,7 +236,7 @@ public void setTag(@NotNull String key, @NotNull String value) { @Override public void removeTag(@NotNull String key) { - // TODO should this go to all scopes? + // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeTag(key); } @@ -257,7 +256,7 @@ public void setExtra(@NotNull String key, @NotNull String value) { @Override public void removeExtra(@NotNull String key) { - // TODO should this go to all scopes? + // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeExtra(key); } @@ -307,12 +306,12 @@ public void setContexts(@NotNull String key, @NotNull Character value) { @Override public void removeContexts(@NotNull String key) { - // TODO should this go to all scopes? + // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeContexts(key); } private @NotNull IScope getDefaultWriteScope() { - // TODO use Scopes.getSpecificScope? + // TODO [HSM] use Scopes.getSpecificScope? if (ScopeType.CURRENT.equals(getOptions().getDefaultScopeType())) { return scope; } @@ -343,7 +342,7 @@ public void clearAttachments() { @Override public @NotNull List getEventProcessors() { - // TODO mechanism for ordering event processors + // TODO [HSM] mechanism for ordering event processors final @NotNull List allEventProcessors = new CopyOnWriteArrayList<>(); allEventProcessors.addAll(globalScope.getEventProcessors()); allEventProcessors.addAll(isolationScope.getEventProcessors()); @@ -412,7 +411,7 @@ public void setPropagationContext(@NotNull PropagationContext propagationContext @Override public @NotNull IScope clone() { - // TODO just return a new CombinedScopeView with forked scope? + // TODO [HSM] just return a new CombinedScopeView with forked scope? return getDefaultWriteScope().clone(); } @@ -435,7 +434,7 @@ public void bindClient(@NotNull ISentryClient client) { @Override public @NotNull ISentryClient getClient() { - // TODO checking for noop here doesn't allow disabling via client, is that ok? + // TODO [HSM] checking for noop here doesn't allow disabling via client, is that ok? final @Nullable ISentryClient current = scope.getClient(); if (!(current instanceof NoOpSentryClient)) { return current; diff --git a/sentry/src/main/java/io/sentry/DefaultScopesStorage.java b/sentry/src/main/java/io/sentry/DefaultScopesStorage.java index 12902a1dff..4a054ee7cc 100644 --- a/sentry/src/main/java/io/sentry/DefaultScopesStorage.java +++ b/sentry/src/main/java/io/sentry/DefaultScopesStorage.java @@ -21,7 +21,7 @@ public ISentryLifecycleToken set(@Nullable IScopes scopes) { @Override public void close() { - // TODO prevent further storing? would this cause problems if singleton, closed and + // TODO [HSM] prevent further storing? would this cause problems if singleton, closed and // re-initialized? currentScopes.remove(); } diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index f0d7335a80..8e6966aac5 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -210,7 +210,7 @@ public void flush(long timeoutMillis) { @Override public @NotNull ISentryLifecycleToken makeCurrent() { - // TODO this wouldn't do anything since it replaced the current with the same Scopes + // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index fcbcd74650..665f789158 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -90,8 +90,8 @@ public final class Scope implements IScope { private @NotNull ISentryClient client = NoOpSentryClient.getInstance(); - // TODO intended only for global scope - // TODO test for memory leak + // TODO [HSM] intended only for global scope + // TODO [HSM] test for memory leak private final @NotNull Map, String>> throwableToSpan = Collections.synchronizedMap(new WeakHashMap<>()); @@ -114,7 +114,7 @@ private Scope(final @NotNull Scope scope) { this.options = scope.options; this.level = scope.level; this.client = scope.client; - // TODO should we do this? didn't do it for Hub + // TODO [HSM] should we do this? didn't do it for Hub this.lastEventId = scope.getLastEventId(); final User userRef = scope.user; diff --git a/sentry/src/main/java/io/sentry/ScopeType.java b/sentry/src/main/java/io/sentry/ScopeType.java index 6f35ce6604..3815cf2081 100644 --- a/sentry/src/main/java/io/sentry/ScopeType.java +++ b/sentry/src/main/java/io/sentry/ScopeType.java @@ -5,6 +5,6 @@ public enum ScopeType { ISOLATION, GLOBAL, - // TODO do we need a combined as well so configureScope + // TODO [HSM] do we need a combined as well so configureScope COMBINED; } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 9319c9b46f..d30a9b6074 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -26,13 +26,13 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull IScope scope; private final @NotNull IScope isolationScope; - // TODO just for debugging + @SuppressWarnings("UnusedVariable") private final @Nullable Scopes parentScopes; private final @NotNull String creator; - // TODO should this be set on all scopes (global, isolation, current)? + // TODO [HSM] should this be set on all scopes (global, isolation, current)? private final @NotNull SentryOptions options; private volatile boolean isEnabled; private final @NotNull TracesSampler tracesSampler; @@ -82,12 +82,12 @@ private Scopes( return isolationScope; } - // TODO add to IScopes interface? + // TODO [HSM] add to IScopes interface? public @Nullable Scopes getParent() { return parentScopes; } - // TODO add to IScopes interface? + // TODO [HSM] add to IScopes interface? public boolean isAncestorOf(final @Nullable Scopes otherScopes) { if (otherScopes == null) { return false; @@ -115,7 +115,7 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { return new Scopes(scope.clone(), isolationScope, this, options, creator); } - // TODO always read from root scope? + // TODO [HSM] always read from root scope? @Override public boolean isEnabled() { return isEnabled; @@ -363,7 +363,7 @@ public void endSession() { } private IScope getCombinedScopeView() { - // TODO create in ctor? + // TODO [HSM] create in ctor? return new CombinedScopeView(getGlobalScope(), isolationScope, scope); } @@ -393,7 +393,7 @@ public void close(final boolean isRestarting) { } } - // TODO which scopes do we call this on? isolation and current scope? + // TODO [HSM] which scopes do we call this on? isolation and current scope? configureScope(scope -> scope.clear()); options.getTransactionProfiler().close(); options.getTransactionPerformanceCollector().close(); @@ -429,7 +429,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable } private IScope getSpecificScope(final @Nullable ScopeType scopeType) { - // TODO extract and reuse + // TODO [HSM] extract and reuse if (scopeType != null) { switch (scopeType) { case CURRENT: @@ -582,11 +582,9 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { getCombinedScopeView().setLastEventId(lastEventId); } - // TODO add to IScopes interface + // TODO [HSM] add to IScopes interface public @NotNull IScope getGlobalScope() { - // TODO should be: return Sentry.getGlobalScope(); - // return scope; } @Override @@ -627,7 +625,7 @@ public ISentryLifecycleToken pushIsolationScope() { return Sentry.setCurrentScopes(this); } - // TODO needs to be deprecated because there's no more stack + // TODO [HSM] needs to be deprecated because there's no more stack @Override public void popScope() { if (!isEnabled()) { @@ -637,13 +635,13 @@ public void popScope() { } else { final @Nullable Scopes parent = getParent(); if (parent != null) { - // TODO this is never closed + // TODO [HSM] this is never closed parent.makeCurrent(); } } } - // TODO lots of testing required to see how ThreadLocal is affected + // TODO [HSM] lots of testing required to see how ThreadLocal is affected @Override public void withScope(final @NotNull ScopeCallback callback) { if (!isEnabled()) { @@ -655,8 +653,8 @@ public void withScope(final @NotNull ScopeCallback callback) { } else { final @NotNull IScopes forkedScopes = forkedCurrentScope("withScope"); - // TODO should forkedScopes be made current inside callback? - // TODO forkedScopes.makeCurrent()? + // TODO [HSM] should forkedScopes be made current inside callback? + // TODO [HSM] forkedScopes.makeCurrent()? try { callback.run(forkedScopes.getScope()); } catch (Throwable e) { @@ -726,7 +724,7 @@ public void flush(long timeoutMillis) { if (!isEnabled()) { options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); } - // TODO should this fork isolation scope as well? + // TODO [HSM] should this fork isolation scope as well? return new HubScopesWrapper(forkedCurrentScope("scopes clone")); } @@ -876,24 +874,6 @@ public void setSpanContext( getCombinedScopeView().setSpanContext(throwable, span, transactionName); } - // // TODO this seems unused - // @Nullable - // SpanContext getSpanContext(final @NotNull Throwable throwable) { - // Objects.requireNonNull(throwable, "throwable is required"); - // final Throwable rootCause = ExceptionUtils.findRootCause(throwable); - // final Pair, String> pair = this.throwableToSpan.get(rootCause); - // if (pair != null) { - // final WeakReference spanWeakRef = pair.getFirst(); - // if (spanWeakRef != null) { - // final ISpan span = spanWeakRef.get(); - // if (span != null) { - // return span.getSpanContext(); - // } - // } - // } - // return null; - // } - @Override public @Nullable ISpan getSpan() { ISpan span = null; @@ -947,7 +927,7 @@ public void reportFullyDisplayed() { @NotNull PropagationContext propagationContext = PropagationContext.fromHeaders(getOptions().getLogger(), sentryTrace, baggageHeaders); - // TODO should this go on isolation scope? + // TODO [HSM] should this go on isolation scope? configureScope( (scope) -> { scope.setPropagationContext(propagationContext); diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index d3b0f43bf2..684ab12113 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -207,7 +207,7 @@ public void flush(long timeoutMillis) { @Override public @NotNull ISentryLifecycleToken makeCurrent() { - // TODO this wouldn't do anything since it replaced the current with the same Scopes + // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 42ccc3cf63..bd1fcee04b 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -47,7 +47,7 @@ private Sentry() {} /** The root Scopes or NoOp if Sentry is disabled. */ private static volatile @NotNull IScopes rootScopes = NoOpScopes.getInstance(); - // TODO cannot pass options here + // TODO [HSM] cannot pass options here private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); /** Default value for globalHubMode is false */ @@ -271,7 +271,7 @@ private static synchronized void init( final IScopes scopes = getCurrentScopes(); final IScope rootScope = new Scope(options); final IScope rootIsolationScope = new Scope(options); - // TODO shouldn't replace global scope + // TODO [HSM] shouldn't replace global scope globalScope = new Scope(options); globalScope.bindClient(new SentryClient(options)); rootScopes = new Scopes(rootScope, rootIsolationScope, options, "Sentry.init"); @@ -819,7 +819,6 @@ public static void removeExtra(final @NotNull String key) { public static @NotNull ISentryLifecycleToken pushScope() { // pushScope is no-op in global hub mode if (!globalHubMode) { - // TODO this might have to behave differently from Scopes.pushScope return getCurrentScopes().pushScope(); } return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index c693907121..0523ce7cf0 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -409,7 +409,7 @@ public class SentryOptions { private @NotNull IMainThreadChecker mainThreadChecker = NoOpMainThreadChecker.getInstance(); - // TODO this should default to false on the next major + // TODO [MAJOR] this should default to false on the next major /** Whether OPTIONS requests should be traced. */ private boolean traceOptionsRequests = true; diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index aba5a4b778..5d15c35e17 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -27,7 +27,7 @@ public final class SentryWrapper { * @return the wrapped {@link Callable} * @param - the result type of the {@link Callable} */ - // TODO adapt javadoc + // TODO [HSM] adapt javadoc public static Callable wrapCallable(final @NotNull Callable callable) { final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("wrapCallable"); From b1630ea89d4ae0ecd6947af1618c93223de4fd0a Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Apr 2024 08:04:38 +0200 Subject: [PATCH 34/57] Add getGlobalScope and forkedRootScopes to IScopes --- sentry/api/sentry.api | 15 +++++++++++++++ sentry/src/main/java/io/sentry/Hub.java | 10 ++++++++++ sentry/src/main/java/io/sentry/HubAdapter.java | 10 ++++++++++ .../main/java/io/sentry/HubScopesWrapper.java | 10 ++++++++++ sentry/src/main/java/io/sentry/IScopes.java | 18 +++++++++++++++++- sentry/src/main/java/io/sentry/NoOpHub.java | 10 ++++++++++ sentry/src/main/java/io/sentry/NoOpScopes.java | 10 ++++++++++ sentry/src/main/java/io/sentry/Scopes.java | 7 ++++++- .../src/main/java/io/sentry/ScopesAdapter.java | 10 ++++++++++ sentry/src/main/java/io/sentry/Sentry.java | 2 -- 10 files changed, 98 insertions(+), 4 deletions(-) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 6fa2b29423..0a6c85f6ad 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -528,9 +528,11 @@ public final class io/sentry/Hub : io/sentry/IHub, io/sentry/metrics/MetricsApi$ public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public fun getDefaultTagsForMetrics ()Ljava/util/Map; + public fun getGlobalScope ()Lio/sentry/IScope; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator; @@ -590,8 +592,10 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getGlobalScope ()Lio/sentry/IScope; public static fun getInstance ()Lio/sentry/HubAdapter; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; @@ -650,8 +654,10 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub { public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getGlobalScope ()Lio/sentry/IScope; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getOptions ()Lio/sentry/SentryOptions; @@ -873,8 +879,10 @@ public abstract interface class io/sentry/IScopes { public abstract fun endSession ()V public abstract fun flush (J)V public abstract fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public abstract fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public abstract fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public abstract fun getBaggage ()Lio/sentry/BaggageHeader; + public abstract fun getGlobalScope ()Lio/sentry/IScope; public abstract fun getIsolationScope ()Lio/sentry/IScope; public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; public abstract fun getOptions ()Lio/sentry/SentryOptions; @@ -1368,8 +1376,10 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getGlobalScope ()Lio/sentry/IScope; public static fun getInstance ()Lio/sentry/NoOpHub; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; @@ -1498,8 +1508,10 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getGlobalScope ()Lio/sentry/IScope; public static fun getInstance ()Lio/sentry/NoOpScopes; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; @@ -1977,6 +1989,7 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; public fun getCreator ()Ljava/lang/String; @@ -2043,8 +2056,10 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public fun endSession ()V public fun flush (J)V public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; + public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; public fun getBaggage ()Lio/sentry/BaggageHeader; + public fun getGlobalScope ()Lio/sentry/IScope; public static fun getInstance ()Lio/sentry/ScopesAdapter; public fun getIsolationScope ()Lio/sentry/IScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 35740f4c3e..bcb93c7558 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -668,6 +668,11 @@ public void flush(long timeoutMillis) { return Sentry.forkedCurrentScope(creator); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return Sentry.forkedRootScopes(creator); + } + @Override public @NotNull ISentryLifecycleToken makeCurrent() { return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); @@ -683,6 +688,11 @@ public void flush(long timeoutMillis) { return Sentry.getCurrentScopes().getIsolationScope(); } + @Override + public @NotNull IScope getGlobalScope() { + return Sentry.getGlobalScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index 8e6966aac5..df1a7aa661 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -208,6 +208,11 @@ public void flush(long timeoutMillis) { return Sentry.forkedCurrentScope(creator); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return Sentry.forkedRootScopes(creator); + } + @Override public @NotNull ISentryLifecycleToken makeCurrent() { // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes @@ -224,6 +229,11 @@ public void flush(long timeoutMillis) { return Sentry.getCurrentScopes().getIsolationScope(); } + @Override + public @NotNull IScope getGlobalScope() { + return Sentry.getGlobalScope(); + } + @Override public @NotNull SentryId captureTransaction( @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index 3309e59671..2a50d8ca48 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -203,6 +203,11 @@ public void flush(long timeoutMillis) { return scopes.forkedCurrentScope(creator); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return Sentry.forkedRootScopes(creator); + } + @Override public @NotNull ISentryLifecycleToken makeCurrent() { return scopes.makeCurrent(); @@ -218,6 +223,11 @@ public void flush(long timeoutMillis) { return scopes.getIsolationScope(); } + @Override + public @NotNull IScope getGlobalScope() { + return Sentry.getGlobalScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index af6f41ae13..6eb82cca32 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -375,7 +375,7 @@ default void configureScope(@NotNull ScopeCallback callback) { IHub clone(); /** - * Creates a fork of both current and isolation scope. + * Creates a fork of both current and isolation scope from current scopes. * * @param creator debug information to see why scopes where forked * @return forked Scopes @@ -392,6 +392,15 @@ default void configureScope(@NotNull ScopeCallback callback) { @NotNull IScopes forkedCurrentScope(final @NotNull String creator); + /** + * Creates a fork of both current and isolation scope from root scopes. + * + * @param creator debug information to see why scopes where forked + * @return forked Scopes + */ + @NotNull + IScopes forkedRootScopes(final @NotNull String creator); + /** * Stores this Scopes in store, making it the current one that is used by static API. * @@ -414,6 +423,13 @@ default void configureScope(@NotNull ScopeCallback callback) { */ public @NotNull IScope getIsolationScope(); + /** + * Returns the global scope. + * + * @return global scope + */ + public @NotNull IScope getGlobalScope(); + /** * Captures the transaction and enqueues it for sending to Sentry server. * diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index ac1c542a94..06969518b7 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -185,6 +185,16 @@ public void flush(long timeoutMillis) {} return NoOpScope.getInstance(); } + @Override + public @NotNull IScope getGlobalScope() { + return NoOpScope.getInstance(); + } + + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return NoOpScopes.getInstance(); + } + @Override public @NotNull SentryId captureTransaction( final @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index de75ff8178..74f965f651 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -169,6 +169,11 @@ public void flush(long timeoutMillis) {} return NoOpScopes.getInstance(); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return NoOpScopes.getInstance(); + } + @Override public @NotNull ISentryLifecycleToken makeCurrent() { return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); @@ -184,6 +189,11 @@ public void flush(long timeoutMillis) {} return NoOpScope.getInstance(); } + @Override + public @NotNull IScope getGlobalScope() { + return NoOpScope.getInstance(); + } + @Override public @NotNull SentryId captureTransaction( final @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index d30a9b6074..bb83e5060c 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -115,6 +115,11 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { return new Scopes(scope.clone(), isolationScope, this, options, creator); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return Sentry.forkedRootScopes(creator); + } + // TODO [HSM] always read from root scope? @Override public boolean isEnabled() { @@ -582,7 +587,7 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { getCombinedScopeView().setLastEventId(lastEventId); } - // TODO [HSM] add to IScopes interface + @Override public @NotNull IScope getGlobalScope() { return Sentry.getGlobalScope(); } diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index 684ab12113..c7f11612e9 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -205,6 +205,11 @@ public void flush(long timeoutMillis) { return Sentry.forkedCurrentScope(creator); } + @Override + public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { + return Sentry.forkedRootScopes(creator); + } + @Override public @NotNull ISentryLifecycleToken makeCurrent() { // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes @@ -221,6 +226,11 @@ public void flush(long timeoutMillis) { return Sentry.getCurrentScopes().getIsolationScope(); } + @Override + public @NotNull IScope getGlobalScope() { + return Sentry.getGlobalScope(); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index bd1fcee04b..fe5c8b4112 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -102,8 +102,6 @@ private Sentry() {} * @return the hub */ @ApiStatus.Internal - @ApiStatus.Experimental - @SuppressWarnings("deprecation") public static @NotNull IScopes forkedRootScopes(final @NotNull String creator) { if (globalHubMode) { return rootScopes; From 136b9ceec85df51df5135a485fbd5c7fa0550123 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Apr 2024 08:20:53 +0200 Subject: [PATCH 35/57] Fix EventProcessor ordering on scopes --- .../api/sentry-android-core.api | 3 ++ .../android/core/AnrV2EventProcessor.java | 5 +++ .../core/DefaultAndroidEventProcessor.java | 5 +++ .../PerformanceAndroidEventProcessor.java | 5 +++ .../core/ScreenshotEventProcessor.java | 5 +++ .../core/ViewHierarchyEventProcessor.java | 5 +++ .../api/sentry-opentelemetry-core.api | 1 + .../OpenTelemetryLinkErrorEventProcessor.java | 5 +++ ...tryRequestHttpServletRequestProcessor.java | 5 +++ ...tryRequestHttpServletRequestProcessor.java | 5 +++ .../api/sentry-spring-jakarta.api | 2 + .../jakarta/ContextTagsEventProcessor.java | 5 +++ ...tryRequestHttpServletRequestProcessor.java | 6 +++ .../spring/jakarta/SentrySpringFilter.java | 6 +++ sentry-spring/api/sentry-spring.api | 2 + .../spring/ContextTagsEventProcessor.java | 5 +++ ...tryRequestHttpServletRequestProcessor.java | 6 +++ .../io/sentry/spring/SentrySpringFilter.java | 6 +++ sentry/api/sentry.api | 22 ++++++++++ .../java/io/sentry/CombinedScopeView.java | 19 ++++++--- ...eduplicateMultithreadedEventProcessor.java | 5 +++ ...DuplicateEventDetectionEventProcessor.java | 5 +++ .../main/java/io/sentry/EventProcessor.java | 11 +++++ sentry/src/main/java/io/sentry/IScope.java | 6 +++ .../java/io/sentry/MainEventProcessor.java | 5 +++ sentry/src/main/java/io/sentry/NoOpScope.java | 7 ++++ sentry/src/main/java/io/sentry/Scope.java | 19 ++++++++- .../src/main/java/io/sentry/SentryClient.java | 1 + .../sentry/SentryRuntimeEventProcessor.java | 5 +++ .../EventProcessorAndOrder.java | 34 +++++++++++++++ .../io/sentry/util/EventProcessorUtils.java | 23 ++++++++++ .../java/io/sentry/CombinedScopeViewTest.kt | 42 +++++++++++++++++++ 32 files changed, 278 insertions(+), 8 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java create mode 100644 sentry/src/main/java/io/sentry/util/EventProcessorUtils.java diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 2afe788ef8..9ee8843eea 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -97,6 +97,7 @@ public final class io/sentry/android/core/AnrIntegrationFactory { public final class io/sentry/android/core/AnrV2EventProcessor : io/sentry/BackfillingEventProcessor { public fun (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/android/core/BuildInfoProvider;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction; } @@ -236,6 +237,7 @@ public final class io/sentry/android/core/PhoneStateBreadcrumbsIntegration : io/ public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/android/core/BuildInfoProvider;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction; } @@ -386,6 +388,7 @@ public final class io/sentry/android/core/UserInteractionIntegration : android/a public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/android/core/SentryAndroidOptions;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction; public static fun snapshotViewHierarchy (Landroid/app/Activity;Lio/sentry/ILogger;)Lio/sentry/protocol/ViewHierarchy; diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java index 45f997542b..9ff1294338 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java @@ -429,6 +429,11 @@ private void setOptionsTags(final @NotNull SentryBaseEvent event) { } // endregion + @Override + public @Nullable Long getOrder() { + return 12000L; + } + // region static values private void setStaticValues(final @NotNull SentryEvent event) { mergeUser(event); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java index 45e4b78787..999f187fe5 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java @@ -303,4 +303,9 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) { return transaction; } + + @Override + public @Nullable Long getOrder() { + return 8000L; + } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java index 2502722f85..6a1a7c67fa 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PerformanceAndroidEventProcessor.java @@ -254,4 +254,9 @@ private static SentrySpan timeSpanToSentrySpan( null, defaultSpanData); } + + @Override + public @Nullable Long getOrder() { + return 9000L; + } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ScreenshotEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/ScreenshotEventProcessor.java index 5e07a44078..87a2caf05f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ScreenshotEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ScreenshotEventProcessor.java @@ -98,4 +98,9 @@ public ScreenshotEventProcessor( hint.set(ANDROID_ACTIVITY, activity); return event; } + + @Override + public @Nullable Long getOrder() { + return 10000L; + } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java index 30e9f8de11..f8f42ac145 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java @@ -284,4 +284,9 @@ private static ViewHierarchyNode viewToNode(@NotNull final View view) { return node; } + + @Override + public @Nullable Long getOrder() { + return 11000L; + } } diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/api/sentry-opentelemetry-core.api b/sentry-opentelemetry/sentry-opentelemetry-core/api/sentry-opentelemetry-core.api index 18c73a9b68..78eee943ed 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/api/sentry-opentelemetry-core.api +++ b/sentry-opentelemetry/sentry-opentelemetry-core/api/sentry-opentelemetry-core.api @@ -1,5 +1,6 @@ public final class io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor : io/sentry/EventProcessor { public fun ()V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } diff --git a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java index bfc4cd05f1..9ed17b06dd 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java +++ b/sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryLinkErrorEventProcessor.java @@ -99,4 +99,9 @@ public OpenTelemetryLinkErrorEventProcessor() { return event; } + + @Override + public @Nullable Long getOrder() { + return 6000L; + } } diff --git a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryRequestHttpServletRequestProcessor.java b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryRequestHttpServletRequestProcessor.java index 17ac102090..1ee536cb92 100644 --- a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryRequestHttpServletRequestProcessor.java +++ b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryRequestHttpServletRequestProcessor.java @@ -56,4 +56,9 @@ public SentryRequestHttpServletRequestProcessor(@NotNull HttpServletRequest http private static @Nullable String toString(final @Nullable Enumeration enumeration) { return enumeration != null ? String.join(",", Collections.list(enumeration)) : null; } + + @Override + public @Nullable Long getOrder() { + return 4000L; + } } diff --git a/sentry-servlet/src/main/java/io/sentry/servlet/SentryRequestHttpServletRequestProcessor.java b/sentry-servlet/src/main/java/io/sentry/servlet/SentryRequestHttpServletRequestProcessor.java index b24c0446b0..a005d50c0a 100644 --- a/sentry-servlet/src/main/java/io/sentry/servlet/SentryRequestHttpServletRequestProcessor.java +++ b/sentry-servlet/src/main/java/io/sentry/servlet/SentryRequestHttpServletRequestProcessor.java @@ -56,4 +56,9 @@ public SentryRequestHttpServletRequestProcessor(@NotNull HttpServletRequest http private static @Nullable String toString(final @Nullable Enumeration enumeration) { return enumeration != null ? String.join(",", Collections.list(enumeration)) : null; } + + @Override + public @Nullable Long getOrder() { + return 4000L; + } } diff --git a/sentry-spring-jakarta/api/sentry-spring-jakarta.api b/sentry-spring-jakarta/api/sentry-spring-jakarta.api index 156ab1aaee..7f414c3495 100644 --- a/sentry-spring-jakarta/api/sentry-spring-jakarta.api +++ b/sentry-spring-jakarta/api/sentry-spring-jakarta.api @@ -5,6 +5,7 @@ public final class io/sentry/spring/jakarta/BuildConfig { public final class io/sentry/spring/jakarta/ContextTagsEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/SentryOptions;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } @@ -43,6 +44,7 @@ public class io/sentry/spring/jakarta/SentryInitBeanPostProcessor : org/springfr public class io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/spring/jakarta/tracing/TransactionNameProvider;Ljakarta/servlet/http/HttpServletRequest;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/ContextTagsEventProcessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/ContextTagsEventProcessor.java index 0f00b4e9f7..94f49d8319 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/ContextTagsEventProcessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/ContextTagsEventProcessor.java @@ -38,4 +38,9 @@ public ContextTagsEventProcessor(final @NotNull SentryOptions options) { } return event; } + + @Override + public @Nullable Long getOrder() { + return 14000L; + } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor.java index fa782030c0..91b27ddeac 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestHttpServletRequestProcessor.java @@ -8,6 +8,7 @@ import io.sentry.util.Objects; import jakarta.servlet.http.HttpServletRequest; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** Attaches transaction name from the HTTP request to {@link SentryEvent}. */ @Open @@ -30,4 +31,9 @@ public SentryRequestHttpServletRequestProcessor( } return event; } + + @Override + public @Nullable Long getOrder() { + return 5000L; + } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index 2e3561a982..de8b5bce65 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -24,6 +24,7 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; import org.springframework.util.MimeType; import org.springframework.web.filter.OncePerRequestFilter; @@ -157,5 +158,10 @@ public RequestBodyExtractingEventProcessor( } return event; } + + @Override + public @Nullable Long getOrder() { + return 3000L; + } } } diff --git a/sentry-spring/api/sentry-spring.api b/sentry-spring/api/sentry-spring.api index 58de26098f..7c6af0ecf5 100644 --- a/sentry-spring/api/sentry-spring.api +++ b/sentry-spring/api/sentry-spring.api @@ -5,6 +5,7 @@ public final class io/sentry/spring/BuildConfig { public final class io/sentry/spring/ContextTagsEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/SentryOptions;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } @@ -43,6 +44,7 @@ public class io/sentry/spring/SentryInitBeanPostProcessor : org/springframework/ public class io/sentry/spring/SentryRequestHttpServletRequestProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/spring/tracing/TransactionNameProvider;Ljavax/servlet/http/HttpServletRequest;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } diff --git a/sentry-spring/src/main/java/io/sentry/spring/ContextTagsEventProcessor.java b/sentry-spring/src/main/java/io/sentry/spring/ContextTagsEventProcessor.java index 79330bdf51..41ff04d0c4 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/ContextTagsEventProcessor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/ContextTagsEventProcessor.java @@ -38,4 +38,9 @@ public ContextTagsEventProcessor(final @NotNull SentryOptions options) { } return event; } + + @Override + public @Nullable Long getOrder() { + return 14000L; + } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestHttpServletRequestProcessor.java b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestHttpServletRequestProcessor.java index 38c1067cab..426571ba6e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestHttpServletRequestProcessor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestHttpServletRequestProcessor.java @@ -8,6 +8,7 @@ import io.sentry.util.Objects; import javax.servlet.http.HttpServletRequest; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** Attaches transaction name from the HTTP request to {@link SentryEvent}. */ @Open @@ -30,4 +31,9 @@ public SentryRequestHttpServletRequestProcessor( } return event; } + + @Override + public @Nullable Long getOrder() { + return 5000L; + } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 252a07910c..af55ac2ce3 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; import org.springframework.util.MimeType; import org.springframework.web.filter.OncePerRequestFilter; @@ -157,5 +158,10 @@ public RequestBodyExtractingEventProcessor( } return event; } + + @Override + public @Nullable Long getOrder() { + return 3000L; + } } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 0a6c85f6ad..30e3e82689 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -255,6 +255,7 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope { public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; + public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; @@ -342,6 +343,7 @@ public final class io/sentry/DateUtils { public final class io/sentry/DeduplicateMultithreadedEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/SentryOptions;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } @@ -377,6 +379,7 @@ public final class io/sentry/DsnUtil { public final class io/sentry/DuplicateEventDetectionEventProcessor : io/sentry/EventProcessor { public fun (Lio/sentry/SentryOptions;)V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; } @@ -392,6 +395,7 @@ public final class io/sentry/EnvelopeSender : io/sentry/IEnvelopeSender { } public abstract interface class io/sentry/EventProcessor { + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction; } @@ -788,6 +792,7 @@ public abstract interface class io/sentry/IScope { public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; public abstract fun getLevel ()Lio/sentry/SentryLevel; public abstract fun getOptions ()Lio/sentry/SentryOptions; + public abstract fun getOrderedEventProcessors ()Ljava/util/List; public abstract fun getPropagationContext ()Lio/sentry/PropagationContext; public abstract fun getRequest ()Lio/sentry/protocol/Request; public abstract fun getScreen ()Ljava/lang/String; @@ -1160,6 +1165,7 @@ public abstract interface class io/sentry/JsonUnknown { public final class io/sentry/MainEventProcessor : io/sentry/EventProcessor, java/io/Closeable { public fun (Lio/sentry/SentryOptions;)V public fun close ()V + public fun getOrder ()Ljava/lang/Long; public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent; public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction; } @@ -1447,6 +1453,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; + public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; @@ -1889,6 +1896,7 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; + public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; @@ -1958,6 +1966,7 @@ public abstract class io/sentry/ScopeObserverAdapter : io/sentry/IScopeObserver } public final class io/sentry/ScopeType : java/lang/Enum { + public static final field COMBINED Lio/sentry/ScopeType; public static final field CURRENT Lio/sentry/ScopeType; public static final field GLOBAL Lio/sentry/ScopeType; public static final field ISOLATION Lio/sentry/ScopeType; @@ -3805,6 +3814,14 @@ public final class io/sentry/internal/debugmeta/ResourcesDebugMetaLoader : io/se public fun loadDebugMeta ()Ljava/util/List; } +public final class io/sentry/internal/eventprocessor/EventProcessorAndOrder : java/lang/Comparable { + public fun (Lio/sentry/EventProcessor;Ljava/lang/Long;)V + public fun compareTo (Lio/sentry/internal/eventprocessor/EventProcessorAndOrder;)I + public synthetic fun compareTo (Ljava/lang/Object;)I + public fun getEventProcessor ()Lio/sentry/EventProcessor; + public fun getOrder ()Ljava/lang/Long; +} + public abstract interface class io/sentry/internal/gestures/GestureTargetLocator { public abstract fun locate (Ljava/lang/Object;FFLio/sentry/internal/gestures/UiElement$Type;)Lio/sentry/internal/gestures/UiElement; } @@ -5381,6 +5398,11 @@ public final class io/sentry/util/DebugMetaPropertiesApplier { public static fun getProguardUuid (Ljava/util/Properties;)Ljava/lang/String; } +public final class io/sentry/util/EventProcessorUtils { + public fun ()V + public static fun unwrap (Ljava/util/List;)Ljava/util/List; +} + public final class io/sentry/util/ExceptionUtils { public fun ()V public static fun findRootCause (Ljava/lang/Throwable;)Ljava/lang/Throwable; diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index ee6fdad06e..332f77f128 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -1,9 +1,11 @@ package io.sentry; +import io.sentry.internal.eventprocessor.EventProcessorAndOrder; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; import io.sentry.protocol.SentryId; import io.sentry.protocol.User; +import io.sentry.util.EventProcessorUtils; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -341,15 +343,20 @@ public void clearAttachments() { } @Override - public @NotNull List getEventProcessors() { - // TODO [HSM] mechanism for ordering event processors - final @NotNull List allEventProcessors = new CopyOnWriteArrayList<>(); - allEventProcessors.addAll(globalScope.getEventProcessors()); - allEventProcessors.addAll(isolationScope.getEventProcessors()); - allEventProcessors.addAll(scope.getEventProcessors()); + public @NotNull List getOrderedEventProcessors() { + final @NotNull List allEventProcessors = new CopyOnWriteArrayList<>(); + allEventProcessors.addAll(globalScope.getOrderedEventProcessors()); + allEventProcessors.addAll(isolationScope.getOrderedEventProcessors()); + allEventProcessors.addAll(scope.getOrderedEventProcessors()); + Collections.sort(allEventProcessors); return allEventProcessors; } + @Override + public @NotNull List getEventProcessors() { + return EventProcessorUtils.unwrap(getOrderedEventProcessors()); + } + @Override public void addEventProcessor(@NotNull EventProcessor eventProcessor) { getDefaultWriteScope().addEventProcessor(eventProcessor); diff --git a/sentry/src/main/java/io/sentry/DeduplicateMultithreadedEventProcessor.java b/sentry/src/main/java/io/sentry/DeduplicateMultithreadedEventProcessor.java index 924b253db8..b5869a6379 100644 --- a/sentry/src/main/java/io/sentry/DeduplicateMultithreadedEventProcessor.java +++ b/sentry/src/main/java/io/sentry/DeduplicateMultithreadedEventProcessor.java @@ -62,4 +62,9 @@ public DeduplicateMultithreadedEventProcessor(final @NotNull SentryOptions optio processedEvents.put(type, currentEventTid); return event; } + + @Override + public @Nullable Long getOrder() { + return 7000L; + } } diff --git a/sentry/src/main/java/io/sentry/DuplicateEventDetectionEventProcessor.java b/sentry/src/main/java/io/sentry/DuplicateEventDetectionEventProcessor.java index e9b77a8860..5004e6514e 100644 --- a/sentry/src/main/java/io/sentry/DuplicateEventDetectionEventProcessor.java +++ b/sentry/src/main/java/io/sentry/DuplicateEventDetectionEventProcessor.java @@ -62,4 +62,9 @@ private static boolean containsAnyKey( } return causes; } + + @Override + public @Nullable Long getOrder() { + return 1000L; + } } diff --git a/sentry/src/main/java/io/sentry/EventProcessor.java b/sentry/src/main/java/io/sentry/EventProcessor.java index ba67508614..6a8f3c7057 100644 --- a/sentry/src/main/java/io/sentry/EventProcessor.java +++ b/sentry/src/main/java/io/sentry/EventProcessor.java @@ -32,4 +32,15 @@ default SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) { default SentryTransaction process(@NotNull SentryTransaction transaction, @NotNull Hint hint) { return transaction; } + + /** + * Controls when this EventProcessor is invoked. + * + * @return order higher number = later, lower number = earlier (negative values may also be + * passed), null = latest (note: multiple event processors using null may lead to random + * ordering) + */ + default @Nullable Long getOrder() { + return null; + } } diff --git a/sentry/src/main/java/io/sentry/IScope.java b/sentry/src/main/java/io/sentry/IScope.java index d761ccc192..c16deae90f 100644 --- a/sentry/src/main/java/io/sentry/IScope.java +++ b/sentry/src/main/java/io/sentry/IScope.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.internal.eventprocessor.EventProcessorAndOrder; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; import io.sentry.protocol.SentryId; @@ -303,9 +304,14 @@ public interface IScope { * * @return the event processors list */ + @ApiStatus.Internal @NotNull List getEventProcessors(); + @ApiStatus.Internal + @NotNull + List getOrderedEventProcessors(); + /** * Adds an event processor to the Scope's event processors list * diff --git a/sentry/src/main/java/io/sentry/MainEventProcessor.java b/sentry/src/main/java/io/sentry/MainEventProcessor.java index abbf21c84e..23a14eb3a6 100644 --- a/sentry/src/main/java/io/sentry/MainEventProcessor.java +++ b/sentry/src/main/java/io/sentry/MainEventProcessor.java @@ -307,4 +307,9 @@ boolean isClosed() { HostnameCache getHostnameCache() { return hostnameCache; } + + @Override + public @Nullable Long getOrder() { + return 0L; + } } diff --git a/sentry/src/main/java/io/sentry/NoOpScope.java b/sentry/src/main/java/io/sentry/NoOpScope.java index 400a7e739f..c4195ed377 100644 --- a/sentry/src/main/java/io/sentry/NoOpScope.java +++ b/sentry/src/main/java/io/sentry/NoOpScope.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.internal.eventprocessor.EventProcessorAndOrder; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; import io.sentry.protocol.SentryId; @@ -183,6 +184,12 @@ public void clearAttachments() {} return new ArrayList<>(); } + @ApiStatus.Internal + @Override + public @NotNull List getOrderedEventProcessors() { + return new ArrayList<>(); + } + @Override public void addEventProcessor(@NotNull EventProcessor eventProcessor) {} diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index 665f789158..e0a5d5b257 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.internal.eventprocessor.EventProcessorAndOrder; import io.sentry.protocol.App; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; @@ -7,6 +8,7 @@ import io.sentry.protocol.TransactionNameSource; import io.sentry.protocol.User; import io.sentry.util.CollectionUtils; +import io.sentry.util.EventProcessorUtils; import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; import io.sentry.util.Pair; @@ -61,7 +63,7 @@ public final class Scope implements IScope { private @NotNull Map extra = new ConcurrentHashMap<>(); /** Scope's event processor list */ - private @NotNull List eventProcessors = new CopyOnWriteArrayList<>(); + private @NotNull List eventProcessors = new CopyOnWriteArrayList<>(); /** Scope's SentryOptions */ private final @NotNull SentryOptions options; @@ -766,6 +768,19 @@ public void clearAttachments() { @NotNull @Override public List getEventProcessors() { + return EventProcessorUtils.unwrap(eventProcessors); + } + + /** + * Returns the Scope's event processors including their order + * + * @return the event processors list and their order + */ + @ApiStatus.Internal + @NotNull + @Override + public List getOrderedEventProcessors() { + // TODO [HSM] This isn't actually ordered but only gets ordered in CombinedScopeView return eventProcessors; } @@ -776,7 +791,7 @@ public List getEventProcessors() { */ @Override public void addEventProcessor(final @NotNull EventProcessor eventProcessor) { - eventProcessors.add(eventProcessor); + eventProcessors.add(new EventProcessorAndOrder(eventProcessor, eventProcessor.getOrder())); } /** diff --git a/sentry/src/main/java/io/sentry/SentryClient.java b/sentry/src/main/java/io/sentry/SentryClient.java index ea70371722..43db89a75b 100644 --- a/sentry/src/main/java/io/sentry/SentryClient.java +++ b/sentry/src/main/java/io/sentry/SentryClient.java @@ -139,6 +139,7 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul } } + // TODO [HSM] EventProcessors from options are always executed after those from scopes event = processEvent(event, hint, options.getEventProcessors()); if (event != null) { diff --git a/sentry/src/main/java/io/sentry/SentryRuntimeEventProcessor.java b/sentry/src/main/java/io/sentry/SentryRuntimeEventProcessor.java index 9d0d6443aa..ca19a9ca74 100644 --- a/sentry/src/main/java/io/sentry/SentryRuntimeEventProcessor.java +++ b/sentry/src/main/java/io/sentry/SentryRuntimeEventProcessor.java @@ -42,4 +42,9 @@ public SentryRuntimeEventProcessor() { } return event; } + + @Override + public @Nullable Long getOrder() { + return 2000L; + } } diff --git a/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java b/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java new file mode 100644 index 0000000000..1f504f2555 --- /dev/null +++ b/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java @@ -0,0 +1,34 @@ +package io.sentry.internal.eventprocessor; + +import io.sentry.EventProcessor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class EventProcessorAndOrder implements Comparable { + + private final @NotNull EventProcessor eventProcessor; + private final @NotNull Long order; + + public EventProcessorAndOrder( + final @NotNull EventProcessor eventProcessor, final @Nullable Long order) { + this.eventProcessor = eventProcessor; + if (order == null) { + this.order = System.nanoTime(); + } else { + this.order = order; + } + } + + public @NotNull EventProcessor getEventProcessor() { + return eventProcessor; + } + + public @NotNull Long getOrder() { + return order; + } + + @Override + public int compareTo(@NotNull EventProcessorAndOrder o) { + return order.compareTo(o.order); + } +} diff --git a/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java b/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java new file mode 100644 index 0000000000..b47d40ecd9 --- /dev/null +++ b/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java @@ -0,0 +1,23 @@ +package io.sentry.util; + +import io.sentry.EventProcessor; +import io.sentry.internal.eventprocessor.EventProcessorAndOrder; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import org.jetbrains.annotations.Nullable; + +public final class EventProcessorUtils { + + public static List unwrap( + final @Nullable List orderedEventProcessor) { + final List eventProcessors = new CopyOnWriteArrayList<>(); + + if (orderedEventProcessor != null) { + for (EventProcessorAndOrder eventProcessorAndOrder : orderedEventProcessor) { + eventProcessors.add(eventProcessorAndOrder.getEventProcessor()); + } + } + + return eventProcessors; + } +} diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index 38023da18e..d8e6783c4c 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -69,4 +69,46 @@ class CombinedScopeViewTest { assertEquals("current 3", breadcrumbs2.poll().message) assertEquals("current 4", breadcrumbs2.poll().message) } + + @Test + fun `event processors from options are not returned`() { + val options = SentryOptions().also { + it.addEventProcessor(MainEventProcessor(it)) + } + + val globalScope = Scope(options) + val isolationScope = Scope(options) + val scope = Scope(options) + + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + assertEquals(0, combined.eventProcessors.size) + } + + @Test + fun `event processors from options and all scopes in order`() { + val options = SentryOptions() + + val globalScope = Scope(options) + val isolationScope = Scope(options) + val scope = Scope(options) + + val first = TestEventProcessor(0).also { scope.addEventProcessor(it) } + val second = TestEventProcessor(1000).also { globalScope.addEventProcessor(it) } + val third = TestEventProcessor(2000).also { isolationScope.addEventProcessor(it) } + val fourth = TestEventProcessor(3000).also { scope.addEventProcessor(it) } + + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + val eventProcessors = combined.eventProcessors + + assertEquals(first, eventProcessors.get(0)) + assertEquals(second, eventProcessors.get(1)) + assertEquals(third, eventProcessors.get(2)) + assertEquals(fourth, eventProcessors.get(3)) + } + + class TestEventProcessor(val orderNumber: Long?) : EventProcessor { + override fun getOrder() = orderNumber + } } From 94d54efe06e3daf7df91d8b2129626491fb13092 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Apr 2024 09:44:57 +0200 Subject: [PATCH 36/57] Reuse code in Scopes --- .../java/io/sentry/CombinedScopeView.java | 45 ++++++++++++------- sentry/src/main/java/io/sentry/Scope.java | 2 +- sentry/src/main/java/io/sentry/Scopes.java | 36 +++------------ 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 332f77f128..b125ce6c81 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -1,5 +1,7 @@ package io.sentry; +import static io.sentry.Scope.createBreadcrumbsList; + import io.sentry.internal.eventprocessor.EventProcessorAndOrder; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; @@ -173,17 +175,6 @@ public void setFingerprint(@NotNull List fingerprint) { return breadcrumbs; } - /** - * Creates a breadcrumb list with the max number of breadcrumbs - * - * @param maxBreadcrumb the max number of breadcrumbs - * @return the breadcrumbs queue - */ - // TODO [HSM] copied from Scope, should reuse instead - private @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { - return SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb)); - } - @Override public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) { getDefaultWriteScope().addBreadcrumb(breadcrumb, hint); @@ -313,14 +304,34 @@ public void removeContexts(@NotNull String key) { } private @NotNull IScope getDefaultWriteScope() { - // TODO [HSM] use Scopes.getSpecificScope? - if (ScopeType.CURRENT.equals(getOptions().getDefaultScopeType())) { - return scope; + return getSpecificScope(null); + } + + IScope getSpecificScope(final @Nullable ScopeType scopeType) { + if (scopeType != null) { + switch (scopeType) { + case CURRENT: + return scope; + case ISOLATION: + return isolationScope; + case GLOBAL: + return globalScope; + default: + break; + } } - if (ScopeType.ISOLATION.equals(getOptions().getDefaultScopeType())) { - return isolationScope; + + switch (getOptions().getDefaultScopeType()) { + case CURRENT: + return scope; + case ISOLATION: + return isolationScope; + case GLOBAL: + return globalScope; + default: + // calm the compiler + return scope; } - return globalScope; } @Override diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index e0a5d5b257..521716b141 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -755,7 +755,7 @@ public void clearAttachments() { * @param maxBreadcrumb the max number of breadcrumbs * @return the breadcrumbs queue */ - private @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { + static @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { return SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb)); } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index bb83e5060c..75300ec818 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -39,6 +39,8 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; private final @NotNull MetricsApi metricsApi; + private final @NotNull CombinedScopeView combinedScope; + Scopes( final @NotNull IScope scope, final @NotNull IScope isolationScope, @@ -55,6 +57,7 @@ private Scopes( final @NotNull String creator) { validateOptions(options); + this.combinedScope = new CombinedScopeView(getGlobalScope(), isolationScope, scope); this.scope = scope; this.isolationScope = isolationScope; this.parentScopes = parentScopes; @@ -368,8 +371,7 @@ public void endSession() { } private IScope getCombinedScopeView() { - // TODO [HSM] create in ctor? - return new CombinedScopeView(getGlobalScope(), isolationScope, scope); + return combinedScope; } @Override @@ -433,34 +435,6 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable } } - private IScope getSpecificScope(final @Nullable ScopeType scopeType) { - // TODO [HSM] extract and reuse - if (scopeType != null) { - switch (scopeType) { - case CURRENT: - return scope; - case ISOLATION: - return isolationScope; - case GLOBAL: - return getGlobalScope(); - default: - break; - } - } - - switch (getOptions().getDefaultScopeType()) { - case CURRENT: - return scope; - case ISOLATION: - return isolationScope; - case GLOBAL: - return getGlobalScope(); - default: - // calm the compiler - return scope; - } - } - @Override public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { addBreadcrumb(breadcrumb, new Hint()); @@ -679,7 +653,7 @@ public void configureScope( "Instance is disabled and this 'configureScope' call is a no-op."); } else { try { - callback.run(getSpecificScope(scopeType)); + callback.run(combinedScope.getSpecificScope(scopeType)); } catch (Throwable e) { options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); } From 017599ddb0fa2712beeae470c2fa8c9295bfe826 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Apr 2024 15:03:05 +0200 Subject: [PATCH 37/57] No longer replace global scope --- .../android/core/InternalSentrySdk.java | 1 + .../io/sentry/android/core/SentryAndroid.java | 4 + sentry/api/sentry.api | 4 + .../java/io/sentry/CombinedScopeView.java | 9 +- sentry/src/main/java/io/sentry/IScope.java | 3 + sentry/src/main/java/io/sentry/NoOpScope.java | 3 + sentry/src/main/java/io/sentry/Scope.java | 21 +- sentry/src/main/java/io/sentry/Scopes.java | 206 ++++++++++-------- sentry/src/main/java/io/sentry/Sentry.java | 12 +- 9 files changed, 160 insertions(+), 103 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index 692d8562f8..3170a4f1ec 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -44,6 +44,7 @@ public final class InternalSentrySdk { @Nullable public static IScope getCurrentScope() { final @NotNull AtomicReference scopeRef = new AtomicReference<>(); + // TODO [HSM] should this retrieve combined scope? ScopesAdapter.getInstance() .configureScope( scope -> { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index 424de4d82e..b677cc5e34 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -24,6 +24,10 @@ /** Sentry initialization class */ public final class SentryAndroid { + static { + Sentry.getGlobalScope().replaceOptions(new SentryAndroidOptions()); + } + // SystemClock.uptimeMillis() isn't affected by phone provider or clock changes. private static final long sdkInitMillis = SystemClock.uptimeMillis(); diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 30e3e82689..d16d15b44b 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -268,6 +268,7 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V + public fun replaceOptions (Lio/sentry/SentryOptions;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -805,6 +806,7 @@ public abstract interface class io/sentry/IScope { public abstract fun removeContexts (Ljava/lang/String;)V public abstract fun removeExtra (Ljava/lang/String;)V public abstract fun removeTag (Ljava/lang/String;)V + public abstract fun replaceOptions (Lio/sentry/SentryOptions;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1466,6 +1468,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V + public fun replaceOptions (Lio/sentry/SentryOptions;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V @@ -1909,6 +1912,7 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun removeContexts (Ljava/lang/String;)V public fun removeExtra (Ljava/lang/String;)V public fun removeTag (Ljava/lang/String;)V + public fun replaceOptions (Lio/sentry/SentryOptions;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index b125ce6c81..38873bb574 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -16,6 +16,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -395,7 +396,7 @@ public void withTransaction(Scope.@NotNull IWithTransaction callback) { @Override public @NotNull SentryOptions getOptions() { - return scope.getOptions(); + return globalScope.getOptions(); } @Override @@ -474,4 +475,10 @@ public void setSpanContext( @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) { globalScope.setSpanContext(throwable, span, transactionName); } + + @ApiStatus.Internal + @Override + public void replaceOptions(@NotNull SentryOptions options) { + globalScope.replaceOptions(options); + } } diff --git a/sentry/src/main/java/io/sentry/IScope.java b/sentry/src/main/java/io/sentry/IScope.java index c16deae90f..8259a225ac 100644 --- a/sentry/src/main/java/io/sentry/IScope.java +++ b/sentry/src/main/java/io/sentry/IScope.java @@ -396,4 +396,7 @@ void setSpanContext( final @NotNull Throwable throwable, final @NotNull ISpan span, final @NotNull String transactionName); + + @ApiStatus.Internal + void replaceOptions(final @NotNull SentryOptions options); } diff --git a/sentry/src/main/java/io/sentry/NoOpScope.java b/sentry/src/main/java/io/sentry/NoOpScope.java index c4195ed377..5335f49519 100644 --- a/sentry/src/main/java/io/sentry/NoOpScope.java +++ b/sentry/src/main/java/io/sentry/NoOpScope.java @@ -276,4 +276,7 @@ public void assignTraceContext(@NotNull SentryEvent event) {} @Override public void setSpanContext( @NotNull Throwable throwable, @NotNull ISpan span, @NotNull String transactionName) {} + + @Override + public void replaceOptions(@NotNull SentryOptions options) {} } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index 521716b141..e894445bd8 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -54,7 +54,7 @@ public final class Scope implements IScope { private @NotNull List fingerprint = new ArrayList<>(); /** Scope's breadcrumb queue */ - private final @NotNull Queue breadcrumbs; + private volatile @NotNull Queue breadcrumbs; /** Scope's tags */ private @NotNull Map tags = new ConcurrentHashMap<>(); @@ -66,7 +66,7 @@ public final class Scope implements IScope { private @NotNull List eventProcessors = new CopyOnWriteArrayList<>(); /** Scope's SentryOptions */ - private final @NotNull SentryOptions options; + private volatile @NotNull SentryOptions options; // TODO Consider: Scope clone doesn't clone sessions @@ -1038,6 +1038,23 @@ public void setSpanContext( } } + @ApiStatus.Internal + @Override + public void replaceOptions(final @NotNull SentryOptions options) { + // TODO [HSM] check if already enabled and noop in that case? + // if (!isEnabled()) {} + this.options = options; + final Queue oldBreadcrumbs = breadcrumbs; + breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs()); + for (Breadcrumb breadcrumb : oldBreadcrumbs) { + /* + this should trigger beforeBreadcrumb + and notify observers for breadcrumbs added before options where customized in Sentry.init + */ + addBreadcrumb(breadcrumb); + } + } + /** The IWithTransaction callback */ @ApiStatus.Internal public interface IWithTransaction { diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 75300ec818..cbeb9ae88f 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -31,9 +31,6 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @Nullable Scopes parentScopes; private final @NotNull String creator; - - // TODO [HSM] should this be set on all scopes (global, isolation, current)? - private final @NotNull SentryOptions options; private volatile boolean isEnabled; private final @NotNull TracesSampler tracesSampler; private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; @@ -44,28 +41,27 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { Scopes( final @NotNull IScope scope, final @NotNull IScope isolationScope, - final @NotNull SentryOptions options, final @NotNull String creator) { - this(scope, isolationScope, null, options, creator); + this(scope, isolationScope, null, creator); } private Scopes( final @NotNull IScope scope, final @NotNull IScope isolationScope, final @Nullable Scopes parentScopes, - final @NotNull SentryOptions options, final @NotNull String creator) { - validateOptions(options); - this.combinedScope = new CombinedScopeView(getGlobalScope(), isolationScope, scope); this.scope = scope; this.isolationScope = isolationScope; this.parentScopes = parentScopes; this.creator = creator; - this.options = options; + + final @NotNull SentryOptions options = getOptions(); + validateOptions(options); this.tracesSampler = new TracesSampler(options); this.transactionPerformanceCollector = options.getTransactionPerformanceCollector(); + // TODO [HSM] Checking isEnabled may not be what we want with global scope anymore this.isEnabled = true; this.metricsApi = new MetricsApi(this); @@ -110,12 +106,12 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { @Override public @NotNull IScopes forkedScopes(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope.clone(), this, options, creator); + return new Scopes(scope.clone(), isolationScope.clone(), this, creator); } @Override public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope, this, options, creator); + return new Scopes(scope.clone(), isolationScope, this, creator); } @Override @@ -146,12 +142,12 @@ public boolean isEnabled() { final @Nullable ScopeCallback scopeCallback) { SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'captureEvent' call is a no-op."); } else if (event == null) { - options.getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter."); + getOptions().getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter."); } else { try { assignTraceContext(event); @@ -160,7 +156,7 @@ public boolean isEnabled() { sentryId = getClient().captureEvent(event, localScope, hint); updateLastEventId(sentryId); } catch (Throwable e) { - options + getOptions() .getLogger() .log( SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e); @@ -185,7 +181,9 @@ private IScope buildLocalScope( callback.run(localScope); return localScope; } catch (Throwable t) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t); + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t); } } return parentScope; @@ -211,20 +209,24 @@ private IScope buildLocalScope( final @Nullable ScopeCallback scopeCallback) { SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'captureMessage' call is a no-op."); } else if (message == null) { - options.getLogger().log(SentryLevel.WARNING, "captureMessage called with null parameter."); + getOptions() + .getLogger() + .log(SentryLevel.WARNING, "captureMessage called with null parameter."); } else { try { final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback); sentryId = getClient().captureMessage(message, level, localScope); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing message: " + message, e); + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error while capturing message: " + message, e); } } updateLastEventId(sentryId); @@ -239,7 +241,7 @@ private IScope buildLocalScope( SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -251,7 +253,7 @@ private IScope buildLocalScope( sentryId = capturedEnvelopeId; } } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e); } } return sentryId; @@ -278,13 +280,15 @@ private IScope buildLocalScope( final @Nullable ScopeCallback scopeCallback) { SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'captureException' call is a no-op."); } else if (throwable == null) { - options.getLogger().log(SentryLevel.WARNING, "captureException called with null parameter."); + getOptions() + .getLogger() + .log(SentryLevel.WARNING, "captureException called with null parameter."); } else { try { final SentryEvent event = new SentryEvent(throwable); @@ -294,7 +298,7 @@ private IScope buildLocalScope( sentryId = getClient().captureEvent(event, localScope, hint); } catch (Throwable e) { - options + getOptions() .getLogger() .log( SentryLevel.ERROR, "Error while capturing exception: " + throwable.getMessage(), e); @@ -307,7 +311,7 @@ private IScope buildLocalScope( @Override public void captureUserFeedback(final @NotNull UserFeedback userFeedback) { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -316,7 +320,7 @@ public void captureUserFeedback(final @NotNull UserFeedback userFeedback) { try { getClient().captureUserFeedback(userFeedback); } catch (Throwable e) { - options + getOptions() .getLogger() .log( SentryLevel.ERROR, @@ -329,7 +333,7 @@ public void captureUserFeedback(final @NotNull UserFeedback userFeedback) { @Override public void startSession() { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op."); @@ -349,7 +353,7 @@ public void startSession() { getClient().captureSession(pair.getCurrent(), hint); } else { - options.getLogger().log(SentryLevel.WARNING, "Session could not be started."); + getOptions().getLogger().log(SentryLevel.WARNING, "Session could not be started."); } } } @@ -357,7 +361,7 @@ public void startSession() { @Override public void endSession() { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op."); } else { @@ -383,17 +387,17 @@ public void close() { @SuppressWarnings("FutureReturnValueIgnored") public void close(final boolean isRestarting) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'close' call is a no-op."); } else { try { - for (Integration integration : options.getIntegrations()) { + for (Integration integration : getOptions().getIntegrations()) { if (integration instanceof Closeable) { try { ((Closeable) integration).close(); } catch (IOException e) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e); } @@ -402,19 +406,20 @@ public void close(final boolean isRestarting) { // TODO [HSM] which scopes do we call this on? isolation and current scope? configureScope(scope -> scope.clear()); - options.getTransactionProfiler().close(); - options.getTransactionPerformanceCollector().close(); - final @NotNull ISentryExecutorService executorService = options.getExecutorService(); + getOptions().getTransactionProfiler().close(); + getOptions().getTransactionPerformanceCollector().close(); + final @NotNull ISentryExecutorService executorService = getOptions().getExecutorService(); if (isRestarting) { - executorService.submit(() -> executorService.close(options.getShutdownTimeoutMillis())); + executorService.submit( + () -> executorService.close(getOptions().getShutdownTimeoutMillis())); } else { - executorService.close(options.getShutdownTimeoutMillis()); + executorService.close(getOptions().getShutdownTimeoutMillis()); } // TODO: should we end session before closing client? getClient().close(isRestarting); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e); } isEnabled = false; } @@ -423,13 +428,15 @@ public void close(final boolean isRestarting) { @Override public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'addBreadcrumb' call is a no-op."); } else if (breadcrumb == null) { - options.getLogger().log(SentryLevel.WARNING, "addBreadcrumb called with null parameter."); + getOptions() + .getLogger() + .log(SentryLevel.WARNING, "addBreadcrumb called with null parameter."); } else { getCombinedScopeView().addBreadcrumb(breadcrumb, hint); } @@ -443,7 +450,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { @Override public void setLevel(final @Nullable SentryLevel level) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op."); } else { @@ -454,7 +461,7 @@ public void setLevel(final @Nullable SentryLevel level) { @Override public void setTransaction(final @Nullable String transaction) { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -462,14 +469,14 @@ public void setTransaction(final @Nullable String transaction) { } else if (transaction != null) { getCombinedScopeView().setTransaction(transaction); } else { - options.getLogger().log(SentryLevel.WARNING, "Transaction cannot be null"); + getOptions().getLogger().log(SentryLevel.WARNING, "Transaction cannot be null"); } } @Override public void setUser(final @Nullable User user) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op."); } else { @@ -480,13 +487,15 @@ public void setUser(final @Nullable User user) { @Override public void setFingerprint(final @NotNull List fingerprint) { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'setFingerprint' call is a no-op."); } else if (fingerprint == null) { - options.getLogger().log(SentryLevel.WARNING, "setFingerprint called with null parameter."); + getOptions() + .getLogger() + .log(SentryLevel.WARNING, "setFingerprint called with null parameter."); } else { getCombinedScopeView().setFingerprint(fingerprint); } @@ -495,7 +504,7 @@ public void setFingerprint(final @NotNull List fingerprint) { @Override public void clearBreadcrumbs() { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -508,11 +517,11 @@ public void clearBreadcrumbs() { @Override public void setTag(final @NotNull String key, final @NotNull String value) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setTag' call is a no-op."); } else if (key == null || value == null) { - options.getLogger().log(SentryLevel.WARNING, "setTag called with null parameter."); + getOptions().getLogger().log(SentryLevel.WARNING, "setTag called with null parameter."); } else { getCombinedScopeView().setTag(key, value); } @@ -521,11 +530,11 @@ public void setTag(final @NotNull String key, final @NotNull String value) { @Override public void removeTag(final @NotNull String key) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'removeTag' call is a no-op."); } else if (key == null) { - options.getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter."); + getOptions().getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter."); } else { getCombinedScopeView().removeTag(key); } @@ -534,11 +543,11 @@ public void removeTag(final @NotNull String key) { @Override public void setExtra(final @NotNull String key, final @NotNull String value) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'setExtra' call is a no-op."); } else if (key == null || value == null) { - options.getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter."); + getOptions().getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter."); } else { getCombinedScopeView().setExtra(key, value); } @@ -547,11 +556,11 @@ public void setExtra(final @NotNull String key, final @NotNull String value) { @Override public void removeExtra(final @NotNull String key) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'removeExtra' call is a no-op."); } else if (key == null) { - options.getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter."); + getOptions().getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter."); } else { getCombinedScopeView().removeExtra(key); } @@ -574,7 +583,7 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { @Override public ISentryLifecycleToken pushScope() { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op."); return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); @@ -587,7 +596,7 @@ public ISentryLifecycleToken pushScope() { @Override public ISentryLifecycleToken pushIsolationScope() { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -608,7 +617,7 @@ public ISentryLifecycleToken pushIsolationScope() { @Override public void popScope() { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op."); } else { @@ -627,7 +636,7 @@ public void withScope(final @NotNull ScopeCallback callback) { try { callback.run(NoOpScope.getInstance()); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); } } else { @@ -637,7 +646,7 @@ public void withScope(final @NotNull ScopeCallback callback) { try { callback.run(forkedScopes.getScope()); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); } } } @@ -646,7 +655,7 @@ public void withScope(final @NotNull ScopeCallback callback) { public void configureScope( final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -655,7 +664,9 @@ public void configureScope( try { callback.run(combinedScope.getSpecificScope(scopeType)); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); } } } @@ -663,15 +674,15 @@ public void configureScope( @Override public void bindClient(final @NotNull ISentryClient client) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'bindClient' call is a no-op."); } else { if (client != null) { - options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); + getOptions().getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); getCombinedScopeView().bindClient(client); } else { - options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); + getOptions().getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); getCombinedScopeView().bindClient(NoOpSentryClient.getInstance()); } } @@ -685,14 +696,14 @@ public boolean isHealthy() { @Override public void flush(long timeoutMillis) { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'flush' call is a no-op."); } else { try { getClient().flush(timeoutMillis); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e); } } } @@ -701,7 +712,7 @@ public void flush(long timeoutMillis) { @SuppressWarnings("deprecation") public @NotNull IHub clone() { if (!isEnabled()) { - options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); + getOptions().getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); } // TODO [HSM] should this fork isolation scope as well? return new HubScopesWrapper(forkedCurrentScope("scopes clone")); @@ -718,14 +729,14 @@ public void flush(long timeoutMillis) { SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'captureTransaction' call is a no-op."); } else { if (!transaction.isFinished()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -733,18 +744,18 @@ public void flush(long timeoutMillis) { transaction.getEventId()); } else { if (!Boolean.TRUE.equals(transaction.isSampled())) { - options + getOptions() .getLogger() .log( SentryLevel.DEBUG, "Transaction %s was dropped due to sampling decision.", transaction.getEventId()); - if (options.getBackpressureMonitor().getDownsampleFactor() > 0) { - options + if (getOptions().getBackpressureMonitor().getDownsampleFactor() > 0) { + getOptions() .getClientReportRecorder() .recordLostEvent(DiscardReason.BACKPRESSURE, DataCategory.Transaction); } else { - options + getOptions() .getClientReportRecorder() .recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Transaction); } @@ -759,7 +770,7 @@ public void flush(long timeoutMillis) { hint, profilingTraceData); } catch (Throwable e) { - options + getOptions() .getLogger() .log( SentryLevel.ERROR, @@ -786,23 +797,23 @@ public void flush(long timeoutMillis) { ITransaction transaction; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, "Instance is disabled and this 'startTransaction' returns a no-op."); transaction = NoOpTransaction.getInstance(); - } else if (!options.getInstrumenter().equals(transactionContext.getInstrumenter())) { - options + } else if (!getOptions().getInstrumenter().equals(transactionContext.getInstrumenter())) { + getOptions() .getLogger() .log( SentryLevel.DEBUG, "Returning no-op for instrumenter %s as the SDK has been configured to use instrumenter %s", transactionContext.getInstrumenter(), - options.getInstrumenter()); + getOptions().getInstrumenter()); transaction = NoOpTransaction.getInstance(); - } else if (!options.isTracingEnabled()) { - options + } else if (!getOptions().isTracingEnabled()) { + getOptions() .getLogger() .log( SentryLevel.INFO, "Tracing is disabled and this 'startTransaction' returns a no-op."); @@ -820,7 +831,7 @@ public void flush(long timeoutMillis) { // The listener is called only if the transaction exists, as the transaction is needed to // stop it if (samplingDecision.getSampled() && samplingDecision.getProfileSampled()) { - final ITransactionProfiler transactionProfiler = options.getTransactionProfiler(); + final ITransactionProfiler transactionProfiler = getOptions().getTransactionProfiler(); // If the profiler is not running, we start and bind it here. if (!transactionProfiler.isRunning()) { transactionProfiler.start(); @@ -857,7 +868,7 @@ public void setSpanContext( public @Nullable ISpan getSpan() { ISpan span = null; if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'getSpan' call is a no-op."); } else { @@ -871,7 +882,7 @@ public void setSpanContext( public @Nullable ITransaction getTransaction() { ITransaction span = null; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -884,19 +895,20 @@ public void setSpanContext( @Override public @NotNull SentryOptions getOptions() { - return options; + return combinedScope.getOptions(); } @Override public @Nullable Boolean isCrashedLastRun() { return SentryCrashLastRunState.getInstance() - .isCrashedLastRun(options.getCacheDirPath(), !options.isEnableAutoSessionTracking()); + .isCrashedLastRun( + getOptions().getCacheDirPath(), !getOptions().isEnableAutoSessionTracking()); } @Override public void reportFullyDisplayed() { - if (options.isEnableTimeToFullDisplayTracing()) { - options.getFullyDisplayedReporter().reportFullyDrawn(); + if (getOptions().isEnableTimeToFullDisplayTracing()) { + getOptions().getFullyDisplayedReporter().reportFullyDrawn(); } } @@ -911,7 +923,7 @@ public void reportFullyDisplayed() { (scope) -> { scope.setPropagationContext(propagationContext); }); - if (options.isTracingEnabled()) { + if (getOptions().isTracingEnabled()) { return TransactionContext.fromPropagationContext(propagationContext); } else { return null; @@ -921,7 +933,7 @@ public void reportFullyDisplayed() { @Override public @Nullable SentryTraceHeader getTraceparent() { if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -940,7 +952,7 @@ public void reportFullyDisplayed() { @Override public @Nullable BaggageHeader getBaggage() { if (!isEnabled()) { - options + getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'getBaggage' call is a no-op."); } else { @@ -959,7 +971,7 @@ public void reportFullyDisplayed() { public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { SentryId sentryId = SentryId.EMPTY_ID; if (!isEnabled()) { - options + getOptions() .getLogger() .log( SentryLevel.WARNING, @@ -968,7 +980,9 @@ public void reportFullyDisplayed() { try { sentryId = getClient().captureCheckIn(checkIn, getCombinedScopeView(), null); } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing check-in for slug", e); + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error while capturing check-in for slug", e); } } updateLastEventId(sentryId); @@ -993,17 +1007,17 @@ public void reportFullyDisplayed() { @Override public @NotNull Map getDefaultTagsForMetrics() { - if (!options.isEnableDefaultTagsForMetrics()) { + if (!getOptions().isEnableDefaultTagsForMetrics()) { return Collections.emptyMap(); } final @NotNull Map tags = new HashMap<>(); - final @Nullable String release = options.getRelease(); + final @Nullable String release = getOptions().getRelease(); if (release != null) { tags.put("release", release); } - final @Nullable String environment = options.getEnvironment(); + final @Nullable String environment = getOptions().getEnvironment(); if (environment != null) { tags.put("environment", environment); } @@ -1026,7 +1040,7 @@ public void reportFullyDisplayed() { @Override public @Nullable LocalMetricsAggregator getLocalMetricsAggregator() { - if (!options.isEnableSpanLocalMetricAggregation()) { + if (!getOptions().isEnableSpanLocalMetricAggregation()) { return null; } final @Nullable ISpan span = getSpan(); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index fe5c8b4112..38d033f28a 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -47,7 +47,12 @@ private Sentry() {} /** The root Scopes or NoOp if Sentry is disabled. */ private static volatile @NotNull IScopes rootScopes = NoOpScopes.getInstance(); - // TODO [HSM] cannot pass options here + /** + * This initializes global scope with default options. Options will later be replaced on + * Sentry.init + * + *

For Android options will also be (temporarily) replaced by SentryAndroid static block. + */ private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); /** Default value for globalHubMode is false */ @@ -265,14 +270,13 @@ private static synchronized void init( options.getLogger().log(SentryLevel.INFO, "GlobalHubMode: '%s'", String.valueOf(globalHubMode)); Sentry.globalHubMode = globalHubMode; + globalScope.replaceOptions(options); final IScopes scopes = getCurrentScopes(); final IScope rootScope = new Scope(options); final IScope rootIsolationScope = new Scope(options); - // TODO [HSM] shouldn't replace global scope - globalScope = new Scope(options); globalScope.bindClient(new SentryClient(options)); - rootScopes = new Scopes(rootScope, rootIsolationScope, options, "Sentry.init"); + rootScopes = new Scopes(rootScope, rootIsolationScope, "Sentry.init"); getScopesStorage().set(rootScopes); From f4c2b3cf3f12ba5c36943b2111c6290772a7fee0 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Apr 2024 16:31:09 +0200 Subject: [PATCH 38/57] Replace hub occurrences in comments, var names etc. --- .../ActivityBreadcrumbsIntegrationTest.kt | 42 ++-- .../core/ActivityLifecycleIntegrationTest.kt | 212 +++++++++--------- .../EnvelopeFileObserverIntegrationTest.kt | 8 +- .../android/core/InternalSentrySdkTest.kt | 29 ++- .../sentry/android/core/SentryAndroidTest.kt | 6 +- .../core/UserInteractionIntegrationTest.kt | 18 +- .../SentryInstrumentationAnotherTest.kt | 2 - .../graphql/SentryInstrumentationTest.kt | 2 - .../samples/android/ProfilingActivity.kt | 4 +- .../spring/boot/jakarta/SentryProperties.java | 3 +- .../webflux/AbstractSentryWebFilter.java | 23 +- .../webflux/SentryWebFluxTracingFilterTest.kt | 2 - .../java/io/sentry/spring/EnableSentry.java | 2 +- .../io/sentry/spring/SentryTaskDecorator.java | 11 +- .../spring/tracing/SentryTracingFilter.java | 2 +- .../spring/webflux/SentryWebFilter.java | 20 +- .../webflux/SentryWebFluxTracingFilterTest.kt | 2 - sentry/api/sentry.api | 1 + .../main/java/io/sentry/EnvelopeSender.java | 2 +- .../main/java/io/sentry/HubScopesWrapper.java | 2 +- sentry/src/main/java/io/sentry/IScopes.java | 22 +- sentry/src/main/java/io/sentry/NoOpHub.java | 3 + .../src/main/java/io/sentry/OutboxSender.java | 2 +- sentry/src/main/java/io/sentry/Scopes.java | 22 +- ...achedEnvelopeFireAndForgetIntegration.java | 2 +- .../SendFireAndForgetEnvelopeSender.java | 2 +- .../sentry/SendFireAndForgetOutboxSender.java | 2 +- sentry/src/main/java/io/sentry/Sentry.java | 20 +- .../main/java/io/sentry/SentryOptions.java | 4 +- .../main/java/io/sentry/SentryWrapper.java | 12 +- .../io/sentry/ShutdownHookIntegration.java | 2 +- .../UncaughtExceptionHandlerIntegration.java | 2 +- .../test/java/io/sentry/ScopesAdapterTest.kt | 78 +++---- sentry/src/test/java/io/sentry/SentryTest.kt | 32 +-- .../test/java/io/sentry/SentryTracerTest.kt | 92 ++++---- .../test/java/io/sentry/SentryWrapperTest.kt | 68 +++--- ...UncaughtExceptionHandlerIntegrationTest.kt | 2 +- .../java/io/sentry/util/CheckInUtilsTest.kt | 7 - 38 files changed, 382 insertions(+), 385 deletions(-) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityBreadcrumbsIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityBreadcrumbsIntegrationTest.kt index 10dc60e74b..56dabd2fbc 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityBreadcrumbsIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityBreadcrumbsIntegrationTest.kt @@ -4,7 +4,7 @@ import android.app.Activity import android.app.Application import android.os.Bundle import io.sentry.Breadcrumb -import io.sentry.Hub +import io.sentry.Scopes import io.sentry.SentryLevel import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull @@ -20,7 +20,7 @@ class ActivityBreadcrumbsIntegrationTest { private class Fixture { val application = mock() - val hub = mock() + val scopes = mock() val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -28,7 +28,7 @@ class ActivityBreadcrumbsIntegrationTest { fun getSut(enabled: Boolean = true): ActivityBreadcrumbsIntegration { options.isEnableActivityLifecycleBreadcrumbs = enabled - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) return ActivityBreadcrumbsIntegration( application ) @@ -40,7 +40,7 @@ class ActivityBreadcrumbsIntegrationTest { @Test fun `When ActivityBreadcrumbsIntegration is disabled, it should not register the activity callback`() { val sut = fixture.getSut(false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application, never()).registerActivityLifecycleCallbacks(any()) } @@ -48,7 +48,7 @@ class ActivityBreadcrumbsIntegrationTest { @Test fun `When ActivityBreadcrumbsIntegration is enabled, it should register the activity callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application).registerActivityLifecycleCallbacks(any()) @@ -59,12 +59,12 @@ class ActivityBreadcrumbsIntegrationTest { @Test fun `When breadcrumb is added, type and category should be set`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("ui.lifecycle", it.category) assertEquals("navigation", it.type) @@ -78,77 +78,77 @@ class ActivityBreadcrumbsIntegrationTest { @Test fun `When activity is created, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is started, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityStarted(activity) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is resumed, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityResumed(activity) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is paused, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityPaused(activity) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is stopped, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityStopped(activity) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is save instance, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivitySaveInstanceState(activity, fixture.bundle) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test fun `When activity is destroyed, it should add a breadcrumb`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityDestroyed(activity) - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt index 2d20a16ec5..e21f650162 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt @@ -14,10 +14,10 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.DateUtils import io.sentry.FullyDisplayedReporter -import io.sentry.Hub import io.sentry.IScope import io.sentry.Scope import io.sentry.ScopeCallback +import io.sentry.Scopes import io.sentry.Sentry import io.sentry.SentryDate import io.sentry.SentryDateProvider @@ -71,7 +71,7 @@ class ActivityLifecycleIntegrationTest { private class Fixture { val application = mock() - val hub = mock() + val scopes = mock() val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -92,13 +92,13 @@ class ActivityLifecycleIntegrationTest { ): ActivityLifecycleIntegration { initializer?.configure(options) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) // We let the ActivityLifecycleIntegration create the proper transaction here val optionCaptor = argumentCaptor() val contextCaptor = argumentCaptor() - whenever(hub.startTransaction(contextCaptor.capture(), optionCaptor.capture())).thenAnswer { - val t = SentryTracer(contextCaptor.lastValue, hub, optionCaptor.lastValue) + whenever(scopes.startTransaction(contextCaptor.capture(), optionCaptor.capture())).thenAnswer { + val t = SentryTracer(contextCaptor.lastValue, scopes, optionCaptor.lastValue) transaction = t return@thenAnswer t } @@ -145,7 +145,7 @@ class ActivityLifecycleIntegrationTest { @Test fun `When ActivityLifecycleIntegration is registered, it registers activity callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application).registerActivityLifecycleCallbacks(any()) } @@ -153,7 +153,7 @@ class ActivityLifecycleIntegrationTest { @Test fun `When ActivityLifecycleIntegration is closed, it should unregister the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() @@ -163,7 +163,7 @@ class ActivityLifecycleIntegrationTest { @Test fun `When ActivityLifecycleIntegration is closed, it should close the ActivityFramesTracker`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() @@ -173,39 +173,39 @@ class ActivityLifecycleIntegrationTest { @Test fun `When tracing is disabled, do not start tracing`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub, never()).startTransaction(any(), any()) + verify(fixture.scopes, never()).startTransaction(any(), any()) } @Test fun `When tracing is enabled but activity is running, do not start tracing again`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction(any(), any()) + verify(fixture.scopes).startTransaction(any(), any()) } @Test fun `Transaction op is ui_load and idle+deadline timeouts are set`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("ui.load", it.operation) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -221,7 +221,7 @@ class ActivityLifecycleIntegrationTest { fun `Activity gets added to ActivityFramesTracker during transaction creation`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityStarted(activity) @@ -233,14 +233,14 @@ class ActivityLifecycleIntegrationTest { fun `Transaction name is the Activity's name`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( check { assertEquals("Activity", it.name) assertEquals(TransactionNameSource.COMPONENT, it.transactionNameSource) @@ -254,9 +254,9 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) sut.applyScope(scope, fixture.transaction) @@ -273,11 +273,11 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) - val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.hub) + val previousTransaction = SentryTracer(TransactionContext("name", "op"), fixture.scopes) scope.transaction = previousTransaction sut.applyScope(scope, fixture.transaction) @@ -297,14 +297,14 @@ class ActivityLifecycleIntegrationTest { it.isEnableTimeToFullDisplayTracing = true it.idleTimeout = 200 }) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) sut.ttidSpanMap.values.first().finish() sut.ttfdSpanMap.values.first().finish() // then transaction should not be immediatelly finished - verify(fixture.hub, never()) + verify(fixture.scopes, never()) .captureTransaction( anyOrNull(), anyOrNull(), @@ -316,7 +316,7 @@ class ActivityLifecycleIntegrationTest { Thread.sleep(400) // then the transaction should be finished - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(SpanStatus.OK, it.status) }, @@ -330,13 +330,13 @@ class ActivityLifecycleIntegrationTest { fun `When tracing auto finish is enabled, it doesn't stop the transaction on onActivityPostResumed`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) sut.onActivityPostResumed(activity) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( check { assertEquals(SpanStatus.OK, it.status) }, @@ -350,7 +350,7 @@ class ActivityLifecycleIntegrationTest { fun `When tracing has status, do not overwrite it`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -360,7 +360,7 @@ class ActivityLifecycleIntegrationTest { sut.onActivityPostResumed(activity) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(SpanStatus.UNKNOWN_ERROR, it.status) }, @@ -376,43 +376,43 @@ class ActivityLifecycleIntegrationTest { it.tracesSampleRate = 1.0 it.isEnableActivityLifecycleTracingAutoFinish = false }) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) sut.onActivityPostResumed(activity) - verify(fixture.hub, never()).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test fun `When tracing is disabled, do not finish transaction`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityPostResumed(activity) - verify(fixture.hub, never()).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test fun `When Activity is destroyed but transaction is running, finish it`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test fun `When transaction is started, adds to WeakWef`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -424,7 +424,7 @@ class ActivityLifecycleIntegrationTest { fun `When Activity is destroyed removes WeakRef`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -437,7 +437,7 @@ class ActivityLifecycleIntegrationTest { fun `When Activity is destroyed, sets appStartSpan status to cancelled and finish it`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -454,7 +454,7 @@ class ActivityLifecycleIntegrationTest { fun `When Activity is destroyed, sets appStartSpan to null`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -469,7 +469,7 @@ class ActivityLifecycleIntegrationTest { fun `When Activity is destroyed, sets ttidSpan status to deadline_exceeded and finish it`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -486,7 +486,7 @@ class ActivityLifecycleIntegrationTest { fun `When Activity is destroyed, sets ttidSpan to null`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -503,7 +503,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -521,7 +521,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() @@ -537,25 +537,25 @@ class ActivityLifecycleIntegrationTest { fun `When new Activity and transaction is created, finish previous ones`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(mock(), mock()) sut.onActivityCreated(mock(), fixture.bundle) - verify(fixture.hub).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test fun `do not stop transaction on resumed if API 29`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, mock()) sut.onActivityResumed(activity) - verify(fixture.hub, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) } @Test @@ -563,7 +563,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut(Build.VERSION_CODES.P) fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, mock()) @@ -571,21 +571,21 @@ class ActivityLifecycleIntegrationTest { sut.ttfdSpanMap.values.first().finish() sut.onActivityResumed(activity) - verify(fixture.hub, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) } @Test fun `start transaction on created if API less than 29`() { val sut = fixture.getSut(Build.VERSION_CODES.P) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) setAppStartTime() val activity = mock() sut.onActivityCreated(activity, mock()) - verify(fixture.hub).startTransaction(any(), any()) + verify(fixture.scopes).startTransaction(any(), any()) } @Test @@ -593,7 +593,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, mock()) @@ -611,7 +611,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, mock()) @@ -626,7 +626,7 @@ class ActivityLifecycleIntegrationTest { fun `App start is Cold when savedInstanceState is null`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, null) @@ -638,7 +638,7 @@ class ActivityLifecycleIntegrationTest { fun `App start is Warm when savedInstanceState is not null`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() val bundle = Bundle() @@ -651,7 +651,7 @@ class ActivityLifecycleIntegrationTest { fun `Do not overwrite App start type after set`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() val bundle = Bundle() @@ -665,7 +665,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true, start transaction with given appStartTime`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -674,7 +674,7 @@ class ActivityLifecycleIntegrationTest { sut.onActivityCreated(activity, fixture.bundle) // call only once - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { assertEquals(date.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp()) @@ -686,7 +686,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true and app start sampling decision is set, start transaction with isAppStart true`() { AppStartMetrics.getInstance().appStartSamplingDecision = mock() val sut = fixture.getSut { it.tracesSampleRate = 1.0 } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -694,7 +694,7 @@ class ActivityLifecycleIntegrationTest { val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { assertEquals(date.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp()) @@ -706,7 +706,7 @@ class ActivityLifecycleIntegrationTest { @Test fun `When firstActivityCreated is true and app start sampling decision is not set, start transaction with isAppStart false`() { val sut = fixture.getSut { it.tracesSampleRate = 1.0 } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -714,7 +714,7 @@ class ActivityLifecycleIntegrationTest { val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { assertEquals(date.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp()) @@ -727,19 +727,19 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is false and app start sampling decision is set, start transaction with isAppStart false`() { AppStartMetrics.getInstance().appStartSamplingDecision = mock() val sut = fixture.getSut { it.tracesSampleRate = 1.0 } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - verify(fixture.hub).startTransaction(any(), check { assertFalse(it.isAppStartTransaction) }) + verify(fixture.scopes).startTransaction(any(), check { assertFalse(it.isAppStartTransaction) }) } @Test fun `When firstActivityCreated is true, do not create app start span if not foregroundImportance`() { val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_BACKGROUND) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually set by SentryPerformanceProvider val date = SentryNanotimeDate(Date(1), 0) @@ -750,7 +750,7 @@ class ActivityLifecycleIntegrationTest { sut.onActivityCreated(activity, fixture.bundle) // call only once - verify(fixture.hub).startTransaction( + verify(fixture.scopes).startTransaction( any(), check { assertNotEquals(date, it.startTimestamp) } ) @@ -760,7 +760,7 @@ class ActivityLifecycleIntegrationTest { fun `Create and finish app start span immediately in case SDK init is deferred`() { val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually set by SentryPerformanceProvider val startDate = SentryNanotimeDate(Date(1), 0) @@ -786,7 +786,7 @@ class ActivityLifecycleIntegrationTest { fun `When SentryPerformanceProvider is disabled, app start time span is still created`() { val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually done by SentryPerformanceProvider, if disabled it's done by // SentryAndroid.init @@ -814,7 +814,7 @@ class ActivityLifecycleIntegrationTest { fun `When app-start end time is already set, it should not be overwritten`() { val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually done by SentryPerformanceProvider val startDate = SentryNanotimeDate(Date(1), 0) @@ -838,7 +838,7 @@ class ActivityLifecycleIntegrationTest { fun `When activity lifecycle happens multiple times, app-start end time should not be overwritten`() { val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND) fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually done by SentryPerformanceProvider val startDate = SentryNanotimeDate(Date(1), 0) @@ -876,7 +876,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true, start app start warm span with given appStartTime`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -893,7 +893,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true, start app start cold span with given appStartTime`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -910,7 +910,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true, start app start span with Warm description`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -927,7 +927,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is true, start app start span with Cold description`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime(date) @@ -944,7 +944,7 @@ class ActivityLifecycleIntegrationTest { fun `When firstActivityCreated is false, start transaction but not with given appStartTime`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val date = SentryNanotimeDate(Date(1), 0) setAppStartTime() @@ -965,12 +965,12 @@ class ActivityLifecycleIntegrationTest { fun `When transaction is finished, it gets removed from scope`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) - whenever(fixture.hub.configureScope(any())).thenAnswer { + whenever(fixture.scopes.configureScope(any())).thenAnswer { val scope = Scope(fixture.options) scope.transaction = fixture.transaction @@ -988,7 +988,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = false - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -1001,7 +1001,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -1016,7 +1016,7 @@ class ActivityLifecycleIntegrationTest { fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true fixture.options.executorService = deferredExecutorService - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) val ttfdSpan = sut.ttfdSpanMap[activity] @@ -1032,7 +1032,7 @@ class ActivityLifecycleIntegrationTest { assertEquals(SpanStatus.DEADLINE_EXCEEDED, ttfdSpan.status) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { // ttfd timed out, so its measurement should not be set val ttfdMeasurement = it.measurements[MeasurementValue.KEY_TIME_TO_FULL_DISPLAY] @@ -1049,7 +1049,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) val ttfdSpan = sut.ttfdSpanMap[activity] @@ -1072,7 +1072,7 @@ class ActivityLifecycleIntegrationTest { assertNull(autoCloseFuture) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { // ttfd was finished successfully, so its measurement should be set val ttfdMeasurement = it.measurements[MeasurementValue.KEY_TIME_TO_FULL_DISPLAY] @@ -1090,7 +1090,7 @@ class ActivityLifecycleIntegrationTest { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() val activity2 = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -1128,7 +1128,7 @@ class ActivityLifecycleIntegrationTest { whenever(activity.findViewById(any())).thenReturn(view) // Make the integration create the spans and register to the FirstDrawDoneListener - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) sut.onActivityResumed(activity) @@ -1143,7 +1143,7 @@ class ActivityLifecycleIntegrationTest { assertTrue(ttidSpan.isFinished) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { // ttid measurement should be set val ttidMeasurement = it.measurements[MeasurementValue.KEY_TIME_TO_INITIAL_DISPLAY] @@ -1166,7 +1166,7 @@ class ActivityLifecycleIntegrationTest { whenever(activity.findViewById(any())).thenReturn(view) // Make the integration create the spans and register to the FirstDrawDoneListener - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) sut.onActivityResumed(activity) @@ -1189,7 +1189,7 @@ class ActivityLifecycleIntegrationTest { assertEquals(newEndDate, ttidSpan.finishDate) sut.onActivityDestroyed(activity) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { // ttid and ttfd measurements should be the same val ttidMeasurement = it.measurements[MeasurementValue.KEY_TIME_TO_INITIAL_DISPLAY] @@ -1211,7 +1211,7 @@ class ActivityLifecycleIntegrationTest { fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) // The ttid span should be running @@ -1233,7 +1233,7 @@ class ActivityLifecycleIntegrationTest { fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true fixture.options.executorService = deferredExecutorService - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) sut.onActivityResumed(activity) @@ -1268,7 +1268,7 @@ class ActivityLifecycleIntegrationTest { fixture.options.tracesSampleRate = 1.0 fixture.options.isEnableTimeToFullDisplayTracing = true - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) val ttfdSpan = sut.ttfdSpanMap[activity] assertNotNull(ttfdSpan) @@ -1294,15 +1294,15 @@ class ActivityLifecycleIntegrationTest { val argumentCaptor: ArgumentCaptor = ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = Scope(fixture.options) val propagationContextAtStart = scope.propagationContext - whenever(fixture.hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(fixture.scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) // once for the screen, and once for the tracing propagation context - verify(fixture.hub, times(2)).configureScope(any()) + verify(fixture.scopes, times(2)).configureScope(any()) assertNotSame(propagationContextAtStart, scope.propagationContext) } @@ -1314,15 +1314,15 @@ class ActivityLifecycleIntegrationTest { val argumentCaptor: ArgumentCaptor = ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = mock() - whenever(fixture.hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(fixture.scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) // once for the screen, and once for the tracing propagation context - verify(fixture.hub, times(2)).configureScope(any()) + verify(fixture.scopes, times(2)).configureScope(any()) verify(scope).setScreen(any()) } @@ -1335,32 +1335,32 @@ class ActivityLifecycleIntegrationTest { val argumentCaptor: ArgumentCaptor = ArgumentCaptor.forClass(ScopeCallback::class.java) val scope = Scope(fixture.options) val propagationContextAtStart = scope.propagationContext - whenever(fixture.hub.configureScope(argumentCaptor.capture())).thenAnswer { + whenever(fixture.scopes.configureScope(argumentCaptor.capture())).thenAnswer { argumentCaptor.value.run(scope) } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityCreated(activity, fixture.bundle) // once for the screen, and once for the tracing propagation context - verify(fixture.hub, times(2)).configureScope(any()) + verify(fixture.scopes, times(2)).configureScope(any()) val propagationContextAfterNewTrace = scope.propagationContext assertNotSame(propagationContextAtStart, propagationContextAfterNewTrace) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) sut.onActivityCreated(activity, fixture.bundle) // once for the screen, but not for the tracing propagation context - verify(fixture.hub).configureScope(any()) + verify(fixture.scopes).configureScope(any()) assertSame(propagationContextAfterNewTrace, scope.propagationContext) sut.onActivityDestroyed(activity) - clearInvocations(fixture.hub) + clearInvocations(fixture.scopes) sut.onActivityCreated(activity, fixture.bundle) // once for the screen, and once for the tracing propagation context - verify(fixture.hub, times(2)).configureScope(any()) + verify(fixture.scopes, times(2)).configureScope(any()) assertNotSame(propagationContextAfterNewTrace, scope.propagationContext) } @@ -1368,7 +1368,7 @@ class ActivityLifecycleIntegrationTest { fun `when transaction is finished, sets frame metrics`() { val sut = fixture.getSut() fixture.options.tracesSampleRate = 1.0 - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) val activity = mock() sut.onActivityCreated(activity, fixture.bundle) @@ -1384,7 +1384,7 @@ class ActivityLifecycleIntegrationTest { fixture.options.tracesSampleRate = 1.0 fixture.options.dateProvider = SentryDateProvider { now } - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) // usually done by SentryPerformanceProvider val startDate = SentryNanotimeDate(Date(5678), 910) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt index 192565f101..82d0543833 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt @@ -1,9 +1,11 @@ package io.sentry.android.core import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.Hub import io.sentry.ILogger +import io.sentry.IScope import io.sentry.IScopes +import io.sentry.Scope +import io.sentry.Scopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.test.DeferredExecutorService @@ -72,8 +74,8 @@ class EnvelopeFileObserverIntegrationTest { options.cacheDirPath = file.absolutePath options.addIntegration(integrationMock) options.setSerializer(mock()) -// val expected = HubAdapter.getInstance() - val scopes = Hub(options) + val globalScope = Scope(options) + val scopes = Scopes(mock(), mock(), globalScope, "test") // verify(integrationMock).register(expected, options) scopes.close() verify(integrationMock).close() diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt index b34e79991f..e64cbe227e 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt @@ -5,9 +5,9 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.Hub import io.sentry.IScope import io.sentry.Scope +import io.sentry.Scopes import io.sentry.Sentry import io.sentry.SentryEnvelope import io.sentry.SentryEnvelopeHeader @@ -82,7 +82,7 @@ class InternalSentrySdkTest { fun captureEnvelopeWithEvent(event: SentryEvent = SentryEvent()) { // create an envelope with session data - val options = Sentry.getCurrentHub().options + val options = Sentry.getCurrentScopes().options val eventId = SentryId() val header = SentryEnvelopeHeader(eventId) val eventItem = SentryEnvelopeItem.fromEvent(options.serializer, event) @@ -110,20 +110,19 @@ class InternalSentrySdkTest { } @Test - fun `current scope returns null when hub is no-op`() { - Sentry.getCurrentHub().close() + fun `current scope returns null when scopes is no-op`() { + Sentry.getCurrentScopes().close() val scope = InternalSentrySdk.getCurrentScope() assertNull(scope) } @Test - fun `current scope returns obj when hub is active`() { + fun `current scope returns obj when scopes is active`() { + val options = SentryOptions().apply { + dsn = "https://key@uri/1234567" + } Sentry.setCurrentScopes( - Hub( - SentryOptions().apply { - dsn = "https://key@uri/1234567" - } - ) + Scopes(Scope(options), Scope(options), Scope(options), "test") ) val scope = InternalSentrySdk.getCurrentScope() assertNotNull(scope) @@ -131,13 +130,13 @@ class InternalSentrySdkTest { @Test fun `current scope returns a copy of the scope`() { + val options = SentryOptions().apply { + dsn = "https://key@uri/1234567" + } Sentry.setCurrentScopes( - Hub( - SentryOptions().apply { - dsn = "https://key@uri/1234567" - } - ) + Scopes(Scope(options), Scope(options), Scope(options), "test") ) + // TODO [HSM] add breadcrumbs to all scopes and assert they are there Sentry.addBreadcrumb("test") // when the clone is modified diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt index cd0f8ed8c0..3ba130c0c9 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt @@ -330,7 +330,7 @@ class SentryAndroidTest { } var session: Session? = null - Sentry.getCurrentHub().configureScope { scope -> + Sentry.getCurrentScopes().configureScope { scope -> session = scope.session } callback(session) @@ -342,7 +342,7 @@ class SentryAndroidTest { fixture.initSut { options -> options.isEnableAutoSessionTracking = false } - Sentry.getCurrentHub().withScope { scope -> + Sentry.getCurrentScopes().withScope { scope -> assertNull(scope.session) } } @@ -378,7 +378,7 @@ class SentryAndroidTest { it.release = "io.sentry.sample@1.1.0+220" it.environment = "debug" // this is necessary to delay the AnrV2Integration processing to execute the configure - // scope block below (otherwise it won't be possible as hub is no-op before .init) + // scope block below (otherwise it won't be possible as scopes is no-op before .init) it.executorService.submit { Sentry.configureScope { scope -> // make sure the scope values changed to test that we're still using previously diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/UserInteractionIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/UserInteractionIntegrationTest.kt index d43dfe1419..379e3db353 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/UserInteractionIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/UserInteractionIntegrationTest.kt @@ -7,7 +7,7 @@ import android.content.res.Resources import android.util.DisplayMetrics import android.view.Window import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.Hub +import io.sentry.Scopes import io.sentry.android.core.internal.gestures.NoOpWindowCallback import io.sentry.android.core.internal.gestures.SentryWindowCallback import org.junit.runner.RunWith @@ -26,7 +26,7 @@ class UserInteractionIntegrationTest { private class Fixture { val application = mock() - val hub = mock() + val scopes = mock() val options = SentryAndroidOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -39,7 +39,7 @@ class UserInteractionIntegrationTest { isAndroidXAvailable: Boolean = true ): UserInteractionIntegration { whenever(loadClass.isClassAvailable(any(), anyOrNull())).thenReturn(isAndroidXAvailable) - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) whenever(window.callback).thenReturn(callback) whenever(activity.window).thenReturn(window) @@ -65,7 +65,7 @@ class UserInteractionIntegrationTest { @Test fun `when user interaction breadcrumb is enabled registers a callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application).registerActivityLifecycleCallbacks(any()) } @@ -75,7 +75,7 @@ class UserInteractionIntegrationTest { val sut = fixture.getSut() fixture.options.isEnableUserInteractionBreadcrumbs = false - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application, never()).registerActivityLifecycleCallbacks(any()) } @@ -83,7 +83,7 @@ class UserInteractionIntegrationTest { @Test fun `when UserInteractionIntegration is closed unregisters the callback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.close() @@ -94,7 +94,7 @@ class UserInteractionIntegrationTest { fun `when androidx is unavailable doesn't register a callback`() { val sut = fixture.getSut(isAndroidXAvailable = false) - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) verify(fixture.application, never()).registerActivityLifecycleCallbacks(any()) } @@ -102,7 +102,7 @@ class UserInteractionIntegrationTest { @Test fun `registers window callback on activity resumed`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityResumed(fixture.activity) @@ -114,7 +114,7 @@ class UserInteractionIntegrationTest { @Test fun `when no original callback delegates to NoOpWindowCallback`() { val sut = fixture.getSut() - sut.register(fixture.hub, fixture.options) + sut.register(fixture.scopes, fixture.options) sut.onActivityResumed(fixture.activity) diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt index 087a1dfa72..6309155929 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationAnotherTest.kt @@ -28,7 +28,6 @@ import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLScalarType import graphql.schema.GraphQLSchema import io.sentry.Breadcrumb -import io.sentry.HubScopesWrapper import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions @@ -358,7 +357,6 @@ class SentryInstrumentationAnotherTest { } fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } diff --git a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt index 2bb46f79fc..7128b839e3 100644 --- a/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt +++ b/sentry-graphql/src/test/kotlin/io/sentry/graphql/SentryInstrumentationTest.kt @@ -18,7 +18,6 @@ import graphql.schema.GraphQLScalarType import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaGenerator import graphql.schema.idl.SchemaParser -import io.sentry.HubScopesWrapper import io.sentry.IScopes import io.sentry.Sentry import io.sentry.SentryOptions @@ -221,7 +220,6 @@ class SentryInstrumentationTest { } fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) closure.invoke() } diff --git a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt index a7004deb35..64e3f48441 100644 --- a/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt +++ b/sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt @@ -75,7 +75,7 @@ class ProfilingActivity : AppCompatActivity() { private fun finishTransactionAndPrintResults(t: ITransaction) { t.finish() profileFinished = true - val profilesDirPath = Sentry.getCurrentHub().options.profilingTracesDirPath + val profilesDirPath = Sentry.getCurrentScopes().options.profilingTracesDirPath if (profilesDirPath == null) { Toast.makeText(this, R.string.profiling_no_dir_set, Toast.LENGTH_SHORT).show() return @@ -84,7 +84,7 @@ class ProfilingActivity : AppCompatActivity() { // We have concurrent profiling now. We have to wait for all transactions to finish (e.g. button click) // before reading the profile, otherwise it's empty and a crash occurs if (Sentry.getSpan() != null) { - val timeout = Sentry.getCurrentHub().options.idleTimeout ?: 0 + val timeout = Sentry.getCurrentScopes().options.idleTimeout ?: 0 val duration = (getProfileDuration() * 1000).toLong() Thread.sleep((timeout - duration).coerceAtLeast(0)) } diff --git a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryProperties.java b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryProperties.java index 7b3469d7f1..80ea79932c 100644 --- a/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryProperties.java +++ b/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryProperties.java @@ -163,7 +163,8 @@ public void setLoggers(final @NotNull List loggers) { @Open public static class Reactive { /** - * Enable/Disable usage of {@link io.micrometer.context.ThreadLocalAccessor} for Hub propagation + * Enable/Disable usage of {@link io.micrometer.context.ThreadLocalAccessor} for Scopes + * propagation */ private boolean threadLocalAccessorEnabled = true; diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java index 5995728785..86e17f27c3 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/AbstractSentryWebFilter.java @@ -44,17 +44,18 @@ public AbstractSentryWebFilter(final @NotNull IScopes scopes) { } protected @Nullable ITransaction maybeStartTransaction( - final @NotNull IScopes requestHub, final @NotNull ServerHttpRequest request) { - if (requestHub.isEnabled()) { + final @NotNull IScopes requestScopes, final @NotNull ServerHttpRequest request) { + if (requestScopes.isEnabled()) { final @NotNull HttpHeaders headers = request.getHeaders(); final @Nullable String sentryTraceHeader = headers.getFirst(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeaders = headers.get(BaggageHeader.BAGGAGE_HEADER); final @Nullable TransactionContext transactionContext = - requestHub.continueTrace(sentryTraceHeader, baggageHeaders); + requestScopes.continueTrace(sentryTraceHeader, baggageHeaders); - if (requestHub.getOptions().isTracingEnabled() && shouldTraceRequest(requestHub, request)) { - return startTransaction(requestHub, request, transactionContext); + if (requestScopes.getOptions().isTracingEnabled() + && shouldTraceRequest(requestScopes, request)) { + return startTransaction(requestScopes, request, transactionContext); } } @@ -63,7 +64,7 @@ public AbstractSentryWebFilter(final @NotNull IScopes scopes) { protected void doFinally( final @NotNull ServerWebExchange serverWebExchange, - final @NotNull IScopes requestHub, + final @NotNull IScopes requestScopes, final @Nullable ITransaction transaction) { if (transaction != null) { finishTransaction(serverWebExchange, transaction); @@ -72,9 +73,9 @@ protected void doFinally( } protected void doFirst( - final @NotNull ServerWebExchange serverWebExchange, final @NotNull IScopes requestHub) { - if (requestHub.isEnabled()) { - serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); + final @NotNull ServerWebExchange serverWebExchange, final @NotNull IScopes requestScopes) { + if (requestScopes.isEnabled()) { + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestScopes); final ServerHttpRequest request = serverWebExchange.getRequest(); final ServerHttpResponse response = serverWebExchange.getResponse(); @@ -83,8 +84,8 @@ protected void doFirst( hint.set(WEBFLUX_FILTER_RESPONSE, response); final String methodName = request.getMethod() != null ? request.getMethod().name() : "unknown"; - requestHub.addBreadcrumb(Breadcrumb.http(request.getURI().toString(), methodName), hint); - requestHub.configureScope( + requestScopes.addBreadcrumb(Breadcrumb.http(request.getURI().toString(), methodName), hint); + requestScopes.configureScope( scope -> scope.setRequest(sentryRequestResolver.resolveSentryRequest(request))); } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt index 44f4925c2d..76e4e0e2b6 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,7 +2,6 @@ package io.sentry.spring.jakarta.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubScopesWrapper import io.sentry.ILogger import io.sentry.IScopes import io.sentry.PropagationContext @@ -87,7 +86,6 @@ class SentryWebFluxTracingFilterTest { private val fixture = Fixture() fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) it.`when` { Sentry.forkedRootScopes(any()) }.thenReturn(fixture.scopes) closure.invoke() diff --git a/sentry-spring/src/main/java/io/sentry/spring/EnableSentry.java b/sentry-spring/src/main/java/io/sentry/spring/EnableSentry.java index efb9ce529b..055afd3484 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/EnableSentry.java +++ b/sentry-spring/src/main/java/io/sentry/spring/EnableSentry.java @@ -12,7 +12,7 @@ * *

    *
  • creates bean of type {@link io.sentry.SentryOptions} - *
  • registers {@link io.sentry.IHub} for sending Sentry events + *
  • registers {@link io.sentry.IScopes} for sending Sentry events *
  • registers {@link SentryExceptionResolver} to send Sentry event for any uncaught exception * in Spring MVC flow. *
diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index 8c3b9ac1f4..2968eede43 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -9,19 +9,20 @@ import org.springframework.scheduling.annotation.Async; /** - * Sets a current hub on a thread running a {@link Runnable} given by parameter. Used to propagate - * the current {@link IScopes} on the thread executing async task - like MVC controller methods - * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. + * Forks current scope for the thread running a {@link Runnable} given by parameter. Used to + * propagate the current {@link IScopes} on the thread executing async task - like MVC controller + * methods returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override // TODO [HSM] should there also be a SentryIsolatedTaskDecorator or similar that uses // forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IScopes newHub = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); + final IScopes forkedScopes = + Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); return () -> { - try (final @NotNull ISentryLifecycleToken ignored = newHub.makeCurrent()) { + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { runnable.run(); } }; diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java index 50cdb8dc3a..b2519ba14e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTracingFilter.java @@ -59,7 +59,7 @@ public SentryTracingFilter() { public SentryTracingFilter( final @NotNull IScopes scopes, final @NotNull TransactionNameProvider transactionNameProvider) { - this.scopes = Objects.requireNonNull(scopes, "scopes is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); } diff --git a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java index e32ede6947..0601dcaeb3 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/webflux/SentryWebFilter.java @@ -50,23 +50,23 @@ public SentryWebFilter(final @NotNull IScopes scopes) { public Mono filter( final @NotNull ServerWebExchange serverWebExchange, final @NotNull WebFilterChain webFilterChain) { - @NotNull IScopes requestHub = Sentry.forkedRootScopes("request.webflux"); - if (!requestHub.isEnabled()) { + @NotNull IScopes requestScopes = Sentry.forkedRootScopes("request.webflux"); + if (!requestScopes.isEnabled()) { return webFilterChain.filter(serverWebExchange); } - final boolean isTracingEnabled = requestHub.getOptions().isTracingEnabled(); + final boolean isTracingEnabled = requestScopes.getOptions().isTracingEnabled(); final @NotNull ServerHttpRequest request = serverWebExchange.getRequest(); final @NotNull HttpHeaders headers = request.getHeaders(); final @Nullable String sentryTraceHeader = headers.getFirst(SentryTraceHeader.SENTRY_TRACE_HEADER); final @Nullable List baggageHeaders = headers.get(BaggageHeader.BAGGAGE_HEADER); final @Nullable TransactionContext transactionContext = - requestHub.continueTrace(sentryTraceHeader, baggageHeaders); + requestScopes.continueTrace(sentryTraceHeader, baggageHeaders); final @Nullable ITransaction transaction = - isTracingEnabled && shouldTraceRequest(requestHub, request) - ? startTransaction(requestHub, request, transactionContext) + isTracingEnabled && shouldTraceRequest(requestScopes, request) + ? startTransaction(requestScopes, request, transactionContext) : null; if (transaction != null) { @@ -91,8 +91,8 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) }) .doFirst( () -> { - serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestHub); - Sentry.setCurrentScopes(requestHub); + serverWebExchange.getAttributes().put(SENTRY_SCOPES_KEY, requestScopes); + Sentry.setCurrentScopes(requestScopes); final ServerHttpResponse response = serverWebExchange.getResponse(); final Hint hint = new Hint(); @@ -100,9 +100,9 @@ isTracingEnabled && shouldTraceRequest(requestHub, request) hint.set(WEBFLUX_FILTER_RESPONSE, response); final String methodName = request.getMethod() != null ? request.getMethod().name() : "unknown"; - requestHub.addBreadcrumb( + requestScopes.addBreadcrumb( Breadcrumb.http(request.getURI().toString(), methodName), hint); - requestHub.configureScope( + requestScopes.configureScope( scope -> scope.setRequest(sentryRequestResolver.resolveSentryRequest(request))); }); } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt index ff527abd7d..cb764f31e9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/webflux/SentryWebFluxTracingFilterTest.kt @@ -2,7 +2,6 @@ package io.sentry.spring.webflux import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.HubScopesWrapper import io.sentry.ILogger import io.sentry.IScopes import io.sentry.PropagationContext @@ -87,7 +86,6 @@ class SentryWebFluxTracingFilterTest { private val fixture = Fixture() fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { - it.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(fixture.scopes)) it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes) it.`when` { Sentry.forkedRootScopes(any()) }.thenReturn(fixture.scopes) closure.invoke() diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index d16d15b44b..d87ff71ccc 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -1979,6 +1979,7 @@ public final class io/sentry/ScopeType : java/lang/Enum { } public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/MetricsApi$IMetricsInterface { + public fun (Lio/sentry/IScope;Lio/sentry/IScope;Lio/sentry/IScope;Ljava/lang/String;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V public fun bindClient (Lio/sentry/ISentryClient;)V diff --git a/sentry/src/main/java/io/sentry/EnvelopeSender.java b/sentry/src/main/java/io/sentry/EnvelopeSender.java index 3a157f59d3..9f630fc260 100644 --- a/sentry/src/main/java/io/sentry/EnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/EnvelopeSender.java @@ -28,7 +28,7 @@ public EnvelopeSender( final long flushTimeoutMillis, final int maxQueueSize) { super(scopes, logger, flushTimeoutMillis, maxQueueSize); - this.scopes = Objects.requireNonNull(scopes, "Hub is required."); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); } diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index 2a50d8ca48..14e9a03e25 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -14,7 +14,7 @@ @Deprecated public final class HubScopesWrapper implements IHub { - private final IScopes scopes; + private final @NotNull IScopes scopes; public HubScopesWrapper(final @NotNull IScopes scopes) { this.scopes = scopes; diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 6eb82cca32..b639836ca1 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -13,7 +13,7 @@ public interface IScopes { /** - * Check if the Hub is enabled/active. + * Check if Sentry is enabled/active. * * @return true if its enabled or false otherwise. */ @@ -188,11 +188,11 @@ SentryId captureException( /** Ends the current session */ void endSession(); - /** Flushes out the queue for up to timeout seconds and disable the Hub. */ + /** Flushes out the queue for up to timeout seconds and disable the Scopes. */ void close(); /** - * Flushes out the queue for up to timeout seconds and disable the Hub. + * Flushes out the queue for up to timeout seconds and disable the Scopes. * * @param isRestarting if true, avoids locking the main thread when finishing the queue. */ @@ -320,7 +320,7 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { * SDK in globalHubMode (defaults to true on Android) {@link * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope * changes may be dropped when executed in parallel. Use {@link - * IHub#configureScope(ScopeCallback)} instead. + * IScopes#configureScope(ScopeCallback)} instead. * * @param callback the callback */ @@ -343,7 +343,7 @@ default void configureScope(@NotNull ScopeCallback callback) { void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback); /** - * Binds a different client to the hub + * Binds a different client to the scopes * * @param client the client. */ @@ -357,7 +357,7 @@ default void configureScope(@NotNull ScopeCallback callback) { boolean isHealthy(); /** - * Flushes events queued up, but keeps the Hub enabled. Not implemented yet. + * Flushes events queued up, but keeps the scopes enabled. Not implemented yet. * * @param timeoutMillis time in milliseconds */ @@ -540,9 +540,9 @@ ITransaction startTransaction( /** * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * <meta> HTML tags. Also see {@link IScopes#getBaggage()}. * - * @deprecated please use {@link IHub#getTraceparent()} instead. + * @deprecated please use {@link IScopes#getTraceparent()} instead. * @return sentry trace header or null */ @Deprecated @@ -610,7 +610,7 @@ void setSpanContext( void reportFullyDisplayed(); /** - * @deprecated See {@link IHub#reportFullyDisplayed()}. + * @deprecated See {@link IScopes#reportFullyDisplayed()}. */ @Deprecated default void reportFullDisplayed() { @@ -631,7 +631,7 @@ TransactionContext continueTrace( /** * Returns the "sentry-trace" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getBaggage()}. + * <meta> HTML tags. Also see {@link IScopes#getBaggage()}. * * @return sentry trace header or null */ @@ -640,7 +640,7 @@ TransactionContext continueTrace( /** * Returns the "baggage" header that allows tracing across services. Can also be used in - * <meta> HTML tags. Also see {@link IHub#getTraceparent()}. + * <meta> HTML tags. Also see {@link IScopes#getTraceparent()}. * * @return baggage header or null */ diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index 06969518b7..653c8de9f9 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -12,6 +12,9 @@ import org.jetbrains.annotations.Nullable; @Deprecated +/** + * @deprecated use {@link NoOpScopes} instead. + */ public final class NoOpHub implements IHub { private static final NoOpHub instance = new NoOpHub(); diff --git a/sentry/src/main/java/io/sentry/OutboxSender.java b/sentry/src/main/java/io/sentry/OutboxSender.java index f80bf030c8..4e223da03d 100644 --- a/sentry/src/main/java/io/sentry/OutboxSender.java +++ b/sentry/src/main/java/io/sentry/OutboxSender.java @@ -49,7 +49,7 @@ public OutboxSender( final long flushTimeoutMillis, final int maxQueueSize) { super(scopes, logger, flushTimeoutMillis, maxQueueSize); - this.scopes = Objects.requireNonNull(scopes, "Hub is required."); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required."); this.envelopeReader = Objects.requireNonNull(envelopeReader, "Envelope reader is required."); this.serializer = Objects.requireNonNull(serializer, "Serializer is required."); this.logger = Objects.requireNonNull(logger, "Logger is required."); diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index cbeb9ae88f..757a3870a8 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -26,6 +26,7 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull IScope scope; private final @NotNull IScope isolationScope; + private final @NotNull IScope globalScope; @SuppressWarnings("UnusedVariable") private final @Nullable Scopes parentScopes; @@ -38,21 +39,24 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull CombinedScopeView combinedScope; - Scopes( + public Scopes( final @NotNull IScope scope, final @NotNull IScope isolationScope, + final @NotNull IScope globalScope, final @NotNull String creator) { - this(scope, isolationScope, null, creator); + this(scope, isolationScope, globalScope, null, creator); } private Scopes( final @NotNull IScope scope, final @NotNull IScope isolationScope, + final @NotNull IScope globalScope, final @Nullable Scopes parentScopes, final @NotNull String creator) { - this.combinedScope = new CombinedScopeView(getGlobalScope(), isolationScope, scope); + this.combinedScope = new CombinedScopeView(globalScope, isolationScope, scope); this.scope = scope; this.isolationScope = isolationScope; + this.globalScope = globalScope; this.parentScopes = parentScopes; this.creator = creator; @@ -106,12 +110,12 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { @Override public @NotNull IScopes forkedScopes(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope.clone(), this, creator); + return new Scopes(scope.clone(), isolationScope.clone(), globalScope, this, creator); } @Override public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope, this, creator); + return new Scopes(scope.clone(), isolationScope, globalScope, this, creator); } @Override @@ -419,7 +423,7 @@ public void close(final boolean isRestarting) { // TODO: should we end session before closing client? getClient().close(isRestarting); } catch (Throwable e) { - getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e); + getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e); } isEnabled = false; } @@ -572,7 +576,7 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { @Override public @NotNull IScope getGlobalScope() { - return Sentry.getGlobalScope(); + return globalScope; } @Override @@ -712,7 +716,7 @@ public void flush(long timeoutMillis) { @SuppressWarnings("deprecation") public @NotNull IHub clone() { if (!isEnabled()) { - getOptions().getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); + getOptions().getLogger().log(SentryLevel.WARNING, "Disabled Scopes cloned."); } // TODO [HSM] should this fork isolation scope as well? return new HubScopesWrapper(forkedCurrentScope("scopes clone")); @@ -1054,7 +1058,7 @@ private static void validateOptions(final @NotNull SentryOptions options) { Objects.requireNonNull(options, "SentryOptions is required."); if (options.getDsn() == null || options.getDsn().isEmpty()) { throw new IllegalArgumentException( - "Hub requires a DSN to be instantiated. Considering using the NoOpHub if no DSN is available."); + "Scopes requires a DSN to be instantiated. Considering using the NoOpScopes if no DSN is available."); } } } diff --git a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java index 2160d1607e..8234affa05 100644 --- a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java +++ b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java @@ -67,7 +67,7 @@ public SendCachedEnvelopeFireAndForgetIntegration( @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { - this.scopes = Objects.requireNonNull(scopes, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); final String cachedDir = options.getCacheDirPath(); diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java index 155946b95a..11c254458a 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetEnvelopeSender.java @@ -22,7 +22,7 @@ public SendFireAndForgetEnvelopeSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( final @NotNull IScopes scopes, final @NotNull SentryOptions options) { - Objects.requireNonNull(scopes, "Hub is required"); + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); diff --git a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java index e7913b5b2f..b14b3b6921 100644 --- a/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java +++ b/sentry/src/main/java/io/sentry/SendFireAndForgetOutboxSender.java @@ -22,7 +22,7 @@ public SendFireAndForgetOutboxSender( @Override public @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget create( final @NotNull IScopes scopes, final @NotNull SentryOptions options) { - Objects.requireNonNull(scopes, "Hub is required"); + Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); final String dirPath = sendFireAndForgetDirPath.getDirPath(); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 38d033f28a..214a16362a 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -58,7 +58,7 @@ private Sentry() {} /** Default value for globalHubMode is false */ private static final boolean GLOBAL_HUB_DEFAULT_MODE = false; - /** whether to use a single (global) Hub as opposed to one per thread. */ + /** whether to use a single (global) Scopes as opposed to one per thread. */ private static volatile boolean globalHubMode = GLOBAL_HUB_DEFAULT_MODE; @ApiStatus.Internal @@ -104,7 +104,7 @@ private Sentry() {} /** * Returns a new Scopes which is cloned from the rootScopes. * - * @return the hub + * @return the forked scopes */ @ApiStatus.Internal public static @NotNull IScopes forkedRootScopes(final @NotNull String creator) { @@ -139,7 +139,7 @@ private Sentry() {} } /** - * Check if the current Hub is enabled/active. + * Check if Sentry is enabled/active. * * @return true if its enabled or false otherwise. */ @@ -276,7 +276,7 @@ private static synchronized void init( final IScope rootScope = new Scope(options); final IScope rootIsolationScope = new Scope(options); globalScope.bindClient(new SentryClient(options)); - rootScopes = new Scopes(rootScope, rootIsolationScope, "Sentry.init"); + rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init"); getScopesStorage().set(rootScopes); @@ -288,10 +288,10 @@ private static synchronized void init( options.setExecutorService(new SentryExecutorService()); } - // when integrations are registered on Hub ctor and async integrations are fired, + // when integrations are registered on Scopes ctor and async integrations are fired, // it might and actually happened that integrations called captureSomething - // and hub was still NoOp. - // Registering integrations here make sure that Hub is already created. + // and Scopes was still NoOp. + // Registering integrations here make sure that Scopes is already created. for (final Integration integration : options.getIntegrations()) { integration.register(ScopesAdapter.getInstance(), options); } @@ -872,7 +872,7 @@ public static void configureScope( } /** - * Binds a different client to the current hub + * Binds a different client to the current Scopes * * @param client the client. */ @@ -885,7 +885,7 @@ public static boolean isHealthy() { } /** - * Flushes events queued up to the current hub. Not implemented yet. + * Flushes events queued up to the current Scopes. Not implemented yet. * * @param timeoutMillis time in milliseconds */ @@ -1037,7 +1037,7 @@ public static void reportFullDisplayed() { reportFullyDisplayed(); } - /** the metrics API for the current hub */ + /** the metrics API for the current Scopes */ @NotNull @ApiStatus.Experimental public static MetricsApi metrics() { diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index 0523ce7cf0..34475d2f8c 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -315,7 +315,7 @@ public class SentryOptions { /** Maximum number of spans that can be atteched to single transaction. */ private int maxSpans = 1000; - /** Registers hook that flushes {@link Hub} when main thread shuts down. */ + /** Registers hook that flushes {@link Scopes} when main thread shuts down. */ private boolean enableShutdownHook = true; /** @@ -2496,7 +2496,7 @@ public interface BeforeEmitMetricCallback { /** * Creates SentryOptions instance without initializing any of the internal parts. * - *

Used by {@link NoOpHub}. + *

Used by {@link NoOpScopes}. * * @return SentryOptions */ diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index 5d15c35e17..16bf0ef7ec 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -12,16 +12,16 @@ *

  • {@link Supplier} * * - * that clones the Hub before execution and restores it afterwards. This prevents reused threads - * (e.g. from thread-pools) from getting an incorrect state. + * that forks the current scope before execution and restores it afterwards. This prevents reused + * threads (e.g. from thread-pools) from getting an incorrect state. */ public final class SentryWrapper { /** * Helper method to wrap {@link Callable} * - *

    Clones the Hub before execution and restores it afterwards. This prevents reused threads - * (e.g. from thread-pools) from getting an incorrect state. + *

    Forks the current scope before execution and restores it afterwards. This prevents reused + * threads (e.g. from thread-pools) from getting an incorrect state. * * @param callable - the {@link Callable} to be wrapped * @return the wrapped {@link Callable} @@ -51,8 +51,8 @@ public static Callable wrapCallableIsolated(final @NotNull Callable ca /** * Helper method to wrap {@link Supplier} * - *

    Clones the Hub before execution and restores it afterwards. This prevents reused threads - * (e.g. from thread-pools) from getting an incorrect state. + *

    Forks the current scope before execution and restores it afterwards. This prevents reused + * threads (e.g. from thread-pools) from getting an incorrect state. * * @param supplier - the {@link Supplier} to be wrapped * @return the wrapped {@link Supplier} diff --git a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java index d957a87ccf..c31d31aebb 100644 --- a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java +++ b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java @@ -10,7 +10,7 @@ import org.jetbrains.annotations.TestOnly; import org.jetbrains.annotations.VisibleForTesting; -/** Registers hook that flushes {@link Hub} when main thread shuts down. */ +/** Registers hook that flushes {@link Scopes} when main thread shuts down. */ public final class ShutdownHookIntegration implements Integration, Closeable { private final @NotNull Runtime runtime; diff --git a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java index 47ceaa084e..98844bd038 100644 --- a/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java +++ b/sentry/src/main/java/io/sentry/UncaughtExceptionHandlerIntegration.java @@ -54,7 +54,7 @@ public final void register(final @NotNull IScopes scopes, final @NotNull SentryO } registered = true; - this.scopes = Objects.requireNonNull(scopes, "Hub is required"); + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); this.options diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt index 7637e6c74e..38fc9875b0 100644 --- a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -26,12 +26,12 @@ class ScopesAdapterTest { Sentry.close() } - @Test fun `isEnabled calls Hub`() { + @Test fun `isEnabled calls Scopes`() { ScopesAdapter.getInstance().isEnabled verify(scopes).isEnabled } - @Test fun `captureEvent calls Hub`() { + @Test fun `captureEvent calls Scopes`() { val event = mock() val hint = mock() val scopeCallback = mock() @@ -42,7 +42,7 @@ class ScopesAdapterTest { verify(scopes).captureEvent(eq(event), eq(hint), eq(scopeCallback)) } - @Test fun `captureMessage calls Hub`() { + @Test fun `captureMessage calls Scopes`() { val scopeCallback = mock() val sentryLevel = mock() ScopesAdapter.getInstance().captureMessage("message", sentryLevel) @@ -52,14 +52,14 @@ class ScopesAdapterTest { verify(scopes).captureMessage(eq("message"), eq(sentryLevel), eq(scopeCallback)) } - @Test fun `captureEnvelope calls Hub`() { + @Test fun `captureEnvelope calls Scopes`() { val envelope = mock() val hint = mock() ScopesAdapter.getInstance().captureEnvelope(envelope, hint) verify(scopes).captureEnvelope(eq(envelope), eq(hint)) } - @Test fun `captureException calls Hub`() { + @Test fun `captureException calls Scopes`() { val throwable = mock() val hint = mock() val scopeCallback = mock() @@ -70,142 +70,142 @@ class ScopesAdapterTest { verify(scopes).captureException(eq(throwable), eq(hint), eq(scopeCallback)) } - @Test fun `captureUserFeedback calls Hub`() { + @Test fun `captureUserFeedback calls Scopes`() { val userFeedback = mock() ScopesAdapter.getInstance().captureUserFeedback(userFeedback) verify(scopes).captureUserFeedback(eq(userFeedback)) } - @Test fun `captureCheckIn calls Hub`() { + @Test fun `captureCheckIn calls Scopes`() { val checkIn = mock() ScopesAdapter.getInstance().captureCheckIn(checkIn) verify(scopes).captureCheckIn(eq(checkIn)) } - @Test fun `startSession calls Hub`() { + @Test fun `startSession calls Scopes`() { ScopesAdapter.getInstance().startSession() verify(scopes).startSession() } - @Test fun `endSession calls Hub`() { + @Test fun `endSession calls Scopes`() { ScopesAdapter.getInstance().endSession() verify(scopes).endSession() } - @Test fun `close calls Hub`() { + @Test fun `close calls Scopes`() { ScopesAdapter.getInstance().close() verify(scopes).close(false) } - @Test fun `close with isRestarting true calls Hub with isRestarting false`() { + @Test fun `close with isRestarting true calls Scopes with isRestarting false`() { ScopesAdapter.getInstance().close(true) verify(scopes).close(false) } - @Test fun `close with isRestarting false calls Hub with isRestarting false`() { + @Test fun `close with isRestarting false calls Scopes with isRestarting false`() { ScopesAdapter.getInstance().close(false) verify(scopes).close(false) } - @Test fun `addBreadcrumb calls Hub`() { + @Test fun `addBreadcrumb calls Scopes`() { val breadcrumb = mock() val hint = mock() ScopesAdapter.getInstance().addBreadcrumb(breadcrumb, hint) verify(scopes).addBreadcrumb(eq(breadcrumb), eq(hint)) } - @Test fun `setLevel calls Hub`() { + @Test fun `setLevel calls Scopes`() { val sentryLevel = mock() ScopesAdapter.getInstance().setLevel(sentryLevel) verify(scopes).setLevel(eq(sentryLevel)) } - @Test fun `setTransaction calls Hub`() { + @Test fun `setTransaction calls Scopes`() { ScopesAdapter.getInstance().setTransaction("transaction") verify(scopes).setTransaction(eq("transaction")) } - @Test fun `setUser calls Hub`() { + @Test fun `setUser calls Scopes`() { val user = mock() ScopesAdapter.getInstance().setUser(user) verify(scopes).setUser(eq(user)) } - @Test fun `setFingerprint calls Hub`() { + @Test fun `setFingerprint calls Scopes`() { val fingerprint = ArrayList() ScopesAdapter.getInstance().setFingerprint(fingerprint) verify(scopes).setFingerprint(eq(fingerprint)) } - @Test fun `clearBreadcrumbs calls Hub`() { + @Test fun `clearBreadcrumbs calls Scopes`() { ScopesAdapter.getInstance().clearBreadcrumbs() verify(scopes).clearBreadcrumbs() } - @Test fun `setTag calls Hub`() { + @Test fun `setTag calls Scopes`() { ScopesAdapter.getInstance().setTag("key", "value") verify(scopes).setTag(eq("key"), eq("value")) } - @Test fun `removeTag calls Hub`() { + @Test fun `removeTag calls Scopes`() { ScopesAdapter.getInstance().removeTag("key") verify(scopes).removeTag(eq("key")) } - @Test fun `setExtra calls Hub`() { + @Test fun `setExtra calls Scopes`() { ScopesAdapter.getInstance().setExtra("key", "value") verify(scopes).setExtra(eq("key"), eq("value")) } - @Test fun `removeExtra calls Hub`() { + @Test fun `removeExtra calls Scopes`() { ScopesAdapter.getInstance().removeExtra("key") verify(scopes).removeExtra(eq("key")) } - @Test fun `getLastEventId calls Hub`() { + @Test fun `getLastEventId calls Scopes`() { ScopesAdapter.getInstance().lastEventId verify(scopes).lastEventId } - @Test fun `pushScope calls Hub`() { + @Test fun `pushScope calls Scopes`() { ScopesAdapter.getInstance().pushScope() verify(scopes).pushScope() } - @Test fun `popScope calls Hub`() { + @Test fun `popScope calls Scopes`() { ScopesAdapter.getInstance().popScope() verify(scopes).popScope() } - @Test fun `withScope calls Hub`() { + @Test fun `withScope calls Scopes`() { val scopeCallback = mock() ScopesAdapter.getInstance().withScope(scopeCallback) verify(scopes).withScope(eq(scopeCallback)) } - @Test fun `configureScope calls Hub`() { + @Test fun `configureScope calls Scopes`() { val scopeCallback = mock() ScopesAdapter.getInstance().configureScope(scopeCallback) verify(scopes).configureScope(anyOrNull(), eq(scopeCallback)) } - @Test fun `bindClient calls Hub`() { + @Test fun `bindClient calls Scopes`() { val client = mock() ScopesAdapter.getInstance().bindClient(client) verify(scopes).bindClient(eq(client)) } - @Test fun `flush calls Hub`() { + @Test fun `flush calls Scopes`() { ScopesAdapter.getInstance().flush(1) verify(scopes).flush(eq(1)) } - @Test fun `clone calls Hub`() { + @Test fun `clone calls Scopes`() { ScopesAdapter.getInstance().clone() verify(scopes).clone() } - @Test fun `captureTransaction calls Hub`() { + @Test fun `captureTransaction calls Scopes`() { val transaction = mock() val traceContext = mock() val hint = mock() @@ -214,7 +214,7 @@ class ScopesAdapterTest { verify(scopes).captureTransaction(eq(transaction), eq(traceContext), eq(hint), eq(profilingTraceData)) } - @Test fun `startTransaction calls Hub`() { + @Test fun `startTransaction calls Scopes`() { val transactionContext = mock() val samplingContext = mock() val transactionOptions = mock() @@ -227,39 +227,39 @@ class ScopesAdapterTest { verify(scopes).startTransaction(eq(transactionContext), eq(transactionOptions)) } - @Test fun `traceHeaders calls Hub`() { + @Test fun `traceHeaders calls Scopes`() { ScopesAdapter.getInstance().traceHeaders() verify(scopes).traceHeaders() } - @Test fun `setSpanContext calls Hub`() { + @Test fun `setSpanContext calls Scopes`() { val throwable = mock() val span = mock() ScopesAdapter.getInstance().setSpanContext(throwable, span, "transactionName") verify(scopes).setSpanContext(eq(throwable), eq(span), eq("transactionName")) } - @Test fun `getSpan calls Hub`() { + @Test fun `getSpan calls Scopes`() { ScopesAdapter.getInstance().span verify(scopes).span } - @Test fun `getTransaction calls Hub`() { + @Test fun `getTransaction calls Scopes`() { ScopesAdapter.getInstance().transaction verify(scopes).transaction } - @Test fun `getOptions calls Hub`() { + @Test fun `getOptions calls Scopes`() { ScopesAdapter.getInstance().options verify(scopes).options } - @Test fun `isCrashedLastRun calls Hub`() { + @Test fun `isCrashedLastRun calls Scopes`() { ScopesAdapter.getInstance().isCrashedLastRun verify(scopes).isCrashedLastRun } - @Test fun `reportFullyDisplayed calls Hub`() { + @Test fun `reportFullyDisplayed calls Scopes`() { ScopesAdapter.getInstance().reportFullyDisplayed() verify(scopes).reportFullyDisplayed() } diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 77d443f909..b1316158b5 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -422,25 +422,25 @@ class SentryTest { assertNotNull(scopes) assertFalse(Sentry.getCurrentScopes().isNoOp) - val newMainHubClone = Sentry.forkedRootScopes("test") - newMainHubClone.addBreadcrumb("breadcrumbMainClone") + val forkedRootScopes = Sentry.forkedRootScopes("test") + forkedRootScopes.addBreadcrumb("breadcrumbMainClone") scopes.captureMessage("messageCurrent") - newMainHubClone.captureMessage("messageMainClone") + forkedRootScopes.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) val mainCloneEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageMainClone" } - val currentHubEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageCurrent" } + val currentScopesEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageCurrent" } assertNotNull(mainCloneEvent) assertNotNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) assertNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) assertNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) - assertNotNull(currentHubEvent) - assertNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) - assertNotNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) - assertNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) + assertNotNull(currentScopesEvent) + assertNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) + assertNotNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) + assertNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) } @Test @@ -473,25 +473,25 @@ class SentryTest { assertNotNull(scopes) assertFalse(scopes.isNoOp) - val newMainHubClone = Sentry.forkedRootScopes("test") - newMainHubClone.addBreadcrumb("breadcrumbMainClone") + val forkedRootScopes = Sentry.forkedRootScopes("test") + forkedRootScopes.addBreadcrumb("breadcrumbMainClone") scopes.captureMessage("messageCurrent") - newMainHubClone.captureMessage("messageMainClone") + forkedRootScopes.captureMessage("messageMainClone") assertEquals(2, capturedEvents.size) val mainCloneEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageMainClone" } - val currentHubEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageCurrent" } + val currentScopesEvent = capturedEvents.firstOrNull { it.message?.formatted == "messageCurrent" } assertNotNull(mainCloneEvent) assertNotNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) assertNotNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) assertNull(mainCloneEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) - assertNotNull(currentHubEvent) - assertNotNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) - assertNotNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) - assertNull(currentHubEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) + assertNotNull(currentScopesEvent) + assertNotNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbMainClone" }) + assertNotNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbCurrent" }) + assertNull(currentScopesEvent.breadcrumbs?.firstOrNull { it.message == "breadcrumbNoOp" }) } @Test diff --git a/sentry/src/test/java/io/sentry/SentryTracerTest.kt b/sentry/src/test/java/io/sentry/SentryTracerTest.kt index f92e90799c..ccd96a2543 100644 --- a/sentry/src/test/java/io/sentry/SentryTracerTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTracerTest.kt @@ -29,16 +29,16 @@ class SentryTracerTest { private class Fixture { val options = SentryOptions() - val hub: Hub + val scopes: Scopes val transactionPerformanceCollector: TransactionPerformanceCollector init { options.dsn = "https://key@sentry.io/proj" options.environment = "environment" options.release = "release@3.0.0" - hub = spy(Hub(options)) + scopes = spy(Scopes(Scope(options), Scope(options), Scope(options), "test")) transactionPerformanceCollector = spy(DefaultTransactionPerformanceCollector(options)) - hub.bindClient(mock()) + scopes.bindClient(mock()) } fun getSut( @@ -61,7 +61,7 @@ class SentryTracerTest { transactionOptions.deadlineTimeout = deadlineTimeout transactionOptions.isTrimEnd = trimEnd transactionOptions.transactionFinishedCallback = transactionFinishedCallback - return SentryTracer(TransactionContext("name", "op", samplingDecision), hub, transactionOptions, performanceCollector) + return SentryTracer(TransactionContext("name", "op", samplingDecision), scopes, transactionOptions, performanceCollector) } } @@ -150,7 +150,7 @@ class SentryTracerTest { fun `when transaction is finished, transaction is captured`() { val tracer = fixture.getSut() tracer.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(it.transaction, tracer.name) }, @@ -185,10 +185,10 @@ class SentryTracerTest { @Test fun `when transaction is finished, transaction is cleared from the scope`() { val tracer = fixture.getSut() - fixture.hub.configureScope { it.transaction = tracer } - assertNotNull(fixture.hub.span) + fixture.scopes.configureScope { it.transaction = tracer } + assertNotNull(fixture.scopes.span) tracer.finish() - assertNull(fixture.hub.span) + assertNull(fixture.scopes.span) } @Test @@ -197,7 +197,7 @@ class SentryTracerTest { val ex = RuntimeException() tracer.throwable = ex tracer.finish() - verify(fixture.hub).setSpanContext(ex, tracer.root, "name") + verify(fixture.scopes).setSpanContext(ex, tracer.root, "name") } @Test @@ -206,7 +206,7 @@ class SentryTracerTest { tracer.setTag("tag1", "val1") tracer.setTag("tag2", "val2") tracer.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(mapOf("tag1" to "val1", "tag2" to "val2"), it.tags) assertNotNull(it.contexts.trace) { @@ -226,7 +226,7 @@ class SentryTracerTest { val span = tracer.startChild("op2") span.spanContext.sampled = false tracer.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) assertEquals("op1", it.spans.first().op) @@ -253,7 +253,7 @@ class SentryTracerTest { tracer.setContext("otel", otelContext) tracer.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(otelContext, it.contexts["otel"]) }, @@ -404,8 +404,8 @@ class SentryTracerTest { transaction.finish(SpanStatus.UNKNOWN_ERROR) // call only once - verify(fixture.hub).setSpanContext(ex, transaction.root, "name") - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).setSpanContext(ex, transaction.root, "name") + verify(fixture.scopes).captureTransaction( check { assertNotNull(it.contexts.trace) { assertEquals(SpanStatus.OK, it.status) @@ -486,20 +486,20 @@ class SentryTracerTest { } @Test - fun `when waiting for children, finishing transaction does not call hub if all children are not finished`() { + fun `when waiting for children, finishing transaction does not call scopes if all children are not finished`() { val transaction = fixture.getSut(waitForChildren = true) transaction.startChild("op") transaction.finish() - verify(fixture.hub, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) } @Test - fun `when waiting for children, finishing transaction calls hub if all children are finished`() { + fun `when waiting for children, finishing transaction calls scopes if all children are finished`() { val transaction = fixture.getSut(waitForChildren = true) val child = transaction.startChild("op") child.finish() transaction.finish() - verify(fixture.hub).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test @@ -516,21 +516,21 @@ class SentryTracerTest { } @Test - fun `when waiting for children, hub is not called until transaction is finished`() { + fun `when waiting for children, scopes is not called until transaction is finished`() { val transaction = fixture.getSut(waitForChildren = true) val child = transaction.startChild("op") child.finish() - verify(fixture.hub, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) } @Test - fun `when waiting for children, finishing last child calls hub if transaction is already finished`() { + fun `when waiting for children, finishing last child calls scopes if transaction is already finished`() { val transaction = fixture.getSut(waitForChildren = true) val child = transaction.startChild("op") transaction.finish(SpanStatus.INVALID_ARGUMENT) - verify(fixture.hub, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) + verify(fixture.scopes, never()).captureTransaction(any(), any(), anyOrNull(), anyOrNull()) child.finish() - verify(fixture.hub, times(1)).captureTransaction( + verify(fixture.scopes, times(1)).captureTransaction( check { assertEquals(SpanStatus.INVALID_ARGUMENT, it.status) }, @@ -552,7 +552,7 @@ class SentryTracerTest { transaction.finish(SpanStatus.INVALID_ARGUMENT) - verify(fixture.hub, times(1)).captureTransaction( + verify(fixture.scopes, times(1)).captureTransaction( check { assertEquals(2, it.spans.size) // span status/timestamp is retained @@ -575,7 +575,7 @@ class SentryTracerTest { it.isTraceSampling = true it.isSendDefaultPii = true }) - fixture.hub.setUser( + fixture.scopes.setUser( User().apply { id = "user-id" others = mapOf("segment" to "pro") @@ -598,7 +598,7 @@ class SentryTracerTest { val transaction = fixture.getSut({ it.isTraceSampling = true }) - fixture.hub.setUser( + fixture.scopes.setUser( User().apply { id = "user-id" others = mapOf("segment" to "pro") @@ -622,7 +622,7 @@ class SentryTracerTest { it.isTraceSampling = true }) val traceBeforeUserSet = transaction.traceContext() - fixture.hub.setUser( + fixture.scopes.setUser( User().apply { id = "user-id" } @@ -652,7 +652,7 @@ class SentryTracerTest { it.isSendDefaultPii = true }) - fixture.hub.setUser( + fixture.scopes.setUser( User().apply { id = "userId12345" others = mapOf("segment" to "pro") @@ -682,7 +682,7 @@ class SentryTracerTest { it.release = "1.0.99-rc.7" }) - fixture.hub.setUser( + fixture.scopes.setUser( User().apply { id = "userId12345" others = mapOf("segment" to "pro") @@ -713,7 +713,7 @@ class SentryTracerTest { it.isSendDefaultPii = true }) - fixture.hub.setUser(null) + fixture.scopes.setUser(null) val header = transaction.toBaggageHeader(null) assertNotNull(header) { @@ -735,7 +735,7 @@ class SentryTracerTest { val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true)) transaction.setData("key", "val") transaction.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals("val", it.getExtra("key")) }, @@ -752,7 +752,7 @@ class SentryTracerTest { span.setData("key", "val") span.finish() transaction.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertNotNull(it.spans.first().data) { assertEquals("val", it["key"]) @@ -840,7 +840,7 @@ class SentryTracerTest { await.untilFalse(transaction.isFinishTimerRunning) - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), @@ -857,7 +857,7 @@ class SentryTracerTest { await.untilFalse(transaction.isFinishTimerRunning) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), @@ -916,7 +916,7 @@ class SentryTracerTest { await.untilFalse(transaction.isFinishTimerRunning) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(2, it.spans.size) assertEquals(transaction.root.finishDate, span2.finishDate) @@ -954,7 +954,7 @@ class SentryTracerTest { transaction.setMeasurement("days", 2, MeasurementUnit.Duration.DAY) transaction.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1.0f, it.measurements["metric1"]!!.value) assertEquals(null, it.measurements["metric1"]!!.unit) @@ -975,7 +975,7 @@ class SentryTracerTest { transaction.setMeasurement("metric1", 2, MeasurementUnit.Duration.DAY) transaction.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(2, it.measurements["metric1"]!!.value) assertEquals("day", it.measurements["metric1"]!!.unit) @@ -993,7 +993,7 @@ class SentryTracerTest { transaction.setMeasurementFromChild("metric1", 2, MeasurementUnit.Duration.DAY) transaction.finish() - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1.0f, it.measurements["metric1"]!!.value) assertNull(it.measurements["metric1"]!!.unit) @@ -1063,7 +1063,7 @@ class SentryTracerTest { assertTrue(span.isFinished) // and the transaction should be captured - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) assertEquals(transaction.root.finishDate!!.nanoTimestamp(), span.finishDate!!.nanoTimestamp()) @@ -1093,7 +1093,7 @@ class SentryTracerTest { assertTrue(span.isFinished) // and the transaction should be captured - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(1, it.spans.size) assertEquals(transactionFinishDate, span.finishDate) @@ -1134,7 +1134,7 @@ class SentryTracerTest { assertEquals(expectedParentStartDate, parentSpan.startDate) assertEquals(expectedParentEndDate, parentSpan.finishDate) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(3, it.spans.size) }, @@ -1174,7 +1174,7 @@ class SentryTracerTest { assertEquals(expectedParentStartDate, parentSpan.startDate) assertEquals(expectedParentEndDate, parentSpan.finishDate) - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(3, it.spans.size) }, @@ -1265,7 +1265,7 @@ class SentryTracerTest { assertEquals(transaction.finishDate, span1.finishDate) // and the transaction should be captured with both spans - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(2, it.spans.size) }, @@ -1288,7 +1288,7 @@ class SentryTracerTest { transaction.forceFinish(SpanStatus.ABORTED, false, null) // then a transaction should be captured with 0 spans - verify(fixture.hub).captureTransaction( + verify(fixture.scopes).captureTransaction( check { assertEquals(0, it.spans.size) }, @@ -1311,7 +1311,7 @@ class SentryTracerTest { transaction.forceFinish(SpanStatus.ABORTED, true, null) // then the transaction should be captured with 0 spans - verify(fixture.hub, never()).captureTransaction( + verify(fixture.scopes, never()).captureTransaction( anyOrNull(), anyOrNull(), anyOrNull(), @@ -1335,7 +1335,7 @@ class SentryTracerTest { tracer.scheduleFinish() assertTrue(tracer.isFinished) - verify(fixture.hub).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) + verify(fixture.scopes).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull()) } @Test diff --git a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt index a3511450f0..a830446428 100644 --- a/sentry/src/test/java/io/sentry/SentryWrapperTest.kt +++ b/sentry/src/test/java/io/sentry/SentryWrapperTest.kt @@ -27,7 +27,7 @@ class SentryWrapperTest { } @Test - fun `hub is reset to its state within the thread after supply is done`() { + fun `scopes are reset to its state within the thread after supply is done`() { Sentry.init { it.dsn = dsn it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> @@ -35,20 +35,20 @@ class SentryWrapperTest { } } - val mainHub = Sentry.getCurrentScopes() - val threadedHub = mainHub.forkedCurrentScope("test") + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = mainScopes.forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = CompletableFuture.supplyAsync( SentryWrapper.wrapSupplierIsolated { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) "Result 1" }, executor @@ -57,8 +57,8 @@ class SentryWrapperTest { callableFuture.join() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } @@ -164,25 +164,25 @@ class SentryWrapperTest { } @Test - fun `hub is reset to its state within the thread after callable is done`() { + fun `scopes are reset to its state within the thread after callable is done`() { Sentry.init { it.dsn = dsn } - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( SentryWrapper.wrapCallable { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) "Result 1" } ) @@ -190,8 +190,8 @@ class SentryWrapperTest { callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } @@ -204,20 +204,20 @@ class SentryWrapperTest { } } - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = CompletableFuture.supplyAsync( SentryWrapper.wrapSupplierIsolated { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) "Result 1" }, executor @@ -226,8 +226,8 @@ class SentryWrapperTest { callableFuture.join() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } @@ -338,20 +338,20 @@ class SentryWrapperTest { it.dsn = dsn } - val mainHub = Sentry.getCurrentScopes() - val threadedHub = Sentry.getCurrentScopes().forkedCurrentScope("test") + val mainScopes = Sentry.getCurrentScopes() + val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test") executor.submit { - Sentry.setCurrentScopes(threadedHub) + Sentry.setCurrentScopes(threadedScopes) }.get() - assertEquals(mainHub, Sentry.getCurrentScopes()) + assertEquals(mainScopes, Sentry.getCurrentScopes()) val callableFuture = executor.submit( SentryWrapper.wrapCallableIsolated { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertNotEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertNotEquals(threadedScopes, Sentry.getCurrentScopes()) "Result 1" } ) @@ -359,8 +359,8 @@ class SentryWrapperTest { callableFuture.get() executor.submit { - assertNotEquals(mainHub, Sentry.getCurrentScopes()) - assertEquals(threadedHub, Sentry.getCurrentScopes()) + assertNotEquals(mainScopes, Sentry.getCurrentScopes()) + assertEquals(threadedScopes, Sentry.getCurrentScopes()) }.get() } } diff --git a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt index ec366e1901..51e6d32914 100644 --- a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt @@ -105,7 +105,7 @@ class UncaughtExceptionHandlerIntegrationTest { options.addIntegration(integrationMock) options.cacheDirPath = fixture.file.absolutePath options.setSerializer(mock()) - val scopes = Hub(options) + val scopes = Scopes(Scope(options), Scope(options), Scope(options), "test") scopes.close() verify(integrationMock).close() } diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index 7058083ac9..889459dd35 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -1,7 +1,6 @@ package io.sentry.util import io.sentry.CheckInStatus -import io.sentry.HubScopesWrapper import io.sentry.IScopes import io.sentry.ISentryLifecycleToken import io.sentry.MonitorConfig @@ -61,7 +60,6 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) sentry.`when` { Sentry.pushIsolationScope() }.then { scopes.pushIsolationScope() lifecycleToken @@ -98,7 +96,6 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) sentry.`when` { Sentry.pushIsolationScope() }.then { scopes.pushIsolationScope() lifecycleToken @@ -139,7 +136,6 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) sentry.`when` { Sentry.pushIsolationScope() }.then { scopes.pushIsolationScope() lifecycleToken @@ -178,7 +174,6 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) sentry.`when` { Sentry.pushIsolationScope() }.then { scopes.pushIsolationScope() lifecycleToken @@ -219,7 +214,6 @@ class CheckInUtilsTest { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { @@ -247,7 +241,6 @@ class CheckInUtilsTest { Mockito.mockStatic(Sentry::class.java).use { sentry -> val scopes = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.getCurrentHub() }.thenReturn(HubScopesWrapper(scopes)) whenever(scopes.options).thenReturn( SentryOptions().apply { cron = SentryOptions.Cron().apply { From 61c9d4a894bb24f90da06ca83799720741504492 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 18 Apr 2024 12:14:17 +0200 Subject: [PATCH 39/57] Implement ScopesTest --- sentry/src/main/java/io/sentry/Scopes.java | 1 + sentry/src/test/java/io/sentry/ScopesTest.kt | 2186 ++++++++++++++++++ 2 files changed, 2187 insertions(+) create mode 100644 sentry/src/test/java/io/sentry/ScopesTest.kt diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 757a3870a8..538c192304 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -421,6 +421,7 @@ public void close(final boolean isRestarting) { } // TODO: should we end session before closing client? + // TODO [HSM] should we go through all clients (global, isolation, current) and close them? getClient().close(isRestarting); } catch (Throwable e) { getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e); diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt new file mode 100644 index 0000000000..1b886aea5b --- /dev/null +++ b/sentry/src/test/java/io/sentry/ScopesTest.kt @@ -0,0 +1,2186 @@ +package io.sentry + +import io.sentry.backpressure.IBackpressureMonitor +import io.sentry.cache.EnvelopeCache +import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport +import io.sentry.clientreport.DiscardReason +import io.sentry.clientreport.DiscardedEvent +import io.sentry.hints.SessionEndHint +import io.sentry.hints.SessionStartHint +import io.sentry.protocol.SentryId +import io.sentry.protocol.SentryTransaction +import io.sentry.protocol.User +import io.sentry.test.DeferredExecutorService +import io.sentry.test.callMethod +import io.sentry.util.HintUtils +import io.sentry.util.StringUtils +import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull +import org.mockito.kotlin.argWhere +import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.check +import org.mockito.kotlin.doAnswer +import org.mockito.kotlin.doThrow +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.spy +import org.mockito.kotlin.times +import org.mockito.kotlin.verify +import org.mockito.kotlin.verifyNoMoreInteractions +import org.mockito.kotlin.whenever +import java.io.File +import java.nio.file.Files +import java.util.Queue +import java.util.UUID +import java.util.concurrent.atomic.AtomicReference +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.test.fail + +class ScopesTest { + + private lateinit var file: File + private lateinit var profilingTraceFile: File + + @BeforeTest + fun `set up`() { + file = Files.createTempDirectory("sentry-disk-cache-test").toAbsolutePath().toFile() + profilingTraceFile = Files.createTempFile("trace", ".trace").toFile() + profilingTraceFile.writeText("sampledProfile") + SentryCrashLastRunState.getInstance().reset() + } + + @AfterTest + fun shutdown() { + file.deleteRecursively() + profilingTraceFile.delete() + Sentry.close() + SentryCrashLastRunState.getInstance().reset() + } + + private fun createScopes(options: SentryOptions): Scopes { + return Scopes(Scope(options), Scope(options), Scope(options), "test") + } + + @Test + fun `when no dsn available, ctor throws illegal arg`() { + val ex = assertFailsWith { createScopes(SentryOptions()) } + assertEquals("Scopes requires a DSN to be instantiated. Considering using the NoOpScopes if no DSN is available.", ex.message) + } + + @Test + fun `when isolation scope is forked, integrations are not registered`() { + val integrationMock = mock() + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + options.addIntegration(integrationMock) +// val expected = HubAdapter.getInstance() + val scopes = createScopes(options) +// verify(integrationMock).register(expected, options) + scopes.forkedScopes("test") + verifyNoMoreInteractions(integrationMock) + } + + @Test + fun `when current scope is forked, integrations are not registered`() { + val integrationMock = mock() + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + options.addIntegration(integrationMock) +// val expected = HubAdapter.getInstance() + val scopes = createScopes(options) +// verify(integrationMock).register(expected, options) + scopes.forkedCurrentScope("test") + verifyNoMoreInteractions(integrationMock) + } + + @Test + fun `when isolation scope is forked, scope changes are isolated`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val scopes = createScopes(options) + var firstScope: IScope? = null + scopes.configureScope { + firstScope = it + it.setTag("scopes", "a") + } + var cloneScope: IScope? = null + val clone = scopes.forkedScopes("test") + clone.configureScope { + cloneScope = it + it.setTag("scopes", "b") + } + assertEquals("a", firstScope!!.tags["scopes"]) + assertEquals("b", cloneScope!!.tags["scopes"]) + } + + @Test + fun `when current scope is forked, scope changes are not isolated`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val scopes = createScopes(options) + var firstScope: IScope? = null + scopes.configureScope { + firstScope = it + it.setTag("scopes", "a") + } + var cloneScope: IScope? = null + val clone = scopes.forkedCurrentScope("test") + clone.configureScope { + cloneScope = it + it.setTag("scopes", "b") + } + assertEquals("b", firstScope!!.tags["scopes"]) + assertEquals("b", cloneScope!!.tags["scopes"]) + } + + @Test + fun `when scopes is initialized, breadcrumbs are capped as per options`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.maxBreadcrumbs = 5 + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + (1..10).forEach { _ -> sut.addBreadcrumb(Breadcrumb(), null) } + var actual = 0 + sut.configureScope { + actual = it.breadcrumbs.size + } + assertEquals(options.maxBreadcrumbs, actual) + } + + @Test + fun `when beforeBreadcrumb returns null, crumb is dropped`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { _: Breadcrumb, _: Any? -> null } + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + sut.addBreadcrumb(Breadcrumb(), null) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + assertEquals(0, breadcrumbs!!.size) + } + + @Test + fun `when beforeBreadcrumb modifies crumb, crumb is stored modified`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + val expected = "expected" + options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb: Breadcrumb, _: Any? -> breadcrumb.message = expected; breadcrumb; } + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val crumb = Breadcrumb() + crumb.message = "original" + sut.addBreadcrumb(crumb) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + assertEquals(expected, breadcrumbs!!.first().message) + } + + @Test + fun `when beforeBreadcrumb is null, crumb is stored`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.beforeBreadcrumb = null + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val expected = Breadcrumb() + sut.addBreadcrumb(expected) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + assertEquals(expected, breadcrumbs!!.single()) + } + + @Test + fun `when beforeSend throws an exception, breadcrumb adds an entry to the data field with exception message`() { + val exception = Exception("test") + + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { _: Breadcrumb, _: Any? -> throw exception } + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + + val actual = Breadcrumb() + sut.addBreadcrumb(actual) + + assertEquals("test", actual.data["sentry:message"]) + } + + @Test + fun `when initialized, lastEventId is empty`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + + @Test + fun `when addBreadcrumb is called on disabled client, no-op`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + sut.close() + sut.addBreadcrumb(Breadcrumb()) + assertTrue(breadcrumbs!!.isEmpty()) + } + + @Test + fun `when addBreadcrumb is called with message and category, breadcrumb object has values`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + sut.addBreadcrumb("message", "category") + assertEquals("message", breadcrumbs!!.single().message) + assertEquals("category", breadcrumbs!!.single().category) + } + + @Test + fun `when addBreadcrumb is called with message, breadcrumb object has value`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + var breadcrumbs: Queue? = null + sut.configureScope { breadcrumbs = it.breadcrumbs } + sut.addBreadcrumb("message", "category") + assertEquals("message", breadcrumbs!!.single().message) + assertEquals("category", breadcrumbs!!.single().category) + } + + @Test + fun `when flush is called on disabled client, no-op`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.flush(1000) + verify(mockClient, never()).flush(1000) + } + + @Test + fun `when flush is called, client flush gets called`() { + val (sut, mockClient) = getEnabledScopes() + + sut.flush(1000) + verify(mockClient).flush(1000) + } + + //region captureEvent tests + @Test + fun `when captureEvent is called and event is null, lastEventId is empty`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + sut.callMethod("captureEvent", SentryEvent::class.java, null) + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + + @Test + fun `when captureEvent is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.captureEvent(SentryEvent()) + verify(mockClient, never()).captureEvent(any(), any()) + } + + @Test + fun `when captureEvent is called with a valid argument, captureEvent on the client should be called`() { + val (sut, mockClient) = getEnabledScopes() + + val event = SentryEvent() + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called on disabled scopes, lastEventId does not get overwritten`() { + val (sut, mockClient) = getEnabledScopes() + whenever(mockClient.captureEvent(any(), any(), anyOrNull())).thenReturn(SentryId(UUID.randomUUID())) + val event = SentryEvent() + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + val lastEventId = sut.lastEventId + sut.close() + sut.captureEvent(event, hints) + assertEquals(lastEventId, sut.lastEventId) + } + + @Test + fun `when captureEvent is called and session tracking is disabled, it should not capture a session`() { + val (sut, mockClient) = getEnabledScopes() + + val event = SentryEvent() + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + verify(mockClient, never()).captureSession(any(), any()) + } + + @Test + fun `when captureEvent is called but no session started, it should not capture a session`() { + val (sut, mockClient) = getEnabledScopes() + + val event = SentryEvent() + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + verify(mockClient, never()).captureSession(any(), any()) + } + + @Test + fun `when captureEvent is called and event has exception which has been previously attached with span context, sets span context to the event`() { + val (sut, mockClient) = getEnabledScopes() + val exception = RuntimeException() + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(exception, span, "tx-name") + + val event = SentryEvent(exception) + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + assertEquals(span.spanContext, event.contexts.trace) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called and event has exception which root cause has been previously attached with span context, sets span context to the event`() { + val (sut, mockClient) = getEnabledScopes() + val rootCause = RuntimeException() + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(rootCause, span, "tx-name") + + val event = SentryEvent(RuntimeException(rootCause)) + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + assertEquals(span.spanContext, event.contexts.trace) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called and event has exception which non-root cause has been previously attached with span context, sets span context to the event`() { + val (sut, mockClient) = getEnabledScopes() + val rootCause = RuntimeException() + val exceptionAssignedToSpan = RuntimeException(rootCause) + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(exceptionAssignedToSpan, span, "tx-name") + + val event = SentryEvent(RuntimeException(exceptionAssignedToSpan)) + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + assertEquals(span.spanContext, event.contexts.trace) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called and event has exception which has been previously attached with span context and trace context already set, does not set new span context to the event`() { + val (sut, mockClient) = getEnabledScopes() + val exception = RuntimeException() + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(exception, span, "tx-name") + + val event = SentryEvent(exception) + val originalSpanContext = SpanContext("op") + event.contexts.trace = originalSpanContext + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + assertEquals(originalSpanContext, event.contexts.trace) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called and event has exception which has not been previously attached with span context, does not set new span context to the event`() { + val (sut, mockClient) = getEnabledScopes() + + val event = SentryEvent(RuntimeException()) + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureEvent(event, hints) + assertNull(event.contexts.trace) + verify(mockClient).captureEvent(eq(event), any(), eq(hints)) + } + + @Test + fun `when captureEvent is called with a ScopeCallback then the modified scope is sent to the client`() { + val (sut, mockClient) = getEnabledScopes() + + sut.captureEvent(SentryEvent(), null) { + it.setTag("test", "testValue") + } + + verify(mockClient).captureEvent( + any(), + check { + assertEquals("testValue", it.tags["test"]) + }, + anyOrNull() + ) + } + + @Test + fun `when captureEvent is called with a ScopeCallback then subsequent calls to captureEvent send the unmodified Scope to the client`() { + val (sut, mockClient) = getEnabledScopes() + val argumentCaptor = argumentCaptor() + + sut.captureEvent(SentryEvent(), null) { + it.setTag("test", "testValue") + } + + sut.captureEvent(SentryEvent()) + + verify(mockClient, times(2)).captureEvent( + any(), + argumentCaptor.capture(), + anyOrNull() + ) + + assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) + assertNull(argumentCaptor.allValues[1].tags["test"]) + } + + @Test + fun `when captureEvent is called with a ScopeCallback that crashes then the event should still be captured`() { + val (sut, mockClient, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + sut.captureEvent(SentryEvent(), null) { + throw exception + } + + verify(mockClient).captureEvent( + any(), + anyOrNull(), + anyOrNull() + ) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + //endregion + + //region captureMessage tests + @Test + fun `when captureMessage is called and event is null, lastEventId is empty`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + sut.callMethod("captureMessage", String::class.java, null) + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + + @Test + fun `when captureMessage is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.captureMessage("test") + verify(mockClient, never()).captureMessage(any(), any()) + } + + @Test + fun `when captureMessage is called with a valid message, captureMessage on the client should be called`() { + val (sut, mockClient) = getEnabledScopes() + + sut.captureMessage("test") + verify(mockClient).captureMessage(any(), any(), any()) + } + + @Test + fun `when captureMessage is called, level is INFO by default`() { + val (sut, mockClient) = getEnabledScopes() + sut.captureMessage("test") + verify(mockClient).captureMessage(eq("test"), eq(SentryLevel.INFO), any()) + } + + @Test + fun `when captureMessage is called with a ScopeCallback then the modified scope is sent to the client`() { + val (sut, mockClient) = getEnabledScopes() + + sut.captureMessage("test") { + it.setTag("test", "testValue") + } + + verify(mockClient).captureMessage( + any(), + any(), + check { + assertEquals("testValue", it.tags["test"]) + } + ) + } + + @Test + fun `when captureMessage is called with a ScopeCallback then subsequent calls to captureMessage send the unmodified Scope to the client`() { + val (sut, mockClient) = getEnabledScopes() + val argumentCaptor = argumentCaptor() + + sut.captureMessage("testMessage") { + it.setTag("test", "testValue") + } + + sut.captureMessage("test", SentryLevel.INFO) + + verify(mockClient, times(2)).captureMessage( + any(), + any(), + argumentCaptor.capture() + ) + + assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) + assertNull(argumentCaptor.allValues[1].tags["test"]) + } + + @Test + fun `when captureMessage is called with a ScopeCallback that crashes then the message should still be captured`() { + val (sut, mockClient, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + sut.captureMessage("Hello World") { + throw exception + } + + verify(mockClient).captureMessage( + any(), + anyOrNull(), + anyOrNull() + ) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + + //endregion + + //region captureException tests + @Test + fun `when captureException is called and exception is null, lastEventId is empty`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + sut.callMethod("captureException", Throwable::class.java, null) + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + + @Test + fun `when captureException is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.captureException(Throwable()) + verify(mockClient, never()).captureEvent(any(), any(), any()) + } + + @Test + fun `when captureException is called with a valid argument and hint, captureEvent on the client should be called`() { + val (sut, mockClient) = getEnabledScopes() + + val hints = HintUtils.createWithTypeCheckHint({}) + sut.captureException(Throwable(), hints) + verify(mockClient).captureEvent(any(), any(), any()) + } + + @Test + fun `when captureException is called with a valid argument but no hint, captureEvent on the client should be called`() { + val (sut, mockClient) = getEnabledScopes() + + sut.captureException(Throwable()) + verify(mockClient).captureEvent(any(), any(), any()) + } + + @Test + fun `when captureException is called with an exception which has been previously attached with span context, span context should be set on the event before capturing`() { + val (sut, mockClient) = getEnabledScopes() + val throwable = Throwable() + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(throwable, span, "tx-name") + + sut.captureException(throwable) + verify(mockClient).captureEvent( + check { + assertEquals(span.spanContext, it.contexts.trace) + assertEquals("tx-name", it.transaction) + }, + any(), + anyOrNull() + ) + } + + @Test + fun `when captureException is called with an exception which has not been previously attached with span context, span context should not be set on the event before capturing`() { + val (sut, mockClient) = getEnabledScopes() + val span = mock() + whenever(span.spanContext).thenReturn(SpanContext("op")) + sut.setSpanContext(Throwable(), span, "tx-name") + + sut.captureException(Throwable()) + verify(mockClient).captureEvent( + check { + assertNull(it.contexts.trace) + }, + any(), + anyOrNull() + ) + } + + @Test + fun `when captureException is called with a ScopeCallback then the modified scope is sent to the client`() { + val (sut, mockClient) = getEnabledScopes() + + sut.captureException(Throwable(), null) { + it.setTag("test", "testValue") + } + + verify(mockClient).captureEvent( + any(), + check { + assertEquals("testValue", it.tags["test"]) + }, + anyOrNull() + ) + } + + @Test + fun `when captureException is called with a ScopeCallback then subsequent calls to captureException send the unmodified Scope to the client`() { + val (sut, mockClient) = getEnabledScopes() + val argumentCaptor = argumentCaptor() + + sut.captureException(Throwable(), null) { + it.setTag("test", "testValue") + } + + sut.captureException(Throwable()) + + verify(mockClient, times(2)).captureEvent( + any(), + argumentCaptor.capture(), + anyOrNull() + ) + + assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) + assertNull(argumentCaptor.allValues[1].tags["test"]) + } + + @Test + fun `when captureException is called with a ScopeCallback that crashes then the exception should still be captured`() { + val (sut, mockClient, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + sut.captureException(Throwable()) { + throw exception + } + + verify(mockClient).captureEvent( + any(), + anyOrNull(), + anyOrNull() + ) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + + //endregion + + //region captureUserFeedback tests + + @Test + fun `when captureUserFeedback is called it is forwarded to the client`() { + val (sut, mockClient) = getEnabledScopes() + sut.captureUserFeedback(userFeedback) + + verify(mockClient).captureUserFeedback( + check { + assertEquals(userFeedback.eventId, it.eventId) + assertEquals(userFeedback.email, it.email) + assertEquals(userFeedback.name, it.name) + assertEquals(userFeedback.comments, it.comments) + } + ) + } + + @Test + fun `when captureUserFeedback is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.captureUserFeedback(userFeedback) + verify(mockClient, never()).captureUserFeedback(any()) + } + + @Test + fun `when captureUserFeedback is called and client throws, don't crash`() { + val (sut, mockClient) = getEnabledScopes() + + whenever(mockClient.captureUserFeedback(any())).doThrow(IllegalArgumentException("")) + + sut.captureUserFeedback(userFeedback) + } + + private val userFeedback: UserFeedback get() { + val eventId = SentryId("c2fb8fee2e2b49758bcb67cda0f713c7") + return UserFeedback(eventId).apply { + name = "John" + email = "john@me.com" + comments = "comment" + } + } + + //region captureCheckIn tests + + @Test + fun `when captureCheckIn is called it is forwarded to the client`() { + val (sut, mockClient) = getEnabledScopes() + sut.captureCheckIn(checkIn) + + verify(mockClient).captureCheckIn( + check { + assertEquals(checkIn.checkInId, it.checkInId) + assertEquals(checkIn.monitorSlug, it.monitorSlug) + assertEquals(checkIn.status, it.status) + }, + any(), + anyOrNull() + ) + } + + @Test + fun `when captureCheckIn is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.captureCheckIn(checkIn) + verify(mockClient, never()).captureCheckIn(any(), any(), anyOrNull()) + } + + @Test + fun `when captureCheckIn is called and client throws, don't crash`() { + val (sut, mockClient) = getEnabledScopes() + + whenever(mockClient.captureCheckIn(any(), any(), anyOrNull())).doThrow(IllegalArgumentException("")) + + sut.captureCheckIn(checkIn) + } + + private val checkIn: CheckIn = CheckIn("some_slug", CheckInStatus.OK) + + //endregion + + //region close tests + @Test + fun `when close is called on disabled client, do nothing`() { + val (sut, mockClient) = getEnabledScopes() + sut.close() + + sut.close() + verify(mockClient).close(eq(false)) // 1 to close, but next one wont be recorded + } + + @Test + fun `when close is called and client is alive, close on the client should be called`() { + val (sut, mockClient) = getEnabledScopes() + + sut.close() + verify(mockClient).close(eq(false)) + } + + @Test + fun `when close is called with isRestarting false and client is alive, close on the client should be called with isRestarting false`() { + val (sut, mockClient) = getEnabledScopes() + + sut.close(false) + verify(mockClient).close(eq(false)) + } + + @Test + fun `when close is called with isRestarting true and client is alive, close on the client should be called with isRestarting true`() { + val (sut, mockClient) = getEnabledScopes() + + sut.close(true) + verify(mockClient).close(eq(true)) + } + //endregion + + //region withScope tests + @Test + fun `when withScope is called on disabled client, execute on NoOpScope`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + sut.close() + + sut.withScope(scopeCallback) + verify(scopeCallback).run(NoOpScope.getInstance()) + } + + @Test + fun `when withScope is called with alive client, run should be called`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + + sut.withScope(scopeCallback) + verify(scopeCallback).run(any()) + } + + @Test + fun `when withScope throws an exception then it should be caught`() { + val (scopes, _, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + val scopeCallback = ScopeCallback { + throw exception + } + + scopes.withScope(scopeCallback) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + //endregion + + //region configureScope tests + @Test + fun `when configureScope is called on disabled client, do nothing`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + sut.close() + + sut.configureScope(scopeCallback) + verify(scopeCallback, never()).run(any()) + } + + @Test + fun `when configureScope is called with alive client, run should be called`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + + sut.configureScope(scopeCallback) + verify(scopeCallback).run(any()) + } + + @Test + fun `when configureScope throws an exception then it should be caught`() { + val (scopes, _, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + val scopeCallback = ScopeCallback { + throw exception + } + + scopes.configureScope(scopeCallback) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + //endregion + + @Test + fun `when integration is registered, scopes is enabled`() { + val mock = mock() + + var options: SentryOptions? = null + // init main scopes and make it enabled + Sentry.init { + it.addIntegration(mock) + it.dsn = "https://key@sentry.io/proj" + it.cacheDirPath = file.absolutePath + it.setSerializer(mock()) + options = it + } + + doAnswer { + val scopes = it.arguments[0] as IScopes + assertTrue(scopes.isEnabled) + }.whenever(mock).register(any(), eq(options!!)) + + verify(mock).register(any(), eq(options!!)) + } + + //region setLevel tests + @Test + fun `when setLevel is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + scopes.setLevel(SentryLevel.INFO) + assertNull(scope?.level) + } + + @Test + fun `when setLevel is called, level is set`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.setLevel(SentryLevel.INFO) + assertEquals(SentryLevel.INFO, scope?.level) + } + //endregion + + //region setTransaction tests + @Test + fun `when setTransaction is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + scopes.setTransaction("test") + assertNull(scope?.transactionName) + } + + @Test + fun `when setTransaction is called, and transaction is not set, transaction name is changed`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.setTransaction("test") + assertEquals("test", scope?.transactionName) + } + + @Test + fun `when setTransaction is called, and transaction is set, transaction name is changed`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + val tx = scopes.startTransaction("test", "op") + scopes.configureScope { it.setTransaction(tx) } + + assertEquals("test", scope?.transactionName) + } + + @Test + fun `when startTransaction is called with different instrumenter, no-op is returned`() { + val scopes = generateScopes() + + val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } + val transactionOptions = TransactionOptions() + val tx = scopes.startTransaction(transactionContext, transactionOptions) + + assertTrue(tx is NoOpTransaction) + } + + @Test + fun `when startTransaction is called with different instrumenter, no-op is returned 2`() { + val scopes = generateScopes() { + it.instrumenter = Instrumenter.OTEL + } + + val tx = scopes.startTransaction("test", "op") + + assertTrue(tx is NoOpTransaction) + } + + @Test + fun `when startTransaction is called with configured instrumenter, it works`() { + val scopes = generateScopes() { + it.instrumenter = Instrumenter.OTEL + } + + val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } + val transactionOptions = TransactionOptions() + val tx = scopes.startTransaction(transactionContext, transactionOptions) + + assertFalse(tx is NoOpTransaction) + } + //endregion + + //region setUser tests + @Test + fun `when setUser is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + scopes.setUser(User()) + assertNull(scope?.user) + } + + @Test + fun `when setUser is called, user is set`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + val user = User() + scopes.setUser(user) + assertEquals(user, scope?.user) + } + //endregion + + //region setFingerprint tests + @Test + fun `when setFingerprint is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + val fingerprint = listOf("abc") + scopes.setFingerprint(fingerprint) + assertEquals(0, scope?.fingerprint?.count()) + } + + @Test + fun `when setFingerprint is called with null parameter, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.callMethod("setFingerprint", List::class.java, null) + assertEquals(0, scope?.fingerprint?.count()) + } + + @Test + fun `when setFingerprint is called, fingerprint is set`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + val fingerprint = listOf("abc") + scopes.setFingerprint(fingerprint) + assertEquals(1, scope?.fingerprint?.count()) + } + //endregion + + //region clearBreadcrumbs tests + @Test + fun `when clearBreadcrumbs is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.addBreadcrumb(Breadcrumb()) + assertEquals(1, scope?.breadcrumbs?.count()) + + scopes.close() + + assertEquals(0, scope?.breadcrumbs?.count()) + } + + @Test + fun `when clearBreadcrumbs is called, clear breadcrumbs`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.addBreadcrumb(Breadcrumb()) + assertEquals(1, scope?.breadcrumbs?.count()) + scopes.clearBreadcrumbs() + assertEquals(0, scope?.breadcrumbs?.count()) + } + //endregion + + //region setTag tests + @Test + fun `when setTag is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + scopes.setTag("test", "test") + assertEquals(0, scope?.tags?.count()) + } + + @Test + fun `when setTag is called with null parameters, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + assertEquals(0, scope?.tags?.count()) + } + + @Test + fun `when setTag is called, tag is set`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.setTag("test", "test") + assertEquals(1, scope?.tags?.count()) + } + //endregion + + //region setExtra tests + @Test + fun `when setExtra is called on disabled client, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + scopes.close() + + scopes.setExtra("test", "test") + assertEquals(0, scope?.extras?.count()) + } + + @Test + fun `when setExtra is called with null parameters, do nothing`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) + assertEquals(0, scope?.extras?.count()) + } + + @Test + fun `when setExtra is called, extra is set`() { + val scopes = generateScopes() + var scope: IScope? = null + scopes.configureScope { + scope = it + } + + scopes.setExtra("test", "test") + assertEquals(1, scope?.extras?.count()) + } + //endregion + + //region captureEnvelope tests + @Test + fun `when captureEnvelope is called and envelope is null, throws IllegalArgumentException`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + try { + sut.callMethod("captureEnvelope", SentryEnvelope::class.java, null) + fail() + } catch (e: Exception) { + assertTrue(e.cause is java.lang.IllegalArgumentException) + } + } + + @Test + fun `when captureEnvelope is called on disabled client, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + sut.close() + + sut.captureEnvelope(SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf())) + verify(mockClient, never()).captureEnvelope(any(), any()) + } + + @Test + fun `when captureEnvelope is called with a valid envelope, captureEnvelope on the client should be called`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) + sut.captureEnvelope(envelope) + verify(mockClient).captureEnvelope(any(), anyOrNull()) + } + + @Test + fun `when captureEnvelope is called, lastEventId is not set`() { + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + setSerializer(mock()) + } + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + whenever(mockClient.captureEnvelope(any(), anyOrNull())).thenReturn(SentryId()) + val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) + sut.captureEnvelope(envelope) + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + //endregion + + //region startSession tests + @Test + fun `when startSession is called on disabled client, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + sut.close() + + sut.startSession() + verify(mockClient, never()).captureSession(any(), any()) + } + + @Test + fun `when startSession is called, starts a session`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + sut.startSession() + verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) + } + + @Test + fun `when startSession is called and there's a session, stops it and starts a new one`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + sut.startSession() + sut.startSession() + verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionEndHint::class.java) }) + verify(mockClient, times(2)).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) + } + //endregion + + //region endSession tests + @Test + fun `when endSession is called on disabled client, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + sut.close() + + sut.endSession() + verify(mockClient, never()).captureSession(any(), any()) + } + + @Test + fun `when endSession is called and session tracking is disabled, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + sut.endSession() + verify(mockClient, never()).captureSession(any(), any()) + } + + @Test + fun `when endSession is called, end a session`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + sut.startSession() + sut.endSession() + verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) + verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionEndHint::class.java) }) + } + + @Test + fun `when endSession is called and there's no session, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.release = "0.0.1" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + sut.endSession() + verify(mockClient, never()).captureSession(any(), any()) + } + //endregion + + //region captureTransaction tests + @Test + fun `when captureTransaction is called on disabled client, do nothing`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + sut.close() + + val sentryTracer = SentryTracer(TransactionContext("name", "op"), sut) + sentryTracer.finish() + sut.captureTransaction(SentryTransaction(sentryTracer), null as TraceContext?) + verify(mockClient, never()).captureTransaction(any(), any(), any()) + verify(mockClient, never()).captureTransaction(any(), any(), any(), anyOrNull(), anyOrNull()) + } + + @Test + fun `when captureTransaction and transaction is sampled, captureTransaction on the client should be called`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) + sentryTracer.finish() + val traceContext = sentryTracer.traceContext() + verify(mockClient).captureTransaction(any(), equalTraceContext(traceContext), any(), eq(null), eq(null)) + } + + @Test + fun `when captureTransaction is called, lastEventId is not set`() { + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + setSerializer(mock()) + } + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + whenever(mockClient.captureTransaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(SentryId()) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) + sentryTracer.finish() + assertEquals(SentryId.EMPTY_ID, sut.lastEventId) + } + + @Test + fun `when captureTransaction and transaction is not finished, captureTransaction on the client should not be called`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) + sut.captureTransaction(SentryTransaction(sentryTracer), null as TraceContext?) + verify(mockClient, never()).captureTransaction(any(), any(), any(), eq(null), anyOrNull()) + } + + @Test + fun `when captureTransaction and transaction is not sampled, captureTransaction on the client should not be called`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) + sentryTracer.finish() + val traceContext = sentryTracer.traceContext() + verify(mockClient, never()).captureTransaction(any(), equalTraceContext(traceContext), any(), eq(null), anyOrNull()) + } + + @Test + fun `transactions lost due to sampling are recorded as lost`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) + sentryTracer.finish() + + assertClientReport( + options.clientReportRecorder, + listOf(DiscardedEvent(DiscardReason.SAMPLE_RATE.reason, DataCategory.Transaction.category, 1)) + ) + } + + @Test + fun `transactions lost due to sampling caused by backpressure are recorded as lost`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + val mockBackpressureMonitor = mock() + options.backpressureMonitor = mockBackpressureMonitor + whenever(mockBackpressureMonitor.downsampleFactor).thenReturn(1) + + val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) + sentryTracer.finish() + + assertClientReport( + options.clientReportRecorder, + listOf(DiscardedEvent(DiscardReason.BACKPRESSURE.reason, DataCategory.Transaction.category, 1)) + ) + } + //endregion + + //region profiling tests + + @Test + fun `when startTransaction and profiling is enabled, transaction is profiled only if sampled`() { + val mockTransactionProfiler = mock() + val mockClient = mock() + whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } + val scopes = generateScopes { + it.setTransactionProfiler(mockTransactionProfiler) + } + scopes.bindClient(mockClient) + // Transaction is not sampled, so it should not be profiled + val contexts = TransactionContext("name", "op", TracesSamplingDecision(false, null, true, null)) + val transaction = scopes.startTransaction(contexts) + transaction.finish() + verify(mockClient, never()).captureEnvelope(any()) + + // Transaction is sampled, so it should be profiled + val sampledContexts = TransactionContext("name", "op", TracesSamplingDecision(true, null, true, null)) + val sampledTransaction = scopes.startTransaction(sampledContexts) + sampledTransaction.finish() + verify(mockClient).captureEnvelope(any()) + } + + @Test + fun `when startTransaction and is sampled but profiling is disabled, transaction is not profiled`() { + val mockTransactionProfiler = mock() + val mockClient = mock() + whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } + val scopes = generateScopes { + it.profilesSampleRate = 0.0 + it.setTransactionProfiler(mockTransactionProfiler) + } + scopes.bindClient(mockClient) + val contexts = TransactionContext("name", "op") + val transaction = scopes.startTransaction(contexts) + transaction.finish() + verify(mockClient, never()).captureEnvelope(any()) + } + + @Test + fun `when profiler is running and isAppStartTransaction is false, startTransaction does not interact with profiler`() { + val mockTransactionProfiler = mock() + whenever(mockTransactionProfiler.isRunning).thenReturn(true) + val scopes = generateScopes { + it.profilesSampleRate = 1.0 + it.setTransactionProfiler(mockTransactionProfiler) + } + val context = TransactionContext("name", "op") + scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + verify(mockTransactionProfiler, never()).start() + verify(mockTransactionProfiler, never()).bindTransaction(any()) + } + + @Test + fun `when profiler is running and isAppStartTransaction is true, startTransaction binds current profile`() { + val mockTransactionProfiler = mock() + whenever(mockTransactionProfiler.isRunning).thenReturn(true) + val scopes = generateScopes { + it.profilesSampleRate = 1.0 + it.setTransactionProfiler(mockTransactionProfiler) + } + val context = TransactionContext("name", "op") + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) + verify(mockTransactionProfiler, never()).start() + verify(mockTransactionProfiler).bindTransaction(eq(transaction)) + } + + @Test + fun `when profiler is not running, startTransaction starts and binds current profile`() { + val mockTransactionProfiler = mock() + whenever(mockTransactionProfiler.isRunning).thenReturn(false) + val scopes = generateScopes { + it.profilesSampleRate = 1.0 + it.setTransactionProfiler(mockTransactionProfiler) + } + val context = TransactionContext("name", "op") + val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) + verify(mockTransactionProfiler).start() + verify(mockTransactionProfiler).bindTransaction(eq(transaction)) + } + //endregion + + //region startTransaction tests + @Test + fun `when startTransaction, creates transaction`() { + val scopes = generateScopes() + val contexts = TransactionContext("name", "op") + + val transaction = scopes.startTransaction(contexts) + assertTrue(transaction is SentryTracer) + assertEquals(contexts, transaction.root.spanContext) + } + + @Test + fun `when startTransaction with bindToScope set to false, transaction is not attached to the scope`() { + val scopes = generateScopes() + + scopes.startTransaction("name", "op", TransactionOptions()) + + scopes.configureScope { + assertNull(it.span) + } + } + + @Test + fun `when startTransaction without bindToScope set, transaction is not attached to the scope`() { + val scopes = generateScopes() + + scopes.startTransaction("name", "op") + + scopes.configureScope { + assertNull(it.span) + } + } + + @Test + fun `when startTransaction with bindToScope set to true, transaction is attached to the scope`() { + val scopes = generateScopes() + + val transaction = scopes.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) + + scopes.configureScope { + assertEquals(transaction, it.span) + } + } + + @Test + fun `when startTransaction and no tracing sampling is configured, event is not sampled`() { + val scopes = generateScopes { + it.tracesSampleRate = 0.0 + } + + val transaction = scopes.startTransaction("name", "op") + assertFalse(transaction.isSampled!!) + } + + @Test + fun `when startTransaction and no profile sampling is configured, profile is not sampled`() { + val scopes = generateScopes { + it.tracesSampleRate = 1.0 + it.profilesSampleRate = 0.0 + } + + val transaction = scopes.startTransaction("name", "op") + assertTrue(transaction.isSampled!!) + assertFalse(transaction.isProfileSampled!!) + } + + @Test + fun `when startTransaction with parent sampled and no traces sampler provided, transaction inherits sampling decision`() { + val scopes = generateScopes() + val transactionContext = TransactionContext("name", "op") + transactionContext.parentSampled = true + val transaction = scopes.startTransaction(transactionContext) + assertNotNull(transaction) + assertNotNull(transaction.isSampled) + assertTrue(transaction.isSampled!!) + } + + @Test + fun `when startTransaction with parent profile sampled and no profile sampler provided, transaction inherits profile sampling decision`() { + val scopes = generateScopes() + val transactionContext = TransactionContext("name", "op") + transactionContext.setParentSampled(true, true) + val transaction = scopes.startTransaction(transactionContext) + assertTrue(transaction.isProfileSampled!!) + } + + @Test + fun `Scopes should close the sentry executor processor, profiler and performance collector on close call`() { + val executor = mock() + val profiler = mock() + val performanceCollector = mock() + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + cacheDirPath = file.absolutePath + executorService = executor + setTransactionProfiler(profiler) + transactionPerformanceCollector = performanceCollector + } + val sut = createScopes(options) + sut.close() + verify(executor).close(any()) + verify(profiler).close() + verify(performanceCollector).close() + } + + @Test + fun `Scopes with isRestarting true should close the sentry executor in the background`() { + val executor = spy(DeferredExecutorService()) + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + executorService = executor + } + val sut = createScopes(options) + sut.close(true) + verify(executor, never()).close(any()) + executor.runAll() + verify(executor).close(any()) + } + + @Test + fun `Scopes with isRestarting false should close the sentry executor in the background`() { + val executor = mock() + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + executorService = executor + } + val sut = createScopes(options) + sut.close(false) + verify(executor).close(any()) + } + + @Test + fun `Scopes close should clear the scope`() { + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + } + + val sut = createScopes(options) + sut.addBreadcrumb("Test") + sut.startTransaction("test", "test.op", TransactionOptions().also { it.isBindToScope = true }) + sut.close() + + // we have to clone the scope, so its isEnabled returns true, but it's still built up from + // the old scope preserving its data + val clone = sut.forkedScopes("test") + var oldScope: IScope? = null + clone.configureScope { scope -> oldScope = scope } + assertNull(oldScope!!.transaction) + assertTrue(oldScope!!.breadcrumbs.isEmpty()) + } + + @Test + fun `when tracesSampleRate and tracesSampler are not set on SentryOptions, startTransaction returns NoOp`() { + val scopes = generateScopes { + it.tracesSampleRate = null + it.tracesSampler = null + } + val transaction = scopes.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) + assertTrue(transaction is NoOpTransaction) + } + //endregion + + //region startTransaction tests + @Test + fun `when traceHeaders and no transaction is active, traceHeaders are generated from scope`() { + val scopes = generateScopes() + + var spanId: SpanId? = null + scopes.configureScope { spanId = it.propagationContext.spanId } + + val traceHeader = scopes.traceHeaders() + assertNotNull(traceHeader) + assertEquals(spanId, traceHeader.spanId) + } + + @Test + fun `when traceHeaders and there is an active transaction, traceHeaders are not null`() { + val scopes = generateScopes() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } + + assertNotNull(scopes.traceHeaders()) + } + //endregion + + //region getSpan tests + @Test + fun `when there is no active transaction, getSpan returns null`() { + val scopes = generateScopes() + assertNull(scopes.span) + } + + @Test + fun `when there is no active transaction, getTransaction returns null`() { + val scopes = generateScopes() + assertNull(scopes.transaction) + } + + @Test + fun `when there is active transaction bound to the scope, getTransaction and getSpan return active transaction`() { + val scopes = generateScopes() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.transaction = tx } + + assertEquals(tx, scopes.transaction) + assertEquals(tx, scopes.span) + } + + @Test + fun `when there is a transaction but the scopes is closed, getTransaction returns null`() { + val scopes = generateScopes() + scopes.startTransaction("name", "op") + scopes.close() + + assertNull(scopes.transaction) + } + + @Test + fun `when there is active span within a transaction bound to the scope, getSpan returns active span`() { + val scopes = generateScopes() + val tx = scopes.startTransaction("aTransaction", "op") + scopes.configureScope { it.setTransaction(tx) } + scopes.configureScope { it.setTransaction(tx) } + val span = tx.startChild("op") + + assertEquals(tx, scopes.transaction) + assertEquals(span, scopes.span) + } + // endregion + + //region setSpanContext + @Test + fun `associates span context with throwable`() { + val (scopes, mockClient) = getEnabledScopes() + val transaction = scopes.startTransaction("aTransaction", "op") + val span = transaction.startChild("op") + val exception = RuntimeException() + + scopes.setSpanContext(exception, span, "tx-name") + scopes.captureEvent(SentryEvent(exception)) + + verify(mockClient).captureEvent( + check { + assertEquals(span.spanContext, it.contexts.trace) + }, + anyOrNull(), + anyOrNull() + ) + } + // endregion + + @Test + fun `isCrashedLastRun does not delete native marker if auto session is enabled`() { + val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) + nativeMarker.mkdirs() + nativeMarker.createNewFile() + val scopes = generateScopes() as Scopes + + assertTrue(scopes.isCrashedLastRun!!) + assertTrue(nativeMarker.exists()) + } + + @Test + fun `isCrashedLastRun deletes the native marker if auto session is disabled`() { + val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) + nativeMarker.mkdirs() + nativeMarker.createNewFile() + val scopes = generateScopes { + it.isEnableAutoSessionTracking = false + } + + assertTrue(scopes.isCrashedLastRun!!) + assertFalse(nativeMarker.exists()) + } + + @Test + fun `reportFullyDisplayed is ignored if TimeToFullDisplayTracing is disabled`() { + var called = false + val scopes = generateScopes { + it.fullyDisplayedReporter.registerFullyDrawnListener { + called = !called + } + } + scopes.reportFullyDisplayed() + assertFalse(called) + } + + @Test + fun `reportFullyDisplayed calls FullyDisplayedReporter if TimeToFullDisplayTracing is enabled`() { + var called = false + val scopes = generateScopes { + it.isEnableTimeToFullDisplayTracing = true + it.fullyDisplayedReporter.registerFullyDrawnListener { + called = !called + } + } + scopes.reportFullyDisplayed() + assertTrue(called) + } + + @Test + fun `reportFullyDisplayed calls FullyDisplayedReporter only once`() { + var called = false + val scopes = generateScopes { + it.isEnableTimeToFullDisplayTracing = true + it.fullyDisplayedReporter.registerFullyDrawnListener { + called = !called + } + } + scopes.reportFullyDisplayed() + assertTrue(called) + scopes.reportFullyDisplayed() + assertTrue(called) + } + + @Test + fun `reportFullDisplayed calls reportFullyDisplayed`() { + val scopes = spy(generateScopes()) + scopes.reportFullDisplayed() + verify(scopes).reportFullyDisplayed() + } + + @Test + fun `continueTrace creates propagation context from headers and returns transaction context if performance enabled`() { + val scopes = generateScopes() + val traceId = SentryId() + val parentSpanId = SpanId() + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + + scopes.configureScope { scope -> + assertEquals(traceId, scope.propagationContext.traceId) + assertEquals(parentSpanId, scope.propagationContext.parentSpanId) + } + + assertEquals(traceId, transactionContext!!.traceId) + assertEquals(parentSpanId, transactionContext!!.parentSpanId) + } + + @Test + fun `continueTrace creates new propagation context if header invalid and returns transaction context if performance enabled`() { + val scopes = generateScopes() + val traceId = SentryId() + var propagationContextHolder = AtomicReference() + + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } + val propagationContextAtStart = propagationContextHolder.get()!! + + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + + scopes.configureScope { scope -> + assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) + assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) + assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) + + assertEquals(scope.propagationContext.traceId, transactionContext!!.traceId) + assertEquals(scope.propagationContext.parentSpanId, transactionContext!!.parentSpanId) + assertEquals(scope.propagationContext.spanId, transactionContext!!.spanId) + } + } + + @Test + fun `continueTrace creates propagation context from headers and returns null if performance disabled`() { + val scopes = generateScopes { it.enableTracing = false } + val traceId = SentryId() + val parentSpanId = SpanId() + val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + + scopes.configureScope { scope -> + assertEquals(traceId, scope.propagationContext.traceId) + assertEquals(parentSpanId, scope.propagationContext.parentSpanId) + } + + assertNull(transactionContext) + } + + @Test + fun `continueTrace creates new propagation context if header invalid and returns null if performance disabled`() { + val scopes = generateScopes { it.enableTracing = false } + val traceId = SentryId() + var propagationContextHolder = AtomicReference() + + scopes.configureScope { propagationContextHolder.set(it.propagationContext) } + val propagationContextAtStart = propagationContextHolder.get()!! + + val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) + + scopes.configureScope { scope -> + assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) + assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) + assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) + } + + assertNull(transactionContext) + } + + @Test + fun `scopes provides no tags for metrics, if metric option is disabled`() { + val scopes = generateScopes { + it.isEnableMetrics = false + it.isEnableDefaultTagsForMetrics = true + } as Scopes + + assertTrue( + scopes.defaultTagsForMetrics.isEmpty() + ) + } + + @Test + fun `scopes provides no tags for metrics, if default tags option is disabled`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableDefaultTagsForMetrics = false + } as Scopes + + assertTrue( + scopes.defaultTagsForMetrics.isEmpty() + ) + } + + @Test + fun `scopes provides minimum default tags for metrics, if nothing is set up`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableDefaultTagsForMetrics = true + } as Scopes + + assertEquals( + mapOf( + "environment" to "production" + ), + scopes.defaultTagsForMetrics + ) + } + + @Test + fun `scopes provides default tags for metrics, based on options and running transaction`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableDefaultTagsForMetrics = true + it.environment = "test" + it.release = "1.0" + } as Scopes + scopes.startTransaction( + "name", + "op", + TransactionOptions().apply { isBindToScope = true } + ) + + assertEquals( + mapOf( + "environment" to "test", + "release" to "1.0", + "transaction" to "name" + ), + scopes.defaultTagsForMetrics + ) + } + + @Test + fun `scopes provides no local metric aggregator if metrics feature is disabled`() { + val scopes = generateScopes { + it.isEnableMetrics = false + it.isEnableSpanLocalMetricAggregation = true + } as Scopes + + scopes.startTransaction( + "name", + "op", + TransactionOptions().apply { isBindToScope = true } + ) + + assertNull(scopes.localMetricsAggregator) + } + + @Test + fun `scopes provides no local metric aggregator if local aggregation feature is disabled`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableSpanLocalMetricAggregation = false + } as Scopes + + scopes.startTransaction( + "name", + "op", + TransactionOptions().apply { isBindToScope = true } + ) + + assertNull(scopes.localMetricsAggregator) + } + + @Test + fun `scopes provides local metric aggregator if feature is enabled`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableSpanLocalMetricAggregation = true + } as Scopes + + scopes.startTransaction( + "name", + "op", + TransactionOptions().apply { isBindToScope = true } + ) + assertNotNull(scopes.localMetricsAggregator) + } + + @Test + fun `scopes startSpanForMetric starts a child span`() { + val scopes = generateScopes { + it.isEnableMetrics = true + it.isEnableSpanLocalMetricAggregation = true + it.sampleRate = 1.0 + } as Scopes + + val txn = scopes.startTransaction( + "name.txn", + "op.txn", + TransactionOptions().apply { isBindToScope = true } + ) + + val span = scopes.startSpanForMetric("op", "key")!! + + assertEquals("op", span.spanContext.op) + assertEquals("key", span.spanContext.description) + assertEquals(span.spanContext.parentSpanId, txn.spanContext.spanId) + } + + private val dsnTest = "https://key@sentry.io/proj" + + private fun generateScopes(optionsConfiguration: Sentry.OptionsConfiguration? = null): IScopes { + val options = SentryOptions().apply { + dsn = dsnTest + cacheDirPath = file.absolutePath + setSerializer(mock()) + tracesSampleRate = 1.0 + } + optionsConfiguration?.configure(options) + return createScopes(options) + } + + private fun getEnabledScopes(): Triple { + val logger = mock() + + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + options.tracesSampleRate = 1.0 + options.isDebug = true + options.setLogger(logger) + + val sut = createScopes(options) + val mockClient = mock() + sut.bindClient(mockClient) + return Triple(sut, mockClient, logger) + } + + private fun hashedFolder(): String { + val hash = StringUtils.calculateStringHash(dsnTest, mock()) + val fileHashFolder = File(file.absolutePath, hash!!) + return fileHashFolder.absolutePath + } + + private fun equalTraceContext(expectedContext: TraceContext?): TraceContext? { + expectedContext ?: return eq(null) + + return argWhere { actual -> + expectedContext.traceId == actual.traceId && + expectedContext.transaction == actual.transaction && + expectedContext.environment == actual.environment && + expectedContext.release == actual.release && + expectedContext.publicKey == actual.publicKey && + expectedContext.sampleRate == actual.sampleRate && + expectedContext.userId == actual.userId && + expectedContext.userSegment == actual.userSegment + } + } +} From 04f3892d57f422ddf8e4dc441dd9587c609dce2f Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 18 Apr 2024 12:16:29 +0200 Subject: [PATCH 40/57] Implement CombinedScopeViewTest --- .../java/io/sentry/CombinedScopeView.java | 1 + .../java/io/sentry/CombinedScopeViewTest.kt | 1023 ++++++++++++++++- 2 files changed, 981 insertions(+), 43 deletions(-) diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 38873bb574..44a87da0b1 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -145,6 +145,7 @@ public void setRequest(@Nullable Request request) { @Override public @NotNull List getFingerprint() { + // TODO [HSM] should these be merged? final @Nullable List current = scope.getFingerprint(); if (!current.isEmpty()) { return current; diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index d8e6783c4c..11725947cb 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -1,26 +1,61 @@ package io.sentry +import io.sentry.protocol.Request +import io.sentry.protocol.SentryId +import io.sentry.protocol.User +import junit.framework.TestCase.assertFalse +import junit.framework.TestCase.assertTrue +import org.junit.Assert.assertNotEquals +import org.mockito.kotlin.any +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.same +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import java.lang.RuntimeException +import java.util.UUID import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertSame class CombinedScopeViewTest { + private class Fixture { + lateinit var globalScope: IScope + lateinit var isolationScope: IScope + lateinit var scope: IScope + lateinit var options: SentryOptions + lateinit var scopes: IScopes + + fun getSut(options: SentryOptions = SentryOptions()): CombinedScopeView { + options.dsn = "https://key@sentry.io/proj" + options.release = "0.1" + this.options = options + globalScope = Scope(options) + isolationScope = Scope(options) + scope = Scope(options) + scopes = Scopes(scope, isolationScope, globalScope, "test") + + return CombinedScopeView(globalScope, isolationScope, scope) + } + } + + private val fixture = Fixture() + @Test fun `adds breadcrumbs from all scopes in sorted order`() { - val options = SentryOptions() - val globalScope = Scope(options) - val isolationScope = Scope(options) - val scope = Scope(options) + val combined = fixture.getSut() - val combined = CombinedScopeView(globalScope, isolationScope, scope) + fixture.globalScope.addBreadcrumb(Breadcrumb.info("global 1")) + fixture.isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 1")) - globalScope.addBreadcrumb(Breadcrumb.info("global 1")) - isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) - scope.addBreadcrumb(Breadcrumb.info("current 1")) - - globalScope.addBreadcrumb(Breadcrumb.info("global 2")) - isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) - scope.addBreadcrumb(Breadcrumb.info("current 2")) + fixture.globalScope.addBreadcrumb(Breadcrumb.info("global 2")) + fixture.isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 2")) val breadcrumbs = combined.breadcrumbs assertEquals("global 1", breadcrumbs.poll().message) @@ -34,19 +69,15 @@ class CombinedScopeViewTest { @Test fun `oldest breadcrumbs are dropped first`() { val options = SentryOptions().also { it.maxBreadcrumbs = 5 } - val globalScope = Scope(options) - val isolationScope = Scope(options) - val scope = Scope(options) - - val combined = CombinedScopeView(globalScope, isolationScope, scope) + val combined = fixture.getSut(options) - globalScope.addBreadcrumb(Breadcrumb.info("global 1")) - isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) - scope.addBreadcrumb(Breadcrumb.info("current 1")) + fixture.globalScope.addBreadcrumb(Breadcrumb.info("global 1")) + fixture.isolationScope.addBreadcrumb(Breadcrumb.info("isolation 1")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 1")) - globalScope.addBreadcrumb(Breadcrumb.info("global 2")) - isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) - scope.addBreadcrumb(Breadcrumb.info("current 2")) + fixture.globalScope.addBreadcrumb(Breadcrumb.info("global 2")) + fixture.isolationScope.addBreadcrumb(Breadcrumb.info("isolation 2")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 2")) val breadcrumbs = combined.breadcrumbs // assertEquals("global 1", breadcrumbs.poll().message) <-- was dropped @@ -56,8 +87,8 @@ class CombinedScopeViewTest { assertEquals("isolation 2", breadcrumbs.poll().message) assertEquals("current 2", breadcrumbs.poll().message) - scope.addBreadcrumb(Breadcrumb.info("current 3")) - scope.addBreadcrumb(Breadcrumb.info("current 4")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 3")) + fixture.scope.addBreadcrumb(Breadcrumb.info("current 4")) val breadcrumbs2 = combined.breadcrumbs // assertEquals("global 1", breadcrumbs.poll().message) <-- was dropped @@ -70,35 +101,73 @@ class CombinedScopeViewTest { assertEquals("current 4", breadcrumbs2.poll().message) } + @Test + fun `can add breadcrumb with hint`() { + var capturedHint: Hint? = null + val combined = fixture.getSut( + SentryOptions().also { + it.beforeBreadcrumb = + SentryOptions.BeforeBreadcrumbCallback { breadcrumb: Breadcrumb, hint: Hint -> + capturedHint = hint + breadcrumb + } + } + ) + + combined.addBreadcrumb(Breadcrumb.info("aBreadcrumb"), Hint().also { it.set("aTest", "aValue") }) + + assertNotNull(capturedHint) + assertEquals("aValue", capturedHint?.get("aTest")) + + val breadcrumbs = combined.breadcrumbs + assertEquals(1, breadcrumbs.size) + assertEquals("aBreadcrumb", breadcrumbs.first().message) + } + + @Test + fun `adds breadcrumb to default scope`() { + val combined = fixture.getSut() + combined.addBreadcrumb(Breadcrumb.info("aBreadcrumb")) + + assertEquals(ScopeType.ISOLATION, combined.options.defaultScopeType) + assertEquals(0, fixture.scope.breadcrumbs.size) + assertEquals(1, fixture.isolationScope.breadcrumbs.size) + assertEquals(0, fixture.globalScope.breadcrumbs.size) + } + + @Test + fun `clears breadcrumbs only from default scope`() { + val combined = fixture.getSut() + fixture.scope.addBreadcrumb(Breadcrumb.info("scopeBreadcrumb")) + fixture.isolationScope.addBreadcrumb(Breadcrumb.info("isolationBreadcrumb")) + fixture.globalScope.addBreadcrumb(Breadcrumb.info("globalBreadcrumb")) + + combined.clearBreadcrumbs() + + assertEquals(ScopeType.ISOLATION, combined.options.defaultScopeType) + assertEquals(1, fixture.scope.breadcrumbs.size) + assertEquals(0, fixture.isolationScope.breadcrumbs.size) + assertEquals(1, fixture.globalScope.breadcrumbs.size) + } + @Test fun `event processors from options are not returned`() { val options = SentryOptions().also { it.addEventProcessor(MainEventProcessor(it)) } - - val globalScope = Scope(options) - val isolationScope = Scope(options) - val scope = Scope(options) - - val combined = CombinedScopeView(globalScope, isolationScope, scope) + val combined = fixture.getSut(options) assertEquals(0, combined.eventProcessors.size) } @Test - fun `event processors from options and all scopes in order`() { - val options = SentryOptions() - - val globalScope = Scope(options) - val isolationScope = Scope(options) - val scope = Scope(options) - - val first = TestEventProcessor(0).also { scope.addEventProcessor(it) } - val second = TestEventProcessor(1000).also { globalScope.addEventProcessor(it) } - val third = TestEventProcessor(2000).also { isolationScope.addEventProcessor(it) } - val fourth = TestEventProcessor(3000).also { scope.addEventProcessor(it) } + fun `event processors from all scopes are returned in order`() { + val combined = fixture.getSut() - val combined = CombinedScopeView(globalScope, isolationScope, scope) + val first = TestEventProcessor(0).also { fixture.scope.addEventProcessor(it) } + val second = TestEventProcessor(1000).also { fixture.globalScope.addEventProcessor(it) } + val third = TestEventProcessor(2000).also { fixture.isolationScope.addEventProcessor(it) } + val fourth = TestEventProcessor(3000).also { fixture.scope.addEventProcessor(it) } val eventProcessors = combined.eventProcessors @@ -108,6 +177,874 @@ class CombinedScopeViewTest { assertEquals(fourth, eventProcessors.get(3)) } + @Test + fun `adds event processor to default scope`() { + val combined = fixture.getSut() + + val eventProcessor = MainEventProcessor(fixture.options) + combined.addEventProcessor(eventProcessor) + + assertEquals(ScopeType.ISOLATION, combined.options.defaultScopeType) + assertFalse(fixture.scope.eventProcessors.contains(eventProcessor)) + assertTrue(fixture.isolationScope.eventProcessors.contains(eventProcessor)) + assertFalse(fixture.globalScope.eventProcessors.contains(eventProcessor)) + } + + @Test + fun `prefers level from current scope`() { + val combined = fixture.getSut() + fixture.scope.level = SentryLevel.DEBUG + fixture.isolationScope.level = SentryLevel.INFO + fixture.globalScope.level = SentryLevel.WARNING + + assertEquals(SentryLevel.DEBUG, combined.level) + } + + @Test + fun `uses isolation scope level if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.level = SentryLevel.INFO + fixture.globalScope.level = SentryLevel.WARNING + + assertEquals(SentryLevel.INFO, combined.level) + } + + @Test + fun `uses global scope level if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.level = SentryLevel.WARNING + + assertEquals(SentryLevel.WARNING, combined.level) + } + + @Test + fun `returns null level if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.level) + } + + @Test + fun `setLevel modifies default scope`() { + val combined = fixture.getSut() + combined.level = SentryLevel.ERROR + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.level) + assertEquals(SentryLevel.ERROR, fixture.isolationScope.level) + assertNull(fixture.globalScope.level) + } + + @Test + fun `prefers transaction name from current scope`() { + val combined = fixture.getSut() + fixture.scope.setTransaction("scopeTransaction") + fixture.isolationScope.setTransaction("isolationTransaction") + fixture.globalScope.setTransaction("globalTransaction") + + assertEquals("scopeTransaction", combined.transactionName) + } + + @Test + fun `uses isolation transaction name if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.setTransaction("isolationTransaction") + fixture.globalScope.setTransaction("globalTransaction") + + assertEquals("isolationTransaction", combined.transactionName) + } + + @Test + fun `uses global transaction name if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.setTransaction("globalTransaction") + + assertEquals("globalTransaction", combined.transactionName) + } + + @Test + fun `returns null transaction name if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.transactionName) + } + + @Test + fun `setTransaction(String) modifies default scope`() { + val combined = fixture.getSut() + combined.setTransaction("aTransaction") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.transactionName) + assertEquals("aTransaction", fixture.isolationScope.transactionName) + assertNull(fixture.globalScope.transactionName) + } + + @Test + fun `prefers transaction andspan from current scope`() { + val combined = fixture.getSut() + fixture.scope.setTransaction(createTransaction("scopeTransaction")) + fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) + fixture.globalScope.setTransaction(createTransaction("globalTransaction")) + + assertEquals("scopeTransaction", combined.transaction!!.name) + assertEquals("scopeTransactionSpan", combined.span!!.operation) + } + + @Test + fun `uses isolation scope transaction andspan if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) + fixture.globalScope.setTransaction(createTransaction("globalTransaction")) + + assertEquals("isolationTransaction", combined.transaction!!.name) + assertEquals("isolationTransactionSpan", combined.span!!.operation) + } + + @Test + fun `uses global transaction andscope span if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.setTransaction(createTransaction("globalTransaction")) + + assertEquals("globalTransaction", combined.transaction!!.name) + assertEquals("globalTransactionSpan", combined.span!!.operation) + } + + @Test + fun `returns null transaction and span if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.transaction) + assertNull(combined.span) + } + + @Test + fun `setTransaction(ITransaction) modifies default scope`() { + val combined = fixture.getSut() + val tx = createTransaction("aTransaction") + combined.setTransaction(tx) + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.transaction) + assertSame(tx, fixture.isolationScope.transaction) + assertNull(fixture.globalScope.transaction) + } + + @Test + fun `clears transaction from default scope`() { + val combined = fixture.getSut() + fixture.scope.setTransaction(createTransaction("scopeTransaction")) + fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) + fixture.globalScope.setTransaction(createTransaction("globalTransaction")) + + combined.clearTransaction() + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNotNull(fixture.scope.transaction) + assertNull(fixture.isolationScope.transaction) + assertNotNull(fixture.globalScope.transaction) + } + + @Test + fun `prefers user from current scope`() { + val combined = fixture.getSut() + fixture.scope.user = User().also { it.name = "scopeUser" } + fixture.isolationScope.user = User().also { it.name = "isolationUser" } + fixture.globalScope.user = User().also { it.name = "globalUser" } + + assertEquals("scopeUser", combined.user!!.name) + } + + @Test + fun `uses isolation scope user if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.user = User().also { it.name = "isolationUser" } + fixture.globalScope.user = User().also { it.name = "globalUser" } + + assertEquals("isolationUser", combined.user!!.name) + } + + @Test + fun `uses global scope user if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.user = User().also { it.name = "globalUser" } + + assertEquals("globalUser", combined.user!!.name) + } + + @Test + fun `returns null user if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.user) + } + + @Test + fun `set user modifies default scope`() { + val combined = fixture.getSut() + val user = User().also { it.name = "aUser" } + combined.user = user + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.user) + assertSame(user, fixture.isolationScope.user) + assertNull(fixture.globalScope.user) + } + + @Test + fun `prefers screen from current scope`() { + val combined = fixture.getSut() + fixture.scope.screen = "scopeScreen" + fixture.isolationScope.screen = "isolationScreen" + fixture.globalScope.screen = "globalScreen" + + assertEquals("scopeScreen", combined.screen) + } + + @Test + fun `uses isolation scope screen if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.screen = "isolationScreen" + fixture.globalScope.screen = "globalScreen" + + assertEquals("isolationScreen", combined.screen) + } + + @Test + fun `uses global scope screen if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.screen = "globalScreen" + + assertEquals("globalScreen", combined.screen) + } + + @Test + fun `returns null screen if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.screen) + } + + @Test + fun `set screen modifies default scope`() { + val combined = fixture.getSut() + combined.screen = "aScreen" + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.screen) + assertEquals("aScreen", fixture.isolationScope.screen) + assertNull(fixture.globalScope.screen) + } + + @Test + fun `prefers request from current scope`() { + val combined = fixture.getSut() + fixture.scope.request = Request().also { it.queryString = "scopeRequest" } + fixture.isolationScope.request = Request().also { it.queryString = "isolationRequest" } + fixture.globalScope.request = Request().also { it.queryString = "globalRequest" } + + assertEquals("scopeRequest", combined.request!!.queryString) + } + + @Test + fun `uses isolation scope request if none in current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.request = Request().also { it.queryString = "isolationRequest" } + fixture.globalScope.request = Request().also { it.queryString = "globalRequest" } + + assertEquals("isolationRequest", combined.request!!.queryString) + } + + @Test + fun `uses global scope request if none in current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.request = Request().also { it.queryString = "globalRequest" } + + assertEquals("globalRequest", combined.request!!.queryString) + } + + @Test + fun `returns null request if none in any scope`() { + val combined = fixture.getSut() + + assertNull(combined.request) + } + + @Test + fun `set request modifies default scope`() { + val combined = fixture.getSut() + val request = Request().also { it.queryString = "aRequest" } + combined.request = request + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.request) + assertSame(request, fixture.isolationScope.request) + assertNull(fixture.globalScope.request) + } + + @Test + fun `clear removes from default scope`() { + val combined = fixture.getSut() + + fixture.scope.level = SentryLevel.DEBUG + fixture.isolationScope.level = SentryLevel.INFO + fixture.globalScope.level = SentryLevel.WARNING + + combined.clear() + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNotNull(fixture.scope.level) + assertNull(fixture.isolationScope.level) + assertNotNull(fixture.globalScope.level) + } + + @Test + fun `tags are combined from all scopes`() { + val combined = fixture.getSut() + + fixture.scope.setTag("scopeTag", "scopeValue") + fixture.isolationScope.setTag("isolationTag", "isolationValue") + fixture.globalScope.setTag("globalTag", "globalValue") + + val tags = combined.tags + assertEquals("scopeValue", tags["scopeTag"]) + assertEquals("isolationValue", tags["isolationTag"]) + assertEquals("globalValue", tags["globalTag"]) + } + + @Test + fun `setTag writes to default scope`() { + val combined = fixture.getSut() + combined.setTag("aTag", "aValue") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.tags["aTag"]) + assertEquals("aValue", fixture.isolationScope.tags["aTag"]) + assertNull(fixture.globalScope.tags["aTag"]) + } + + @Test + fun `prefer scope value for tags with same key`() { + val combined = fixture.getSut() + + fixture.scope.setTag("aTag", "scopeValue") + fixture.isolationScope.setTag("aTag", "isolationValue") + fixture.globalScope.setTag("aTag", "globalValue") + + assertEquals("scopeValue", combined.tags["aTag"]) + } + + @Test + fun `uses isolation scope value for tags with same key if scope does not have it`() { + val combined = fixture.getSut() + + fixture.isolationScope.setTag("aTag", "isolationValue") + fixture.globalScope.setTag("aTag", "globalValue") + + assertEquals("isolationValue", combined.tags["aTag"]) + } + + @Test + fun `uses global scope value for tags with same key if scope and isolation scope do not have it`() { + val combined = fixture.getSut() + + fixture.globalScope.setTag("aTag", "globalValue") + + assertEquals("globalValue", combined.tags["aTag"]) + } + + @Test + fun `removeTag removes from default scope`() { + val combined = fixture.getSut() + + fixture.scope.setTag("aTag", "scopeValue") + fixture.isolationScope.setTag("aTag", "isolationValue") + fixture.globalScope.setTag("aTag", "globalValue") + + combined.removeTag("aTag") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals("scopeValue", fixture.scope.tags["aTag"]) + assertNull(fixture.isolationScope.tags["aTag"]) + assertEquals("globalValue", fixture.globalScope.tags["aTag"]) + } + + @Test + fun `extras are combined from all scopes`() { + val combined = fixture.getSut() + + fixture.scope.setExtra("scopeExtra", "scopeValue") + fixture.isolationScope.setExtra("isolationExtra", "isolationValue") + fixture.globalScope.setExtra("globalExtra", "globalValue") + + val extras = combined.extras + assertEquals("scopeValue", extras["scopeExtra"]) + assertEquals("isolationValue", extras["isolationExtra"]) + assertEquals("globalValue", extras["globalExtra"]) + } + + @Test + fun `setExtra writes to default scope`() { + val combined = fixture.getSut() + combined.setExtra("someExtra", "aValue") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.extras["someExtra"]) + assertEquals("aValue", fixture.isolationScope.extras["someExtra"]) + assertNull(fixture.globalScope.extras["someExtra"]) + } + + @Test + fun `prefer scope value for extras with same key`() { + val combined = fixture.getSut() + + fixture.scope.setExtra("someExtra", "scopeValue") + fixture.isolationScope.setExtra("someExtra", "isolationValue") + fixture.globalScope.setExtra("someExtra", "globalValue") + + assertEquals("scopeValue", combined.extras["someExtra"]) + } + + @Test + fun `uses isolation scope value for extras with same key if scope does not have it`() { + val combined = fixture.getSut() + + fixture.isolationScope.setExtra("someExtra", "isolationValue") + fixture.globalScope.setExtra("someExtra", "globalValue") + + assertEquals("isolationValue", combined.extras["someExtra"]) + } + + @Test + fun `uses global scope value for extras with same key if scope and isolation scope do not have it`() { + val combined = fixture.getSut() + + fixture.globalScope.setExtra("someExtra", "globalValue") + + assertEquals("globalValue", combined.extras["someExtra"]) + } + + @Test + fun `removeExtra removes from default scope`() { + val combined = fixture.getSut() + + fixture.scope.setExtra("someExtra", "scopeValue") + fixture.isolationScope.setExtra("someExtra", "isolationValue") + fixture.globalScope.setExtra("someExtra", "globalValue") + + combined.removeExtra("someExtra") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals("scopeValue", fixture.scope.extras["someExtra"]) + assertNull(fixture.isolationScope.extras["someExtra"]) + assertEquals("globalValue", fixture.globalScope.extras["someExtra"]) + } + + // TODO [HSM] CombinedContextsView does not override all map methods, leading to unwanted behaviour + // we should probably no longer extend Map but instead keep an internal map + // and offer a toMap function or similar +// @Test +// fun `combines context from all scopes`() { +// val combined = fixture.getSut() +// fixture.scope.setContexts("scopeContext", "scopeValue") +// fixture.isolationScope.setContexts("isolationContext", "isolationValue") +// fixture.globalScope.setContexts("globalContext", "globalValue") +// +// val contexts = combined.contexts +// assertEquals("scopeValue", contexts["scopeContext"]) +// } + + // TODO [HSM] test all setContext methods + + // TODO [HSM] fingerprint tests (discuss how it should behave first) + + @Test + fun `combines attachments Æ’rom all scopes`() { + val combined = fixture.getSut() + + fixture.scope.addAttachment(createAttachment("scopeAttachment.png")) + fixture.isolationScope.addAttachment(createAttachment("isolationAttachment.png")) + fixture.globalScope.addAttachment(createAttachment("globalAttachment.png")) + + val attachments = combined.attachments + assertNotNull(attachments.firstOrNull { it.filename == "scopeAttachment.png" }) + assertNotNull(attachments.firstOrNull { it.filename == "isolationAttachment.png" }) + assertNotNull(attachments.firstOrNull { it.filename == "globalAttachment.png" }) + } + + @Test + fun `adds attachment to default scope`() { + val combined = fixture.getSut() + combined.addAttachment(createAttachment("someAttachment.png")) + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.attachments.firstOrNull { it.filename == "someAttachment.png" }) + assertNotNull(fixture.isolationScope.attachments.firstOrNull { it.filename == "someAttachment.png" }) + assertNull(fixture.globalScope.attachments.firstOrNull { it.filename == "someAttachment.png" }) + } + + @Test + fun `clears attachments only from default scope`() { + val combined = fixture.getSut() + + fixture.scope.addAttachment(createAttachment("scopeAttachment.png")) + fixture.isolationScope.addAttachment(createAttachment("isolationAttachment.png")) + fixture.globalScope.addAttachment(createAttachment("globalAttachment.png")) + + combined.clearAttachments() + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNotNull(fixture.scope.attachments.firstOrNull { it.filename == "scopeAttachment.png" }) + assertNull(fixture.isolationScope.attachments.firstOrNull { it.filename == "isolationAttachment.png" }) + assertNotNull(fixture.globalScope.attachments.firstOrNull { it.filename == "globalAttachment.png" }) + } + + @Test + fun `returns options from global scope`() { + val scopeOptions = SentryOptions().also { it.dist = "scopeDist" } + val isolationOptions = SentryOptions().also { it.dist = "isolationDist" } + val globalOptions = SentryOptions().also { it.dist = "globalDist" } + + val combined = CombinedScopeView(Scope(globalOptions), Scope(isolationOptions), Scope(scopeOptions)) + assertEquals("globalDist", combined.options.dist) + } + + @Test + fun `replaces options on global scope`() { + val scopeOptions = SentryOptions().also { it.dist = "scopeDist" } + val isolationOptions = SentryOptions().also { it.dist = "isolationDist" } + val globalOptions = SentryOptions().also { it.dist = "globalDist" } + + val globalScope = Scope(globalOptions) + val isolationScope = Scope(isolationOptions) + val scope = Scope(scopeOptions) + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + val newOptions = SentryOptions().also { it.dist = "newDist" } + combined.replaceOptions(newOptions) + + assertEquals("scopeDist", scope.options.dist) + assertEquals("isolationDist", isolationScope.options.dist) + assertEquals("newDist", globalScope.options.dist) + } + + @Test + fun `prefers client from scope`() { + val combined = fixture.getSut() + + val scopeClient = SentryClient(fixture.options) + fixture.scope.bindClient(scopeClient) + + val isolationClient = SentryClient(fixture.options) + fixture.isolationScope.bindClient(isolationClient) + + val globalClient = SentryClient(fixture.options) + fixture.globalScope.bindClient(globalClient) + + assertSame(scopeClient, combined.client) + } + + @Test + fun `uses isolation scope client if noop on current scope`() { + val combined = fixture.getSut() + + val isolationClient = SentryClient(fixture.options) + fixture.isolationScope.bindClient(isolationClient) + + val globalClient = SentryClient(fixture.options) + fixture.globalScope.bindClient(globalClient) + + assertSame(isolationClient, combined.client) + } + + @Test + fun `uses global scope client if noop on current and isolation scope`() { + val combined = fixture.getSut() + + val globalClient = SentryClient(fixture.options) + fixture.globalScope.bindClient(globalClient) + + assertSame(globalClient, combined.client) + } + + @Test + fun `binds client to default scope`() { + val combined = fixture.getSut() + val client = SentryClient(fixture.options) + combined.bindClient(client) + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertTrue(fixture.scope.client is NoOpSentryClient) + assertSame(client, fixture.isolationScope.client) + assertTrue(fixture.globalScope.client is NoOpSentryClient) + } + + @Test + fun `getSpecificScope(null) returns scope defined in options CURRENT`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.CURRENT }) + assertSame(fixture.scope, combined.getSpecificScope(null)) + } + + @Test + fun `getSpecificScope(null) returns scope defined in options ISOLATION`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.ISOLATION }) + assertSame(fixture.isolationScope, combined.getSpecificScope(null)) + } + + @Test + fun `getSpecificScope(null) returns scope defined in options GLOBAL`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.GLOBAL }) + assertSame(fixture.globalScope, combined.getSpecificScope(null)) + } + + @Test + fun `getSpecificScope(CURRENT) returns scope`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.ISOLATION }) + assertSame(fixture.scope, combined.getSpecificScope(ScopeType.CURRENT)) + } + + @Test + fun `getSpecificScope(ISOLATION) returns scope`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.CURRENT }) + assertSame(fixture.isolationScope, combined.getSpecificScope(ScopeType.ISOLATION)) + } + + @Test + fun `getSpecificScope(GLOBAL) returns scope`() { + val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.CURRENT }) + assertSame(fixture.globalScope, combined.getSpecificScope(ScopeType.GLOBAL)) + } + + @Test + fun `forwards setSpanContext to global scope`() { + val scope = mock() + val isolationScope = mock() + val globalScope = mock() + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + val options = SentryOptions().also { it.dsn = "https://key@sentry.io/proj" } + whenever(globalScope.options).thenReturn(options) + + val exception = RuntimeException("someEx") + val transaction = createTransaction("aTransaction", Scopes(scope, isolationScope, globalScope, "test")) + combined.setSpanContext(exception, transaction, "aTransaction") + + verify(scope, never()).setSpanContext(any(), any(), any()) + verify(isolationScope, never()).setSpanContext(any(), any(), any()) + verify(globalScope).setSpanContext(same(exception), same(transaction), eq("aTransaction")) + } + + @Test + fun `withTransaction uses default scope`() { + val combined = fixture.getSut() + fixture.scope.setTransaction(createTransaction("scopeTransaction")) + fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) + fixture.globalScope.setTransaction(createTransaction("globalTransaction")) + + var capturedTransaction: ITransaction? = null + combined.withTransaction { transaction -> + capturedTransaction = transaction + } + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals("isolationTransaction", capturedTransaction?.name) + } + + @Test + fun `forwards assignTraceContext to global scope`() { + val scope = mock() + val isolationScope = mock() + val globalScope = mock() + val combined = CombinedScopeView(globalScope, isolationScope, scope) + + val event = SentryEvent() + combined.assignTraceContext(event) + + verify(scope, never()).assignTraceContext(any()) + verify(isolationScope, never()).assignTraceContext(any()) + verify(globalScope).assignTraceContext(same(event)) + } + + @Test + fun `retrieves last event id from global scope`() { + val combined = fixture.getSut() + fixture.scope.lastEventId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")) + fixture.isolationScope.lastEventId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")) + fixture.globalScope.lastEventId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de")) + + assertEquals("e81d4e2ebcf211e6869b7df92533d2de", combined.lastEventId.toString()) + } + + @Test + fun `sets last event id on all scopes`() { + val combined = fixture.getSut() + combined.lastEventId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db")) + + assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.scope.lastEventId.toString()) + assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.isolationScope.lastEventId.toString()) + assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.globalScope.lastEventId.toString()) + } + + @Test + fun `retrieves propagation context from default scope`() { + val combined = fixture.getSut() + fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")) } + fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")) } + fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de")) } + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals("d81d4e2ebcf211e6869b7df92533d2dd", combined.propagationContext.traceId.toString()) + } + + @Test + fun `sets propagation context on default scope`() { + val combined = fixture.getSut() + + combined.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db")) } + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNotEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.scope.propagationContext.traceId.toString()) + assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.isolationScope.propagationContext.traceId.toString()) + assertNotEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.globalScope.propagationContext.traceId.toString()) + } + + @Test + fun `withPropagationContext uses default scope`() { + val combined = fixture.getSut() + fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")) } + fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")) } + fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de")) } + + var capturedPropagationContext: PropagationContext? = null + combined.withPropagationContext { propagationContext -> + capturedPropagationContext = propagationContext + } + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals("d81d4e2ebcf211e6869b7df92533d2dd", capturedPropagationContext?.traceId.toString()) + } + + @Test + fun `starts session on default scope`() { + val combined = fixture.getSut() + + combined.startSession() + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNull(fixture.scope.session) + assertNotNull(fixture.isolationScope.session) + assertNull(fixture.globalScope.session) + } + + @Test + fun `ends session on default scope`() { + val combined = fixture.getSut() + fixture.scope.startSession() + fixture.isolationScope.startSession() + fixture.globalScope.startSession() + + combined.endSession() + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertNotNull(fixture.scope.session) + assertNull(fixture.isolationScope.session) + assertNotNull(fixture.globalScope.session) + } + + @Test + fun `prefers session from current scope`() { + val combined = fixture.getSut() + fixture.scope.startSession() + fixture.isolationScope.startSession() + fixture.globalScope.startSession() + + assertSame(fixture.scope.session, combined.session) + } + + @Test + fun `uses isolation scope session if none on current scope`() { + val combined = fixture.getSut() + fixture.isolationScope.startSession() + fixture.globalScope.startSession() + + assertSame(fixture.isolationScope.session, combined.session) + } + + @Test + fun `uses global scope session if none on current or isolation scope`() { + val combined = fixture.getSut() + fixture.globalScope.startSession() + + assertSame(fixture.globalScope.session, combined.session) + } + + @Test + fun `withSession uses default scope`() { + val combined = fixture.getSut() + fixture.scope.startSession() + fixture.isolationScope.startSession() + fixture.globalScope.startSession() + + var capturedSession: Session? = null + combined.withSession { session -> + capturedSession = session + } + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertSame(fixture.isolationScope.session, capturedSession) + } + + @Test + fun `sets fingerprint on default scope`() { + val combined = fixture.getSut() + combined.fingerprint = listOf("aFingerprint") + + assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType) + assertEquals(0, fixture.scope.fingerprint.size) + assertEquals(1, fixture.isolationScope.fingerprint.size) + assertEquals(0, fixture.globalScope.fingerprint.size) + } + + @Test + fun `prefers fingerprint from current scope`() { + val combined = fixture.getSut() + fixture.scope.fingerprint = listOf("scopeFingerprint") + fixture.isolationScope.fingerprint = listOf("isolationFingerprint") + fixture.globalScope.fingerprint = listOf("globalFingerprint") + + assertEquals(listOf("scopeFingerprint"), combined.fingerprint) + } + + @Test + fun `uses isolation scope fingerprint if current scope does not have one`() { + val combined = fixture.getSut() + fixture.isolationScope.fingerprint = listOf("isolationFingerprint") + fixture.globalScope.fingerprint = listOf("globalFingerprint") + + assertEquals(listOf("isolationFingerprint"), combined.fingerprint) + } + + @Test + fun `uses global scope fingerprint if current and isolation scope do not have one`() { + val combined = fixture.getSut() + fixture.globalScope.fingerprint = listOf("globalFingerprint") + + assertEquals(listOf("globalFingerprint"), combined.fingerprint) + } + + // TODO [HSM] test clone + + private fun createTransaction(name: String, scopes: Scopes? = null): ITransaction { + val scopesToUse = scopes ?: fixture.scopes + return SentryTracer(TransactionContext(name, "op", TracesSamplingDecision(true)), scopesToUse).also { + it.startChild("${name}Span") + } + } + + private fun createAttachment(name: String): Attachment { + return Attachment("a".toByteArray(), name, "image/png", false) + } + class TestEventProcessor(val orderNumber: Long?) : EventProcessor { override fun getOrder() = orderNumber } From 840c1949e5a5f2c60915c1cf005453c7e906d6a8 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 19 Apr 2024 08:48:16 +0200 Subject: [PATCH 41/57] Fix combined contexts --- .../android/core/AnrV2EventProcessorTest.kt | 2 +- sentry/api/sentry.api | 25 +- .../java/io/sentry/CombinedContextsView.java | 65 +- .../java/io/sentry/protocol/Contexts.java | 70 ++- .../io/sentry/CombinedContextsViewTest.kt | 568 ++++++++++++++++++ .../java/io/sentry/CombinedScopeViewTest.kt | 88 ++- .../CombinedContextsViewSerializationTest.kt | 89 +++ 7 files changed, 887 insertions(+), 20 deletions(-) create mode 100644 sentry/src/test/java/io/sentry/CombinedContextsViewTest.kt create mode 100644 sentry/src/test/java/io/sentry/protocol/CombinedContextsViewSerializationTest.kt diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2EventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2EventProcessorTest.kt index b581856fe0..2f34b4d2e0 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2EventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2EventProcessorTest.kt @@ -169,7 +169,7 @@ class AnrV2EventProcessorTest { assertNull(processed.platform) assertNull(processed.exceptions) - assertEquals(emptyMap(), processed.contexts) + assertTrue(processed.contexts.isEmpty) } @Test diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index d87ff71ccc..32d530a5a1 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -210,6 +210,9 @@ public final class io/sentry/CheckInStatus : java/lang/Enum { public final class io/sentry/CombinedContextsView : io/sentry/protocol/Contexts { public fun (Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/ScopeType;)V + public fun containsKey (Ljava/lang/Object;)Z + public fun entrySet ()Ljava/util/Set; + public fun get (Ljava/lang/Object;)Ljava/lang/Object; public fun getApp ()Lio/sentry/protocol/App; public fun getBrowser ()Lio/sentry/protocol/Browser; public fun getDevice ()Lio/sentry/protocol/Device; @@ -217,7 +220,12 @@ public final class io/sentry/CombinedContextsView : io/sentry/protocol/Contexts public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem; public fun getResponse ()Lio/sentry/protocol/Response; public fun getRuntime ()Lio/sentry/protocol/SentryRuntime; + public fun getSize ()I public fun getTrace ()Lio/sentry/SpanContext; + public fun isEmpty ()Z + public fun keys ()Ljava/util/Enumeration; + public fun put (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; + public fun remove (Ljava/lang/Object;)Ljava/lang/Object; public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V public fun setApp (Lio/sentry/protocol/App;)V public fun setBrowser (Lio/sentry/protocol/Browser;)V @@ -227,6 +235,7 @@ public final class io/sentry/CombinedContextsView : io/sentry/protocol/Contexts public fun setResponse (Lio/sentry/protocol/Response;)V public fun setRuntime (Lio/sentry/protocol/SentryRuntime;)V public fun setTrace (Lio/sentry/SpanContext;)V + public fun size ()I public fun withResponse (Lio/sentry/util/HintUtils$SentryConsumer;)V } @@ -4162,9 +4171,13 @@ public final class io/sentry/protocol/Browser$JsonKeys { public fun ()V } -public class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMap, io/sentry/JsonSerializable { +public class io/sentry/protocol/Contexts : io/sentry/JsonSerializable { public fun ()V public fun (Lio/sentry/protocol/Contexts;)V + public fun containsKey (Ljava/lang/Object;)Z + public fun entrySet ()Ljava/util/Set; + public fun equals (Ljava/lang/Object;)Z + public fun get (Ljava/lang/Object;)Ljava/lang/Object; public fun getApp ()Lio/sentry/protocol/App; public fun getBrowser ()Lio/sentry/protocol/Browser; public fun getDevice ()Lio/sentry/protocol/Device; @@ -4172,8 +4185,17 @@ public class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMa public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem; public fun getResponse ()Lio/sentry/protocol/Response; public fun getRuntime ()Lio/sentry/protocol/SentryRuntime; + public fun getSize ()I public fun getTrace ()Lio/sentry/SpanContext; + public fun hashCode ()I + public fun isEmpty ()Z + public fun keys ()Ljava/util/Enumeration; + public fun put (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; + public fun putAll (Lio/sentry/protocol/Contexts;)V + public fun putAll (Ljava/util/Map;)V + public fun remove (Ljava/lang/Object;)Ljava/lang/Object; public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V + public fun set (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; public fun setApp (Lio/sentry/protocol/App;)V public fun setBrowser (Lio/sentry/protocol/Browser;)V public fun setDevice (Lio/sentry/protocol/Device;)V @@ -4182,6 +4204,7 @@ public class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMa public fun setResponse (Lio/sentry/protocol/Response;)V public fun setRuntime (Lio/sentry/protocol/SentryRuntime;)V public fun setTrace (Lio/sentry/SpanContext;)V + public fun size ()I public fun withResponse (Lio/sentry/util/HintUtils$SentryConsumer;)V } diff --git a/sentry/src/main/java/io/sentry/CombinedContextsView.java b/sentry/src/main/java/io/sentry/CombinedContextsView.java index 3362a8ea1e..3dd7428975 100644 --- a/sentry/src/main/java/io/sentry/CombinedContextsView.java +++ b/sentry/src/main/java/io/sentry/CombinedContextsView.java @@ -10,6 +10,9 @@ import io.sentry.protocol.SentryRuntime; import io.sentry.util.HintUtils; import java.io.IOException; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -203,12 +206,72 @@ public void setResponse(@NotNull Response response) { getDefaultContexts().setResponse(response); } + @Override + public int size() { + return mergeContexts().size(); + } + + @Override + public int getSize() { + return size(); + } + + @Override + public boolean isEmpty() { + return globalContexts.isEmpty() && isolationContexts.isEmpty() && currentContexts.isEmpty(); + } + + @Override + public boolean containsKey(final @NotNull Object key) { + return globalContexts.containsKey(key) + || isolationContexts.containsKey(key) + || currentContexts.containsKey(key); + } + + @Override + public @Nullable Object get(final @NotNull Object key) { + final @Nullable Object current = currentContexts.get(key); + if (current != null) { + return current; + } + final @Nullable Object isolation = isolationContexts.get(key); + if (isolation != null) { + return isolation; + } + return globalContexts.get(key); + } + + @Override + public @Nullable Object put(final @NotNull String key, final @Nullable Object value) { + return getDefaultContexts().put(key, value); + } + + @Override + public @Nullable Object remove(final @NotNull Object key) { + // TODO [HSM] should this remove from all contexts? + return getDefaultContexts().remove(key); + } + + @Override + public @NotNull Enumeration keys() { + return mergeContexts().keys(); + } + + @Override + public @NotNull Set> entrySet() { + return mergeContexts().entrySet(); + } + @Override public void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException { + mergeContexts().serialize(writer, logger); + } + + private @NotNull Contexts mergeContexts() { final @NotNull Contexts allContexts = new Contexts(); allContexts.putAll(globalContexts); allContexts.putAll(isolationContexts); allContexts.putAll(currentContexts); - allContexts.serialize(writer, logger); + return allContexts; } } diff --git a/sentry/src/main/java/io/sentry/protocol/Contexts.java b/sentry/src/main/java/io/sentry/protocol/Contexts.java index aa157e665e..4e4c7c5c90 100644 --- a/sentry/src/main/java/io/sentry/protocol/Contexts.java +++ b/sentry/src/main/java/io/sentry/protocol/Contexts.java @@ -12,16 +12,21 @@ import io.sentry.vendor.gson.stream.JsonToken; import java.io.IOException; import java.util.Collections; +import java.util.Enumeration; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @Open -public class Contexts extends ConcurrentHashMap implements JsonSerializable { +public class Contexts implements JsonSerializable { private static final long serialVersionUID = 252445813254943011L; + private final @NotNull ConcurrentHashMap internalStorage = + new ConcurrentHashMap(); + /** Response lock, Ops should be atomic */ private final @NotNull Object responseLock = new Object(); @@ -140,6 +145,69 @@ public void setResponse(final @NotNull Response response) { } } + public int size() { + return internalStorage.size(); + } + + public int getSize() { + return size(); + } + + public boolean isEmpty() { + return internalStorage.isEmpty(); + } + + public boolean containsKey(final @NotNull Object key) { + return internalStorage.containsKey(key); + } + + public @Nullable Object get(final @NotNull Object key) { + return internalStorage.get(key); + } + + public @Nullable Object put(final @NotNull String key, final @Nullable Object value) { + return internalStorage.put(key, value); + } + + public @Nullable Object set(final @NotNull String key, final @Nullable Object value) { + return put(key, value); + } + + public @Nullable Object remove(final @NotNull Object key) { + return internalStorage.remove(key); + } + + public @NotNull Enumeration keys() { + return internalStorage.keys(); + } + + public @NotNull Set> entrySet() { + return internalStorage.entrySet(); + } + + public void putAll(Map m) { + internalStorage.putAll(m); + } + + public void putAll(final @NotNull Contexts contexts) { + internalStorage.putAll(contexts.internalStorage); + } + + @Override + public boolean equals(Object obj) { + if (obj != null && obj instanceof Contexts) { + final @NotNull Contexts otherContexts = (Contexts) obj; + return internalStorage.equals(otherContexts.internalStorage); + } + + return false; + } + + @Override + public int hashCode() { + return internalStorage.hashCode(); + } + // region json @Override diff --git a/sentry/src/test/java/io/sentry/CombinedContextsViewTest.kt b/sentry/src/test/java/io/sentry/CombinedContextsViewTest.kt new file mode 100644 index 0000000000..624ca2417d --- /dev/null +++ b/sentry/src/test/java/io/sentry/CombinedContextsViewTest.kt @@ -0,0 +1,568 @@ +package io.sentry + +import io.sentry.protocol.App +import io.sentry.protocol.Browser +import io.sentry.protocol.Contexts +import io.sentry.protocol.Device +import io.sentry.protocol.Gpu +import io.sentry.protocol.OperatingSystem +import io.sentry.protocol.Response +import io.sentry.protocol.SentryRuntime +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class CombinedContextsViewTest { + + private class Fixture { + lateinit var current: Contexts + lateinit var isolation: Contexts + lateinit var global: Contexts + + fun getSut(): CombinedContextsView { + current = Contexts() + isolation = Contexts() + global = Contexts() + + return CombinedContextsView(global, isolation, current, ScopeType.ISOLATION) + } + } + + private val fixture = Fixture() + + @Test + fun `uses default context CURRENT`() { + fixture.getSut() + val combined = CombinedContextsView(fixture.global, fixture.isolation, fixture.current, ScopeType.CURRENT) + combined.trace = SpanContext("some") + assertEquals("some", fixture.current.trace?.op) + } + + @Test + fun `uses default context ISOLATION`() { + fixture.getSut() + val combined = CombinedContextsView(fixture.global, fixture.isolation, fixture.current, ScopeType.ISOLATION) + combined.trace = SpanContext("some") + assertEquals("some", fixture.isolation.trace?.op) + } + + @Test + fun `uses default context GLOBAL`() { + fixture.getSut() + val combined = CombinedContextsView(fixture.global, fixture.isolation, fixture.current, ScopeType.GLOBAL) + combined.trace = SpanContext("some") + assertEquals("some", fixture.global.trace?.op) + } + + @Test + fun `prefers trace from current context`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + fixture.isolation.trace = SpanContext("isolation") + fixture.global.trace = SpanContext("global") + + assertEquals("current", combined.trace?.op) + } + + @Test + fun `uses isolation trace if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.trace = SpanContext("isolation") + fixture.global.trace = SpanContext("global") + + assertEquals("isolation", combined.trace?.op) + } + + @Test + fun `uses global trace if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.trace = SpanContext("global") + + assertEquals("global", combined.trace?.op) + } + + @Test + fun `sets trace on default context`() { + val combined = fixture.getSut() + combined.trace = SpanContext("some") + + assertNull(fixture.current.trace) + assertEquals("some", fixture.isolation.trace?.op) + assertNull(fixture.global.trace) + } + + @Test + fun `prefers app from current context`() { + val combined = fixture.getSut() + fixture.current.setApp(App().also { it.appName = "current" }) + fixture.isolation.setApp(App().also { it.appName = "isolation" }) + fixture.global.setApp(App().also { it.appName = "global" }) + + assertEquals("current", combined.app?.appName) + } + + @Test + fun `uses isolation app if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setApp(App().also { it.appName = "isolation" }) + fixture.global.setApp(App().also { it.appName = "global" }) + + assertEquals("isolation", combined.app?.appName) + } + + @Test + fun `uses global app if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setApp(App().also { it.appName = "global" }) + + assertEquals("global", combined.app?.appName) + } + + @Test + fun `sets app on default context`() { + val combined = fixture.getSut() + combined.setApp(App().also { it.appName = "some" }) + + assertNull(fixture.current.app) + assertEquals("some", fixture.isolation.app?.appName) + assertNull(fixture.global.app) + } + + @Test + fun `prefers browser from current context`() { + val combined = fixture.getSut() + fixture.current.setBrowser(Browser().also { it.name = "current" }) + fixture.isolation.setBrowser(Browser().also { it.name = "isolation" }) + fixture.global.setBrowser(Browser().also { it.name = "global" }) + + assertEquals("current", combined.browser?.name) + } + + @Test + fun `uses isolation browser if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setBrowser(Browser().also { it.name = "isolation" }) + fixture.global.setBrowser(Browser().also { it.name = "global" }) + + assertEquals("isolation", combined.browser?.name) + } + + @Test + fun `uses global browser if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setBrowser(Browser().also { it.name = "global" }) + + assertEquals("global", combined.browser?.name) + } + + @Test + fun `sets browser on default context`() { + val combined = fixture.getSut() + combined.setBrowser(Browser().also { it.name = "some" }) + + assertNull(fixture.current.browser) + assertEquals("some", fixture.isolation.browser?.name) + assertNull(fixture.global.browser) + } + + @Test + fun `prefers device from current context`() { + val combined = fixture.getSut() + fixture.current.setDevice(Device().also { it.name = "current" }) + fixture.isolation.setDevice(Device().also { it.name = "isolation" }) + fixture.global.setDevice(Device().also { it.name = "global" }) + + assertEquals("current", combined.device?.name) + } + + @Test + fun `uses isolation device if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setDevice(Device().also { it.name = "isolation" }) + fixture.global.setDevice(Device().also { it.name = "global" }) + + assertEquals("isolation", combined.device?.name) + } + + @Test + fun `uses global device if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setDevice(Device().also { it.name = "global" }) + + assertEquals("global", combined.device?.name) + } + + @Test + fun `sets device on default context`() { + val combined = fixture.getSut() + combined.setDevice(Device().also { it.name = "some" }) + + assertNull(fixture.current.device) + assertEquals("some", fixture.isolation.device?.name) + assertNull(fixture.global.device) + } + + @Test + fun `prefers operatingSystem from current context`() { + val combined = fixture.getSut() + fixture.current.setOperatingSystem(OperatingSystem().also { it.name = "current" }) + fixture.isolation.setOperatingSystem(OperatingSystem().also { it.name = "isolation" }) + fixture.global.setOperatingSystem(OperatingSystem().also { it.name = "global" }) + + assertEquals("current", combined.operatingSystem?.name) + } + + @Test + fun `uses isolation operatingSystem if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setOperatingSystem(OperatingSystem().also { it.name = "isolation" }) + fixture.global.setOperatingSystem(OperatingSystem().also { it.name = "global" }) + + assertEquals("isolation", combined.operatingSystem?.name) + } + + @Test + fun `uses global operatingSystem if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setOperatingSystem(OperatingSystem().also { it.name = "global" }) + + assertEquals("global", combined.operatingSystem?.name) + } + + @Test + fun `sets operatingSystem on default context`() { + val combined = fixture.getSut() + combined.setOperatingSystem(OperatingSystem().also { it.name = "some" }) + + assertNull(fixture.current.operatingSystem) + assertEquals("some", fixture.isolation.operatingSystem?.name) + assertNull(fixture.global.operatingSystem) + } + + @Test + fun `prefers runtime from current context`() { + val combined = fixture.getSut() + fixture.current.setRuntime(SentryRuntime().also { it.name = "current" }) + fixture.isolation.setRuntime(SentryRuntime().also { it.name = "isolation" }) + fixture.global.setRuntime(SentryRuntime().also { it.name = "global" }) + + assertEquals("current", combined.runtime?.name) + } + + @Test + fun `uses isolation runtime if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setRuntime(SentryRuntime().also { it.name = "isolation" }) + fixture.global.setRuntime(SentryRuntime().also { it.name = "global" }) + + assertEquals("isolation", combined.runtime?.name) + } + + @Test + fun `uses global runtime if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setRuntime(SentryRuntime().also { it.name = "global" }) + + assertEquals("global", combined.runtime?.name) + } + + @Test + fun `sets runtime on default context`() { + val combined = fixture.getSut() + combined.setRuntime(SentryRuntime().also { it.name = "some" }) + + assertNull(fixture.current.runtime) + assertEquals("some", fixture.isolation.runtime?.name) + assertNull(fixture.global.runtime) + } + + @Test + fun `prefers gpu from current context`() { + val combined = fixture.getSut() + fixture.current.setGpu(Gpu().also { it.name = "current" }) + fixture.isolation.setGpu(Gpu().also { it.name = "isolation" }) + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertEquals("current", combined.gpu?.name) + } + + @Test + fun `uses isolation gpu if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setGpu(Gpu().also { it.name = "isolation" }) + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertEquals("isolation", combined.gpu?.name) + } + + @Test + fun `uses global gpu if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertEquals("global", combined.gpu?.name) + } + + @Test + fun `sets gpu on default context`() { + val combined = fixture.getSut() + combined.setGpu(Gpu().also { it.name = "some" }) + + assertNull(fixture.current.gpu) + assertEquals("some", fixture.isolation.gpu?.name) + assertNull(fixture.global.gpu) + } + + @Test + fun `prefers response from current context`() { + val combined = fixture.getSut() + fixture.current.setResponse(Response().also { it.cookies = "current" }) + fixture.isolation.setResponse(Response().also { it.cookies = "isolation" }) + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + assertEquals("current", combined.response?.cookies) + } + + @Test + fun `uses isolation response if current context does not have it`() { + val combined = fixture.getSut() + fixture.isolation.setResponse(Response().also { it.cookies = "isolation" }) + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + assertEquals("isolation", combined.response?.cookies) + } + + @Test + fun `uses global response if current and isolation context do not have it`() { + val combined = fixture.getSut() + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + assertEquals("global", combined.response?.cookies) + } + + @Test + fun `sets response on default context`() { + val combined = fixture.getSut() + combined.setResponse(Response().also { it.cookies = "some" }) + + assertNull(fixture.current.response) + assertEquals("some", fixture.isolation.response?.cookies) + assertNull(fixture.global.response) + } + + @Test + fun `withResponse is executed on current if present`() { + val combined = fixture.getSut() + fixture.current.setResponse(Response().also { it.cookies = "current" }) + fixture.isolation.setResponse(Response().also { it.cookies = "isolation" }) + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + combined.withResponse { response -> + response.cookies = "updated" + } + + assertEquals("updated", fixture.current.response?.cookies) + assertEquals("isolation", fixture.isolation.response?.cookies) + assertEquals("global", fixture.global.response?.cookies) + } + + @Test + fun `withResponse is executed on isolation if current not present`() { + val combined = fixture.getSut() + fixture.isolation.setResponse(Response().also { it.cookies = "isolation" }) + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + combined.withResponse { response -> + response.cookies = "updated" + } + + assertNull(fixture.current.response) + assertEquals("updated", fixture.isolation.response?.cookies) + assertEquals("global", fixture.global.response?.cookies) + } + + @Test + fun `withResponse is executed on global if current and isoaltion not present`() { + val combined = fixture.getSut() + fixture.global.setResponse(Response().also { it.cookies = "global" }) + + combined.withResponse { response -> + response.cookies = "updated" + } + + assertNull(fixture.current.response) + assertNull(fixture.isolation.response) + assertEquals("updated", fixture.global.response?.cookies) + } + + @Test + fun `withResponse is executed on default if not present anywhere`() { + val combined = fixture.getSut() + + combined.withResponse { response -> + response.cookies = "updated" + } + + assertNull(fixture.current.response) + assertEquals("updated", fixture.isolation.response?.cookies) + assertNull(fixture.global.response) + } + + @Test + fun `size combines contexts`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + fixture.isolation.setApp(App().also { it.appName = "isolation" }) + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertEquals(3, combined.size) + } + + @Test + fun `size considers overrides`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + fixture.isolation.trace = SpanContext("isolation") + fixture.global.trace = SpanContext("global") + + assertEquals(1, combined.size) + } + + @Test + fun `isEmpty`() { + val combined = fixture.getSut() + assertTrue(combined.isEmpty) + } + + @Test + fun `isNotEmpty if current has value`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + + assertFalse(combined.isEmpty) + } + + @Test + fun `isNotEmpty if isolation has value`() { + val combined = fixture.getSut() + fixture.isolation.setApp(App().also { it.appName = "isolation" }) + + assertFalse(combined.isEmpty) + } + + @Test + fun `isNotEmpty if global has value`() { + val combined = fixture.getSut() + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertFalse(combined.isEmpty) + } + + @Test + fun `containsKey false`() { + val combined = fixture.getSut() + assertFalse(combined.containsKey("trace")) + } + + @Test + fun `containsKey current`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + assertTrue(combined.containsKey("trace")) + } + + @Test + fun `containsKey isolation`() { + val combined = fixture.getSut() + fixture.isolation.trace = SpanContext("isolation") + assertTrue(combined.containsKey("trace")) + } + + @Test + fun `containsKey global`() { + val combined = fixture.getSut() + fixture.global.trace = SpanContext("global") + assertTrue(combined.containsKey("trace")) + } + + @Test + fun `keys combines contexts`() { + val combined = fixture.getSut() + fixture.current.trace = SpanContext("current") + fixture.isolation.setApp(App().also { it.appName = "isolation" }) + fixture.global.setGpu(Gpu().also { it.name = "global" }) + + assertEquals(listOf("app", "gpu", "trace"), combined.keys().toList().sorted()) + } + + @Test + fun `entrySet combines contexts`() { + val combined = fixture.getSut() + val trace = SpanContext("current") + fixture.current.trace = trace + val app = App().also { it.appName = "isolation" } + fixture.isolation.setApp(app) + val gpu = Gpu().also { it.name = "global" } + fixture.global.setGpu(gpu) + + val entrySet = combined.entrySet() + assertEquals(3, entrySet.size) + assertNotNull(entrySet.firstOrNull { it.key == "trace" && it.value == trace }) + assertNotNull(entrySet.firstOrNull { it.key == "app" && it.value == app }) + assertNotNull(entrySet.firstOrNull { it.key == "gpu" && it.value == gpu }) + } + + @Test + fun `get prefers current`() { + val combined = fixture.getSut() + fixture.current.put("test", "current") + fixture.isolation.put("test", "isolation") + fixture.global.put("test", "global") + + assertEquals("current", combined.get("test")) + } + + @Test + fun `get uses isolation if not in current`() { + val combined = fixture.getSut() + fixture.isolation.put("test", "isolation") + fixture.global.put("test", "global") + + assertEquals("isolation", combined.get("test")) + } + + @Test + fun `get uses global if not in current or isolation`() { + val combined = fixture.getSut() + fixture.global.put("test", "global") + + assertEquals("global", combined.get("test")) + } + + @Test + fun `put stores in default context`() { + val combined = fixture.getSut() + combined.put("test", "aValue") + + assertNull(fixture.current.get("test")) + assertEquals("aValue", fixture.isolation.get("test")) + assertNull(fixture.global.get("test")) + } + + @Test + fun `remove removes from default context`() { + val combined = fixture.getSut() + fixture.current.put("test", "current") + fixture.isolation.put("test", "isolation") + fixture.global.put("test", "global") + + combined.remove("test") + + assertEquals("current", fixture.current.get("test")) + assertNull(fixture.isolation.get("test")) + assertEquals("global", fixture.global.get("test")) + } +} diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index 11725947cb..65a1bc49f7 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -1,5 +1,6 @@ package io.sentry +import io.sentry.protocol.Device import io.sentry.protocol.Request import io.sentry.protocol.SentryId import io.sentry.protocol.User @@ -640,26 +641,81 @@ class CombinedScopeViewTest { assertEquals("globalValue", fixture.globalScope.extras["someExtra"]) } - // TODO [HSM] CombinedContextsView does not override all map methods, leading to unwanted behaviour - // we should probably no longer extend Map but instead keep an internal map - // and offer a toMap function or similar -// @Test -// fun `combines context from all scopes`() { -// val combined = fixture.getSut() -// fixture.scope.setContexts("scopeContext", "scopeValue") -// fixture.isolationScope.setContexts("isolationContext", "isolationValue") -// fixture.globalScope.setContexts("globalContext", "globalValue") -// -// val contexts = combined.contexts -// assertEquals("scopeValue", contexts["scopeContext"]) -// } + @Test + fun `combines context from all scopes`() { + val combined = fixture.getSut() + fixture.scope.setContexts("scopeContext", "scopeValue") + fixture.isolationScope.setContexts("isolationContext", "isolationValue") + fixture.globalScope.setContexts("globalContext", "globalValue") - // TODO [HSM] test all setContext methods + val contexts = combined.contexts + assertEquals(mapOf("value" to "scopeValue"), contexts["scopeContext"]) + } + + @Test + fun `current scope context overrides context of other scopes`() { + val combined = fixture.getSut() + fixture.scope.setContexts("someContext", "scopeValue") + fixture.isolationScope.setContexts("someContext", "isolationValue") + fixture.globalScope.setContexts("someContext", "globalValue") + + val contexts = combined.contexts + assertEquals(mapOf("value" to "scopeValue"), contexts["someContext"]) + } + + @Test + fun `isolation scope context overrides global context`() { + val combined = fixture.getSut() + fixture.isolationScope.setContexts("someContext", "isolationValue") + fixture.globalScope.setContexts("someContext", "globalValue") + + val contexts = combined.contexts + assertEquals(mapOf("value" to "isolationValue"), contexts["someContext"]) + } + + @Test + fun `setContexts writes to default scope`() { + val combined = fixture.getSut() + combined.setContexts("aString", "stringValue") + combined.setContexts("aChar", 'c') + combined.setContexts("aNumber", 1) + combined.setContexts("someObject", Device().also { it.brand = "someDeviceBrand" }) + combined.setContexts("someArray", arrayOf("a", "b")) + combined.setContexts("someList", listOf("c", "d", "e")) - // TODO [HSM] fingerprint tests (discuss how it should behave first) + assertNull(fixture.scope.contexts["aString"]) + assertNull(fixture.scope.contexts["aChar"]) + assertNull(fixture.scope.contexts["aNumber"]) + assertNull(fixture.scope.contexts["someObject"]) + assertNull(fixture.scope.contexts["someArray"]) + assertNull(fixture.scope.contexts["someList"]) + + assertEquals(mapOf("value" to "stringValue"), fixture.isolationScope.contexts["aString"]) + assertEquals(mapOf("value" to 'c'), fixture.isolationScope.contexts["aChar"]) + assertEquals(mapOf("value" to 1), fixture.isolationScope.contexts["aNumber"]) + assertEquals("someDeviceBrand", (fixture.isolationScope.contexts["someObject"] as? Device)?.brand) + val arrayValue = (fixture.isolationScope.contexts["someArray"] as? Map)?.get("value") as? Array + assertEquals(2, arrayValue?.size) + assertEquals("a", arrayValue?.get(0)) + assertEquals("b", arrayValue?.get(1)) + val listValue = (fixture.isolationScope.contexts["someList"] as? Map)?.get("value") as? List + assertEquals(3, listValue?.size) + assertEquals("c", listValue?.get(0)) + assertEquals("d", listValue?.get(1)) + assertEquals("e", listValue?.get(2)) + + assertNull(fixture.globalScope.contexts["aString"]) + assertNull(fixture.globalScope.contexts["aChar"]) + assertNull(fixture.globalScope.contexts["aNumber"]) + assertNull(fixture.globalScope.contexts["someObject"]) + assertNull(fixture.globalScope.contexts["someArray"]) + assertNull(fixture.globalScope.contexts["someList"]) + } + + // TODO [HSM] test all setContext methods @Test - fun `combines attachments Æ’rom all scopes`() { + fun `combines attachments from all scopes`() { val combined = fixture.getSut() fixture.scope.addAttachment(createAttachment("scopeAttachment.png")) diff --git a/sentry/src/test/java/io/sentry/protocol/CombinedContextsViewSerializationTest.kt b/sentry/src/test/java/io/sentry/protocol/CombinedContextsViewSerializationTest.kt new file mode 100644 index 0000000000..87cb226abc --- /dev/null +++ b/sentry/src/test/java/io/sentry/protocol/CombinedContextsViewSerializationTest.kt @@ -0,0 +1,89 @@ +package io.sentry.protocol + +import io.sentry.CombinedContextsView +import io.sentry.ILogger +import io.sentry.JsonObjectWriter +import io.sentry.ScopeType +import org.junit.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import kotlin.test.assertEquals + +class CombinedContextsViewSerializationTest { + + class Fixture { + val logger = mock() + + fun getSut(): CombinedContextsView { + val current = Contexts() + val isolation = Contexts() + val global = Contexts() + val combined = CombinedContextsView(global, isolation, current, ScopeType.ISOLATION) + + current.setApp(AppSerializationTest.Fixture().getSut()) + current.setBrowser(BrowserSerializationTest.Fixture().getSut()) + current.trace = SpanContextSerializationTest.Fixture().getSut() + + isolation.setDevice(DeviceSerializationTest.Fixture().getSut()) + isolation.setOperatingSystem(OperatingSystemSerializationTest.Fixture().getSut()) + isolation.setResponse(ResponseSerializationTest.Fixture().getSut()) + + global.setRuntime(SentryRuntimeSerializationTest.Fixture().getSut()) + global.setGpu(GpuSerializationTest.Fixture().getSut()) + + return combined + } + } + private val fixture = Fixture() + + @Test + fun serialize() { + val expected = SerializationUtils.sanitizedFile("json/contexts.json") + val actual = SerializationUtils.serializeToString(fixture.getSut(), fixture.logger) + + assertEquals(expected, actual) + } + + @Test + fun serializeUnknownEntry() { + val sut = fixture.getSut() + sut["fixture-key"] = "fixture-value" + + val writer = mock().apply { + whenever(name(any())).thenReturn(this) + } + sut.serialize(writer, fixture.logger) + + verify(writer).name("fixture-key") + verify(writer).value(fixture.logger, "fixture-value") + } + + @Test + fun deserialize() { + val expectedJson = SerializationUtils.sanitizedFile("json/contexts.json") + val actual = SerializationUtils.deserializeJson( + expectedJson, + Contexts.Deserializer(), + fixture.logger + ) + val actualJson = SerializationUtils.serializeToString(actual, fixture.logger) + + assertEquals(expectedJson, actualJson) + } + + @Test + fun deserializeUnknownEntry() { + val sut = fixture.getSut() + sut["fixture-key"] = "fixture-value" + val serialized = SerializationUtils.serializeToString(sut, fixture.logger) + val deserialized = SerializationUtils.deserializeJson( + serialized, + Contexts.Deserializer(), + fixture.logger + ) + + assertEquals("fixture-value", deserialized["fixture-key"]) + } +} From ab1c3a6ccd5557c1620d70606f659fa317bd0476 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 19 Apr 2024 13:48:36 +0200 Subject: [PATCH 42/57] Use combined scopes for cross platform --- .../main/java/io/sentry/android/core/InternalSentrySdk.java | 3 ++- .../java/io/sentry/android/core/InternalSentrySdkTest.kt | 6 +++++- sentry/src/main/java/io/sentry/CombinedScopeView.java | 6 +++--- sentry/src/main/java/io/sentry/HubAdapter.java | 1 - sentry/src/main/java/io/sentry/ScopeType.java | 2 -- sentry/src/main/java/io/sentry/Scopes.java | 4 +--- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index 3170a4f1ec..84a2ec4d38 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -9,6 +9,7 @@ import io.sentry.IScopes; import io.sentry.ISerializer; import io.sentry.ObjectWriter; +import io.sentry.ScopeType; import io.sentry.ScopesAdapter; import io.sentry.SentryEnvelope; import io.sentry.SentryEnvelopeItem; @@ -44,9 +45,9 @@ public final class InternalSentrySdk { @Nullable public static IScope getCurrentScope() { final @NotNull AtomicReference scopeRef = new AtomicReference<>(); - // TODO [HSM] should this retrieve combined scope? ScopesAdapter.getInstance() .configureScope( + ScopeType.COMBINED, scope -> { scopeRef.set(scope.clone()); }); diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt index e64cbe227e..f4f2c696d3 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt @@ -7,6 +7,7 @@ import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.IScope import io.sentry.Scope +import io.sentry.ScopeType import io.sentry.Scopes import io.sentry.Sentry import io.sentry.SentryEnvelope @@ -138,6 +139,9 @@ class InternalSentrySdkTest { ) // TODO [HSM] add breadcrumbs to all scopes and assert they are there Sentry.addBreadcrumb("test") + Sentry.configureScope(ScopeType.CURRENT) { scope -> scope.addBreadcrumb(Breadcrumb("currentBreadcrumb")) } + Sentry.configureScope(ScopeType.ISOLATION) { scope -> scope.addBreadcrumb(Breadcrumb("isolationBreadcrumb")) } + Sentry.configureScope(ScopeType.GLOBAL) { scope -> scope.addBreadcrumb(Breadcrumb("globalBreadcrumb")) } // when the clone is modified val clonedScope = InternalSentrySdk.getCurrentScope()!! @@ -145,7 +149,7 @@ class InternalSentrySdkTest { // then modifications should not be reflected Sentry.configureScope { scope -> - assertEquals(1, scope.breadcrumbs.size) + assertEquals(3, scope.breadcrumbs.size) } } diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 44a87da0b1..66e557e16d 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -318,6 +318,8 @@ IScope getSpecificScope(final @Nullable ScopeType scopeType) { return isolationScope; case GLOBAL: return globalScope; + case COMBINED: + return this; default: break; } @@ -431,8 +433,7 @@ public void setPropagationContext(@NotNull PropagationContext propagationContext @Override public @NotNull IScope clone() { - // TODO [HSM] just return a new CombinedScopeView with forked scope? - return getDefaultWriteScope().clone(); + return new CombinedScopeView(globalScope, isolationScope.clone(), scope.clone()); } @Override @@ -454,7 +455,6 @@ public void bindClient(@NotNull ISentryClient client) { @Override public @NotNull ISentryClient getClient() { - // TODO [HSM] checking for noop here doesn't allow disabling via client, is that ok? final @Nullable ISentryClient current = scope.getClient(); if (!(current instanceof NoOpSentryClient)) { return current; diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index df1a7aa661..266770ddce 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -215,7 +215,6 @@ public void flush(long timeoutMillis) { @Override public @NotNull ISentryLifecycleToken makeCurrent() { - // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } diff --git a/sentry/src/main/java/io/sentry/ScopeType.java b/sentry/src/main/java/io/sentry/ScopeType.java index 3815cf2081..513b92115d 100644 --- a/sentry/src/main/java/io/sentry/ScopeType.java +++ b/sentry/src/main/java/io/sentry/ScopeType.java @@ -4,7 +4,5 @@ public enum ScopeType { CURRENT, ISOLATION, GLOBAL, - - // TODO [HSM] do we need a combined as well so configureScope COMBINED; } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 538c192304..5b3d0e672b 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -646,9 +646,7 @@ public void withScope(final @NotNull ScopeCallback callback) { } else { final @NotNull IScopes forkedScopes = forkedCurrentScope("withScope"); - // TODO [HSM] should forkedScopes be made current inside callback? - // TODO [HSM] forkedScopes.makeCurrent()? - try { + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { callback.run(forkedScopes.getScope()); } catch (Throwable e) { getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); From 23506c5b0e68fbde4e77feb6466856239ff10f22 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 23 Apr 2024 12:34:36 +0200 Subject: [PATCH 43/57] Changes according to reviews of previous PRs --- .../android/core/InternalSentrySdkTest.kt | 1 - .../io/sentry/kotlin/SentryContextTest.kt | 22 ++++--- sentry-quartz/api/sentry-quartz.api | 2 +- .../io/sentry/quartz/SentryJobListener.java | 9 +-- .../api/sentry-servlet-jakarta.api | 2 +- .../jakarta/SentryServletRequestListener.java | 9 +-- .../SentryServletRequestListenerTest.kt | 11 ++-- sentry-servlet/api/sentry-servlet.api | 2 +- .../servlet/SentryServletRequestListener.java | 9 +-- .../SentryServletRequestListenerTest.kt | 11 ++-- .../spring/jakarta/SentrySpringFilter.java | 25 ++++---- .../spring/jakarta/SentryTaskDecorator.java | 10 ++-- .../jakarta/checkin/SentryCheckInAdvice.java | 37 ++++++------ .../tracing/SentryTransactionAdvice.java | 48 +++++++-------- .../spring/jakarta/webflux/ReactorUtils.java | 8 +-- .../spring/jakarta/SentryCheckInAdviceTest.kt | 29 ++++++---- .../spring/jakarta/SentrySpringFilterTest.kt | 11 +++- .../tracing/SentryTransactionAdviceTest.kt | 6 +- .../io/sentry/spring/SentrySpringFilter.java | 25 ++++---- .../io/sentry/spring/SentryTaskDecorator.java | 12 ++-- .../spring/checkin/SentryCheckInAdvice.java | 37 ++++++------ .../tracing/SentryTransactionAdvice.java | 48 +++++++-------- .../sentry/spring/SentryCheckInAdviceTest.kt | 27 ++++++--- .../sentry/spring/SentrySpringFilterTest.kt | 11 +++- .../tracing/SentryTransactionAdviceTest.kt | 6 +- sentry/api/sentry.api | 10 +++- .../src/main/java/io/sentry/Breadcrumb.java | 6 +- .../java/io/sentry/CombinedContextsView.java | 2 +- sentry/src/main/java/io/sentry/Hub.java | 25 ++++++++ .../src/main/java/io/sentry/HubAdapter.java | 9 +++ .../main/java/io/sentry/HubScopesWrapper.java | 9 +++ sentry/src/main/java/io/sentry/IScopes.java | 38 ++++++++++-- .../main/java/io/sentry/IScopesStorage.java | 2 + sentry/src/main/java/io/sentry/NoOpHub.java | 9 +++ .../src/main/java/io/sentry/NoOpScopes.java | 9 +++ sentry/src/main/java/io/sentry/Scopes.java | 58 ++++++++++++------- .../main/java/io/sentry/ScopesAdapter.java | 10 +++- sentry/src/main/java/io/sentry/Sentry.java | 24 +++++++- .../main/java/io/sentry/SentryWrapper.java | 44 ++++++++++---- .../java/io/sentry/util/CheckInUtils.java | 43 +++++++------- .../java/io/sentry/CombinedScopeViewTest.kt | 2 - sentry/src/test/java/io/sentry/ScopesTest.kt | 37 ++++++++++++ .../java/io/sentry/util/CheckInUtilsTest.kt | 40 ++++++++----- 43 files changed, 522 insertions(+), 273 deletions(-) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt index f4f2c696d3..4a8d5217dd 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt @@ -137,7 +137,6 @@ class InternalSentrySdkTest { Sentry.setCurrentScopes( Scopes(Scope(options), Scope(options), Scope(options), "test") ) - // TODO [HSM] add breadcrumbs to all scopes and assert they are there Sentry.addBreadcrumb("test") Sentry.configureScope(ScopeType.CURRENT) { scope -> scope.addBreadcrumb(Breadcrumb("currentBreadcrumb")) } Sentry.configureScope(ScopeType.ISOLATION) { scope -> scope.addBreadcrumb(Breadcrumb("isolationBreadcrumb")) } diff --git a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt index bd498846dd..9cffd744d8 100644 --- a/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt +++ b/sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt @@ -16,6 +16,10 @@ import kotlin.test.assertNull class SentryContextTest { + // TODO [HSM] In global hub mode SentryContext behaves differently + // because Sentry.getCurrentScopes always returns rootScopes + // What's the desired behaviour? + @BeforeTest fun init() { Sentry.init("https://key@sentry.io/123") @@ -183,8 +187,8 @@ class SentryContextTest { val c2 = launch( SentryContext( - Sentry.getCurrentScopes().clone().also { - Sentry.setTag("cloned", "clonedValue") + Sentry.getCurrentScopes().forkedScopes("test").also { + it.setTag("cloned", "clonedValue") } ) ) { @@ -198,13 +202,13 @@ class SentryContextTest { c2.join() assertNotNull(getTag("c1")) - assertNotNull(getTag("c2")) - assertNotNull(getTag("cloned")) + assertNull(getTag("c2")) + assertNull(getTag("cloned")) }.join() assertNotNull(getTag("c1")) - assertNotNull(getTag("c2")) - assertNotNull(getTag("cloned")) + assertNull(getTag("c2")) + assertNull(getTag("cloned")) return@runBlocking } @@ -223,7 +227,7 @@ class SentryContextTest { val c2 = launch( SentryContext( - Sentry.getCurrentScopes().clone().also { + Sentry.getCurrentScopes().forkedCurrentScope("test").also { it.configureScope(ScopeType.CURRENT) { scope -> scope.setTag("cloned", "clonedValue") } @@ -253,7 +257,7 @@ class SentryContextTest { @Test fun `mergeForChild returns copy of initial context if Key not present`() { val initialContextElement = SentryContext( - Sentry.getCurrentScopes().clone().also { + Sentry.getCurrentScopes().forkedScopes("test").also { it.setTag("cloned", "clonedValue") } ) @@ -266,7 +270,7 @@ class SentryContextTest { @Test fun `mergeForChild returns passed context`() { val initialContextElement = SentryContext( - Sentry.getCurrentScopes().clone().also { + Sentry.getCurrentScopes().forkedScopes("test").also { it.setTag("cloned", "clonedValue") } ) diff --git a/sentry-quartz/api/sentry-quartz.api b/sentry-quartz/api/sentry-quartz.api index 21ca2abca6..bb8b142a91 100644 --- a/sentry-quartz/api/sentry-quartz.api +++ b/sentry-quartz/api/sentry-quartz.api @@ -5,7 +5,7 @@ public final class io/sentry/quartz/BuildConfig { public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener { public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String; - public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; + public static final field SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public static final field SENTRY_SLUG_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index 03f1acbbed..38dbffdc8e 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -25,7 +25,7 @@ public final class SentryJobListener implements JobListener { public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; public static final String SENTRY_SLUG_KEY = "sentry-slug"; - public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; + public static final String SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY = "sentry-scope-lifecycle"; private final @NotNull IScopes scopes; @@ -52,14 +52,15 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { if (maybeSlug == null) { return; } - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = + scopes.forkedScopes("SentryJobListener").makeCurrent(); TracingUtils.startNewTrace(scopes); final @NotNull String slug = maybeSlug; final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn); context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); context.put(SENTRY_SLUG_KEY, slug); - context.put(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); + context.put(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY, lifecycleToken); } catch (Throwable t) { scopes .getOptions() @@ -107,7 +108,7 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); } finally { - LifecycleHelper.close(context.get(SENTRY_LIFECYCLE_TOKEN_KEY)); + LifecycleHelper.close(context.get(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY)); } } } diff --git a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api index adde86fda5..d89edeec60 100644 --- a/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api +++ b/sentry-servlet-jakarta/api/sentry-servlet-jakarta.api @@ -9,7 +9,7 @@ public class io/sentry/servlet/jakarta/SentryServletContainerInitializer : jakar } public class io/sentry/servlet/jakarta/SentryServletRequestListener : jakarta/servlet/ServletRequestListener { - public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; + public static final field SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljakarta/servlet/ServletRequestEvent;)V diff --git a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java index f5b3be30b9..9c8edeaf71 100644 --- a/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java +++ b/sentry-servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java @@ -23,7 +23,7 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; + public static final String SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY = "sentry-scope-lifecycle"; private final IScopes scopes; @@ -38,15 +38,16 @@ public SentryServletRequestListener() { @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); - LifecycleHelper.close(servletRequest.getAttribute(SENTRY_LIFECYCLE_TOKEN_KEY)); + LifecycleHelper.close(servletRequest.getAttribute(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY)); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = + scopes.forkedScopes("SentryServletRequestListener").makeCurrent(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); - servletRequest.setAttribute(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); + servletRequest.setAttribute(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY, lifecycleToken); if (servletRequest instanceof HttpServletRequest) { final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; diff --git a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt index 30ef3da1ed..322c410117 100644 --- a/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt +++ b/sentry-servlet-jakarta/src/test/kotlin/io/sentry/servlet/jakarta/SentryServletRequestListenerTest.kt @@ -4,6 +4,7 @@ import io.sentry.Breadcrumb import io.sentry.IScopes import io.sentry.ISentryLifecycleToken import jakarta.servlet.ServletRequestEvent +import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check import org.mockito.kotlin.eq @@ -28,7 +29,8 @@ class SentryServletRequestListenerTest { init { whenever(event.servletRequest).thenReturn(request) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } } @@ -38,7 +40,8 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.scopes).pushIsolationScope() + verify(fixture.scopes).forkedScopes(any()) + verify(fixture.scopes).makeCurrent() } @Test @@ -53,12 +56,12 @@ class SentryServletRequestListenerTest { }, anyOrNull() ) - verify(fixture.request).setAttribute(eq("sentry-lifecycle"), same(fixture.lifecycleToken)) + verify(fixture.request).setAttribute(eq("sentry-scope-lifecycle"), same(fixture.lifecycleToken)) } @Test fun `pops scope when request gets destroyed`() { - whenever(fixture.request.getAttribute(eq("sentry-lifecycle"))).thenReturn(fixture.lifecycleToken) + whenever(fixture.request.getAttribute(eq("sentry-scope-lifecycle"))).thenReturn(fixture.lifecycleToken) fixture.listener.requestDestroyed(fixture.event) verify(fixture.lifecycleToken).close() diff --git a/sentry-servlet/api/sentry-servlet.api b/sentry-servlet/api/sentry-servlet.api index 63d3cf4b33..3bbffa1b5d 100644 --- a/sentry-servlet/api/sentry-servlet.api +++ b/sentry-servlet/api/sentry-servlet.api @@ -9,7 +9,7 @@ public class io/sentry/servlet/SentryServletContainerInitializer : javax/servlet } public class io/sentry/servlet/SentryServletRequestListener : javax/servlet/ServletRequestListener { - public static final field SENTRY_LIFECYCLE_TOKEN_KEY Ljava/lang/String; + public static final field SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY Ljava/lang/String; public fun ()V public fun (Lio/sentry/IScopes;)V public fun requestDestroyed (Ljavax/servlet/ServletRequestEvent;)V diff --git a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java index 4587daa655..0a2a2f5d23 100644 --- a/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java +++ b/sentry-servlet/src/main/java/io/sentry/servlet/SentryServletRequestListener.java @@ -23,7 +23,7 @@ @Open public class SentryServletRequestListener implements ServletRequestListener { - public static final String SENTRY_LIFECYCLE_TOKEN_KEY = "sentry-lifecycle"; + public static final String SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY = "sentry-scope-lifecycle"; private final IScopes scopes; @@ -38,15 +38,16 @@ public SentryServletRequestListener() { @Override public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); - LifecycleHelper.close(servletRequest.getAttribute(SENTRY_LIFECYCLE_TOKEN_KEY)); + LifecycleHelper.close(servletRequest.getAttribute(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY)); } @Override public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); + final @NotNull ISentryLifecycleToken lifecycleToken = + scopes.forkedScopes("SentryServletRequestListener").makeCurrent(); final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); - servletRequest.setAttribute(SENTRY_LIFECYCLE_TOKEN_KEY, lifecycleToken); + servletRequest.setAttribute(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY, lifecycleToken); if (servletRequest instanceof HttpServletRequest) { final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; diff --git a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt index d72b93179f..07327599d3 100644 --- a/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt +++ b/sentry-servlet/src/test/kotlin/io/sentry/servlet/SentryServletRequestListenerTest.kt @@ -4,6 +4,7 @@ import io.sentry.Breadcrumb import io.sentry.IScopes import io.sentry.ISentryLifecycleToken import org.assertj.core.api.Assertions.assertThat +import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.check import org.mockito.kotlin.mock @@ -26,7 +27,8 @@ class SentryServletRequestListenerTest { request.requestURI = "http://localhost:8080/some-uri" request.method = "post" whenever(event.servletRequest).thenReturn(request) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } } @@ -36,7 +38,8 @@ class SentryServletRequestListenerTest { fun `pushes scope when request gets initialized`() { fixture.listener.requestInitialized(fixture.event) - verify(fixture.scopes).pushIsolationScope() + verify(fixture.scopes).forkedScopes(any()) + verify(fixture.scopes).makeCurrent() } @Test @@ -51,12 +54,12 @@ class SentryServletRequestListenerTest { }, anyOrNull() ) - assertSame(fixture.lifecycleToken, fixture.request.getAttribute("sentry-lifecycle")) + assertSame(fixture.lifecycleToken, fixture.request.getAttribute("sentry-scope-lifecycle")) } @Test fun `pops scope when request gets destroyed`() { - fixture.request.setAttribute("sentry-lifecycle", fixture.lifecycleToken) + fixture.request.setAttribute("sentry-scope-lifecycle", fixture.lifecycleToken) fixture.listener.requestDestroyed(fixture.event) verify(fixture.lifecycleToken).close() diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index de8b5bce65..c7573701ec 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -31,7 +31,7 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IScopes scopes; + private final @NotNull IScopes scopesBeforeForking; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; @@ -39,7 +39,7 @@ public SentrySpringFilter( final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.scopes = Objects.requireNonNull(scopes, "scopes are required"); + this.scopesBeforeForking = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); @@ -59,27 +59,28 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (scopes.isEnabled()) { + if (scopesBeforeForking.isEnabled()) { // request may qualify for caching request body, if so resolve cached request - final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - try { + final HttpServletRequest request = + resolveHttpServletRequest(scopesBeforeForking, servletRequest); + final @NotNull IScopes forkedScopes = scopesBeforeForking.forkedScopes("SentrySpringFilter"); + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); - configureScope(request); + forkedScopes.addBreadcrumb( + Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + configureScope(forkedScopes, request); filterChain.doFilter(request, response); - } finally { - lifecycleToken.close(); } } else { filterChain.doFilter(servletRequest, response); } } - private void configureScope(HttpServletRequest request) { + private void configureScope( + final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) { try { scopes.configureScope( scope -> { @@ -106,7 +107,7 @@ private void configureScope(HttpServletRequest request) { } private @NotNull HttpServletRequest resolveHttpServletRequest( - final @NotNull HttpServletRequest request) { + final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) { if (scopes.getOptions().isSendDefaultPii() && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java index 42c35919d7..ba75795260 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryTaskDecorator.java @@ -9,16 +9,14 @@ import org.springframework.scheduling.annotation.Async; /** - * Sets a current scopes on a thread running a {@link Runnable} given by parameter. Used to - * propagate the current {@link IScopes} on the thread executing async task - like MVC controller - * methods returning a {@link Callable} or Spring beans methods annotated with {@link Async}. + * Forks scopes for a thread running a {@link Runnable} given by parameter. Used to propagate the + * current {@link IScopes} on the thread executing async task - like MVC controller methods + * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override - // TODO [HSM] should there also be a SentryIsolatedTaskDecorator or similar that uses - // forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); + final IScopes newScopes = Sentry.getCurrentScopes().forkedScopes("SentryTaskDecorator"); return () -> { try (final @NotNull ISentryLifecycleToken ignored = newScopes.makeCurrent()) { diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index e51647cba8..dd22f4dc5d 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -87,27 +87,28 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - TracingUtils.startNewTrace(scopes); + try (final @NotNull ISentryLifecycleToken ignored = + scopes.forkedScopes("SentryCheckInAdvice").makeCurrent()) { + TracingUtils.startNewTrace(scopes); - @Nullable SentryId checkInId = null; - final long startTime = System.currentTimeMillis(); - boolean didError = false; + @Nullable SentryId checkInId = null; + final long startTime = System.currentTimeMillis(); + boolean didError = false; - try { - if (!isHeartbeatOnly) { - checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + try { + if (!isHeartbeatOnly) { + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + } + return invocation.proceed(); + } catch (Throwable e) { + didError = true; + throw e; + } finally { + final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; + CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); + checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); + scopes.captureCheckIn(checkIn); } - return invocation.proceed(); - } catch (Throwable e) { - didError = true; - throw e; - } finally { - final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; - CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); - checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - scopes.captureCheckIn(checkIn); - lifecycleToken.close(); } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java index da781afcd8..c85831ae8f 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTransactionAdvice.java @@ -29,14 +29,14 @@ public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring_jakarta.advice"; - private final @NotNull IScopes scopes; + private final @NotNull IScopes scopesBeforeForking; public SentryTransactionAdvice() { this(ScopesAdapter.getInstance()); } public SentryTransactionAdvice(final @NotNull IScopes scopes) { - this.scopes = Objects.requireNonNull(scopes, "scopes are required"); + this.scopesBeforeForking = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -57,7 +57,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final TransactionNameAndSource nameAndSource = resolveTransactionName(invocation, sentryTransaction); - final boolean isTransactionActive = isTransactionActive(); + final boolean isTransactionActive = isTransactionActive(scopesBeforeForking); if (isTransactionActive) { // transaction is already active, we do not start new transaction @@ -69,25 +69,27 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - final TransactionOptions transactionOptions = new TransactionOptions(); - transactionOptions.setBindToScope(true); - final ITransaction transaction = - scopes.startTransaction( - new TransactionContext(nameAndSource.name, nameAndSource.source, operation), - transactionOptions); - transaction.getSpanContext().setOrigin(TRACE_ORIGIN); - try { - final Object result = invocation.proceed(); - transaction.setStatus(SpanStatus.OK); - return result; - } catch (Throwable e) { - transaction.setStatus(SpanStatus.INTERNAL_ERROR); - transaction.setThrowable(e); - throw e; - } finally { - transaction.finish(); - lifecycleToken.close(); + final @NotNull IScopes forkedScopes = + scopesBeforeForking.forkedScopes("SentryTransactionAdvice"); + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { + final TransactionOptions transactionOptions = new TransactionOptions(); + transactionOptions.setBindToScope(true); + final ITransaction transaction = + forkedScopes.startTransaction( + new TransactionContext(nameAndSource.name, nameAndSource.source, operation), + transactionOptions); + transaction.getSpanContext().setOrigin(TRACE_ORIGIN); + try { + final Object result = invocation.proceed(); + transaction.setStatus(SpanStatus.OK); + return result; + } catch (Throwable e) { + transaction.setStatus(SpanStatus.INTERNAL_ERROR); + transaction.setThrowable(e); + throw e; + } finally { + transaction.finish(); + } } } } @@ -106,7 +108,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } } - private boolean isTransactionActive() { + private boolean isTransactionActive(final @NotNull IScopes scopes) { return scopes.getSpan() != null; } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java index e160be7ac9..1c2bb0afcf 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java @@ -9,8 +9,6 @@ import reactor.util.context.Context; @ApiStatus.Experimental -// TODO [HSM] do we keep old methods around and deprecate them? -// TODO [HSM] do we need to offer isolated variants? public final class ReactorUtils { /** @@ -28,7 +26,7 @@ public static Mono withSentry(final @NotNull Mono mono) { } /** - * Writes a new Sentry {@link IScopes} cloned from the main scopes to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} forked from the main scopes to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

    This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -76,7 +74,7 @@ public static Flux withSentry(final @NotNull Flux flux) { } /** - * Writes a new Sentry {@link IScopes} cloned from the main scopes to the {@link Context} and uses + * Writes a new Sentry {@link IScopes} forked from the main scopes to the {@link Context} and uses * {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. * *

    This requires - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be @@ -99,7 +97,7 @@ public static Flux withSentryForkedRoots(final @NotNull Flux flux) { public static Flux withSentryScopes( final @NotNull Flux flux, final @NotNull IScopes scopes) { /** - * WARNING: Cannot set the scopes as current. It would be used by others to clone again causing + * WARNING: Cannot set the scopes as current. It would be used by others to fork again causing * shared scopes and thus leading to issues like unrelated breadcrumbs showing up in events. */ // Sentry.setCurrentScopes(forkedScopes); diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt index e87b5f5b26..e02f1cb62a 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt @@ -63,7 +63,8 @@ class SentryCheckInAdviceTest { fun setup() { reset(scopes) whenever(scopes.options).thenReturn(SentryOptions()) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } @Test @@ -84,7 +85,8 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes, times(2)).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -108,7 +110,8 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes, times(2)).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -128,7 +131,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -149,7 +153,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -163,9 +168,10 @@ class SentryCheckInAdviceTest { assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(scopes, never()).pushScope() + verify(scopes, never()).forkedScopes(any()) + verify(scopes, never()).makeCurrent() verify(scopes, never()).captureCheckIn(any()) - verify(scopes, never()).popScope() + verify(lifecycleToken, never()).close() } @Test @@ -183,7 +189,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -203,7 +210,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -223,7 +231,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt index ac394deb31..1c1f2b5c13 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt @@ -39,6 +39,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { val scopes = mock() + val scopesBeforeForking = mock() val response = MockHttpServletResponse() val lifecycleToken = mock() val chain = mock() @@ -47,16 +48,19 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) + whenever(scopesBeforeForking.options).thenReturn(options) + whenever(scopesBeforeForking.isEnabled).thenReturn(true) whenever(scopes.options).thenReturn(options) whenever(scopes.isEnabled).thenReturn(true) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopesBeforeForking.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(scopes) + return SentrySpringFilter(scopesBeforeForking) } } @@ -67,7 +71,8 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).pushIsolationScope() + verify(fixture.scopesBeforeForking).forkedScopes(any()) + verify(fixture.scopes).makeCurrent() } @Test diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt index b0f83782e0..978c5baa63 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTransactionAdviceTest.kt @@ -58,7 +58,8 @@ class SentryTransactionAdviceTest { dsn = "https://key@sentry.io/proj" } ) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } @Test @@ -145,7 +146,8 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() } @Test diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index af55ac2ce3..d450e1451a 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -31,7 +31,7 @@ @Open public class SentrySpringFilter extends OncePerRequestFilter { - private final @NotNull IScopes scopes; + private final @NotNull IScopes scopesBeforeForking; private final @NotNull SentryRequestResolver requestResolver; private final @NotNull TransactionNameProvider transactionNameProvider; @@ -39,7 +39,7 @@ public SentrySpringFilter( final @NotNull IScopes scopes, final @NotNull SentryRequestResolver requestResolver, final @NotNull TransactionNameProvider transactionNameProvider) { - this.scopes = Objects.requireNonNull(scopes, "scopes are required"); + this.scopesBeforeForking = Objects.requireNonNull(scopes, "scopes are required"); this.requestResolver = Objects.requireNonNull(requestResolver, "requestResolver is required"); this.transactionNameProvider = Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required"); @@ -59,27 +59,28 @@ protected void doFilterInternal( final @NotNull HttpServletResponse response, final @NotNull FilterChain filterChain) throws ServletException, IOException { - if (scopes.isEnabled()) { + if (scopesBeforeForking.isEnabled()) { // request may qualify for caching request body, if so resolve cached request - final HttpServletRequest request = resolveHttpServletRequest(servletRequest); - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - try { + final HttpServletRequest request = + resolveHttpServletRequest(scopesBeforeForking, servletRequest); + final @NotNull IScopes forkedScopes = scopesBeforeForking.forkedScopes("SentrySpringFilter"); + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { final Hint hint = new Hint(); hint.set(SPRING_REQUEST_FILTER_REQUEST, servletRequest); hint.set(SPRING_REQUEST_FILTER_RESPONSE, response); - scopes.addBreadcrumb(Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); - configureScope(request); + forkedScopes.addBreadcrumb( + Breadcrumb.http(request.getRequestURI(), request.getMethod()), hint); + configureScope(forkedScopes, request); filterChain.doFilter(request, response); - } finally { - lifecycleToken.close(); } } else { filterChain.doFilter(servletRequest, response); } } - private void configureScope(HttpServletRequest request) { + private void configureScope( + final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) { try { scopes.configureScope( scope -> { @@ -106,7 +107,7 @@ private void configureScope(HttpServletRequest request) { } private @NotNull HttpServletRequest resolveHttpServletRequest( - final @NotNull HttpServletRequest request) { + final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) { if (scopes.getOptions().isSendDefaultPii() && qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) { try { diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java index 2968eede43..3b0960cb44 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryTaskDecorator.java @@ -9,17 +9,15 @@ import org.springframework.scheduling.annotation.Async; /** - * Forks current scope for the thread running a {@link Runnable} given by parameter. Used to - * propagate the current {@link IScopes} on the thread executing async task - like MVC controller - * methods returning a {@link Callable} or Spring beans methods annotated with {@link Async}. + * Forks scopes for the thread running a {@link Runnable} given by parameter. Used to propagate the + * current {@link IScopes} on the thread executing async task - like MVC controller methods + * returning a {@link Callable} or Spring beans methods annotated with {@link Async}. */ public final class SentryTaskDecorator implements TaskDecorator { @Override - // TODO [HSM] should there also be a SentryIsolatedTaskDecorator or similar that uses - // forkedScopes()? public @NotNull Runnable decorate(final @NotNull Runnable runnable) { - final IScopes forkedScopes = - Sentry.getCurrentScopes().forkedCurrentScope("spring.taskDecorator"); + final @NotNull IScopes forkedScopes = + Sentry.getCurrentScopes().forkedScopes("SentryTaskDecorator"); return () -> { try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index 9e59093b16..58e7e9430e 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -90,27 +90,28 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - TracingUtils.startNewTrace(scopes); + try (final @NotNull ISentryLifecycleToken ignored = + scopes.forkedScopes("SentryCheckInAdvice").makeCurrent()) { + TracingUtils.startNewTrace(scopes); - @Nullable SentryId checkInId = null; - final long startTime = System.currentTimeMillis(); - boolean didError = false; + @Nullable SentryId checkInId = null; + final long startTime = System.currentTimeMillis(); + boolean didError = false; - try { - if (!isHeartbeatOnly) { - checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + try { + if (!isHeartbeatOnly) { + checkInId = scopes.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + } + return invocation.proceed(); + } catch (Throwable e) { + didError = true; + throw e; + } finally { + final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; + CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); + checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); + scopes.captureCheckIn(checkIn); } - return invocation.proceed(); - } catch (Throwable e) { - didError = true; - throw e; - } finally { - final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; - CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); - checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - scopes.captureCheckIn(checkIn); - lifecycleToken.close(); } } diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java index a885510fcd..e293eb0b9c 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentryTransactionAdvice.java @@ -28,14 +28,14 @@ @Open public class SentryTransactionAdvice implements MethodInterceptor { private static final String TRACE_ORIGIN = "auto.function.spring.advice"; - private final @NotNull IScopes scopes; + private final @NotNull IScopes scopesBeforeForking; public SentryTransactionAdvice() { this(ScopesAdapter.getInstance()); } public SentryTransactionAdvice(final @NotNull IScopes scopes) { - this.scopes = Objects.requireNonNull(scopes, "scopes are required"); + this.scopesBeforeForking = Objects.requireNonNull(scopes, "scopes are required"); } @SuppressWarnings("deprecation") @@ -56,7 +56,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl final TransactionNameAndSource nameAndSource = resolveTransactionName(invocation, sentryTransaction); - final boolean isTransactionActive = isTransactionActive(); + final boolean isTransactionActive = isTransactionActive(scopesBeforeForking); if (isTransactionActive) { // transaction is already active, we do not start new transaction @@ -68,25 +68,27 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } else { operation = "bean"; } - final @NotNull ISentryLifecycleToken lifecycleToken = scopes.pushIsolationScope(); - final TransactionOptions transactionOptions = new TransactionOptions(); - transactionOptions.setBindToScope(true); - final ITransaction transaction = - scopes.startTransaction( - new TransactionContext(nameAndSource.name, nameAndSource.source, operation), - transactionOptions); - transaction.getSpanContext().setOrigin(TRACE_ORIGIN); - try { - final Object result = invocation.proceed(); - transaction.setStatus(SpanStatus.OK); - return result; - } catch (Throwable e) { - transaction.setStatus(SpanStatus.INTERNAL_ERROR); - transaction.setThrowable(e); - throw e; - } finally { - transaction.finish(); - lifecycleToken.close(); + final @NotNull IScopes forkedScopes = + scopesBeforeForking.forkedScopes("SentryTransactionAdvice"); + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { + final TransactionOptions transactionOptions = new TransactionOptions(); + transactionOptions.setBindToScope(true); + final ITransaction transaction = + forkedScopes.startTransaction( + new TransactionContext(nameAndSource.name, nameAndSource.source, operation), + transactionOptions); + transaction.getSpanContext().setOrigin(TRACE_ORIGIN); + try { + final Object result = invocation.proceed(); + transaction.setStatus(SpanStatus.OK); + return result; + } catch (Throwable e) { + transaction.setStatus(SpanStatus.INTERNAL_ERROR); + transaction.setThrowable(e); + throw e; + } finally { + transaction.finish(); + } } } } @@ -105,7 +107,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl } } - private boolean isTransactionActive() { + private boolean isTransactionActive(final @NotNull IScopes scopes) { return scopes.getSpan() != null; } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt index 57bd293756..6e18ef64f9 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt @@ -64,7 +64,8 @@ class SentryCheckInAdviceTest { fun setup() { reset(scopes) whenever(scopes.options).thenReturn(SentryOptions()) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } @Test @@ -85,7 +86,8 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes, times(2)).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -109,7 +111,8 @@ class SentryCheckInAdviceTest { assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes, times(2)).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -129,7 +132,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -150,7 +154,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -164,7 +169,8 @@ class SentryCheckInAdviceTest { assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) - verify(scopes, never()).pushIsolationScope() + verify(scopes, never()).forkedScopes(any()) + verify(scopes, never()).makeCurrent() verify(scopes, never()).captureCheckIn(any()) verify(lifecycleToken, never()).close() } @@ -184,7 +190,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -204,7 +211,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } @@ -224,7 +232,8 @@ class SentryCheckInAdviceTest { assertNotNull(doneCheckIn.duration) val order = inOrder(scopes, lifecycleToken) - order.verify(scopes).pushIsolationScope() + order.verify(scopes).forkedScopes(any()) + order.verify(scopes).makeCurrent() order.verify(scopes).captureCheckIn(any()) order.verify(lifecycleToken).close() } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt index 6037e253c8..f4dce4cad0 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt @@ -39,6 +39,7 @@ import kotlin.test.fail class SentrySpringFilterTest { private class Fixture { val scopes = mock() + val scopesBeforeForking = mock() val response = MockHttpServletResponse() val lifecycleToken = mock() val chain = mock() @@ -47,16 +48,19 @@ class SentrySpringFilterTest { fun getSut(request: HttpServletRequest? = null, options: SentryOptions = SentryOptions()): SentrySpringFilter { scope = Scope(options) + whenever(scopesBeforeForking.options).thenReturn(options) + whenever(scopesBeforeForking.isEnabled).thenReturn(true) whenever(scopes.options).thenReturn(options) whenever(scopes.isEnabled).thenReturn(true) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopesBeforeForking.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) this.request = request ?: MockHttpServletRequest().apply { this.requestURI = "http://localhost:8080/some-uri" this.method = "post" } - return SentrySpringFilter(scopes) + return SentrySpringFilter(scopesBeforeForking) } } @@ -67,7 +71,8 @@ class SentrySpringFilterTest { val listener = fixture.getSut() listener.doFilter(fixture.request, fixture.response, fixture.chain) - verify(fixture.scopes).pushIsolationScope() + verify(fixture.scopesBeforeForking).forkedScopes(any()) + verify(fixture.scopes).makeCurrent() } @Test diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt index 8a3d8ee46c..3c35bad8e4 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/tracing/SentryTransactionAdviceTest.kt @@ -58,7 +58,8 @@ class SentryTransactionAdviceTest { dsn = "https://key@sentry.io/proj" } ) - whenever(scopes.pushIsolationScope()).thenReturn(lifecycleToken) + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) } @Test @@ -145,7 +146,8 @@ class SentryTransactionAdviceTest { @Test fun `pushes the scope when advice starts`() { classAnnotatedSampleService.hello() - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() } @Test diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 32d530a5a1..22f0210afb 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -579,6 +579,7 @@ public final class io/sentry/Hub : io/sentry/IHub, io/sentry/metrics/MetricsApi$ public fun startSpanForMetric (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan; public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -640,6 +641,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub { public fun startSession ()V public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -701,6 +703,7 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub { public fun startSession ()V public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -933,6 +936,7 @@ public abstract interface class io/sentry/IScopes { public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction; public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public abstract fun withIsolationScope (Lio/sentry/ScopeCallback;)V public abstract fun withScope (Lio/sentry/ScopeCallback;)V } @@ -1428,6 +1432,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub { public fun startSession ()V public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -1562,6 +1567,7 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun startSession ()V public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -2023,7 +2029,6 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator; public fun getMetricsAggregator ()Lio/sentry/IMetricsAggregator; public fun getOptions ()Lio/sentry/SentryOptions; - public fun getParent ()Lio/sentry/Scopes; public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; public fun getScope ()Lio/sentry/IScope; public fun getSpan ()Lio/sentry/ISpan; @@ -2052,6 +2057,7 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri public fun startSpanForMetric (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan; public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -2113,6 +2119,7 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public fun startSession ()V public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public fun withIsolationScope (Lio/sentry/ScopeCallback;)V public fun withScope (Lio/sentry/ScopeCallback;)V } @@ -2218,6 +2225,7 @@ public final class io/sentry/Sentry { public static fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public static fun startTransaction (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; public static fun traceHeaders ()Lio/sentry/SentryTraceHeader; + public static fun withIsolationScope (Lio/sentry/ScopeCallback;)V public static fun withScope (Lio/sentry/ScopeCallback;)V } diff --git a/sentry/src/main/java/io/sentry/Breadcrumb.java b/sentry/src/main/java/io/sentry/Breadcrumb.java index ddb19e5052..b4e2fadd71 100644 --- a/sentry/src/main/java/io/sentry/Breadcrumb.java +++ b/sentry/src/main/java/io/sentry/Breadcrumb.java @@ -667,11 +667,7 @@ public void setUnknown(@Nullable Map unknown) { @Override @SuppressWarnings("JavaUtilDate") public int compareTo(@NotNull Breadcrumb o) { - int timestampCompare = timestamp.compareTo(o.timestamp); - if (timestampCompare == 0) { - return nanos.compareTo(o.nanos); - } - return timestampCompare; + return nanos.compareTo(o.nanos); } public static final class JsonKeys { diff --git a/sentry/src/main/java/io/sentry/CombinedContextsView.java b/sentry/src/main/java/io/sentry/CombinedContextsView.java index 3dd7428975..40c220bb31 100644 --- a/sentry/src/main/java/io/sentry/CombinedContextsView.java +++ b/sentry/src/main/java/io/sentry/CombinedContextsView.java @@ -54,7 +54,7 @@ public void setTrace(@Nullable SpanContext traceContext) { getDefaultContexts().setTrace(traceContext); } - private Contexts getDefaultContexts() { + private @NotNull Contexts getDefaultContexts() { switch (defaultScopeType) { case CURRENT: return currentContexts; diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index bcb93c7558..63081e6cd2 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +// TODO [HSM] remove Hub class @Deprecated public final class Hub implements IHub, MetricsApi.IMetricsInterface { @@ -563,6 +564,7 @@ public void reportFullyDisplayed() { } @Override + @Deprecated public void popScope() { if (!isEnabled()) { options @@ -593,6 +595,26 @@ public void withScope(final @NotNull ScopeCallback callback) { } } + @Override + public void withIsolationScope(final @NotNull ScopeCallback callback) { + if (!isEnabled()) { + try { + callback.run(NoOpScope.getInstance()); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + } + + } else { + pushScope(); + try { + callback.run(stack.peek().getScope()); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); + } + popScope(); + } + } + @Override public void configureScope( final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { @@ -679,16 +701,19 @@ public void flush(long timeoutMillis) { } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return Sentry.getCurrentScopes().getScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return Sentry.getCurrentScopes().getIsolationScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return Sentry.getGlobalScope(); } diff --git a/sentry/src/main/java/io/sentry/HubAdapter.java b/sentry/src/main/java/io/sentry/HubAdapter.java index 266770ddce..df0669504a 100644 --- a/sentry/src/main/java/io/sentry/HubAdapter.java +++ b/sentry/src/main/java/io/sentry/HubAdapter.java @@ -164,6 +164,7 @@ public void removeExtra(@NotNull String key) { } @Override + @Deprecated public void popScope() { Sentry.popScope(); } @@ -173,6 +174,11 @@ public void withScope(@NotNull ScopeCallback callback) { Sentry.withScope(callback); } + @Override + public void withIsolationScope(@NotNull ScopeCallback callback) { + Sentry.withIsolationScope(callback); + } + @Override public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { Sentry.configureScope(scopeType, callback); @@ -219,16 +225,19 @@ public void flush(long timeoutMillis) { } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return Sentry.getCurrentScopes().getScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return Sentry.getCurrentScopes().getIsolationScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return Sentry.getGlobalScope(); } diff --git a/sentry/src/main/java/io/sentry/HubScopesWrapper.java b/sentry/src/main/java/io/sentry/HubScopesWrapper.java index 14e9a03e25..4909f6b193 100644 --- a/sentry/src/main/java/io/sentry/HubScopesWrapper.java +++ b/sentry/src/main/java/io/sentry/HubScopesWrapper.java @@ -159,6 +159,7 @@ public void removeExtra(@NotNull String key) { } @Override + @Deprecated public void popScope() { scopes.popScope(); } @@ -168,6 +169,11 @@ public void withScope(@NotNull ScopeCallback callback) { scopes.withScope(callback); } + @Override + public void withIsolationScope(@NotNull ScopeCallback callback) { + scopes.withIsolationScope(callback); + } + @Override public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { scopes.configureScope(scopeType, callback); @@ -214,16 +220,19 @@ public void flush(long timeoutMillis) { } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return scopes.getScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return scopes.getIsolationScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return Sentry.getGlobalScope(); } diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index b639836ca1..d6b95574d7 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -312,12 +312,19 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { @NotNull ISentryLifecycleToken pushIsolationScope(); - /** Removes the first scope */ + /** + * Removes the first scope and restores its parent. + * + * @deprecated please call {@link ISentryLifecycleToken#close()} on the token returned by {@link + * IScopes#pushScope()} or {@link IScopes#pushIsolationScope()} instead. + */ + @Deprecated void popScope(); /** - * Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry - * SDK in globalHubMode (defaults to true on Android) {@link + * Runs the callback with a new current scope which gets dropped at the end. + * + *

    If you're using the Sentry SDK in globalHubMode (defaults to true on Android) {@link * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope * changes may be dropped when executed in parallel. Use {@link * IScopes#configureScope(ScopeCallback)} instead. @@ -326,6 +333,19 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { */ void withScope(@NotNull ScopeCallback callback); + /** + * Runs the callback with a new isolation scope which gets dropped at the end. Current scope is + * also forked. + * + *

    If you're using the Sentry SDK in globalHubMode (defaults to true on Android) {@link + * Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope + * changes may be dropped when executed in parallel. Use {@link IScopes#configureScope(ScopeType, + * ScopeCallback)} instead. + * + * @param callback the callback + */ + void withIsolationScope(@NotNull ScopeCallback callback); + /** * Configures the scope through the callback. * @@ -414,21 +434,27 @@ default void configureScope(@NotNull ScopeCallback callback) { * * @return scope */ - public @NotNull IScope getScope(); + @ApiStatus.Internal + @NotNull + IScope getScope(); /** * Returns the isolation scope of this Scopes. * * @return isolation scope */ - public @NotNull IScope getIsolationScope(); + @ApiStatus.Internal + @NotNull + IScope getIsolationScope(); /** * Returns the global scope. * * @return global scope */ - public @NotNull IScope getGlobalScope(); + @ApiStatus.Internal + @NotNull + IScope getGlobalScope(); /** * Captures the transaction and enqueues it for sending to Sentry server. diff --git a/sentry/src/main/java/io/sentry/IScopesStorage.java b/sentry/src/main/java/io/sentry/IScopesStorage.java index 92f6b587c4..d067d6bafd 100644 --- a/sentry/src/main/java/io/sentry/IScopesStorage.java +++ b/sentry/src/main/java/io/sentry/IScopesStorage.java @@ -1,9 +1,11 @@ package io.sentry; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public interface IScopesStorage { + @NotNull ISentryLifecycleToken set(final @Nullable IScopes scopes); @Nullable diff --git a/sentry/src/main/java/io/sentry/NoOpHub.java b/sentry/src/main/java/io/sentry/NoOpHub.java index 653c8de9f9..3625eb7c06 100644 --- a/sentry/src/main/java/io/sentry/NoOpHub.java +++ b/sentry/src/main/java/io/sentry/NoOpHub.java @@ -137,6 +137,7 @@ public void removeExtra(@NotNull String key) {} } @Override + @Deprecated public void popScope() {} @Override @@ -144,6 +145,11 @@ public void withScope(@NotNull ScopeCallback callback) { callback.run(NoOpScope.getInstance()); } + @Override + public void withIsolationScope(@NotNull ScopeCallback callback) { + callback.run(NoOpScope.getInstance()); + } + @Override public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {} @@ -179,16 +185,19 @@ public void flush(long timeoutMillis) {} } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return NoOpScope.getInstance(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return NoOpScope.getInstance(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return NoOpScope.getInstance(); } diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index 74f965f651..945203066b 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -132,6 +132,7 @@ public void removeExtra(@NotNull String key) {} } @Override + @Deprecated public void popScope() {} @Override @@ -139,6 +140,11 @@ public void withScope(@NotNull ScopeCallback callback) { callback.run(NoOpScope.getInstance()); } + @Override + public void withIsolationScope(@NotNull ScopeCallback callback) { + callback.run(NoOpScope.getInstance()); + } + @Override public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {} @@ -180,16 +186,19 @@ public void flush(long timeoutMillis) {} } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return NoOpScope.getInstance(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return NoOpScope.getInstance(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return NoOpScope.getInstance(); } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 5b3d0e672b..3d9b39a936 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -28,7 +28,6 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @NotNull IScope isolationScope; private final @NotNull IScope globalScope; - @SuppressWarnings("UnusedVariable") private final @Nullable Scopes parentScopes; private final @NotNull String creator; @@ -76,18 +75,21 @@ private Scopes( } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return scope; } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return isolationScope; } - // TODO [HSM] add to IScopes interface? - public @Nullable Scopes getParent() { - return parentScopes; + @Override + @ApiStatus.Internal + public @NotNull IScope getGlobalScope() { + return globalScope; } // TODO [HSM] add to IScopes interface? @@ -100,9 +102,8 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { return true; } - final @Nullable Scopes parent = otherScopes.getParent(); - if (parent != null) { - return isAncestorOf(parent); + if (otherScopes.parentScopes != null) { + return isAncestorOf(otherScopes.parentScopes); } return false; @@ -408,8 +409,8 @@ public void close(final boolean isRestarting) { } } - // TODO [HSM] which scopes do we call this on? isolation and current scope? configureScope(scope -> scope.clear()); + configureScope(ScopeType.ISOLATION, scope -> scope.clear()); getOptions().getTransactionProfiler().close(); getOptions().getTransactionPerformanceCollector().close(); final @NotNull ISentryExecutorService executorService = getOptions().getExecutorService(); @@ -421,8 +422,9 @@ public void close(final boolean isRestarting) { } // TODO: should we end session before closing client? - // TODO [HSM] should we go through all clients (global, isolation, current) and close them? - getClient().close(isRestarting); + configureScope(ScopeType.CURRENT, scope -> scope.getClient().close(isRestarting)); + configureScope(ScopeType.ISOLATION, scope -> scope.getClient().close(isRestarting)); + configureScope(ScopeType.GLOBAL, scope -> scope.getClient().close(isRestarting)); } catch (Throwable e) { getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e); } @@ -575,11 +577,6 @@ private void updateLastEventId(final @NotNull SentryId lastEventId) { getCombinedScopeView().setLastEventId(lastEventId); } - @Override - public @NotNull IScope getGlobalScope() { - return globalScope; - } - @Override public @NotNull SentryId getLastEventId() { return getCombinedScopeView().getLastEventId(); @@ -618,17 +615,16 @@ public ISentryLifecycleToken pushIsolationScope() { return Sentry.setCurrentScopes(this); } - // TODO [HSM] needs to be deprecated because there's no more stack @Override + @Deprecated public void popScope() { if (!isEnabled()) { getOptions() .getLogger() .log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op."); } else { - final @Nullable Scopes parent = getParent(); + final @Nullable Scopes parent = parentScopes; if (parent != null) { - // TODO [HSM] this is never closed parent.makeCurrent(); } } @@ -654,6 +650,29 @@ public void withScope(final @NotNull ScopeCallback callback) { } } + @Override + public void withIsolationScope(final @NotNull ScopeCallback callback) { + if (!isEnabled()) { + try { + callback.run(NoOpScope.getInstance()); + } catch (Throwable e) { + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e); + } + + } else { + final @NotNull IScopes forkedScopes = forkedScopes("withIsolationScope"); + try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) { + callback.run(forkedScopes.getIsolationScope()); + } catch (Throwable e) { + getOptions() + .getLogger() + .log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e); + } + } + } + @Override public void configureScope( final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { @@ -717,8 +736,7 @@ public void flush(long timeoutMillis) { if (!isEnabled()) { getOptions().getLogger().log(SentryLevel.WARNING, "Disabled Scopes cloned."); } - // TODO [HSM] should this fork isolation scope as well? - return new HubScopesWrapper(forkedCurrentScope("scopes clone")); + return new HubScopesWrapper(forkedScopes("scopes clone")); } @ApiStatus.Internal diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index c7f11612e9..005480ccf5 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -160,6 +160,7 @@ public void removeExtra(@NotNull String key) { } @Override + @Deprecated public void popScope() { Sentry.popScope(); } @@ -169,6 +170,11 @@ public void withScope(@NotNull ScopeCallback callback) { Sentry.withScope(callback); } + @Override + public void withIsolationScope(@NotNull ScopeCallback callback) { + Sentry.withIsolationScope(callback); + } + @Override public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) { Sentry.configureScope(scopeType, callback); @@ -212,21 +218,23 @@ public void flush(long timeoutMillis) { @Override public @NotNull ISentryLifecycleToken makeCurrent() { - // TODO [HSM] this wouldn't do anything since it replaced the current with the same Scopes return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } @Override + @ApiStatus.Internal public @NotNull IScope getScope() { return Sentry.getCurrentScopes().getScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getIsolationScope() { return Sentry.getCurrentScopes().getIsolationScope(); } @Override + @ApiStatus.Internal public @NotNull IScope getGlobalScope() { return Sentry.getGlobalScope(); } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 214a16362a..4c751f68f1 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -53,6 +53,8 @@ private Sentry() {} * *

    For Android options will also be (temporarily) replaced by SentryAndroid static block. */ + // TODO [HSM] use SentryOptions.empty and address + // https://github.com/getsentry/sentry-java/issues/2541 private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); /** Default value for globalHubMode is false */ @@ -89,7 +91,7 @@ private Sentry() {} if (globalHubMode) { return rootScopes; } - IScopes scopes = getScopesStorage().get(); + @Nullable IScopes scopes = getScopesStorage().get(); if (scopes == null || scopes.isNoOp()) { scopes = rootScopes.forkedScopes("getCurrentScopes"); getScopesStorage().set(scopes); @@ -835,7 +837,13 @@ public static void removeExtra(final @NotNull String key) { return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); } - /** Removes the first scope */ + /** + * Removes the first scope and restores its parent. + * + * @deprecated please call {@link ISentryLifecycleToken#close()} on the token returned by {@link + * Sentry#pushScope()} or {@link Sentry#pushIsolationScope()} instead. + */ + @Deprecated public static void popScope() { // popScope is no-op in global hub mode if (!globalHubMode) { @@ -844,7 +852,7 @@ public static void popScope() { } /** - * Runs the callback with a new scope which gets dropped at the end + * Runs the callback with a new current scope which gets dropped at the end * * @param callback the callback */ @@ -852,6 +860,16 @@ public static void withScope(final @NotNull ScopeCallback callback) { getCurrentScopes().withScope(callback); } + /** + * Runs the callback with a new isolation scope which gets dropped at the end. Current scope is + * also forked. + * + * @param callback the callback + */ + public static void withIsolationScope(final @NotNull ScopeCallback callback) { + getCurrentScopes().withIsolationScope(callback); + } + /** * Configures the scope through the callback. * diff --git a/sentry/src/main/java/io/sentry/SentryWrapper.java b/sentry/src/main/java/io/sentry/SentryWrapper.java index 16bf0ef7ec..78505cf297 100644 --- a/sentry/src/main/java/io/sentry/SentryWrapper.java +++ b/sentry/src/main/java/io/sentry/SentryWrapper.java @@ -12,24 +12,26 @@ *

  • {@link Supplier} * * - * that forks the current scope before execution and restores it afterwards. This prevents reused - * threads (e.g. from thread-pools) from getting an incorrect state. + * that forks the current scope(s) before execution and restores previous state afterwards. Which + * scope(s) are forked, depends on the method used here. This prevents reused threads (e.g. from + * thread-pools) from getting an incorrect state. */ +// TODO [HSM] only deliver isolated variant as default for now public final class SentryWrapper { /** * Helper method to wrap {@link Callable} * - *

    Forks the current scope before execution and restores it afterwards. This prevents reused - * threads (e.g. from thread-pools) from getting an incorrect state. + *

    Forks current scope before execution and restores previous state afterwards. This prevents + * reused threads (e.g. from thread-pools) from getting an incorrect state. * * @param callable - the {@link Callable} to be wrapped * @return the wrapped {@link Callable} * @param - the result type of the {@link Callable} */ - // TODO [HSM] adapt javadoc public static Callable wrapCallable(final @NotNull Callable callable) { - final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope("wrapCallable"); + final IScopes newScopes = + Sentry.getCurrentScopes().forkedCurrentScope("SentryWrapper.wrapCallable"); return () -> { try (ISentryLifecycleToken ignored = newScopes.makeCurrent()) { @@ -38,8 +40,18 @@ public static Callable wrapCallable(final @NotNull Callable callable) }; } + /** + * Helper method to wrap {@link Callable} + * + *

    Forks current and isolation scope before execution and restores previous state afterwards. + * This prevents reused threads (e.g. from thread-pools) from getting an incorrect state. + * + * @param callable - the {@link Callable} to be wrapped + * @return the wrapped {@link Callable} + * @param - the result type of the {@link Callable} + */ public static Callable wrapCallableIsolated(final @NotNull Callable callable) { - final IScopes newScopes = Sentry.getCurrentScopes().forkedScopes("wrapCallable"); + final IScopes newScopes = Sentry.getCurrentScopes().forkedScopes("SentryWrapper.wrapCallable"); return () -> { try (ISentryLifecycleToken ignored = newScopes.makeCurrent()) { @@ -51,15 +63,15 @@ public static Callable wrapCallableIsolated(final @NotNull Callable ca /** * Helper method to wrap {@link Supplier} * - *

    Forks the current scope before execution and restores it afterwards. This prevents reused - * threads (e.g. from thread-pools) from getting an incorrect state. + *

    Forks current scope before execution and restores previous state afterwards. This prevents + * reused threads (e.g. from thread-pools) from getting an incorrect state. * * @param supplier - the {@link Supplier} to be wrapped * @return the wrapped {@link Supplier} * @param - the result type of the {@link Supplier} */ public static Supplier wrapSupplier(final @NotNull Supplier supplier) { - final IScopes newScopes = Sentry.forkedCurrentScope("wrapSupplier"); + final IScopes newScopes = Sentry.forkedCurrentScope("SentryWrapper.wrapSupplier"); return () -> { try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) { @@ -68,8 +80,18 @@ public static Supplier wrapSupplier(final @NotNull Supplier supplier) }; } + /** + * Helper method to wrap {@link Supplier} + * + *

    Forks current and isolation scope before execution and restores previous state afterwards. + * This prevents reused threads (e.g. from thread-pools) from getting an incorrect state. + * + * @param supplier - the {@link Supplier} to be wrapped + * @return the wrapped {@link Supplier} + * @param - the result type of the {@link Supplier} + */ public static Supplier wrapSupplierIsolated(final @NotNull Supplier supplier) { - final IScopes newScopes = Sentry.forkedScopes("wrapSupplier"); + final IScopes newScopes = Sentry.forkedScopes("SentryWrapper.wrapSupplier"); return () -> { try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) { diff --git a/sentry/src/main/java/io/sentry/util/CheckInUtils.java b/sentry/src/main/java/io/sentry/util/CheckInUtils.java index d42cf4edf4..3f13064cbb 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -31,29 +31,30 @@ public static U withCheckIn( final @Nullable MonitorConfig monitorConfig, final @NotNull Callable callable) throws Exception { - final @NotNull ISentryLifecycleToken lifecycleToken = Sentry.pushIsolationScope(); - final @NotNull IScopes scopes = Sentry.getCurrentScopes(); - final long startTime = System.currentTimeMillis(); - boolean didError = false; + try (final @NotNull ISentryLifecycleToken ignored = + Sentry.forkedScopes("CheckInUtils").makeCurrent()) { + final @NotNull IScopes scopes = Sentry.getCurrentScopes(); + final long startTime = System.currentTimeMillis(); + boolean didError = false; - TracingUtils.startNewTrace(scopes); + TracingUtils.startNewTrace(scopes); - CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); - if (monitorConfig != null) { - inProgressCheckIn.setMonitorConfig(monitorConfig); - } - @Nullable SentryId checkInId = scopes.captureCheckIn(inProgressCheckIn); - try { - return callable.call(); - } catch (Throwable t) { - didError = true; - throw t; - } finally { - final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; - CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); - checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); - scopes.captureCheckIn(checkIn); - lifecycleToken.close(); + CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); + if (monitorConfig != null) { + inProgressCheckIn.setMonitorConfig(monitorConfig); + } + @Nullable SentryId checkInId = scopes.captureCheckIn(inProgressCheckIn); + try { + return callable.call(); + } catch (Throwable t) { + didError = true; + throw t; + } finally { + final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; + CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); + checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); + scopes.captureCheckIn(checkIn); + } } } diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index 65a1bc49f7..3359c9b788 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -712,8 +712,6 @@ class CombinedScopeViewTest { assertNull(fixture.globalScope.contexts["someList"]) } - // TODO [HSM] test all setContext methods - @Test fun `combines attachments from all scopes`() { val combined = fixture.getSut() diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt index 1b886aea5b..62ffd3d0cc 100644 --- a/sentry/src/test/java/io/sentry/ScopesTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesTest.kt @@ -883,6 +883,43 @@ class ScopesTest { } //endregion + //region withIsolationScope tests + @Test + fun `when withIsolationScope is called on disabled client, execute on NoOpScope`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + sut.close() + + sut.withIsolationScope(scopeCallback) + verify(scopeCallback).run(NoOpScope.getInstance()) + } + + @Test + fun `when withIsolationScope is called with alive client, run should be called`() { + val (sut) = getEnabledScopes() + + val scopeCallback = mock() + + sut.withIsolationScope(scopeCallback) + verify(scopeCallback).run(any()) + } + + @Test + fun `when withIsolationScope throws an exception then it should be caught`() { + val (scopes, _, logger) = getEnabledScopes() + + val exception = Exception("scope callback exception") + val scopeCallback = ScopeCallback { + throw exception + } + + scopes.withIsolationScope(scopeCallback) + + verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) + } + //endregion + //region configureScope tests @Test fun `when configureScope is called on disabled client, do nothing`() { diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index 889459dd35..943837d697 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -60,10 +60,11 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.pushIsolationScope() }.then { - scopes.pushIsolationScope() - lifecycleToken + sentry.`when` { Sentry.forkedScopes(any()) }.then { + scopes.forkedScopes("test") } + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) whenever(scopes.options).thenReturn(SentryOptions()) val returnValue = CheckInUtils.withCheckIn("monitor-1") { return@withCheckIn "test1" @@ -71,7 +72,8 @@ class CheckInUtilsTest { assertEquals("test1", returnValue) inOrder(scopes, lifecycleToken) { - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -96,10 +98,11 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.pushIsolationScope() }.then { - scopes.pushIsolationScope() - lifecycleToken + sentry.`when` { Sentry.forkedScopes(any()) }.then { + scopes.forkedScopes("test") } + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) try { CheckInUtils.withCheckIn("monitor-1") { @@ -111,7 +114,8 @@ class CheckInUtilsTest { } inOrder(scopes, lifecycleToken) { - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -136,10 +140,11 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.pushIsolationScope() }.then { - scopes.pushIsolationScope() - lifecycleToken + sentry.`when` { Sentry.forkedScopes(any()) }.then { + scopes.forkedScopes("test") } + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)) val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) { @@ -148,7 +153,8 @@ class CheckInUtilsTest { assertEquals("test1", returnValue) inOrder(scopes, lifecycleToken) { - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { @@ -174,10 +180,11 @@ class CheckInUtilsTest { val scopes = mock() val lifecycleToken = mock() sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) - sentry.`when` { Sentry.pushIsolationScope() }.then { - scopes.pushIsolationScope() - lifecycleToken + sentry.`when` { Sentry.forkedScopes(any()) }.then { + scopes.forkedScopes("test") } + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) whenever(scopes.options).thenReturn(SentryOptions()) val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)).apply { failureIssueThreshold = 10 @@ -189,7 +196,8 @@ class CheckInUtilsTest { assertEquals("test1", returnValue) inOrder(scopes, lifecycleToken) { - verify(scopes).pushIsolationScope() + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() verify(scopes).configureScope(any()) verify(scopes).captureCheckIn( check { From c9b6f8bee293d921d39efe49283e730bdd5ce665 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 23 Apr 2024 15:26:55 +0200 Subject: [PATCH 44/57] more --- sentry/api/sentry.api | 8 ++++---- sentry/src/main/java/io/sentry/CombinedScopeView.java | 10 +++++----- sentry/src/main/java/io/sentry/IScope.java | 2 +- sentry/src/main/java/io/sentry/NoOpScope.java | 2 +- sentry/src/main/java/io/sentry/Scope.java | 2 +- .../src/test/java/io/sentry/CombinedScopeViewTest.kt | 6 +++--- sentry/src/test/java/io/sentry/ScopesTest.kt | 4 ---- 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 22f0210afb..42446f8e58 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -259,12 +259,12 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope { public fun getClient ()Lio/sentry/ISentryClient; public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEventProcessors ()Ljava/util/List; + public fun getEventProcessorsWithOrder ()Ljava/util/List; public fun getExtras ()Ljava/util/Map; public fun getFingerprint ()Ljava/util/List; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; - public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; @@ -800,12 +800,12 @@ public abstract interface class io/sentry/IScope { public abstract fun getClient ()Lio/sentry/ISentryClient; public abstract fun getContexts ()Lio/sentry/protocol/Contexts; public abstract fun getEventProcessors ()Ljava/util/List; + public abstract fun getEventProcessorsWithOrder ()Ljava/util/List; public abstract fun getExtras ()Ljava/util/Map; public abstract fun getFingerprint ()Ljava/util/List; public abstract fun getLastEventId ()Lio/sentry/protocol/SentryId; public abstract fun getLevel ()Lio/sentry/SentryLevel; public abstract fun getOptions ()Lio/sentry/SentryOptions; - public abstract fun getOrderedEventProcessors ()Ljava/util/List; public abstract fun getPropagationContext ()Lio/sentry/PropagationContext; public abstract fun getRequest ()Lio/sentry/protocol/Request; public abstract fun getScreen ()Ljava/lang/String; @@ -1463,13 +1463,13 @@ public final class io/sentry/NoOpScope : io/sentry/IScope { public fun getClient ()Lio/sentry/ISentryClient; public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEventProcessors ()Ljava/util/List; + public fun getEventProcessorsWithOrder ()Ljava/util/List; public fun getExtras ()Ljava/util/Map; public fun getFingerprint ()Ljava/util/List; public static fun getInstance ()Lio/sentry/NoOpScope; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; - public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; @@ -1909,12 +1909,12 @@ public final class io/sentry/Scope : io/sentry/IScope { public fun getClient ()Lio/sentry/ISentryClient; public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEventProcessors ()Ljava/util/List; + public fun getEventProcessorsWithOrder ()Ljava/util/List; public fun getExtras ()Ljava/util/Map; public fun getFingerprint ()Ljava/util/List; public fun getLastEventId ()Lio/sentry/protocol/SentryId; public fun getLevel ()Lio/sentry/SentryLevel; public fun getOptions ()Lio/sentry/SentryOptions; - public fun getOrderedEventProcessors ()Ljava/util/List; public fun getPropagationContext ()Lio/sentry/PropagationContext; public fun getRequest ()Lio/sentry/protocol/Request; public fun getScreen ()Ljava/lang/String; diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 66e557e16d..2ed33d56ab 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -358,18 +358,18 @@ public void clearAttachments() { } @Override - public @NotNull List getOrderedEventProcessors() { + public @NotNull List getEventProcessorsWithOrder() { final @NotNull List allEventProcessors = new CopyOnWriteArrayList<>(); - allEventProcessors.addAll(globalScope.getOrderedEventProcessors()); - allEventProcessors.addAll(isolationScope.getOrderedEventProcessors()); - allEventProcessors.addAll(scope.getOrderedEventProcessors()); + allEventProcessors.addAll(globalScope.getEventProcessorsWithOrder()); + allEventProcessors.addAll(isolationScope.getEventProcessorsWithOrder()); + allEventProcessors.addAll(scope.getEventProcessorsWithOrder()); Collections.sort(allEventProcessors); return allEventProcessors; } @Override public @NotNull List getEventProcessors() { - return EventProcessorUtils.unwrap(getOrderedEventProcessors()); + return EventProcessorUtils.unwrap(getEventProcessorsWithOrder()); } @Override diff --git a/sentry/src/main/java/io/sentry/IScope.java b/sentry/src/main/java/io/sentry/IScope.java index 8259a225ac..e7bfd55909 100644 --- a/sentry/src/main/java/io/sentry/IScope.java +++ b/sentry/src/main/java/io/sentry/IScope.java @@ -310,7 +310,7 @@ public interface IScope { @ApiStatus.Internal @NotNull - List getOrderedEventProcessors(); + List getEventProcessorsWithOrder(); /** * Adds an event processor to the Scope's event processors list diff --git a/sentry/src/main/java/io/sentry/NoOpScope.java b/sentry/src/main/java/io/sentry/NoOpScope.java index 5335f49519..0226310b4b 100644 --- a/sentry/src/main/java/io/sentry/NoOpScope.java +++ b/sentry/src/main/java/io/sentry/NoOpScope.java @@ -186,7 +186,7 @@ public void clearAttachments() {} @ApiStatus.Internal @Override - public @NotNull List getOrderedEventProcessors() { + public @NotNull List getEventProcessorsWithOrder() { return new ArrayList<>(); } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index e894445bd8..009feeb1b2 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -779,7 +779,7 @@ public List getEventProcessors() { @ApiStatus.Internal @NotNull @Override - public List getOrderedEventProcessors() { + public List getEventProcessorsWithOrder() { // TODO [HSM] This isn't actually ordered but only gets ordered in CombinedScopeView return eventProcessors; } diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index 3359c9b788..3bc51eaaf9 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -282,7 +282,7 @@ class CombinedScopeViewTest { } @Test - fun `prefers transaction andspan from current scope`() { + fun `prefers transaction and span from current scope`() { val combined = fixture.getSut() fixture.scope.setTransaction(createTransaction("scopeTransaction")) fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) @@ -293,7 +293,7 @@ class CombinedScopeViewTest { } @Test - fun `uses isolation scope transaction andspan if none in current scope`() { + fun `uses isolation scope transaction and span if none in current scope`() { val combined = fixture.getSut() fixture.isolationScope.setTransaction(createTransaction("isolationTransaction")) fixture.globalScope.setTransaction(createTransaction("globalTransaction")) @@ -303,7 +303,7 @@ class CombinedScopeViewTest { } @Test - fun `uses global transaction andscope span if none in current or isolation scope`() { + fun `uses global transaction and scope span if none in current or isolation scope`() { val combined = fixture.getSut() fixture.globalScope.setTransaction(createTransaction("globalTransaction")) diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt index 62ffd3d0cc..5bcecc31f6 100644 --- a/sentry/src/test/java/io/sentry/ScopesTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesTest.kt @@ -85,9 +85,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) options.addIntegration(integrationMock) -// val expected = HubAdapter.getInstance() val scopes = createScopes(options) -// verify(integrationMock).register(expected, options) scopes.forkedScopes("test") verifyNoMoreInteractions(integrationMock) } @@ -100,9 +98,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) options.addIntegration(integrationMock) -// val expected = HubAdapter.getInstance() val scopes = createScopes(options) -// verify(integrationMock).register(expected, options) scopes.forkedCurrentScope("test") verifyNoMoreInteractions(integrationMock) } From 696a809eb72f4059a2225b42b23244f44ceb8c88 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 23 Apr 2024 16:19:26 +0200 Subject: [PATCH 45/57] even more --- .../main/java/io/sentry/protocol/Contexts.java | 4 +++- .../java/io/sentry/CombinedScopeViewTest.kt | 18 +++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sentry/src/main/java/io/sentry/protocol/Contexts.java b/sentry/src/main/java/io/sentry/protocol/Contexts.java index 4e4c7c5c90..40a5914151 100644 --- a/sentry/src/main/java/io/sentry/protocol/Contexts.java +++ b/sentry/src/main/java/io/sentry/protocol/Contexts.java @@ -25,7 +25,7 @@ public class Contexts implements JsonSerializable { private static final long serialVersionUID = 252445813254943011L; private final @NotNull ConcurrentHashMap internalStorage = - new ConcurrentHashMap(); + new ConcurrentHashMap<>(); /** Response lock, Ops should be atomic */ private final @NotNull Object responseLock = new Object(); @@ -146,10 +146,12 @@ public void setResponse(final @NotNull Response response) { } public int size() { + // since this used to extend map return internalStorage.size(); } public int getSize() { + // for kotlin .size return size(); } diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index 3bc51eaaf9..a18e708f86 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -172,10 +172,10 @@ class CombinedScopeViewTest { val eventProcessors = combined.eventProcessors - assertEquals(first, eventProcessors.get(0)) - assertEquals(second, eventProcessors.get(1)) - assertEquals(third, eventProcessors.get(2)) - assertEquals(fourth, eventProcessors.get(3)) + assertEquals(first, eventProcessors[0]) + assertEquals(second, eventProcessors[1]) + assertEquals(third, eventProcessors[2]) + assertEquals(fourth, eventProcessors[3]) } @Test @@ -525,7 +525,7 @@ class CombinedScopeViewTest { } @Test - fun `prefer scope value for tags with same key`() { + fun `prefer current scope value for tags with same key`() { val combined = fixture.getSut() fixture.scope.setTag("aTag", "scopeValue") @@ -596,7 +596,7 @@ class CombinedScopeViewTest { } @Test - fun `prefer scope value for extras with same key`() { + fun `prefer current scope value for extras with same key`() { val combined = fixture.getSut() fixture.scope.setExtra("someExtra", "scopeValue") @@ -852,19 +852,19 @@ class CombinedScopeViewTest { } @Test - fun `getSpecificScope(CURRENT) returns scope`() { + fun `getSpecificScope(CURRENT) returns current scope`() { val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.ISOLATION }) assertSame(fixture.scope, combined.getSpecificScope(ScopeType.CURRENT)) } @Test - fun `getSpecificScope(ISOLATION) returns scope`() { + fun `getSpecificScope(ISOLATION) returns isolation scope`() { val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.CURRENT }) assertSame(fixture.isolationScope, combined.getSpecificScope(ScopeType.ISOLATION)) } @Test - fun `getSpecificScope(GLOBAL) returns scope`() { + fun `getSpecificScope(GLOBAL) returns global scope`() { val combined = fixture.getSut(SentryOptions().also { it.defaultScopeType = ScopeType.CURRENT }) assertSame(fixture.globalScope, combined.getSpecificScope(ScopeType.GLOBAL)) } From 847200d722e467d995eb962484292b07e450113c Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 24 Apr 2024 11:36:48 +0200 Subject: [PATCH 46/57] isEnabled checks client instead of having a property on Scopes --- .../EnvelopeFileObserverIntegrationTest.kt | 8 +-- .../android/core/InternalSentrySdkTest.kt | 13 ++-- .../io/sentry/logback/SentryAppenderTest.kt | 25 +++++-- .../api/sentry-test-support.api | 7 ++ .../src/main/kotlin/io/sentry/test/Mocks.kt | 27 +++++++ .../java/io/sentry/CombinedContextsView.java | 1 - .../java/io/sentry/CombinedScopeView.java | 3 - sentry/src/main/java/io/sentry/Scope.java | 23 +++--- sentry/src/main/java/io/sentry/Scopes.java | 26 ++----- .../io/sentry/util/EventProcessorUtils.java | 5 +- .../java/io/sentry/CombinedScopeViewTest.kt | 15 +++- .../src/test/java/io/sentry/HubAdapterTest.kt | 3 +- .../test/java/io/sentry/ScopesAdapterTest.kt | 3 +- sentry/src/test/java/io/sentry/ScopesTest.kt | 71 +++++++++++++------ sentry/src/test/java/io/sentry/SentryTest.kt | 5 +- .../test/java/io/sentry/SentryTracerTest.kt | 4 +- sentry/src/test/java/io/sentry/StackTest.kt | 3 +- ...UncaughtExceptionHandlerIntegrationTest.kt | 3 +- .../sentry/metrics/MetricsIntegrationTest.kt | 1 + 19 files changed, 157 insertions(+), 89 deletions(-) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt index 82d0543833..69b2eee2ec 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt @@ -2,14 +2,12 @@ package io.sentry.android.core import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.ILogger -import io.sentry.IScope import io.sentry.IScopes -import io.sentry.Scope -import io.sentry.Scopes import io.sentry.SentryLevel import io.sentry.SentryOptions import io.sentry.test.DeferredExecutorService import io.sentry.test.ImmediateExecutorService +import io.sentry.test.createTestScopes import org.junit.runner.RunWith import org.mockito.kotlin.eq import org.mockito.kotlin.mock @@ -74,9 +72,7 @@ class EnvelopeFileObserverIntegrationTest { options.cacheDirPath = file.absolutePath options.addIntegration(integrationMock) options.setSerializer(mock()) - val globalScope = Scope(options) - val scopes = Scopes(mock(), mock(), globalScope, "test") -// verify(integrationMock).register(expected, options) + val scopes = createTestScopes(options) scopes.close() verify(integrationMock).close() } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt index 4a8d5217dd..07a120d527 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt @@ -8,7 +8,6 @@ import io.sentry.Hint import io.sentry.IScope import io.sentry.Scope import io.sentry.ScopeType -import io.sentry.Scopes import io.sentry.Sentry import io.sentry.SentryEnvelope import io.sentry.SentryEnvelopeHeader @@ -24,6 +23,7 @@ import io.sentry.protocol.Contexts import io.sentry.protocol.Mechanism import io.sentry.protocol.SentryId import io.sentry.protocol.User +import io.sentry.test.createTestScopes import io.sentry.transport.ITransport import io.sentry.transport.RateLimiter import org.junit.runner.RunWith @@ -106,13 +106,14 @@ class InternalSentrySdkTest { @BeforeTest fun `set up`() { + Sentry.close() context = ApplicationProvider.getApplicationContext() DeviceInfoUtil.resetInstance() } @Test fun `current scope returns null when scopes is no-op`() { - Sentry.getCurrentScopes().close() + Sentry.setCurrentScopes(createTestScopes(enabled = false)) val scope = InternalSentrySdk.getCurrentScope() assertNull(scope) } @@ -122,9 +123,7 @@ class InternalSentrySdkTest { val options = SentryOptions().apply { dsn = "https://key@uri/1234567" } - Sentry.setCurrentScopes( - Scopes(Scope(options), Scope(options), Scope(options), "test") - ) + Sentry.setCurrentScopes(createTestScopes(options)) val scope = InternalSentrySdk.getCurrentScope() assertNotNull(scope) } @@ -134,9 +133,7 @@ class InternalSentrySdkTest { val options = SentryOptions().apply { dsn = "https://key@uri/1234567" } - Sentry.setCurrentScopes( - Scopes(Scope(options), Scope(options), Scope(options), "test") - ) + Sentry.setCurrentScopes(createTestScopes(options)) Sentry.addBreadcrumb("test") Sentry.configureScope(ScopeType.CURRENT) { scope -> scope.addBreadcrumb(Breadcrumb("currentBreadcrumb")) } Sentry.configureScope(ScopeType.ISOLATION) { scope -> scope.addBreadcrumb(Breadcrumb("isolationBreadcrumb")) } diff --git a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt index 96c3dad9f1..3abc2cdd11 100644 --- a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt +++ b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt @@ -35,17 +35,18 @@ import kotlin.test.assertNull import kotlin.test.assertTrue class SentryAppenderTest { - private class Fixture(dsn: String? = "http://key@localhost/proj", minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null, contextTags: List? = null, encoder: Encoder? = null, sendDefaultPii: Boolean = false) { + private class Fixture(dsn: String? = "http://key@localhost/proj", minimumBreadcrumbLevel: Level? = null, minimumEventLevel: Level? = null, contextTags: List? = null, encoder: Encoder? = null, sendDefaultPii: Boolean = false, options: SentryOptions = SentryOptions(), startLater: Boolean = false) { val logger: Logger = LoggerFactory.getLogger(SentryAppenderTest::class.java) val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext val transportFactory = mock() val transport = mock() val utcTimeZone: ZoneId = ZoneId.of("UTC") + val appender = SentryAppender() + var encoder: Encoder? = null init { whenever(this.transportFactory.create(any(), any())).thenReturn(transport) - val appender = SentryAppender() - val options = SentryOptions() + this.encoder = encoder options.dsn = dsn options.isSendDefaultPii = sendDefaultPii contextTags?.forEach { options.addContextTag(it) } @@ -59,6 +60,12 @@ class SentryAppenderTest { val rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME) rootLogger.level = Level.TRACE rootLogger.addAppender(appender) + if (!startLater) { + start() + } + } + + fun start() { appender.start() encoder?.start() loggerContext.start() @@ -82,17 +89,25 @@ class SentryAppenderTest { @Test fun `does not initialize Sentry if Sentry is already enabled`() { - fixture = Fixture() + fixture = Fixture( + startLater = true, + options = SentryOptions().also { + it.setTag("only-present-if-logger-init-was-run", "another-value") + } + ) Sentry.init { it.dsn = "http://key@localhost/proj" it.environment = "manual-environment" it.setTransportFactory(fixture.transportFactory) + it.setTag("tag-from-first-init", "some-value") } + fixture.start() + fixture.logger.error("testing environment field") verify(fixture.transport).send( checkEvent { event -> - assertEquals("manual-environment", event.environment) + assertNull(event.tags?.get("only-present-if-logger-init-was-run")) }, anyOrNull() ) diff --git a/sentry-test-support/api/sentry-test-support.api b/sentry-test-support/api/sentry-test-support.api index dd1a4b69d3..27d7034690 100644 --- a/sentry-test-support/api/sentry-test-support.api +++ b/sentry-test-support/api/sentry-test-support.api @@ -31,6 +31,13 @@ public final class io/sentry/test/ImmediateExecutorService : io/sentry/ISentryEx public fun submit (Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Future; } +public final class io/sentry/test/MocksKt { + public static final fun createSentryClientMock (Z)Lio/sentry/ISentryClient; + public static synthetic fun createSentryClientMock$default (ZILjava/lang/Object;)Lio/sentry/ISentryClient; + public static final fun createTestScopes (Lio/sentry/SentryOptions;ZLio/sentry/IScope;Lio/sentry/IScope;Lio/sentry/IScope;)Lio/sentry/Scopes; + public static synthetic fun createTestScopes$default (Lio/sentry/SentryOptions;ZLio/sentry/IScope;Lio/sentry/IScope;Lio/sentry/IScope;ILjava/lang/Object;)Lio/sentry/Scopes; +} + public final class io/sentry/test/ReflectionKt { public static final fun collectInterfaceHierarchy (Ljava/lang/Class;)Ljava/util/List; public static final fun containsMethod (Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Class;)Z diff --git a/sentry-test-support/src/main/kotlin/io/sentry/test/Mocks.kt b/sentry-test-support/src/main/kotlin/io/sentry/test/Mocks.kt index 168f3e6372..26f0066beb 100644 --- a/sentry-test-support/src/main/kotlin/io/sentry/test/Mocks.kt +++ b/sentry-test-support/src/main/kotlin/io/sentry/test/Mocks.kt @@ -1,11 +1,19 @@ // ktlint-disable filename package io.sentry.test +import io.sentry.IScope +import io.sentry.ISentryClient import io.sentry.ISentryExecutorService +import io.sentry.Scope +import io.sentry.Scopes +import io.sentry.SentryOptions import io.sentry.backpressure.IBackpressureMonitor +import org.mockito.kotlin.any import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever import java.util.concurrent.Callable import java.util.concurrent.Future +import java.util.concurrent.atomic.AtomicBoolean class ImmediateExecutorService : ISentryExecutorService { override fun submit(runnable: Runnable): Future<*> { @@ -65,3 +73,22 @@ class DeferredExecutorService : ISentryExecutorService { fun hasScheduledRunnables(): Boolean = scheduledRunnables.isNotEmpty() } + +fun createSentryClientMock(enabled: Boolean = true) = mock().also { + val isEnabled = AtomicBoolean(enabled) + whenever(it.isEnabled).then { isEnabled.get() } + whenever(it.close()).then { isEnabled.set(false) } + whenever(it.close(any())).then { isEnabled.set(false) } +} + +fun createTestScopes(options: SentryOptions? = null, enabled: Boolean = true, scope: IScope? = null, isolationScope: IScope? = null, globalScope: IScope? = null): Scopes { + val optionsToUse = options ?: SentryOptions().also { it.dsn = "https://key@sentry.io/proj" } + val scopeToUse = scope ?: Scope(optionsToUse) + val isolationScopeToUse = isolationScope ?: Scope(optionsToUse) + val globalScopeToUse = globalScope ?: Scope(optionsToUse) + return Scopes(scopeToUse, isolationScopeToUse, globalScopeToUse, "test").also { + if (enabled) { + it.bindClient(createSentryClientMock()) + } + } +} diff --git a/sentry/src/main/java/io/sentry/CombinedContextsView.java b/sentry/src/main/java/io/sentry/CombinedContextsView.java index 40c220bb31..3720fa5b93 100644 --- a/sentry/src/main/java/io/sentry/CombinedContextsView.java +++ b/sentry/src/main/java/io/sentry/CombinedContextsView.java @@ -248,7 +248,6 @@ public boolean containsKey(final @NotNull Object key) { @Override public @Nullable Object remove(final @NotNull Object key) { - // TODO [HSM] should this remove from all contexts? return getDefaultContexts().remove(key); } diff --git a/sentry/src/main/java/io/sentry/CombinedScopeView.java b/sentry/src/main/java/io/sentry/CombinedScopeView.java index 2ed33d56ab..86b90379ad 100644 --- a/sentry/src/main/java/io/sentry/CombinedScopeView.java +++ b/sentry/src/main/java/io/sentry/CombinedScopeView.java @@ -231,7 +231,6 @@ public void setTag(@NotNull String key, @NotNull String value) { @Override public void removeTag(@NotNull String key) { - // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeTag(key); } @@ -251,7 +250,6 @@ public void setExtra(@NotNull String key, @NotNull String value) { @Override public void removeExtra(@NotNull String key) { - // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeExtra(key); } @@ -301,7 +299,6 @@ public void setContexts(@NotNull String key, @NotNull Character value) { @Override public void removeContexts(@NotNull String key) { - // TODO [HSM] should this go to all scopes? getDefaultWriteScope().removeContexts(key); } diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index 009feeb1b2..3df40a2b01 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -780,7 +780,6 @@ public List getEventProcessors() { @NotNull @Override public List getEventProcessorsWithOrder() { - // TODO [HSM] This isn't actually ordered but only gets ordered in CombinedScopeView return eventProcessors; } @@ -1041,17 +1040,17 @@ public void setSpanContext( @ApiStatus.Internal @Override public void replaceOptions(final @NotNull SentryOptions options) { - // TODO [HSM] check if already enabled and noop in that case? - // if (!isEnabled()) {} - this.options = options; - final Queue oldBreadcrumbs = breadcrumbs; - breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs()); - for (Breadcrumb breadcrumb : oldBreadcrumbs) { - /* - this should trigger beforeBreadcrumb - and notify observers for breadcrumbs added before options where customized in Sentry.init - */ - addBreadcrumb(breadcrumb); + if (!getClient().isEnabled()) { + this.options = options; + final Queue oldBreadcrumbs = breadcrumbs; + breadcrumbs = createBreadcrumbsList(options.getMaxBreadcrumbs()); + for (Breadcrumb breadcrumb : oldBreadcrumbs) { + /* + this should trigger beforeBreadcrumb + and notify observers for breadcrumbs added before options where customized in Sentry.init + */ + addBreadcrumb(breadcrumb); + } } } diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 3d9b39a936..59540d105b 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -31,7 +31,6 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface { private final @Nullable Scopes parentScopes; private final @NotNull String creator; - private volatile boolean isEnabled; private final @NotNull TracesSampler tracesSampler; private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; private final @NotNull MetricsApi metricsApi; @@ -63,10 +62,6 @@ private Scopes( validateOptions(options); this.tracesSampler = new TracesSampler(options); this.transactionPerformanceCollector = options.getTransactionPerformanceCollector(); - - // TODO [HSM] Checking isEnabled may not be what we want with global scope anymore - this.isEnabled = true; - this.metricsApi = new MetricsApi(this); } @@ -124,10 +119,9 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) { return Sentry.forkedRootScopes(creator); } - // TODO [HSM] always read from root scope? @Override public boolean isEnabled() { - return isEnabled; + return getClient().isEnabled(); } @Override @@ -428,7 +422,6 @@ public void close(final boolean isRestarting) { } catch (Throwable e) { getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e); } - isEnabled = false; } } @@ -695,18 +688,12 @@ public void configureScope( @Override public void bindClient(final @NotNull ISentryClient client) { - if (!isEnabled()) { - getOptions() - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'bindClient' call is a no-op."); + if (client != null) { + getOptions().getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); + getCombinedScopeView().bindClient(client); } else { - if (client != null) { - getOptions().getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); - getCombinedScopeView().bindClient(client); - } else { - getOptions().getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); - getCombinedScopeView().bindClient(NoOpSentryClient.getInstance()); - } + getOptions().getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); + getCombinedScopeView().bindClient(NoOpSentryClient.getInstance()); } } @@ -939,7 +926,6 @@ public void reportFullyDisplayed() { @NotNull PropagationContext propagationContext = PropagationContext.fromHeaders(getOptions().getLogger(), sentryTrace, baggageHeaders); - // TODO [HSM] should this go on isolation scope? configureScope( (scope) -> { scope.setPropagationContext(propagationContext); diff --git a/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java b/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java index b47d40ecd9..8fb73982c0 100644 --- a/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java +++ b/sentry/src/main/java/io/sentry/util/EventProcessorUtils.java @@ -2,6 +2,7 @@ import io.sentry.EventProcessor; import io.sentry.internal.eventprocessor.EventProcessorAndOrder; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.jetbrains.annotations.Nullable; @@ -10,7 +11,7 @@ public final class EventProcessorUtils { public static List unwrap( final @Nullable List orderedEventProcessor) { - final List eventProcessors = new CopyOnWriteArrayList<>(); + final List eventProcessors = new ArrayList<>(); if (orderedEventProcessor != null) { for (EventProcessorAndOrder eventProcessorAndOrder : orderedEventProcessor) { @@ -18,6 +19,6 @@ public static List unwrap( } } - return eventProcessors; + return new CopyOnWriteArrayList<>(eventProcessors); } } diff --git a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt index a18e708f86..b73a7adcc8 100644 --- a/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt +++ b/sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt @@ -4,6 +4,7 @@ import io.sentry.protocol.Device import io.sentry.protocol.Request import io.sentry.protocol.SentryId import io.sentry.protocol.User +import io.sentry.test.createTestScopes import junit.framework.TestCase.assertFalse import junit.framework.TestCase.assertTrue import org.junit.Assert.assertNotEquals @@ -38,7 +39,7 @@ class CombinedScopeViewTest { globalScope = Scope(options) isolationScope = Scope(options) scope = Scope(options) - scopes = Scopes(scope, isolationScope, globalScope, "test") + scopes = createTestScopes(options, scope = scope, isolationScope = isolationScope, globalScope = globalScope) return CombinedScopeView(globalScope, isolationScope, scope) } @@ -801,6 +802,9 @@ class CombinedScopeViewTest { @Test fun `uses isolation scope client if noop on current scope`() { val combined = fixture.getSut() + fixture.scope.bindClient(NoOpSentryClient.getInstance()) + fixture.isolationScope.bindClient(NoOpSentryClient.getInstance()) + fixture.globalScope.bindClient(NoOpSentryClient.getInstance()) val isolationClient = SentryClient(fixture.options) fixture.isolationScope.bindClient(isolationClient) @@ -814,6 +818,9 @@ class CombinedScopeViewTest { @Test fun `uses global scope client if noop on current and isolation scope`() { val combined = fixture.getSut() + fixture.scope.bindClient(NoOpSentryClient.getInstance()) + fixture.isolationScope.bindClient(NoOpSentryClient.getInstance()) + fixture.globalScope.bindClient(NoOpSentryClient.getInstance()) val globalClient = SentryClient(fixture.options) fixture.globalScope.bindClient(globalClient) @@ -824,6 +831,10 @@ class CombinedScopeViewTest { @Test fun `binds client to default scope`() { val combined = fixture.getSut() + fixture.scope.bindClient(NoOpSentryClient.getInstance()) + fixture.isolationScope.bindClient(NoOpSentryClient.getInstance()) + fixture.globalScope.bindClient(NoOpSentryClient.getInstance()) + val client = SentryClient(fixture.options) combined.bindClient(client) @@ -880,7 +891,7 @@ class CombinedScopeViewTest { whenever(globalScope.options).thenReturn(options) val exception = RuntimeException("someEx") - val transaction = createTransaction("aTransaction", Scopes(scope, isolationScope, globalScope, "test")) + val transaction = createTransaction("aTransaction", createTestScopes(options = options, scope = scope, isolationScope = isolationScope, globalScope = globalScope)) combined.setSpanContext(exception, transaction, "aTransaction") verify(scope, never()).setSpanContext(any(), any(), any()) diff --git a/sentry/src/test/java/io/sentry/HubAdapterTest.kt b/sentry/src/test/java/io/sentry/HubAdapterTest.kt index c8e17bfb2b..9c6ff6ddf1 100644 --- a/sentry/src/test/java/io/sentry/HubAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/HubAdapterTest.kt @@ -2,6 +2,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User +import io.sentry.test.createSentryClientMock import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq @@ -190,7 +191,7 @@ class HubAdapterTest { } @Test fun `bindClient calls Hub`() { - val client = mock() + val client = createSentryClientMock() HubAdapter.getInstance().bindClient(client) verify(scopes).bindClient(eq(client)) } diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt index 38fc9875b0..19123a23ed 100644 --- a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -2,6 +2,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User +import io.sentry.test.createSentryClientMock import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq @@ -190,7 +191,7 @@ class ScopesAdapterTest { } @Test fun `bindClient calls Scopes`() { - val client = mock() + val client = createSentryClientMock() ScopesAdapter.getInstance().bindClient(client) verify(scopes).bindClient(eq(client)) } diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt index 5bcecc31f6..33a0ce90a8 100644 --- a/sentry/src/test/java/io/sentry/ScopesTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesTest.kt @@ -12,6 +12,8 @@ import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import io.sentry.test.DeferredExecutorService import io.sentry.test.callMethod +import io.sentry.test.createSentryClientMock +import io.sentry.test.createTestScopes import io.sentry.util.HintUtils import io.sentry.util.StringUtils import org.mockito.kotlin.any @@ -68,7 +70,9 @@ class ScopesTest { } private fun createScopes(options: SentryOptions): Scopes { - return Scopes(Scope(options), Scope(options), Scope(options), "test") + return createTestScopes(options).also { + it.bindClient(SentryClient(options)) + } } @Test @@ -244,7 +248,7 @@ class ScopesTest { options.setSerializer(mock()) val sut = createScopes(options) var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } + sut.configureScope(ScopeType.COMBINED) { breadcrumbs = it.breadcrumbs } sut.close() sut.addBreadcrumb(Breadcrumb()) assertTrue(breadcrumbs!!.isEmpty()) @@ -1279,7 +1283,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock(enabled = false) sut.bindClient(mockClient) sut.close() @@ -1294,7 +1298,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) @@ -1309,7 +1313,7 @@ class ScopesTest { setSerializer(mock()) } val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) whenever(mockClient.captureEnvelope(any(), anyOrNull())).thenReturn(SentryId()) val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) @@ -1327,10 +1331,14 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.close() + sut.configureScope(ScopeType.ISOLATION) { scope -> + scope.client.isEnabled + } + sut.startSession() verify(mockClient, never()).captureSession(any(), any()) } @@ -1343,7 +1351,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.startSession() @@ -1358,7 +1366,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.startSession() @@ -1377,7 +1385,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock(enabled = false) sut.bindClient(mockClient) sut.close() @@ -1393,7 +1401,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.endSession() @@ -1408,7 +1416,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.startSession() @@ -1425,7 +1433,7 @@ class ScopesTest { options.release = "0.0.1" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.endSession() @@ -1441,7 +1449,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) sut.close() @@ -1459,7 +1467,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) @@ -1475,7 +1483,7 @@ class ScopesTest { setSerializer(mock()) } val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) whenever(mockClient.captureTransaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(SentryId()) @@ -1491,7 +1499,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) @@ -1506,7 +1514,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) @@ -1522,7 +1530,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) @@ -1541,7 +1549,7 @@ class ScopesTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) val mockBackpressureMonitor = mock() options.backpressureMonitor = mockBackpressureMonitor @@ -1562,7 +1570,7 @@ class ScopesTest { @Test fun `when startTransaction and profiling is enabled, transaction is profiled only if sampled`() { val mockTransactionProfiler = mock() - val mockClient = mock() + val mockClient = createSentryClientMock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } val scopes = generateScopes { it.setTransactionProfiler(mockTransactionProfiler) @@ -1584,7 +1592,7 @@ class ScopesTest { @Test fun `when startTransaction and is sampled but profiling is disabled, transaction is not profiled`() { val mockTransactionProfiler = mock() - val mockClient = mock() + val mockClient = createSentryClientMock() whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } val scopes = generateScopes { it.profilesSampleRate = 0.0 @@ -1785,6 +1793,7 @@ class ScopesTest { // we have to clone the scope, so its isEnabled returns true, but it's still built up from // the old scope preserving its data val clone = sut.forkedScopes("test") + clone.bindClient(createSentryClientMock(enabled = true)) var oldScope: IScope? = null clone.configureScope { scope -> oldScope = scope } assertNull(oldScope!!.transaction) @@ -2166,6 +2175,24 @@ class ScopesTest { assertEquals(span.spanContext.parentSpanId, txn.spanContext.spanId) } + @Test + fun `is considered enabled if client is enabled()`() { + val scopes = generateScopes() as Scopes + val client = mock() + whenever(client.isEnabled).thenReturn(true) + scopes.bindClient(client) + assertTrue(scopes.isEnabled) + } + + @Test + fun `is considered disabled if client is disabled()`() { + val scopes = generateScopes() as Scopes + val client = mock() + whenever(client.isEnabled).thenReturn(false) + scopes.bindClient(client) + assertFalse(scopes.isEnabled) + } + private val dsnTest = "https://key@sentry.io/proj" private fun generateScopes(optionsConfiguration: Sentry.OptionsConfiguration? = null): IScopes { @@ -2191,7 +2218,7 @@ class ScopesTest { options.setLogger(logger) val sut = createScopes(options) - val mockClient = mock() + val mockClient = createSentryClientMock() sut.bindClient(mockClient) return Triple(sut, mockClient, logger) } diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index b1316158b5..28305b8eec 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -15,6 +15,7 @@ import io.sentry.protocol.SdkVersion import io.sentry.protocol.SentryId import io.sentry.protocol.SentryThread import io.sentry.test.ImmediateExecutorService +import io.sentry.test.createSentryClientMock import io.sentry.util.PlatformTestManipulator import io.sentry.util.thread.IMainThreadChecker import io.sentry.util.thread.MainThreadChecker @@ -266,7 +267,7 @@ class SentryTest { fun `captureUserFeedback gets forwarded to client`() { Sentry.init { it.dsn = dsn } - val client = mock() + val client = createSentryClientMock() Sentry.getCurrentScopes().bindClient(client) val userFeedback = UserFeedback(SentryId.EMPTY_ID) @@ -860,7 +861,7 @@ class SentryTest { fun `captureCheckIn gets forwarded to client`() { Sentry.init { it.dsn = dsn } - val client = mock() + val client = createSentryClientMock() Sentry.getCurrentScopes().bindClient(client) val checkIn = CheckIn("some_slug", CheckInStatus.OK) diff --git a/sentry/src/test/java/io/sentry/SentryTracerTest.kt b/sentry/src/test/java/io/sentry/SentryTracerTest.kt index ccd96a2543..d3404a853c 100644 --- a/sentry/src/test/java/io/sentry/SentryTracerTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTracerTest.kt @@ -2,6 +2,7 @@ package io.sentry import io.sentry.protocol.TransactionNameSource import io.sentry.protocol.User +import io.sentry.test.createTestScopes import io.sentry.util.thread.IMainThreadChecker import org.awaitility.kotlin.await import org.mockito.kotlin.any @@ -36,9 +37,8 @@ class SentryTracerTest { options.dsn = "https://key@sentry.io/proj" options.environment = "environment" options.release = "release@3.0.0" - scopes = spy(Scopes(Scope(options), Scope(options), Scope(options), "test")) + scopes = spy(createTestScopes(options)) transactionPerformanceCollector = spy(DefaultTransactionPerformanceCollector(options)) - scopes.bindClient(mock()) } fun getSut( diff --git a/sentry/src/test/java/io/sentry/StackTest.kt b/sentry/src/test/java/io/sentry/StackTest.kt index 13089ab6a4..c8b0aa8a9f 100644 --- a/sentry/src/test/java/io/sentry/StackTest.kt +++ b/sentry/src/test/java/io/sentry/StackTest.kt @@ -1,6 +1,7 @@ package io.sentry import io.sentry.Stack.StackItem +import io.sentry.test.createSentryClientMock import org.mockito.kotlin.mock import kotlin.test.Test import kotlin.test.assertEquals @@ -10,7 +11,7 @@ class StackTest { private class Fixture { val options = SentryOptions() - val client = mock() + val client = createSentryClientMock() val scope = Scope(options) lateinit var rootItem: StackItem diff --git a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt index 51e6d32914..93b84c9892 100644 --- a/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/UncaughtExceptionHandlerIntegrationTest.kt @@ -5,6 +5,7 @@ import io.sentry.exception.ExceptionMechanismException import io.sentry.hints.DiskFlushNotification import io.sentry.hints.EventDropReason.MULTITHREADED_DEDUPLICATION import io.sentry.protocol.SentryId +import io.sentry.test.createTestScopes import io.sentry.util.HintUtils import org.mockito.kotlin.any import org.mockito.kotlin.argThat @@ -105,7 +106,7 @@ class UncaughtExceptionHandlerIntegrationTest { options.addIntegration(integrationMock) options.cacheDirPath = fixture.file.absolutePath options.setSerializer(mock()) - val scopes = Scopes(Scope(options), Scope(options), Scope(options), "test") + val scopes = createTestScopes(options) scopes.close() verify(integrationMock).close() } diff --git a/sentry/src/test/java/io/sentry/metrics/MetricsIntegrationTest.kt b/sentry/src/test/java/io/sentry/metrics/MetricsIntegrationTest.kt index 56d40f5b86..bf7a1f0f43 100644 --- a/sentry/src/test/java/io/sentry/metrics/MetricsIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/metrics/MetricsIntegrationTest.kt @@ -70,6 +70,7 @@ class MetricsIntegrationTest { Sentry.init(options) val client = mock() + whenever(client.isEnabled).thenReturn(true) val aggregator = MetricsAggregator(options, client) whenever(client.metricsAggregator).thenReturn(aggregator) Sentry.bindClient(client) From 8e86d3b54c03eff74b53fdbee4801ae0300e5241 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 25 Apr 2024 11:13:32 +0200 Subject: [PATCH 47/57] Use SentryOptions.empty --- .../src/main/java/io/sentry/android/core/SentryAndroid.java | 4 ---- sentry/src/main/java/io/sentry/Sentry.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index b677cc5e34..424de4d82e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -24,10 +24,6 @@ /** Sentry initialization class */ public final class SentryAndroid { - static { - Sentry.getGlobalScope().replaceOptions(new SentryAndroidOptions()); - } - // SystemClock.uptimeMillis() isn't affected by phone provider or clock changes. private static final long sdkInitMillis = SystemClock.uptimeMillis(); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 4c751f68f1..05cba51dc0 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -55,7 +55,7 @@ private Sentry() {} */ // TODO [HSM] use SentryOptions.empty and address // https://github.com/getsentry/sentry-java/issues/2541 - private static volatile @NotNull IScope globalScope = new Scope(new SentryOptions()); + private static volatile @NotNull IScope globalScope = new Scope(SentryOptions.empty()); /** Default value for globalHubMode is false */ private static final boolean GLOBAL_HUB_DEFAULT_MODE = false; From 06db22842dbbb207b7fadb7a3f61a49680003ca6 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 25 Apr 2024 13:44:11 +0200 Subject: [PATCH 48/57] Remove Hub --- sentry/api/sentry.api | 66 - sentry/src/main/java/io/sentry/Hub.java | 1069 ---------- sentry/src/main/java/io/sentry/Scope.java | 3 +- sentry/src/test/java/io/sentry/HubTest.kt | 2149 --------------------- 4 files changed, 1 insertion(+), 3286 deletions(-) delete mode 100644 sentry/src/main/java/io/sentry/Hub.java delete mode 100644 sentry/src/test/java/io/sentry/HubTest.kt diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 42446f8e58..b201a47d0c 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -517,72 +517,6 @@ public final class io/sentry/HttpStatusCodeRange { public fun isInRange (I)Z } -public final class io/sentry/Hub : io/sentry/IHub, io/sentry/metrics/MetricsApi$IMetricsInterface { - public fun (Lio/sentry/SentryOptions;)V - public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V - public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V - public fun bindClient (Lio/sentry/ISentryClient;)V - public fun captureCheckIn (Lio/sentry/CheckIn;)Lio/sentry/protocol/SentryId; - public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId; - public fun captureException (Ljava/lang/Throwable;Lio/sentry/Hint;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId; - public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/ScopeCallback;)Lio/sentry/protocol/SentryId; - public fun captureTransaction (Lio/sentry/protocol/SentryTransaction;Lio/sentry/TraceContext;Lio/sentry/Hint;Lio/sentry/ProfilingTraceData;)Lio/sentry/protocol/SentryId; - public fun captureUserFeedback (Lio/sentry/UserFeedback;)V - public fun clearBreadcrumbs ()V - public fun clone ()Lio/sentry/IHub; - public synthetic fun clone ()Ljava/lang/Object; - public fun close ()V - public fun close (Z)V - public fun configureScope (Lio/sentry/ScopeType;Lio/sentry/ScopeCallback;)V - public fun continueTrace (Ljava/lang/String;Ljava/util/List;)Lio/sentry/TransactionContext; - public fun endSession ()V - public fun flush (J)V - public fun forkedCurrentScope (Ljava/lang/String;)Lio/sentry/IScopes; - public fun forkedRootScopes (Ljava/lang/String;)Lio/sentry/IScopes; - public fun forkedScopes (Ljava/lang/String;)Lio/sentry/IScopes; - public fun getBaggage ()Lio/sentry/BaggageHeader; - public fun getDefaultTagsForMetrics ()Ljava/util/Map; - public fun getGlobalScope ()Lio/sentry/IScope; - public fun getIsolationScope ()Lio/sentry/IScope; - public fun getLastEventId ()Lio/sentry/protocol/SentryId; - public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator; - public fun getMetricsAggregator ()Lio/sentry/IMetricsAggregator; - public fun getOptions ()Lio/sentry/SentryOptions; - public fun getRateLimiter ()Lio/sentry/transport/RateLimiter; - public fun getScope ()Lio/sentry/IScope; - public fun getSpan ()Lio/sentry/ISpan; - public fun getTraceparent ()Lio/sentry/SentryTraceHeader; - public fun getTransaction ()Lio/sentry/ITransaction; - public fun isCrashedLastRun ()Ljava/lang/Boolean; - public fun isEnabled ()Z - public fun isHealthy ()Z - public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; - public fun metrics ()Lio/sentry/metrics/MetricsApi; - public fun popScope ()V - public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken; - public fun pushScope ()Lio/sentry/ISentryLifecycleToken; - public fun removeExtra (Ljava/lang/String;)V - public fun removeTag (Ljava/lang/String;)V - public fun reportFullyDisplayed ()V - public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V - public fun setFingerprint (Ljava/util/List;)V - public fun setLevel (Lio/sentry/SentryLevel;)V - public fun setSpanContext (Ljava/lang/Throwable;Lio/sentry/ISpan;Ljava/lang/String;)V - public fun setTag (Ljava/lang/String;Ljava/lang/String;)V - public fun setTransaction (Ljava/lang/String;)V - public fun setUser (Lio/sentry/protocol/User;)V - public fun startSession ()V - public fun startSpanForMetric (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan; - public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction; - public fun traceHeaders ()Lio/sentry/SentryTraceHeader; - public fun withIsolationScope (Lio/sentry/ScopeCallback;)V - public fun withScope (Lio/sentry/ScopeCallback;)V -} - public final class io/sentry/HubAdapter : io/sentry/IHub { public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V public fun addBreadcrumb (Lio/sentry/Breadcrumb;Lio/sentry/Hint;)V diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java deleted file mode 100644 index 63081e6cd2..0000000000 --- a/sentry/src/main/java/io/sentry/Hub.java +++ /dev/null @@ -1,1069 +0,0 @@ -package io.sentry; - -import io.sentry.Stack.StackItem; -import io.sentry.clientreport.DiscardReason; -import io.sentry.hints.SessionEndHint; -import io.sentry.hints.SessionStartHint; -import io.sentry.metrics.LocalMetricsAggregator; -import io.sentry.metrics.MetricsApi; -import io.sentry.protocol.SentryId; -import io.sentry.protocol.SentryTransaction; -import io.sentry.protocol.User; -import io.sentry.transport.RateLimiter; -import io.sentry.util.ExceptionUtils; -import io.sentry.util.HintUtils; -import io.sentry.util.Objects; -import io.sentry.util.Pair; -import io.sentry.util.TracingUtils; -import java.io.Closeable; -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -// TODO [HSM] remove Hub class -@Deprecated -public final class Hub implements IHub, MetricsApi.IMetricsInterface { - - private volatile @NotNull SentryId lastEventId; - private final @NotNull SentryOptions options; - private volatile boolean isEnabled; - private final @NotNull Stack stack; - private final @NotNull TracesSampler tracesSampler; - private final @NotNull Map, String>> throwableToSpan = - Collections.synchronizedMap(new WeakHashMap<>()); - private final @NotNull TransactionPerformanceCollector transactionPerformanceCollector; - private final @NotNull MetricsApi metricsApi; - - public Hub(final @NotNull SentryOptions options) { - this(options, createRootStackItem(options)); - // Integrations are no longer registered on Hub ctor, but on Sentry.init - } - - private Hub(final @NotNull SentryOptions options, final @NotNull Stack stack) { - validateOptions(options); - - this.options = options; - this.tracesSampler = new TracesSampler(options); - this.stack = stack; - this.lastEventId = SentryId.EMPTY_ID; - this.transactionPerformanceCollector = options.getTransactionPerformanceCollector(); - - // Integrations will use this Hub instance once registered. - // Make sure Hub ready to be used then. - this.isEnabled = true; - - this.metricsApi = new MetricsApi(this); - } - - private Hub(final @NotNull SentryOptions options, final @NotNull StackItem rootStackItem) { - this(options, new Stack(options.getLogger(), rootStackItem)); - } - - private static void validateOptions(final @NotNull SentryOptions options) { - Objects.requireNonNull(options, "SentryOptions is required."); - if (options.getDsn() == null || options.getDsn().isEmpty()) { - throw new IllegalArgumentException( - "Hub requires a DSN to be instantiated. Considering using the NoOpHub if no DSN is available."); - } - } - - private static StackItem createRootStackItem(final @NotNull SentryOptions options) { - validateOptions(options); - final IScope scope = new Scope(options); - final ISentryClient client = new SentryClient(options); - return new StackItem(options, client, scope); - } - - @Override - public boolean isEnabled() { - return isEnabled; - } - - @Override - public @NotNull SentryId captureEvent( - final @NotNull SentryEvent event, final @Nullable Hint hint) { - return captureEventInternal(event, hint, null); - } - - @Override - public @NotNull SentryId captureEvent( - final @NotNull SentryEvent event, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback) { - return captureEventInternal(event, hint, callback); - } - - private @NotNull SentryId captureEventInternal( - final @NotNull SentryEvent event, - final @Nullable Hint hint, - final @Nullable ScopeCallback scopeCallback) { - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, "Instance is disabled and this 'captureEvent' call is a no-op."); - } else if (event == null) { - options.getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter."); - } else { - try { - assignTraceContext(event); - final StackItem item = stack.peek(); - - final IScope scope = buildLocalScope(item.getScope(), scopeCallback); - - sentryId = item.getClient().captureEvent(event, scope, hint); - this.lastEventId = sentryId; - } catch (Throwable e) { - options - .getLogger() - .log( - SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e); - } - } - return sentryId; - } - - @Override - public @NotNull SentryId captureMessage( - final @NotNull String message, final @NotNull SentryLevel level) { - return captureMessageInternal(message, level, null); - } - - @Override - public @NotNull SentryId captureMessage( - final @NotNull String message, - final @NotNull SentryLevel level, - final @NotNull ScopeCallback callback) { - return captureMessageInternal(message, level, callback); - } - - private @NotNull SentryId captureMessageInternal( - final @NotNull String message, - final @NotNull SentryLevel level, - final @Nullable ScopeCallback scopeCallback) { - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureMessage' call is a no-op."); - } else if (message == null) { - options.getLogger().log(SentryLevel.WARNING, "captureMessage called with null parameter."); - } else { - try { - final StackItem item = stack.peek(); - - final IScope scope = buildLocalScope(item.getScope(), scopeCallback); - - sentryId = item.getClient().captureMessage(message, level, scope); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing message: " + message, e); - } - } - this.lastEventId = sentryId; - return sentryId; - } - - @ApiStatus.Internal - @Override - public @NotNull SentryId captureEnvelope( - final @NotNull SentryEnvelope envelope, final @Nullable Hint hint) { - Objects.requireNonNull(envelope, "SentryEnvelope is required."); - - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureEnvelope' call is a no-op."); - } else { - try { - final SentryId capturedEnvelopeId = - stack.peek().getClient().captureEnvelope(envelope, hint); - if (capturedEnvelopeId != null) { - sentryId = capturedEnvelopeId; - } - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e); - } - } - return sentryId; - } - - @Override - public @NotNull SentryId captureException( - final @NotNull Throwable throwable, final @Nullable Hint hint) { - return captureExceptionInternal(throwable, hint, null); - } - - @Override - public @NotNull SentryId captureException( - final @NotNull Throwable throwable, - final @Nullable Hint hint, - final @NotNull ScopeCallback callback) { - - return captureExceptionInternal(throwable, hint, callback); - } - - private @NotNull SentryId captureExceptionInternal( - final @NotNull Throwable throwable, - final @Nullable Hint hint, - final @Nullable ScopeCallback scopeCallback) { - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureException' call is a no-op."); - } else if (throwable == null) { - options.getLogger().log(SentryLevel.WARNING, "captureException called with null parameter."); - } else { - try { - final StackItem item = stack.peek(); - final SentryEvent event = new SentryEvent(throwable); - assignTraceContext(event); - - final IScope scope = buildLocalScope(item.getScope(), scopeCallback); - - sentryId = item.getClient().captureEvent(event, scope, hint); - } catch (Throwable e) { - options - .getLogger() - .log( - SentryLevel.ERROR, "Error while capturing exception: " + throwable.getMessage(), e); - } - } - this.lastEventId = sentryId; - return sentryId; - } - - private void assignTraceContext(final @NotNull SentryEvent event) { - if (options.isTracingEnabled() && event.getThrowable() != null) { - final Pair, String> pair = - throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable())); - if (pair != null) { - final WeakReference spanWeakRef = pair.getFirst(); - if (event.getContexts().getTrace() == null && spanWeakRef != null) { - final ISpan span = spanWeakRef.get(); - if (span != null) { - event.getContexts().setTrace(span.getSpanContext()); - } - } - final String transactionName = pair.getSecond(); - if (event.getTransaction() == null && transactionName != null) { - event.setTransaction(transactionName); - } - } - } - } - - @Override - public void captureUserFeedback(final @NotNull UserFeedback userFeedback) { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureUserFeedback' call is a no-op."); - } else { - try { - final StackItem item = stack.peek(); - item.getClient().captureUserFeedback(userFeedback); - } catch (Throwable e) { - options - .getLogger() - .log( - SentryLevel.ERROR, - "Error while capturing captureUserFeedback: " + userFeedback.toString(), - e); - } - } - } - - @Override - public void startSession() { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op."); - } else { - final StackItem item = this.stack.peek(); - final Scope.SessionPair pair = item.getScope().startSession(); - if (pair != null) { - // TODO: add helper overload `captureSessions` to pass a list of sessions and submit a - // single envelope - // Or create the envelope here with both items and call `captureEnvelope` - if (pair.getPrevious() != null) { - final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint()); - - item.getClient().captureSession(pair.getPrevious(), hint); - } - - final Hint hint = HintUtils.createWithTypeCheckHint(new SessionStartHint()); - - item.getClient().captureSession(pair.getCurrent(), hint); - } else { - options.getLogger().log(SentryLevel.WARNING, "Session could not be started."); - } - } - } - - @Override - public void endSession() { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op."); - } else { - final StackItem item = this.stack.peek(); - final Session previousSession = item.getScope().endSession(); - if (previousSession != null) { - final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint()); - - item.getClient().captureSession(previousSession, hint); - } - } - } - - @Override - public void close() { - close(false); - } - - @Override - @SuppressWarnings("FutureReturnValueIgnored") - public void close(final boolean isRestarting) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'close' call is a no-op."); - } else { - try { - for (Integration integration : options.getIntegrations()) { - if (integration instanceof Closeable) { - try { - ((Closeable) integration).close(); - } catch (IOException e) { - options - .getLogger() - .log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e); - } - } - } - - configureScope(scope -> scope.clear()); - options.getTransactionProfiler().close(); - options.getTransactionPerformanceCollector().close(); - final @NotNull ISentryExecutorService executorService = options.getExecutorService(); - if (isRestarting) { - executorService.submit(() -> executorService.close(options.getShutdownTimeoutMillis())); - } else { - executorService.close(options.getShutdownTimeoutMillis()); - } - - // Close the top-most client - final StackItem item = stack.peek(); - // TODO: should we end session before closing client? - item.getClient().close(isRestarting); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e); - } - isEnabled = false; - } - } - - @Override - public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'addBreadcrumb' call is a no-op."); - } else if (breadcrumb == null) { - options.getLogger().log(SentryLevel.WARNING, "addBreadcrumb called with null parameter."); - } else { - stack.peek().getScope().addBreadcrumb(breadcrumb, hint); - } - } - - @Override - public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) { - addBreadcrumb(breadcrumb, new Hint()); - } - - @Override - public void setLevel(final @Nullable SentryLevel level) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op."); - } else { - stack.peek().getScope().setLevel(level); - } - } - - @Override - public void setTransaction(final @Nullable String transaction) { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'setTransaction' call is a no-op."); - } else if (transaction != null) { - stack.peek().getScope().setTransaction(transaction); - } else { - options.getLogger().log(SentryLevel.WARNING, "Transaction cannot be null"); - } - } - - @Override - public void setUser(final @Nullable User user) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op."); - } else { - stack.peek().getScope().setUser(user); - } - } - - @Override - public void setFingerprint(final @NotNull List fingerprint) { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'setFingerprint' call is a no-op."); - } else if (fingerprint == null) { - options.getLogger().log(SentryLevel.WARNING, "setFingerprint called with null parameter."); - } else { - stack.peek().getScope().setFingerprint(fingerprint); - } - } - - @Override - public void clearBreadcrumbs() { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'clearBreadcrumbs' call is a no-op."); - } else { - stack.peek().getScope().clearBreadcrumbs(); - } - } - - @Override - public void setTag(final @NotNull String key, final @NotNull String value) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'setTag' call is a no-op."); - } else if (key == null || value == null) { - options.getLogger().log(SentryLevel.WARNING, "setTag called with null parameter."); - } else { - stack.peek().getScope().setTag(key, value); - } - } - - @Override - public void removeTag(final @NotNull String key) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'removeTag' call is a no-op."); - } else if (key == null) { - options.getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter."); - } else { - stack.peek().getScope().removeTag(key); - } - } - - @Override - public void setExtra(final @NotNull String key, final @NotNull String value) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'setExtra' call is a no-op."); - } else if (key == null || value == null) { - options.getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter."); - } else { - stack.peek().getScope().setExtra(key, value); - } - } - - @Override - public void removeExtra(final @NotNull String key) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'removeExtra' call is a no-op."); - } else if (key == null) { - options.getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter."); - } else { - stack.peek().getScope().removeExtra(key); - } - } - - @Override - public @NotNull SentryId getLastEventId() { - return lastEventId; - } - - @Override - public @NotNull ISentryLifecycleToken pushScope() { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op."); - } else { - final StackItem item = stack.peek(); - final StackItem newItem = new StackItem(options, item.getClient(), item.getScope().clone()); - stack.push(newItem); - } - return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); - } - - @Override - public @NotNull ISentryLifecycleToken pushIsolationScope() { - return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); - } - - @Override - public @NotNull SentryOptions getOptions() { - return this.stack.peek().getOptions(); - } - - @Override - public @Nullable Boolean isCrashedLastRun() { - return SentryCrashLastRunState.getInstance() - .isCrashedLastRun(options.getCacheDirPath(), !options.isEnableAutoSessionTracking()); - } - - @Override - public void reportFullyDisplayed() { - if (options.isEnableTimeToFullDisplayTracing()) { - options.getFullyDisplayedReporter().reportFullyDrawn(); - } - } - - @Override - @Deprecated - public void popScope() { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op."); - } else { - stack.pop(); - } - } - - @Override - public void withScope(final @NotNull ScopeCallback callback) { - if (!isEnabled()) { - try { - callback.run(NoOpScope.getInstance()); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); - } - - } else { - pushScope(); - try { - callback.run(stack.peek().getScope()); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); - } - popScope(); - } - } - - @Override - public void withIsolationScope(final @NotNull ScopeCallback callback) { - if (!isEnabled()) { - try { - callback.run(NoOpScope.getInstance()); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); - } - - } else { - pushScope(); - try { - callback.run(stack.peek().getScope()); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e); - } - popScope(); - } - } - - @Override - public void configureScope( - final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'configureScope' call is a no-op."); - } else { - try { - callback.run(stack.peek().getScope()); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e); - } - } - } - - @Override - public void bindClient(final @NotNull ISentryClient client) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'bindClient' call is a no-op."); - } else { - final StackItem item = stack.peek(); - if (client != null) { - options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope."); - item.setClient(client); - } else { - options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope."); - item.setClient(NoOpSentryClient.getInstance()); - } - } - } - - @Override - public boolean isHealthy() { - return stack.peek().getClient().isHealthy(); - } - - @Override - public void flush(long timeoutMillis) { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'flush' call is a no-op."); - } else { - try { - stack.peek().getClient().flush(timeoutMillis); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e); - } - } - } - - @Override - public @NotNull IHub clone() { - if (!isEnabled()) { - options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned."); - } - // Clone will be invoked in parallel - return new Hub(this.options, new Stack(this.stack)); - } - - @Override - public @NotNull IScopes forkedScopes(@NotNull String creator) { - return Sentry.forkedScopes(creator); - } - - @Override - public @NotNull IScopes forkedCurrentScope(@NotNull String creator) { - return Sentry.forkedCurrentScope(creator); - } - - @Override - public @NotNull IScopes forkedRootScopes(final @NotNull String creator) { - return Sentry.forkedRootScopes(creator); - } - - @Override - public @NotNull ISentryLifecycleToken makeCurrent() { - return NoOpScopesStorage.NoOpScopesLifecycleToken.getInstance(); - } - - @Override - @ApiStatus.Internal - public @NotNull IScope getScope() { - return Sentry.getCurrentScopes().getScope(); - } - - @Override - @ApiStatus.Internal - public @NotNull IScope getIsolationScope() { - return Sentry.getCurrentScopes().getIsolationScope(); - } - - @Override - @ApiStatus.Internal - public @NotNull IScope getGlobalScope() { - return Sentry.getGlobalScope(); - } - - @ApiStatus.Internal - @Override - public @NotNull SentryId captureTransaction( - final @NotNull SentryTransaction transaction, - final @Nullable TraceContext traceContext, - final @Nullable Hint hint, - final @Nullable ProfilingTraceData profilingTraceData) { - Objects.requireNonNull(transaction, "transaction is required"); - - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureTransaction' call is a no-op."); - } else { - if (!transaction.isFinished()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Transaction: %s is not finished and this 'captureTransaction' call is a no-op.", - transaction.getEventId()); - } else { - if (!Boolean.TRUE.equals(transaction.isSampled())) { - options - .getLogger() - .log( - SentryLevel.DEBUG, - "Transaction %s was dropped due to sampling decision.", - transaction.getEventId()); - if (options.getBackpressureMonitor().getDownsampleFactor() > 0) { - options - .getClientReportRecorder() - .recordLostEvent(DiscardReason.BACKPRESSURE, DataCategory.Transaction); - } else { - options - .getClientReportRecorder() - .recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Transaction); - } - } else { - StackItem item = null; - try { - item = stack.peek(); - sentryId = - item.getClient() - .captureTransaction( - transaction, traceContext, item.getScope(), hint, profilingTraceData); - } catch (Throwable e) { - options - .getLogger() - .log( - SentryLevel.ERROR, - "Error while capturing transaction with id: " + transaction.getEventId(), - e); - } - } - } - } - return sentryId; - } - - @Override - public @NotNull ITransaction startTransaction( - final @NotNull TransactionContext transactionContext, - final @NotNull TransactionOptions transactionOptions) { - return createTransaction(transactionContext, transactionOptions); - } - - private @NotNull ITransaction createTransaction( - final @NotNull TransactionContext transactionContext, - final @NotNull TransactionOptions transactionOptions) { - Objects.requireNonNull(transactionContext, "transactionContext is required"); - - ITransaction transaction; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'startTransaction' returns a no-op."); - transaction = NoOpTransaction.getInstance(); - } else if (!options.getInstrumenter().equals(transactionContext.getInstrumenter())) { - options - .getLogger() - .log( - SentryLevel.DEBUG, - "Returning no-op for instrumenter %s as the SDK has been configured to use instrumenter %s", - transactionContext.getInstrumenter(), - options.getInstrumenter()); - transaction = NoOpTransaction.getInstance(); - } else if (!options.isTracingEnabled()) { - options - .getLogger() - .log( - SentryLevel.INFO, "Tracing is disabled and this 'startTransaction' returns a no-op."); - transaction = NoOpTransaction.getInstance(); - } else { - final SamplingContext samplingContext = - new SamplingContext(transactionContext, transactionOptions.getCustomSamplingContext()); - @NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext); - transactionContext.setSamplingDecision(samplingDecision); - - transaction = - new SentryTracer( - transactionContext, this, transactionOptions, transactionPerformanceCollector); - - // The listener is called only if the transaction exists, as the transaction is needed to - // stop it - if (samplingDecision.getSampled() && samplingDecision.getProfileSampled()) { - final ITransactionProfiler transactionProfiler = options.getTransactionProfiler(); - // If the profiler is not running, we start and bind it here. - if (!transactionProfiler.isRunning()) { - transactionProfiler.start(); - transactionProfiler.bindTransaction(transaction); - } else if (transactionOptions.isAppStartTransaction()) { - // If the profiler is running and the current transaction is the app start, we bind it. - transactionProfiler.bindTransaction(transaction); - } - } - } - if (transactionOptions.isBindToScope()) { - configureScope(scope -> scope.setTransaction(transaction)); - } - return transaction; - } - - @Deprecated - @SuppressWarnings("InlineMeSuggester") - @Override - public @Nullable SentryTraceHeader traceHeaders() { - return getTraceparent(); - } - - @Override - public @Nullable ISpan getSpan() { - ISpan span = null; - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'getSpan' call is a no-op."); - } else { - span = stack.peek().getScope().getSpan(); - } - return span; - } - - @Override - @ApiStatus.Internal - public @Nullable ITransaction getTransaction() { - ITransaction span = null; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'getTransaction' call is a no-op."); - } else { - span = stack.peek().getScope().getTransaction(); - } - return span; - } - - @Override - @ApiStatus.Internal - public void setSpanContext( - final @NotNull Throwable throwable, - final @NotNull ISpan span, - final @NotNull String transactionName) { - Objects.requireNonNull(throwable, "throwable is required"); - Objects.requireNonNull(span, "span is required"); - Objects.requireNonNull(transactionName, "transactionName is required"); - // to match any cause, span context is always attached to the root cause of the exception - final Throwable rootCause = ExceptionUtils.findRootCause(throwable); - // the most inner span should be assigned to a throwable - if (!throwableToSpan.containsKey(rootCause)) { - throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName)); - } - } - - @Nullable - SpanContext getSpanContext(final @NotNull Throwable throwable) { - Objects.requireNonNull(throwable, "throwable is required"); - final Throwable rootCause = ExceptionUtils.findRootCause(throwable); - final Pair, String> pair = this.throwableToSpan.get(rootCause); - if (pair != null) { - final WeakReference spanWeakRef = pair.getFirst(); - if (spanWeakRef != null) { - final ISpan span = spanWeakRef.get(); - if (span != null) { - return span.getSpanContext(); - } - } - } - return null; - } - - private IScope buildLocalScope( - final @NotNull IScope scope, final @Nullable ScopeCallback callback) { - if (callback != null) { - try { - final IScope localScope = scope.clone(); - callback.run(localScope); - return localScope; - } catch (Throwable t) { - options.getLogger().log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t); - } - } - return scope; - } - - @Override - public @Nullable TransactionContext continueTrace( - final @Nullable String sentryTrace, final @Nullable List baggageHeaders) { - @NotNull - PropagationContext propagationContext = - PropagationContext.fromHeaders(getOptions().getLogger(), sentryTrace, baggageHeaders); - configureScope( - (scope) -> { - scope.setPropagationContext(propagationContext); - }); - if (options.isTracingEnabled()) { - return TransactionContext.fromPropagationContext(propagationContext); - } else { - return null; - } - } - - @Override - public @Nullable SentryTraceHeader getTraceparent() { - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'getTraceparent' call is a no-op."); - } else { - final @Nullable TracingUtils.TracingHeaders headers = - TracingUtils.trace(this, null, getSpan()); - if (headers != null) { - return headers.getSentryTraceHeader(); - } - } - - return null; - } - - @Override - public @Nullable BaggageHeader getBaggage() { - if (!isEnabled()) { - options - .getLogger() - .log(SentryLevel.WARNING, "Instance is disabled and this 'getBaggage' call is a no-op."); - } else { - final @Nullable TracingUtils.TracingHeaders headers = - TracingUtils.trace(this, null, getSpan()); - if (headers != null) { - return headers.getBaggageHeader(); - } - } - - return null; - } - - @Override - @ApiStatus.Experimental - public @NotNull SentryId captureCheckIn(final @NotNull CheckIn checkIn) { - SentryId sentryId = SentryId.EMPTY_ID; - if (!isEnabled()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Instance is disabled and this 'captureCheckIn' call is a no-op."); - } else { - try { - StackItem item = stack.peek(); - sentryId = item.getClient().captureCheckIn(checkIn, item.getScope(), null); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Error while capturing check-in for slug", e); - } - } - this.lastEventId = sentryId; - return sentryId; - } - - @ApiStatus.Internal - @Override - public @Nullable RateLimiter getRateLimiter() { - final StackItem item = stack.peek(); - return item.getClient().getRateLimiter(); - } - - @Override - public @NotNull MetricsApi metrics() { - return metricsApi; - } - - @Override - public @NotNull IMetricsAggregator getMetricsAggregator() { - return stack.peek().getClient().getMetricsAggregator(); - } - - @Override - public @NotNull Map getDefaultTagsForMetrics() { - if (!options.isEnableDefaultTagsForMetrics()) { - return Collections.emptyMap(); - } - - final @NotNull Map tags = new HashMap<>(); - final @Nullable String release = options.getRelease(); - if (release != null) { - tags.put("release", release); - } - - final @Nullable String environment = options.getEnvironment(); - if (environment != null) { - tags.put("environment", environment); - } - - final @Nullable String txnName = stack.peek().getScope().getTransactionName(); - if (txnName != null) { - tags.put("transaction", txnName); - } - return Collections.unmodifiableMap(tags); - } - - @Override - public @Nullable ISpan startSpanForMetric(@NotNull String op, @NotNull String description) { - final @Nullable ISpan span = getSpan(); - if (span != null) { - return span.startChild(op, description); - } - return null; - } - - @Override - public @Nullable LocalMetricsAggregator getLocalMetricsAggregator() { - if (!options.isEnableSpanLocalMetricAggregation()) { - return null; - } - final @Nullable ISpan span = getSpan(); - if (span != null) { - return span.getLocalMetricsAggregator(); - } - return null; - } -} diff --git a/sentry/src/main/java/io/sentry/Scope.java b/sentry/src/main/java/io/sentry/Scope.java index 3df40a2b01..0d42326e13 100644 --- a/sentry/src/main/java/io/sentry/Scope.java +++ b/sentry/src/main/java/io/sentry/Scope.java @@ -116,7 +116,6 @@ private Scope(final @NotNull Scope scope) { this.options = scope.options; this.level = scope.level; this.client = scope.client; - // TODO [HSM] should we do this? didn't do it for Hub this.lastEventId = scope.getLastEventId(); final User userRef = scope.user; @@ -838,7 +837,7 @@ public SessionPair startSession() { SessionPair pair = null; synchronized (sessionLock) { if (session != null) { - // Assumes session will NOT flush itself (Not passing any hub to it) + // Assumes session will NOT flush itself (Not passing any scopes to it) session.end(); } previousSession = session; diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt deleted file mode 100644 index f30fd0a966..0000000000 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ /dev/null @@ -1,2149 +0,0 @@ -package io.sentry - -import io.sentry.backpressure.IBackpressureMonitor -import io.sentry.cache.EnvelopeCache -import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport -import io.sentry.clientreport.DiscardReason -import io.sentry.clientreport.DiscardedEvent -import io.sentry.hints.SessionEndHint -import io.sentry.hints.SessionStartHint -import io.sentry.protocol.SentryId -import io.sentry.protocol.SentryTransaction -import io.sentry.protocol.User -import io.sentry.test.DeferredExecutorService -import io.sentry.test.callMethod -import io.sentry.util.HintUtils -import io.sentry.util.StringUtils -import org.mockito.kotlin.any -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.argWhere -import org.mockito.kotlin.argumentCaptor -import org.mockito.kotlin.check -import org.mockito.kotlin.doAnswer -import org.mockito.kotlin.doThrow -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import org.mockito.kotlin.never -import org.mockito.kotlin.spy -import org.mockito.kotlin.times -import org.mockito.kotlin.verify -import org.mockito.kotlin.verifyNoMoreInteractions -import org.mockito.kotlin.whenever -import java.io.File -import java.nio.file.Files -import java.util.Queue -import java.util.UUID -import java.util.concurrent.atomic.AtomicReference -import kotlin.test.AfterTest -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertFalse -import kotlin.test.assertNotEquals -import kotlin.test.assertNotNull -import kotlin.test.assertNull -import kotlin.test.assertTrue -import kotlin.test.fail - -class HubTest { - - private lateinit var file: File - private lateinit var profilingTraceFile: File - - @BeforeTest - fun `set up`() { - file = Files.createTempDirectory("sentry-disk-cache-test").toAbsolutePath().toFile() - profilingTraceFile = Files.createTempFile("trace", ".trace").toFile() - profilingTraceFile.writeText("sampledProfile") - } - - @AfterTest - fun shutdown() { - file.deleteRecursively() - profilingTraceFile.delete() - Sentry.close() - } - - @Test - fun `when no dsn available, ctor throws illegal arg`() { - val ex = assertFailsWith { Hub(SentryOptions()) } - assertEquals("Hub requires a DSN to be instantiated. Considering using the NoOpHub if no DSN is available.", ex.message) - } - - @Test - fun `when scopes is cloned, integrations are not registered`() { - val integrationMock = mock() - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - options.addIntegration(integrationMock) -// val expected = HubAdapter.getInstance() - val scopes = Hub(options) -// verify(integrationMock).register(expected, options) - scopes.clone() - verifyNoMoreInteractions(integrationMock) - } - - @Test - fun `when scopes is cloned, scope changes are isolated`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val scopes = Hub(options) - var firstScope: IScope? = null - scopes.configureScope { - firstScope = it - it.setTag("scopes", "a") - } - var cloneScope: IScope? = null - val clone = scopes.clone() - clone.configureScope { - cloneScope = it - it.setTag("scopes", "b") - } - assertEquals("a", firstScope!!.tags["scopes"]) - assertEquals("b", cloneScope!!.tags["scopes"]) - } - - @Test - fun `when scopes is initialized, breadcrumbs are capped as per options`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.maxBreadcrumbs = 5 - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - (1..10).forEach { _ -> sut.addBreadcrumb(Breadcrumb(), null) } - var actual = 0 - sut.configureScope { - actual = it.breadcrumbs.size - } - assertEquals(options.maxBreadcrumbs, actual) - } - - @Test - fun `when beforeBreadcrumb returns null, crumb is dropped`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { _: Breadcrumb, _: Any? -> null } - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - sut.addBreadcrumb(Breadcrumb(), null) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - assertEquals(0, breadcrumbs!!.size) - } - - @Test - fun `when beforeBreadcrumb modifies crumb, crumb is stored modified`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - val expected = "expected" - options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb: Breadcrumb, _: Any? -> breadcrumb.message = expected; breadcrumb; } - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val crumb = Breadcrumb() - crumb.message = "original" - sut.addBreadcrumb(crumb) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - assertEquals(expected, breadcrumbs!!.first().message) - } - - @Test - fun `when beforeBreadcrumb is null, crumb is stored`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.beforeBreadcrumb = null - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val expected = Breadcrumb() - sut.addBreadcrumb(expected) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - assertEquals(expected, breadcrumbs!!.single()) - } - - @Test - fun `when beforeSend throws an exception, breadcrumb adds an entry to the data field with exception message`() { - val exception = Exception("test") - - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { _: Breadcrumb, _: Any? -> throw exception } - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - - val actual = Breadcrumb() - sut.addBreadcrumb(actual) - - assertEquals("test", actual.data["sentry:message"]) - } - - @Test - fun `when initialized, lastEventId is empty`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - - @Test - fun `when addBreadcrumb is called on disabled client, no-op`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - sut.close() - sut.addBreadcrumb(Breadcrumb()) - assertTrue(breadcrumbs!!.isEmpty()) - } - - @Test - fun `when addBreadcrumb is called with message and category, breadcrumb object has values`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - sut.addBreadcrumb("message", "category") - assertEquals("message", breadcrumbs!!.single().message) - assertEquals("category", breadcrumbs!!.single().category) - } - - @Test - fun `when addBreadcrumb is called with message, breadcrumb object has value`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - var breadcrumbs: Queue? = null - sut.configureScope { breadcrumbs = it.breadcrumbs } - sut.addBreadcrumb("message", "category") - assertEquals("message", breadcrumbs!!.single().message) - assertEquals("category", breadcrumbs!!.single().category) - } - - @Test - fun `when flush is called on disabled client, no-op`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.flush(1000) - verify(mockClient, never()).flush(1000) - } - - @Test - fun `when flush is called, client flush gets called`() { - val (sut, mockClient) = getEnabledHub() - - sut.flush(1000) - verify(mockClient).flush(1000) - } - - //region captureEvent tests - @Test - fun `when captureEvent is called and event is null, lastEventId is empty`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - sut.callMethod("captureEvent", SentryEvent::class.java, null) - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - - @Test - fun `when captureEvent is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.captureEvent(SentryEvent()) - verify(mockClient, never()).captureEvent(any(), any()) - } - - @Test - fun `when captureEvent is called with a valid argument, captureEvent on the client should be called`() { - val (sut, mockClient) = getEnabledHub() - - val event = SentryEvent() - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called on disabled scopes, lastEventId does not get overwritten`() { - val (sut, mockClient) = getEnabledHub() - whenever(mockClient.captureEvent(any(), any(), anyOrNull())).thenReturn(SentryId(UUID.randomUUID())) - val event = SentryEvent() - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - val lastEventId = sut.lastEventId - sut.close() - sut.captureEvent(event, hints) - assertEquals(lastEventId, sut.lastEventId) - } - - @Test - fun `when captureEvent is called and session tracking is disabled, it should not capture a session`() { - val (sut, mockClient) = getEnabledHub() - - val event = SentryEvent() - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - verify(mockClient, never()).captureSession(any(), any()) - } - - @Test - fun `when captureEvent is called but no session started, it should not capture a session`() { - val (sut, mockClient) = getEnabledHub() - - val event = SentryEvent() - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - verify(mockClient, never()).captureSession(any(), any()) - } - - @Test - fun `when captureEvent is called and event has exception which has been previously attached with span context, sets span context to the event`() { - val (sut, mockClient) = getEnabledHub() - val exception = RuntimeException() - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(exception, span, "tx-name") - - val event = SentryEvent(exception) - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - assertEquals(span.spanContext, event.contexts.trace) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called and event has exception which root cause has been previously attached with span context, sets span context to the event`() { - val (sut, mockClient) = getEnabledHub() - val rootCause = RuntimeException() - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(rootCause, span, "tx-name") - - val event = SentryEvent(RuntimeException(rootCause)) - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - assertEquals(span.spanContext, event.contexts.trace) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called and event has exception which non-root cause has been previously attached with span context, sets span context to the event`() { - val (sut, mockClient) = getEnabledHub() - val rootCause = RuntimeException() - val exceptionAssignedToSpan = RuntimeException(rootCause) - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(exceptionAssignedToSpan, span, "tx-name") - - val event = SentryEvent(RuntimeException(exceptionAssignedToSpan)) - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - assertEquals(span.spanContext, event.contexts.trace) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called and event has exception which has been previously attached with span context and trace context already set, does not set new span context to the event`() { - val (sut, mockClient) = getEnabledHub() - val exception = RuntimeException() - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(exception, span, "tx-name") - - val event = SentryEvent(exception) - val originalSpanContext = SpanContext("op") - event.contexts.trace = originalSpanContext - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - assertEquals(originalSpanContext, event.contexts.trace) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called and event has exception which has not been previously attached with span context, does not set new span context to the event`() { - val (sut, mockClient) = getEnabledHub() - - val event = SentryEvent(RuntimeException()) - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureEvent(event, hints) - assertNull(event.contexts.trace) - verify(mockClient).captureEvent(eq(event), any(), eq(hints)) - } - - @Test - fun `when captureEvent is called with a ScopeCallback then the modified scope is sent to the client`() { - val (sut, mockClient) = getEnabledHub() - - sut.captureEvent(SentryEvent(), null) { - it.setTag("test", "testValue") - } - - verify(mockClient).captureEvent( - any(), - check { - assertEquals("testValue", it.tags["test"]) - }, - anyOrNull() - ) - } - - @Test - fun `when captureEvent is called with a ScopeCallback then subsequent calls to captureEvent send the unmodified Scope to the client`() { - val (sut, mockClient) = getEnabledHub() - val argumentCaptor = argumentCaptor() - - sut.captureEvent(SentryEvent(), null) { - it.setTag("test", "testValue") - } - - sut.captureEvent(SentryEvent()) - - verify(mockClient, times(2)).captureEvent( - any(), - argumentCaptor.capture(), - anyOrNull() - ) - - assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) - assertNull(argumentCaptor.allValues[1].tags["test"]) - } - - @Test - fun `when captureEvent is called with a ScopeCallback that crashes then the event should still be captured`() { - val (sut, mockClient, logger) = getEnabledHub() - - val exception = Exception("scope callback exception") - sut.captureEvent(SentryEvent(), null) { - throw exception - } - - verify(mockClient).captureEvent( - any(), - anyOrNull(), - anyOrNull() - ) - - verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) - } - //endregion - - //region captureMessage tests - @Test - fun `when captureMessage is called and event is null, lastEventId is empty`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - sut.callMethod("captureMessage", String::class.java, null) - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - - @Test - fun `when captureMessage is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.captureMessage("test") - verify(mockClient, never()).captureMessage(any(), any()) - } - - @Test - fun `when captureMessage is called with a valid message, captureMessage on the client should be called`() { - val (sut, mockClient) = getEnabledHub() - - sut.captureMessage("test") - verify(mockClient).captureMessage(any(), any(), any()) - } - - @Test - fun `when captureMessage is called, level is INFO by default`() { - val (sut, mockClient) = getEnabledHub() - sut.captureMessage("test") - verify(mockClient).captureMessage(eq("test"), eq(SentryLevel.INFO), any()) - } - - @Test - fun `when captureMessage is called with a ScopeCallback then the modified scope is sent to the client`() { - val (sut, mockClient) = getEnabledHub() - - sut.captureMessage("test") { - it.setTag("test", "testValue") - } - - verify(mockClient).captureMessage( - any(), - any(), - check { - assertEquals("testValue", it.tags["test"]) - } - ) - } - - @Test - fun `when captureMessage is called with a ScopeCallback then subsequent calls to captureMessage send the unmodified Scope to the client`() { - val (sut, mockClient) = getEnabledHub() - val argumentCaptor = argumentCaptor() - - sut.captureMessage("testMessage") { - it.setTag("test", "testValue") - } - - sut.captureMessage("test", SentryLevel.INFO) - - verify(mockClient, times(2)).captureMessage( - any(), - any(), - argumentCaptor.capture() - ) - - assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) - assertNull(argumentCaptor.allValues[1].tags["test"]) - } - - @Test - fun `when captureMessage is called with a ScopeCallback that crashes then the message should still be captured`() { - val (sut, mockClient, logger) = getEnabledHub() - - val exception = Exception("scope callback exception") - sut.captureMessage("Hello World") { - throw exception - } - - verify(mockClient).captureMessage( - any(), - anyOrNull(), - anyOrNull() - ) - - verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) - } - - //endregion - - //region captureException tests - @Test - fun `when captureException is called and exception is null, lastEventId is empty`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - sut.callMethod("captureException", Throwable::class.java, null) - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - - @Test - fun `when captureException is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.captureException(Throwable()) - verify(mockClient, never()).captureEvent(any(), any(), any()) - } - - @Test - fun `when captureException is called with a valid argument and hint, captureEvent on the client should be called`() { - val (sut, mockClient) = getEnabledHub() - - val hints = HintUtils.createWithTypeCheckHint({}) - sut.captureException(Throwable(), hints) - verify(mockClient).captureEvent(any(), any(), any()) - } - - @Test - fun `when captureException is called with a valid argument but no hint, captureEvent on the client should be called`() { - val (sut, mockClient) = getEnabledHub() - - sut.captureException(Throwable()) - verify(mockClient).captureEvent(any(), any(), any()) - } - - @Test - fun `when captureException is called with an exception which has been previously attached with span context, span context should be set on the event before capturing`() { - val (sut, mockClient) = getEnabledHub() - val throwable = Throwable() - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(throwable, span, "tx-name") - - sut.captureException(throwable) - verify(mockClient).captureEvent( - check { - assertEquals(span.spanContext, it.contexts.trace) - assertEquals("tx-name", it.transaction) - }, - any(), - anyOrNull() - ) - } - - @Test - fun `when captureException is called with an exception which has not been previously attached with span context, span context should not be set on the event before capturing`() { - val (sut, mockClient) = getEnabledHub() - val span = mock() - whenever(span.spanContext).thenReturn(SpanContext("op")) - sut.setSpanContext(Throwable(), span, "tx-name") - - sut.captureException(Throwable()) - verify(mockClient).captureEvent( - check { - assertNull(it.contexts.trace) - }, - any(), - anyOrNull() - ) - } - - @Test - fun `when captureException is called with a ScopeCallback then the modified scope is sent to the client`() { - val (sut, mockClient) = getEnabledHub() - - sut.captureException(Throwable(), null) { - it.setTag("test", "testValue") - } - - verify(mockClient).captureEvent( - any(), - check { - assertEquals("testValue", it.tags["test"]) - }, - anyOrNull() - ) - } - - @Test - fun `when captureException is called with a ScopeCallback then subsequent calls to captureException send the unmodified Scope to the client`() { - val (sut, mockClient) = getEnabledHub() - val argumentCaptor = argumentCaptor() - - sut.captureException(Throwable(), null) { - it.setTag("test", "testValue") - } - - sut.captureException(Throwable()) - - verify(mockClient, times(2)).captureEvent( - any(), - argumentCaptor.capture(), - anyOrNull() - ) - - assertEquals("testValue", argumentCaptor.allValues[0].tags["test"]) - assertNull(argumentCaptor.allValues[1].tags["test"]) - } - - @Test - fun `when captureException is called with a ScopeCallback that crashes then the exception should still be captured`() { - val (sut, mockClient, logger) = getEnabledHub() - - val exception = Exception("scope callback exception") - sut.captureException(Throwable()) { - throw exception - } - - verify(mockClient).captureEvent( - any(), - anyOrNull(), - anyOrNull() - ) - - verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) - } - - //endregion - - //region captureUserFeedback tests - - @Test - fun `when captureUserFeedback is called it is forwarded to the client`() { - val (sut, mockClient) = getEnabledHub() - sut.captureUserFeedback(userFeedback) - - verify(mockClient).captureUserFeedback( - check { - assertEquals(userFeedback.eventId, it.eventId) - assertEquals(userFeedback.email, it.email) - assertEquals(userFeedback.name, it.name) - assertEquals(userFeedback.comments, it.comments) - } - ) - } - - @Test - fun `when captureUserFeedback is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.captureUserFeedback(userFeedback) - verify(mockClient, never()).captureUserFeedback(any()) - } - - @Test - fun `when captureUserFeedback is called and client throws, don't crash`() { - val (sut, mockClient) = getEnabledHub() - - whenever(mockClient.captureUserFeedback(any())).doThrow(IllegalArgumentException("")) - - sut.captureUserFeedback(userFeedback) - } - - private val userFeedback: UserFeedback get() { - val eventId = SentryId("c2fb8fee2e2b49758bcb67cda0f713c7") - return UserFeedback(eventId).apply { - name = "John" - email = "john@me.com" - comments = "comment" - } - } - - //region captureCheckIn tests - - @Test - fun `when captureCheckIn is called it is forwarded to the client`() { - val (sut, mockClient) = getEnabledHub() - sut.captureCheckIn(checkIn) - - verify(mockClient).captureCheckIn( - check { - assertEquals(checkIn.checkInId, it.checkInId) - assertEquals(checkIn.monitorSlug, it.monitorSlug) - assertEquals(checkIn.status, it.status) - }, - any(), - anyOrNull() - ) - } - - @Test - fun `when captureCheckIn is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.captureCheckIn(checkIn) - verify(mockClient, never()).captureCheckIn(any(), any(), anyOrNull()) - } - - @Test - fun `when captureCheckIn is called and client throws, don't crash`() { - val (sut, mockClient) = getEnabledHub() - - whenever(mockClient.captureCheckIn(any(), any(), anyOrNull())).doThrow(IllegalArgumentException("")) - - sut.captureCheckIn(checkIn) - } - - private val checkIn: CheckIn = CheckIn("some_slug", CheckInStatus.OK) - - //endregion - - //region close tests - @Test - fun `when close is called on disabled client, do nothing`() { - val (sut, mockClient) = getEnabledHub() - sut.close() - - sut.close() - verify(mockClient).close(eq(false)) // 1 to close, but next one wont be recorded - } - - @Test - fun `when close is called and client is alive, close on the client should be called`() { - val (sut, mockClient) = getEnabledHub() - - sut.close() - verify(mockClient).close(eq(false)) - } - - @Test - fun `when close is called with isRestarting false and client is alive, close on the client should be called with isRestarting false`() { - val (sut, mockClient) = getEnabledHub() - - sut.close(false) - verify(mockClient).close(eq(false)) - } - - @Test - fun `when close is called with isRestarting true and client is alive, close on the client should be called with isRestarting true`() { - val (sut, mockClient) = getEnabledHub() - - sut.close(true) - verify(mockClient).close(eq(true)) - } - //endregion - - //region withScope tests - @Test - fun `when withScope is called on disabled client, execute on NoOpScope`() { - val (sut) = getEnabledHub() - - val scopeCallback = mock() - sut.close() - - sut.withScope(scopeCallback) - verify(scopeCallback).run(NoOpScope.getInstance()) - } - - @Test - fun `when withScope is called with alive client, run should be called`() { - val (sut) = getEnabledHub() - - val scopeCallback = mock() - - sut.withScope(scopeCallback) - verify(scopeCallback).run(any()) - } - - @Test - fun `when withScope throws an exception then it should be caught`() { - val (scopes, _, logger) = getEnabledHub() - - val exception = Exception("scope callback exception") - val scopeCallback = ScopeCallback { - throw exception - } - - scopes.withScope(scopeCallback) - - verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) - } - //endregion - - //region configureScope tests - @Test - fun `when configureScope is called on disabled client, do nothing`() { - val (sut) = getEnabledHub() - - val scopeCallback = mock() - sut.close() - - sut.configureScope(scopeCallback) - verify(scopeCallback, never()).run(any()) - } - - @Test - fun `when configureScope is called with alive client, run should be called`() { - val (sut) = getEnabledHub() - - val scopeCallback = mock() - - sut.configureScope(scopeCallback) - verify(scopeCallback).run(any()) - } - - @Test - fun `when configureScope throws an exception then it should be caught`() { - val (scopes, _, logger) = getEnabledHub() - - val exception = Exception("scope callback exception") - val scopeCallback = ScopeCallback { - throw exception - } - - scopes.configureScope(scopeCallback) - - verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception)) - } - //endregion - - @Test - fun `when integration is registered, scopes is enabled`() { - val mock = mock() - - var options: SentryOptions? = null - // init main scopes and make it enabled - Sentry.init { - it.addIntegration(mock) - it.dsn = "https://key@sentry.io/proj" - it.cacheDirPath = file.absolutePath - it.setSerializer(mock()) - options = it - } - - doAnswer { - val scopes = it.arguments[0] as IScopes - assertTrue(scopes.isEnabled) - }.whenever(mock).register(any(), eq(options!!)) - - verify(mock).register(any(), eq(options!!)) - } - - //region setLevel tests - @Test - fun `when setLevel is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - scopes.setLevel(SentryLevel.INFO) - assertNull(scope?.level) - } - - @Test - fun `when setLevel is called, level is set`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.setLevel(SentryLevel.INFO) - assertEquals(SentryLevel.INFO, scope?.level) - } - //endregion - - //region setTransaction tests - @Test - fun `when setTransaction is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - scopes.setTransaction("test") - assertNull(scope?.transactionName) - } - - @Test - fun `when setTransaction is called, and transaction is not set, transaction name is changed`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.setTransaction("test") - assertEquals("test", scope?.transactionName) - } - - @Test - fun `when setTransaction is called, and transaction is set, transaction name is changed`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - val tx = scopes.startTransaction("test", "op") - scopes.configureScope { it.setTransaction(tx) } - - assertEquals("test", scope?.transactionName) - } - - @Test - fun `when startTransaction is called with different instrumenter, no-op is returned`() { - val scopes = generateHub() - - val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } - val transactionOptions = TransactionOptions() - val tx = scopes.startTransaction(transactionContext, transactionOptions) - - assertTrue(tx is NoOpTransaction) - } - - @Test - fun `when startTransaction is called with different instrumenter, no-op is returned 2`() { - val scopes = generateHub() { - it.instrumenter = Instrumenter.OTEL - } - - val tx = scopes.startTransaction("test", "op") - - assertTrue(tx is NoOpTransaction) - } - - @Test - fun `when startTransaction is called with configured instrumenter, it works`() { - val scopes = generateHub() { - it.instrumenter = Instrumenter.OTEL - } - - val transactionContext = TransactionContext("test", "op").also { it.instrumenter = Instrumenter.OTEL } - val transactionOptions = TransactionOptions() - val tx = scopes.startTransaction(transactionContext, transactionOptions) - - assertFalse(tx is NoOpTransaction) - } - //endregion - - //region setUser tests - @Test - fun `when setUser is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - scopes.setUser(User()) - assertNull(scope?.user) - } - - @Test - fun `when setUser is called, user is set`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - val user = User() - scopes.setUser(user) - assertEquals(user, scope?.user) - } - //endregion - - //region setFingerprint tests - @Test - fun `when setFingerprint is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - val fingerprint = listOf("abc") - scopes.setFingerprint(fingerprint) - assertEquals(0, scope?.fingerprint?.count()) - } - - @Test - fun `when setFingerprint is called with null parameter, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.callMethod("setFingerprint", List::class.java, null) - assertEquals(0, scope?.fingerprint?.count()) - } - - @Test - fun `when setFingerprint is called, fingerprint is set`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - val fingerprint = listOf("abc") - scopes.setFingerprint(fingerprint) - assertEquals(1, scope?.fingerprint?.count()) - } - //endregion - - //region clearBreadcrumbs tests - @Test - fun `when clearBreadcrumbs is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.addBreadcrumb(Breadcrumb()) - assertEquals(1, scope?.breadcrumbs?.count()) - - scopes.close() - - assertEquals(0, scope?.breadcrumbs?.count()) - } - - @Test - fun `when clearBreadcrumbs is called, clear breadcrumbs`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.addBreadcrumb(Breadcrumb()) - assertEquals(1, scope?.breadcrumbs?.count()) - scopes.clearBreadcrumbs() - assertEquals(0, scope?.breadcrumbs?.count()) - } - //endregion - - //region setTag tests - @Test - fun `when setTag is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - scopes.setTag("test", "test") - assertEquals(0, scope?.tags?.count()) - } - - @Test - fun `when setTag is called with null parameters, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.callMethod("setTag", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) - assertEquals(0, scope?.tags?.count()) - } - - @Test - fun `when setTag is called, tag is set`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.setTag("test", "test") - assertEquals(1, scope?.tags?.count()) - } - //endregion - - //region setExtra tests - @Test - fun `when setExtra is called on disabled client, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - scopes.close() - - scopes.setExtra("test", "test") - assertEquals(0, scope?.extras?.count()) - } - - @Test - fun `when setExtra is called with null parameters, do nothing`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.callMethod("setExtra", parameterTypes = arrayOf(String::class.java, String::class.java), null, null) - assertEquals(0, scope?.extras?.count()) - } - - @Test - fun `when setExtra is called, extra is set`() { - val scopes = generateHub() - var scope: IScope? = null - scopes.configureScope { - scope = it - } - - scopes.setExtra("test", "test") - assertEquals(1, scope?.extras?.count()) - } - //endregion - - //region captureEnvelope tests - @Test - fun `when captureEnvelope is called and envelope is null, throws IllegalArgumentException`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - try { - sut.callMethod("captureEnvelope", SentryEnvelope::class.java, null) - fail() - } catch (e: Exception) { - assertTrue(e.cause is java.lang.IllegalArgumentException) - } - } - - @Test - fun `when captureEnvelope is called on disabled client, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - sut.close() - - sut.captureEnvelope(SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf())) - verify(mockClient, never()).captureEnvelope(any(), any()) - } - - @Test - fun `when captureEnvelope is called with a valid envelope, captureEnvelope on the client should be called`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) - sut.captureEnvelope(envelope) - verify(mockClient).captureEnvelope(any(), anyOrNull()) - } - - @Test - fun `when captureEnvelope is called, lastEventId is not set`() { - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - setSerializer(mock()) - } - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - whenever(mockClient.captureEnvelope(any(), anyOrNull())).thenReturn(SentryId()) - val envelope = SentryEnvelope(SentryId(UUID.randomUUID()), null, setOf()) - sut.captureEnvelope(envelope) - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - //endregion - - //region startSession tests - @Test - fun `when startSession is called on disabled client, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - sut.close() - - sut.startSession() - verify(mockClient, never()).captureSession(any(), any()) - } - - @Test - fun `when startSession is called, starts a session`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - sut.startSession() - verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) - } - - @Test - fun `when startSession is called and there's a session, stops it and starts a new one`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - sut.startSession() - sut.startSession() - verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionEndHint::class.java) }) - verify(mockClient, times(2)).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) - } - //endregion - - //region endSession tests - @Test - fun `when endSession is called on disabled client, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - sut.close() - - sut.endSession() - verify(mockClient, never()).captureSession(any(), any()) - } - - @Test - fun `when endSession is called and session tracking is disabled, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - sut.endSession() - verify(mockClient, never()).captureSession(any(), any()) - } - - @Test - fun `when endSession is called, end a session`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - sut.startSession() - sut.endSession() - verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionStartHint::class.java) }) - verify(mockClient).captureSession(any(), argWhere { HintUtils.hasType(it, SessionEndHint::class.java) }) - } - - @Test - fun `when endSession is called and there's no session, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.release = "0.0.1" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - sut.endSession() - verify(mockClient, never()).captureSession(any(), any()) - } - //endregion - - //region captureTransaction tests - @Test - fun `when captureTransaction is called on disabled client, do nothing`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - sut.close() - - val sentryTracer = SentryTracer(TransactionContext("name", "op"), sut) - sentryTracer.finish() - sut.captureTransaction(SentryTransaction(sentryTracer), null as TraceContext?) - verify(mockClient, never()).captureTransaction(any(), any(), any()) - verify(mockClient, never()).captureTransaction(any(), any(), any(), anyOrNull(), anyOrNull()) - } - - @Test - fun `when captureTransaction and transaction is sampled, captureTransaction on the client should be called`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) - sentryTracer.finish() - val traceContext = sentryTracer.traceContext() - verify(mockClient).captureTransaction(any(), equalTraceContext(traceContext), any(), eq(null), eq(null)) - } - - @Test - fun `when captureTransaction is called, lastEventId is not set`() { - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - setSerializer(mock()) - } - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - whenever(mockClient.captureTransaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(SentryId()) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) - sentryTracer.finish() - assertEquals(SentryId.EMPTY_ID, sut.lastEventId) - } - - @Test - fun `when captureTransaction and transaction is not finished, captureTransaction on the client should not be called`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), sut) - sut.captureTransaction(SentryTransaction(sentryTracer), null as TraceContext?) - verify(mockClient, never()).captureTransaction(any(), any(), any(), eq(null), anyOrNull()) - } - - @Test - fun `when captureTransaction and transaction is not sampled, captureTransaction on the client should not be called`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) - sentryTracer.finish() - val traceContext = sentryTracer.traceContext() - verify(mockClient, never()).captureTransaction(any(), equalTraceContext(traceContext), any(), eq(null), anyOrNull()) - } - - @Test - fun `transactions lost due to sampling are recorded as lost`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) - sentryTracer.finish() - - assertClientReport( - options.clientReportRecorder, - listOf(DiscardedEvent(DiscardReason.SAMPLE_RATE.reason, DataCategory.Transaction.category, 1)) - ) - } - - @Test - fun `transactions lost due to sampling caused by backpressure are recorded as lost`() { - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - val mockBackpressureMonitor = mock() - options.backpressureMonitor = mockBackpressureMonitor - whenever(mockBackpressureMonitor.downsampleFactor).thenReturn(1) - - val sentryTracer = SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(false)), sut) - sentryTracer.finish() - - assertClientReport( - options.clientReportRecorder, - listOf(DiscardedEvent(DiscardReason.BACKPRESSURE.reason, DataCategory.Transaction.category, 1)) - ) - } - //endregion - - //region profiling tests - - @Test - fun `when startTransaction and profiling is enabled, transaction is profiled only if sampled`() { - val mockTransactionProfiler = mock() - val mockClient = mock() - whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val scopes = generateHub { - it.setTransactionProfiler(mockTransactionProfiler) - } - scopes.bindClient(mockClient) - // Transaction is not sampled, so it should not be profiled - val contexts = TransactionContext("name", "op", TracesSamplingDecision(false, null, true, null)) - val transaction = scopes.startTransaction(contexts) - transaction.finish() - verify(mockClient, never()).captureEnvelope(any()) - - // Transaction is sampled, so it should be profiled - val sampledContexts = TransactionContext("name", "op", TracesSamplingDecision(true, null, true, null)) - val sampledTransaction = scopes.startTransaction(sampledContexts) - sampledTransaction.finish() - verify(mockClient).captureEnvelope(any()) - } - - @Test - fun `when startTransaction and is sampled but profiling is disabled, transaction is not profiled`() { - val mockTransactionProfiler = mock() - val mockClient = mock() - whenever(mockTransactionProfiler.onTransactionFinish(any(), anyOrNull(), anyOrNull())).thenAnswer { mockClient.captureEnvelope(mock()) } - val scopes = generateHub { - it.profilesSampleRate = 0.0 - it.setTransactionProfiler(mockTransactionProfiler) - } - scopes.bindClient(mockClient) - val contexts = TransactionContext("name", "op") - val transaction = scopes.startTransaction(contexts) - transaction.finish() - verify(mockClient, never()).captureEnvelope(any()) - } - - @Test - fun `when profiler is running and isAppStartTransaction is false, startTransaction does not interact with profiler`() { - val mockTransactionProfiler = mock() - whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val scopes = generateHub { - it.profilesSampleRate = 1.0 - it.setTransactionProfiler(mockTransactionProfiler) - } - val context = TransactionContext("name", "op") - scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) - verify(mockTransactionProfiler, never()).start() - verify(mockTransactionProfiler, never()).bindTransaction(any()) - } - - @Test - fun `when profiler is running and isAppStartTransaction is true, startTransaction binds current profile`() { - val mockTransactionProfiler = mock() - whenever(mockTransactionProfiler.isRunning).thenReturn(true) - val scopes = generateHub { - it.profilesSampleRate = 1.0 - it.setTransactionProfiler(mockTransactionProfiler) - } - val context = TransactionContext("name", "op") - val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = true }) - verify(mockTransactionProfiler, never()).start() - verify(mockTransactionProfiler).bindTransaction(eq(transaction)) - } - - @Test - fun `when profiler is not running, startTransaction starts and binds current profile`() { - val mockTransactionProfiler = mock() - whenever(mockTransactionProfiler.isRunning).thenReturn(false) - val scopes = generateHub { - it.profilesSampleRate = 1.0 - it.setTransactionProfiler(mockTransactionProfiler) - } - val context = TransactionContext("name", "op") - val transaction = scopes.startTransaction(context, TransactionOptions().apply { isAppStartTransaction = false }) - verify(mockTransactionProfiler).start() - verify(mockTransactionProfiler).bindTransaction(eq(transaction)) - } - //endregion - - //region startTransaction tests - @Test - fun `when startTransaction, creates transaction`() { - val scopes = generateHub() - val contexts = TransactionContext("name", "op") - - val transaction = scopes.startTransaction(contexts) - assertTrue(transaction is SentryTracer) - assertEquals(contexts, transaction.root.spanContext) - } - - @Test - fun `when startTransaction with bindToScope set to false, transaction is not attached to the scope`() { - val scopes = generateHub() - - scopes.startTransaction("name", "op", TransactionOptions()) - - scopes.configureScope { - assertNull(it.span) - } - } - - @Test - fun `when startTransaction without bindToScope set, transaction is not attached to the scope`() { - val scopes = generateHub() - - scopes.startTransaction("name", "op") - - scopes.configureScope { - assertNull(it.span) - } - } - - @Test - fun `when startTransaction with bindToScope set to true, transaction is attached to the scope`() { - val scopes = generateHub() - - val transaction = scopes.startTransaction("name", "op", TransactionOptions().also { it.isBindToScope = true }) - - scopes.configureScope { - assertEquals(transaction, it.span) - } - } - - @Test - fun `when startTransaction and no tracing sampling is configured, event is not sampled`() { - val scopes = generateHub { - it.tracesSampleRate = 0.0 - } - - val transaction = scopes.startTransaction("name", "op") - assertFalse(transaction.isSampled!!) - } - - @Test - fun `when startTransaction and no profile sampling is configured, profile is not sampled`() { - val scopes = generateHub { - it.tracesSampleRate = 1.0 - it.profilesSampleRate = 0.0 - } - - val transaction = scopes.startTransaction("name", "op") - assertTrue(transaction.isSampled!!) - assertFalse(transaction.isProfileSampled!!) - } - - @Test - fun `when startTransaction with parent sampled and no traces sampler provided, transaction inherits sampling decision`() { - val scopes = generateHub() - val transactionContext = TransactionContext("name", "op") - transactionContext.parentSampled = true - val transaction = scopes.startTransaction(transactionContext) - assertNotNull(transaction) - assertNotNull(transaction.isSampled) - assertTrue(transaction.isSampled!!) - } - - @Test - fun `when startTransaction with parent profile sampled and no profile sampler provided, transaction inherits profile sampling decision`() { - val scopes = generateHub() - val transactionContext = TransactionContext("name", "op") - transactionContext.setParentSampled(true, true) - val transaction = scopes.startTransaction(transactionContext) - assertTrue(transaction.isProfileSampled!!) - } - - @Test - fun `Hub should close the sentry executor processor, profiler and performance collector on close call`() { - val executor = mock() - val profiler = mock() - val performanceCollector = mock() - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - cacheDirPath = file.absolutePath - executorService = executor - setTransactionProfiler(profiler) - transactionPerformanceCollector = performanceCollector - } - val sut = Hub(options) - sut.close() - verify(executor).close(any()) - verify(profiler).close() - verify(performanceCollector).close() - } - - @Test - fun `Hub with isRestarting true should close the sentry executor in the background`() { - val executor = spy(DeferredExecutorService()) - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - executorService = executor - } - val sut = Hub(options) - sut.close(true) - verify(executor, never()).close(any()) - executor.runAll() - verify(executor).close(any()) - } - - @Test - fun `Hub with isRestarting false should close the sentry executor in the background`() { - val executor = mock() - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - executorService = executor - } - val sut = Hub(options) - sut.close(false) - verify(executor).close(any()) - } - - @Test - fun `Hub close should clear the scope`() { - val options = SentryOptions().apply { - dsn = "https://key@sentry.io/proj" - } - - val sut = Hub(options) - sut.addBreadcrumb("Test") - sut.startTransaction("test", "test.op", TransactionOptions().also { it.isBindToScope = true }) - sut.close() - - // we have to clone the scope, so its isEnabled returns true, but it's still built up from - // the old scope preserving its data - val clone = sut.clone() - var oldScope: IScope? = null - clone.configureScope { scope -> oldScope = scope } - assertNull(oldScope!!.transaction) - assertTrue(oldScope!!.breadcrumbs.isEmpty()) - } - - @Test - fun `when tracesSampleRate and tracesSampler are not set on SentryOptions, startTransaction returns NoOp`() { - val scopes = generateHub { - it.tracesSampleRate = null - it.tracesSampler = null - } - val transaction = scopes.startTransaction(TransactionContext("name", "op", TracesSamplingDecision(true))) - assertTrue(transaction is NoOpTransaction) - } - //endregion - - //region startTransaction tests - @Test - fun `when traceHeaders and no transaction is active, traceHeaders are generated from scope`() { - val scopes = generateHub() - - var spanId: SpanId? = null - scopes.configureScope { spanId = it.propagationContext.spanId } - - val traceHeader = scopes.traceHeaders() - assertNotNull(traceHeader) - assertEquals(spanId, traceHeader.spanId) - } - - @Test - fun `when traceHeaders and there is an active transaction, traceHeaders are not null`() { - val scopes = generateHub() - val tx = scopes.startTransaction("aTransaction", "op") - scopes.configureScope { it.setTransaction(tx) } - - assertNotNull(scopes.traceHeaders()) - } - //endregion - - //region getSpan tests - @Test - fun `when there is no active transaction, getSpan returns null`() { - val scopes = generateHub() - assertNull(scopes.span) - } - - @Test - fun `when there is no active transaction, getTransaction returns null`() { - val scopes = generateHub() - assertNull(scopes.transaction) - } - - @Test - fun `when there is active transaction bound to the scope, getTransaction and getSpan return active transaction`() { - val scopes = generateHub() - val tx = scopes.startTransaction("aTransaction", "op") - scopes.configureScope { it.transaction = tx } - - assertEquals(tx, scopes.transaction) - assertEquals(tx, scopes.span) - } - - @Test - fun `when there is a transaction but the scopes is closed, getTransaction returns null`() { - val scopes = generateHub() - scopes.startTransaction("name", "op") - scopes.close() - - assertNull(scopes.transaction) - } - - @Test - fun `when there is active span within a transaction bound to the scope, getSpan returns active span`() { - val scopes = generateHub() - val tx = scopes.startTransaction("aTransaction", "op") - scopes.configureScope { it.setTransaction(tx) } - scopes.configureScope { it.setTransaction(tx) } - val span = tx.startChild("op") - - assertEquals(tx, scopes.transaction) - assertEquals(span, scopes.span) - } - // endregion - - //region setSpanContext - @Test - fun `associates span context with throwable`() { - val (scopes, mockClient) = getEnabledHub() - val transaction = scopes.startTransaction("aTransaction", "op") - val span = transaction.startChild("op") - val exception = RuntimeException() - - scopes.setSpanContext(exception, span, "tx-name") - scopes.captureEvent(SentryEvent(exception)) - - verify(mockClient).captureEvent( - check { - assertEquals(span.spanContext, it.contexts.trace) - }, - anyOrNull(), - anyOrNull() - ) - } - - @Test - fun `returns null when no span context associated with throwable`() { - val scopes = generateHub() as Hub - assertNull(scopes.getSpanContext(RuntimeException())) - } - // endregion - - @Test - fun `isCrashedLastRun does not delete native marker if auto session is enabled`() { - val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) - nativeMarker.mkdirs() - nativeMarker.createNewFile() - val scopes = generateHub() as Hub - - assertTrue(scopes.isCrashedLastRun!!) - assertTrue(nativeMarker.exists()) - } - - @Test - fun `isCrashedLastRun deletes the native marker if auto session is disabled`() { - val nativeMarker = File(hashedFolder(), EnvelopeCache.NATIVE_CRASH_MARKER_FILE) - nativeMarker.mkdirs() - nativeMarker.createNewFile() - val scopes = generateHub { - it.isEnableAutoSessionTracking = false - } - - assertTrue(scopes.isCrashedLastRun!!) - assertFalse(nativeMarker.exists()) - } - - @Test - fun `reportFullyDisplayed is ignored if TimeToFullDisplayTracing is disabled`() { - var called = false - val scopes = generateHub { - it.fullyDisplayedReporter.registerFullyDrawnListener { - called = !called - } - } - scopes.reportFullyDisplayed() - assertFalse(called) - } - - @Test - fun `reportFullyDisplayed calls FullyDisplayedReporter if TimeToFullDisplayTracing is enabled`() { - var called = false - val scopes = generateHub { - it.isEnableTimeToFullDisplayTracing = true - it.fullyDisplayedReporter.registerFullyDrawnListener { - called = !called - } - } - scopes.reportFullyDisplayed() - assertTrue(called) - } - - @Test - fun `reportFullyDisplayed calls FullyDisplayedReporter only once`() { - var called = false - val scopes = generateHub { - it.isEnableTimeToFullDisplayTracing = true - it.fullyDisplayedReporter.registerFullyDrawnListener { - called = !called - } - } - scopes.reportFullyDisplayed() - assertTrue(called) - scopes.reportFullyDisplayed() - assertTrue(called) - } - - @Test - fun `reportFullDisplayed calls reportFullyDisplayed`() { - val scopes = spy(generateHub()) - scopes.reportFullDisplayed() - verify(scopes).reportFullyDisplayed() - } - - @Test - fun `continueTrace creates propagation context from headers and returns transaction context if performance enabled`() { - val scopes = generateHub() - val traceId = SentryId() - val parentSpanId = SpanId() - val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - - scopes.configureScope { scope -> - assertEquals(traceId, scope.propagationContext.traceId) - assertEquals(parentSpanId, scope.propagationContext.parentSpanId) - } - - assertEquals(traceId, transactionContext!!.traceId) - assertEquals(parentSpanId, transactionContext!!.parentSpanId) - } - - @Test - fun `continueTrace creates new propagation context if header invalid and returns transaction context if performance enabled`() { - val scopes = generateHub() - val traceId = SentryId() - var propagationContextHolder = AtomicReference() - - scopes.configureScope { propagationContextHolder.set(it.propagationContext) } - val propagationContextAtStart = propagationContextHolder.get()!! - - val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - - scopes.configureScope { scope -> - assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) - assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) - assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) - - assertEquals(scope.propagationContext.traceId, transactionContext!!.traceId) - assertEquals(scope.propagationContext.parentSpanId, transactionContext!!.parentSpanId) - assertEquals(scope.propagationContext.spanId, transactionContext!!.spanId) - } - } - - @Test - fun `continueTrace creates propagation context from headers and returns null if performance disabled`() { - val scopes = generateHub { it.enableTracing = false } - val traceId = SentryId() - val parentSpanId = SpanId() - val transactionContext = scopes.continueTrace("$traceId-$parentSpanId-1", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - - scopes.configureScope { scope -> - assertEquals(traceId, scope.propagationContext.traceId) - assertEquals(parentSpanId, scope.propagationContext.parentSpanId) - } - - assertNull(transactionContext) - } - - @Test - fun `continueTrace creates new propagation context if header invalid and returns null if performance disabled`() { - val scopes = generateHub { it.enableTracing = false } - val traceId = SentryId() - var propagationContextHolder = AtomicReference() - - scopes.configureScope { propagationContextHolder.set(it.propagationContext) } - val propagationContextAtStart = propagationContextHolder.get()!! - - val transactionContext = scopes.continueTrace("invalid", listOf("sentry-public_key=502f25099c204a2fbf4cb16edc5975d1,sentry-sample_rate=1,sentry-trace_id=$traceId,sentry-transaction=HTTP%20GET")) - - scopes.configureScope { scope -> - assertNotEquals(propagationContextAtStart.traceId, scope.propagationContext.traceId) - assertNotEquals(propagationContextAtStart.parentSpanId, scope.propagationContext.parentSpanId) - assertNotEquals(propagationContextAtStart.spanId, scope.propagationContext.spanId) - } - - assertNull(transactionContext) - } - - @Test - fun `scopes provides no tags for metrics, if metric option is disabled`() { - val scopes = generateHub { - it.isEnableMetrics = false - it.isEnableDefaultTagsForMetrics = true - } as Hub - - assertTrue( - scopes.defaultTagsForMetrics.isEmpty() - ) - } - - @Test - fun `scopes provides no tags for metrics, if default tags option is disabled`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableDefaultTagsForMetrics = false - } as Hub - - assertTrue( - scopes.defaultTagsForMetrics.isEmpty() - ) - } - - @Test - fun `scopes provides minimum default tags for metrics, if nothing is set up`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableDefaultTagsForMetrics = true - } as Hub - - assertEquals( - mapOf( - "environment" to "production" - ), - scopes.defaultTagsForMetrics - ) - } - - @Test - fun `scopes provides default tags for metrics, based on options and running transaction`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableDefaultTagsForMetrics = true - it.environment = "test" - it.release = "1.0" - } as Hub - scopes.startTransaction( - "name", - "op", - TransactionOptions().apply { isBindToScope = true } - ) - - assertEquals( - mapOf( - "environment" to "test", - "release" to "1.0", - "transaction" to "name" - ), - scopes.defaultTagsForMetrics - ) - } - - @Test - fun `scopes provides no local metric aggregator if metrics feature is disabled`() { - val scopes = generateHub { - it.isEnableMetrics = false - it.isEnableSpanLocalMetricAggregation = true - } as Hub - - scopes.startTransaction( - "name", - "op", - TransactionOptions().apply { isBindToScope = true } - ) - - assertNull(scopes.localMetricsAggregator) - } - - @Test - fun `scopes provides no local metric aggregator if local aggregation feature is disabled`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableSpanLocalMetricAggregation = false - } as Hub - - scopes.startTransaction( - "name", - "op", - TransactionOptions().apply { isBindToScope = true } - ) - - assertNull(scopes.localMetricsAggregator) - } - - @Test - fun `scopes provides local metric aggregator if feature is enabled`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableSpanLocalMetricAggregation = true - } as Hub - - scopes.startTransaction( - "name", - "op", - TransactionOptions().apply { isBindToScope = true } - ) - assertNotNull(scopes.localMetricsAggregator) - } - - @Test - fun `scopes startSpanForMetric starts a child span`() { - val scopes = generateHub { - it.isEnableMetrics = true - it.isEnableSpanLocalMetricAggregation = true - it.sampleRate = 1.0 - } as Hub - - val txn = scopes.startTransaction( - "name.txn", - "op.txn", - TransactionOptions().apply { isBindToScope = true } - ) - - val span = scopes.startSpanForMetric("op", "key")!! - - assertEquals("op", span.spanContext.op) - assertEquals("key", span.spanContext.description) - assertEquals(span.spanContext.parentSpanId, txn.spanContext.spanId) - } - - private val dsnTest = "https://key@sentry.io/proj" - - private fun generateHub(optionsConfiguration: Sentry.OptionsConfiguration? = null): IScopes { - val options = SentryOptions().apply { - dsn = dsnTest - cacheDirPath = file.absolutePath - setSerializer(mock()) - tracesSampleRate = 1.0 - } - optionsConfiguration?.configure(options) - return Hub(options) - } - - private fun getEnabledHub(): Triple { - val logger = mock() - - val options = SentryOptions() - options.cacheDirPath = file.absolutePath - options.dsn = "https://key@sentry.io/proj" - options.setSerializer(mock()) - options.tracesSampleRate = 1.0 - options.isDebug = true - options.setLogger(logger) - - val sut = Hub(options) - val mockClient = mock() - sut.bindClient(mockClient) - return Triple(sut, mockClient, logger) - } - - private fun hashedFolder(): String { - val hash = StringUtils.calculateStringHash(dsnTest, mock()) - val fileHashFolder = File(file.absolutePath, hash!!) - return fileHashFolder.absolutePath - } - - private fun equalTraceContext(expectedContext: TraceContext?): TraceContext? { - expectedContext ?: return eq(null) - - return argWhere { actual -> - expectedContext.traceId == actual.traceId && - expectedContext.transaction == actual.transaction && - expectedContext.environment == actual.environment && - expectedContext.release == actual.release && - expectedContext.publicKey == actual.publicKey && - expectedContext.sampleRate == actual.sampleRate && - expectedContext.userId == actual.userId && - expectedContext.userSegment == actual.userSegment - } - } -} From 8b006e9f7ca766039c80f4239c5e71a48b3bcf06 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 29 Apr 2024 16:40:30 +0200 Subject: [PATCH 49/57] Report suppressed exceptions as exception group --- .../io/sentry/SentryExceptionFactory.java | 40 +++++++++++-- .../java/io/sentry/protocol/Mechanism.java | 57 +++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/sentry/src/main/java/io/sentry/SentryExceptionFactory.java b/sentry/src/main/java/io/sentry/SentryExceptionFactory.java index 6652ebb504..12ce461ce1 100644 --- a/sentry/src/main/java/io/sentry/SentryExceptionFactory.java +++ b/sentry/src/main/java/io/sentry/SentryExceptionFactory.java @@ -12,7 +12,7 @@ import java.util.Deque; import java.util.HashSet; import java.util.List; -import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -136,12 +136,20 @@ public List getSentryExceptions(final @NotNull Throwable throwa @TestOnly @NotNull Deque extractExceptionQueue(final @NotNull Throwable throwable) { - final Deque exceptions = new ArrayDeque<>(); - final Set circularityDetector = new HashSet<>(); + return extractExceptionQueueInternal( + throwable, new AtomicInteger(-1), new HashSet<>(), new ArrayDeque<>()); + } + + Deque extractExceptionQueueInternal( + final @NotNull Throwable throwable, + final @NotNull AtomicInteger exceptionId, + final @NotNull HashSet circularityDetector, + final @NotNull Deque exceptions) { Mechanism exceptionMechanism; Thread thread; Throwable currentThrowable = throwable; + Integer parentId = exceptionId.get(); // Stack the exceptions to send them in the reverse order while (currentThrowable != null && circularityDetector.add(currentThrowable)) { @@ -155,12 +163,11 @@ Deque extractExceptionQueue(final @NotNull Throwable throwable) thread = exceptionMechanismThrowable.getThread(); snapshot = exceptionMechanismThrowable.isSnapshot(); } else { - exceptionMechanism = null; + exceptionMechanism = new Mechanism(); thread = Thread.currentThread(); } - final boolean includeSentryFrames = - exceptionMechanism != null && Boolean.FALSE.equals(exceptionMechanism.isHandled()); + final boolean includeSentryFrames = Boolean.FALSE.equals(exceptionMechanism.isHandled()); final List frames = sentryStackTraceFactory.getStackFrames( currentThrowable.getStackTrace(), includeSentryFrames); @@ -168,7 +175,28 @@ Deque extractExceptionQueue(final @NotNull Throwable throwable) getSentryException( currentThrowable, exceptionMechanism, thread.getId(), frames, snapshot); exceptions.addFirst(exception); + + if (exceptionMechanism.getType() == null) { + exceptionMechanism.setType("chained"); + } + + if (exceptionId.get() >= 0) { + exceptionMechanism.setParentId(parentId); + } + + final int currentExceptionId = exceptionId.incrementAndGet(); + exceptionMechanism.setExceptionId(currentExceptionId); + + Throwable[] suppressed = currentThrowable.getSuppressed(); + if (suppressed != null && suppressed.length > 0) { + exceptionMechanism.setExceptionGroup(true); + for (Throwable suppressedThrowable : suppressed) { + extractExceptionQueueInternal( + suppressedThrowable, exceptionId, circularityDetector, exceptions); + } + } currentThrowable = currentThrowable.getCause(); + parentId = currentExceptionId; } return exceptions; diff --git a/sentry/src/main/java/io/sentry/protocol/Mechanism.java b/sentry/src/main/java/io/sentry/protocol/Mechanism.java index 648aed39c2..8fc9aedf77 100644 --- a/sentry/src/main/java/io/sentry/protocol/Mechanism.java +++ b/sentry/src/main/java/io/sentry/protocol/Mechanism.java @@ -67,6 +67,18 @@ public final class Mechanism implements JsonUnknown, JsonSerializable { * for grouping or display purposes. */ private @Nullable Boolean synthetic; + /** + * Exception ID. Used. e.g. for exception groups to build a hierarchy. This is referenced as + * parent by child exceptions which for Java SDK means Throwable.getSuppressed(). + */ + private @Nullable Integer exceptionId; + /** Parent exception ID. Used e.g. for exception groups to build a hierarchy. */ + private @Nullable Integer parentId; + /** + * Whether this is a group of exceptions. For Java SDK this means there were suppressed + * exceptions. + */ + private @Nullable Boolean exceptionGroup; @SuppressWarnings("unused") private @Nullable Map unknown; @@ -140,6 +152,30 @@ public void setSynthetic(final @Nullable Boolean synthetic) { this.synthetic = synthetic; } + public @Nullable Integer getExceptionId() { + return exceptionId; + } + + public void setExceptionId(final @Nullable Integer exceptionId) { + this.exceptionId = exceptionId; + } + + public @Nullable Integer getParentId() { + return parentId; + } + + public void setParentId(final @Nullable Integer parentId) { + this.parentId = parentId; + } + + public @Nullable Boolean isExceptionGroup() { + return exceptionGroup; + } + + public void setExceptionGroup(final @Nullable Boolean exceptionGroup) { + this.exceptionGroup = exceptionGroup; + } + // JsonKeys public static final class JsonKeys { @@ -150,6 +186,9 @@ public static final class JsonKeys { public static final String META = "meta"; public static final String DATA = "data"; public static final String SYNTHETIC = "synthetic"; + public static final String EXCEPTION_ID = "exception_id"; + public static final String PARENT_ID = "parent_id"; + public static final String IS_EXCEPTION_GROUP = "is_exception_group"; } // JsonUnknown @@ -191,6 +230,15 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger if (synthetic != null) { writer.name(JsonKeys.SYNTHETIC).value(synthetic); } + if (exceptionId != null) { + writer.name(JsonKeys.EXCEPTION_ID).value(logger, exceptionId); + } + if (parentId != null) { + writer.name(JsonKeys.PARENT_ID).value(logger, parentId); + } + if (exceptionGroup != null) { + writer.name(JsonKeys.IS_EXCEPTION_GROUP).value(exceptionGroup); + } if (unknown != null) { for (String key : unknown.keySet()) { Object value = unknown.get(key); @@ -238,6 +286,15 @@ public static final class Deserializer implements JsonDeserializer { case JsonKeys.SYNTHETIC: mechanism.synthetic = reader.nextBooleanOrNull(); break; + case JsonKeys.EXCEPTION_ID: + mechanism.exceptionId = reader.nextIntegerOrNull(); + break; + case JsonKeys.PARENT_ID: + mechanism.parentId = reader.nextIntegerOrNull(); + break; + case JsonKeys.IS_EXCEPTION_GROUP: + mechanism.exceptionGroup = reader.nextBooleanOrNull(); + break; default: if (unknown == null) { unknown = new HashMap<>(); From fe6dcf4a7ed0eeed0a76d21d8994e6d562da9dcd Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 30 Apr 2024 10:46:54 +0200 Subject: [PATCH 50/57] api dump --- sentry/api/sentry.api | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index b201a47d0c..5a68a92811 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -4472,18 +4472,24 @@ public final class io/sentry/protocol/Mechanism : io/sentry/JsonSerializable, io public fun (Ljava/lang/Thread;)V public fun getData ()Ljava/util/Map; public fun getDescription ()Ljava/lang/String; + public fun getExceptionId ()Ljava/lang/Integer; public fun getHelpLink ()Ljava/lang/String; public fun getMeta ()Ljava/util/Map; + public fun getParentId ()Ljava/lang/Integer; public fun getSynthetic ()Ljava/lang/Boolean; public fun getType ()Ljava/lang/String; public fun getUnknown ()Ljava/util/Map; + public fun isExceptionGroup ()Ljava/lang/Boolean; public fun isHandled ()Ljava/lang/Boolean; public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V public fun setData (Ljava/util/Map;)V public fun setDescription (Ljava/lang/String;)V + public fun setExceptionGroup (Ljava/lang/Boolean;)V + public fun setExceptionId (Ljava/lang/Integer;)V public fun setHandled (Ljava/lang/Boolean;)V public fun setHelpLink (Ljava/lang/String;)V public fun setMeta (Ljava/util/Map;)V + public fun setParentId (Ljava/lang/Integer;)V public fun setSynthetic (Ljava/lang/Boolean;)V public fun setType (Ljava/lang/String;)V public fun setUnknown (Ljava/util/Map;)V @@ -4498,9 +4504,12 @@ public final class io/sentry/protocol/Mechanism$Deserializer : io/sentry/JsonDes public final class io/sentry/protocol/Mechanism$JsonKeys { public static final field DATA Ljava/lang/String; public static final field DESCRIPTION Ljava/lang/String; + public static final field EXCEPTION_ID Ljava/lang/String; public static final field HANDLED Ljava/lang/String; public static final field HELP_LINK Ljava/lang/String; + public static final field IS_EXCEPTION_GROUP Ljava/lang/String; public static final field META Ljava/lang/String; + public static final field PARENT_ID Ljava/lang/String; public static final field SYNTHETIC Ljava/lang/String; public static final field TYPE Ljava/lang/String; public fun ()V From 7316651b8d58c6663cd9d33a8e898605252d2544 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 30 Apr 2024 14:08:09 +0200 Subject: [PATCH 51/57] add tests for suppressed exceptions --- .../io/sentry/SentryExceptionFactoryTest.kt | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt index 888f17e0a3..84a51f1205 100644 --- a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt @@ -209,6 +209,95 @@ class SentryExceptionFactoryTest { assertEquals(777, frame.lineno) } + @Test + fun `when exception has has suppressed exceptions, add them and show as group`() { + val exception = Exception("message") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val mechanism = Mechanism() + mechanism.type = "ANR" + val thread = Thread() + val throwable = ExceptionMechanismException(mechanism, exception, thread) + + val queue = fixture.getSut().extractExceptionQueue(throwable) + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(1, suppressedInQueue.mechanism?.exceptionId) + assertEquals(0, suppressedInQueue.mechanism?.parentId) + + assertEquals("message", mainInQueue.value) + assertEquals(0, mainInQueue.mechanism?.exceptionId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + } + + @Test + fun `nested exception that contains suppressed exceptions is marked as group`() { + val exception = Exception("inner") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val outerException = Exception("outer", exception) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(2, suppressedInQueue.mechanism?.exceptionId) + assertEquals(1, suppressedInQueue.mechanism?.parentId) + + assertEquals("inner", mainInQueue.value) + assertEquals(1, mainInQueue.mechanism?.exceptionId) + assertEquals(0, mainInQueue.mechanism?.parentId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + + @Test + fun `nested exception within Mechanism that contains suppressed exceptions is marked as group`() { + val exception = Exception("inner") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val mechanism = Mechanism() + mechanism.type = "ANR" + val thread = Thread() + + val outerException = ExceptionMechanismException(mechanism, Exception("outer", exception), thread) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(2, suppressedInQueue.mechanism?.exceptionId) + assertEquals(1, suppressedInQueue.mechanism?.parentId) + + assertEquals("inner", mainInQueue.value) + assertEquals(1, mainInQueue.mechanism?.exceptionId) + assertEquals(0, mainInQueue.mechanism?.parentId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + internal class InnerClassThrowable constructor(cause: Throwable? = null) : Throwable(cause) private val anonymousException = object : Exception() { From 3518c059282616a23ee8465afeadd3fde8ac70c1 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 30 Apr 2024 12:10:06 +0000 Subject: [PATCH 52/57] Format code --- sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt index 84a51f1205..c67264faf3 100644 --- a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt @@ -244,7 +244,6 @@ class SentryExceptionFactoryTest { val queue = fixture.getSut().extractExceptionQueue(outerException) - val suppressedInQueue = queue.pop() val mainInQueue = queue.pop() val outerInQueue = queue.pop() @@ -278,7 +277,6 @@ class SentryExceptionFactoryTest { val queue = fixture.getSut().extractExceptionQueue(outerException) - val suppressedInQueue = queue.pop() val mainInQueue = queue.pop() val outerInQueue = queue.pop() From 663b33757015e2328409614a656f09644bae4434 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 30 Apr 2024 15:29:55 +0200 Subject: [PATCH 53/57] add additinoal test --- .../io/sentry/SentryExceptionFactoryTest.kt | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt index c67264faf3..9fa51ba934 100644 --- a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt @@ -210,7 +210,7 @@ class SentryExceptionFactoryTest { } @Test - fun `when exception has has suppressed exceptions, add them and show as group`() { + fun `when exception with mechanism suppressed exceptions, add them and show as group`() { val exception = Exception("message") val suppressedException = Exception("suppressed") exception.addSuppressed(suppressedException) @@ -296,6 +296,106 @@ class SentryExceptionFactoryTest { assertNull(outerInQueue.mechanism?.isExceptionGroup) } + @Test + fun `nested exception with nested exception that contain suppressed exceptions are marked as group`() { + val innerMostException = Exception("innermost") + val innerMostSuppressed = Exception("innermostSuppressed") + innerMostException.addSuppressed(innerMostSuppressed) + + val innerException = Exception("inner", innerMostException) + val innerSuppressed = Exception("suppressed") + innerException.addSuppressed(innerSuppressed) + + val outerException = Exception("outer", innerException) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + val innerMostSuppressedInQueue = queue.pop() + val innerMostExceptionInQueue = queue.pop() + val innerSuppressedInQueue = queue.pop() + val innerExceptionInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("innermostSuppressed", innerMostSuppressedInQueue.value) + assertEquals(4, innerMostSuppressedInQueue.mechanism?.exceptionId) + assertEquals(3, innerMostSuppressedInQueue.mechanism?.parentId) + assertNull(innerMostSuppressedInQueue.mechanism?.isExceptionGroup) + + assertEquals("innermost", innerMostExceptionInQueue.value) + assertEquals(3, innerMostExceptionInQueue.mechanism?.exceptionId) + assertEquals(1, innerMostExceptionInQueue.mechanism?.parentId) + assertEquals(true, innerMostExceptionInQueue.mechanism?.isExceptionGroup) + + assertEquals("suppressed", innerSuppressedInQueue.value) + assertEquals(2, innerSuppressedInQueue.mechanism?.exceptionId) + assertEquals(1, innerSuppressedInQueue.mechanism?.parentId) + assertNull(innerSuppressedInQueue.mechanism?.isExceptionGroup) + + assertEquals("inner", innerExceptionInQueue.value) + assertEquals(1, innerExceptionInQueue.mechanism?.exceptionId) + assertEquals(0, innerExceptionInQueue.mechanism?.parentId) + assertEquals(true, innerExceptionInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + + @Test + fun `nested exception with nested exception that contain suppressed exceptions with a nested exception are marked as group`() { + val innerMostException = Exception("innermost") + + val innerMostSuppressedNestedException = Exception("innermostSuppressedNested"); + val innerMostSuppressed = Exception("innermostSuppressed", innerMostSuppressedNestedException) + innerMostException.addSuppressed(innerMostSuppressed) + + val innerException = Exception("inner", innerMostException) + val innerSuppressed = Exception("suppressed") + innerException.addSuppressed(innerSuppressed) + + val outerException = Exception("outer", innerException) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + val innerMostSuppressedNestedExceptionInQueue = queue.pop() + val innerMostSuppressedInQueue = queue.pop() + val innerMostExceptionInQueue = queue.pop() + val innerSuppressedInQueue = queue.pop() + val innerExceptionInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("innermostSuppressedNested", innerMostSuppressedNestedExceptionInQueue.value) + assertEquals(5, innerMostSuppressedNestedExceptionInQueue.mechanism?.exceptionId) + assertEquals(4, innerMostSuppressedNestedExceptionInQueue.mechanism?.parentId) + assertNull(innerMostSuppressedNestedExceptionInQueue.mechanism?.isExceptionGroup) + + assertEquals("innermostSuppressed", innerMostSuppressedInQueue.value) + assertEquals(4, innerMostSuppressedInQueue.mechanism?.exceptionId) + assertEquals(3, innerMostSuppressedInQueue.mechanism?.parentId) + assertNull(innerMostSuppressedInQueue.mechanism?.isExceptionGroup) + + assertEquals("innermost", innerMostExceptionInQueue.value) + assertEquals(3, innerMostExceptionInQueue.mechanism?.exceptionId) + assertEquals(1, innerMostExceptionInQueue.mechanism?.parentId) + assertEquals(true, innerMostExceptionInQueue.mechanism?.isExceptionGroup) + + assertEquals("suppressed", innerSuppressedInQueue.value) + assertEquals(2, innerSuppressedInQueue.mechanism?.exceptionId) + assertEquals(1, innerSuppressedInQueue.mechanism?.parentId) + assertNull(innerSuppressedInQueue.mechanism?.isExceptionGroup) + + assertEquals("inner", innerExceptionInQueue.value) + assertEquals(1, innerExceptionInQueue.mechanism?.exceptionId) + assertEquals(0, innerExceptionInQueue.mechanism?.parentId) + assertEquals(true, innerExceptionInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + internal class InnerClassThrowable constructor(cause: Throwable? = null) : Throwable(cause) private val anonymousException = object : Exception() { From 2cf9d8f6f0c039fb51c3fa9e31fcee99331a5c2a Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 30 Apr 2024 13:32:08 +0000 Subject: [PATCH 54/57] Format code --- sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt index 9fa51ba934..8eb7f0e42d 100644 --- a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt @@ -346,7 +346,7 @@ class SentryExceptionFactoryTest { fun `nested exception with nested exception that contain suppressed exceptions with a nested exception are marked as group`() { val innerMostException = Exception("innermost") - val innerMostSuppressedNestedException = Exception("innermostSuppressedNested"); + val innerMostSuppressedNestedException = Exception("innermostSuppressedNested") val innerMostSuppressed = Exception("innermostSuppressed", innerMostSuppressedNestedException) innerMostException.addSuppressed(innerMostSuppressed) From bde22c407f5d1afa34f4800dcd639faa9bcad987 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Wed, 1 May 2024 20:00:31 +0200 Subject: [PATCH 55/57] apply suggestion --- sentry/src/main/java/io/sentry/SentryExceptionFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry/src/main/java/io/sentry/SentryExceptionFactory.java b/sentry/src/main/java/io/sentry/SentryExceptionFactory.java index 12ce461ce1..2808df11c0 100644 --- a/sentry/src/main/java/io/sentry/SentryExceptionFactory.java +++ b/sentry/src/main/java/io/sentry/SentryExceptionFactory.java @@ -149,7 +149,7 @@ Deque extractExceptionQueueInternal( Thread thread; Throwable currentThrowable = throwable; - Integer parentId = exceptionId.get(); + int parentId = exceptionId.get(); // Stack the exceptions to send them in the reverse order while (currentThrowable != null && circularityDetector.add(currentThrowable)) { From f52300bff387d9f89e1f4b5023dc2aff0233abe0 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Fri, 10 May 2024 11:31:55 +0200 Subject: [PATCH 56/57] add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b578ed17f..bda62d9ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ # Changelog -## Unreleased +## 8.0.0 Unreleased ### Features - Add support for Spring Rest Client ([#3199](https://github.com/getsentry/sentry-java/pull/3199)) +- Report exceptions returned by Throwable.getSuppressed() to Sentry as exception groups ([#3396] https://github.com/getsentry/sentry-java/pull/3396) ## 7.6.0 From 5897bd78bf6e3f9d09cac66dad8225b58b2324da Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Fri, 10 May 2024 13:20:54 +0200 Subject: [PATCH 57/57] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bda62d9ba2..1c90c3589e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 8.0.0 Unreleased +## Unreleased ### Features