Skip to content

Commit fa5400c

Browse files
committed
refactor(sendThreads): replaced sendThreads boolean with ThreadSendPolicy
1 parent b7ca7cf commit fa5400c

8 files changed

Lines changed: 42 additions & 21 deletions

File tree

bugsnag/src/main/java/com/bugsnag/Bugsnag.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,13 @@ public void setReleaseStage(String releaseStage) {
308308
}
309309

310310
/**
311-
* Set whether Bugsnag should capture and report thread-state for all
312-
* running threads. This is often not useful for Java web apps, since
313-
* there could be thousands of active threads depending on your
314-
* environment.
311+
* Set when Bugsnag should capture and report thread-state for all
312+
* running threads.
315313
*
316314
* @param sendThreads should we send thread state with error reports
317315
* @see #setEnabledReleaseStages
318316
*/
319-
public void setSendThreads(boolean sendThreads) {
317+
public void setSendThreads(ThreadSendPolicy sendThreads) {
320318
config.setSendThreads(sendThreads);
321319
}
322320

bugsnag/src/main/java/com/bugsnag/BugsnagAppender.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class BugsnagAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
6969
private String releaseStage;
7070

7171
/** Whether thread state should be sent to Bugsnag. */
72-
private boolean sendThreads = false;
72+
private ThreadSendPolicy sendThreads = ThreadSendPolicy.NEVER;
7373

7474
/** Bugsnag API request timeout. */
7575
private int timeout;
@@ -527,9 +527,9 @@ public void setReleaseStage(String releaseStage) {
527527
}
528528

529529
/**
530-
* @see Bugsnag#setSendThreads(boolean)
530+
* @see Bugsnag#setSendThreads(ThreadSendPolicy)
531531
*/
532-
public void setSendThreads(boolean sendThreads) {
532+
public void setSendThreads(ThreadSendPolicy sendThreads) {
533533
this.sendThreads = sendThreads;
534534

535535
if (bugsnag != null) {

bugsnag/src/main/java/com/bugsnag/BugsnagEvent.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ public class BugsnagEvent {
3636
*/
3737
protected BugsnagEvent(Configuration config, Throwable throwable) {
3838
this(config, throwable, HandledState.newInstance(
39-
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION), java.lang.Thread.currentThread(), null);
39+
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION), Thread.currentThread(), null);
4040
}
4141

4242
BugsnagEvent(Configuration config, Throwable throwable,
43-
HandledState handledState, java.lang.Thread currentThread) {
43+
HandledState handledState, Thread currentThread) {
4444
this(config, throwable, handledState, currentThread, null);
4545
}
4646

4747
BugsnagEvent(Configuration config, Throwable throwable,
48-
HandledState handledState, java.lang.Thread currentThread, FeatureFlagStore clientFeatureFlagStore) {
48+
HandledState handledState, Thread currentThread, FeatureFlagStore clientFeatureFlagStore) {
4949
this.config = config;
5050
this.error = new BugsnagError(config, throwable);
5151
this.handledState = handledState;
@@ -58,9 +58,11 @@ protected BugsnagEvent(Configuration config, Throwable throwable) {
5858
featureFlagStore.merge(clientFeatureFlagStore);
5959
}
6060

61-
if (config.isSendThreads()) {
61+
boolean sendThreads = config.getSendThreads() == ThreadSendPolicy.ALWAYS ||
62+
(config.getSendThreads() == ThreadSendPolicy.UNHANDLED_ONLY && handledState.isUnhandled());
63+
if (sendThreads) {
6264
Throwable exc = handledState.isUnhandled() ? throwable : null;
63-
Map<java.lang.Thread, StackTraceElement[]> allStackTraces = java.lang.Thread.getAllStackTraces();
65+
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
6466
threads = BugsnagThread.getLiveThreads(config, currentThread, allStackTraces, exc);
6567
} else {
6668
threads = null;

bugsnag/src/main/java/com/bugsnag/Configuration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Configuration {
4343
private Set<String> enabledReleaseStages = null;
4444
private String[] projectPackages;
4545
private String releaseStage;
46-
private boolean sendThreads = false;
46+
private ThreadSendPolicy sendThreads = ThreadSendPolicy.NEVER;
4747
private Serializer serializer = new DefaultSerializer();
4848

4949
Collection<Callback> callbacks = new ConcurrentLinkedQueue<Callback>();
@@ -328,11 +328,11 @@ public void setReleaseStage(String releaseStage) {
328328
this.releaseStage = releaseStage;
329329
}
330330

331-
public boolean isSendThreads() {
331+
public ThreadSendPolicy getSendThreads() {
332332
return sendThreads;
333333
}
334334

335-
public void setSendThreads(boolean sendThreads) {
335+
public void setSendThreads(ThreadSendPolicy sendThreads) {
336336
this.sendThreads = sendThreads;
337337
}
338338

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bugsnag;
2+
3+
/**
4+
* Controls whether we should capture and serialize the state of all threads at the time of an error.
5+
*/
6+
public enum ThreadSendPolicy {
7+
/**
8+
* Threads should be captured for all events. This is often not useful for Java web apps, since
9+
* there could be thousands of active threads depending on your environment.
10+
*/
11+
ALWAYS,
12+
13+
/**
14+
* Threads should be captured for unhandled events only.
15+
*/
16+
UNHANDLED_ONLY,
17+
18+
/**
19+
* Threads should never be captured.
20+
*/
21+
NEVER
22+
}

bugsnag/src/test/java/com/bugsnag/AppenderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public void testExceptionSeverities() {
138138

139139
@Test
140140
public void testBugsnagConfig() {
141-
142141
// Get the Bugsnag instance
143142
Configuration config = getConfig(appender.getClient());
144143
assertEquals("test", config.getReleaseStage());
@@ -175,7 +174,7 @@ public void testBugsnagConfig() {
175174
assertTrue(projectPackages.contains("com.company.package2"));
176175
assertTrue(projectPackages.contains("com.company.package1"));
177176

178-
assertTrue(config.isSendThreads());
177+
assertEquals(ThreadSendPolicy.ALWAYS, config.getSendThreads());
179178
}
180179

181180
@Test

bugsnag/src/test/java/com/bugsnag/BugsnagTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ public void close() {
438438

439439
@Test
440440
public void testSendThreads() {
441-
bugsnag.setSendThreads(true);
441+
bugsnag.setSendThreads(ThreadSendPolicy.ALWAYS);
442442
bugsnag.setDelivery(new Delivery() {
443443
@Override
444444
public void deliver(Serializer serializer, Object object, Map<String, String> headers) {
445445
BugsnagEvent event = ((Notification) object).getEvents().get(0);
446446
// There is information about at least one thread
447-
assertTrue(event.getThreads().size() > 0);
447+
assertFalse(event.getThreads().isEmpty());
448448
}
449449

450450
@Override

bugsnag/src/test/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</tab>
3434
</metadata>
3535

36-
<sendThreads>true</sendThreads>
36+
<sendThreads>ALWAYS</sendThreads>
3737

3838
</appender>
3939

0 commit comments

Comments
 (0)