Skip to content

Commit

Permalink
feat: increase breadcrumb time precision to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Oct 16, 2020
1 parent 6c87fce commit 4d3636b
Show file tree
Hide file tree
Showing 23 changed files with 44 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

### Bug fixes

* Increase breadcrumb time precision to milliseconds
[#954](https://github.com/bugsnag/bugsnag-android/pull/954)

## TBD

### Bug fixes

* Avoid crash when initializing bugsnag in attachBaseContext
[#953](https://github.com/bugsnag/bugsnag-android/pull/953)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DateUtils {
@Override
protected DateFormat initialValue() {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
iso8601.setTimeZone(tz);
return iso8601;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"type": "manual",
"metaData": {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "metadata",
"type": "process",
"metaData": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"type": "manual",
"metaData": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"freeDisk": 120935100007,
"freeMemory": 2234092234234,
"orientation": "landscape",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
}
4 changes: 2 additions & 2 deletions bugsnag-android-core/src/test/resources/event_redaction.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [
{
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "Whoops",
"type": "log",
"metaData": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"groupingHash": "herpderp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": [],
"session": {
"id": "123",
"startedAt": "1970-01-01T00:00:00Z",
"startedAt": "1970-01-01T00:00:00.000Z",
"events": {
"handled": 0,
"unhandled": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00Z"
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [
{
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "helloworld",
"type": "manual",
"metaData": {}
Expand Down
2 changes: 1 addition & 1 deletion bugsnag-android-core/src/test/resources/json_stream.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"long": 123,
"float": 123.45,
"streamable": {
"timestamp": "1970-01-01T00:00:00Z",
"timestamp": "1970-01-01T00:00:00.000Z",
"name": "whoops",
"type": "log",
"metaData": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"sessions": [
{
"id": "123",
"startedAt": "1970-01-01T00:00:00Z",
"startedAt": "1970-01-01T00:00:00.000Z",
"user": {}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal class DeviceSerializationTest {
@Test
fun testPassesNativeSuite() {
val expectedJson = loadJson("device_serialization_$testCase.json")
verifyNativeRun(run(testCase.toInt(), expectedJson))
// sanitize device time to standard value
val timestampEnd = expectedJson.indexOf("Z\"}}")
val sanitizedJson = expectedJson.replaceRange(timestampEnd - 3, timestampEnd, "000")
verifyNativeRun(run(testCase.toInt(), sanitizedJson))
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"device":{"osName":"android","id":"f5gh7","locale":"En","osVersion":"8.1","manufacturer":"Samsung","model":"S7","orientation":"portrait","runtimeVersions":{"androidApiLevel":29,"osBuild":"BullDog 5.2"},"cpuAbi":["x86"],"totalMemory":512340922,"jailbroken":true,"time":"2029-01-01T00:00:00Z"}}
{"device":{"osName":"android","id":"f5gh7","locale":"En","osVersion":"8.1","manufacturer":"Samsung","model":"S7","orientation":"portrait","runtimeVersions":{"androidApiLevel":29,"osBuild":"BullDog 5.2"},"cpuAbi":["x86"],"totalMemory":512340922,"jailbroken":true,"time":"2029-01-01T00:00:00.000Z"}}
13 changes: 9 additions & 4 deletions bugsnag-plugin-android-ndk/src/main/jni/utils/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ void bsg_serialize_app_metadata(const bsg_app_info app, JSON_Object *event_obj)
json_object_dotset_string(event_obj, "metaData.app.activeScreen", app.active_screen);
}

void bsg_serialize_device(const bsg_device_info device, JSON_Object *event_obj) {
void bsg_serialize_device(const bsg_device_info device, JSON_Object *event_obj, struct timeval current_time) {
json_object_dotset_string(event_obj, "device.osName", device.os_name);
json_object_dotset_string(event_obj, "device.id", device.id);
json_object_dotset_string(event_obj, "device.locale", device.locale);
Expand All @@ -406,9 +406,11 @@ void bsg_serialize_device(const bsg_device_info device, JSON_Object *event_obj)
json_object_dotset_number(event_obj, "device.totalMemory", device.total_memory);
json_object_dotset_boolean(event_obj, "device.jailbroken", device.jailbroken);

char report_time[sizeof "2018-10-08T12:07:09Z"];
if (device.time > 0) {
strftime(report_time, sizeof report_time, "%FT%TZ", gmtime(&device.time));
int milliseconds = current_time.tv_usec / 1000;
char report_time[sizeof "2018-10-08T12:07:09.000Z"];
strftime(report_time, sizeof report_time, "%FT%T", gmtime(&device.time));
sprintf(report_time, "%s.%03dZ", report_time, milliseconds);
json_object_dotset_string(event_obj, "device.time", report_time);
}
}
Expand Down Expand Up @@ -563,7 +565,10 @@ char *bsg_serialize_event_to_json_string(bugsnag_event *event) {
bsg_serialize_handled_state(event, event_obj);
bsg_serialize_app(event->app, event_obj);
bsg_serialize_app_metadata(event->app, event_obj);
bsg_serialize_device(event->device, event_obj);

struct timeval current_time = {};
gettimeofday(&current_time, NULL);
bsg_serialize_device(event->device, event_obj, current_time);
bsg_serialize_device_metadata(event->device, event_obj);
bsg_serialize_custom_metadata(event->metadata, event_obj);
bsg_serialize_user(event->user, event_obj);
Expand Down
2 changes: 1 addition & 1 deletion bugsnag-plugin-android-ndk/src/main/jni/utils/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void bsg_serialize_context(const bugsnag_event *event, JSON_Object *event_obj);
void bsg_serialize_handled_state(const bugsnag_event *event, JSON_Object *event_obj);
void bsg_serialize_app(const bsg_app_info app, JSON_Object *event_obj);
void bsg_serialize_app_metadata(const bsg_app_info app, JSON_Object *event_obj);
void bsg_serialize_device(const bsg_device_info device, JSON_Object *event_obj);
void bsg_serialize_device(const bsg_device_info device, JSON_Object *event_obj, struct timeval current_time);
void bsg_serialize_device_metadata(const bsg_device_info device, JSON_Object *event_obj);
void bsg_serialize_custom_metadata(const bugsnag_metadata metadata, JSON_Object *event_obj);
void bsg_serialize_user(const bugsnag_user user, JSON_Object *event_obj);
Expand Down
3 changes: 2 additions & 1 deletion bugsnag-plugin-android-ndk/src/test/cpp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ TEST test_device_serialization(test_case *test_case) {
JSON_Value *event_val = json_value_init_object();
JSON_Object *event = json_value_get_object(event_val);
bsg_device_info *device = test_case->data_ptr;
bsg_serialize_device(*device, event);
struct timeval current_time = {};
bsg_serialize_device(*device, event, current_time);
free(device);
return validate_serialized_json(test_case, event_val);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setup() {
public void serialize() {
Map<String, Object> map = new HashMap<>();
new BreadcrumbSerializer().serialize(map, crumb);
assertEquals("1970-01-01T00:00:00Z", map.get("timestamp"));
assertEquals("1970-01-01T00:00:00.000Z", map.get("timestamp"));
assertEquals("Whoops", map.get("message"));
assertEquals("state", map.get("type"));
assertEquals(metadata, map.get("metadata"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void serialize() {
assertEquals(20923423434L, map.get("freeDisk"));
assertEquals(23409662345L, map.get("freeMemory"));
assertEquals("portrait", map.get("orientation"));
assertEquals("1970-01-01T00:00:00Z", map.get("time"));
assertEquals("1970-01-01T00:00:00.000Z", map.get("time"));

Map<String, Object> runtimeVersions = new HashMap<>();
runtimeVersions.put("androidApiLevel", 27);
Expand Down

0 comments on commit 4d3636b

Please sign in to comment.