Skip to content

Commit 5225a2c

Browse files
authored
NR-267244 changed the way watchOS determines offline (#244)
* NR-267244 changed the way watchOS determines offline * Added unit tests for currentReachabilityStatusTo * Changed the url defines to use the Constants.h file
1 parent 9eda379 commit 5225a2c

File tree

13 files changed

+103
-10
lines changed

13 files changed

+103
-10
lines changed

Agent/Analytics/Constants.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ extern NSString *const kNRMA_Attrib_file_private;
9090
extern NSString *const kNRMA_EventStoreFilename;
9191

9292
extern NSString *const kNRMA_Offline_folder;
93+
94+
extern NSString *const kNRMA_Collector_connect_url;
95+
extern NSString *const kNRMA_Collector_data_url;
96+
extern NSString *const kNRMA_Collector_hex_url;
97+
9398
// Integer Analytics Constants
9499
static int kNRMA_Attrib_Max_Name_Length = 256;
95100
static int kNRMA_Attrib_Max_Value_Size_Bytes = 4096;

Agent/Analytics/Constants.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@
9696
NSString * const kNRMA_EventStoreFilename = @"eventsStore.txt";
9797

9898
NSString * const kNRMA_Offline_folder = @"offlineStorage";
99+
100+
NSString * const kNRMA_Collector_connect_url = @"/mobile/v4/connect";
101+
NSString * const kNRMA_Collector_data_url = @"/mobile/v3/data";
102+
NSString * const kNRMA_Collector_hex_url = @"/mobile/f";

Agent/Analytics/Events/NRMAMobileEvent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ - (nonnull instancetype) initWithTimestamp:(NSTimeInterval)timestamp
3535
if([NRMAFlags shouldEnableOfflineStorage]) {
3636
NRMAReachability* r = [NewRelicInternalUtils reachability];
3737
@synchronized(r) {
38+
#if TARGET_OS_WATCH
39+
NRMANetworkStatus status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostDataURL]]];
40+
#else
3841
NRMANetworkStatus status = [r currentReachabilityStatus];
42+
#endif
3943
if (status == NotReachable) {
4044
[self addAttribute:kNRMA_Attrib_offline value:@YES];
4145
}

Agent/Analytics/NRMAAnalytics.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,11 @@ - (BOOL) checkOfflineStatus {
912912
if([NRMAFlags shouldEnableOfflineStorage]) {
913913
NRMAReachability* r = [NewRelicInternalUtils reachability];
914914
@synchronized(r) {
915+
#if TARGET_OS_WATCH
916+
NRMANetworkStatus status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostDataURL]]];
917+
#else
915918
NRMANetworkStatus status = [r currentReachabilityStatus];
919+
#endif
916920
return (status == NotReachable);
917921
}
918922
}

