Skip to content

Commit

Permalink
Add breadcrumb support
Browse files Browse the repository at this point in the history
I've tried to keep the API as high-level as possible, and not expose too
much to the user.

Fixes #70
  • Loading branch information
marcomorain committed Sep 25, 2016
1 parent e3a4cc0 commit d7f890d
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions sentry-android/src/main/java/com/joshdholtz/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.EnglishReasonPhraseCatalog;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
Expand Down Expand Up @@ -60,6 +61,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -82,6 +84,7 @@ public class Sentry {
private final static String VERSION = "0.2.0";
private final static String sentryVersion = "7";
private static final int MAX_QUEUE_LENGTH = 50;
private static final int MAX_BREADCRUMBS = 10;

public static boolean debug = false;

Expand All @@ -94,6 +97,9 @@ public class Sentry {
private JSONObject contexts = new JSONObject();
private Executor executor;


private LinkedList<Breadcrumb> breadcrumbs = new LinkedList<>();

public enum SentryEventLevel {

FATAL("fatal"),
Expand Down Expand Up @@ -148,6 +154,8 @@ public static void init(Context context, String dsn, boolean setupUncaughtExcept

if (setupUncaughtExceptionHandler) {
Sentry.getInstance().setupUncaughtExceptionHandler();


}
}

Expand Down Expand Up @@ -304,6 +312,7 @@ public static void captureEvent(SentryEventBuilder builder) {
if (Sentry.getInstance().packageInfo != null) {
builder.setRelease(Integer.toString(Sentry.getInstance().packageInfo.versionCode));
}
builder.event.put("breadcrumbs", Sentry.getInstance().currentBreadcrumbs());
if (Sentry.getInstance().captureListener != null) {

builder = Sentry.getInstance().captureListener.beforeCapture(builder);
Expand Down Expand Up @@ -610,6 +619,104 @@ public interface SentryEventCaptureListener {

}



public final static class Breadcrumb {

enum Type {

Default("default"),
HTTP("http"),
Navigation("navigation");

private final String value;

Type(String value) {
this.value = value;
}
}

public final long timestamp;
public final Type type;
public final String message;
public final String category;
public final SentryEventLevel level;
public final Map<String, String> data = new HashMap<>();

public Breadcrumb(long timestamp, Type type, String message, String category, SentryEventLevel level) {
this.timestamp = timestamp;
this.type = type;
this.message = message;
this.category = category;
this.level = level;
}
}

private void pushBreadcrum(Breadcrumb b) {
while (breadcrumbs.size() >= MAX_BREADCRUMBS) {
breadcrumbs.removeFirst();
}
breadcrumbs.add(b);
}

public static void addNavigationBreadcrumb(String category, String from, String to) {
final Breadcrumb b = new Breadcrumb(
System.currentTimeMillis()/1000,
Breadcrumb.Type.Navigation,
"",
category,
SentryEventLevel.INFO);

b.data.put("from", from);
b.data.put("to", to);
getInstance().pushBreadcrum(b);
}

public static void addHttpBreadcrumb(String url, String method, int statusCode) {
final String reason = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.US);
final Breadcrumb b = new Breadcrumb(
System.currentTimeMillis()/1000,
Breadcrumb.Type.HTTP,
"",
String.format("http.%s", method.toLowerCase()),
SentryEventLevel.INFO);

b.data.put("url", url);
b.data.put("method", method);
b.data.put("status_code", Integer.toString(statusCode));
b.data.put("reason", reason);
getInstance().pushBreadcrum(b);
}

public static void addBreadcrumb(String category, String message) {
getInstance().pushBreadcrum(new Breadcrumb(
System.currentTimeMillis()/1000,
Breadcrumb.Type.Default,
message,
category,
SentryEventLevel.INFO));
}

private JSONArray currentBreadcrumbs() {
final JSONArray crumbs = new JSONArray();
for (Breadcrumb breadcrumb : breadcrumbs) {
final JSONObject json = new JSONObject();

try {
json.put("timestamp", breadcrumb.timestamp);
json.put("type", breadcrumb.type.value);
json.put("message", breadcrumb.message);
json.put("category", breadcrumb.category);
json.put("level", breadcrumb.level.value);
json.put("data", new JSONObject(breadcrumb.data));
} catch (JSONException jse) {
Log.e(TAG, "Error serializing breadcrumbs", jse);
}
crumbs.put(json);
}
return crumbs;
}

public static class SentryEventRequest implements Serializable {
private final String requestData;
private final UUID uuid;
Expand Down

0 comments on commit d7f890d

Please sign in to comment.