Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit babf435

Browse files
authored
fixing a bunch of things to send event correctly (#74)
1 parent 564483d commit babf435

File tree

10 files changed

+134
-9
lines changed

10 files changed

+134
-9
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/AndroidSerializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.sentry.core.ILogger;
88
import io.sentry.core.ISerializer;
99
import io.sentry.core.SentryEvent;
10+
import io.sentry.core.SentryLevel;
1011
import io.sentry.core.protocol.Device;
1112
import io.sentry.core.protocol.SentryId;
1213
import java.io.IOException;
@@ -38,6 +39,8 @@ private Gson provideGson() {
3839
Device.DeviceOrientation.class, new OrientationSerializerAdapter(logger))
3940
.registerTypeAdapter(
4041
Device.DeviceOrientation.class, new OrientationDeserializerAdapter(logger))
42+
.registerTypeAdapter(SentryLevel.class, new SentryLevelSerializerAdapter(logger))
43+
.registerTypeAdapter(SentryLevel.class, new SentryLevelDeserializerAdapter(logger))
4144
.registerTypeAdapterFactory(UnknownPropertiesTypeAdapterFactory.get())
4245
.create();
4346
}

sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ private Long getMemorySize(ActivityManager.MemoryInfo memInfo) {
157157
// we can get some inspiration here
158158
// https://github.com/flutter/plugins/blob/master/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java
159159
private Device getDevice() {
160+
// TODO: missing name and usable memory
161+
160162
Device device = new Device();
161163
device.setManufacturer(Build.MANUFACTURER);
162164
device.setBrand(Build.BRAND);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.sentry.android.core.adapters;
2+
3+
import static io.sentry.core.ILogger.log;
4+
5+
import com.google.gson.JsonDeserializationContext;
6+
import com.google.gson.JsonDeserializer;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonParseException;
9+
import io.sentry.core.ILogger;
10+
import io.sentry.core.SentryLevel;
11+
import java.lang.reflect.Type;
12+
import java.util.Locale;
13+
14+
public class SentryLevelDeserializerAdapter implements JsonDeserializer<SentryLevel> {
15+
16+
private final ILogger logger;
17+
18+
public SentryLevelDeserializerAdapter(ILogger logger) {
19+
this.logger = logger;
20+
}
21+
22+
@Override
23+
public SentryLevel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
24+
throws JsonParseException {
25+
try {
26+
return json == null ? null : SentryLevel.valueOf(json.getAsString().toUpperCase(Locale.ROOT));
27+
} catch (Exception e) {
28+
log(logger, SentryLevel.ERROR, "Error when deserializing SentryLevel", e);
29+
}
30+
return null;
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.sentry.android.core.adapters;
2+
3+
import static io.sentry.core.ILogger.log;
4+
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonPrimitive;
7+
import com.google.gson.JsonSerializationContext;
8+
import com.google.gson.JsonSerializer;
9+
import io.sentry.core.ILogger;
10+
import io.sentry.core.SentryLevel;
11+
import java.lang.reflect.Type;
12+
import java.util.Locale;
13+
14+
public class SentryLevelSerializerAdapter implements JsonSerializer<SentryLevel> {
15+
16+
private final ILogger logger;
17+
18+
public SentryLevelSerializerAdapter(ILogger logger) {
19+
this.logger = logger;
20+
}
21+
22+
@Override
23+
public JsonElement serialize(SentryLevel src, Type typeOfSrc, JsonSerializationContext context) {
24+
try {
25+
return src == null ? null : new JsonPrimitive(src.name().toLowerCase(Locale.ROOT));
26+
} catch (Exception e) {
27+
log(logger, SentryLevel.ERROR, "Error when serializing SentryLevel", e);
28+
}
29+
return null;
30+
}
31+
}

sentry-android-core/src/test/java/io/sentry/android/core/AndroidSerializerTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.google.gson.internal.LinkedTreeMap
66
import com.nhaarman.mockitokotlin2.mock
77
import io.sentry.core.DateUtils
88
import io.sentry.core.SentryEvent
9+
import io.sentry.core.SentryLevel
910
import io.sentry.core.protocol.Contexts
1011
import io.sentry.core.protocol.Device
1112
import java.io.StringWriter
@@ -200,6 +201,33 @@ class AndroidSerializerTest {
200201
assertEquals(Device.DeviceOrientation.LANDSCAPE, Device.DeviceOrientation.valueOf(orientation.toUpperCase())) // here too
201202
}
202203

204+
@Test
205+
fun `when serializing a SentryLevel, it should become a sentry level string`() {
206+
val sentryEvent = generateEmptySentryEvent()
207+
sentryEvent.eventId = null
208+
sentryEvent.timestamp = null
209+
sentryEvent.level = SentryLevel.DEBUG
210+
211+
val expected = "{\"level\":\"debug\"}"
212+
213+
val actual = serializeToString(sentryEvent)
214+
215+
assertEquals(expected, actual)
216+
}
217+
218+
@Test
219+
fun `when deserializing a sentry level string, it should become a SentryLevel`() {
220+
val sentryEvent = generateEmptySentryEvent()
221+
sentryEvent.eventId = null
222+
sentryEvent.timestamp = null
223+
224+
val jsonEvent = "{\"level\":\"debug\"}"
225+
226+
val actual = serializer.deserializeEvent(jsonEvent)
227+
228+
assertEquals(SentryLevel.DEBUG, actual.level)
229+
}
230+
203231
private fun generateEmptySentryEvent(): SentryEvent {
204232
return SentryEvent().apply {
205233
contexts = null

sentry-core/src/main/java/io/sentry/core/Sentry.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,35 @@ public static SentryId captureException(Throwable throwable) {
7474
return getCurrentHub().captureException(throwable);
7575
}
7676

77-
public void addBreadcrumb(Breadcrumb breadcrumb) {
77+
public static void addBreadcrumb(Breadcrumb breadcrumb) {
7878
getCurrentHub().addBreadcrumb(breadcrumb);
7979
}
8080

81-
public SentryId getLastEventId() {
81+
public static SentryId getLastEventId() {
8282
return getCurrentHub().getLastEventId();
8383
}
8484

85-
public void pushScope() {
85+
public static void pushScope() {
8686
getCurrentHub().pushScope();
8787
}
8888

89-
public void popScope() {
89+
public static void popScope() {
9090
getCurrentHub().popScope();
9191
}
9292

93-
public void withScope(ScopeCallback callback) {
93+
public static void withScope(ScopeCallback callback) {
9494
getCurrentHub().withScope(callback);
9595
}
9696

97-
public void configureScope(ScopeCallback callback) {
97+
public static void configureScope(ScopeCallback callback) {
9898
getCurrentHub().configureScope(callback);
9999
}
100100

101-
public void bindClient(SentryClient client) {
101+
public static void bindClient(SentryClient client) {
102102
getCurrentHub().bindClient(client);
103103
}
104104

105-
public void flush(int timeoutMills) {
105+
public static void flush(int timeoutMills) {
106106
getCurrentHub().flush(timeoutMills);
107107
}
108108

sentry-core/src/main/java/io/sentry/core/protocol/Contexts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public OperatingSystem getOperatingSystem() {
3939
}
4040

4141
public void setOperatingSystem(OperatingSystem operatingSystem) {
42-
this.put(Device.TYPE, operatingSystem);
42+
this.put(OperatingSystem.TYPE, operatingSystem);
4343
}
4444

4545
public Runtime getRuntime() {

sentry-sample/src/main/java/io/sentry/sample/MainActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import android.os.Bundle;
44
import android.os.StrictMode;
55
import androidx.appcompat.app.AppCompatActivity;
6+
import io.sentry.core.Breadcrumb;
67
import io.sentry.core.Sentry;
8+
import io.sentry.core.SentryLevel;
9+
import io.sentry.core.protocol.User;
10+
import java.util.Collections;
711
import timber.log.Timber;
812

913
public class MainActivity extends AppCompatActivity {
@@ -37,6 +41,25 @@ protected void onCreate(Bundle savedInstanceState) {
3741
view -> {
3842
Sentry.captureException(new Exception("Some exception."));
3943
});
44+
45+
findViewById(R.id.breadcrumb)
46+
.setOnClickListener(
47+
view -> {
48+
Sentry.configureScope(
49+
scope -> {
50+
Breadcrumb breadcrumb = new Breadcrumb();
51+
breadcrumb.setMessage("Breadcrumb");
52+
scope.addBreadcrumb(breadcrumb);
53+
scope.setExtra("extra", "extra");
54+
scope.setFingerprint(Collections.singletonList("fingerprint"));
55+
scope.setLevel(SentryLevel.INFO);
56+
scope.setTransaction("transaction");
57+
User user = new User();
58+
user.setUsername("username");
59+
scope.setUser(user);
60+
scope.setTag("tag", "tag");
61+
});
62+
});
4063
}
4164

4265
private void districtMode() {

sentry-sample/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@
2020
android:layout_height="wrap_content"
2121
android:id="@+id/capture_exception"
2222
android:text="@string/capture_exception"/>
23+
24+
<Button android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:id="@+id/breadcrumb"
27+
android:text="@string/breadcrumb"/>
2328
</LinearLayout>

sentry-sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<string name="crash_me">Crash Me</string>
44
<string name="send_message">Send Message</string>
55
<string name="capture_exception">Capture Exception</string>
6+
<string name="breadcrumb">Breadcrumb</string>
67
</resources>

0 commit comments

Comments
 (0)