Agent/General/NewRelicAgentInternal.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,11 @@ - (void) onSessionStart {
527527
NRMAReachability* r = [NewRelicInternalUtils reachability];
528528
NRMANetworkStatus status;
529529
@synchronized(r) {
530+
#if TARGET_OS_WATCH
531+
status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostHexURL]]];
532+
#else
530533
status = [r currentReachabilityStatus];
534+
#endif
531535
}
532536
if ([NRMAFlags shouldEnableHandledExceptionEvents]) {
533537
self.handledExceptionsController = [[NRMAHandledExceptions alloc] initWithAnalyticsController:self.analyticsController

Agent/HandledException/NRMAHandledExceptions.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <Analytics/AnalyticsController.hpp>
2222
#import "NRMABool.h"
2323
#import "NRMASupportMetricHelper.h"
24+
#import "Constants.h"
2425

2526
@interface NRMAAnalytics(Protected)
2627
// Because the NRMAAnalytics class interfaces with non Objective-C++ files, we cannot expose the API on the header. Therefore, we must use this reference.
@@ -76,11 +77,10 @@ - (instancetype) initWithAnalyticsController:(NRMAAnalytics*)analytics
7677
std::vector<std::shared_ptr<NewRelic::Hex::Report::Library>> libs;
7778
NSString* appToken = agentConfiguration.applicationToken.value;
7879
NSString* protocol = agentConfiguration.useSSL?@"https://":@"http://";
79-
NSString* hexCollectorPath = @"/mobile/f";
8080
NSString* collectorHost = [NSString stringWithFormat:@"%@%@%@",
8181
protocol,
8282
agentConfiguration.collectorHost,
83-
hexCollectorPath];
83+
kNRMA_Collector_hex_url];
8484

8585
NSString* version = [NRMAAgentConfiguration connectionInformation].applicationInformation.appVersion;
8686

@@ -135,7 +135,11 @@ - (void) onHarvest {
135135
if([NRMAFlags shouldEnableOfflineStorage]) {
136136
NRMAReachability* r = [NewRelicInternalUtils reachability];
137137
@synchronized(r) {
138+
#if TARGET_OS_WATCH
139+
NRMANetworkStatus status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostHexURL]]];
140+
#else
138141
NRMANetworkStatus status = [r currentReachabilityStatus];
142+
#endif
139143
if (status != NotReachable) {
140144
[self processAndPublishPersistedReports]; // When using offline we always want to send from persisted because the keyContext doesn't persist.
141145
_controller->resetKeyContext();
@@ -161,7 +165,11 @@ - (void) checkOffline:(std::shared_ptr<NewRelic::Hex::Report::HexReport>) report
161165
if([NRMAFlags shouldEnableOfflineStorage]) {
162166
NRMAReachability* r = [NewRelicInternalUtils reachability];
163167
@synchronized(r) {
168+
#if TARGET_OS_WATCH
169+
NRMANetworkStatus status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostHexURL]]];
170+
#else
164171
NRMANetworkStatus status = [r currentReachabilityStatus];
172+
#endif
165173
if (status == NotReachable) {
166174
report->setAttributeNoValidation(__kNRMA_Attrib_offline, true);
167175
}

Agent/Harvester/NRMAHarvesterConnection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#import "NRMAConnection.h"
1414
#import "NRMAOfflineStorage.h"
1515

16-
#define kCOLLECTOR_CONNECT_URI @"/mobile/v4/connect"
17-
#define kCOLLECTOR_DATA_URL @"/mobile/v3/data"
1816
#define kAPPLICATION_TOKEN_HEADER @"X-App-License-Key"
1917
#define kCONNECT_TIME_HEADER @"X-NewRelic-Connect-Time"
2018

Agent/Harvester/NRMAHarvesterConnection.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "NRMAMeasurements.h"
1212
#import <zlib.h>
1313
#import "NRMATaskQueue.h"
14+
#import "Constants.h"
1415
#import <time.h>
1516
#import "NRMAHarvesterConnection+GZip.h"
1617
#import "NRMASupportMetricHelper.h"
@@ -230,12 +231,12 @@ - (NSURLRequest*) createDataPost:(NSString *)message
230231
}
231232
- (NSString*) collectorConnectURL
232233
{
233-
return [self collectorHostURL:(NSString*)kCOLLECTOR_CONNECT_URI];
234+
return [self collectorHostURL:(NSString*)kNRMA_Collector_connect_url];
234235
}
235236

236237
- (NSString*) collectorHostDataURL
237238
{
238-
return [self collectorHostURL:(NSString*)kCOLLECTOR_DATA_URL];
239+
return [self collectorHostURL:(NSString*)kNRMA_Collector_data_url];
239240
}
240241

241242
- (NSString*) collectorHostURL:(NSString*)resource

Agent/Utilities/NewRelicInternalUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ NSTimeInterval NRMAMillisecondTimestamp(void);
5252
// Returns the connection type, wifi, ethernet, or cellular.
5353
+ (NSString*) connectionType;
5454

