Skip to content

Commit

Permalink
Add sentry logging
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Jul 11, 2024
1 parent f310386 commit 6144736
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fluXis.Game/FluXisGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using fluXis.Game.Screens.Select;
using fluXis.Game.Screens.Skinning;
using fluXis.Game.Utils;
using fluXis.Game.Utils.Sentry;
using fluXis.Shared.API.Packets.Other;
using fluXis.Shared.API.Packets.User;
using fluXis.Shared.Components.Users;
Expand Down Expand Up @@ -75,6 +76,8 @@ public partial class FluXisGame : FluXisGameBase, IKeyBindingHandler<FluXisGloba

private LoadInfo loadInfo { get; } = new();

private SentryClient sentry { get; }

private bool isExiting;

private readonly BindableDouble inactiveVolume = new(1f);
Expand All @@ -84,6 +87,12 @@ public partial class FluXisGame : FluXisGameBase, IKeyBindingHandler<FluXisGloba
[UsedImplicitly]
public bool Sex { get; private set; }

public FluXisGame()
{
// created here so that we can catch events before the game even starts
sentry = new SentryClient(this);
}

[BackgroundDependencyLoader]
private void load()
{
Expand Down Expand Up @@ -210,6 +219,8 @@ protected override void LoadComplete()
base.LoadComplete();
WaitForReady(() => PerformUpdateCheck(true));

sentry.BindUser(APIClient.User);

loadLocales();

toolbar.AllowOverlays.BindTo(allowOverlays);
Expand Down
96 changes: 96 additions & 0 deletions fluXis.Game/Utils/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using fluXis.Game.Configuration;
using fluXis.Shared.Components.Users;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using Sentry;

namespace fluXis.Game.Utils.Sentry;

public partial class SentryClient : Component
{
private FluXisGame game { get; }
private IDisposable session { get; }
private IBindable<APIUser> user { get; set; }

public SentryClient(FluXisGame game)
{
this.game = game;

Logger.Log("Initializing sentry client...");

if (FluXisGameBase.IsDebug)
return;

session = SentrySdk.Init(opt =>
{
opt.Dsn = "https://[email protected]/2";
opt.IsEnvironmentUser = false;
opt.AutoSessionTracking = true;
opt.IsGlobalModeEnabled = true;
opt.Release = FluXisGameBase.VersionString.TrimStart('v');
});

Logger.Log("Initialized sentry client.");

Logger.NewEntry += onEntry;
}

public void BindUser(IBindable<APIUser> bind)
{
if (session == null)
return;

user = bind.GetBoundCopy();
user.BindValueChanged(e =>
{
SentrySdk.ConfigureScope(s => s.User = new SentryUser
{
Username = e.NewValue?.Username ?? "Guest User",
Id = $"{e.NewValue?.ID ?? 0}"
});
});
}

private void onEntry(LogEntry entry)
{
if (entry.Level != LogLevel.Error)
return;

var ex = entry.Exception;

if (ex == null)
return;

SentrySdk.CaptureEvent(new SentryEvent(ex)
{
Message = entry.Message,
Level = entry.Level switch
{
LogLevel.Debug => SentryLevel.Debug,
LogLevel.Verbose => SentryLevel.Info,
LogLevel.Important => SentryLevel.Warning,
LogLevel.Error => SentryLevel.Error,
_ => throw new ArgumentOutOfRangeException()
}
}, scope =>
{
scope.Contexts["config"] = new
{
Game = game.Dependencies.Get<FluXisConfig>()?.GetCurrentConfigurationForLogging(),
Framework = game.Dependencies.Get<FrameworkConfigManager>()?.GetCurrentConfigurationForLogging(),
};
});
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

Logger.NewEntry -= onEntry;
session?.Dispose();
}
}
1 change: 1 addition & 0 deletions fluXis.Game/fluXis.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Realm" Version="11.7.0" />
<PackageReference Include="Sentry" Version="4.8.1" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />

<!-- Required for building specific platforms with TeamCity -->
Expand Down

0 comments on commit 6144736

Please sign in to comment.