Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add `SentrySdk.GetPersistedBreadcrumbs()` API to retrieve breadcrumbs from the previous Android app session ([#4977](https://github.com/getsentry/sentry-dotnet/pull/4977))

### Fixes

- The SDK now logs a `Warning` instead of an `Error` when being ratelimited ([#4927](https://github.com/getsentry/sentry-dotnet/pull/4927))
Expand Down
45 changes: 45 additions & 0 deletions src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Sentry.Android;
using Sentry.Android.Callbacks;
using Sentry.Android.Extensions;
Expand All @@ -22,6 +23,7 @@ namespace Sentry;
public static partial class SentrySdk
{
private static AndroidContext AppContext { get; set; } = Application.Context;
private static List<Breadcrumb>? _persistedBreadcrumbs;

private static void InitSentryAndroidSdk(SentryOptions options)
{
Expand Down Expand Up @@ -171,6 +173,11 @@ private static void InitSentryAndroidSdk(SentryOptions options)
o.ConnectionStatusProvider =
new AndroidConnectionStatusProvider(AppContext, o, buildInfoProvider, timeProvider, mainHandler).JavaCast<IConnectionStatusProvider>();
o.AddIntegration(new SystemEventsBreadcrumbsIntegration(AppContext, mainHandler).JavaCast<JavaSdk.IIntegration>());

// Read persisted breadcrumbs now, inside the options callback, before the SDK starts.
// This avoids a race condition with breadcrumb-producing integrations that start writing
// to the QueueFile as soon as SentryAndroid.Init() completes.
_persistedBreadcrumbs = ReadPersistedBreadcrumbs(o);
});

// Now initialize the Android SDK (with a logger only if we're debugging)
Expand Down Expand Up @@ -286,4 +293,42 @@ private static void AndroidEnvironment_UnhandledExceptionRaiser(object? _, Raise
#pragma warning restore CA1422
#pragma warning restore CS0618
}

/// <summary>
/// Retrieves breadcrumbs that were persisted to disk by the Android SDK's scope observer.
/// These are breadcrumbs from the previous app session that were saved before the app terminated.
/// </summary>
/// <returns>A list of persisted breadcrumbs, or an empty list if unavailable.</returns>
public static IReadOnlyList<Breadcrumb> GetPersistedBreadcrumbs() =>
_persistedBreadcrumbs ?? [];

private static List<Breadcrumb>? ReadPersistedBreadcrumbs(JavaSdk.SentryOptions nativeOptions)
{
if (nativeOptions.CacheDirPath is null)
{
return null;
}

// Create a temporary observer to read breadcrumbs from the previous session's QueueFile.
// This must be called before SentryAndroid.Init() completes, to avoid a race condition
// with breadcrumb-producing integrations that write to the same QueueFile concurrently.
using var observer = new JavaSdk.Cache.PersistingScopeObserver(nativeOptions);
Copy link
Copy Markdown
Member

@Flash0ver Flash0ver Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: could the user code just read the file directly and/or find and read the "breadcrumbs.json" themselves?

We're a bit hesitant of adding an API for a single use case, that is also either pessimizing all other scenarios, or a bit of a "dangerous" API (order of invocations).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, not easily -- the file has a special format and QueueFile is excluded from the bindings, plus, some necessary classes are package-private. Though, theoretically, exposing the QueueFile should be enough -- the rest can be achieved with a bit more code on the user's side

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a middle ground, we could expose the ReadPersistedBreadcrumbs method publicly so users can call this themselves (before initialising the Android SDK) and dispose of the breadcrumbs once they're done with them.

We wouldn't then call this automatically and store the results for the lifetime of the application in _persistedBreadcrumbs (even users that don't care about this functionality).

That way the functionality is there for folks that need to use it, but everyone else doesn't pay a feature bloat tax.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that's what I suggested to @Flash0ver internally as well. If y'all are fine with that, we could go that way 👍

var result = observer.Read(nativeOptions, "breadcrumbs.json",
Java.Lang.Class.FromType(typeof(Java.Util.IList)));

if (result is not Java.Util.IList javaList)
{
return null;
}

var breadcrumbs = new List<Breadcrumb>();
for (var i = 0; i < javaList.Size(); i++)
{
if (javaList.Get(i)?.JavaCast<JavaSdk.Breadcrumb>() is { } javaBreadcrumb)
{
breadcrumbs.Add(javaBreadcrumb.ToBreadcrumb());
}
}
return breadcrumbs;
}
}
Loading