55+
// Determines if a url is reachable.
56+
+ (NRMANetworkStatus)currentReachabilityStatusTo:(NSURL*)url;
57+
58+
// Returns the url for the data endpoint.
59+
+ (NSString*) collectorHostDataURL;
60+
61+
// Returns the url for the hex endpoint.
62+
+ (NSString*) collectorHostHexURL;
63+
5564
// Returns the NRMANetworkStatus
5665
+ (NRMANetworkStatus) networkStatus;
5766

Agent/Utilities/NewRelicInternalUtils.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#import "NRLogger.h"
1212
#import "NRConstants.h"
1313
#import "NRTimer.h"
14+
#import "Constants.h"
15+
#import "NewRelicAgentInternal.h"
16+
#import <netdb.h>
1417

1518
#if !TARGET_OS_TV && !TARGET_OS_WATCH
1619

@@ -36,6 +39,7 @@
3639
#import "NRMADEBUG_Reachability.h"
3740
#endif
3841

42+
3943
// This gets autogenerated into a _vers file using the "Current Project Version" from the project file.
4044
NSTimeInterval NRMAMillisecondTimestamp(void) {
4145
return (NSTimeInterval)(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) * 1000;
@@ -196,6 +200,35 @@ + (NSString*) connectionType {
196200
}
197201
}
198202

203+
+ (NRMANetworkStatus)currentReachabilityStatusTo:(NSURL*)url {
204+
if([self checkReachablityTo:url]) return ReachableViaUnknown;
205+
return NotReachable;
206+
}
207+
208+
+ (BOOL) checkReachablityTo:(NSURL*)url {
209+
struct hostent *remoteHostEnt = gethostbyname([[url host] UTF8String]);
210+
if(remoteHostEnt == NULL) return false;
211+
return [[url host] isEqualToString:[NSString stringWithUTF8String:*remoteHostEnt->h_aliases]];
212+
}
213+
214+
+ (NSString*) collectorHostDataURL
215+
{
216+
return [self collectorHostURL:(NSString*)kNRMA_Collector_data_url];
217+
}
218+
219+
+ (NSString*) collectorHostHexURL
220+
{
221+
return [self collectorHostURL:(NSString*)kNRMA_Collector_hex_url];
222+
}
223+
224+
+ (NSString*) collectorHostURL:(NSString*)resource
225+
{
226+
NSString* protocol = [[NewRelicAgentInternal sharedInstance] agentConfiguration].useSSL ? @"https://":@"http://";
227+
NSString* collectorHost = [[NewRelicAgentInternal sharedInstance] agentConfiguration].collectorHost;
228+
if(collectorHost == NULL) collectorHost = kNRMA_DEFAULT_COLLECTOR_HOST;
229+
return [NSString stringWithFormat:@"%@%@%@",protocol, collectorHost,resource];
230+
}
231+
199232
// Returns the carrier name, or 'wifi' if the device is on a wifi network.
200233
+ (NSString*) carrierName {
201234
#if TARGET_OS_TV
@@ -319,6 +352,9 @@ + (NSString*) deviceOrientation {
319352
}
320353

321354
+ (NRMANetworkStatus) networkStatus {
355+
#if TARGET_OS_WATCH
356+
NRMANetworkStatus status = [NewRelicInternalUtils currentReachabilityStatusTo:[NSURL URLWithString:[NewRelicInternalUtils collectorHostDataURL]]];
357+
#else
322358
#ifdef NRMA_REACHABILITY_DEBUG
323359
static NRMADEBUG_Reachability* debugLog;
324360
static dispatch_once_t onceToken;
@@ -371,6 +407,7 @@ + (NRMANetworkStatus) networkStatus {
371407
if (debugLog.total % 1000 == 0){
372408
NRLOG_INFO(@"DEBUG UPDATE: %@",debugLog);
373409
}
410+
#endif
374411
#endif
375412
return status;
376413
}

0 commit comments

Comments
 (0)