From e863f72f9f53e4600d1de074bbb9591c2337153a Mon Sep 17 00:00:00 2001 From: Pallab Maiti Date: Fri, 6 Oct 2023 11:01:31 +0530 Subject: [PATCH] feat: metrics for DMT and DBEncryption (#404) --- Sources/Classes/Headers/Public/RSMetricsReporter.h | 11 +++++++++++ Sources/Classes/RSDBPersistentManager.m | 3 +++ Sources/Classes/RSDeviceModeManager.m | 1 + Sources/Classes/RSDeviceModeTransformationManager.m | 7 +++++++ Sources/Classes/RSMetricsReporter.m | 11 +++++++++++ 5 files changed, 33 insertions(+) diff --git a/Sources/Classes/Headers/Public/RSMetricsReporter.h b/Sources/Classes/Headers/Public/RSMetricsReporter.h index c7000856..52916c5c 100644 --- a/Sources/Classes/Headers/Public/RSMetricsReporter.h +++ b/Sources/Classes/Headers/Public/RSMetricsReporter.h @@ -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; @@ -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 diff --git a/Sources/Classes/RSDBPersistentManager.m b/Sources/Classes/RSDBPersistentManager.m index 5fc27b13..fab335d2 100644 --- a/Sources/Classes/RSDBPersistentManager.m +++ b/Sources/Classes/RSDBPersistentManager.m @@ -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; } } @@ -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]]; } @@ -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]]; } diff --git a/Sources/Classes/RSDeviceModeManager.m b/Sources/Classes/RSDeviceModeManager.m index 6492f20b..ac7dffca 100644 --- a/Sources/Classes/RSDeviceModeManager.m +++ b/Sources/Classes/RSDeviceModeManager.m @@ -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]]; diff --git a/Sources/Classes/RSDeviceModeTransformationManager.m b/Sources/Classes/RSDeviceModeTransformationManager.m index 51d4edfd..221bea95 100644 --- a/Sources/Classes/RSDeviceModeTransformationManager.m +++ b/Sources/Classes/RSDeviceModeTransformationManager.m @@ -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 @@ -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"]; @@ -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]; } @@ -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]; @@ -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; diff --git a/Sources/Classes/RSMetricsReporter.m b/Sources/Classes/RSMetricsReporter.m index 64a0507b..045e76f3 100644 --- a/Sources/Classes/RSMetricsReporter.m +++ b/Sources/Classes/RSMetricsReporter.m @@ -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"; @@ -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";