-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AndroidScopeObserver * Update CHANGELOG.md
- Loading branch information
1 parent
36ccacf
commit 610dee8
Showing
5 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Sentry.Android.Extensions; | ||
using Sentry.Extensibility; | ||
|
||
namespace Sentry.Android; | ||
|
||
internal sealed class AndroidScopeObserver : IScopeObserver | ||
{ | ||
private readonly SentryOptions _options; | ||
private readonly IScopeObserver? _innerObserver; | ||
|
||
public AndroidScopeObserver(SentryOptions options) | ||
{ | ||
_options = options; | ||
|
||
// Chain any previous observer, but guard against circular reference. | ||
var observer = options.ScopeObserver; | ||
_innerObserver = observer is AndroidScopeObserver ? null : observer; | ||
} | ||
|
||
public void AddBreadcrumb(Breadcrumb breadcrumb) | ||
{ | ||
try | ||
{ | ||
var b = breadcrumb.ToJavaBreadcrumb(); | ||
Java.Sentry.AddBreadcrumb(b); | ||
} | ||
finally | ||
{ | ||
_innerObserver?.AddBreadcrumb(breadcrumb); | ||
} | ||
} | ||
|
||
public void SetExtra(string key, object? value) | ||
{ | ||
try | ||
{ | ||
if (value is null) | ||
{ | ||
_options.LogDebug("Extra with key '{0}' was null.", key); | ||
return; | ||
} | ||
|
||
if (value is string s) | ||
{ | ||
Java.Sentry.SetExtra(key, s); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var json = JsonSerializer.Serialize(value); | ||
Java.Sentry.SetExtra(key, json); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_options.LogError("Extra with key '{0}' could not be serialized.", ex, key); | ||
} | ||
} | ||
finally | ||
{ | ||
_innerObserver?.SetExtra(key, value); | ||
} | ||
} | ||
|
||
public void SetTag(string key, string value) | ||
{ | ||
try | ||
{ | ||
Java.Sentry.SetTag(key, value); | ||
} | ||
finally | ||
{ | ||
_innerObserver?.SetTag(key, value); | ||
} | ||
} | ||
|
||
public void UnsetTag(string key) | ||
{ | ||
try | ||
{ | ||
Java.Sentry.RemoveTag(key); | ||
} | ||
finally | ||
{ | ||
_innerObserver?.UnsetTag(key); | ||
} | ||
} | ||
|
||
public void SetUser(User? user) | ||
{ | ||
try | ||
{ | ||
var u = user?.ToJavaUser(); | ||
Java.Sentry.SetUser(u); | ||
} | ||
finally | ||
{ | ||
_innerObserver?.SetUser(user); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections.ObjectModel; | ||
|
||
namespace Sentry.Android.Extensions; | ||
|
||
internal static class UserExtensions | ||
{ | ||
private static readonly IDictionary<string, string> EmptyDictionary = | ||
new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()); | ||
|
||
public static User ToUser(this Java.Protocol.User user) => | ||
new() | ||
{ | ||
Email = user.Email, | ||
Id = user.Id, | ||
IpAddress = user.IpAddress, | ||
Username = user.Username, | ||
Other = user.Others ?? EmptyDictionary | ||
}; | ||
|
||
public static Java.Protocol.User ToJavaUser(this User user) => | ||
new() | ||
{ | ||
Email = user.Email, | ||
Id = user.Id, | ||
IpAddress = user.IpAddress, | ||
Username = user.Username, | ||
Others = user.Other.Count == 0 ? null : user.Other | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters