Skip to content

Commit

Permalink
Sentry integration (#644)
Browse files Browse the repository at this point in the history
* Sentry integration

* Force crash button fix

* Unit tests fixes
  • Loading branch information
akrol95 authored Jan 3, 2025
1 parent 08c4a12 commit 1877701
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 32 deletions.
2 changes: 1 addition & 1 deletion BMM.Core/BMM.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
<PackageReference Include="IdentityModel.OidcClient" Version="6.0.0" />
<PackageReference Include="Microsoft.AppCenter" Version="5.0.1" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.1" />
<PackageReference Include="MvvmCross" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.JsonLocalization" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.Messenger" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.Visibility" Version="9.2.0" />
<PackageReference Include="Sentry" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.34.0" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
<PackageReference Update="Microsoft.Maui.Graphics" Version="8.0.80" />
Expand Down
9 changes: 9 additions & 0 deletions BMM.Core/Exceptions/ForcedExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BMM.Core.Exceptions
{
public class ForcedException : Exception
{
public ForcedException(object sender) : base(sender.ToString())
{
}
}
}
57 changes: 57 additions & 0 deletions BMM.Core/Helpers/AnalyticsInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.Maui.Devices;

namespace BMM.Core.Helpers
{
public abstract class AnalyticsInitializer
{
private static string iOSPrefix = "ios_";
private static string AndroidPrefix = "android_";
private static string Dev = "dev";
private static string Prod = "prod";

public static void DroidRegister()
{
if (!AppCenter.Configured)
AppCenter.Start(GlobalConstants.DroidAppSecret, typeof(Analytics));

InitSentry();
}

public static void IOSRegister()
{
if (!AppCenter.Configured)
AppCenter.Start(GlobalConstants.iOSAppSecret, typeof(Analytics));

InitSentry();
}

private static void InitSentry()
{
SentrySdk.Init(options =>
{
options.Dsn = GlobalConstants.SentryDsn;
options.Environment = GetSentryEnvironment();
options.Debug = false;
options.TracesSampleRate = 1.0;
options.ProfilesSampleRate = 1.0;
});
}

private static string GetSentryEnvironment()
{
string env;

#if ENV_INT
env = Dev;
#else
env = Prod;
#endif

return DeviceInfo.Current.Platform == DevicePlatform.Android
? $"{AndroidPrefix}{env}"
: $"{iOSPrefix}{env}";
}
}
}
21 changes: 0 additions & 21 deletions BMM.Core/Helpers/AppCenterHelper.cs

This file was deleted.

3 changes: 2 additions & 1 deletion BMM.Core/Helpers/GlobalConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class GlobalConstants
public const int DefaultNumberOfPodcastTracksToDownload = 3;
public const string DroidAppSecret = "#{Android_APP_SECRET_PLACEHOLDER}#";
public const string iOSAppSecret = "#{iOS_APP_SECRET_PLACEHOLDER}#";

public const string SentryDsn = "#{SENTRY_DSN_PLACEHOLDER}#";

public const string AndroidUpdateLink = "https://play.google.com/store/apps/details?id=org.brunstad.bmm";
public const string IosUpdateLink = "itms-apps://apps.apple.com/no/app/bmm-brunstad/id777577855";
}
Expand Down
17 changes: 14 additions & 3 deletions BMM.Core/Implementations/Logger/BaseLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using BMM.Core.Implementations.Security;
using BMM.Core.Utils;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Crashes;

