Skip to content

Release health integration & Event-listener #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c236f00
release health
bruno-garcia Jun 10, 2021
ca9fdca
added release health integration & event listener
bitsandfoxes Jun 10, 2021
e41e94b
renamed gameobject creation
bitsandfoxes Jun 10, 2021
30b8378
removed debugging log
bitsandfoxes Jun 10, 2021
69bc5da
changelog
bruno-garcia Jun 10, 2021
e269b6f
Merge remote-tracking branch 'origin' into feat/release-health
bruno-garcia Jun 14, 2021
f5da014
Merge branch 'feat/release-health' into feat/release-health-listener
bruno-garcia Jun 14, 2021
7a77891
poc session start/end log output
bitsandfoxes Jun 15, 2021
c9fafa3
review changes
bitsandfoxes Jun 16, 2021
2223d36
default execution attribute
bitsandfoxes Jun 16, 2021
5d45bec
removed debug output
bitsandfoxes Jun 16, 2021
c2617fc
added default session timeout
bitsandfoxes Jun 16, 2021
32537ea
preparation for .net session tracking
bitsandfoxes Jun 21, 2021
11d15b5
Merge remote-tracking branch 'origin' into feat/release-health-listener
bruno-garcia Jun 22, 2021
f6eadb8
Merge remote-tracking branch 'origin' into feat/release-health-listen…
bruno-garcia Jun 23, 2021
6c8e4fe
merged main
bitsandfoxes Jun 23, 2021
18926f8
wip
bruno-garcia Jun 23, 2021
9ee1c26
Merge remote-tracking branch 'origin' into feat/release-health-listen…
bruno-garcia Jun 23, 2021
43a0fd7
Merge branch 'feat/release-health-listener-pause-resume' into feat/re…
bruno-garcia Jun 23, 2021
845ef22
Merge remote-tracking branch 'origin/feat/release-health-listener' in…
bruno-garcia Jun 23, 2021
f37af92
dotnet 3.6.0
bruno-garcia Jun 23, 2021
1b1c082
changelog
bruno-garcia Jun 23, 2021
f831b31
tweaked listener creation
bitsandfoxes Jun 24, 2021
c64184c
changed 'DebugOnlyInEditor' default to false
bitsandfoxes Jun 24, 2021
a2a8c0a
session restarting cleanup
bitsandfoxes Jun 25, 2021
8f0f41b
cleanup
bitsandfoxes Jun 25, 2021
81bc925
internal running state
bitsandfoxes Jun 25, 2021
651d725
Apply suggestions from code review
bruno-garcia Jun 25, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Sentry config is now a scriptable object ([#220](https://github.com/getsentry/sentry-unity/pull/220))
- Unity protocol ([#234](https://github.com/getsentry/sentry-unity/pull/234))
- Release health integration & Event-listener ([#225](https://github.com/getsentry/sentry-unity/pull/225))

### Fixes

Expand Down
3 changes: 3 additions & 0 deletions samples/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<!-- Stops msbuild from looking on the part directory. Can't deal with the properties set such as C# 9 -->
</Project>
86 changes: 86 additions & 0 deletions src/Sentry.Unity/ApplicationPauseListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using UnityEngine;

namespace Sentry.Unity
{
/// <summary>
/// A MonoBehavior used to forward application focus events to subscribers.
/// </summary>
[DefaultExecutionOrder(-900)]
internal class ApplicationPauseListener : MonoBehaviour
{
/// <summary>
/// Hook to receive an event when the application gains focus.
/// <remarks>
/// Listens to OnApplicationFocus for all platforms except Android, where we listen to OnApplicationPause.
/// </remarks>
/// </summary>
public event Action? ApplicationResuming;

/// <summary>
/// Hook to receive an event when the application loses focus.
/// <remarks>
/// Listens to OnApplicationFocus for all platforms except Android, where we listen to OnApplicationPause.
/// </remarks>
/// </summary>
public event Action? ApplicationPausing;

// Keeping internal track of running state because OnApplicationPause and OnApplicationFocus get called during startup and would fire false resume events
private bool _isRunning = true;

/// <summary>
/// To receive Leaving/Resuming events on Android.
/// <remarks>
/// On Android, when the on-screen keyboard is enabled, it causes a OnApplicationFocus(false) event.
/// Additionally, if you press "Home" at the moment the keyboard is enabled, the OnApplicationFocus() event is
/// not called, but OnApplicationPause() is called instead.
/// </remarks>
/// <seealso href="https://docs.unity3d.com/2019.4/Documentation/ScriptReference/MonoBehaviour.OnApplicationPause.html"/>
/// </summary>
private void OnApplicationPause(bool pauseStatus)
{
if (Application.platform != RuntimePlatform.Android)
{
return;
}

if (pauseStatus && _isRunning)
{
_isRunning = false;
ApplicationPausing?.Invoke();
}
else if (!pauseStatus && !_isRunning)
{
_isRunning = true;
ApplicationResuming?.Invoke();
}
}

/// <summary>
/// To receive Leaving/Resuming events on all platforms except Android.
/// </summary>
/// <param name="hasFocus"></param>
private void OnApplicationFocus(bool hasFocus)
{
// To avoid event duplication on Android since the pause event will be handled via OnApplicationPause
if (Application.platform == RuntimePlatform.Android)
{
return;
}

if (hasFocus && !_isRunning)
{
_isRunning = true;
ApplicationResuming?.Invoke();
}
else if (!hasFocus && _isRunning)
{
_isRunning = false;
ApplicationPausing?.Invoke();
}
}

// The GameObject has to destroy itself since it was created with HideFlags.HideAndDontSave
private void OnApplicationQuit() => Destroy(gameObject);
}
}
33 changes: 33 additions & 0 deletions src/Sentry.Unity/Integrations/SessionIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Sentry.Extensibility;
using Sentry.Integrations;
using UnityEngine;

namespace Sentry.Unity.Integrations
{
public class SessionIntegration : ISdkIntegration
{
public void Register(IHub hub, SentryOptions options)
{
if (!options.AutoSessionTracking)
{
return;
}

options.DiagnosticLogger?.LogDebug("Registering Session integration.");

// HideFlags.HideAndDontSave hides the GameObject in the hierarchy and prevents changing of scenes from destroying it
var gameListenerObject = new GameObject("SentryListener") {hideFlags = HideFlags.HideAndDontSave};
var gameListener = gameListenerObject.AddComponent<ApplicationPauseListener>();
gameListener.ApplicationResuming += () =>
{
options.DiagnosticLogger?.LogDebug("Resuming session.");
hub.ResumeSession();
};
gameListener.ApplicationPausing += () =>
{
options.DiagnosticLogger?.LogDebug("Pausing session.");
hub.PauseSession();
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void OnQuitting()
// Note: On Windows Store Apps and Windows Phone 8.1 there is no application quit event. Consider using OnApplicationFocus event when focusStatus equals false.
// Note: On WebGL it is not possible to implement OnApplicationQuit due to nature of the browser tabs closing.
_application.LogMessageReceived -= OnLogMessageReceived;
_hub?.EndSession();
_hub?.FlushAsync(_sentryOptions?.ShutdownTimeout ?? TimeSpan.FromSeconds(1)).GetAwaiter().GetResult();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Sentry.Unity/SentryOptionsUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Sentry.Extensibility;
using System;
using Sentry.Unity.Integrations;
using UnityEngine;

namespace Sentry.Unity
{
Expand All @@ -12,6 +11,7 @@ public static void SetDefaults(SentryUnityOptions options, IApplication? applica

options.Enabled = true;
options.Dsn = null;
options.AutoSessionTracking = true;
options.CaptureInEditor = true;
options.RequestBodyCompressionLevel = CompressionLevelWithAuto.NoCompression;
options.AttachStacktrace = false;
Expand All @@ -26,7 +26,7 @@ public static void SetDefaults(SentryUnityOptions options, IApplication? applica
options.CacheDirectoryPath = application.PersistentDataPath;

options.Debug = true;
options.DebugOnlyInEditor = true;
options.DebugOnlyInEditor = false;
options.DiagnosticLevel = SentryLevel.Warning;

TryAttachLogger(options, application);
Expand All @@ -46,7 +46,7 @@ public static void SetDefaults(ScriptableSentryUnityOptions options)
options.EnableOfflineCaching = true;

options.Debug = true;
options.DebugOnlyInEditor = true;
options.DebugOnlyInEditor = false;
options.DiagnosticLevel = SentryLevel.Warning;
}

Expand Down
1 change: 0 additions & 1 deletion src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel;
using Sentry.Extensibility;
using UnityEngine;

namespace Sentry.Unity
{
Expand Down
1 change: 1 addition & 0 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public SentryUnityOptions()
this.AddIntegration(new UnityApplicationLoggingIntegration());
this.AddIntegration(new UnityBeforeSceneLoadIntegration());
this.AddIntegration(new SceneManagerIntegration());
this.AddIntegration(new SessionIntegration());
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion src/sentry-dotnet
Submodule sentry-dotnet updated 61 files
+13 −0 CHANGELOG.md
+1 −2 Directory.Build.props
+2 −0 src/Sentry.Serilog/SentrySinkExtensions.cs
+6 −32 src/Sentry/Breadcrumb.cs
+27 −19 src/Sentry/Envelopes/Envelope.cs
+2 −2 src/Sentry/Envelopes/EnvelopeItem.cs
+10 −54 src/Sentry/Exceptions/Mechanism.cs
+6 −35 src/Sentry/Exceptions/SentryException.cs
+20 −130 src/Sentry/Exceptions/SentryStackFrame.cs
+1 −11 src/Sentry/Exceptions/SentryStackTrace.cs
+14 −0 src/Sentry/Extensibility/DisabledHub.cs
+14 −0 src/Sentry/Extensibility/HubAdapter.cs
+48 −27 src/Sentry/GlobalSessionManager.cs
+13 −0 src/Sentry/IHub.cs
+0 −93 src/Sentry/IJsonSerializable.cs
+0 −5 src/Sentry/ISession.cs
+6 −2 src/Sentry/ISessionManager.cs
+26 −0 src/Sentry/Integrations/AutoSessionTrackingIntegration.cs
+0 −17 src/Sentry/Internal/Empty.cs
+3 −7 src/Sentry/Internal/Enricher.cs
+14 −2 src/Sentry/Internal/Extensions/CollectionsExtensions.cs
+326 −43 src/Sentry/Internal/Extensions/JsonExtensions.cs
+15 −0 src/Sentry/Internal/Extensions/MiscExtensions.cs
+58 −19 src/Sentry/Internal/Hub.cs
+19 −4 src/Sentry/Internal/MainSentryEventProcessor.cs
+2 −11 src/Sentry/Package.cs
+7 −35 src/Sentry/Protocol/App.cs
+2 −10 src/Sentry/Protocol/Browser.cs
+5 −1 src/Sentry/Protocol/Contexts.cs
+43 −186 src/Sentry/Protocol/Device.cs
+15 −75 src/Sentry/Protocol/Gpu.cs
+6 −30 src/Sentry/Protocol/OperatingSystem.cs
+4 −20 src/Sentry/Protocol/Runtime.cs
+6 −30 src/Sentry/Protocol/Trace.cs
+18 −4 src/Sentry/Reflection/AssemblyExtensions.cs
+24 −0 src/Sentry/ReportAssembliesMode.cs
+15 −55 src/Sentry/Request.cs
+10 −25 src/Sentry/SdkVersion.cs
+1 −1 src/Sentry/Sentry.csproj
+23 −147 src/Sentry/SentryEvent.cs
+4 −25 src/Sentry/SentryMessage.cs
+37 −4 src/Sentry/SentryOptions.cs
+10 −0 src/Sentry/SentrySdk.cs
+5 −29 src/Sentry/SentryThread.cs
+1 −0 src/Sentry/SentryValues.cs
+5 −13 src/Sentry/Session.cs
+30 −47 src/Sentry/SessionUpdate.cs
+9 −39 src/Sentry/Span.cs
+16 −109 src/Sentry/Transaction.cs
+8 −32 src/Sentry/User.cs
+3 −19 src/Sentry/UserFeedback.cs
+4 −0 test/Sentry.Serilog.Tests/SentrySerilogSinkExtensionsTests.cs
+21 −28 test/Sentry.Tests/GlobalSessionManagerTests.cs
+1 −0 test/Sentry.Tests/Helpers/JsonSerializableExtensions.cs
+104 −6 test/Sentry.Tests/HubTests.cs
+4 −4 test/Sentry.Tests/Internals/Http/HttpTransportTests.cs
+39 −0 test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
+1 −2 test/Sentry.Tests/PlatformAbstractions/RuntimeInfoTests.cs
+22 −2 test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs
+1 −1 test/Sentry.Tests/Sentry.Tests.csproj
+5 −6 test/Sentry.Tests/SessionTests.cs
22 changes: 14 additions & 8 deletions test/Sentry.Unity.Tests/Stubs/TestHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,16 @@ public void CaptureTransaction(Transaction transaction)

public void CaptureSession(SessionUpdate sessionUpdate)
{
throw new NotImplementedException();
}

public Task FlushAsync(TimeSpan timeout)
{
throw new NotImplementedException();
return Task.CompletedTask;
}

public void ConfigureScope(Action<Scope> configureScope) => _configureScopeCalls.Add(configureScope);

public Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
{
throw new NotImplementedException();
}
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope) => Task.CompletedTask;

public void BindClient(ISentryClient client)
{
Expand Down Expand Up @@ -93,12 +89,22 @@ public void BindException(Exception exception, ISpan span)

public void StartSession()
{
throw new NotImplementedException();
// TODO: test sessions
}

public void PauseSession()
{
// TODO: test sessions
}

public void ResumeSession()
{
// TODO: test sessions
}

public void EndSession(SessionEndStatus status = SessionEndStatus.Exited)
{
throw new NotImplementedException();
// TODO: test sessions
}
}
}