Skip to content

Commit 89cad6d

Browse files
Refactor and update Android options (#1705)
* Refactor and update Android options * Update CHANGELOG.md
1 parent 77a03e2 commit 89cad6d

23 files changed

+786
-660
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Update Sentry-Android to 6.0.0-rc.1 ([#1686](https://github.com/getsentry/sentry-dotnet/pull/1686))
2626
- Update Sentry-Android to 6.0.0 ([#1697](https://github.com/getsentry/sentry-dotnet/pull/1697))
2727
- Set Java/Android SDK options ([#1694](https://github.com/getsentry/sentry-dotnet/pull/1694))
28+
- Refactor and update Android options ([#1705](https://github.com/getsentry/sentry-dotnet/pull/1705))
2829

2930
### Fixes
3031

SentryMaui.slnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"solution": {
33
"path": "Sentry.sln",
44
"projects": [
5+
"samples\\Sentry.Samples.Android\\Sentry.Samples.Android.csproj",
56
"samples\\Sentry.Samples.Maui\\Sentry.Samples.Maui.csproj",
67
"src\\Sentry.Extensions.Logging\\Sentry.Extensions.Logging.csproj",
78
"src\\Sentry.Maui\\Sentry.Maui.csproj",

src/Sentry/Android/BeforeBreadcrumbCallback.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Sentry/Android/BeforeSendCallback.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Sentry.Android.Extensions;
2+
3+
namespace Sentry.Android.Callbacks;
4+
5+
internal class BeforeBreadcrumbCallback : JavaObject, Java.SentryOptions.IBeforeBreadcrumbCallback
6+
{
7+
private readonly Func<Breadcrumb, Breadcrumb?> _beforeBreadcrumb;
8+
9+
public BeforeBreadcrumbCallback(Func<Breadcrumb, Breadcrumb?> beforeBreadcrumb)
10+
{
11+
_beforeBreadcrumb = beforeBreadcrumb;
12+
}
13+
14+
public Java.Breadcrumb? Execute(Java.Breadcrumb b, Java.Hint h)
15+
{
16+
// Note: Hint is unused due to:
17+
// https://github.com/getsentry/sentry-dotnet/issues/1469
18+
19+
var breadcrumb = b.ToBreadcrumb();
20+
var result = _beforeBreadcrumb.Invoke(breadcrumb);
21+
22+
if (result == breadcrumb)
23+
{
24+
// The result is the same object as was input, and all properties are immutable,
25+
// so we can return the original Java object for better performance.
26+
return b;
27+
}
28+
29+
return result?.ToJavaBreadcrumb();
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Sentry.Android.Extensions;
2+
using Sentry.Extensibility;
3+
4+
namespace Sentry.Android.Callbacks;
5+
6+
internal class BeforeSendCallback : JavaObject, Java.SentryOptions.IBeforeSendCallback
7+
{
8+
private readonly Func<SentryEvent, SentryEvent?> _beforeSend;
9+
private readonly SentryOptions _options;
10+
private readonly Java.SentryOptions _javaOptions;
11+
12+
public BeforeSendCallback(
13+
Func<SentryEvent, SentryEvent?> beforeSend,
14+
SentryOptions options,
15+
Java.SentryOptions javaOptions)
16+
{
17+
_beforeSend = beforeSend;
18+
_options = options;
19+
_javaOptions = javaOptions;
20+
}
21+
22+
public Java.SentryEvent? Execute(Java.SentryEvent e, Java.Hint h)
23+
{
24+
// Note: Hint is unused due to:
25+
// https://github.com/getsentry/sentry-dotnet/issues/1469
26+
27+
var evnt = e.ToSentryEvent(_javaOptions);
28+
var result = _beforeSend.Invoke(evnt);
29+
return result?.ToJavaSentryEvent(_options, _javaOptions);
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Sentry.Android.Callbacks;
2+
3+
internal class OptionsConfigurationCallback : JavaObject, Java.Sentry.IOptionsConfiguration
4+
{
5+
private readonly Action<SentryAndroidOptions> _configureOptions;
6+
7+
public OptionsConfigurationCallback(Action<SentryAndroidOptions> configureOptions) =>
8+
_configureOptions = configureOptions;
9+
10+
public void Configure(JavaObject optionsObject)
11+
{
12+
var options = (SentryAndroidOptions)optionsObject;
13+
_configureOptions(options);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Sentry.Android.Extensions;
2+
3+
namespace Sentry.Android.Callbacks;
4+
5+
internal class TracesSamplerCallback : JavaObject, Java.SentryOptions.ITracesSamplerCallback
6+
{
7+
private readonly Func<TransactionSamplingContext, double?> _tracesSampler;
8+
9+
public TracesSamplerCallback(Func<TransactionSamplingContext, double?> tracesSampler)
10+
{
11+
_tracesSampler = tracesSampler;
12+
}
13+
14+
public JavaDouble? Sample(Java.SamplingContext c)
15+
{
16+
var context = c.ToTransactionSamplingContext();
17+
return (JavaDouble?)_tracesSampler.Invoke(context);
18+
}
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Sentry.Android.Extensions;
2+
3+
internal static class BreadcrumbExtensions
4+
{
5+
public static Breadcrumb ToBreadcrumb(this Java.Breadcrumb breadcrumb)
6+
{
7+
var data = breadcrumb.Data.ToDictionary(x => x.Key, x => x.Value?.ToString() ?? "");
8+
9+
return new(breadcrumb.Timestamp.ToDateTimeOffset(),
10+
breadcrumb.Message,
11+
breadcrumb.Type,
12+
data,
13+
breadcrumb.Category,
14+
breadcrumb.Level?.ToBreadcrumbLevel() ?? default);
15+
}
16+
17+
public static Java.Breadcrumb ToJavaBreadcrumb(this Breadcrumb breadcrumb)
18+
{
19+
var javaBreadcrumb = new Java.Breadcrumb(breadcrumb.Timestamp.ToJavaDate())
20+
{
21+
Message = breadcrumb.Message,
22+
Type = breadcrumb.Type,
23+
Category = breadcrumb.Category,
24+
Level = breadcrumb.Level.ToJavaSentryLevel()
25+
};
26+
27+
if (breadcrumb.Data is { } data)
28+
{
29+
var javaData = javaBreadcrumb.Data;
30+
foreach (var item in data)
31+
{
32+
var value = item.Value ?? "";
33+
javaData.Add(item.Key, value!);
34+
}
35+
}
36+
37+
return javaBreadcrumb;
38+
}
39+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace Sentry.Android.Extensions;
2+
3+
internal static class EnumExtensions
4+
{
5+
public static SentryLevel ToSentryLevel(this Java.SentryLevel level) =>
6+
level.Name() switch
7+
{
8+
"DEBUG" => SentryLevel.Debug,
9+
"INFO" => SentryLevel.Info,
10+
"WARNING" => SentryLevel.Warning,
11+
"ERROR" => SentryLevel.Error,
12+
"FATAL" => SentryLevel.Fatal,
13+
_ => throw new ArgumentOutOfRangeException(nameof(level), level.Name(), message: default)
14+
};
15+
16+
public static Java.SentryLevel ToJavaSentryLevel(this SentryLevel level) =>
17+
level switch
18+
{
19+
SentryLevel.Debug => Java.SentryLevel.Debug!,
20+
SentryLevel.Info => Java.SentryLevel.Info!,
21+
SentryLevel.Warning => Java.SentryLevel.Warning!,
22+
SentryLevel.Error => Java.SentryLevel.Error!,
23+
SentryLevel.Fatal => Java.SentryLevel.Fatal!,
24+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, message: default)
25+
};
26+
27+
public static BreadcrumbLevel ToBreadcrumbLevel(this Java.SentryLevel level) =>
28+
level.Name() switch
29+
{
30+
"DEBUG" => BreadcrumbLevel.Debug,
31+
"INFO" => BreadcrumbLevel.Info,
32+
"WARNING" => BreadcrumbLevel.Warning,
33+
"ERROR" => BreadcrumbLevel.Error,
34+
"FATAL" => BreadcrumbLevel.Critical,
35+
_ => throw new ArgumentOutOfRangeException(nameof(level), level.Name(), message: default)
36+
};
37+
38+
public static Java.SentryLevel ToJavaSentryLevel(this BreadcrumbLevel level) =>
39+
level switch
40+
{
41+
BreadcrumbLevel.Debug => Java.SentryLevel.Debug!,
42+
BreadcrumbLevel.Info => Java.SentryLevel.Info!,
43+
BreadcrumbLevel.Warning => Java.SentryLevel.Warning!,
44+
BreadcrumbLevel.Error => Java.SentryLevel.Error!,
45+
BreadcrumbLevel.Critical => Java.SentryLevel.Fatal!,
46+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, message: default)
47+
};
48+
49+
public static SpanStatus ToSpanStatus(this Java.SpanStatus status) =>
50+
status.Name() switch
51+
{
52+
"OK" => SpanStatus.Ok,
53+
"CANCELLED" => SpanStatus.Cancelled,
54+
"INTERNAL_ERROR" => SpanStatus.InternalError,
55+
"UNKNOWN" => SpanStatus.UnknownError,
56+
"UNKNOWN_ERROR" => SpanStatus.UnknownError,
57+
"INVALID_ARGUMENT" => SpanStatus.InvalidArgument,
58+
"DEADLINE_EXCEEDED" => SpanStatus.DeadlineExceeded,
59+
"NOT_FOUND" => SpanStatus.NotFound,
60+
"ALREADY_EXISTS" => SpanStatus.AlreadyExists,
61+
"PERMISSION_DENIED" => SpanStatus.PermissionDenied,
62+
"RESOURCE_EXHAUSTED" => SpanStatus.ResourceExhausted,
63+
"FAILED_PRECONDITION" => SpanStatus.FailedPrecondition,
64+
"ABORTED" => SpanStatus.Aborted,
65+
"OUT_OF_RANGE" => SpanStatus.OutOfRange,
66+
"UNIMPLEMENTED" => SpanStatus.Unimplemented,
67+
"UNAVAILABLE" => SpanStatus.Unavailable,
68+
"DATA_LOSS" => SpanStatus.DataLoss,
69+
"UNAUTHENTICATED" => SpanStatus.Unauthenticated,
70+
_ => throw new ArgumentOutOfRangeException(nameof(status), status.Name(), message: default)
71+
};
72+
73+
public static Java.SpanStatus ToJavaSpanStatus(this SpanStatus status) =>
74+
status switch
75+
{
76+
SpanStatus.Ok => Java.SpanStatus.Ok!,
77+
SpanStatus.Cancelled => Java.SpanStatus.Cancelled!,
78+
SpanStatus.InternalError => Java.SpanStatus.InternalError!,
79+
SpanStatus.UnknownError => Java.SpanStatus.UnknownError!,
80+
SpanStatus.InvalidArgument => Java.SpanStatus.InvalidArgument!,
81+
SpanStatus.DeadlineExceeded => Java.SpanStatus.DeadlineExceeded!,
82+
SpanStatus.NotFound => Java.SpanStatus.NotFound!,
83+
SpanStatus.AlreadyExists => Java.SpanStatus.AlreadyExists!,
84+
SpanStatus.PermissionDenied => Java.SpanStatus.PermissionDenied!,
85+
SpanStatus.ResourceExhausted => Java.SpanStatus.ResourceExhausted!,
86+
SpanStatus.FailedPrecondition => Java.SpanStatus.FailedPrecondition!,
87+
SpanStatus.Aborted => Java.SpanStatus.Aborted!,
88+
SpanStatus.OutOfRange => Java.SpanStatus.OutOfRange!,
89+
SpanStatus.Unimplemented => Java.SpanStatus.Unimplemented!,
90+
SpanStatus.Unavailable => Java.SpanStatus.Unavailable!,
91+
SpanStatus.DataLoss => Java.SpanStatus.DataLoss!,
92+
SpanStatus.Unauthenticated => Java.SpanStatus.Unauthenticated!,
93+
_ => throw new ArgumentOutOfRangeException(nameof(status), status, message: default)
94+
};
95+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Sentry.Android.Extensions;
2+
3+
internal static class JavaExtensions
4+
{
5+
public static DateTimeOffset ToDateTimeOffset(this JavaDate timestamp) => DateTimeOffset.FromUnixTimeMilliseconds(timestamp.Time);
6+
7+
public static JavaDate ToJavaDate(this DateTimeOffset timestamp) => new(timestamp.ToUnixTimeMilliseconds());
8+
9+
public static Exception ToException(this Throwable throwable) => Throwable.ToException(throwable);
10+
11+
public static Throwable ToThrowable(this Exception exception) => Throwable.FromException(exception);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Sentry.Android.Extensions;
2+
3+
internal static class SMiscExtensions
4+
{
5+
public static SentryId ToSentryId(this Java.Protocol.SentryId sentryId) => new(Guid.Parse(sentryId.ToString()));
6+
7+
public static Java.Protocol.SentryId ToJavaSentryId(this SentryId sentryId) => new(sentryId.ToString());
8+
9+
public static SpanId ToSpanId(this Java.SpanId spanId) => new(spanId.ToString());
10+
11+
public static Java.SpanId ToJavaSpanId(this SpanId spanId) => new(spanId.ToString());
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Sentry.Android.Facades;
2+
3+
namespace Sentry.Android.Extensions;
4+
5+
internal static class SamplingContextExtensions
6+
{
7+
private static readonly Dictionary<string, object?> EmptyObjectDictionary = new();
8+
9+
public static TransactionSamplingContext ToTransactionSamplingContext(this Java.SamplingContext context)
10+
{
11+
var transactionContext = new TransactionContextFacade(context.TransactionContext);
12+
13+
//var customSamplingContext = context.CustomSamplingContext?.Data
14+
// .ToDictionary(x => x.Key, x => (object?)x.Value) ?? EmptyObjectDictionary;
15+
16+
var customSamplingContext = ((IReadOnlyDictionary<string, object?>?)context.CustomSamplingContext?.Data)
17+
?? EmptyObjectDictionary;
18+
19+
return new(transactionContext, customSamplingContext);
20+
}
21+
}

0 commit comments

Comments
 (0)