namespace BMM.Core.Implementations.Logger
{
Expand Down Expand Up @@ -47,7 +46,11 @@ public void Warn(string tag, string message)
public virtual void Error(string tag, string message)
{
var parameters = InitializeDictionaryWithBasicParameters(tag, message);
Crashes.TrackError(new ErrorWithoutException(tag + " - " + message), parameters);

SentrySdk.CaptureMessage(tag + " - " + message,
GetSentryScope(parameters),
SentryLevel.Error);

AppCenterLog.Error(tag, message);
Microsoft.AppCenter.Analytics.Analytics.TrackEvent("Error without exception", parameters);
}
Expand All @@ -56,7 +59,7 @@ public virtual void Error(string tag, string message, Exception exception, bool
{
var parameters = InitializeDictionaryWithBasicParameters(tag, message, presentedToUser);

Crashes.TrackError(exception, parameters);
SentrySdk.CaptureException(exception, GetSentryScope(parameters));
AppCenterLog.Error(tag, message, exception);

parameters.Add(StackTrackParameterName, exception.StackTrace);
Expand All @@ -65,6 +68,14 @@ public virtual void Error(string tag, string message, Exception exception, bool
Microsoft.AppCenter.Analytics.Analytics.TrackEvent("Error with exception", parameters);
}

private static Action<Scope> GetSentryScope(IDictionary<string, string> parameters)
{
return scope =>
{
scope.SetExtras(parameters.Select(s => new KeyValuePair<string, object>(s.Key, s.Value)));
};
}

private void AddConnectionType(IDictionary<string, string> parameters)
{
parameters.Add(AnalyticsConstants.ConnectionParameterName, AnalyticsUtils.GetConnectionType(_connection));
Expand Down
4 changes: 3 additions & 1 deletion BMM.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Text;
using Acr.UserDialogs;
using BMM.Core.Exceptions;
using BMM.Core.Extensions;
using BMM.Core.GuardedActions.BibleStudy.Interfaces;
using BMM.Core.GuardedActions.DebugInfo.Interfaces;
Expand Down Expand Up @@ -32,6 +33,7 @@
using BMM.Core.Translation;
using BMM.Core.Utils;
using BMM.Core.ViewModels.Base;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Devices;
using MvvmCross;
using MvvmCross.Commands;
Expand Down Expand Up @@ -541,7 +543,7 @@ private async Task ShowCachedTracks()

private void CrashTheApp()
{
throw new Exception("Forcefully crash the app!");
throw new ForcedException("Forcefully crash the app!");
}

private async Task Logout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Acr.UserDialogs;
using BMM.Api.Implementation.Models;
using BMM.Core.Exceptions;
using BMM.Core.GuardedActions.BibleStudy.Interfaces;
using BMM.Core.GuardedActions.Settings.Interfaces;
using BMM.Core.Implementations;
Expand All @@ -19,12 +20,14 @@
using BMM.Core.Implementations.FirebaseRemoteConfig;
using BMM.Core.Implementations.Notifications;
using BMM.Core.Implementations.Security;
using BMM.Core.Implementations.Storage;
using BMM.Core.Implementations.UI;
using BMM.Core.Models;
using BMM.Core.Models.POs.Other;
using BMM.Core.Test.Unit.ViewModels.Base;
using BMM.Core.Translation;
using BMM.Core.ViewModels;
using Microsoft.Maui.Storage;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources;
using Moq;
using MvvmCross.Localization;
Expand Down Expand Up @@ -66,6 +69,7 @@ public class SettingsViewModelTests : BaseViewModelTests
private Mock<IMvxNavigationService> _mvxNavigationService;
private Mock<IBadgeService> _badgeService;
private Mock<IMvxMessenger> _messageService;
private IPreferences _preferencesMock;

public override void SetUp()
{
Expand Down Expand Up @@ -128,6 +132,9 @@ public override void SetUp()
_storageManager.Setup(x => x.Storages).Returns(new ObservableCollection<IFileStorage>() {defaultStorage.Object});
_storageManager.Setup(x => x.SelectedStorage).Returns(defaultStorage.Object);
_storageManager.Setup(x => x.HasMultipleStorageSupport).Returns(true);

_preferencesMock = Substitute.For<IPreferences>();
AppSettings.SetImplementation(_preferencesMock);
}

public SettingsViewModel CreateSettingsViewModel()
Expand Down Expand Up @@ -200,7 +207,7 @@ public async Task OnSelectedItem_ShouldCrashAppUsingDedicatedButton()
await settingsViewModel.Initialize();

// Act & Assert
Assert.Throws<Exception>(() => settingsViewModel.ListItems.OfType<SelectableListItem>().FirstOrDefault(x => x.Title == "Crash the app")?.OnSelected.Execute());
Assert.Throws<ForcedException>(() => settingsViewModel.ListItems.OfType<SelectableListItem>().FirstOrDefault(x => x.Title == "Crash the app")?.OnSelected.Execute());
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion BMM.UI.Android/Application/Helpers/SetupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SetupHelper
/// </summary>
public static void EnsureInitialized()
{
AppCenterHelper.DroidRegister();
AnalyticsInitializer.DroidRegister();
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(BmmApplication.Instance);
setup.EnsureInitialized();
}
Expand Down
1 change: 1 addition & 0 deletions BMM.UI.Android/BMM.UI.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="MvvmCross.DroidX.SwipeRefreshLayout" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.Color" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.JsonLocalization" Version="9.2.0" />
<PackageReference Include="Sentry" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="XAB.FlexboxLayout" Version="3.0.0.2" />
<PackageReference Include="Xam.Plugins.Android.ExoPlayer" Version="2.19.1" />
Expand Down
2 changes: 1 addition & 1 deletion BMM.UI.Android/SplashScreenActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SplashScreenActivity() : base(Resource.Layout.splash_screen)

protected override void OnCreate(Bundle savedInstanceState)
{
AppCenterHelper.DroidRegister();
AnalyticsInitializer.DroidRegister();
base.OnCreate(savedInstanceState);
SetNotificationToHandle();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BMM.Api.Framework;
using BMM.Core.Exceptions;
using BMM.Core.Implementations.Analytics;
using BMM.UI.iOS.TableViewCell.Base;
using MvvmCross;
Expand Down Expand Up @@ -62,7 +63,7 @@ public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
tableView.DeselectRow(indexPath, true);
base.RowSelected(tableView, indexPath);
}
catch (Exception e)
catch (Exception e) when (e is not ForcedException)
{
Mvx
.IoCProvider
Expand Down
1 change: 1 addition & 0 deletions BMM.UI.iOS/BMM.UI.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@
<PackageReference Include="MvvmCross" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.Color" Version="9.2.0" />
<PackageReference Include="MvvmCross.Plugin.JsonLocalization" Version="9.2.0" />
<PackageReference Include="Sentry" Version="5.0.0" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Xamarin.Firebase.iOS.CloudMessaging" Version="8.10.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion BMM.UI.iOS/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Application
// This is the main entry point of the application.
private static void Main(string[] args)
{
AppCenterHelper.IOSRegister();
AnalyticsInitializer.IOSRegister();
BmmApplication.Main(args, "BmmApplication", "AppDelegate");
}
}
Expand Down

0 comments on commit 1877701

Please sign in to comment.