Skip to content

Commit

Permalink
feat: sdk 94 error stats integrations (#309)
Browse files Browse the repository at this point in the history
feat: added error reporting
SDK-409-Enable disable errors through Source Config
chore: added r8 rules to sample app
  • Loading branch information
itsdebs authored Sep 20, 2023
1 parent 3e1a67a commit 96cc9f1
Show file tree
Hide file tree
Showing 31 changed files with 302 additions and 82 deletions.
8 changes: 5 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 33
consumerProguardFiles 'proguard-consumer-rules.pro'
// consumerProguardFiles 'proguard-consumer-rules.pro'
buildConfigField("String", "VERSION_NAME", "\"${VERSION_NAME}\"")
buildConfigField("String", "VERSION_CODE", "\"${VERSION_CODE}\"")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -44,8 +44,10 @@ dependencies {
implementation 'androidx.annotation:annotation:1.6.0'


implementation 'com.rudderstack.android.sdk:rudderreporter:0.1.0'
implementation 'com.rudderstack.kotlin.sdk:gsonrudderadapter:0.1.0'
implementation ('com.rudderstack.android.sdk:rudderreporter:0.2.0')
// implementation(project(path: ':rudderreporter'))
// implementation(project(path: ':gsonrudderadapter'))
implementation 'com.rudderstack.kotlin.sdk:gsonrudderadapter:0.2.0'

// required for new life cycle methods
compileOnly 'androidx.lifecycle:lifecycle-process:2.6.1'
Expand Down
8 changes: 4 additions & 4 deletions core/proguard-consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keep class com.rudderstack.android.sdk.core.* { *; }
-keep class com.rudderstack.android.sdk.core.ecomm.* { *; }
-keep class com.rudderstack.android.sdk.core.util.* { *; }
#
#-keep class com.rudderstack.android.sdk.core.* { *; }
#-keep class com.rudderstack.android.sdk.core.ecomm.* { *; }
#-keep class com.rudderstack.android.sdk.core.util.* { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AppVersion {
RudderLogger.logDebug("Current Installed Version: " + currentVersion);
RudderLogger.logDebug("Current Installed Build: " + currentBuild);
} catch (PackageManager.NameNotFoundException ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.rudderstack.android.sdk.core;

import static com.rudderstack.android.sdk.core.DBPersistentManager.BACKSLASH;
import static com.rudderstack.android.sdk.core.DBPersistentManager.EVENT;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.icu.text.Collator;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand Down Expand Up @@ -160,6 +160,7 @@ static DBPersistentManager getInstance(Application application, DbManagerParams
return factory;
} catch (Exception e) {
RudderLogger.logError("DBPersistentManager: createPersistenceFactory: Failed to instantiate class: " + params.persistenceProviderFactoryClassName);
ReportManager.reportError(e);
}
return null;

Expand Down Expand Up @@ -254,6 +255,7 @@ void flushEvents() {
}
} catch (SQLiteDatabaseCorruptException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
}
}

Expand Down Expand Up @@ -284,13 +286,15 @@ void clearEventsFromDB(List<Integer> messageIds) {
}
} catch (SQLiteDatabaseCorruptException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
}
}

/*
* returns messageIds and messages returned on executing the supplied SQL statement
* */
void getEventsFromDB(List<Integer> messageIds, List<String> messages, String selectSQL) {
void getEventsFromDB(List<Integer> messageIds, List<String> messages, String
selectSQL) {
Map<Integer, Integer> messageIdStatusMap = new HashMap<>();
getEventsFromDB(messageIdStatusMap, messages, selectSQL);
messageIds.addAll(messageIdStatusMap.keySet());
Expand Down Expand Up @@ -336,14 +340,16 @@ void getEventsFromDB(Map<Integer, Integer> messageIdStatusMap,//(id (row_id), st

} catch (SQLiteDatabaseCorruptException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
}
}

/*
* retrieve `count` number of messages from DB and store messageIds and messages separately
* */
//unit test this
void fetchCloudModeEventsFromDB(ArrayList<Integer> messageIds, ArrayList<String> messages, int count) {
void fetchCloudModeEventsFromDB
(ArrayList<Integer> messageIds, ArrayList<String> messages, int count) {
String selectSQL = String.format(Locale.US, "SELECT * FROM %s WHERE %s IN (%d, %d) ORDER BY %s ASC LIMIT %d",
EVENTS_TABLE_NAME, DBPersistentManager.STATUS_COL, DBPersistentManager.STATUS_NEW,
DBPersistentManager.STATUS_DEVICE_MODE_DONE, UPDATED_COL, count);
Expand All @@ -352,15 +358,17 @@ void fetchCloudModeEventsFromDB(ArrayList<Integer> messageIds, ArrayList<String>
}

//unit test this
void fetchDeviceModeEventsFromDb(List<Integer> messageIds, List<String> messages, int count) {
void fetchDeviceModeEventsFromDb(List<Integer> messageIds, List<String> messages,
int count) {
String selectSQL = String.format(Locale.US, "SELECT * FROM %s WHERE %s IN (%d, %d) ORDER BY %s ASC LIMIT %d",
EVENTS_TABLE_NAME, DBPersistentManager.STATUS_COL, DBPersistentManager.STATUS_NEW,
DBPersistentManager.STATUS_CLOUD_MODE_DONE, UPDATED_COL, count);
RudderLogger.logDebug(String.format(Locale.US, "DBPersistentManager: fetchDeviceModeEventsFromDb: selectSQL: %s", selectSQL));
getEventsFromDB(messageIds, messages, selectSQL);
}

public void fetchDeviceModeWithProcessedPendingEventsFromDb(List<Integer> messageIds, List<String> messages, int limit) {
public void fetchDeviceModeWithProcessedPendingEventsFromDb
(List<Integer> messageIds, List<String> messages, int limit) {
String selectSQL = String.format(Locale.US, "SELECT * FROM %s WHERE %s IN (%d, %d) AND %s = %d ORDER BY %s ASC LIMIT %d",
EVENTS_TABLE_NAME, DBPersistentManager.STATUS_COL, DBPersistentManager.STATUS_NEW,
DBPersistentManager.STATUS_CLOUD_MODE_DONE, DM_PROCESSED_COL, DM_PROCESSED_PENDING, UPDATED_COL, limit);
Expand Down Expand Up @@ -438,6 +446,7 @@ private int getCountForCommand(String sql) {

} catch (SQLiteDatabaseCorruptException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
}

return count;
Expand All @@ -460,6 +469,8 @@ void startHandlerThread() {
} catch (SQLiteDatabaseCorruptException | ConcurrentModificationException |
NullPointerException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);

}
};
// Need to perform db operations on a separate thread to support strict mode.
Expand Down Expand Up @@ -678,7 +689,7 @@ public void handleMessage(@NonNull Message msg) {
if (this.persistence.isAccessible()) {
EventInsertionCallback callback = (EventInsertionCallback) msg.obj;
Bundle msgBundle = msg.getData();
String messageJson = msgBundle.getString(EVENT);
String messageJson = msgBundle.getString(DBPersistentManager.EVENT);
long updatedTime = System.currentTimeMillis();
RudderLogger.logDebug(String.format(Locale.US, "DBPersistentManager: " +
"saveEvent: Inserting Message %s into table %s as Updated at %d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import static com.rudderstack.android.sdk.core.ReportManager.LABEL_TYPE;
import static com.rudderstack.android.sdk.core.ReportManager.LABEL_TYPE_DATA_PLANE_URL_INVALID;
import static com.rudderstack.android.sdk.core.ReportManager.LABEL_TYPE_SOURCE_DISABLED;
import static com.rudderstack.android.sdk.core.ReportManager.enableStatsCollection;
import static com.rudderstack.android.sdk.core.ReportManager.incrementDiscardedCounter;
import static com.rudderstack.android.sdk.core.ReportManager.initiateRudderReporter;
import static com.rudderstack.android.sdk.core.ReportManager.isStatsReporterAvailable;
import static com.rudderstack.android.sdk.core.util.Utils.lifeCycleDependenciesExists;

import android.app.Application;
Expand All @@ -15,7 +14,6 @@
import android.os.Looper;
import android.os.Message;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -24,7 +22,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.rudderstack.android.ruddermetricsreporterandroid.Metrics;
import com.rudderstack.android.sdk.core.consent.ConsentFilterHandler;
import com.rudderstack.android.sdk.core.consent.RudderConsentFilter;
import com.rudderstack.android.sdk.core.util.RudderContextSerializer;
Expand Down Expand Up @@ -98,7 +95,8 @@ public void handleMessage(Message msg) {
RudderLogger.logDebug(String.format("EventRepository: constructor: %s", this.config.toString()));

try {

ReportManager.addErrorMetadata(ReportManager.METADATA_SECTION_GZIP, ReportManager.METADATA_GZIP_KEY_IS_ENABLED,
_config.isGzipEnabled());
// initiate RudderPreferenceManager
initiatePreferenceManager(_application);

Expand Down Expand Up @@ -153,6 +151,7 @@ public void handleMessage(Message msg) {

initiateRudderReporterFromPrefetchedConfig();
} catch (Exception ex) {
ReportManager.reportError(ex);
RudderLogger.logError("EventRepository: constructor: Exception occurred: " + ex.getMessage());
RudderLogger.logError(ex.getCause());
}
Expand All @@ -164,7 +163,7 @@ private void initiateRudderReporterFromPrefetchedConfig() {
if (serverConfig != null && serverConfig.source != null
&& serverConfig.source.sourceConfiguration != null) {
RudderLogger.logDebug("EventRepository: constructor: Prefetched source serverConfig is available");
enableStatsCollection(serverConfig.source.sourceConfiguration.getStatsCollection());
enableStatsCollection(application, writeKey, serverConfig.source.sourceConfiguration.getStatsCollection());
} else {
RudderLogger.logDebug("EventRepository: constructor: Prefetched source serverConfig is not available");
}
Expand Down Expand Up @@ -215,7 +214,7 @@ private void initializeDbManager(Application application) {
RudderConfig.DBEncryption dbEncryption = config.getDbEncryption();
DBPersistentManager.DbManagerParams dbManagerParams = new DBPersistentManager.DbManagerParams(dbEncryption.enable,
dbEncryption.getPersistenceProviderFactoryClassName(), dbEncryption.key);
this.dbManager = DBPersistentManager.getInstance(application,dbManagerParams);
this.dbManager = DBPersistentManager.getInstance(application, dbManagerParams);
dbManager.checkForMigrations();
dbManager.startHandlerThread();
}
Expand Down Expand Up @@ -249,6 +248,7 @@ private void updateAuthHeaderString(String writeKey) {
this.authHeaderString = Base64.encodeToString((String.format(Locale.US, "%s:", writeKey)).getBytes(CHARSET_UTF_8), Base64.NO_WRAP);
RudderLogger.logDebug(String.format(Locale.US, "EventRepository: constructor: authHeaderString: %s", this.authHeaderString));
} catch (UnsupportedEncodingException ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex);
}
}
Expand All @@ -274,8 +274,8 @@ private void initiateSDK(@Nullable RudderConsentFilter consentFilter) {
if (serverConfig != null) {
isSDKEnabled = serverConfig.source.isSourceEnabled;
if (isSDKEnabled) {
if(serverConfig.source.sourceConfiguration != null)
enableStatsCollection(serverConfig.source.sourceConfiguration.getStatsCollection());
if (serverConfig.source.sourceConfiguration != null)
enableStatsCollection(application, writeKey, serverConfig.source.sourceConfiguration.getStatsCollection());
dataResidencyManager.setDataResidencyUrls(serverConfig);
dataPlaneUrl = dataResidencyManager.getDataPlaneUrl();
if (dataPlaneUrl == null) {
Expand Down Expand Up @@ -317,32 +317,11 @@ private void initiateSDK(@Nullable RudderConsentFilter consentFilter) {
}
} catch (Exception ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
}
}).start();
}

private void enableStatsCollection( @NonNull SourceConfiguration.StatsCollection statsCollection) {
if(!isStatsReporterAvailable()){
if(statsCollection.getMetrics().isEnabled()){
RudderLogger.logDebug("EventRepository: Creating Metrics Reporter");
initiateRudderReporter(application, writeKey );
return;
}
RudderLogger.logDebug("EventRepository: Metrics collection is not initialized");
return;
}
Metrics rudderMetrics = ReportManager.getMetrics();
if(rudderMetrics == null)
return;
boolean metricsCollection = statsCollection.getMetrics().isEnabled();
if (!metricsCollection) {
RudderLogger.logDebug("EventRepository: Disabling Metrics Collection:");
rudderMetrics.enable(false);
return;
}
RudderLogger.logDebug("EventRepository: Metrics Collection is enabled");

}

private void saveFlushConfig() {
RudderFlushConfig rudderFlushConfig = new RudderFlushConfig(dataPlaneUrl, authHeaderString,
Expand Down Expand Up @@ -499,6 +478,7 @@ void updateAnonymousId(@NonNull String anonymousId) {
try {
this.anonymousIdHeaderString = Base64.encodeToString(RudderContext.getAnonymousId().getBytes(CHARSET_UTF_8), Base64.NO_WRAP);
} catch (Exception ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex.getCause());
}
networkManager.updateAnonymousIdHeaderString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ static NetworkResponses flushEventsToServer(String payload, String dataPlaneUrl,
}
}
} catch (Exception ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex);
}
return NetworkResponses.ERROR;
Expand Down Expand Up @@ -246,6 +247,7 @@ static String getPayloadFromMessages(List<Integer> messageIds, List<String> mess
// finally return the entire payload
return builder.toString();
} catch (Exception ex) {
ReportManager.reportError(ex);
RudderLogger.logError(ex);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void trackDeepLinks(Activity activity) {
message.setType(MessageType.TRACK);
repository.processMessage(message);
} catch (Exception e) {
ReportManager.reportError(e);
RudderLogger.logError("ApplicationLifeCycleManager: trackDeepLinks: Error occurred while tracking deep link" + e);
}
}
Expand All @@ -115,6 +116,7 @@ private void setURIQueryParams(RudderProperty rudderProperty, Uri uri) {
}
}
} catch (Exception e) {
ReportManager.reportError(e);
RudderLogger.logError("ApplicationLifeCycleManager: trackDeepLinks: Failed to get uri query parameters: " + e);
}
rudderProperty.putValue("url", uri.toString());
Expand Down
Loading

0 comments on commit 96cc9f1

Please sign in to comment.