| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- // Copyright 2020 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags.h"
- #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
- #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
- #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags+Private.h"
- #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
- #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
- #define ONE_DAY_SECONDS 24 * 60 * 60
- typedef NS_ENUM(NSInteger, FPRConfigValueType) {
- // Config value type String.
- FPRConfigValueTypeString,
- // Config value type Bool.
- FPRConfigValueTypeBool,
- // Config value type Integer.
- FPRConfigValueTypeInteger,
- // Config value type Float.
- FPRConfigValueTypeFloat,
- };
- @interface FPRRemoteConfigFlags ()
- /** @brief Represents if a fetch is currently in progress. */
- @property(atomic) BOOL fetchInProgress;
- /** @brief Dictionary of different config keys and value types. */
- @property(nonatomic) NSDictionary<NSString *, NSNumber *> *configKeys;
- /** @brief Last time the configs were cached. */
- @property(nonatomic) NSDate *lastCachedTime;
- /** @brief Status of the last remote config fetch. */
- @property(nonatomic) FIRRemoteConfigFetchStatus lastFetchStatus;
- @end
- @implementation FPRRemoteConfigFlags
- + (nullable instancetype)sharedInstance {
- static FPRRemoteConfigFlags *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- FIRRemoteConfig *rc = [FIRRemoteConfig remoteConfigWithFIRNamespace:@"fireperf"
- app:[FIRApp defaultApp]];
- instance = [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:rc];
- });
- return instance;
- }
- - (instancetype)initWithRemoteConfig:(FIRRemoteConfig *)config {
- self = [super init];
- if (self) {
- _fprRemoteConfig = config;
- _userDefaults = [FPRConfigurations sharedInstance].userDefaults;
- self.fetchInProgress = NO;
- NSMutableDictionary<NSString *, NSNumber *> *keysToCache =
- [[NSMutableDictionary<NSString *, NSNumber *> alloc] init];
- [keysToCache setObject:@(FPRConfigValueTypeInteger) forKey:@"fpr_log_source"];
- [keysToCache setObject:@(FPRConfigValueTypeBool) forKey:@"fpr_enabled"];
- [keysToCache setObject:@(FPRConfigValueTypeString) forKey:@"fpr_disabled_ios_versions"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger) forKey:@"fpr_rl_time_limit_sec"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger) forKey:@"fpr_rl_trace_event_count_fg"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger) forKey:@"fpr_rl_trace_event_count_bg"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_rl_network_request_event_count_fg"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_rl_network_request_event_count_bg"];
- [keysToCache setObject:@(FPRConfigValueTypeFloat) forKey:@"fpr_vc_trace_sampling_rate"];
- [keysToCache setObject:@(FPRConfigValueTypeFloat)
- forKey:@"fpr_vc_network_request_sampling_rate"];
- [keysToCache setObject:@(FPRConfigValueTypeFloat) forKey:@"fpr_vc_session_sampling_rate"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_session_gauge_cpu_capture_frequency_fg_ms"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_session_gauge_cpu_capture_frequency_bg_ms"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_session_gauge_memory_capture_frequency_fg_ms"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger)
- forKey:@"fpr_session_gauge_memory_capture_frequency_bg_ms"];
- [keysToCache setObject:@(FPRConfigValueTypeInteger) forKey:@"fpr_session_max_duration_min"];
- self.configKeys = [keysToCache copy];
- [self update];
- }
- return self;
- }
- - (void)update {
- // If a fetch is already happening, do not attempt a fetch.
- if (self.fetchInProgress) {
- return;
- }
- NSTimeInterval timeIntervalSinceLastFetch =
- [self.fprRemoteConfig.lastFetchTime timeIntervalSinceNow];
- if (!self.fprRemoteConfig.lastFetchTime ||
- ABS(timeIntervalSinceLastFetch) > kFPRConfigFetchIntervalInSeconds) {
- self.fetchInProgress = YES;
- [self.fprRemoteConfig
- fetchAndActivateWithCompletionHandler:^(FIRRemoteConfigFetchAndActivateStatus status,
- NSError *_Nullable error) {
- self.lastFetchStatus = self.fprRemoteConfig.lastFetchStatus;
- if (status == FIRRemoteConfigFetchAndActivateStatusError) {
- FPRLogError(kFPRConfigurationFetchFailure, @"Unable to fetch configurations.");
- } else {
- self.lastFetchedTime = self.fprRemoteConfig.lastFetchTime;
- // If a fetch was successful,
- // 1. Clear the old cache
- [self resetCache];
- // 2. Cache the new config values
- [self cacheConfigValues];
- }
- self.fetchInProgress = NO;
- }];
- } else if (self.fprRemoteConfig.lastFetchTime) {
- // Update the last fetched time to know that remote config fetch has happened in the past.
- self.lastFetchedTime = self.fprRemoteConfig.lastFetchTime;
- }
- }
- #pragma mark - Util methods.
- - (void)resetCache {
- [self.configKeys
- enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *valueType, BOOL *stop) {
- NSString *cacheKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, key];
- [self.userDefaults removeObjectForKey:cacheKey];
- }];
- }
- - (void)cacheConfigValues {
- [self.configKeys
- enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *valueType, BOOL *stop) {
- FIRRemoteConfigValue *rcValue = [self.fprRemoteConfig configValueForKey:key];
- // Cache only values that comes from remote.
- if (rcValue != nil && rcValue.source == FIRRemoteConfigSourceRemote) {
- FPRConfigValueType configValueType = [valueType integerValue];
- NSString *cacheKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, key];
- if (configValueType == FPRConfigValueTypeInteger) {
- NSInteger integerValue = [[rcValue numberValue] integerValue];
- [self.userDefaults setInteger:integerValue forKey:cacheKey];
- } else if (configValueType == FPRConfigValueTypeFloat) {
- float floatValue = [[rcValue numberValue] floatValue];
- [self.userDefaults setFloat:floatValue forKey:cacheKey];
- } else if (configValueType == FPRConfigValueTypeBool) {
- BOOL boolValue = [rcValue boolValue];
- [self.userDefaults setBool:boolValue forKey:cacheKey];
- } else if (configValueType == FPRConfigValueTypeString) {
- NSString *stringValue = [rcValue stringValue];
- [self.userDefaults setObject:stringValue forKey:cacheKey];
- }
- self.lastCachedTime = [NSDate date];
- }
- }];
- }
- - (id)cachedValueForConfigFlag:(NSString *)configFlag {
- // If the cached value is too old, return nil.
- if (ABS([self.lastFetchedTime timeIntervalSinceNow]) > 7 * ONE_DAY_SECONDS) {
- return nil;
- }
- NSString *cacheKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, configFlag];
- id cachedValueObject = [self.userDefaults objectForKey:cacheKey];
- return cachedValueObject;
- }
- #pragma mark - Config value fetch methods.
- - (NSString *)getStringValueForFlag:(NSString *)flagName defaultValue:(NSString *)defaultValue {
- id cachedValueObject = [self cachedValueForConfigFlag:flagName];
- if ([cachedValueObject isKindOfClass:[NSString class]]) {
- return (NSString *)cachedValueObject;
- }
- return defaultValue;
- }
- - (int)getIntValueForFlag:(NSString *)flagName defaultValue:(int)defaultValue {
- id cachedValueObject = [self cachedValueForConfigFlag:flagName];
- if (cachedValueObject) {
- return [cachedValueObject intValue];
- }
- return defaultValue;
- }
- - (float)getFloatValueForFlag:(NSString *)flagName defaultValue:(float)defaultValue {
- id cachedValueObject = [self cachedValueForConfigFlag:flagName];
- if (cachedValueObject) {
- return [cachedValueObject floatValue];
- }
- return defaultValue;
- }
- - (BOOL)getBoolValueForFlag:(NSString *)flagName defaultValue:(BOOL)defaultValue {
- id cachedValueObject = [self cachedValueForConfigFlag:flagName];
- if (cachedValueObject) {
- return [cachedValueObject boolValue];
- }
- return defaultValue;
- }
- #pragma mark - Configuration methods.
- - (int)logSourceWithDefaultValue:(int)logSource {
- return [self getIntValueForFlag:@"fpr_log_source" defaultValue:logSource];
- }
- - (BOOL)performanceSDKEnabledWithDefaultValue:(BOOL)sdkEnabled {
- /* Order of preference:
- * 1. If remote config fetch was a failure, return NO.
- * 2. If the fetch was successful, but RC does not have the value (not a remote value),
- * return YES.
- * 3. Else, use the value from RC.
- */
- if (self.lastFetchStatus == FIRRemoteConfigFetchStatusFailure) {
- return NO;
- }
- return [self getBoolValueForFlag:@"fpr_enabled" defaultValue:sdkEnabled];
- }
- - (NSSet<NSString *> *)sdkDisabledVersionsWithDefaultValue:(NSSet<NSString *> *)sdkVersions {
- NSMutableSet<NSString *> *disabledVersions = [[NSMutableSet<NSString *> alloc] init];
- NSString *sdkVersionsString = [[self getStringValueForFlag:@"fpr_disabled_ios_versions"
- defaultValue:@""]
- stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if (sdkVersionsString.length > 0) {
- NSArray<NSString *> *sdkVersionStrings = [sdkVersionsString componentsSeparatedByString:@";"];
- for (NSString *sdkVersionString in sdkVersionStrings) {
- NSString *trimmedString = [sdkVersionString
- stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if (trimmedString.length > 0) {
- [disabledVersions addObject:trimmedString];
- }
- }
- } else {
- return sdkVersions;
- }
- return [disabledVersions copy];
- }
- #pragma mark - Rate limiting flags
- - (int)rateLimitTimeDurationWithDefaultValue:(int)durationInSeconds {
- return [self getIntValueForFlag:@"fpr_rl_time_limit_sec" defaultValue:durationInSeconds];
- }
- - (int)rateLimitTraceCountInForegroundWithDefaultValue:(int)eventCount {
- return [self getIntValueForFlag:@"fpr_rl_trace_event_count_fg" defaultValue:eventCount];
- }
- - (int)rateLimitTraceCountInBackgroundWithDefaultValue:(int)eventCount {
- return [self getIntValueForFlag:@"fpr_rl_trace_event_count_bg" defaultValue:eventCount];
- }
- - (int)rateLimitNetworkRequestCountInForegroundWithDefaultValue:(int)eventCount {
- return [self getIntValueForFlag:@"fpr_rl_network_request_event_count_fg" defaultValue:eventCount];
- }
- - (int)rateLimitNetworkRequestCountInBackgroundWithDefaultValue:(int)eventCount {
- return [self getIntValueForFlag:@"fpr_rl_network_request_event_count_bg" defaultValue:eventCount];
- }
- #pragma mark - Sampling flags
- - (float)traceSamplingRateWithDefaultValue:(float)samplingRate {
- return [self getFloatValueForFlag:@"fpr_vc_trace_sampling_rate" defaultValue:samplingRate];
- }
- - (float)networkRequestSamplingRateWithDefaultValue:(float)samplingRate {
- return [self getFloatValueForFlag:@"fpr_vc_network_request_sampling_rate"
- defaultValue:samplingRate];
- }
- #pragma mark - Session flags
- - (float)sessionSamplingRateWithDefaultValue:(float)samplingRate {
- return [self getFloatValueForFlag:@"fpr_vc_session_sampling_rate" defaultValue:samplingRate];
- }
- - (int)sessionGaugeCPUCaptureFrequencyInForegroundWithDefaultValue:(int)defaultFrequency {
- return [self getIntValueForFlag:@"fpr_session_gauge_cpu_capture_frequency_fg_ms"
- defaultValue:defaultFrequency];
- }
- - (int)sessionGaugeCPUCaptureFrequencyInBackgroundWithDefaultValue:(int)defaultFrequency {
- return [self getIntValueForFlag:@"fpr_session_gauge_cpu_capture_frequency_bg_ms"
- defaultValue:defaultFrequency];
- }
- - (int)sessionGaugeMemoryCaptureFrequencyInForegroundWithDefaultValue:(int)defaultFrequency {
- return [self getIntValueForFlag:@"fpr_session_gauge_memory_capture_frequency_fg_ms"
- defaultValue:defaultFrequency];
- }
- - (int)sessionGaugeMemoryCaptureFrequencyInBackgroundWithDefaultValue:(int)defaultFrequency {
- return [self getIntValueForFlag:@"fpr_session_gauge_memory_capture_frequency_bg_ms"
- defaultValue:defaultFrequency];
- }
- - (int)sessionMaxDurationWithDefaultValue:(int)maxDurationInMinutes {
- return [self getIntValueForFlag:@"fpr_session_max_duration_min"
- defaultValue:maxDurationInMinutes];
- }
- @end
|