Skip to content

Commit cfff60b

Browse files
committed
feat(event) added DeliveryStrategy to delivery for event
1 parent e4155f1 commit cfff60b

File tree

6 files changed

+80
-18
lines changed

6 files changed

+80
-18
lines changed

bugsnag-android-core/api/bugsnag-android-core.api

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ public final class com/bugsnag/android/DeliveryStatus$Companion {
275275
public final fun forHttpResponseCode (I)Lcom/bugsnag/android/DeliveryStatus;
276276
}
277277

278+
public final class com/bugsnag/android/DeliveryStrategy : java/lang/Enum {
279+
public static final field SEND_IMMEDIATELY Lcom/bugsnag/android/DeliveryStrategy;
280+
public static final field STORE_AND_FLUSH Lcom/bugsnag/android/DeliveryStrategy;
281+
public static final field STORE_AND_SEND Lcom/bugsnag/android/DeliveryStrategy;
282+
public static final field STORE_ONLY Lcom/bugsnag/android/DeliveryStrategy;
283+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
284+
public static fun valueOf (Ljava/lang/String;)Lcom/bugsnag/android/DeliveryStrategy;
285+
public static fun values ()[Lcom/bugsnag/android/DeliveryStrategy;
286+
}
287+
278288
public class com/bugsnag/android/Device : com/bugsnag/android/JsonStream$Streamable {
279289
public final fun getCpuAbi ()[Ljava/lang/String;
280290
public final fun getId ()Ljava/lang/String;
@@ -392,6 +402,7 @@ public class com/bugsnag/android/Event : com/bugsnag/android/FeatureFlagAware, c
392402
public fun getApp ()Lcom/bugsnag/android/AppWithState;
393403
public fun getBreadcrumbs ()Ljava/util/List;
394404
public fun getContext ()Ljava/lang/String;
405+
public fun getDeliveryStrategy ()Lcom/bugsnag/android/DeliveryStrategy;
395406
public fun getDevice ()Lcom/bugsnag/android/DeviceWithState;
396407
public fun getErrors ()Ljava/util/List;
397408
public fun getFeatureFlags ()Ljava/util/List;
@@ -408,6 +419,7 @@ public class com/bugsnag/android/Event : com/bugsnag/android/FeatureFlagAware, c
408419
public fun leaveBreadcrumb (Ljava/lang/String;Lcom/bugsnag/android/BreadcrumbType;Ljava/util/Map;)Lcom/bugsnag/android/Breadcrumb;
409420
public fun setApiKey (Ljava/lang/String;)V
410421
public fun setContext (Ljava/lang/String;)V
422+
public fun setDeliveryStrategy (Lcom/bugsnag/android/DeliveryStrategy;)V
411423
public fun setErrorReportingThread (J)V
412424
public fun setErrorReportingThread (Lcom/bugsnag/android/Thread;)V
413425
public fun setGroupingDiscriminator (Ljava/lang/String;)Ljava/lang/String;

bugsnag-android-core/src/main/java/com/bugsnag/android/DeliveryDelegate.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,44 @@ void deliver(@NonNull Event event) {
5454
}
5555
}
5656

57-
if (event.getImpl().getOriginalUnhandled()) {
58-
// should only send unhandled errors if they don't terminate the process (i.e. ANRs)
59-
String severityReasonType = event.getImpl().getSeverityReasonType();
60-
boolean promiseRejection = REASON_PROMISE_REJECTION.equals(severityReasonType);
61-
boolean anr = event.getImpl().isAnr(event);
62-
if (anr || promiseRejection) {
63-
cacheEvent(event, true);
64-
} else if (immutableConfig.getAttemptDeliveryOnCrash()) {
65-
cacheAndSendSynchronously(event);
57+
DeliveryStrategy deliveryStrategy = null;
58+
if (event.getDeliveryStrategy() == null) {
59+
if (event.getImpl().getOriginalUnhandled()) {
60+
String severityReasonType = event.getImpl().getSeverityReasonType();
61+
boolean promiseRejection = REASON_PROMISE_REJECTION.equals(severityReasonType);
62+
boolean anr = event.getImpl().isAnr(event);
63+
if (anr || promiseRejection) {
64+
deliveryStrategy = DeliveryStrategy.STORE_AND_FLUSH;
65+
} else if (immutableConfig.getAttemptDeliveryOnCrash()) {
66+
deliveryStrategy = DeliveryStrategy.STORE_AND_SEND;
67+
} else {
68+
deliveryStrategy = DeliveryStrategy.STORE_ONLY;
69+
}
6670
} else {
67-
cacheEvent(event, false);
71+
deliveryStrategy = DeliveryStrategy.SEND_IMMEDIATELY;
72+
}
73+
}
74+
75+
if (deliveryStrategy != null) {
76+
event.setDeliveryStrategy(deliveryStrategy);
77+
switch (deliveryStrategy) {
78+
case STORE_AND_FLUSH:
79+
cacheEvent(event, true);
80+
break;
81+
case STORE_AND_SEND:
82+
cacheAndSendSynchronously(event);
83+
break;
84+
case STORE_ONLY:
85+
cacheEvent(event, false);
86+
break;
87+
case SEND_IMMEDIATELY:
88+
if (callbackState.runOnSendTasks(event, logger)) {
89+
String apiKey = event.getApiKey();
90+
EventPayload eventPayload = new EventPayload(apiKey, event, notifier, immutableConfig);
91+
deliverPayloadAsync(event, eventPayload);
92+
}
93+
break;
6894
}
69-
} else if (callbackState.runOnSendTasks(event, logger)) {
70-
// Build the eventPayload
71-
String apiKey = event.getApiKey();
72-
EventPayload eventPayload = new EventPayload(apiKey, event, notifier, immutableConfig);
73-
deliverPayloadAsync(event, eventPayload);
7495
}
7596
}
7697

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.bugsnag.android
2+
3+
enum class DeliveryStrategy {
4+
STORE_ONLY,
5+
STORE_AND_FLUSH,
6+
STORE_AND_SEND,
7+
SEND_IMMEDIATELY,
8+
}

bugsnag-android-core/src/main/java/com/bugsnag/android/Event.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Event implements JsonStream.Streamable, MetadataAware, UserAware, F
2323

2424
private final EventInternal impl;
2525
private final Logger logger;
26+
private DeliveryStrategy deliveryStrategy;
2627

2728
Event(@Nullable Throwable originalError,
2829
@NonNull ImmutableConfig config,
@@ -575,4 +576,17 @@ void setRedactedKeys(Collection<Pattern> redactedKeys) {
575576
void setInternalMetrics(InternalMetrics metrics) {
576577
impl.setInternalMetrics(metrics);
577578
}
579+
580+
@Nullable
581+
public DeliveryStrategy getDeliveryStrategy() {
582+
return deliveryStrategy;
583+
}
584+
585+
public void setDeliveryStrategy(@NonNull DeliveryStrategy deliveryStrategy) {
586+
if (deliveryStrategy != null) {
587+
this.deliveryStrategy = deliveryStrategy;
588+
} else {
589+
logNull("Invalid deliveryStrategy");
590+
}
591+
}
578592
}

bugsnag-android-core/src/test/java/com/bugsnag/android/DeliveryDelegateTest.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ internal class DeliveryDelegateTest {
4242
notifier,
4343
BackgroundTaskService()
4444
)
45-
event.session = Session("123", Date(), User(null, null, null), false, notifier, NoopLogger, apiKey)
45+
event.session =
46+
Session("123", Date(), User(null, null, null), false, notifier, NoopLogger, apiKey)
4647
}
4748

4849
@Test
@@ -63,6 +64,8 @@ internal class DeliveryDelegateTest {
6364
assertEquals(0, event.session!!.handledCount)
6465

6566
assertEquals("BUGSNAG_API_KEY", event.session!!.apiKey)
67+
68+
assertEquals(DeliveryStrategy.STORE_ONLY, event.deliveryStrategy)
6669
}
6770

6871
@Test
@@ -71,7 +74,8 @@ internal class DeliveryDelegateTest {
7174
SeverityReason.REASON_HANDLED_EXCEPTION
7275
)
7376
val event = Event(RuntimeException("Whoops!"), config, state, NoopLogger)
74-
event.session = Session("123", Date(), User(null, null, null), false, notifier, NoopLogger, apiKey)
77+
event.session =
78+
Session("123", Date(), User(null, null, null), false, notifier, NoopLogger, apiKey)
7579

7680
var msg: StateEvent.NotifyHandled? = null
7781
deliveryDelegate.addObserver(
@@ -87,6 +91,8 @@ internal class DeliveryDelegateTest {
8791
// check session count incremented
8892
assertEquals(0, event.session!!.unhandledCount)
8993
assertEquals(1, event.session!!.handledCount)
94+
95+
assertEquals(DeliveryStrategy.SEND_IMMEDIATELY, event.deliveryStrategy)
9096
}
9197

9298
@Test
@@ -107,6 +113,8 @@ internal class DeliveryDelegateTest {
107113

108114
// verify no payload was sent for an Event with no errors
109115
assertNull(msg)
116+
117+
assertEquals(DeliveryStrategy.SEND_IMMEDIATELY, event.deliveryStrategy)
110118
}
111119

112120
@Test

bugsnag-plugin-android-exitinfo/detekt-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<CurrentIssues>
55
<ID>CyclomaticComplexMethod:CodeDescriptions.kt$@RequiresApi(Build.VERSION_CODES.R) @SuppressLint("SwitchIntDef") @Suppress("DEPRECATION") internal fun importanceDescriptionOf(exitInfo: ApplicationExitInfo)</ID>
66
<ID>CyclomaticComplexMethod:CodeDescriptions.kt$@RequiresApi(Build.VERSION_CODES.R) internal fun exitReasonOf(exitInfo: ApplicationExitInfo)</ID>
7-
<ID>LongParameterList:TombstoneParser.kt$TombstoneParser$( exitInfo: ApplicationExitInfo, listOpenFds: Boolean, includeLogcat: Boolean, threadConsumer: (BugsnagThread) -> Unit, fileDescriptorConsumer: (Int, String, String) -> Unit, logcatConsumer: (String) -> Unit )</ID>
87
<ID>MagicNumber:TraceParser.kt$TraceParser$16</ID>
98
<ID>MagicNumber:TraceParser.kt$TraceParser$3</ID>
109
<ID>MaxLineLength:TraceParserInvalidStackframesTest.kt$TraceParserInvalidStackframesTest.Companion$"#01 pc 0000000000000c5c /data/app/~~sKQbJGqVJA5glcnvEjZCMg==/com.example.bugsnag.android-fVuoJh5GpAL7sRAeI3vjSw==/lib/arm64/libentrypoint.so "</ID>

0 commit comments

Comments
 (0)