Skip to content

Commit cde2961

Browse files
authored
feat: Added SentrySdk.IsSessionActive (#4662)
1 parent 8561566 commit cde2961

File tree

10 files changed

+37
-0
lines changed

10 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
### Features
2828

2929
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633), [#4653](https://github.com/getsentry/sentry-dotnet/pull/4653))
30+
- The SDK now provides a `IsSessionActive` to allow checking the session state ([#4662](https://github.com/getsentry/sentry-dotnet/pull/4662))
3031
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633))
3132

3233
### Fixes

src/Sentry/Extensibility/DisabledHub.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class DisabledHub : IHub, IDisposable
1919
/// </summary>
2020
public bool IsEnabled => false;
2121

22+
/// <summary>
23+
/// Always returns false.
24+
/// </summary>
25+
public bool IsSessionActive => false;
26+
2227
private DisabledHub()
2328
{
2429
}

src/Sentry/Extensibility/HubAdapter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ private HubAdapter() { }
3737
/// </summary>
3838
public SentryStructuredLogger Logger { [DebuggerStepThrough] get => SentrySdk.Logger; }
3939

40+
/// <summary>
41+
/// Forwards the call to <see cref="SentrySdk"/>.
42+
/// </summary>
43+
public bool IsSessionActive { [DebuggerStepThrough] get => SentrySdk.IsSessionActive; }
44+
4045
/// <summary>
4146
/// Forwards the call to <see cref="SentrySdk"/>.
4247
/// </summary>

src/Sentry/IHub.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public TransactionContext ContinueTrace(
8888
string? name = null,
8989
string? operation = null);
9090

91+
/// <summary>
92+
/// Gets a value indicating whether there is an active session.
93+
/// </summary>
94+
public bool IsSessionActive { get; }
95+
9196
/// <summary>
9297
/// Starts a new session.
9398
/// </summary>

src/Sentry/Internal/Hub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal class Hub : IHub, IDisposable
3535

3636
public bool IsEnabled => _isEnabled;
3737

38+
public bool IsSessionActive => _sessionManager.IsSessionActive;
39+
3840
internal SentryOptions Options => _options;
3941

4042
private Scope CurrentScope => ScopeManager.GetCurrent().Key;

src/Sentry/SentrySdk.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,9 @@ public static TransactionContext ContinueTrace(
751751
string? operation = null)
752752
=> CurrentHub.ContinueTrace(traceHeader, baggageHeader, name, operation);
753753

754+
/// <inheritdoc cref="IHub.IsSessionActive"/>
755+
public static bool IsSessionActive { [DebuggerStepThrough] get => CurrentHub.IsSessionActive; }
756+
754757
/// <inheritdoc cref="IHub.StartSession"/>
755758
[DebuggerStepThrough]
756759
public static void StartSession()

test/Sentry.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ namespace Sentry
186186
}
187187
public interface IHub : Sentry.ISentryClient, Sentry.ISentryScopeManager
188188
{
189+
bool IsSessionActive { get; }
189190
Sentry.SentryId LastEventId { get; }
190191
Sentry.SentryStructuredLogger Logger { get; }
191192
void BindException(System.Exception exception, Sentry.ISpan span);
@@ -826,6 +827,7 @@ namespace Sentry
826827
public static class SentrySdk
827828
{
828829
public static bool IsEnabled { get; }
830+
public static bool IsSessionActive { get; }
829831
public static Sentry.SentryId LastEventId { get; }
830832
public static Sentry.SentryStructuredLogger Logger { get; }
831833
public static void AddBreadcrumb(Sentry.Breadcrumb breadcrumb, Sentry.SentryHint? hint = null) { }
@@ -1370,6 +1372,7 @@ namespace Sentry.Extensibility
13701372
{
13711373
public static readonly Sentry.Extensibility.DisabledHub Instance;
13721374
public bool IsEnabled { get; }
1375+
public bool IsSessionActive { get; }
13731376
public Sentry.SentryId LastEventId { get; }
13741377
public Sentry.SentryStructuredLogger Logger { get; }
13751378
public void BindClient(Sentry.ISentryClient client) { }
@@ -1416,6 +1419,7 @@ namespace Sentry.Extensibility
14161419
{
14171420
public static readonly Sentry.Extensibility.HubAdapter Instance;
14181421
public bool IsEnabled { get; }
1422+
public bool IsSessionActive { get; }
14191423
public Sentry.SentryId LastEventId { get; }
14201424
public Sentry.SentryStructuredLogger Logger { get; }
14211425
public void AddBreadcrumb(string message, string? category = null, string? type = null, System.Collections.Generic.IDictionary<string, string>? data = null, Sentry.BreadcrumbLevel level = 0) { }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ namespace Sentry
186186
}
187187
public interface IHub : Sentry.ISentryClient, Sentry.ISentryScopeManager
188188
{
189+
bool IsSessionActive { get; }
189190
Sentry.SentryId LastEventId { get; }
190191
Sentry.SentryStructuredLogger Logger { get; }
191192
void BindException(System.Exception exception, Sentry.ISpan span);
@@ -826,6 +827,7 @@ namespace Sentry
826827
public static class SentrySdk
827828
{
828829
public static bool IsEnabled { get; }
830+
public static bool IsSessionActive { get; }
829831
public static Sentry.SentryId LastEventId { get; }
830832
public static Sentry.SentryStructuredLogger Logger { get; }
831833
public static void AddBreadcrumb(Sentry.Breadcrumb breadcrumb, Sentry.SentryHint? hint = null) { }
@@ -1370,6 +1372,7 @@ namespace Sentry.Extensibility
13701372
{
13711373
public static readonly Sentry.Extensibility.DisabledHub Instance;
13721374
public bool IsEnabled { get; }
1375+
public bool IsSessionActive { get; }
13731376
public Sentry.SentryId LastEventId { get; }
13741377
public Sentry.SentryStructuredLogger Logger { get; }
13751378
public void BindClient(Sentry.ISentryClient client) { }
@@ -1416,6 +1419,7 @@ namespace Sentry.Extensibility
14161419
{
14171420
public static readonly Sentry.Extensibility.HubAdapter Instance;
14181421
public bool IsEnabled { get; }
1422+
public bool IsSessionActive { get; }
14191423
public Sentry.SentryId LastEventId { get; }
14201424
public Sentry.SentryStructuredLogger Logger { get; }
14211425
public void AddBreadcrumb(string message, string? category = null, string? type = null, System.Collections.Generic.IDictionary<string, string>? data = null, Sentry.BreadcrumbLevel level = 0) { }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ namespace Sentry
186186
}
187187
public interface IHub : Sentry.ISentryClient, Sentry.ISentryScopeManager
188188
{
189+
bool IsSessionActive { get; }
189190
Sentry.SentryId LastEventId { get; }
190191
Sentry.SentryStructuredLogger Logger { get; }
191192
void BindException(System.Exception exception, Sentry.ISpan span);
@@ -826,6 +827,7 @@ namespace Sentry
826827
public static class SentrySdk
827828
{
828829
public static bool IsEnabled { get; }
830+
public static bool IsSessionActive { get; }
829831
public static Sentry.SentryId LastEventId { get; }
830832
public static Sentry.SentryStructuredLogger Logger { get; }
831833
public static void AddBreadcrumb(Sentry.Breadcrumb breadcrumb, Sentry.SentryHint? hint = null) { }
@@ -1370,6 +1372,7 @@ namespace Sentry.Extensibility
13701372
{
13711373
public static readonly Sentry.Extensibility.DisabledHub Instance;
13721374
public bool IsEnabled { get; }
1375+
public bool IsSessionActive { get; }
13731376
public Sentry.SentryId LastEventId { get; }
13741377
public Sentry.SentryStructuredLogger Logger { get; }
13751378
public void BindClient(Sentry.ISentryClient client) { }
@@ -1416,6 +1419,7 @@ namespace Sentry.Extensibility
14161419
{
14171420
public static readonly Sentry.Extensibility.HubAdapter Instance;
14181421
public bool IsEnabled { get; }
1422+
public bool IsSessionActive { get; }
14191423
public Sentry.SentryId LastEventId { get; }
14201424
public Sentry.SentryStructuredLogger Logger { get; }
14211425
public void AddBreadcrumb(string message, string? category = null, string? type = null, System.Collections.Generic.IDictionary<string, string>? data = null, Sentry.BreadcrumbLevel level = 0) { }

test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ namespace Sentry
174174
}
175175
public interface IHub : Sentry.ISentryClient, Sentry.ISentryScopeManager
176176
{
177+
bool IsSessionActive { get; }
177178
Sentry.SentryId LastEventId { get; }
178179
Sentry.SentryStructuredLogger Logger { get; }
179180
void BindException(System.Exception exception, Sentry.ISpan span);
@@ -802,6 +803,7 @@ namespace Sentry
802803
public static class SentrySdk
803804
{
804805
public static bool IsEnabled { get; }
806+
public static bool IsSessionActive { get; }
805807
public static Sentry.SentryId LastEventId { get; }
806808
public static Sentry.SentryStructuredLogger Logger { get; }
807809
public static void AddBreadcrumb(Sentry.Breadcrumb breadcrumb, Sentry.SentryHint? hint = null) { }
@@ -1346,6 +1348,7 @@ namespace Sentry.Extensibility
13461348
{
13471349
public static readonly Sentry.Extensibility.DisabledHub Instance;
13481350
public bool IsEnabled { get; }
1351+
public bool IsSessionActive { get; }
13491352
public Sentry.SentryId LastEventId { get; }
13501353
public Sentry.SentryStructuredLogger Logger { get; }
13511354
public void BindClient(Sentry.ISentryClient client) { }
@@ -1392,6 +1395,7 @@ namespace Sentry.Extensibility
13921395
{
13931396
public static readonly Sentry.Extensibility.HubAdapter Instance;
13941397
public bool IsEnabled { get; }
1398+
public bool IsSessionActive { get; }
13951399
public Sentry.SentryId LastEventId { get; }
13961400
public Sentry.SentryStructuredLogger Logger { get; }
13971401
public void AddBreadcrumb(string message, string? category = null, string? type = null, System.Collections.Generic.IDictionary<string, string>? data = null, Sentry.BreadcrumbLevel level = 0) { }

0 commit comments

Comments
 (0)