Skip to content

Commit

Permalink
feat: metrics for DMT and DBEncryption
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabmaiti committed Oct 4, 2023
1 parent 850f967 commit 1495c71
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/Classes/Headers/Public/RSMetricsReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ extern NSString *const SDKMETRICS_SC_ATTEMPT_ABORT;
extern NSString *const SDKMETRICS_CM_ATTEMPT_SUCCESS;
extern NSString *const SDKMETRICS_CM_ATTEMPT_RETRY;
extern NSString *const SDKMETRICS_CM_ATTEMPT_ABORT;
extern NSString *const SDKMETRICS_DMT_SUBMITTED;
extern NSString *const SDKMETRICS_DMT_SUCCESS;
extern NSString *const SDKMETRICS_DMT_DISCARD;
extern NSString *const SDKMETRICS_DMT_RETRY;
extern NSString *const SDKMETRICS_DB_ENCRYPT;

extern NSString *const SDKMETRICS_TYPE;
extern NSString *const SDKMETRICS_OPT_OUT;
Expand All @@ -50,6 +55,12 @@ extern NSString *const SDKMETRICS_SOURCE_DISABLED;
extern NSString *const SDKMETRICS_WRITEKEY_INVALID;
extern NSString *const SDKMETRICS_INTEGRATION;
extern NSString *const SDKMETRICS_REQUEST_TIMEOUT;
extern NSString *const SDKMETRICS_CREATED;
extern NSString *const SDKMETRICS_MIGRATE_TO_ENCRYPT;
extern NSString *const SDKMETRICS_MIGRATE_TO_DECRYPT;
extern NSString *const SDKMETRICS_RESOURCE_NOT_FOUND;
extern NSString *const SDKMETRICS_BAD_REQUEST;
extern NSString *const SDKMETRICS_MAX_RETRIES_EXHAUSTED;

@end

Expand Down
3 changes: 3 additions & 0 deletions Sources/Classes/RSDBPersistentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ - (instancetype)initWithDBEncryption:(RSDBEncryption * __nullable)dbEncryption {
if (dbEncryption == nil) {
return [RSDefaultDatabaseProvider new];
} else {
[RSMetricsReporter report:SDKMETRICS_DB_ENCRYPT forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_CREATED} andValue:1];
return dbEncryption.databaseProvider;
}
}
Expand Down Expand Up @@ -102,6 +103,7 @@ - (void)createDB:(RSDBEncryption * __nullable)dbEncryption {
[self closeDB];
[RSUtils removeFile:ENCRYPTED_DB_NAME];
[self openUnencryptedDB];
[RSMetricsReporter report:SDKMETRICS_DB_ENCRYPT forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_MIGRATE_TO_DECRYPT} andValue:1];
} else {
[RSLogger logError:[NSString stringWithFormat:@"RSDBPersistentManager: createDB: Failed to decrypt, error code: %d", code]];
}
Expand Down Expand Up @@ -132,6 +134,7 @@ - (void)createDB:(RSDBEncryption * __nullable)dbEncryption {
[self closeDB];
[RSUtils removeFile:UNENCRYPTED_DB_NAME];
[self openEncryptedDB:dbEncryption.key];
[RSMetricsReporter report:SDKMETRICS_DB_ENCRYPT forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_MIGRATE_TO_ENCRYPT} andValue:1];
} else {
[RSLogger logError:[NSString stringWithFormat:@"RSDBPersistentManager: createDB: Failed to encrypt, error code: %d", code]];
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Classes/RSDeviceModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ -(void) dumpTransformedEvents:(NSArray*) transformedPayloads toDestinationId:(NS
}
RSMessage* transformedMessage = [[RSMessage alloc] initWithDict:transformedPayload[@"event"]];
[self dumpEvent:transformedMessage toDestinations:@[destinationName] withLogTag:@"dumpTransformedEvents"];
[RSMetricsReporter report: SDKMETRICS_DMT_SUCCESS forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: transformedMessage.type} andValue:1];
continue;
}
NSNumber* orderNo = [NSNumber numberWithInt:[transformedPayload[@"orderNo"] intValue]];
Expand Down
7 changes: 7 additions & 0 deletions Sources/Classes/RSDeviceModeTransformationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "RSDeviceModeTransformationManager.h"
#import "RSMetricsReporter.h"

