From 2a630d88b9756c33d67e6035b6f0134761058a0f Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 13:54:46 -0300 Subject: [PATCH 1/7] Use RBLog on RollbarSender. --- .../Sources/RollbarNotifier/RollbarSender.m | 136 ++++++------------ 1 file changed, 44 insertions(+), 92 deletions(-) diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m index 26a54c2f..58dd418d 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m @@ -9,32 +9,11 @@ @implementation RollbarSender -+ (void)trace:(nonnull NSString *)message - withOptions:(nullable RollbarDeveloperOptions *)developerOptions { - - if (!developerOptions) { - // then, let's use default developer options: - developerOptions = [RollbarDeveloperOptions new]; - } - - if (NO == developerOptions.suppressSdkInfoLogging) { - RBLog(message); - } -} - -+ (void)assertError:(nonnull NSString *)error - withOptions:(nullable RollbarDeveloperOptions *)developerOptions { - - [RollbarSender trace:error withOptions:developerOptions]; - - NSAssert(false, error); -} - - (nullable RollbarPayloadPostReply *)sendPayload:(nonnull NSData *)payload usingConfig:(nonnull RollbarConfig *)config { RollbarPayloadPostReply *reply; - + if (config.developerOptions.transmit) { reply = [self transmitPayload:payload toDestination:config.destination @@ -44,17 +23,17 @@ - (nullable RollbarPayloadPostReply *)sendPayload:(nonnull NSData *)payload } else { reply = [RollbarPayloadPostReply greenReply]; // we just successfully short-circuit here... } - + NSString *payloadString = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding]; if (reply && reply.statusCode == 200) { NSUInteger truncateIndex = MIN(payloadString.length, 256); NSString *truncatedString = [payloadString substringToIndex:truncateIndex]; - [RollbarSender trace:[NSString stringWithFormat:@"Transmitted payload: %@...", truncatedString] withOptions:config.developerOptions]; + RBLog(@"Transmitted payload: %@...", truncatedString); } else if (reply) { - [RollbarSender trace:[NSString stringWithFormat:@"Failed to transmit payload (%li status code): %@", (long)reply.statusCode, payloadString] withOptions:config.developerOptions]; + RBLog(@"Failed to transmit payload (%li status code): %@", (long)reply.statusCode, payloadString); } else { - [RollbarSender trace:[NSString stringWithFormat:@"Failed to transmit payload (no reply): %@", payloadString] withOptions:config.developerOptions]; + RBLog(@"Failed to transmit payload (no reply): %@", payloadString); } return reply; @@ -64,75 +43,52 @@ - (nullable RollbarPayloadPostReply *)transmitPayload:(nonnull NSData *)payload toDestination:(nonnull RollbarDestination *)destination usingDeveloperOptions:(nullable RollbarDeveloperOptions *)developerOptions andHttpProxySettings:(nullable RollbarProxy *)httpProxySettings - andHttpsProxySettings:(nullable RollbarProxy *)httpsProxySettings { - + andHttpsProxySettings:(nullable RollbarProxy *)httpsProxySettings +{ NSAssert(payload, @"The payload must be initialized!"); - NSAssert(destination, @"The destination must be initialized!"); NSAssert(destination.endpoint, @"The destination endpoint must be initialized!"); NSAssert(destination.accessToken, @"The destination access token must be initialized!"); - - if (!developerOptions) { - // then, let's use default developer options: - developerOptions = [RollbarDeveloperOptions new]; - } - - if (!httpProxySettings) { - // then, let's use default proxy settingd: - httpProxySettings = [RollbarProxy new]; - } - if (!httpsProxySettings) { - // then, let's use default proxy settingd: - httpsProxySettings = [RollbarProxy new]; - } - + developerOptions = developerOptions ?: [RollbarDeveloperOptions new]; + httpProxySettings = httpProxySettings ?: [RollbarProxy new]; + httpsProxySettings = httpsProxySettings ?: [RollbarProxy new]; + NSHTTPURLResponse *response = [self postPayload:payload toDestination:destination usingDeveloperOptions:developerOptions andHttpProxySettings:httpProxySettings andHttpsProxySettings:httpsProxySettings]; - - RollbarPayloadPostReply *reply = [RollbarPayloadPostReply replyFromHttpResponse:response]; - return reply; + return [RollbarPayloadPostReply replyFromHttpResponse:response]; } - (nullable NSHTTPURLResponse *)postPayload:(nonnull NSData *)payload toDestination:(nonnull RollbarDestination *)destination usingDeveloperOptions:(nonnull RollbarDeveloperOptions *)developerOptions andHttpProxySettings:(nonnull RollbarProxy *)httpProxySettings - andHttpsProxySettings:(nonnull RollbarProxy *)httpsProxySettings { - - + andHttpsProxySettings:(nonnull RollbarProxy *)httpsProxySettings +{ NSURL *url = [NSURL URLWithString:destination.endpoint]; - if (nil == url) { - NSString *message = - [NSString stringWithFormat:@"The destination endpoint URL is malformed: %@", destination.endpoint]; - [RollbarSender assertError:message - withOptions:developerOptions]; + if (url == nil) { + RBLog(@"The destination endpoint URL is malformed: %@", destination.endpoint); return nil; } - + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:destination.accessToken forHTTPHeaderField:@"X-Rollbar-Access-Token"]; [request setHTTPBody:payload]; - - //__block BOOL result = NO; + __block NSHTTPURLResponse *httpResponse = nil; - - // This requires iOS 7.0+ + dispatch_semaphore_t sem = dispatch_semaphore_create(0); - + NSURLSession *session = [NSURLSession sharedSession]; - - if (httpProxySettings.enabled - || httpsProxySettings.enabled) { - - NSDictionary *connectionProxyDictionary = - @{ + + if (httpProxySettings.enabled || httpsProxySettings.enabled) { + NSDictionary *connectionProxyDictionary = @{ @"HTTPEnable" : [NSNumber numberWithBool:httpProxySettings.enabled], @"HTTPProxy" : httpProxySettings.proxyUrl, @"HTTPPort" : [NSNumber numberWithUnsignedInteger:httpProxySettings.proxyPort], @@ -140,29 +96,25 @@ - (nullable NSHTTPURLResponse *)postPayload:(nonnull NSData *)payload @"HTTPSProxy" : httpsProxySettings.proxyUrl, @"HTTPSPort" : [NSNumber numberWithUnsignedInteger:httpsProxySettings.proxyPort] }; - - NSURLSessionConfiguration *sessionConfig = - [NSURLSessionConfiguration ephemeralSessionConfiguration]; + + NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration]; sessionConfig.connectionProxyDictionary = connectionProxyDictionary; session = [NSURLSession sessionWithConfiguration:sessionConfig]; } - - NSURLSessionDataTask *dataTask = - [session dataTaskWithRequest:request - completionHandler:^( - NSData * _Nullable data, - NSURLResponse * _Nullable response, - NSError * _Nullable error) { - httpResponse = [self checkPayloadResponse:response - error:error - data:data - usingDeveloperOptions:developerOptions]; - dispatch_semaphore_signal(sem); - }]; + + NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request + completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + httpResponse = [self checkPayloadResponse:response + error:error + data:data + usingDeveloperOptions:developerOptions]; + dispatch_semaphore_signal(sem); + }]; + [dataTask resume]; - + dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); - + return httpResponse; } @@ -172,20 +124,20 @@ - (nullable NSHTTPURLResponse *)checkPayloadResponse:(NSURLResponse *)response usingDeveloperOptions:(nonnull RollbarDeveloperOptions *)developerOptions { if (error) { - [RollbarSender trace:@"There was an error reporting to Rollbar:" withOptions:developerOptions]; - [RollbarSender trace:[NSString stringWithFormat:@" Error: %@", [error localizedDescription]] withOptions:developerOptions]; + RBLog(@"There was an error reporting to Rollbar:"); + RBLog(@"\tError: %@", [error localizedDescription]); return nil; } - + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if (httpResponse && [httpResponse statusCode] == 200) { - [RollbarSender trace:[NSString stringWithFormat:@"OK response from Rollar: %ld", [httpResponse statusCode]] withOptions:developerOptions]; + RBLog(@"OK response from Rollar: %ld", [httpResponse statusCode]); return httpResponse; } - [RollbarSender trace:@"There was a problem reporting to Rollbar:" withOptions:developerOptions]; - [RollbarSender trace:[NSString stringWithFormat:@" Response: %@", response] withOptions:developerOptions]; - [RollbarSender trace:[NSString stringWithFormat:@" Response data: %@", data] withOptions:developerOptions]; + RBLog(@"There was a problem reporting to Rollbar:"); + RBLog(@"\tResponse: %@", response); + RBLog(@"\tResponse data: %@", data); return nil; } From ae6a5da5d9c5090e7ae6fb86422a1504ee77e7af Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 15:22:03 -0300 Subject: [PATCH 2/7] Fixed RollbarSender and Thread ignoring any response errors from server --- .../iosAppSwift/iosAppSwift/ContentView.swift | 12 +++-- .../RollbarNotifier/RollbarInternalLogging.m | 6 +-- .../Sources/RollbarNotifier/RollbarSender.m | 44 ++++++----------- .../Sources/RollbarNotifier/RollbarThread.m | 49 +++++++++---------- 4 files changed, 47 insertions(+), 64 deletions(-) diff --git a/Demos/iosAppSwift/iosAppSwift/ContentView.swift b/Demos/iosAppSwift/iosAppSwift/ContentView.swift index cfa8d429..097ab81c 100644 --- a/Demos/iosAppSwift/iosAppSwift/ContentView.swift +++ b/Demos/iosAppSwift/iosAppSwift/ContentView.swift @@ -50,6 +50,7 @@ struct ContentView: View { ScrollView { VStack { Group { + button("Log a message", action: example.logMessage) button("Manual Logging Example", action: example.manualLogging) .padding(.bottom) button("Force unwrap nil") { example.forceUnwrapNil(Int?.none) } @@ -90,12 +91,15 @@ struct ContentView_Previews: PreviewProvider { struct Example { let logger = RollbarLogger(configuration: Rollbar.configuration()) - /// Some different ways to explicitly log an error to Rollbar. - func manualLogging() { + /// Log a single informational message to Rollbar. + func logMessage() { Rollbar.infoMessage( "Rollbar is up and running! Enjoy your remote error and log monitoring...", data: ["key_x": "value_x", "key_y": "value_y"]) + } + /// Some different ways to explicitly log an error to Rollbar. + func manualLogging() { Rollbar.log(.error, message: "My log message") let extraInfo = ["item_1": "value_1", "item_2": "value_2"] @@ -175,8 +179,8 @@ struct Example { print("duplicateKeys: \(key)") var map = [key: 1, key: 1] // the illegal op. // implementation detail: - // this function requires a bit more trickery to prevent - // the optimizer from simply removing the line above. + // this function requires a bit more effectful trickery to + // prevent the optimizer from simply removing the line above. if let x = map[key] { map[key] = x + x } diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarInternalLogging.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarInternalLogging.m index 3eb98c21..b69e1f40 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarInternalLogging.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarInternalLogging.m @@ -9,8 +9,7 @@ void RBLog(NSString *format, ...) { va_list args; va_start(args, format); - fprintf( - stdout, "[Rollbar] %s\n", + fprintf(stdout, "[Rollbar] %s\n", [[[NSString alloc] initWithFormat:format arguments:args] UTF8String]); va_end(args); #endif @@ -24,8 +23,7 @@ void RBErr(NSString *format, ...) { va_list args; va_start(args, format); - fprintf( - stderr, "[Rollbar] %s\n", + fprintf(stderr, "[Rollbar] %s\n", [[[NSString alloc] initWithFormat:format arguments:args] UTF8String]); va_end(args); #endif diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m index 58dd418d..2089b251 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m @@ -25,15 +25,14 @@ - (nullable RollbarPayloadPostReply *)sendPayload:(nonnull NSData *)payload } NSString *payloadString = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding]; + NSString *truncatedPayload = [payloadString substringToIndex:MIN(payloadString.length, 128)]; if (reply && reply.statusCode == 200) { - NSUInteger truncateIndex = MIN(payloadString.length, 256); - NSString *truncatedString = [payloadString substringToIndex:truncateIndex]; - RBLog(@"Transmitted payload: %@...", truncatedString); + RBLog(@"Transmitted payload: %@", truncatedPayload); } else if (reply) { - RBLog(@"Failed to transmit payload (%li status code): %@", (long)reply.statusCode, payloadString); + RBLog(@"Failed to transmit payload (%li status code): %@", (long)reply.statusCode, truncatedPayload); } else { - RBLog(@"Failed to transmit payload (no reply): %@", payloadString); + RBLog(@"Failed to transmit payload (no reply): %@", truncatedPayload); } return reply; @@ -83,7 +82,7 @@ - (nullable NSHTTPURLResponse *)postPayload:(nonnull NSData *)payload __block NSHTTPURLResponse *httpResponse = nil; - dispatch_semaphore_t sem = dispatch_semaphore_create(0); + dispatch_semaphore_t sem = dispatch_semaphore_create(NULL); NSURLSession *session = [NSURLSession sharedSession]; @@ -104,10 +103,7 @@ - (nullable NSHTTPURLResponse *)postPayload:(nonnull NSData *)payload NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - httpResponse = [self checkPayloadResponse:response - error:error - data:data - usingDeveloperOptions:developerOptions]; + httpResponse = [self checkPayloadResponse:response error:error]; dispatch_semaphore_signal(sem); }]; @@ -118,28 +114,18 @@ - (nullable NSHTTPURLResponse *)postPayload:(nonnull NSData *)payload return httpResponse; } -- (nullable NSHTTPURLResponse *)checkPayloadResponse:(NSURLResponse *)response - error:(NSError *)error - data:(NSData *)data - usingDeveloperOptions:(nonnull RollbarDeveloperOptions *)developerOptions -{ - if (error) { - RBLog(@"There was an error reporting to Rollbar:"); - RBLog(@"\tError: %@", [error localizedDescription]); - return nil; - } - +- (nullable NSHTTPURLResponse *)checkPayloadResponse:(NSURLResponse *)response error:(NSError *)error { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; - if (httpResponse && [httpResponse statusCode] == 200) { - RBLog(@"OK response from Rollar: %ld", [httpResponse statusCode]); - return httpResponse; - } - RBLog(@"There was a problem reporting to Rollbar:"); - RBLog(@"\tResponse: %@", response); - RBLog(@"\tResponse data: %@", data); + if (httpResponse.statusCode == 200) { + RBLog(@"OK response from Rollar"); + } else { + RBLog(@"There was a problem reporting to Rollbar:"); + RBLog(@"\tError: %@", [error localizedDescription]); + RBLog(@"\tResponse: %@", httpResponse); + } - return nil; + return httpResponse; } @end diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m index 17ddec2d..26985972 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m @@ -496,21 +496,16 @@ - (void)processSavedPayload:(nonnull NSDictionary *)payl return; } - RollbarTriStateFlag success = RollbarTriStateFlag_On; + RollbarTriStateFlag result = RollbarTriStateFlag_On; if (!config) { - success = [self sendPayload:jsonPayload]; // backward compatibility with just upgraded very old SDKs... - } - else if (config.developerOptions.transmit) { - success = [self sendPayload:jsonPayload usingConfig:config]; + result = [self sendPayload:jsonPayload]; // backward compatibility with just upgraded very old SDKs... + } else if (config.developerOptions.transmit) { + result = [self sendPayload:jsonPayload usingConfig:config]; } NSString *payloadsLogFile = nil; - NSString *sdkLogTrace = (RollbarTriStateFlag_None == success) ? nil - : [NSString stringWithFormat:@"%@ payload: %@", - (RollbarTriStateFlag_On == success) ? @"Transmitted" : @"Dropped", - [[NSString alloc] initWithData:jsonPayload encoding:NSUTF8StringEncoding] - ]; - switch(success) { + + switch (result) { case RollbarTriStateFlag_On: // The payload is fully processed and transmitted. // It can be removed from the repo: @@ -544,20 +539,7 @@ - (void)processSavedPayload:(nonnull NSDictionary *)payl [RollbarFileWriter appendSafelyData:jsonPayload toFile:payloadsLogFilePath]; } - if (!config.developerOptions.suppressSdkInfoLogging) { - NSString *sdkLogTrace = nil; - switch(success) { - case RollbarTriStateFlag_On: - RBLog(@"Transmitted payload: %@", [[NSString alloc] initWithData:jsonPayload encoding:NSUTF8StringEncoding]); - break; - case RollbarTriStateFlag_Off: - RBLog(@"Dropped payload: %@", [[NSString alloc] initWithData:jsonPayload encoding:NSUTF8StringEncoding]); - break; - case RollbarTriStateFlag_None: - RBErr(@"Couldn't transmit (and will try) payload: %@", [[NSString alloc] initWithData:jsonPayload encoding:NSUTF8StringEncoding]); - break; - } - } + RBLog([self loggableStringFromPayload:jsonPayload result:result]); } - (void)processSavedItems { @@ -656,7 +638,21 @@ - (void)captureTelemetryDataForNetwork:(BOOL)reachable { #endif } -#pragma mark - Sigleton pattern +#pragma mark - + +- (NSString *)loggableStringFromPayload:(NSData *)jsonPayload result:(RollbarTriStateFlag)result { + NSString *payloadString = [[NSString alloc] initWithData:jsonPayload encoding:NSUTF8StringEncoding]; + NSString *truncatedPayload = [payloadString substringToIndex:MIN(payloadString.length, 128)]; + + NSString *resultString = + result == RollbarTriStateFlag_On ? @"Transmitted" : + result == RollbarTriStateFlag_Off ? @"Dropped" : + @"Unavailable will retry"; + + return [NSString stringWithFormat:@"%@ payload: %@", resultString, truncatedPayload]; +} + +#pragma mark - Singleton pattern + (nonnull instancetype)sharedInstance { @@ -664,7 +660,6 @@ + (nonnull instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - singleton = [[self alloc] initWithTarget:self selector:@selector(run) object:nil]; From b7e2b89e149e9011e60845707cf47ccb0a93ba6e Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 16:48:47 -0300 Subject: [PATCH 3/7] Fixed rate limit header fields not being read correctly. --- .../RollbarNotifier/RollbarPayloadPostReply.m | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadPostReply.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadPostReply.m index 9f0ae7d1..2943da96 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadPostReply.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadPostReply.m @@ -1,54 +1,36 @@ #import "RollbarPayloadPostReply.h" -/// Rollbar API Service enforced payload rate limit: static NSString * const RESPONSE_HEADER_RATE_LIMIT = @"x-rate-limit-limit"; -/// Rollbar API Service enforced remaining payload count until the limit is reached: -static NSString * const RESPONSE_HEADER_REMAINING_COUNT = @"x-rate-limit-remaining"; -/// Rollbar API Service enforced rate limit reset time for the current limit window: -static NSString * const RESPONSE_HEADER_RESET_TIME = @"x-rate-limit-reset"; -/// Rollbar API Service enforced rate limit remaining seconds of the current limit window: -static NSString * const RESPONSE_HEADER_REMAINING_SECONDS = @"x-rate-limit-remaining-seconds"; +static NSString * const RESPONSE_HEADER_RESET_TIME = @"x-rate-limit-reset"; // rate limit reset time for the current limit window +static NSString * const RESPONSE_HEADER_REMAINING_COUNT = @"x-rate-limit-remaining"; // remaining payload count until the limit is reached +static NSString * const RESPONSE_HEADER_REMAINING_SECONDS = @"x-rate-limit-remaining-seconds"; // rate limit remaining seconds of the current limit window @implementation RollbarPayloadPostReply + (nullable RollbarPayloadPostReply *)replyFromHttpResponse:(nonnull NSHTTPURLResponse *)httpResponse { - - NSUInteger rateLimit = - [NSNumber numberWithLongLong:[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_RATE_LIMIT]] - .unsignedIntegerValue; - - NSUInteger remainingCount = - [NSNumber numberWithLongLong:[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_REMAINING_COUNT]] - .unsignedIntegerValue; + NSInteger rateLimit = [[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_RATE_LIMIT] integerValue]; + NSInteger remainingCount = [[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_REMAINING_COUNT] integerValue]; + NSInteger remainingSeconds = [[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_REMAINING_SECONDS] integerValue]; - NSUInteger remainingSeconds = - [NSNumber numberWithLongLong:[httpResponse valueForHTTPHeaderField:RESPONSE_HEADER_REMAINING_SECONDS]] - .unsignedIntegerValue; - return [[RollbarPayloadPostReply alloc] initWithStatusCode:httpResponse.statusCode rateLimit:rateLimit remainingCount:remainingCount - remainingSeconds:remainingSeconds - ]; + remainingSeconds:remainingSeconds]; } - (instancetype)initWithStatusCode:(NSUInteger)statusCode rateLimit:(NSUInteger)rateLimit remainingCount:(NSUInteger)remainingCount - remainingSeconds:(NSUInteger)remainingSeconds { - + remainingSeconds:(NSUInteger)remainingSeconds +{ if (self = [super init]) { self->_statusCode = statusCode; self->_rateLimit = rateLimit; self->_remainingCount = remainingCount; self->_remainingSeconds = remainingSeconds; - - if (self->_remainingCount > 0) { - self->_nextPostTime = [[NSDate alloc] init]; - } - else { - self->_nextPostTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self->_remainingSeconds]; - } + self->_nextPostTime = self->_remainingCount > 0 + ? [[NSDate alloc] init] + : [[NSDate alloc] initWithTimeIntervalSinceNow:self->_remainingSeconds]; } return self; From e721f9190de3122c657c856293e8bf1ed9bb9397 Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 16:49:38 -0300 Subject: [PATCH 4/7] Fixed rate limit time not being awaited correctly. --- .../RollbarDestinationRecord.m | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarDestinationRecord.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarDestinationRecord.m index ccd91b86..86520b3f 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarDestinationRecord.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarDestinationRecord.m @@ -1,5 +1,6 @@ #import "RollbarDestinationRecord.h" #import "RollbarRegistry.h" +#import "RollbarInternalLogging.h" @implementation RollbarDestinationRecord { @private @@ -49,22 +50,14 @@ - (instancetype)initWithDestinationID:(nonnull NSString *)destinationID #pragma mark - methods - (BOOL)canPost { - if (!self->_nextEarliestPost) { return NO; } - - if (NSOrderedDescending == [self->_nextEarliestPost compare:[NSDate date]]) { - return NO; - } - else { - return YES; - } + + return [self->_nextEarliestPost compare:[NSDate date]] != NSOrderedDescending; } - (BOOL)canPostWithConfig:(nonnull RollbarConfig *)config { - - if (self->_nextLocalWindowStart && (self->_localWindowCount >= config.loggingOptions.maximumReportsPerMinute)) { // we already exceeded local rate limits, let's wait till the next local rate limiting window: //self->_nextEarliestPost = self->_nextLocalWindowStart; @@ -74,13 +67,14 @@ - (BOOL)canPostWithConfig:(nonnull RollbarConfig *)config { if (!self->_nextEarliestPost) { return NO; } - - if (NSOrderedDescending == [self->_nextEarliestPost compare:[NSDate date]]) { - return NO; - } - else { - return YES; + + BOOL shouldPost = [self->_nextEarliestPost compare:[NSDate date]] != NSOrderedDescending; + + if (shouldPost) { + RBLog(@"%@ ≤ %@ :: GO", self->_nextEarliestPost, [NSDate date]); } + + return shouldPost; } - (void)recordPostReply:(nullable RollbarPayloadPostReply *)reply { @@ -117,11 +111,10 @@ - (void)recordPostReply:(nullable RollbarPayloadPostReply *)reply { default: self->_nextServerWindowStart = [NSDate dateWithTimeIntervalSinceNow:reply.remainingSeconds];; self->_serverWindowRemainingCount = reply.remainingCount; - if (self->_nextLocalWindowStart ) { + if (self->_nextLocalWindowStart) { self->_localWindowCount = 0; self->_nextLocalWindowStart = [NSDate dateWithTimeIntervalSinceNow:60]; - } - else { + } else { self->_localWindowCount += 1; } break; @@ -172,11 +165,4 @@ - (nonnull NSString *)description { return description; } -//- (NSString *)debugDescription { -// NSString *description = [NSString stringWithFormat:@"totalLoggerRecords: %lu", -// (unsigned long)[self totalLoggerRecords] -// ]; -// return description; -//} - @end From fbc8d3b752b506495d53c778661012e0c9b224e0 Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 16:50:05 -0300 Subject: [PATCH 5/7] Log how many payloads are left in the queue. --- .../RollbarNotifier/RollbarPayloadRepository.h | 2 ++ .../RollbarNotifier/RollbarPayloadRepository.m | 18 ++++++++++++++++++ .../Sources/RollbarNotifier/RollbarThread.m | 18 ++++++++---------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.h b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.h index 910208d7..d8bb2c05 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.h +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.h @@ -76,6 +76,8 @@ NS_ASSUME_NONNULL_BEGIN - (nonnull NSArray *> *)getAllPayloads; +- (NSInteger)getPayloadCount; + - (BOOL)removePayloadByID:(nonnull NSString *)payloadID; - (BOOL)removePayloadsOlderThan:(nonnull NSDate *)cutoffTime; diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.m index 49049627..44237b38 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarPayloadRepository.m @@ -399,6 +399,24 @@ - (BOOL)removeAllDestinations { return result; } +- (NSInteger)getPayloadCount { + const char *sql = "SELECT COUNT(*) FROM payloads"; + sqlite3_stmt *statement; + NSInteger count = 0; + + if (sqlite3_prepare_v2(self->_db, sql, -1, &statement, NULL) == SQLITE_OK) { + while (sqlite3_step(statement) == SQLITE_ROW) { + count = sqlite3_column_int(statement, 0); + break; + } + } else { + RBErr(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(self->_db)); + } + + sqlite3_finalize(statement); + return count; +} + - (BOOL)removePayloadByID:(nonnull NSString *)payloadID { NSString *sql = diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m index 26985972..58895b07 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarThread.m @@ -495,6 +495,8 @@ - (void)processSavedPayload:(nonnull NSDictionary *)payl } return; } + + RBLog(@"Processing %d payloads left", [self->_payloadsRepo getPayloadCount]); RollbarTriStateFlag result = RollbarTriStateFlag_On; if (!config) { @@ -551,23 +553,21 @@ - (void)processSavedItems { return; } #endif - - NSArray *> *payloads = [self->_payloadsRepo getPayloadsWithOffset:0 andLimit:5]; - for(NSDictionary *payload in payloads) { + + NSArray *payloads = [self->_payloadsRepo getPayloadsWithOffset:0 andLimit:5]; + for (NSDictionary *payload in payloads) { @try { [self processSavedPayload:payload]; } @catch (NSException *exception) { RBErr(@"Payload processing EXCEPTION: %@", exception); - } @finally { } } } - (RollbarTriStateFlag)sendPayload:(nonnull NSData *)payload - usingConfig:(nonnull RollbarConfig *)config { - + usingConfig:(nonnull RollbarConfig *)config +{ if (!payload || !config) { - return RollbarTriStateFlag_Off; //obviously invalid payload to sent or invalid destination... } @@ -576,9 +576,7 @@ - (RollbarTriStateFlag)sendPayload:(nonnull NSData *)payload return RollbarTriStateFlag_None; // nothing obviously wrong with the payload - just can not send at the moment } - RollbarPayloadPostReply *reply = [[RollbarSender new] sendPayload:payload - usingConfig:config - ]; + RollbarPayloadPostReply *reply = [[RollbarSender new] sendPayload:payload usingConfig:config]; [record recordPostReply:reply]; if (!reply) { From 1153ee180a1589adf9757e2bdd3fef47bf45ecd3 Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 16:50:24 -0300 Subject: [PATCH 6/7] Remove logging noise. --- .../Sources/RollbarNotifier/RollbarSender.m | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m index 2089b251..b6261c98 100644 --- a/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m +++ b/RollbarNotifier/Sources/RollbarNotifier/RollbarSender.m @@ -24,17 +24,6 @@ - (nullable RollbarPayloadPostReply *)sendPayload:(nonnull NSData *)payload reply = [RollbarPayloadPostReply greenReply]; // we just successfully short-circuit here... } - NSString *payloadString = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding]; - NSString *truncatedPayload = [payloadString substringToIndex:MIN(payloadString.length, 128)]; - - if (reply && reply.statusCode == 200) { - RBLog(@"Transmitted payload: %@", truncatedPayload); - } else if (reply) { - RBLog(@"Failed to transmit payload (%li status code): %@", (long)reply.statusCode, truncatedPayload); - } else { - RBLog(@"Failed to transmit payload (no reply): %@", truncatedPayload); - } - return reply; } From 612e7518e870113f9a5269a33780f7bf6580a185 Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Tue, 28 Mar 2023 17:42:33 -0300 Subject: [PATCH 7/7] Demo: Remark that suppressing internal logging is the default. --- Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift b/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift index 807fc1d5..066112dd 100644 --- a/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift +++ b/Demos/iosAppSwift/iosAppSwift/iosAppSwiftApp.swift @@ -34,7 +34,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { // Optionally anonymize the IP address //config.loggingOptions.captureIp = RollbarCaptureIpType.anonymize - // Suppress Rollbar event being logged (e.g. in XCode debug logs) + // Suppress (default) Rollbar internal logging config.developerOptions.suppressSdkInfoLogging = true config.telemetry.enabled = true