Skip to content
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

Refactor and update Android options #1705

Merged
merged 2 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -25,6 +25,7 @@
- Update Sentry-Android to 6.0.0-rc.1 ([#1686](https://github.com/getsentry/sentry-dotnet/pull/1686))
- Update Sentry-Android to 6.0.0 ([#1697](https://github.com/getsentry/sentry-dotnet/pull/1697))
- Set Java/Android SDK options ([#1694](https://github.com/getsentry/sentry-dotnet/pull/1694))
- Refactor and update Android options ([#1705](https://github.com/getsentry/sentry-dotnet/pull/1705))

### Fixes

Expand Down
1 change: 1 addition & 0 deletions SentryMaui.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"solution": {
"path": "Sentry.sln",
"projects": [
"samples\\Sentry.Samples.Android\\Sentry.Samples.Android.csproj",
"samples\\Sentry.Samples.Maui\\Sentry.Samples.Maui.csproj",
"src\\Sentry.Extensions.Logging\\Sentry.Extensions.Logging.csproj",
"src\\Sentry.Maui\\Sentry.Maui.csproj",
Expand Down
31 changes: 0 additions & 31 deletions src/Sentry/Android/BeforeBreadcrumbCallback.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/Sentry/Android/BeforeSendCallback.cs

This file was deleted.

31 changes: 31 additions & 0 deletions src/Sentry/Android/Callbacks/BeforeBreadcrumbCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Sentry.Android.Extensions;

namespace Sentry.Android.Callbacks;

internal class BeforeBreadcrumbCallback : JavaObject, Java.SentryOptions.IBeforeBreadcrumbCallback
{
private readonly Func<Breadcrumb, Breadcrumb?> _beforeBreadcrumb;

public BeforeBreadcrumbCallback(Func<Breadcrumb, Breadcrumb?> beforeBreadcrumb)
{
_beforeBreadcrumb = beforeBreadcrumb;
}

public Java.Breadcrumb? Execute(Java.Breadcrumb b, Java.Hint h)
{
// Note: Hint is unused due to:
// https://github.com/getsentry/sentry-dotnet/issues/1469

var breadcrumb = b.ToBreadcrumb();
var result = _beforeBreadcrumb.Invoke(breadcrumb);

if (result == breadcrumb)
{
// The result is the same object as was input, and all properties are immutable,
// so we can return the original Java object for better performance.
return b;
}

return result?.ToJavaBreadcrumb();
}
}
31 changes: 31 additions & 0 deletions src/Sentry/Android/Callbacks/BeforeSendCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Sentry.Android.Extensions;
using Sentry.Extensibility;

namespace Sentry.Android.Callbacks;

internal class BeforeSendCallback : JavaObject, Java.SentryOptions.IBeforeSendCallback
{
private readonly Func<SentryEvent, SentryEvent?> _beforeSend;
private readonly SentryOptions _options;
private readonly Java.SentryOptions _javaOptions;

public BeforeSendCallback(
Func<SentryEvent, SentryEvent?> beforeSend,
SentryOptions options,
Java.SentryOptions javaOptions)
{
_beforeSend = beforeSend;
_options = options;
_javaOptions = javaOptions;
}

public Java.SentryEvent? Execute(Java.SentryEvent e, Java.Hint h)
{
// Note: Hint is unused due to:
// https://github.com/getsentry/sentry-dotnet/issues/1469

var evnt = e.ToSentryEvent(_javaOptions);
var result = _beforeSend.Invoke(evnt);
return result?.ToJavaSentryEvent(_options, _javaOptions);
}
}
15 changes: 15 additions & 0 deletions src/Sentry/Android/Callbacks/OptionsConfigurationCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Sentry.Android.Callbacks;

internal class OptionsConfigurationCallback : JavaObject, Java.Sentry.IOptionsConfiguration
{
private readonly Action<SentryAndroidOptions> _configureOptions;

public OptionsConfigurationCallback(Action<SentryAndroidOptions> configureOptions) =>
_configureOptions = configureOptions;

public void Configure(JavaObject optionsObject)
{
var options = (SentryAndroidOptions)optionsObject;
_configureOptions(options);
}
}
19 changes: 19 additions & 0 deletions src/Sentry/Android/Callbacks/TracesSamplerCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Sentry.Android.Extensions;

namespace Sentry.Android.Callbacks;

internal class TracesSamplerCallback : JavaObject, Java.SentryOptions.ITracesSamplerCallback
{
private readonly Func<TransactionSamplingContext, double?> _tracesSampler;

public TracesSamplerCallback(Func<TransactionSamplingContext, double?> tracesSampler)
{
_tracesSampler = tracesSampler;
}

public JavaDouble? Sample(Java.SamplingContext c)
{
var context = c.ToTransactionSamplingContext();
return (JavaDouble?)_tracesSampler.Invoke(context);
}
}
39 changes: 39 additions & 0 deletions src/Sentry/Android/Extensions/BreadcrumbExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Sentry.Android.Extensions;

internal static class BreadcrumbExtensions
{
public static Breadcrumb ToBreadcrumb(this Java.Breadcrumb breadcrumb)
{
var data = breadcrumb.Data.ToDictionary(x => x.Key, x => x.Value?.ToString() ?? "");

return new(breadcrumb.Timestamp.ToDateTimeOffset(),
breadcrumb.Message,
breadcrumb.Type,
data,
breadcrumb.Category,
breadcrumb.Level?.ToBreadcrumbLevel() ?? default);
}

public static Java.Breadcrumb ToJavaBreadcrumb(this Breadcrumb breadcrumb)
{
var javaBreadcrumb = new Java.Breadcrumb(breadcrumb.Timestamp.ToJavaDate())
{
Message = breadcrumb.Message,
Type = breadcrumb.Type,
Category = breadcrumb.Category,
Level = breadcrumb.Level.ToJavaSentryLevel()
};

if (breadcrumb.Data is { } data)
{
var javaData = javaBreadcrumb.Data;
foreach (var item in data)
{
var value = item.Value ?? "";
javaData.Add(item.Key, value!);
}
}

return javaBreadcrumb;
}
}
95 changes: 95 additions & 0 deletions src/Sentry/Android/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
namespace Sentry.Android.Extensions;

internal static class EnumExtensions
{
public static SentryLevel ToSentryLevel(this Java.SentryLevel level) =>
level.Name() switch
{
"DEBUG" => SentryLevel.Debug,
"INFO" => SentryLevel.Info,
"WARNING" => SentryLevel.Warning,
"ERROR" => SentryLevel.Error,
"FATAL" => SentryLevel.Fatal,
_ => throw new ArgumentOutOfRangeException(nameof(level), level.Name(), message: default)
};

public static Java.SentryLevel ToJavaSentryLevel(this SentryLevel level) =>
level switch
{
SentryLevel.Debug => Java.SentryLevel.Debug!,
SentryLevel.Info => Java.SentryLevel.Info!,
SentryLevel.Warning => Java.SentryLevel.Warning!,
SentryLevel.Error => Java.SentryLevel.Error!,
SentryLevel.Fatal => Java.SentryLevel.Fatal!,
_ => throw new ArgumentOutOfRangeException(nameof(level), level, message: default)
};

public static BreadcrumbLevel ToBreadcrumbLevel(this Java.SentryLevel level) =>
level.Name() switch
{
"DEBUG" => BreadcrumbLevel.Debug,
"INFO" => BreadcrumbLevel.Info,
"WARNING" => BreadcrumbLevel.Warning,
"ERROR" => BreadcrumbLevel.Error,
"FATAL" => BreadcrumbLevel.Critical,
_ => throw new ArgumentOutOfRangeException(nameof(level), level.Name(), message: default)
};

public static Java.SentryLevel ToJavaSentryLevel(this BreadcrumbLevel level) =>
level switch
{
BreadcrumbLevel.Debug => Java.SentryLevel.Debug!,
BreadcrumbLevel.Info => Java.SentryLevel.Info!,
BreadcrumbLevel.Warning => Java.SentryLevel.Warning!,
BreadcrumbLevel.Error => Java.SentryLevel.Error!,
BreadcrumbLevel.Critical => Java.SentryLevel.Fatal!,
_ => throw new ArgumentOutOfRangeException(nameof(level), level, message: default)
};

public static SpanStatus ToSpanStatus(this Java.SpanStatus status) =>
status.Name() switch
{
"OK" => SpanStatus.Ok,
"CANCELLED" => SpanStatus.Cancelled,
"INTERNAL_ERROR" => SpanStatus.InternalError,
"UNKNOWN" => SpanStatus.UnknownError,
"UNKNOWN_ERROR" => SpanStatus.UnknownError,
"INVALID_ARGUMENT" => SpanStatus.InvalidArgument,
"DEADLINE_EXCEEDED" => SpanStatus.DeadlineExceeded,
"NOT_FOUND" => SpanStatus.NotFound,
"ALREADY_EXISTS" => SpanStatus.AlreadyExists,
"PERMISSION_DENIED" => SpanStatus.PermissionDenied,
"RESOURCE_EXHAUSTED" => SpanStatus.ResourceExhausted,
"FAILED_PRECONDITION" => SpanStatus.FailedPrecondition,
"ABORTED" => SpanStatus.Aborted,
"OUT_OF_RANGE" => SpanStatus.OutOfRange,
"UNIMPLEMENTED" => SpanStatus.Unimplemented,
"UNAVAILABLE" => SpanStatus.Unavailable,
"DATA_LOSS" => SpanStatus.DataLoss,
"UNAUTHENTICATED" => SpanStatus.Unauthenticated,
_ => throw new ArgumentOutOfRangeException(nameof(status), status.Name(), message: default)
};

public static Java.SpanStatus ToJavaSpanStatus(this SpanStatus status) =>
status switch
{
SpanStatus.Ok => Java.SpanStatus.Ok!,
SpanStatus.Cancelled => Java.SpanStatus.Cancelled!,
SpanStatus.InternalError => Java.SpanStatus.InternalError!,
SpanStatus.UnknownError => Java.SpanStatus.UnknownError!,
SpanStatus.InvalidArgument => Java.SpanStatus.InvalidArgument!,
SpanStatus.DeadlineExceeded => Java.SpanStatus.DeadlineExceeded!,
SpanStatus.NotFound => Java.SpanStatus.NotFound!,
SpanStatus.AlreadyExists => Java.SpanStatus.AlreadyExists!,
SpanStatus.PermissionDenied => Java.SpanStatus.PermissionDenied!,
SpanStatus.ResourceExhausted => Java.SpanStatus.ResourceExhausted!,
SpanStatus.FailedPrecondition => Java.SpanStatus.FailedPrecondition!,
SpanStatus.Aborted => Java.SpanStatus.Aborted!,
SpanStatus.OutOfRange => Java.SpanStatus.OutOfRange!,
SpanStatus.Unimplemented => Java.SpanStatus.Unimplemented!,
SpanStatus.Unavailable => Java.SpanStatus.Unavailable!,
SpanStatus.DataLoss => Java.SpanStatus.DataLoss!,
SpanStatus.Unauthenticated => Java.SpanStatus.Unauthenticated!,
_ => throw new ArgumentOutOfRangeException(nameof(status), status, message: default)
};
}
12 changes: 12 additions & 0 deletions src/Sentry/Android/Extensions/JavaExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sentry.Android.Extensions;

internal static class JavaExtensions
{
public static DateTimeOffset ToDateTimeOffset(this JavaDate timestamp) => DateTimeOffset.FromUnixTimeMilliseconds(timestamp.Time);

public static JavaDate ToJavaDate(this DateTimeOffset timestamp) => new(timestamp.ToUnixTimeMilliseconds());

public static Exception ToException(this Throwable throwable) => Throwable.ToException(throwable);

public static Throwable ToThrowable(this Exception exception) => Throwable.FromException(exception);
}
12 changes: 12 additions & 0 deletions src/Sentry/Android/Extensions/MiscExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sentry.Android.Extensions;

internal static class SMiscExtensions
{
public static SentryId ToSentryId(this Java.Protocol.SentryId sentryId) => new(Guid.Parse(sentryId.ToString()));

public static Java.Protocol.SentryId ToJavaSentryId(this SentryId sentryId) => new(sentryId.ToString());

public static SpanId ToSpanId(this Java.SpanId spanId) => new(spanId.ToString());

public static Java.SpanId ToJavaSpanId(this SpanId spanId) => new(spanId.ToString());
}
21 changes: 21 additions & 0 deletions src/Sentry/Android/Extensions/SamplingContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Sentry.Android.Facades;

namespace Sentry.Android.Extensions;

internal static class SamplingContextExtensions
{
private static readonly Dictionary<string, object?> EmptyObjectDictionary = new();

public static TransactionSamplingContext ToTransactionSamplingContext(this Java.SamplingContext context)
{
var transactionContext = new TransactionContextFacade(context.TransactionContext);

//var customSamplingContext = context.CustomSamplingContext?.Data
// .ToDictionary(x => x.Key, x => (object?)x.Value) ?? EmptyObjectDictionary;

var customSamplingContext = ((IReadOnlyDictionary<string, object?>?)context.CustomSamplingContext?.Data)
?? EmptyObjectDictionary;

return new(transactionContext, customSamplingContext);
}
}
Loading