-
-
Notifications
You must be signed in to change notification settings - Fork 56
Feat/native scope observer #546
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a5e02b
wip: setting native context
vaind 1151a53
refactor: extract ScopeObserver with the common code; finish the nati…
vaind 9b6063f
refactor: (vol 2) make ScopeObserver a base abstract class
vaind 4738c97
fix: AndroidJavaScopeObserver regressions
vaind 9447913
Update src/Sentry.Unity.Native/SentryNativeBridge.cs
vaind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using Sentry.Extensibility; | ||
using UnityEngine; | ||
|
||
namespace Sentry.Unity.Android | ||
{ | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -1,59 +1,104 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Sentry.Extensibility; | ||
using Sentry.Unity.Json; | ||
using UnityEngine; | ||
|
||
namespace Sentry.Unity.Native | ||
namespace Sentry.Unity | ||
{ | ||
/// <summary> | ||
/// Scope Observer for Native through P/Invoke. | ||
/// </summary> | ||
/// <see href="https://github.com/getsentry/sentry-native"/> | ||
public class NativeScopeObserver : IScopeObserver | ||
public class NativeScopeObserver : ScopeObserver | ||
{ | ||
private readonly SentryOptions _options; | ||
public NativeScopeObserver(SentryOptions options) : base("Native", options) { } | ||
|
||
public NativeScopeObserver(SentryOptions options) => _options = options; | ||
|
||
public void AddBreadcrumb(Breadcrumb breadcrumb) | ||
public override void AddBreadcrumbImpl(Breadcrumb breadcrumb) | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Adding breadcrumb m:\"{0}\" l:\"{1}\"", | ||
breadcrumb.Message, | ||
breadcrumb.Level); | ||
// TODO implement | ||
// see https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/ | ||
var crumb = sentry_value_new_breadcrumb(breadcrumb.Type, breadcrumb.Message); | ||
sentry_value_set_by_key(crumb, "level", sentry_value_new_string(breadcrumb.Level.ToString().ToLower())); | ||
sentry_value_set_by_key(crumb, "timestamp", sentry_value_new_string(GetTimestamp(breadcrumb.Timestamp))); | ||
nativeSetValueIfNotNull(crumb, "category", breadcrumb.Category); | ||
sentry_add_breadcrumb(crumb); | ||
} | ||
|
||
public void SetExtra(string key, object? value) | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Setting Extra k:\"{0}\" v:\"{1}\"", key, value); | ||
// TODO implement | ||
} | ||
public override void SetExtraImpl(string key, string? value) => | ||
sentry_set_extra(key, value is null ? sentry_value_new_null() : sentry_value_new_string(value)); | ||
|
||
public void SetTag(string key, string value) | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Setting Tag k:\"{0}\" v:\"{1}\"", key, value); | ||
// TODO implement | ||
} | ||
public override void SetTagImpl(string key, string value) => sentry_set_tag(key, value); | ||
|
||
public override void UnsetTagImpl(string key) => sentry_remove_tag(key); | ||
|
||
public void UnsetTag(string key) | ||
public override void SetUserImpl(User user) | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Unsetting Tag k:\"{0}\"", key); | ||
// TODO implement | ||
// see https://develop.sentry.dev/sdk/event-payloads/user/ | ||
var cUser = sentry_value_new_object(); | ||
nativeSetValueIfNotNull(cUser, "id", user.Id); | ||
nativeSetValueIfNotNull(cUser, "username", user.Username); | ||
nativeSetValueIfNotNull(cUser, "email", user.Email); | ||
nativeSetValueIfNotNull(cUser, "ip_address", user.IpAddress); | ||
sentry_set_user(cUser); | ||
} | ||
|
||
public void SetUser(User? user) | ||
public override void UnsetUserImpl() => sentry_remove_user(); | ||
|
||
[DllImport("sentry")] | ||
private static extern SentryValueU sentry_value_new_object(); | ||
|
||
[DllImport("sentry")] | ||
private static extern SentryValueU sentry_value_new_null(); | ||
|
||
[DllImport("sentry")] | ||
private static extern SentryValueU sentry_value_new_string(string value); | ||
|
||
[DllImport("sentry")] | ||
private static extern SentryValueU sentry_value_new_breadcrumb(string? type, string? message); | ||
|
||
[DllImport("sentry")] | ||
private static extern int sentry_value_set_by_key(SentryValueU value, string k, SentryValueU v); | ||
|
||
private static void nativeSetValueIfNotNull(SentryValueU obj, string key, string? value) | ||
{ | ||
if (user is not null) | ||
if (value is not null) | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Setting User i:\"{0}\" n:\"{1}\"", | ||
user.Id, | ||
user.Username); | ||
// TODO implement | ||
} | ||
else | ||
{ | ||
_options.DiagnosticLogger?.LogDebug("Native Scope Sync - Unsetting User"); | ||
// TODO implement | ||
sentry_value_set_by_key(obj, key, sentry_value_new_string(value)); | ||
} | ||
} | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_add_breadcrumb(SentryValueU breadcrumb); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_set_tag(string key, string value); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_remove_tag(string key); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_set_user(SentryValueU user); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_remove_user(); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_set_extra(string key, SentryValueU value); | ||
|
||
[DllImport("sentry")] | ||
private static extern void sentry_remove_extra(string key); | ||
|
||
// native union sentry_value_u/t | ||
[StructLayout(LayoutKind.Explicit)] | ||
private struct SentryValueU | ||
{ | ||
[FieldOffset(0)] | ||
private ulong _bits; | ||
[FieldOffset(0)] | ||
private double _double; | ||
} | ||
|
||
private static string GetTimestamp(DateTimeOffset timestamp) => | ||
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly. | ||
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip | ||
timestamp.ToString("o"); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.