Skip to content

Commit

Permalink
NR-199912: Adds NRFeatureFlag_BackgroundInstrumentation to NewRelicFe…
Browse files Browse the repository at this point in the history
…atureFlags and NRMAFlags
  • Loading branch information
cdillard-NewRelic committed Jan 3, 2024
1 parent 37bbb15 commit a1933b5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Agent/FeatureFlags/NRMAFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

+ (BOOL) shouldEnableNewEventSystem;

+ (BOOL) shouldEnableBackgroundInstrumentation;

+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags;

// Private Setting
Expand Down
9 changes: 9 additions & 0 deletions Agent/FeatureFlags/NRMAFlags.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ + (BOOL) shouldEnableLogReporting {
+ (BOOL) shouldEnableNewEventSystem {
return ([NRMAFlags featureFlags] & NRFeatureFlag_NewEventSystem) != 0;
}

+ (BOOL) shouldEnableBackgroundInstrumentation {
return ([NRMAFlags featureFlags] & NRFeatureFlag_BackgroundInstrumentation) != 0;
}

+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags {
NSMutableArray *retArray = [NSMutableArray array];
if ((flags & NRFeatureFlag_InteractionTracing) == NRFeatureFlag_InteractionTracing) {
Expand Down Expand Up @@ -226,6 +231,10 @@ + (BOOL) shouldEnableNewEventSystem {
if ((flags & NRFeatureFlag_NewEventSystem) == NRFeatureFlag_NewEventSystem) {
[retArray addObject:@"NewEventSystem"];
}
if ((flags & NRFeatureFlag_BackgroundInstrumentation) == NRFeatureFlag_BackgroundInstrumentation) {
[retArray addObject:@"BackgroundInstrumentation"];
}

return retArray;
}

Expand Down
42 changes: 25 additions & 17 deletions Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ - (id) initWithCollectorAddress:(NSString*)collectorHost
if (self) {


// TODO: Check if this is the right place for this code?
if (@available(iOS 13.0, *)) {
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.newrelic.NRApp.bitcode" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
[self handleAppRefreshTask: task];
}];
} else {
// Fallback on earlier versions
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
// TODO: Check if this is the right place for this code?
if (@available(iOS 13.0, *)) {
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.newrelic.NRApp.bitcode" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
[self handleAppRefreshTask: task];
}];
} else {
// Fallback on earlier versions
}
}


Expand Down Expand Up @@ -198,9 +200,11 @@ - (id) initWithCollectorAddress:(NSString*)collectorHost
selector:@selector(applicationWillTerminate)
name:UIApplicationWillTerminateNotification
object:[UIApplication sharedApplication]];

// TODO: Is this the right place to put this?
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
// TODO: Is this the right place to put this?
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
}

NRLOG_INFO(@"Agent enabled");

Expand Down Expand Up @@ -721,9 +725,13 @@ - (void) applicationDidEnterBackground {

// What would happen if we didn't call agentShutdown?

NRLOG_VERBOSE(@"used to agentShutdown.");

// [self agentShutdown];
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
NRLOG_VERBOSE(@"used to agentShutdown. Continuing since BackgroundInstrumentation enabled.");
}
else {
[self agentShutdown];
}
}
#endif

Expand All @@ -733,10 +741,10 @@ - (void) applicationDidEnterBackground {
// Invalidate the background_task.
background_task = UIBackgroundTaskInvalid;



// Schedule the next background harvest.
[self scheduleHeartbeatTask];
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
// Schedule the next background harvest.
[self scheduleHeartbeatTask];
}
}
});
} else {
Expand Down Expand Up @@ -770,7 +778,7 @@ - (void) agentShutdown {
[NRMALastActivityTraceController clearLastActivityStamp];
}

// We only support background featch in iOS 13+
// We only support background fetch in iOS 13+
- (void) scheduleHeartbeatTask {
if (@available(iOS 13.0, *)) {
// TODO: Pass instrumented app bundle id
Expand Down
2 changes: 2 additions & 0 deletions Agent/Public/NewRelicFeatureFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ typedef NS_OPTIONS(unsigned long long, NRMAFeatureFlags){

// NOTE: Temporarily removed NRFeatureFlag_LogReporting
NRFeatureFlag_NewEventSystem = 1 << 20, // Disabled by default
NRFeatureFlag_BackgroundInstrumentation = 1 << 21, // Disabled by default

};
3 changes: 3 additions & 0 deletions Test Harness/NRTestApp/NRTestApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// NewRelic.enableFeatures([NRMAFeatureFlags.NRFeatureFlag_NewEventSystem])

NewRelic.enableFeatures([NRMAFeatureFlags.NRFeatureFlag_BackgroundInstrumentation])

NewRelic.replaceDeviceIdentifier("myDeviceId")

NewRelic.setMaxEventPoolSize(5000)
NewRelic.setMaxEventBufferTime(60)

if ProcessInfo.processInfo.environment["UITesting"] != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,17 @@ - (void) testFedRampSupportEnable

XCTAssertFalse([NRMAFlags shouldEnableFedRampSupport], @"FedRamp Support should be disabled");
}

- (void) testBackgroundInstrumentationEnable
{
XCTAssertFalse([NRMAFlags shouldEnableBackgroundInstrumentation], @"Since no flags have been set, BackgroundInstrumentation Support should be false");

[NRMAFlags enableFeatures:NRFeatureFlag_BackgroundInstrumentation];

XCTAssertTrue([NRMAFlags shouldEnableBackgroundInstrumentation], @"BackgroundInstrumentation Should be enabled");

[NRMAFlags disableFeatures:NRFeatureFlag_BackgroundInstrumentation];

XCTAssertFalse([NRMAFlags shouldEnableBackgroundInstrumentation], @"BackgroundInstrumentation should be disabled");
}
@end

0 comments on commit a1933b5

Please sign in to comment.