Skip to content

Commit a1933b5

Browse files
NR-199912: Adds NRFeatureFlag_BackgroundInstrumentation to NewRelicFeatureFlags and NRMAFlags
1 parent 37bbb15 commit a1933b5

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

Agent/FeatureFlags/NRMAFlags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
+ (BOOL) shouldEnableNewEventSystem;
5353

54+
+ (BOOL) shouldEnableBackgroundInstrumentation;
55+
5456
+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags;
5557

5658
// Private Setting

Agent/FeatureFlags/NRMAFlags.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ + (BOOL) shouldEnableLogReporting {
168168
+ (BOOL) shouldEnableNewEventSystem {
169169
return ([NRMAFlags featureFlags] & NRFeatureFlag_NewEventSystem) != 0;
170170
}
171+
172+
+ (BOOL) shouldEnableBackgroundInstrumentation {
173+
return ([NRMAFlags featureFlags] & NRFeatureFlag_BackgroundInstrumentation) != 0;
174+
}
175+
171176
+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags {
172177
NSMutableArray *retArray = [NSMutableArray array];
173178
if ((flags & NRFeatureFlag_InteractionTracing) == NRFeatureFlag_InteractionTracing) {
@@ -226,6 +231,10 @@ + (BOOL) shouldEnableNewEventSystem {
226231
if ((flags & NRFeatureFlag_NewEventSystem) == NRFeatureFlag_NewEventSystem) {
227232
[retArray addObject:@"NewEventSystem"];
228233
}
234+
if ((flags & NRFeatureFlag_BackgroundInstrumentation) == NRFeatureFlag_BackgroundInstrumentation) {
235+
[retArray addObject:@"BackgroundInstrumentation"];
236+
}
237+
229238
return retArray;
230239
}
231240

Agent/General/NewRelicAgentInternal.m

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ - (id) initWithCollectorAddress:(NSString*)collectorHost
151151
if (self) {
152152

153153

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

163165

@@ -198,9 +200,11 @@ - (id) initWithCollectorAddress:(NSString*)collectorHost
198200
selector:@selector(applicationWillTerminate)
199201
name:UIApplicationWillTerminateNotification
200202
object:[UIApplication sharedApplication]];
201-
202-
// TODO: Is this the right place to put this?
203-
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
203+
204+
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
205+
// TODO: Is this the right place to put this?
206+
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
207+
}
204208

205209
NRLOG_INFO(@"Agent enabled");
206210

@@ -721,9 +725,13 @@ - (void) applicationDidEnterBackground {
721725

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

724-
NRLOG_VERBOSE(@"used to agentShutdown.");
725728

726-
// [self agentShutdown];
729+
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
730+
NRLOG_VERBOSE(@"used to agentShutdown. Continuing since BackgroundInstrumentation enabled.");
731+
}
732+
else {
733+
[self agentShutdown];
734+
}
727735
}
728736
#endif
729737

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

736-
737-
738-
// Schedule the next background harvest.
739-
[self scheduleHeartbeatTask];
744+
if ([NRMAFlags shouldEnableBackgroundInstrumentation]) {
745+
// Schedule the next background harvest.
746+
[self scheduleHeartbeatTask];
747+
}
740748
}
741749
});
742750
} else {
@@ -770,7 +778,7 @@ - (void) agentShutdown {
770778
[NRMALastActivityTraceController clearLastActivityStamp];
771779
}
772780

773-
// We only support background featch in iOS 13+
781+
// We only support background fetch in iOS 13+
774782
- (void) scheduleHeartbeatTask {
775783
if (@available(iOS 13.0, *)) {
776784
// TODO: Pass instrumented app bundle id

Agent/Public/NewRelicFeatureFlags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ typedef NS_OPTIONS(unsigned long long, NRMAFeatureFlags){
9999

100100
// NOTE: Temporarily removed NRFeatureFlag_LogReporting
101101
NRFeatureFlag_NewEventSystem = 1 << 20, // Disabled by default
102+
NRFeatureFlag_BackgroundInstrumentation = 1 << 21, // Disabled by default
103+
102104
};

Test Harness/NRTestApp/NRTestApp/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3535

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

38+
NewRelic.enableFeatures([NRMAFeatureFlags.NRFeatureFlag_BackgroundInstrumentation])
39+
3840
NewRelic.replaceDeviceIdentifier("myDeviceId")
3941

42+
NewRelic.setMaxEventPoolSize(5000)
4043
NewRelic.setMaxEventBufferTime(60)
4144

4245
if ProcessInfo.processInfo.environment["UITesting"] != nil {

Tests/Unit-Tests/NewRelicAgentTests/Feature-Flags-Tests/NRMAFeatureFlagsTests.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,17 @@ - (void) testFedRampSupportEnable
249249

250250
XCTAssertFalse([NRMAFlags shouldEnableFedRampSupport], @"FedRamp Support should be disabled");
251251
}
252+
253+
- (void) testBackgroundInstrumentationEnable
254+
{
255+
XCTAssertFalse([NRMAFlags shouldEnableBackgroundInstrumentation], @"Since no flags have been set, BackgroundInstrumentation Support should be false");
256+
257+
[NRMAFlags enableFeatures:NRFeatureFlag_BackgroundInstrumentation];
258+
259+
XCTAssertTrue([NRMAFlags shouldEnableBackgroundInstrumentation], @"BackgroundInstrumentation Should be enabled");
260+
261+
[NRMAFlags disableFeatures:NRFeatureFlag_BackgroundInstrumentation];
262+
263+
XCTAssertFalse([NRMAFlags shouldEnableBackgroundInstrumentation], @"BackgroundInstrumentation should be disabled");
264+
}
252265
@end

0 commit comments

Comments
 (0)