int const DMT_BATCH_SIZE = 12;
int const MAX_RETRIES = 2; // Maximum number of retries
Expand Down Expand Up @@ -60,6 +61,7 @@ - (void) transformationProcessor {
NSString* responsePayload = response.responsePayload;
if (response.state == WRONG_WRITE_KEY) {
[RSLogger logDebug:@"RSDeviceModeTransformationManager: TransformationProcessor: Wrong WriteKey. Aborting the TransformationProcessor."];
[RSMetricsReporter report:SDKMETRICS_DMT_DISCARD forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_WRITEKEY_INVALID} andValue:1];
break;
} else if (response.state == INVALID_URL) {
[RSLogger logDebug:@"RSDeviceModeTransformationManager: TransformationProcessor: Invalid Data Plane URL. Aborting the TransformationProcessor"];
Expand All @@ -69,6 +71,7 @@ - (void) transformationProcessor {
break;
} else if (response.state == BAD_REQUEST) {
[RSLogger logWarn:@"RSDeviceModeTransformationManager: TransformationProcessor: Bad Request, dumping back the original events to the factories"];
[RSMetricsReporter report:SDKMETRICS_DMT_DISCARD forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_BAD_REQUEST} andValue:1];
[strongSelf->deviceModeManager dumpOriginalEventsOnTransformationError:request.batch];
[strongSelf completeDeviceModeEventProcessing:dbMessage];
}
Expand All @@ -78,13 +81,16 @@ - (void) transformationProcessor {
retryCount = 0;
deviceModeSleepCount = 0;
[RSLogger logWarn:[NSString stringWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Failed to transform events even after %d retries, hence dumping back the original events to the factories", MAX_RETRIES]];
[RSMetricsReporter report:SDKMETRICS_DMT_DISCARD forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_MAX_RETRIES_EXHAUSTED} andValue:1];
[strongSelf->deviceModeManager dumpOriginalEventsOnTransformationError:request.batch];
[strongSelf completeDeviceModeEventProcessing:dbMessage];
} else {
[RSLogger logDebug:[NSString stringWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Network Error, Retrying again in %.2f s", (NSTimeInterval)delay/1000]];
[RSMetricsReporter report:SDKMETRICS_DMT_RETRY forMetricType:COUNT withProperties:nil andValue:1];
usleep((useconds_t)delay* 1000);
}
} else if (response.state == RESOURCE_NOT_FOUND) { // So when the customer is not eligible for Device Mode Transformations, we get RESOURCE_NOT_FOUND, and in this case we will dump the original methods itself to the factories.
[RSMetricsReporter report:SDKMETRICS_DMT_DISCARD forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_RESOURCE_NOT_FOUND} andValue:1];
deviceModeSleepCount = 0;
[strongSelf->deviceModeManager dumpOriginalEventsOnTransformationsFeatureDisabled:request.batch];
[strongSelf completeDeviceModeEventProcessing:dbMessage];
Expand Down Expand Up @@ -139,6 +145,7 @@ - (RSTransformationRequest *) __getDeviceModeTransformationRequest:(RSDBMessage
transformationEvent.destinationIds = destinationIds;
transformationEvent.orderNo = [NSNumber numberWithInt:[messageIds[i] intValue]];
[request.batch addObject:transformationEvent];
[RSMetricsReporter report:SDKMETRICS_DMT_SUBMITTED forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: message.type} andValue:1];
}
}
return request;
Expand Down
11 changes: 11 additions & 0 deletions Sources/Classes/RSMetricsReporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ + (void)report:(NSString *)metricName forMetricType:(METRIC_TYPE)metricType with
NSString *const SDKMETRICS_CM_ATTEMPT_SUCCESS = @"cm_attempt_success";
NSString *const SDKMETRICS_CM_ATTEMPT_RETRY = @"cm_attempt_retry";
NSString *const SDKMETRICS_CM_ATTEMPT_ABORT = @"cm_attempt_abort";
NSString *const SDKMETRICS_DMT_SUBMITTED = @"dmt_submitted";
NSString *const SDKMETRICS_DMT_SUCCESS = @"dmt_success";
NSString *const SDKMETRICS_DMT_DISCARD = @"dmt_discard";
NSString *const SDKMETRICS_DMT_RETRY = @"dmt_retry";
NSString *const SDKMETRICS_DB_ENCRYPT = @"db_encrypt";

NSString *const SDKMETRICS_TYPE = @"type";
NSString *const SDKMETRICS_OPT_OUT = @"opt_out";
Expand All @@ -103,3 +108,9 @@ + (void)report:(NSString *)metricName forMetricType:(METRIC_TYPE)metricType with
NSString *const SDKMETRICS_WRITEKEY_INVALID = @"writekey_invalid";
NSString *const SDKMETRICS_INTEGRATION = @"integration";
NSString *const SDKMETRICS_REQUEST_TIMEOUT = @"request_timeout";
NSString *const SDKMETRICS_CREATED = @"created";
NSString *const SDKMETRICS_MIGRATE_TO_ENCRYPT = @"migrate_to_encrypt";
NSString *const SDKMETRICS_MIGRATE_TO_DECRYPT = @"migrate_to_decrypt";
NSString *const SDKMETRICS_RESOURCE_NOT_FOUND = @"resource_not_found";
NSString *const SDKMETRICS_BAD_REQUEST = @"bad_request";
NSString *const SDKMETRICS_MAX_RETRIES_EXHAUSTED = @"max_retries_exhausted";

0 comments on commit 1495c71

Please sign in to comment.