Skip to content

Commit d3f4476

Browse files
committed
Collect breadcrumbs sent early on startup
1 parent d9c30f4 commit d3f4476

File tree

4 files changed

+60
-65
lines changed

4 files changed

+60
-65
lines changed

integration-test/android.Tests.ps1

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ Describe 'MAUI app (<tfm>, <configuration>)' -ForEach @(
5454
{
5555
param(
5656
[string] $Dsn,
57-
[string] $TestArg = 'None',
58-
[ScriptBlock] $Callback = $null
57+
[string] $TestArg = 'None'
5958
)
6059
Write-Host "::group::Run Android app (TestArg=$TestArg)"
6160
$dsn = $Dsn.Replace('http://', 'http://key@') + '/0'
@@ -74,10 +73,7 @@ Describe 'MAUI app (<tfm>, <configuration>)' -ForEach @(
7473

7574
$procid = (& xharness android adb -- shell pidof "io.sentry.dotnet.maui.device.integrationtestapp") -replace '\s', ''
7675
$activity = (& xharness android adb -- shell dumpsys activity activities) -match "io\.sentry\.dotnet\.maui\.device\.integrationtestapp"
77-
if ($procid -and $activity -and $Callback)
78-
{
79-
& $Callback | ForEach-Object { Write-Host $_ }
80-
}
76+
8177
} while ($procid -and $activity)
8278
}
8379

@@ -182,54 +178,27 @@ Describe 'MAUI app (<tfm>, <configuration>)' -ForEach @(
182178
}
183179
}
184180

185-
It 'Delivers battery breadcrumbs in main thread (<configuration>)' -Skip:($configuration -eq "Release") {
186-
try
187-
{
188-
$result = Invoke-SentryServer {
189-
param([string]$url)
190-
RunAndroidApp -Dsn $url -TestArg "BATTERY_CHANGED" {
191-
# Trigger BATTERY_CHANGED events by incrementing the battery level
192-
$battery = [int](& xharness android adb -- shell dumpsys battery get level)
193-
$battery = ($battery % 100) + 1
194-
xharness android adb -v -- shell dumpsys battery set level $battery | ForEach-Object { Write-Host $_ }
195-
}
196-
}
197-
198-
Dump-ServerErrors -Result $result
199-
$result.HasErrors() | Should -BeFalse
200-
$result.Envelopes() | Should -AnyElementMatch "`"type`":`"system`",`"thread_id`":1,`"category`":`"device.event`",`"action`":`"BATTERY_CHANGED`""
201-
$result.Envelopes() | Should -HaveCount 1
202-
}
203-
finally
204-
{
205-
xharness android adb -v -- shell dumpsys battery reset | ForEach-Object { Write-Host $_ }
181+
It 'Delivers battery breadcrumbs in main thread (<configuration>)' {
182+
$result = Invoke-SentryServer {
183+
param([string]$url)
184+
RunAndroidApp -Dsn $url -TestArg "BATTERY_CHANGED"
206185
}
207-
}
208186

209-
It 'Delivers network breadcrumbs in main thread (<configuration>)' -Skip:($configuration -eq "Release") {
210-
try
211-
{
212-
$result = Invoke-SentryServer {
213-
param([string]$url)
214-
RunAndroidApp -Dsn $url -TestArg "NETWORK_CAPABILITIES_CHANGED" {
215-
# Trigger NETWORK_CAPABILITIES_CHANGED events by toggling WiFi on/off
216-
$wifi = (& xharness android adb -- shell settings get global wifi_on) -replace '\s', ''
217-
if ($wifi -eq '1') {
218-
xharness android adb -v -- shell svc wifi disable | ForEach-Object { Write-Host $_ }
219-
} else {
220-
xharness android adb -v -- shell svc wifi enable | ForEach-Object { Write-Host $_ }
221-
}
222-
}
223-
}
187+
Dump-ServerErrors -Result $result
188+
$result.HasErrors() | Should -BeFalse
189+
$result.Envelopes() | Should -AnyElementMatch "`"type`":`"system`",`"thread_id`":`"1`",`"category`":`"device.event`",`"action`":`"BATTERY_CHANGED`""
190+
$result.Envelopes() | Should -HaveCount 1
191+
}
224192

225-
Dump-ServerErrors -Result $result
226-
$result.HasErrors() | Should -BeFalse
227-
$result.Envelopes() | Should -AnyElementMatch "`"type`":`"system`",`"thread_id`":1,`"category`":`"network.event`",`"action`":`"NETWORK_CAPABILITIES_CHANGED`""
228-
$result.Envelopes() | Should -HaveCount 1
229-
}
230-
finally
231-
{
232-
xharness android adb -v -- shell svc wifi enable | ForEach-Object { Write-Host $_ }
193+
It 'Delivers network breadcrumbs in main thread (<configuration>)' {
194+
$result = Invoke-SentryServer {
195+
param([string]$url)
196+
RunAndroidApp -Dsn $url -TestArg "NETWORK_CAPABILITIES_CHANGED"
233197
}
198+
199+
Dump-ServerErrors -Result $result
200+
$result.HasErrors() | Should -BeFalse
201+
$result.Envelopes() | Should -AnyElementMatch "`"type`":`"system`",`"thread_id`":`"1`",`"category`":`"network.event`",`"action`":`"NETWORK_CAPABILITIES_CHANGED`""
202+
$result.Envelopes() | Should -HaveCount 1
234203
}
235204
}

