-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSentryBridgeJava.java
216 lines (195 loc) · 7.28 KB
/
SentryBridgeJava.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2022 Sentry. All Rights Reserved.
package io.sentry.unreal;
import android.app.Activity;
import androidx.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.sentry.Breadcrumb;
import io.sentry.Hint;
import io.sentry.IHub;
import io.sentry.SamplingContext;
import io.sentry.IScope;
import io.sentry.ScopeCallback;
import io.sentry.Sentry;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.android.core.SentryAndroid;
import io.sentry.android.core.SentryAndroidOptions;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.SentryStackFrame;
import io.sentry.protocol.SentryStackTrace;
public class SentryBridgeJava {
public static native void onConfigureScope(long callbackAddr, IScope scope);
public static native SentryEvent onBeforeSend(long handlerAddr, SentryEvent event, Hint hint);
public static native float onTracesSampler(long samplerAddr, SamplingContext samplingContext);
public static void init(Activity activity, final String settingsJsonStr, final long beforeSendHandler) {
SentryAndroid.init(activity, new Sentry.OptionsConfiguration<SentryAndroidOptions>() {
@Override
public void configure(SentryAndroidOptions options) {
try {
JSONObject settingJson = new JSONObject(settingsJsonStr);
options.setDsn(settingJson.getString("dsn"));
options.setRelease(settingJson.getString("release"));
options.setEnvironment(settingJson.getString("environment"));
options.setEnableAutoSessionTracking(settingJson.getBoolean("autoSessionTracking"));
options.setSessionTrackingIntervalMillis(settingJson.getLong("sessionTimeout"));
options.setAttachStacktrace(settingJson.getBoolean("enableStackTrace"));
options.setDebug(settingJson.getBoolean("debug"));
options.setSampleRate(settingJson.getDouble("sampleRate"));
options.setMaxBreadcrumbs(settingJson.getInt("maxBreadcrumbs"));
options.setAttachScreenshot(settingJson.getBoolean("attachScreenshot"));
options.setSendDefaultPii(settingJson.getBoolean("sendDefaultPii"));
options.setBeforeSend(new SentryOptions.BeforeSendCallback() {
@Override
public SentryEvent execute(SentryEvent event, Hint hint) {
preProcessEvent(event);
return onBeforeSend(beforeSendHandler, event, hint);
}
});
JSONArray Includes = settingJson.getJSONArray("inAppInclude");
for (int i = 0; i < Includes.length(); i++) {
options.addInAppInclude(Includes.getString(i));
}
JSONArray Excludes = settingJson.getJSONArray("inAppExclude");
for (int i = 0; i < Excludes.length(); i++) {
options.addInAppExclude(Excludes.getString(i));
}
options.setEnableTracing(settingJson.getBoolean("enableTracing"));
if(settingJson.has("tracesSampleRate")) {
options.setTracesSampleRate(settingJson.getDouble("tracesSampleRate"));
}
if(settingJson.has("tracesSampler")) {
final long samplerAddr = settingJson.getLong("tracesSampler");
options.setTracesSampler(new SentryOptions.TracesSamplerCallback() {
@Override
public Double sample(SamplingContext samplingContext) {
float sampleRate = onTracesSampler(samplerAddr, samplingContext);
if(sampleRate >= 0.0f) {
return (double) sampleRate;
} else {
return null;
}
}
});
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
});
}
private static void preProcessEvent(SentryEvent event) {
if (event.getTags().containsKey("sentry_unreal_exception")) {
SentryException exception = event.getUnhandledException();
if (exception != null) {
exception.setType(event.getTag("sentry_unreal_exception_type"));
exception.setValue(event.getTag("sentry_unreal_exception_message"));
SentryStackTrace trace = exception.getStacktrace();
int numFramesToSkip = Integer.parseInt(event.getTag("sentry_unreal_exception_skip_frames"));
List<SentryStackFrame> frames = trace.getFrames();
trace.setFrames(frames.subList(0, frames.size() - numFramesToSkip));
}
event.removeTag("sentry_unreal_exception_type");
event.removeTag("sentry_unreal_exception_message");
event.removeTag("sentry_unreal_exception_skip_frames");
event.removeTag("sentry_unreal_exception");
}
}
public static void addBreadcrumb(final String message, final String category, final String type, final HashMap<String, String> data, final SentryLevel level) {
Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setMessage(message);
breadcrumb.setCategory(category);
breadcrumb.setType(type);
breadcrumb.setLevel(level);
for (Map.Entry<String,String> entry : data.entrySet()) {
breadcrumb.setData(entry.getKey(), entry.getValue());
}
Sentry.addBreadcrumb(breadcrumb);
}
public static SentryId captureMessageWithScope(final String message, final SentryLevel level, final long callback) throws InterruptedException {
SentryId messageId = Sentry.captureMessage(message, new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
scope.setLevel(level);
onConfigureScope(callback, scope);
}
});
return messageId;
}
public static SentryId captureEventWithScope(final SentryEvent event, final long callback) throws InterruptedException {
SentryId eventId = Sentry.captureEvent(event, new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
onConfigureScope(callback, scope);
}
});
return eventId;
}
public static SentryId captureException(final String type, final String value) {
SentryException exception = new SentryException();
exception.setType(type);
exception.setValue(value);
SentryEvent event = new SentryEvent();
event.setExceptions(Collections.singletonList(exception));
SentryId eventId = Sentry.captureEvent(event);
return eventId;
}
public static void configureScope(final long callback) {
Sentry.configureScope(new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
onConfigureScope(callback, scope);
}
});
}
public static void setContext(final String key, final HashMap<String, String> values) {
Sentry.configureScope(new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
scope.setContexts(key, values);
}
});
}
public static void setTag(final String key, final String value) {
Sentry.configureScope(new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
scope.setTag(key, value);
}
});
}
public static void removeTag(final String key) {
Sentry.configureScope(new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
scope.removeTag(key);
}
});
}
public static void setLevel(final SentryLevel level) {
Sentry.configureScope(new ScopeCallback() {
@Override
public void run(@NonNull IScope scope) {
scope.setLevel(level);
}
});
}
public static SentryOptions getOptions() {
IHub hub = Sentry.getCurrentHub();
return hub.getOptions();
}
public static int isCrashedLastRun() {
Boolean isCrashed = Sentry.isCrashedLastRun();
if(isCrashed == null) {
return -1;
}
return isCrashed ? 1 : 0;
}
}