Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oncorruption logging #188

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/com/amplitude/api/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.DefaultDatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteException;
Expand All @@ -21,6 +22,21 @@

class DatabaseHelper extends SQLiteOpenHelper {

private static class DatabaseCorruptionHandler implements DatabaseErrorHandler {
djih marked this conversation as resolved.
Show resolved Hide resolved

DatabaseErrorHandler defaultHandler = new DefaultDatabaseErrorHandler();

@Override
public void onCorruption(SQLiteDatabase dbObj) {

Diagnostics.getLogger().logError(
"DB: corruption detected, deleting database files", new Throwable()
);

defaultHandler.onCorruption(dbObj);
}
}

static final Map<String, DatabaseHelper> instances = new HashMap<String, DatabaseHelper>();

private static final String TAG = "com.amplitude.api.DatabaseHelper";
Expand Down Expand Up @@ -79,7 +95,7 @@ protected DatabaseHelper(Context context) {
}

protected DatabaseHelper(Context context, String instance) {
super(context, getDatabaseName(instance), null, Constants.DATABASE_VERSION);
super(context, getDatabaseName(instance), null, Constants.DATABASE_VERSION, new DatabaseCorruptionHandler());
file = context.getDatabasePath(getDatabaseName(instance));
instanceName = Utils.normalizeInstanceName(instance);
}
Expand Down
29 changes: 28 additions & 1 deletion src/com/amplitude/api/Diagnostics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.amplitude.api;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import org.json.JSONArray;
Expand Down Expand Up @@ -111,10 +113,35 @@ public void run() {
if (event == null) {
event = new JSONObject();
try {

// add attributes to diagnostic event

event.put("error", AmplitudeClient.truncate(error));
event.put("timestamp", System.currentTimeMillis());
event.put("device_id", deviceId);
event.put("count", 1);
event.put("library", String.format("amplitude-android/%s", Constants.VERSION));

// get memory stats
Runtime runtime = Runtime.getRuntime();
long usedMemInMB = (runtime.totalMemory() - runtime.freeMemory()) / 1048576L;
long maxHeapSizeInMB = runtime.maxMemory() / 1048576L;
long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;
event.put("used_mem_mb", usedMemInMB);
event.put("max_heap_mb", maxHeapSizeInMB);
event.put("heap_avail_mb", availHeapSizeInMB);

// get disk stats
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
else {
bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024 * 1024);
event.put("disk_avail_mb", megAvailable);

if (exception != null) {
String stackTrace = Log.getStackTraceString(exception);
Expand All @@ -123,7 +150,7 @@ public void run() {
}
}

// unsent queues are full, make room by removing
// add unsent errors to queue. if full, make room by removing
if (unsentErrorStrings.size() >= diagnosticEventMaxCount) {
for (int i = 0; i < DIAGNOSTIC_EVENT_MIN_COUNT; i++) {
String errorString = unsentErrorStrings.remove(0);
Expand Down
3 changes: 3 additions & 0 deletions test/com/amplitude/api/DiagnosticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ public void testLogError() {
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optString("error"), "test_error");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optString("library"), "amplitude-android/2.21.0");
assertEquals(logger.unsentErrorStrings.get(1), "test_error1");
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optString("error"), "test_error1");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optString("library"), "amplitude-android/2.21.0");
assertEquals(logger.unsentErrorStrings.get(2), "test_error2");
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optString("error"), "test_error2");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optString("library"), "amplitude-android/2.21.0");

// test truncation
logger.setDiagnosticEventMaxCount(7);
Expand Down