integration-test/net9-maui/App.xaml.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#if ANDROID
22
using Android.OS;
33
#endif
4+
using System.Collections.Concurrent;
45

56
namespace Sentry.Maui.Device.IntegrationTestApp;
67

78
public partial class App : Application
89
{
10+
private static readonly ConcurrentDictionary<string, Breadcrumb> _breadcrumbs = new();
11+
912
public App()
1013
{
1114
InitializeComponent();
@@ -18,6 +21,30 @@ public static bool HasTestArg(string arg)
1821
return string.Equals(TestArg, arg, StringComparison.OrdinalIgnoreCase);
1922
}
2023

24+
public static void RecordBreadcrumb(Breadcrumb breadcrumb)
25+
{
26+
if (breadcrumb.Data?.TryGetValue("action", out var action) != true)
27+
{
28+
return;
29+
}
30+
31+
_breadcrumbs[action] = new Breadcrumb(
32+
breadcrumb.Message,
33+
breadcrumb.Type,
34+
new Dictionary<string, string>
35+
{
36+
["action"] = action,
37+
["thread_id"] = Thread.CurrentThread.ManagedThreadId.ToString()
38+
},
39+
breadcrumb.Category,
40+
breadcrumb.Level);
41+
}
42+
43+
public static bool TryGetBreadcrumb(string action, out Breadcrumb? breadcrumb)
44+
{
45+
return _breadcrumbs.TryGetValue(action, out breadcrumb);
46+
}
47+
2148
protected override Window CreateWindow(IActivationState? activationState)
2249
{
2350
return new Window(new AppShell());

integration-test/net9-maui/MainPage.xaml.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@ protected override void OnAppearing()
2929
{
3030
SentrySdk.CaptureException(ex);
3131
}
32-
App.Kill();
3332
}
34-
else if (App.HasTestArg("None"))
33+
else if (App.TryGetBreadcrumb(App.TestArg, out var breadcrumb) && breadcrumb.Data != null)
3534
{
36-
App.Kill();
35+
SentrySdk.CaptureMessage(App.TestArg, scope =>
36+
{
37+
scope.SetExtra("category", breadcrumb.Category);
38+
foreach (var kvp in breadcrumb.Data)
39+
{
40+
scope.SetExtra(kvp.Key, kvp.Value);
41+
}
42+
scope.SetExtra("type", breadcrumb.Type);
43+
});
3744
}
45+
46+
App.Kill();
3847
}
3948
}

integration-test/net9-maui/MauiProgram.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@ public static MauiApp CreateMauiApp()
2323

2424
options.SetBeforeBreadcrumb((breadcrumb, hint) =>
2525
{
26-
if (breadcrumb.Data?.TryGetValue("action", out var action) == true && App.HasTestArg(action))
27-
{
28-
SentrySdk.CaptureMessage(action, scope =>
29-
{
30-
scope.SetExtra("action", action);
31-
scope.SetExtra("category", breadcrumb.Category);
32-
scope.SetExtra("thread_id", Thread.CurrentThread.ManagedThreadId);
33-
scope.SetExtra("type", breadcrumb.Type);
34-
});
35-
App.Kill();
36-
}
26+
App.RecordBreadcrumb(breadcrumb);
3727
return breadcrumb;
3828
});
3929
})

0 commit comments

Comments
 (0)