From a9936455ee2294560fd6c62694c6f0b6a85ab8dd Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Tue, 1 Dec 2026 16:01:40 +0100 Subject: [PATCH 1/4] ref(core): Add SentryRuntime for the SDK's own collaborators SentryRuntime holds the collaborators the SDK owns rather than the customer (serializer, executor, envelope disk cache). Sentry.init builds it once, through LegacyOptionsBridge, from what is on SentryOptions and copies it back, so existing readers see the same instances. Nothing reads from it yet; follow-up PRs move the readers to constructor injection before the SentryOptions accessors get deprecated. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../api/sentry-android-core.api | 6 + .../android/core/InternalSentrySdk.java | 23 +++ .../io/sentry/android/core/SentryAndroid.java | 14 +- sentry/api/sentry.api | 31 ++++ sentry/src/main/java/io/sentry/Sentry.java | 48 +++++-- .../sentry/internal/InternalIntegration.java | 19 +++ .../io/sentry/internal/LazySerializer.java | 63 ++++++++ .../sentry/internal/LegacyOptionsBridge.java | 79 +++++++++++ .../io/sentry/internal/SentryRuntime.java | 134 ++++++++++++++++++ .../io/sentry/internal/SentryRuntimeTest.kt | 118 +++++++++++++++ 10 files changed, 525 insertions(+), 10 deletions(-) create mode 100644 sentry/src/main/java/io/sentry/internal/InternalIntegration.java create mode 100644 sentry/src/main/java/io/sentry/internal/LazySerializer.java create mode 100644 sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java create mode 100644 sentry/src/main/java/io/sentry/internal/SentryRuntime.java create mode 100644 sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index dd207c3ef69..9a2c6f6d931 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -330,11 +330,17 @@ public final class io/sentry/android/core/InternalSentrySdk { public static fun captureEnvelopeNonTerminating ([B)Lio/sentry/protocol/SentryId; public static fun getAppStartMeasurement ()Ljava/util/Map; public static fun getCurrentScope ()Lio/sentry/IScope; + public static fun getRuntime ()Lio/sentry/internal/SentryRuntime; + public static fun init (Landroid/content/Context;Lio/sentry/ILogger;Lio/sentry/android/core/InternalSentrySdk$OptionsAndRuntimeConfiguration;)V public static fun serializeScope (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/IScope;)Ljava/util/Map; public static fun setTrace (Ljava/lang/String;Ljava/lang/String;Ljava/lang/Double;Ljava/lang/Double;)V public static fun updateSessionForDroppedEventNonTerminating (Z)V } +public abstract interface class io/sentry/android/core/InternalSentrySdk$OptionsAndRuntimeConfiguration { + public abstract fun configure (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/internal/SentryRuntime$Builder;)V +} + public final class io/sentry/android/core/LoadClass : io/sentry/util/LoadClass { public fun ()V public fun isClassAvailable (Ljava/lang/String;Lio/sentry/ILogger;)Z 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 104d2f0c803..bd8f4d791c4 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 @@ -16,6 +16,7 @@ import io.sentry.PropagationContext; import io.sentry.ScopeType; import io.sentry.ScopesAdapter; +import io.sentry.Sentry; import io.sentry.SentryEnvelope; import io.sentry.SentryEnvelopeItem; import io.sentry.SentryEvent; @@ -26,6 +27,7 @@ import io.sentry.android.core.performance.AppStartMetrics; import io.sentry.android.core.performance.TimeSpan; import io.sentry.cache.EnvelopeCache; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.App; import io.sentry.protocol.Device; import io.sentry.protocol.SentryId; @@ -50,6 +52,27 @@ @ApiStatus.Internal public final class InternalSentrySdk { + /** Configures both the options and the SDK's own collaborators. */ + public interface OptionsAndRuntimeConfiguration { + void configure(@NotNull SentryAndroidOptions options, @NotNull SentryRuntime.Builder runtime); + } + + /** + * Like {@link SentryAndroid#init(Context, ILogger, Sentry.OptionsConfiguration)}, but also lets + * the hybrid SDK set the SDK's own collaborators. + */ + public static void init( + final @NotNull Context context, + final @NotNull ILogger logger, + final @NotNull OptionsAndRuntimeConfiguration configuration) { + SentryAndroid.init(context, logger, configuration); + } + + /** The SDK's own collaborators, or {@code null} before init and after close. */ + public static @Nullable SentryRuntime getRuntime() { + return SentryRuntime.getCurrent(); + } + /** * Returns a copy of the current scopes's topmost scope, or null in case the scopes is disabled. */ 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 b131a0af3e7..c8d9039a452 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 @@ -20,6 +20,7 @@ import io.sentry.android.core.performance.TimeSpan; import io.sentry.android.fragment.FragmentLifecycleIntegration; import io.sentry.android.timber.SentryTimberIntegration; +import io.sentry.internal.SentryRuntime; import io.sentry.time.Stopwatch; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.FileUtils; @@ -103,11 +104,18 @@ public static void init( * @param logger your custom logger that implements ILogger * @param configuration Sentry.OptionsConfiguration configuration handler */ - @SuppressLint("NewApi") public static void init( @NotNull final Context context, @NotNull ILogger logger, @NotNull Sentry.OptionsConfiguration configuration) { + init(context, logger, (options, runtime) -> configuration.configure(options)); + } + + @SuppressLint("NewApi") + static void init( + @NotNull final Context context, + @NotNull ILogger logger, + @NotNull InternalSentrySdk.OptionsAndRuntimeConfiguration configuration) { if (isNdkCrashServiceProcess()) { logger.log( SentryLevel.DEBUG, "Skipping SentryAndroid.init in the NDK crash service process."); @@ -118,6 +126,7 @@ public static void init( // even if acquire() throws. Trace.beginSection("SentryAndroid.init"); try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) { + final @NotNull SentryRuntime.Builder runtime = SentryRuntime.builder(); Sentry.init( new SentryAndroidOptionsContainer(), options -> { @@ -161,7 +170,7 @@ public static void init( isDistributionAvailable); try { - configuration.configure(options); + configuration.configure(options, runtime); } catch (Throwable t) { // let it slip, but log it options @@ -202,6 +211,7 @@ public static void init( deduplicateIntegrations(options, isFragmentAvailable, isTimberAvailable); }, + runtime, true); final @NotNull IScopes scopes = Sentry.getCurrentScopes(); diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 3dc8ed74c4a..4a18b58dde2 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2938,6 +2938,7 @@ public final class io/sentry/Sentry { public static fun getTraceparent ()Lio/sentry/SentryTraceHeader; public static fun init ()V public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;)V + public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;Lio/sentry/internal/SentryRuntime$Builder;Z)V public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;Z)V public static fun init (Lio/sentry/Sentry$OptionsConfiguration;)V public static fun init (Lio/sentry/Sentry$OptionsConfiguration;Z)V @@ -5446,6 +5447,17 @@ public final class io/sentry/instrumentation/file/SentryFileWriter : java/io/Out public fun (Ljava/lang/String;Z)V } +public abstract interface class io/sentry/internal/InternalIntegration : io/sentry/Integration { + public abstract fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V +} + +public final class io/sentry/internal/LegacyOptionsBridge { + public static fun buildRuntime (Lio/sentry/internal/SentryRuntime$Builder;Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; + public static fun executorOf (Lio/sentry/SentryOptions;)Lio/sentry/ISentryExecutorService; + public static fun executorOrNew (Lio/sentry/SentryOptions;)Lio/sentry/ISentryExecutorService; + public static fun runtimeOf (Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; +} + public final class io/sentry/internal/ManifestVersionReader { public static fun getInstance ()Lio/sentry/internal/ManifestVersionReader; public fun readManifestFiles ()V @@ -5458,6 +5470,25 @@ public final class io/sentry/internal/ManifestVersionReader$VersionInfoHolder { public fun getSdkVersion ()Ljava/lang/String; } +public final class io/sentry/internal/SentryRuntime { + public static fun builder ()Lio/sentry/internal/SentryRuntime$Builder; + public static fun getCurrent ()Lio/sentry/internal/SentryRuntime; + public fun getEnvelopeDiskCache ()Lio/sentry/cache/IEnvelopeCache; + public fun getExecutorService ()Lio/sentry/ISentryExecutorService; + public fun getSerializer ()Lio/sentry/ISerializer; + public static fun setCurrent (Lio/sentry/internal/SentryRuntime;)V +} + +public final class io/sentry/internal/SentryRuntime$Builder { + public fun build (Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; + public fun buildForTest ()Lio/sentry/internal/SentryRuntime; + public fun envelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)Lio/sentry/internal/SentryRuntime$Builder; + public fun executorService (Lio/sentry/ISentryExecutorService;)Lio/sentry/internal/SentryRuntime$Builder; + public fun hasExecutorService ()Z + public fun lazySerializer (Lio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/internal/SentryRuntime$Builder; + public fun serializer (Lio/sentry/ISerializer;)Lio/sentry/internal/SentryRuntime$Builder; +} + public abstract interface class io/sentry/internal/debugmeta/IDebugMetaLoader { public abstract fun loadDebugMeta ()Ljava/util/List; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 8112cf0ee8b..aa8fdff4eb3 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -6,6 +6,9 @@ import io.sentry.cache.IEnvelopeCache; import io.sentry.cache.PersistingScopeObserver; import io.sentry.config.PropertiesProviderFactory; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.internal.debugmeta.NoOpDebugMetaLoader; import io.sentry.internal.debugmeta.ResourcesDebugMetaLoader; import io.sentry.internal.modules.CompositeModulesLoader; @@ -246,9 +249,26 @@ public static void init( InstantiationException, NoSuchMethodException, InvocationTargetException { + init(clazz, optionsConfiguration, SentryRuntime.builder(), globalHubMode); + } + + /** + * Initializes the SDK, letting the platform's init add its own collaborators to {@code runtime} + * while it configures the options. + */ + @ApiStatus.Internal + public static void init( + final @NotNull OptionsContainer clazz, + final @NotNull OptionsConfiguration optionsConfiguration, + final @NotNull SentryRuntime.Builder runtime, + final boolean globalHubMode) + throws IllegalAccessException, + InstantiationException, + NoSuchMethodException, + InvocationTargetException { final T options = clazz.createInstance(); applyOptionsConfiguration(optionsConfiguration, options); - init(options, globalHubMode); + init(options, runtime, globalHubMode); } /** @@ -301,12 +321,19 @@ public static void init(final @NotNull SentryOptions options) { * @param options options the SentryOptions * @param globalHubMode the globalHubMode */ + private static void init(final @NotNull SentryOptions options, final boolean globalHubMode) { + init(options, SentryRuntime.builder(), globalHubMode); + } + @SuppressWarnings({ "deprecation", "Convert2MethodRef", "FutureReturnValueIgnored" }) // older AGP versions do not support method references - private static void init(final @NotNull SentryOptions options, final boolean globalHubMode) { + private static void init( + final @NotNull SentryOptions options, + final @NotNull SentryRuntime.Builder runtimeBuilder, + final boolean globalHubMode) { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { if (!options.getClass().getName().equals("io.sentry.android.core.SentryAndroidOptions") && Platform.isAndroid()) { @@ -356,11 +383,10 @@ private static void init(final @NotNull SentryOptions options, final boolean glo globalScope.bindClient(new SentryClient(options)); - // If the executorService passed in the init is the same that was previously closed, we have - // to set a new one - if (options.getExecutorService().isClosed()) { - options.setExecutorService(new SentryExecutorService(options)); - } + // Also replaces an executor that a previous init closed, when the options are reused. + final @NotNull SentryRuntime runtime = + LegacyOptionsBridge.buildRuntime(runtimeBuilder, options); + SentryRuntime.setCurrent(runtime); if (options.getTimerExecutorService().isClosed()) { options.setTimerExecutorService( @@ -387,7 +413,12 @@ private static void init(final @NotNull SentryOptions options, final boolean glo // Registering integrations here make sure that Scopes is already created. for (final Integration integration : options.getIntegrations()) { try { - integration.register(ScopesAdapter.getInstance(), options); + if (integration instanceof InternalIntegration) { + ((InternalIntegration) integration) + .register(ScopesAdapter.getInstance(), options, runtime); + } else { + integration.register(ScopesAdapter.getInstance(), options); + } } catch (Throwable t) { options .getLogger() @@ -741,6 +772,7 @@ public static void close() { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { final IScopes scopes = getCurrentScopes(); rootScopes = NoOpScopes.getInstance(); + SentryRuntime.setCurrent(null); // remove thread local to avoid memory leak getScopesStorage().close(); scopes.close(false); diff --git a/sentry/src/main/java/io/sentry/internal/InternalIntegration.java b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java new file mode 100644 index 00000000000..34fb2180426 --- /dev/null +++ b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java @@ -0,0 +1,19 @@ +package io.sentry.internal; + +import io.sentry.IScopes; +import io.sentry.Integration; +import io.sentry.SentryOptions; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * An SDK integration that needs the SDK's own collaborators. Customers create integrations before + * {@code Sentry.init}, so they can't be constructor-injected; {@code Sentry.init} hands them over + * here instead. + */ +@ApiStatus.Internal +public interface InternalIntegration extends Integration { + + void register( + @NotNull IScopes scopes, @NotNull SentryOptions options, @NotNull SentryRuntime runtime); +} diff --git a/sentry/src/main/java/io/sentry/internal/LazySerializer.java b/sentry/src/main/java/io/sentry/internal/LazySerializer.java new file mode 100644 index 00000000000..4cb875c06ca --- /dev/null +++ b/sentry/src/main/java/io/sentry/internal/LazySerializer.java @@ -0,0 +1,63 @@ +package io.sentry.internal; + +import io.sentry.ISerializer; +import io.sentry.JsonDeserializer; +import io.sentry.SentryEnvelope; +import io.sentry.util.LazyEvaluator; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.util.Map; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Creates the real serializer, which is expensive, on first use, so components can take an {@link + * ISerializer} in their constructor without creating it during init. + */ +final class LazySerializer implements ISerializer { + + private final @NotNull LazyEvaluator serializer; + + LazySerializer(final @NotNull LazyEvaluator.Evaluator serializer) { + this.serializer = new LazyEvaluator<>(serializer); + } + + @Override + public @Nullable T deserializeCollection( + final @NotNull Reader reader, + final @NotNull Class clazz, + final @Nullable JsonDeserializer elementDeserializer) { + return serializer.getValue().deserializeCollection(reader, clazz, elementDeserializer); + } + + @Override + public @Nullable T deserialize(final @NotNull Reader reader, final @NotNull Class clazz) { + return serializer.getValue().deserialize(reader, clazz); + } + + @Override + public @Nullable SentryEnvelope deserializeEnvelope(final @NotNull InputStream inputStream) { + return serializer.getValue().deserializeEnvelope(inputStream); + } + + @Override + public void serialize(final @NotNull T entity, final @NotNull Writer writer) + throws IOException { + serializer.getValue().serialize(entity, writer); + } + + @Override + public void serialize( + final @NotNull SentryEnvelope envelope, final @NotNull OutputStream outputStream) + throws Exception { + serializer.getValue().serialize(envelope, outputStream); + } + + @Override + public @NotNull String serialize(final @NotNull Map data) throws Exception { + return serializer.getValue().serialize(data); + } +} diff --git a/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java new file mode 100644 index 00000000000..bcfd94974e1 --- /dev/null +++ b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java @@ -0,0 +1,79 @@ +package io.sentry.internal; + +import io.sentry.ISentryExecutorService; +import io.sentry.NoOpSentryExecutorService; +import io.sentry.SentryExecutorService; +import io.sentry.SentryOptions; +import io.sentry.cache.IEnvelopeCache; +import io.sentry.transport.NoOpEnvelopeCache; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * Keeps the collaborator accessors on {@link SentryOptions} working until 9.0 removes them. Once + * the SDK reads its collaborators from {@link SentryRuntime}, this is the only SDK code that may + * call them; delete it together with them. + */ +@ApiStatus.Internal +public final class LegacyOptionsBridge { + + private LegacyOptionsBridge() {} + + /** + * Builds the runtime, letting a collaborator set through a {@link SentryOptions} setter win over + * the platform's, as it did before the runtime existed. Then copies the result back so code that + * still reads the getters sees the same instances. + */ + public static @NotNull SentryRuntime buildRuntime( + final @NotNull SentryRuntime.Builder builder, final @NotNull SentryOptions options) { + // Shares the options' lazy serializer, so the expensive JsonSerializer is created only once. + builder.lazySerializer(() -> options.getSerializer()); + + if (!builder.hasExecutorService()) { + builder.executorService(executorOrNew(options)); + } + + final @NotNull IEnvelopeCache envelopeDiskCache = options.getEnvelopeDiskCache(); + if (!(envelopeDiskCache instanceof NoOpEnvelopeCache)) { + builder.envelopeDiskCache(envelopeDiskCache); + } + + final @NotNull SentryRuntime runtime = builder.build(options); + options.setExecutorService(runtime.getExecutorService()); + options.setEnvelopeDiskCache(runtime.getEnvelopeDiskCache()); + return runtime; + } + + /** + * The executor a platform's init should hand out: the one already on {@code options}, unless + * there is none or a previous init that reused this options object closed it. + */ + public static @NotNull ISentryExecutorService executorOrNew( + final @NotNull SentryOptions options) { + final @NotNull ISentryExecutorService executorService = options.getExecutorService(); + if (executorService instanceof NoOpSentryExecutorService || executorService.isClosed()) { + return new SentryExecutorService(options); + } + return executorService; + } + + /** + * The executor for code that can't have it injected on 8.x, such as a public constructor that + * only takes {@link SentryOptions}. Each caller needs a replacement in 9.0. + */ + public static @NotNull ISentryExecutorService executorOf(final @NotNull SentryOptions options) { + return options.getExecutorService(); + } + + /** + * A runtime over the collaborators already on {@code options}, for an {@link InternalIntegration} + * registered without one. It creates nothing. + */ + public static @NotNull SentryRuntime runtimeOf(final @NotNull SentryOptions options) { + return SentryRuntime.builder() + .lazySerializer(() -> options.getSerializer()) + .executorService(options.getExecutorService()) + .envelopeDiskCache(options.getEnvelopeDiskCache()) + .build(options); + } +} diff --git a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java new file mode 100644 index 00000000000..9a9dc673012 --- /dev/null +++ b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java @@ -0,0 +1,134 @@ +package io.sentry.internal; + +import io.sentry.ISentryExecutorService; +import io.sentry.ISerializer; +import io.sentry.JsonSerializer; +import io.sentry.SentryExecutorService; +import io.sentry.SentryOptions; +import io.sentry.cache.EnvelopeCache; +import io.sentry.cache.IEnvelopeCache; +import io.sentry.transport.NoOpEnvelopeCache; +import io.sentry.util.LazyEvaluator; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; + +/** + * The SDK's own collaborators, built once per {@code Sentry.init}. Customers configure {@link + * SentryOptions}; the SDK owns this. + * + *

Components take the collaborators they use as constructor parameters instead of this whole + * object. Only the places that wire components together read from it. + */ +@ApiStatus.Internal +public final class SentryRuntime { + + private static volatile @Nullable SentryRuntime current; + + private final @Nullable ISerializer serializer; + private final @Nullable ISentryExecutorService executorService; + private final @Nullable IEnvelopeCache envelopeDiskCache; + + private SentryRuntime(final @NotNull Builder builder) { + this.serializer = builder.serializer; + this.executorService = builder.executorService; + this.envelopeDiskCache = builder.envelopeDiskCache; + } + + public static @NotNull Builder builder() { + return new Builder(); + } + + /** + * The runtime of the current {@code Sentry.init}, for hybrid SDKs that call in from outside and + * have no other way to reach it. SDK code gets its collaborators passed in instead. + */ + public static @Nullable SentryRuntime getCurrent() { + return current; + } + + public static void setCurrent(final @Nullable SentryRuntime runtime) { + current = runtime; + } + + public @NotNull ISerializer getSerializer() { + return require(serializer, "serializer"); + } + + public @NotNull ISentryExecutorService getExecutorService() { + return require(executorService, "executorService"); + } + + public @NotNull IEnvelopeCache getEnvelopeDiskCache() { + return require(envelopeDiskCache, "envelopeDiskCache"); + } + + private static @NotNull T require(final @Nullable T value, final @NotNull String name) { + if (value == null) { + // Only a runtime from buildForTest() has unset collaborators. + throw new IllegalStateException(name + " was not set on this test's SentryRuntime"); + } + return value; + } + + public static final class Builder { + private @Nullable ISerializer serializer; + private @Nullable ISentryExecutorService executorService; + private @Nullable IEnvelopeCache envelopeDiskCache; + + private Builder() {} + + public @NotNull Builder serializer(final @NotNull ISerializer serializer) { + this.serializer = serializer; + return this; + } + + /** Defers creating the serializer, which is expensive, until something first uses it. */ + public @NotNull Builder lazySerializer( + final @NotNull LazyEvaluator.Evaluator serializer) { + this.serializer = new LazySerializer(serializer); + return this; + } + + public @NotNull Builder executorService(final @NotNull ISentryExecutorService executorService) { + this.executorService = executorService; + return this; + } + + public @NotNull Builder envelopeDiskCache(final @NotNull IEnvelopeCache envelopeDiskCache) { + this.envelopeDiskCache = envelopeDiskCache; + return this; + } + + public boolean hasExecutorService() { + return executorService != null; + } + + /** Creates the default for every collaborator that wasn't set. */ + public @NotNull SentryRuntime build(final @NotNull SentryOptions options) { + if (serializer == null) { + lazySerializer(() -> new JsonSerializer(options)); + } + if (executorService == null) { + executorService = new SentryExecutorService(options); + } + if (envelopeDiskCache == null) { + envelopeDiskCache = + options.getCacheDirPath() != null + ? EnvelopeCache.create(options) + : NoOpEnvelopeCache.getInstance(); + } + return new SentryRuntime(this); + } + + /** + * Leaves unset collaborators unset, so a test that reads one it didn't set fails instead of + * silently getting a default. + */ + @TestOnly + public @NotNull SentryRuntime buildForTest() { + return new SentryRuntime(this); + } + } +} diff --git a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt new file mode 100644 index 00000000000..aba6b02a6ad --- /dev/null +++ b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt @@ -0,0 +1,118 @@ +package io.sentry.internal + +import com.google.common.truth.Truth.assertThat +import io.sentry.ISentryExecutorService +import io.sentry.ISerializer +import io.sentry.SentryOptions +import io.sentry.cache.EnvelopeCache +import io.sentry.cache.IEnvelopeCache +import io.sentry.transport.NoOpEnvelopeCache +import java.nio.file.Files +import kotlin.test.Test +import kotlin.test.assertFailsWith +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever + +class SentryRuntimeTest { + + @Test + fun `build creates a disk cache when a cache dir is set`() { + val options = + SentryOptions().apply { cacheDirPath = Files.createTempDirectory("sentry").toString() } + + val runtime = SentryRuntime.builder().build(options) + + assertThat(runtime.envelopeDiskCache).isInstanceOf(EnvelopeCache::class.java) + } + + @Test + fun `build uses a no-op disk cache without a cache dir`() { + val runtime = SentryRuntime.builder().build(SentryOptions()) + + assertThat(runtime.envelopeDiskCache).isSameInstanceAs(NoOpEnvelopeCache.getInstance()) + } + + @Test + fun `the serializer is created on first use, not when it's read`() { + var created = 0 + val runtime = + SentryRuntime.builder() + .lazySerializer { + created++ + mock() + } + .buildForTest() + + val serializer = runtime.serializer + assertThat(created).isEqualTo(0) + + serializer.serialize(mapOf("a" to 1)) + serializer.serialize(mapOf("b" to 2)) + assertThat(created).isEqualTo(1) + } + + @Test + fun `a test runtime fails on a collaborator the test didn't set`() { + val runtime = SentryRuntime.builder().envelopeDiskCache(mock()).buildForTest() + + val error = assertFailsWith { runtime.executorService } + assertThat(error).hasMessageThat().contains("executorService") + } + + @Test + fun `a cache set through the options setter wins over the platform's`() { + val customerCache = mock() + val options = SentryOptions().apply { setEnvelopeDiskCache(customerCache) } + + val runtime = + LegacyOptionsBridge.buildRuntime( + SentryRuntime.builder().envelopeDiskCache(mock()), + options, + ) + + assertThat(runtime.envelopeDiskCache).isSameInstanceAs(customerCache) + } + + @Test + fun `the options getters see the runtime's collaborators`() { + val platformCache = mock() + val options = SentryOptions() + + val runtime = + LegacyOptionsBridge.buildRuntime( + SentryRuntime.builder().envelopeDiskCache(platformCache), + options, + ) + + assertThat(options.envelopeDiskCache).isSameInstanceAs(platformCache) + assertThat(options.executorService).isSameInstanceAs(runtime.executorService) + } + + @Test + fun `a closed executor from a previous init is replaced`() { + val closed = mock() + whenever(closed.isClosed).thenReturn(true) + val options = SentryOptions().apply { executorService = closed } + + val runtime = LegacyOptionsBridge.buildRuntime(SentryRuntime.builder(), options) + + assertThat(runtime.executorService).isNotSameInstanceAs(closed) + runtime.executorService.close(0) + } + + @Test + fun `runtimeOf reuses the collaborators on the options`() { + val executor = mock() + val cache = mock() + val options = + SentryOptions().apply { + executorService = executor + setEnvelopeDiskCache(cache) + } + + val runtime = LegacyOptionsBridge.runtimeOf(options) + + assertThat(runtime.executorService).isSameInstanceAs(executor) + assertThat(runtime.envelopeDiskCache).isSameInstanceAs(cache) + } +} From e2d770e3f0a11162160d134cac2719d34473793a Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 3 Dec 2026 13:51:00 +0100 Subject: [PATCH 2/4] ref(core): Call SentryRuntime's contents internal services "Collaborators" read as jargon. "Internal services" says what they are: the objects the SDK creates and manages for itself, as opposed to what the customer configures on SentryOptions. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../sentry/android/core/InternalSentrySdk.java | 6 +++--- sentry/src/main/java/io/sentry/Sentry.java | 4 ++-- .../io/sentry/internal/InternalIntegration.java | 2 +- .../io/sentry/internal/LegacyOptionsBridge.java | 16 ++++++++-------- .../java/io/sentry/internal/SentryRuntime.java | 12 ++++++------ .../java/io/sentry/internal/SentryRuntimeTest.kt | 6 +++--- 6 files changed, 23 insertions(+), 23 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 bd8f4d791c4..24534b953e2 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 @@ -52,14 +52,14 @@ @ApiStatus.Internal public final class InternalSentrySdk { - /** Configures both the options and the SDK's own collaborators. */ + /** Configures both the options and the SDK's internal services. */ public interface OptionsAndRuntimeConfiguration { void configure(@NotNull SentryAndroidOptions options, @NotNull SentryRuntime.Builder runtime); } /** * Like {@link SentryAndroid#init(Context, ILogger, Sentry.OptionsConfiguration)}, but also lets - * the hybrid SDK set the SDK's own collaborators. + * the hybrid SDK set the SDK's internal services. */ public static void init( final @NotNull Context context, @@ -68,7 +68,7 @@ public static void init( SentryAndroid.init(context, logger, configuration); } - /** The SDK's own collaborators, or {@code null} before init and after close. */ + /** The SDK's internal services, or {@code null} before init and after close. */ public static @Nullable SentryRuntime getRuntime() { return SentryRuntime.getCurrent(); } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index aa8fdff4eb3..e6f78437a12 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -253,8 +253,8 @@ public static void init( } /** - * Initializes the SDK, letting the platform's init add its own collaborators to {@code runtime} - * while it configures the options. + * Initializes the SDK, letting the platform's init add its own internal services to {@code + * runtime} while it configures the options. */ @ApiStatus.Internal public static void init( diff --git a/sentry/src/main/java/io/sentry/internal/InternalIntegration.java b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java index 34fb2180426..e55639b76d9 100644 --- a/sentry/src/main/java/io/sentry/internal/InternalIntegration.java +++ b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java @@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull; /** - * An SDK integration that needs the SDK's own collaborators. Customers create integrations before + * An SDK integration that needs the SDK's internal services. Customers create integrations before * {@code Sentry.init}, so they can't be constructor-injected; {@code Sentry.init} hands them over * here instead. */ diff --git a/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java index bcfd94974e1..0d84c844619 100644 --- a/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java +++ b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java @@ -10,9 +10,9 @@ import org.jetbrains.annotations.NotNull; /** - * Keeps the collaborator accessors on {@link SentryOptions} working until 9.0 removes them. Once - * the SDK reads its collaborators from {@link SentryRuntime}, this is the only SDK code that may - * call them; delete it together with them. + * Keeps the internal-service accessors on {@link SentryOptions} working until 9.0 removes them. + * Once the SDK reads its internal services from {@link SentryRuntime}, this is the only SDK code + * that may call them; delete it together with them. */ @ApiStatus.Internal public final class LegacyOptionsBridge { @@ -20,9 +20,9 @@ public final class LegacyOptionsBridge { private LegacyOptionsBridge() {} /** - * Builds the runtime, letting a collaborator set through a {@link SentryOptions} setter win over - * the platform's, as it did before the runtime existed. Then copies the result back so code that - * still reads the getters sees the same instances. + * Builds the runtime, letting an internal service set through a {@link SentryOptions} setter win + * over the platform's, as it did before the runtime existed. Then copies the result back so code + * that still reads the getters sees the same instances. */ public static @NotNull SentryRuntime buildRuntime( final @NotNull SentryRuntime.Builder builder, final @NotNull SentryOptions options) { @@ -66,8 +66,8 @@ private LegacyOptionsBridge() {} } /** - * A runtime over the collaborators already on {@code options}, for an {@link InternalIntegration} - * registered without one. It creates nothing. + * A runtime over the internal services already on {@code options}, for an {@link + * InternalIntegration} registered without one. It creates nothing. */ public static @NotNull SentryRuntime runtimeOf(final @NotNull SentryOptions options) { return SentryRuntime.builder() diff --git a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java index 9a9dc673012..ec4aaecf15f 100644 --- a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java +++ b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java @@ -15,10 +15,10 @@ import org.jetbrains.annotations.TestOnly; /** - * The SDK's own collaborators, built once per {@code Sentry.init}. Customers configure {@link + * The SDK's internal services, built once per {@code Sentry.init}. Customers configure {@link * SentryOptions}; the SDK owns this. * - *

Components take the collaborators they use as constructor parameters instead of this whole + *

Components take the internal services they use as constructor parameters instead of this whole * object. Only the places that wire components together read from it. */ @ApiStatus.Internal @@ -42,7 +42,7 @@ private SentryRuntime(final @NotNull Builder builder) { /** * The runtime of the current {@code Sentry.init}, for hybrid SDKs that call in from outside and - * have no other way to reach it. SDK code gets its collaborators passed in instead. + * have no other way to reach it. SDK code gets its internal services passed in instead. */ public static @Nullable SentryRuntime getCurrent() { return current; @@ -66,7 +66,7 @@ public static void setCurrent(final @Nullable SentryRuntime runtime) { private static @NotNull T require(final @Nullable T value, final @NotNull String name) { if (value == null) { - // Only a runtime from buildForTest() has unset collaborators. + // Only a runtime from buildForTest() has unset internal services. throw new IllegalStateException(name + " was not set on this test's SentryRuntime"); } return value; @@ -105,7 +105,7 @@ public boolean hasExecutorService() { return executorService != null; } - /** Creates the default for every collaborator that wasn't set. */ + /** Creates the default for every internal service that wasn't set. */ public @NotNull SentryRuntime build(final @NotNull SentryOptions options) { if (serializer == null) { lazySerializer(() -> new JsonSerializer(options)); @@ -123,7 +123,7 @@ public boolean hasExecutorService() { } /** - * Leaves unset collaborators unset, so a test that reads one it didn't set fails instead of + * Leaves unset internal services unset, so a test that reads one it didn't set fails instead of * silently getting a default. */ @TestOnly diff --git a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt index aba6b02a6ad..f10db9d0c13 100644 --- a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt +++ b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt @@ -52,7 +52,7 @@ class SentryRuntimeTest { } @Test - fun `a test runtime fails on a collaborator the test didn't set`() { + fun `a test runtime fails on an internal service the test didn't set`() { val runtime = SentryRuntime.builder().envelopeDiskCache(mock()).buildForTest() val error = assertFailsWith { runtime.executorService } @@ -74,7 +74,7 @@ class SentryRuntimeTest { } @Test - fun `the options getters see the runtime's collaborators`() { + fun `the options getters see the runtime's internal services`() { val platformCache = mock() val options = SentryOptions() @@ -101,7 +101,7 @@ class SentryRuntimeTest { } @Test - fun `runtimeOf reuses the collaborators on the options`() { + fun `runtimeOf reuses the internal services on the options`() { val executor = mock() val cache = mock() val options = From 6ef675812efe6b193b9e96d709696c339a5f13a1 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 3 Dec 2026 15:43:29 +0100 Subject: [PATCH 3/4] ref(core): Rename SentryRuntime to InternalServices io.sentry.protocol.SentryRuntime already exists as the event's runtime context, so the API listed two public SentryRuntime classes. InternalServices says what the class holds, in the words its docs already use. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../api/sentry-android-core.api | 8 +-- .../android/core/InternalSentrySdk.java | 13 ++--- .../io/sentry/android/core/SentryAndroid.java | 12 ++--- sentry/api/sentry.api | 46 ++++++++-------- sentry/src/main/java/io/sentry/Sentry.java | 24 ++++----- .../sentry/internal/InternalIntegration.java | 2 +- ...ntryRuntime.java => InternalServices.java} | 29 +++++----- .../sentry/internal/LegacyOptionsBridge.java | 28 +++++----- ...RuntimeTest.kt => InternalServicesTest.kt} | 54 +++++++++---------- 9 files changed, 109 insertions(+), 107 deletions(-) rename sentry/src/main/java/io/sentry/internal/{SentryRuntime.java => InternalServices.java} (81%) rename sentry/src/test/java/io/sentry/internal/{SentryRuntimeTest.kt => InternalServicesTest.kt} (56%) diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 9a2c6f6d931..5a92e669d40 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -330,15 +330,15 @@ public final class io/sentry/android/core/InternalSentrySdk { public static fun captureEnvelopeNonTerminating ([B)Lio/sentry/protocol/SentryId; public static fun getAppStartMeasurement ()Ljava/util/Map; public static fun getCurrentScope ()Lio/sentry/IScope; - public static fun getRuntime ()Lio/sentry/internal/SentryRuntime; - public static fun init (Landroid/content/Context;Lio/sentry/ILogger;Lio/sentry/android/core/InternalSentrySdk$OptionsAndRuntimeConfiguration;)V + public static fun getInternalServices ()Lio/sentry/internal/InternalServices; + public static fun init (Landroid/content/Context;Lio/sentry/ILogger;Lio/sentry/android/core/InternalSentrySdk$OptionsAndServicesConfiguration;)V public static fun serializeScope (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/IScope;)Ljava/util/Map; public static fun setTrace (Ljava/lang/String;Ljava/lang/String;Ljava/lang/Double;Ljava/lang/Double;)V public static fun updateSessionForDroppedEventNonTerminating (Z)V } -public abstract interface class io/sentry/android/core/InternalSentrySdk$OptionsAndRuntimeConfiguration { - public abstract fun configure (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/internal/SentryRuntime$Builder;)V +public abstract interface class io/sentry/android/core/InternalSentrySdk$OptionsAndServicesConfiguration { + public abstract fun configure (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/internal/InternalServices$Builder;)V } public final class io/sentry/android/core/LoadClass : io/sentry/util/LoadClass { 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 24534b953e2..461bce72151 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 @@ -27,7 +27,7 @@ import io.sentry.android.core.performance.AppStartMetrics; import io.sentry.android.core.performance.TimeSpan; import io.sentry.cache.EnvelopeCache; -import io.sentry.internal.SentryRuntime; +import io.sentry.internal.InternalServices; import io.sentry.protocol.App; import io.sentry.protocol.Device; import io.sentry.protocol.SentryId; @@ -53,8 +53,9 @@ public final class InternalSentrySdk { /** Configures both the options and the SDK's internal services. */ - public interface OptionsAndRuntimeConfiguration { - void configure(@NotNull SentryAndroidOptions options, @NotNull SentryRuntime.Builder runtime); + public interface OptionsAndServicesConfiguration { + void configure( + @NotNull SentryAndroidOptions options, @NotNull InternalServices.Builder services); } /** @@ -64,13 +65,13 @@ public interface OptionsAndRuntimeConfiguration { public static void init( final @NotNull Context context, final @NotNull ILogger logger, - final @NotNull OptionsAndRuntimeConfiguration configuration) { + final @NotNull OptionsAndServicesConfiguration configuration) { SentryAndroid.init(context, logger, configuration); } /** The SDK's internal services, or {@code null} before init and after close. */ - public static @Nullable SentryRuntime getRuntime() { - return SentryRuntime.getCurrent(); + public static @Nullable InternalServices getInternalServices() { + return InternalServices.getCurrent(); } /** 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 c8d9039a452..fe9fc7fea83 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 @@ -20,7 +20,7 @@ import io.sentry.android.core.performance.TimeSpan; import io.sentry.android.fragment.FragmentLifecycleIntegration; import io.sentry.android.timber.SentryTimberIntegration; -import io.sentry.internal.SentryRuntime; +import io.sentry.internal.InternalServices; import io.sentry.time.Stopwatch; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.FileUtils; @@ -108,14 +108,14 @@ public static void init( @NotNull final Context context, @NotNull ILogger logger, @NotNull Sentry.OptionsConfiguration configuration) { - init(context, logger, (options, runtime) -> configuration.configure(options)); + init(context, logger, (options, services) -> configuration.configure(options)); } @SuppressLint("NewApi") static void init( @NotNull final Context context, @NotNull ILogger logger, - @NotNull InternalSentrySdk.OptionsAndRuntimeConfiguration configuration) { + @NotNull InternalSentrySdk.OptionsAndServicesConfiguration configuration) { if (isNdkCrashServiceProcess()) { logger.log( SentryLevel.DEBUG, "Skipping SentryAndroid.init in the NDK crash service process."); @@ -126,7 +126,7 @@ static void init( // even if acquire() throws. Trace.beginSection("SentryAndroid.init"); try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) { - final @NotNull SentryRuntime.Builder runtime = SentryRuntime.builder(); + final @NotNull InternalServices.Builder services = InternalServices.builder(); Sentry.init( new SentryAndroidOptionsContainer(), options -> { @@ -170,7 +170,7 @@ static void init( isDistributionAvailable); try { - configuration.configure(options, runtime); + configuration.configure(options, services); } catch (Throwable t) { // let it slip, but log it options @@ -211,7 +211,7 @@ static void init( deduplicateIntegrations(options, isFragmentAvailable, isTimberAvailable); }, - runtime, + services, true); final @NotNull IScopes scopes = Sentry.getCurrentScopes(); diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 4a18b58dde2..dc5c3c2a1c9 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2938,7 +2938,7 @@ public final class io/sentry/Sentry { public static fun getTraceparent ()Lio/sentry/SentryTraceHeader; public static fun init ()V public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;)V - public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;Lio/sentry/internal/SentryRuntime$Builder;Z)V + public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;Lio/sentry/internal/InternalServices$Builder;Z)V public static fun init (Lio/sentry/OptionsContainer;Lio/sentry/Sentry$OptionsConfiguration;Z)V public static fun init (Lio/sentry/Sentry$OptionsConfiguration;)V public static fun init (Lio/sentry/Sentry$OptionsConfiguration;Z)V @@ -5448,14 +5448,33 @@ public final class io/sentry/instrumentation/file/SentryFileWriter : java/io/Out } public abstract interface class io/sentry/internal/InternalIntegration : io/sentry/Integration { - public abstract fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V + public abstract fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/InternalServices;)V +} + +public final class io/sentry/internal/InternalServices { + public static fun builder ()Lio/sentry/internal/InternalServices$Builder; + public static fun getCurrent ()Lio/sentry/internal/InternalServices; + public fun getEnvelopeDiskCache ()Lio/sentry/cache/IEnvelopeCache; + public fun getExecutorService ()Lio/sentry/ISentryExecutorService; + public fun getSerializer ()Lio/sentry/ISerializer; + public static fun setCurrent (Lio/sentry/internal/InternalServices;)V +} + +public final class io/sentry/internal/InternalServices$Builder { + public fun build (Lio/sentry/SentryOptions;)Lio/sentry/internal/InternalServices; + public fun buildForTest ()Lio/sentry/internal/InternalServices; + public fun envelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)Lio/sentry/internal/InternalServices$Builder; + public fun executorService (Lio/sentry/ISentryExecutorService;)Lio/sentry/internal/InternalServices$Builder; + public fun hasExecutorService ()Z + public fun lazySerializer (Lio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/internal/InternalServices$Builder; + public fun serializer (Lio/sentry/ISerializer;)Lio/sentry/internal/InternalServices$Builder; } public final class io/sentry/internal/LegacyOptionsBridge { - public static fun buildRuntime (Lio/sentry/internal/SentryRuntime$Builder;Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; + public static fun buildServices (Lio/sentry/internal/InternalServices$Builder;Lio/sentry/SentryOptions;)Lio/sentry/internal/InternalServices; public static fun executorOf (Lio/sentry/SentryOptions;)Lio/sentry/ISentryExecutorService; public static fun executorOrNew (Lio/sentry/SentryOptions;)Lio/sentry/ISentryExecutorService; - public static fun runtimeOf (Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; + public static fun servicesOf (Lio/sentry/SentryOptions;)Lio/sentry/internal/InternalServices; } public final class io/sentry/internal/ManifestVersionReader { @@ -5470,25 +5489,6 @@ public final class io/sentry/internal/ManifestVersionReader$VersionInfoHolder { public fun getSdkVersion ()Ljava/lang/String; } -public final class io/sentry/internal/SentryRuntime { - public static fun builder ()Lio/sentry/internal/SentryRuntime$Builder; - public static fun getCurrent ()Lio/sentry/internal/SentryRuntime; - public fun getEnvelopeDiskCache ()Lio/sentry/cache/IEnvelopeCache; - public fun getExecutorService ()Lio/sentry/ISentryExecutorService; - public fun getSerializer ()Lio/sentry/ISerializer; - public static fun setCurrent (Lio/sentry/internal/SentryRuntime;)V -} - -public final class io/sentry/internal/SentryRuntime$Builder { - public fun build (Lio/sentry/SentryOptions;)Lio/sentry/internal/SentryRuntime; - public fun buildForTest ()Lio/sentry/internal/SentryRuntime; - public fun envelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)Lio/sentry/internal/SentryRuntime$Builder; - public fun executorService (Lio/sentry/ISentryExecutorService;)Lio/sentry/internal/SentryRuntime$Builder; - public fun hasExecutorService ()Z - public fun lazySerializer (Lio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/internal/SentryRuntime$Builder; - public fun serializer (Lio/sentry/ISerializer;)Lio/sentry/internal/SentryRuntime$Builder; -} - public abstract interface class io/sentry/internal/debugmeta/IDebugMetaLoader { public abstract fun loadDebugMeta ()Ljava/util/List; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index e6f78437a12..c43f339b122 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -7,8 +7,8 @@ import io.sentry.cache.PersistingScopeObserver; import io.sentry.config.PropertiesProviderFactory; import io.sentry.internal.InternalIntegration; +import io.sentry.internal.InternalServices; import io.sentry.internal.LegacyOptionsBridge; -import io.sentry.internal.SentryRuntime; import io.sentry.internal.debugmeta.NoOpDebugMetaLoader; import io.sentry.internal.debugmeta.ResourcesDebugMetaLoader; import io.sentry.internal.modules.CompositeModulesLoader; @@ -249,18 +249,18 @@ public static void init( InstantiationException, NoSuchMethodException, InvocationTargetException { - init(clazz, optionsConfiguration, SentryRuntime.builder(), globalHubMode); + init(clazz, optionsConfiguration, InternalServices.builder(), globalHubMode); } /** * Initializes the SDK, letting the platform's init add its own internal services to {@code - * runtime} while it configures the options. + * services} while it configures the options. */ @ApiStatus.Internal public static void init( final @NotNull OptionsContainer clazz, final @NotNull OptionsConfiguration optionsConfiguration, - final @NotNull SentryRuntime.Builder runtime, + final @NotNull InternalServices.Builder services, final boolean globalHubMode) throws IllegalAccessException, InstantiationException, @@ -268,7 +268,7 @@ public static void init( InvocationTargetException { final T options = clazz.createInstance(); applyOptionsConfiguration(optionsConfiguration, options); - init(options, runtime, globalHubMode); + init(options, services, globalHubMode); } /** @@ -322,7 +322,7 @@ public static void init(final @NotNull SentryOptions options) { * @param globalHubMode the globalHubMode */ private static void init(final @NotNull SentryOptions options, final boolean globalHubMode) { - init(options, SentryRuntime.builder(), globalHubMode); + init(options, InternalServices.builder(), globalHubMode); } @SuppressWarnings({ @@ -332,7 +332,7 @@ private static void init(final @NotNull SentryOptions options, final boolean glo }) // older AGP versions do not support method references private static void init( final @NotNull SentryOptions options, - final @NotNull SentryRuntime.Builder runtimeBuilder, + final @NotNull InternalServices.Builder servicesBuilder, final boolean globalHubMode) { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { if (!options.getClass().getName().equals("io.sentry.android.core.SentryAndroidOptions") @@ -384,9 +384,9 @@ private static void init( globalScope.bindClient(new SentryClient(options)); // Also replaces an executor that a previous init closed, when the options are reused. - final @NotNull SentryRuntime runtime = - LegacyOptionsBridge.buildRuntime(runtimeBuilder, options); - SentryRuntime.setCurrent(runtime); + final @NotNull InternalServices services = + LegacyOptionsBridge.buildServices(servicesBuilder, options); + InternalServices.setCurrent(services); if (options.getTimerExecutorService().isClosed()) { options.setTimerExecutorService( @@ -415,7 +415,7 @@ private static void init( try { if (integration instanceof InternalIntegration) { ((InternalIntegration) integration) - .register(ScopesAdapter.getInstance(), options, runtime); + .register(ScopesAdapter.getInstance(), options, services); } else { integration.register(ScopesAdapter.getInstance(), options); } @@ -772,7 +772,7 @@ public static void close() { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { final IScopes scopes = getCurrentScopes(); rootScopes = NoOpScopes.getInstance(); - SentryRuntime.setCurrent(null); + InternalServices.setCurrent(null); // remove thread local to avoid memory leak getScopesStorage().close(); scopes.close(false); diff --git a/sentry/src/main/java/io/sentry/internal/InternalIntegration.java b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java index e55639b76d9..5e984998bbe 100644 --- a/sentry/src/main/java/io/sentry/internal/InternalIntegration.java +++ b/sentry/src/main/java/io/sentry/internal/InternalIntegration.java @@ -15,5 +15,5 @@ public interface InternalIntegration extends Integration { void register( - @NotNull IScopes scopes, @NotNull SentryOptions options, @NotNull SentryRuntime runtime); + @NotNull IScopes scopes, @NotNull SentryOptions options, @NotNull InternalServices services); } diff --git a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java b/sentry/src/main/java/io/sentry/internal/InternalServices.java similarity index 81% rename from sentry/src/main/java/io/sentry/internal/SentryRuntime.java rename to sentry/src/main/java/io/sentry/internal/InternalServices.java index ec4aaecf15f..13f0251675e 100644 --- a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java +++ b/sentry/src/main/java/io/sentry/internal/InternalServices.java @@ -22,15 +22,15 @@ * object. Only the places that wire components together read from it. */ @ApiStatus.Internal -public final class SentryRuntime { +public final class InternalServices { - private static volatile @Nullable SentryRuntime current; + private static volatile @Nullable InternalServices current; private final @Nullable ISerializer serializer; private final @Nullable ISentryExecutorService executorService; private final @Nullable IEnvelopeCache envelopeDiskCache; - private SentryRuntime(final @NotNull Builder builder) { + private InternalServices(final @NotNull Builder builder) { this.serializer = builder.serializer; this.executorService = builder.executorService; this.envelopeDiskCache = builder.envelopeDiskCache; @@ -41,15 +41,16 @@ private SentryRuntime(final @NotNull Builder builder) { } /** - * The runtime of the current {@code Sentry.init}, for hybrid SDKs that call in from outside and - * have no other way to reach it. SDK code gets its internal services passed in instead. + * The internal services of the current {@code Sentry.init}, for hybrid SDKs that call in from + * outside and have no other way to reach them. SDK code gets its internal services passed in + * instead. */ - public static @Nullable SentryRuntime getCurrent() { + public static @Nullable InternalServices getCurrent() { return current; } - public static void setCurrent(final @Nullable SentryRuntime runtime) { - current = runtime; + public static void setCurrent(final @Nullable InternalServices services) { + current = services; } public @NotNull ISerializer getSerializer() { @@ -66,8 +67,8 @@ public static void setCurrent(final @Nullable SentryRuntime runtime) { private static @NotNull T require(final @Nullable T value, final @NotNull String name) { if (value == null) { - // Only a runtime from buildForTest() has unset internal services. - throw new IllegalStateException(name + " was not set on this test's SentryRuntime"); + // Only buildForTest() leaves internal services unset. + throw new IllegalStateException(name + " was not set on this test's InternalServices"); } return value; } @@ -106,7 +107,7 @@ public boolean hasExecutorService() { } /** Creates the default for every internal service that wasn't set. */ - public @NotNull SentryRuntime build(final @NotNull SentryOptions options) { + public @NotNull InternalServices build(final @NotNull SentryOptions options) { if (serializer == null) { lazySerializer(() -> new JsonSerializer(options)); } @@ -119,7 +120,7 @@ public boolean hasExecutorService() { ? EnvelopeCache.create(options) : NoOpEnvelopeCache.getInstance(); } - return new SentryRuntime(this); + return new InternalServices(this); } /** @@ -127,8 +128,8 @@ public boolean hasExecutorService() { * silently getting a default. */ @TestOnly - public @NotNull SentryRuntime buildForTest() { - return new SentryRuntime(this); + public @NotNull InternalServices buildForTest() { + return new InternalServices(this); } } } diff --git a/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java index 0d84c844619..9160ea6eda4 100644 --- a/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java +++ b/sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java @@ -11,7 +11,7 @@ /** * Keeps the internal-service accessors on {@link SentryOptions} working until 9.0 removes them. - * Once the SDK reads its internal services from {@link SentryRuntime}, this is the only SDK code + * Once the SDK reads its internal services from {@link InternalServices}, this is the only SDK code * that may call them; delete it together with them. */ @ApiStatus.Internal @@ -20,12 +20,12 @@ public final class LegacyOptionsBridge { private LegacyOptionsBridge() {} /** - * Builds the runtime, letting an internal service set through a {@link SentryOptions} setter win - * over the platform's, as it did before the runtime existed. Then copies the result back so code - * that still reads the getters sees the same instances. + * Builds the internal services, letting an internal service set through a {@link SentryOptions} + * setter win over the platform's, as it did before InternalServices existed. Then copies the + * result back so code that still reads the getters sees the same instances. */ - public static @NotNull SentryRuntime buildRuntime( - final @NotNull SentryRuntime.Builder builder, final @NotNull SentryOptions options) { + public static @NotNull InternalServices buildServices( + final @NotNull InternalServices.Builder builder, final @NotNull SentryOptions options) { // Shares the options' lazy serializer, so the expensive JsonSerializer is created only once. builder.lazySerializer(() -> options.getSerializer()); @@ -38,10 +38,10 @@ private LegacyOptionsBridge() {} builder.envelopeDiskCache(envelopeDiskCache); } - final @NotNull SentryRuntime runtime = builder.build(options); - options.setExecutorService(runtime.getExecutorService()); - options.setEnvelopeDiskCache(runtime.getEnvelopeDiskCache()); - return runtime; + final @NotNull InternalServices services = builder.build(options); + options.setExecutorService(services.getExecutorService()); + options.setEnvelopeDiskCache(services.getEnvelopeDiskCache()); + return services; } /** @@ -66,11 +66,11 @@ private LegacyOptionsBridge() {} } /** - * A runtime over the internal services already on {@code options}, for an {@link - * InternalIntegration} registered without one. It creates nothing. + * The internal services already on {@code options}, for an {@link InternalIntegration} registered + * without them. It creates nothing. */ - public static @NotNull SentryRuntime runtimeOf(final @NotNull SentryOptions options) { - return SentryRuntime.builder() + public static @NotNull InternalServices servicesOf(final @NotNull SentryOptions options) { + return InternalServices.builder() .lazySerializer(() -> options.getSerializer()) .executorService(options.getExecutorService()) .envelopeDiskCache(options.getEnvelopeDiskCache()) diff --git a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt b/sentry/src/test/java/io/sentry/internal/InternalServicesTest.kt similarity index 56% rename from sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt rename to sentry/src/test/java/io/sentry/internal/InternalServicesTest.kt index f10db9d0c13..44a10348734 100644 --- a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt +++ b/sentry/src/test/java/io/sentry/internal/InternalServicesTest.kt @@ -13,37 +13,37 @@ import kotlin.test.assertFailsWith import org.mockito.kotlin.mock import org.mockito.kotlin.whenever -class SentryRuntimeTest { +class InternalServicesTest { @Test fun `build creates a disk cache when a cache dir is set`() { val options = SentryOptions().apply { cacheDirPath = Files.createTempDirectory("sentry").toString() } - val runtime = SentryRuntime.builder().build(options) + val services = InternalServices.builder().build(options) - assertThat(runtime.envelopeDiskCache).isInstanceOf(EnvelopeCache::class.java) + assertThat(services.envelopeDiskCache).isInstanceOf(EnvelopeCache::class.java) } @Test fun `build uses a no-op disk cache without a cache dir`() { - val runtime = SentryRuntime.builder().build(SentryOptions()) + val services = InternalServices.builder().build(SentryOptions()) - assertThat(runtime.envelopeDiskCache).isSameInstanceAs(NoOpEnvelopeCache.getInstance()) + assertThat(services.envelopeDiskCache).isSameInstanceAs(NoOpEnvelopeCache.getInstance()) } @Test fun `the serializer is created on first use, not when it's read`() { var created = 0 - val runtime = - SentryRuntime.builder() + val services = + InternalServices.builder() .lazySerializer { created++ mock() } .buildForTest() - val serializer = runtime.serializer + val serializer = services.serializer assertThat(created).isEqualTo(0) serializer.serialize(mapOf("a" to 1)) @@ -52,10 +52,10 @@ class SentryRuntimeTest { } @Test - fun `a test runtime fails on an internal service the test didn't set`() { - val runtime = SentryRuntime.builder().envelopeDiskCache(mock()).buildForTest() + fun `buildForTest fails on an internal service the test didn't set`() { + val services = InternalServices.builder().envelopeDiskCache(mock()).buildForTest() - val error = assertFailsWith { runtime.executorService } + val error = assertFailsWith { services.executorService } assertThat(error).hasMessageThat().contains("executorService") } @@ -64,28 +64,28 @@ class SentryRuntimeTest { val customerCache = mock() val options = SentryOptions().apply { setEnvelopeDiskCache(customerCache) } - val runtime = - LegacyOptionsBridge.buildRuntime( - SentryRuntime.builder().envelopeDiskCache(mock()), + val services = + LegacyOptionsBridge.buildServices( + InternalServices.builder().envelopeDiskCache(mock()), options, ) - assertThat(runtime.envelopeDiskCache).isSameInstanceAs(customerCache) + assertThat(services.envelopeDiskCache).isSameInstanceAs(customerCache) } @Test - fun `the options getters see the runtime's internal services`() { + fun `the options getters see the same internal services`() { val platformCache = mock() val options = SentryOptions() - val runtime = - LegacyOptionsBridge.buildRuntime( - SentryRuntime.builder().envelopeDiskCache(platformCache), + val services = + LegacyOptionsBridge.buildServices( + InternalServices.builder().envelopeDiskCache(platformCache), options, ) assertThat(options.envelopeDiskCache).isSameInstanceAs(platformCache) - assertThat(options.executorService).isSameInstanceAs(runtime.executorService) + assertThat(options.executorService).isSameInstanceAs(services.executorService) } @Test @@ -94,14 +94,14 @@ class SentryRuntimeTest { whenever(closed.isClosed).thenReturn(true) val options = SentryOptions().apply { executorService = closed } - val runtime = LegacyOptionsBridge.buildRuntime(SentryRuntime.builder(), options) + val services = LegacyOptionsBridge.buildServices(InternalServices.builder(), options) - assertThat(runtime.executorService).isNotSameInstanceAs(closed) - runtime.executorService.close(0) + assertThat(services.executorService).isNotSameInstanceAs(closed) + services.executorService.close(0) } @Test - fun `runtimeOf reuses the internal services on the options`() { + fun `servicesOf reuses the internal services on the options`() { val executor = mock() val cache = mock() val options = @@ -110,9 +110,9 @@ class SentryRuntimeTest { setEnvelopeDiskCache(cache) } - val runtime = LegacyOptionsBridge.runtimeOf(options) + val services = LegacyOptionsBridge.servicesOf(options) - assertThat(runtime.executorService).isSameInstanceAs(executor) - assertThat(runtime.envelopeDiskCache).isSameInstanceAs(cache) + assertThat(services.executorService).isSameInstanceAs(executor) + assertThat(services.envelopeDiskCache).isSameInstanceAs(cache) } } From 221fe034e8ad15137403dd94c197e3210c042be0 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 3 Dec 2026 16:03:03 +0100 Subject: [PATCH 4/4] ref(core): Keep the executor on the options for hybrid SDKs On Android the executor already exists, and integrations start using it, before the hybrid SDK's callback runs. An executor set on the InternalServices builder from that callback would replace it without closing it. Until 9.0, hybrid SDKs set their executor on SentryOptions, which the bridge already honors. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../main/java/io/sentry/android/core/InternalSentrySdk.java | 5 ++++- .../src/main/java/io/sentry/internal/InternalServices.java | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 461bce72151..2480888b535 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 @@ -52,7 +52,10 @@ @ApiStatus.Internal public final class InternalSentrySdk { - /** Configures both the options and the SDK's internal services. */ + /** + * Configures both the options and the SDK's internal services. The executor is the exception: set + * it on the options, see {@link InternalServices.Builder#executorService}. + */ public interface OptionsAndServicesConfiguration { void configure( @NotNull SentryAndroidOptions options, @NotNull InternalServices.Builder services); diff --git a/sentry/src/main/java/io/sentry/internal/InternalServices.java b/sentry/src/main/java/io/sentry/internal/InternalServices.java index 13f0251675e..8d1d7480b48 100644 --- a/sentry/src/main/java/io/sentry/internal/InternalServices.java +++ b/sentry/src/main/java/io/sentry/internal/InternalServices.java @@ -92,6 +92,11 @@ private Builder() {} return this; } + /** + * For the platform's init only. On Android, an executor set here from a hybrid SDK's callback + * would replace one the SDK has already started using, and that one would never be closed. + * Until 9.0, hybrid SDKs set their executor through {@code SentryOptions.setExecutorService}. + */ public @NotNull Builder executorService(final @NotNull ISentryExecutorService executorService) { this.executorService = executorService; return this;