From a1933b528ac88ebe7730d28bf10903931d957eb8 Mon Sep 17 00:00:00 2001 From: Chris Dillard Date: Wed, 3 Jan 2024 10:04:02 -0700 Subject: [PATCH] NR-199912: Adds NRFeatureFlag_BackgroundInstrumentation to NewRelicFeatureFlags and NRMAFlags --- Agent/FeatureFlags/NRMAFlags.h | 2 + Agent/FeatureFlags/NRMAFlags.m | 9 ++++ Agent/General/NewRelicAgentInternal.m | 42 +++++++++++-------- Agent/Public/NewRelicFeatureFlags.h | 2 + .../NRTestApp/NRTestApp/AppDelegate.swift | 3 ++ .../NRMAFeatureFlagsTests.m | 13 ++++++ 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/Agent/FeatureFlags/NRMAFlags.h b/Agent/FeatureFlags/NRMAFlags.h index b5114c67..57b0b8f4 100644 --- a/Agent/FeatureFlags/NRMAFlags.h +++ b/Agent/FeatureFlags/NRMAFlags.h @@ -51,6 +51,8 @@ + (BOOL) shouldEnableNewEventSystem; ++ (BOOL) shouldEnableBackgroundInstrumentation; + + (NSArray*) namesForFlags:(NRMAFeatureFlags)flags; // Private Setting diff --git a/Agent/FeatureFlags/NRMAFlags.m b/Agent/FeatureFlags/NRMAFlags.m index aa3fe48f..5146b02b 100644 --- a/Agent/FeatureFlags/NRMAFlags.m +++ b/Agent/FeatureFlags/NRMAFlags.m @@ -168,6 +168,11 @@ + (BOOL) shouldEnableLogReporting { + (BOOL) shouldEnableNewEventSystem { return ([NRMAFlags featureFlags] & NRFeatureFlag_NewEventSystem) != 0; } + ++ (BOOL) shouldEnableBackgroundInstrumentation { + return ([NRMAFlags featureFlags] & NRFeatureFlag_BackgroundInstrumentation) != 0; +} + + (NSArray*) namesForFlags:(NRMAFeatureFlags)flags { NSMutableArray *retArray = [NSMutableArray array]; if ((flags & NRFeatureFlag_InteractionTracing) == NRFeatureFlag_InteractionTracing) { @@ -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; } diff --git a/Agent/General/NewRelicAgentInternal.m b/Agent/General/NewRelicAgentInternal.m index fb14eedf..f3292252 100644 --- a/Agent/General/NewRelicAgentInternal.m +++ b/Agent/General/NewRelicAgentInternal.m @@ -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 + } } @@ -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"); @@ -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 @@ -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 { @@ -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 diff --git a/Agent/Public/NewRelicFeatureFlags.h b/Agent/Public/NewRelicFeatureFlags.h index 12956e8b..5cec1254 100644 --- a/Agent/Public/NewRelicFeatureFlags.h +++ b/Agent/Public/NewRelicFeatureFlags.h @@ -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 + }; diff --git a/Test Harness/NRTestApp/NRTestApp/AppDelegate.swift b/Test Harness/NRTestApp/NRTestApp/AppDelegate.swift index 6f7e24ca..3074873d 100644 --- a/Test Harness/NRTestApp/NRTestApp/AppDelegate.swift +++ b/Test Harness/NRTestApp/NRTestApp/AppDelegate.swift @@ -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 { diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Feature-Flags-Tests/NRMAFeatureFlagsTests.m b/Tests/Unit-Tests/NewRelicAgentTests/Feature-Flags-Tests/NRMAFeatureFlagsTests.m index 4ca70bca..5f98bd23 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Feature-Flags-Tests/NRMAFeatureFlagsTests.m +++ b/Tests/Unit-Tests/NewRelicAgentTests/Feature-Flags-Tests/NRMAFeatureFlagsTests.m @@ -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