Skip to content

Commit bb5201b

Browse files
committed
Add limits to Breadcrumb length
Fixes #117
1 parent 9e585cb commit bb5201b

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ table below shows an example of what the data will look like in Sentry.
7070

7171
Version | Changes
7272
--- | ---
73+
**1.6.0** | Increase breadcrumb limit to 100 to match other Sentry clients, allow runtime configuration. [#117](https://github.com/joshdholtz/Sentry-Android/issues/117).
7374
**1.5.4** | Ensure that breadcrumbs are added to all exceptions. [#115](https://github.com/joshdholtz/Sentry-Android/issues/115).
7475
**1.5.3** | Fix thread-safety bug when serializing breadcrumbs. [#110](https://github.com/joshdholtz/Sentry-Android/issues/110) (thanks to [fab1an](https://github.com/fab1an)).
7576
**1.5.2** | Send stack-frames to Sentry in the correct order. [#95](https://github.com/joshdholtz/Sentry-Android/pull/95).<br/> Use the [versionName](https://developer.android.com/studio/publish/versioning.html#appversioning), rather than versionCode, as the default value for the release field of events (thanks to [FelixBondarenko](https://github.com/FelixBondarenko)).
@@ -94,7 +95,7 @@ Version | Changes
9495
### Gradle
9596
Available in jCenter
9697
```
97-
compile 'com.joshdholtz.sentry:sentry-android:1.5.0'
98+
compile 'com.joshdholtz.sentry:sentry-android:1.6.0'
9899
```
99100

100101
### Manual

sentry-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
apply plugin: 'com.android.library'
66

77

8-
def SentryAndroidVersion = "1.5.4"
8+
def SentryAndroidVersion = "1.6.0"
99

1010
android {
1111
compileSdkVersion 24

sentry-android/src/androidTest/java/com/joshdholtz/sentry/SentryEventBuilderTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,11 @@ public void testInternalPackageNameRegex() throws Exception {
122122
assertFalse(c, c.matches(Sentry.SentryEventBuilder.isInternalPackage));
123123
}
124124
}
125-
}
125+
126+
public void testMaxBreadcrumbs() {
127+
Sentry.setMaxBreadcrumbs(-5);
128+
assertEquals(0, Sentry.LazyHolder.instance.breadcrumbs.maxBreadcrumbs.get());
129+
Sentry.setMaxBreadcrumbs(1000);
130+
assertEquals(200, Sentry.LazyHolder.instance.breadcrumbs.maxBreadcrumbs.get());
131+
}
132+
}

sentry-android/src/main/java/com/joshdholtz/sentry/Sentry.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.util.concurrent.ThreadFactory;
7575
import java.util.concurrent.ThreadPoolExecutor;
7676
import java.util.concurrent.TimeUnit;
77+
import java.util.concurrent.atomic.AtomicInteger;
7778
import java.util.concurrent.atomic.AtomicLong;
7879
import java.util.concurrent.locks.ReadWriteLock;
7980
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -100,7 +101,7 @@ public class Sentry {
100101
private SentryEventCaptureListener captureListener;
101102
private JSONObject contexts = new JSONObject();
102103
private Executor executor;
103-
private final Breadcrumbs breadcrumbs = new Breadcrumbs();
104+
final Breadcrumbs breadcrumbs = new Breadcrumbs();
104105

105106
public enum SentryEventLevel {
106107

@@ -130,8 +131,8 @@ private static Sentry getInstance() {
130131
return LazyHolder.instance;
131132
}
132133

133-
private static class LazyHolder {
134-
private static final Sentry instance = new Sentry();
134+
static class LazyHolder {
135+
static final Sentry instance = new Sentry();
135136
}
136137

137138
public static void init(Context context, String dsn) {
@@ -258,6 +259,16 @@ public static void setCaptureListener(SentryEventCaptureListener captureListener
258259
Sentry.getInstance().captureListener = captureListener;
259260
}
260261

262+
/**
263+
* Set a limit on the number of breadcrumbs that will be stored by the client, and sent with
264+
* exceptions.
265+
*
266+
* @param maxBreadcrumbs the maximum number of breadcrumbs to store and send.
267+
*/
268+
public static void setMaxBreadcrumbs(int maxBreadcrumbs) {
269+
getInstance().breadcrumbs.setMaxBreadcrumbs(maxBreadcrumbs);
270+
}
271+
261272
public static void captureMessage(String message) {
262273
Sentry.captureMessage(message, SentryEventLevel.INFO);
263274
}
@@ -656,22 +667,23 @@ enum Type {
656667
}
657668
}
658669

659-
private static class Breadcrumbs {
670+
static class Breadcrumbs {
660671

661672
// The max number of breadcrumbs that will be tracked at any one time.
662-
private static final int MAX_BREADCRUMBS = 10;
663-
673+
final AtomicInteger maxBreadcrumbs = new AtomicInteger(100);
664674

665675
// Access to this list must be thread-safe.
666676
// See GitHub Issue #110
667677
// This list is protected by the provided ReadWriteLock.
668-
private final LinkedList<Breadcrumb> breadcrumbs = new LinkedList<>();
669-
private final ReadWriteLock lock = new ReentrantReadWriteLock();
678+
final LinkedList<Breadcrumb> breadcrumbs = new LinkedList<>();
679+
final ReadWriteLock lock = new ReentrantReadWriteLock();
670680

671681
void push(Breadcrumb b) {
672682
try {
673683
lock.writeLock().lock();
674-
while (breadcrumbs.size() >= MAX_BREADCRUMBS) {
684+
685+
final int toRemove = breadcrumbs.size() - maxBreadcrumbs.get() + 1;
686+
for (int i = 0; i < toRemove; i++) {
675687
breadcrumbs.removeFirst();
676688
}
677689
breadcrumbs.add(b);
@@ -702,6 +714,11 @@ JSONArray current() {
702714
return crumbs;
703715
}
704716

717+
void setMaxBreadcrumbs(int maxBreadcrumbs) {
718+
maxBreadcrumbs = Math.min(200, Math.max(0, maxBreadcrumbs));
719+
this.maxBreadcrumbs.set(maxBreadcrumbs);
720+
}
721+
705722
}
706723

707724
/**

sentry-app/src/main/java/com/joshdholtz/sentryapp/MainActivity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ protected void onCreate(Bundle savedInstanceState) {
2525
Sentry.init(this, yourDSN);
2626
Sentry.debug = true;
2727

28+
29+
Sentry.setMaxBreadcrumbs(8);
30+
for (int i=0; i<15; i++) {
31+
Sentry.addBreadcrumb("Limit Test", Integer.toString(i));
32+
}
33+
Sentry.captureMessage("8 breadcrumbs test.");
34+
2835
Sentry.addNavigationBreadcrumb("activity.main", "here", "there");
2936
Sentry.addHttpBreadcrumb("http://example.com", "GET", 202);
3037

3138
Sentry.captureEvent(new Sentry.SentryEventBuilder()
32-
.setMessage("OMG this works woooo")
39+
.setMessage("This event has a message and a stacktrace.")
3340
.setStackTrace(Thread.currentThread().getStackTrace())
3441
);
3542

0 commit comments

Comments
 (0)