| 1 | 
           efrain | 
           1 | 
           // Copyright 2020 Google LLC
  | 
        
        
            | 
            | 
           2 | 
           //
  | 
        
        
            | 
            | 
           3 | 
           // Licensed under the Apache License, Version 2.0 (the "License");
  | 
        
        
            | 
            | 
           4 | 
           // you may not use this file except in compliance with the License.
  | 
        
        
            | 
            | 
           5 | 
           // You may obtain a copy of the License at
  | 
        
        
            | 
            | 
           6 | 
           //
  | 
        
        
            | 
            | 
           7 | 
           //      http://www.apache.org/licenses/LICENSE-2.0
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Unless required by applicable law or agreed to in writing, software
  | 
        
        
            | 
            | 
           10 | 
           // distributed under the License is distributed on an "AS IS" BASIS,
  | 
        
        
            | 
            | 
           11 | 
           // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  | 
        
        
            | 
            | 
           12 | 
           // See the License for the specific language governing permissions and
  | 
        
        
            | 
            | 
           13 | 
           // limitations under the License.
  | 
        
        
            | 
            | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRTrace.h"
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           #import "FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.h"
  | 
        
        
            | 
            | 
           18 | 
           #import "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
  | 
        
        
            | 
            | 
           19 | 
           #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  | 
        
        
            | 
            | 
           20 | 
           #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  | 
        
        
            | 
            | 
           21 | 
           #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  | 
        
        
            | 
            | 
           22 | 
           #import "FirebasePerformance/Sources/FPRClient.h"
  | 
        
        
            | 
            | 
           23 | 
           #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  | 
        
        
            | 
            | 
           24 | 
           #import "FirebasePerformance/Sources/FPRDataUtils.h"
  | 
        
        
            | 
            | 
           25 | 
           #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h"
  | 
        
        
            | 
            | 
           26 | 
           #import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h"
  | 
        
        
            | 
            | 
           27 | 
           #import "FirebasePerformance/Sources/Timer/FIRTrace+Private.h"
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           @interface FIRTrace ()
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           @property(nonatomic, copy, readwrite) NSString *name;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           /** Custom attributes managed internally. */
  | 
        
        
            | 
            | 
           34 | 
           @property(nonatomic) NSMutableDictionary<NSString *, NSString *> *customAttributes;
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           /** Serial queue to manage mutation of attributes. */
  | 
        
        
            | 
            | 
           37 | 
           @property(nonatomic, readwrite) dispatch_queue_t customAttributesSerialQueue;
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
           @property(nonatomic, readwrite) NSDate *startTime;
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           @property(nonatomic, readwrite) NSDate *stopTime;
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           /** Background activity tracker to know the background state of the trace. */
  | 
        
        
            | 
            | 
           44 | 
           @property(nonatomic) FPRTraceBackgroundActivityTracker *backgroundActivityTracker;
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
           /** Property that denotes if the trace is a stage. */
  | 
        
        
            | 
            | 
           47 | 
           @property(nonatomic) BOOL isStage;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
           /** Stops an active stage that is currently active. */
  | 
        
        
            | 
            | 
           50 | 
           - (void)stopActiveStage;
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
           /**
  | 
        
        
            | 
            | 
           53 | 
            * Updates the current trace with the session id.
  | 
        
        
            | 
            | 
           54 | 
            * @param sessionDetails Updated session details of the currently active session.
  | 
        
        
            | 
            | 
           55 | 
            */
  | 
        
        
            | 
            | 
           56 | 
           - (void)updateTraceWithSessionId:(FPRSessionDetails *)sessionDetails;
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
           @end
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
           @implementation FIRTrace
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
           - (instancetype)initWithName:(NSString *)name {
  | 
        
        
            | 
            | 
           63 | 
             NSString *validatedName = FPRReservableName(name);
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
             FIRTrace *trace = [self initTraceWithName:validatedName];
  | 
        
        
            | 
            | 
           66 | 
             trace.internal = NO;
  | 
        
        
            | 
            | 
           67 | 
             return trace;
  | 
        
        
            | 
            | 
           68 | 
           }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
           - (instancetype)initInternalTraceWithName:(NSString *)name {
  | 
        
        
            | 
            | 
           71 | 
             FIRTrace *trace = [self initTraceWithName:name];
  | 
        
        
            | 
            | 
           72 | 
             trace.internal = YES;
  | 
        
        
            | 
            | 
           73 | 
             return trace;
  | 
        
        
            | 
            | 
           74 | 
           }
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
           - (instancetype)initTraceWithName:(NSString *)name {
  | 
        
        
            | 
            | 
           77 | 
             BOOL tracingEnabled = [FPRConfigurations sharedInstance].isDataCollectionEnabled;
  | 
        
        
            | 
            | 
           78 | 
             if (!tracingEnabled) {
  | 
        
        
            | 
            | 
           79 | 
               FPRLogInfo(kFPRTraceDisabled, @"Trace feature is disabled.");
  | 
        
        
            | 
            | 
           80 | 
               return nil;
  | 
        
        
            | 
            | 
           81 | 
             }
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
             BOOL sdkEnabled = [[FPRConfigurations sharedInstance] sdkEnabled];
  | 
        
        
            | 
            | 
           84 | 
             if (!sdkEnabled) {
  | 
        
        
            | 
            | 
           85 | 
               FPRLogInfo(kFPRTraceDisabled, @"Dropping event since Performance SDK is disabled.");
  | 
        
        
            | 
            | 
           86 | 
               return nil;
  | 
        
        
            | 
            | 
           87 | 
             }
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
             FPRAssert(name != nil, @"Name cannot be nil");
  | 
        
        
            | 
            | 
           90 | 
             FPRAssert(name.length > 0, @"Name cannot be an empty string");
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
             if (name == nil || name.length == 0) {
  | 
        
        
            | 
            | 
           93 | 
               FPRLogError(kFPRTraceNoName, @"Failed to initialize because of a nil or zero length name.");
  | 
        
        
            | 
            | 
           94 | 
               return nil;
  | 
        
        
            | 
            | 
           95 | 
             }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
             self = [super init];
  | 
        
        
            | 
            | 
           98 | 
             if (self) {
  | 
        
        
            | 
            | 
           99 | 
               _name = [name copy];
  | 
        
        
            | 
            | 
           100 | 
               _stages = [[NSMutableArray<FIRTrace *> alloc] init];
  | 
        
        
            | 
            | 
           101 | 
               _counterList = [[FPRCounterList alloc] init];
  | 
        
        
            | 
            | 
           102 | 
               _customAttributes = [[NSMutableDictionary<NSString *, NSString *> alloc] init];
  | 
        
        
            | 
            | 
           103 | 
               _customAttributesSerialQueue =
  | 
        
        
            | 
            | 
           104 | 
                   dispatch_queue_create("com.google.perf.customAttributes.trace", DISPATCH_QUEUE_SERIAL);
  | 
        
        
            | 
            | 
           105 | 
               _sessionIdSerialQueue =
  | 
        
        
            | 
            | 
           106 | 
                   dispatch_queue_create("com.google.perf.sessionIds.trace", DISPATCH_QUEUE_SERIAL);
  | 
        
        
            | 
            | 
           107 | 
               _activeSessions = [[NSMutableArray<FPRSessionDetails *> alloc] init];
  | 
        
        
            | 
            | 
           108 | 
               _isStage = NO;
  | 
        
        
            | 
            | 
           109 | 
               _fprClient = [FPRClient sharedInstance];
  | 
        
        
            | 
            | 
           110 | 
             }
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
             return self;
  | 
        
        
            | 
            | 
           113 | 
           }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
           - (instancetype)init {
  | 
        
        
            | 
            | 
           116 | 
             FPRAssert(NO, @"Not a valid initializer.");
  | 
        
        
            | 
            | 
           117 | 
             return nil;
  | 
        
        
            | 
            | 
           118 | 
           }
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
           - (void)dealloc {
  | 
        
        
            | 
            | 
           121 | 
             // Track the number of traces that have started and not stopped.
  | 
        
        
            | 
            | 
           122 | 
             if (!self.isStage && [self isTraceStarted] && ![self isTraceStopped]) {
  | 
        
        
            | 
            | 
           123 | 
               FIRTrace *activeTrace = [FPRAppActivityTracker sharedInstance].activeTrace;
  | 
        
        
            | 
            | 
           124 | 
               [activeTrace incrementMetric:kFPRAppCounterNameTraceNotStopped byInt:1];
  | 
        
        
            | 
            | 
           125 | 
               FPRLogError(kFPRTraceStartedNotStopped, @"Trace name %@ started, not stopped", self.name);
  | 
        
        
            | 
            | 
           126 | 
             }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
             FPRSessionManager *sessionManager = [FPRSessionManager sharedInstance];
  | 
        
        
            | 
            | 
           129 | 
             [sessionManager.sessionNotificationCenter removeObserver:self
  | 
        
        
            | 
            | 
           130 | 
                                                                 name:kFPRSessionIdUpdatedNotification
  | 
        
        
            | 
            | 
           131 | 
                                                               object:sessionManager];
  | 
        
        
            | 
            | 
           132 | 
           }
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
            | 
            | 
           134 | 
           #pragma mark - Public instance methods
  | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           136 | 
           - (void)start {
  | 
        
        
            | 
            | 
           137 | 
             if (![self isTraceStarted]) {
  | 
        
        
            | 
            | 
           138 | 
               if (!self.isStage) {
  | 
        
        
            | 
            | 
           139 | 
                 [[FPRGaugeManager sharedInstance] collectAllGauges];
  | 
        
        
            | 
            | 
           140 | 
               }
  | 
        
        
            | 
            | 
           141 | 
               self.startTime = [NSDate date];
  | 
        
        
            | 
            | 
           142 | 
               self.backgroundActivityTracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  | 
        
        
            | 
            | 
           143 | 
               FPRSessionManager *sessionManager = [FPRSessionManager sharedInstance];
  | 
        
        
            | 
            | 
           144 | 
               if (!self.isStage) {
  | 
        
        
            | 
            | 
           145 | 
                 [self updateTraceWithSessionId:[sessionManager.sessionDetails copy]];
  | 
        
        
            | 
            | 
           146 | 
                 [sessionManager.sessionNotificationCenter addObserver:self
  | 
        
        
            | 
            | 
           147 | 
                                                              selector:@selector(sessionChanged:)
  | 
        
        
            | 
            | 
           148 | 
                                                                  name:kFPRSessionIdUpdatedNotification
  | 
        
        
            | 
            | 
           149 | 
                                                                object:sessionManager];
  | 
        
        
            | 
            | 
           150 | 
               }
  | 
        
        
            | 
            | 
           151 | 
             } else {
  | 
        
        
            | 
            | 
           152 | 
               FPRLogError(kFPRTraceAlreadyStopped,
  | 
        
        
            | 
            | 
           153 | 
                           @"Failed to start trace %@ because it has already been started and stopped.",
  | 
        
        
            | 
            | 
           154 | 
                           self.name);
  | 
        
        
            | 
            | 
           155 | 
             }
  | 
        
        
            | 
            | 
           156 | 
           }
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
           - (void)startWithStartTime:(NSDate *)startTime {
  | 
        
        
            | 
            | 
           159 | 
             [self start];
  | 
        
        
            | 
            | 
           160 | 
             if (startTime) {
  | 
        
        
            | 
            | 
           161 | 
               self.startTime = startTime;
  | 
        
        
            | 
            | 
           162 | 
             }
  | 
        
        
            | 
            | 
           163 | 
           }
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
           - (void)stop {
  | 
        
        
            | 
            | 
           166 | 
             [self stopActiveStage];
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           169 | 
               self.stopTime = [NSDate date];
  | 
        
        
            | 
            | 
           170 | 
               [self.fprClient logTrace:self];
  | 
        
        
            | 
            | 
           171 | 
               if (!self.isStage) {
  | 
        
        
            | 
            | 
           172 | 
                 [[FPRGaugeManager sharedInstance] collectAllGauges];
  | 
        
        
            | 
            | 
           173 | 
               }
  | 
        
        
            | 
            | 
           174 | 
             } else {
  | 
        
        
            | 
            | 
           175 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           176 | 
                           @"Failed to stop the trace %@ because it has not been started.", self.name);
  | 
        
        
            | 
            | 
           177 | 
             }
  | 
        
        
            | 
            | 
           178 | 
              | 
        
        
            | 
            | 
           179 | 
             FPRSessionManager *sessionManager = [FPRSessionManager sharedInstance];
  | 
        
        
            | 
            | 
           180 | 
             [sessionManager.sessionNotificationCenter removeObserver:self
  | 
        
        
            | 
            | 
           181 | 
                                                                 name:kFPRSessionIdUpdatedNotification
  | 
        
        
            | 
            | 
           182 | 
                                                               object:sessionManager];
  | 
        
        
            | 
            | 
           183 | 
           }
  | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
           - (void)cancel {
  | 
        
        
            | 
            | 
           186 | 
             [self stopActiveStage];
  | 
        
        
            | 
            | 
           187 | 
              | 
        
        
            | 
            | 
           188 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           189 | 
               self.stopTime = [NSDate date];
  | 
        
        
            | 
            | 
           190 | 
             } else {
  | 
        
        
            | 
            | 
           191 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           192 | 
                           @"Failed to stop the trace %@ because it has not been started.", self.name);
  | 
        
        
            | 
            | 
           193 | 
             }
  | 
        
        
            | 
            | 
           194 | 
           }
  | 
        
        
            | 
            | 
           195 | 
              | 
        
        
            | 
            | 
           196 | 
           - (NSTimeInterval)totalTraceTimeInterval {
  | 
        
        
            | 
            | 
           197 | 
             return [self.stopTime timeIntervalSinceDate:self.startTime];
  | 
        
        
            | 
            | 
           198 | 
           }
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
           - (NSTimeInterval)startTimeSinceEpoch {
  | 
        
        
            | 
            | 
           201 | 
             return [self.startTime timeIntervalSince1970];
  | 
        
        
            | 
            | 
           202 | 
           }
  | 
        
        
            | 
            | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
           - (BOOL)isCompleteAndValid {
  | 
        
        
            | 
            | 
           205 | 
             // Check if the trace time interval is valid.
  | 
        
        
            | 
            | 
           206 | 
             if (self.totalTraceTimeInterval <= 0) {
  | 
        
        
            | 
            | 
           207 | 
               return NO;
  | 
        
        
            | 
            | 
           208 | 
             }
  | 
        
        
            | 
            | 
           209 | 
              | 
        
        
            | 
            | 
           210 | 
             // Check if the counter list is valid.
  | 
        
        
            | 
            | 
           211 | 
             if (![self.counterList isValid]) {
  | 
        
        
            | 
            | 
           212 | 
               return NO;
  | 
        
        
            | 
            | 
           213 | 
             }
  | 
        
        
            | 
            | 
           214 | 
              | 
        
        
            | 
            | 
           215 | 
             // Check if the stages are valid.
  | 
        
        
            | 
            | 
           216 | 
             __block BOOL validTrace = YES;
  | 
        
        
            | 
            | 
           217 | 
             [self.stages enumerateObjectsUsingBlock:^(FIRTrace *stage, NSUInteger idx, BOOL *stop) {
  | 
        
        
            | 
            | 
           218 | 
               validTrace = [stage isCompleteAndValid];
  | 
        
        
            | 
            | 
           219 | 
               if (!validTrace) {
  | 
        
        
            | 
            | 
           220 | 
                 *stop = YES;
  | 
        
        
            | 
            | 
           221 | 
               }
  | 
        
        
            | 
            | 
           222 | 
             }];
  | 
        
        
            | 
            | 
           223 | 
              | 
        
        
            | 
            | 
           224 | 
             return validTrace;
  | 
        
        
            | 
            | 
           225 | 
           }
  | 
        
        
            | 
            | 
           226 | 
              | 
        
        
            | 
            | 
           227 | 
           - (FPRTraceState)backgroundTraceState {
  | 
        
        
            | 
            | 
           228 | 
             FPRTraceBackgroundActivityTracker *backgroundActivityTracker = self.backgroundActivityTracker;
  | 
        
        
            | 
            | 
           229 | 
             if (backgroundActivityTracker) {
  | 
        
        
            | 
            | 
           230 | 
               return backgroundActivityTracker.traceBackgroundState;
  | 
        
        
            | 
            | 
           231 | 
             }
  | 
        
        
            | 
            | 
           232 | 
              | 
        
        
            | 
            | 
           233 | 
             return FPRTraceStateUnknown;
  | 
        
        
            | 
            | 
           234 | 
           }
  | 
        
        
            | 
            | 
           235 | 
              | 
        
        
            | 
            | 
           236 | 
           - (NSArray<FPRSessionDetails *> *)sessions {
  | 
        
        
            | 
            | 
           237 | 
             __block NSArray<FPRSessionDetails *> *sessionInfos = nil;
  | 
        
        
            | 
            | 
           238 | 
             dispatch_sync(self.sessionIdSerialQueue, ^{
  | 
        
        
            | 
            | 
           239 | 
               sessionInfos = [self.activeSessions copy];
  | 
        
        
            | 
            | 
           240 | 
             });
  | 
        
        
            | 
            | 
           241 | 
             return sessionInfos;
  | 
        
        
            | 
            | 
           242 | 
           }
  | 
        
        
            | 
            | 
           243 | 
              | 
        
        
            | 
            | 
           244 | 
           #pragma mark - Stage related methods
  | 
        
        
            | 
            | 
           245 | 
              | 
        
        
            | 
            | 
           246 | 
           - (void)startStageNamed:(NSString *)stageName startTime:(NSDate *)startTime {
  | 
        
        
            | 
            | 
           247 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           248 | 
               [self stopActiveStage];
  | 
        
        
            | 
            | 
           249 | 
              | 
        
        
            | 
            | 
           250 | 
               if (self.isInternal) {
  | 
        
        
            | 
            | 
           251 | 
                 self.activeStage = [[FIRTrace alloc] initInternalTraceWithName:stageName];
  | 
        
        
            | 
            | 
           252 | 
                 [self.activeStage startWithStartTime:startTime];
  | 
        
        
            | 
            | 
           253 | 
               } else {
  | 
        
        
            | 
            | 
           254 | 
                 NSString *validatedStageName = FPRReservableName(stageName);
  | 
        
        
            | 
            | 
           255 | 
                 if (validatedStageName.length > 0) {
  | 
        
        
            | 
            | 
           256 | 
                   self.activeStage = [[FIRTrace alloc] initWithName:validatedStageName];
  | 
        
        
            | 
            | 
           257 | 
                   [self.activeStage startWithStartTime:startTime];
  | 
        
        
            | 
            | 
           258 | 
                 } else {
  | 
        
        
            | 
            | 
           259 | 
                   FPRLogError(kFPRTraceEmptyName, @"The stage name cannot be empty.");
  | 
        
        
            | 
            | 
           260 | 
                 }
  | 
        
        
            | 
            | 
           261 | 
               }
  | 
        
        
            | 
            | 
           262 | 
              | 
        
        
            | 
            | 
           263 | 
               self.activeStage.isStage = YES;
  | 
        
        
            | 
            | 
           264 | 
               // Do not track background activity tracker for stages.
  | 
        
        
            | 
            | 
           265 | 
               self.activeStage.backgroundActivityTracker = nil;
  | 
        
        
            | 
            | 
           266 | 
             } else {
  | 
        
        
            | 
            | 
           267 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           268 | 
                           @"Failed to create stage %@ because the trace has not been started.", stageName);
  | 
        
        
            | 
            | 
           269 | 
             }
  | 
        
        
            | 
            | 
           270 | 
           }
  | 
        
        
            | 
            | 
           271 | 
              | 
        
        
            | 
            | 
           272 | 
           - (void)startStageNamed:(NSString *)stageName {
  | 
        
        
            | 
            | 
           273 | 
             [self startStageNamed:stageName startTime:nil];
  | 
        
        
            | 
            | 
           274 | 
           }
  | 
        
        
            | 
            | 
           275 | 
              | 
        
        
            | 
            | 
           276 | 
           - (void)stopActiveStage {
  | 
        
        
            | 
            | 
           277 | 
             if (self.activeStage) {
  | 
        
        
            | 
            | 
           278 | 
               [self.activeStage cancel];
  | 
        
        
            | 
            | 
           279 | 
               [self.stages addObject:self.activeStage];
  | 
        
        
            | 
            | 
           280 | 
               self.activeStage = nil;
  | 
        
        
            | 
            | 
           281 | 
             }
  | 
        
        
            | 
            | 
           282 | 
           }
  | 
        
        
            | 
            | 
           283 | 
              | 
        
        
            | 
            | 
           284 | 
           #pragma mark - Counter related methods
  | 
        
        
            | 
            | 
           285 | 
              | 
        
        
            | 
            | 
           286 | 
           - (NSDictionary *)counters {
  | 
        
        
            | 
            | 
           287 | 
             return [self.counterList counters];
  | 
        
        
            | 
            | 
           288 | 
           }
  | 
        
        
            | 
            | 
           289 | 
              | 
        
        
            | 
            | 
           290 | 
           - (NSUInteger)numberOfCounters {
  | 
        
        
            | 
            | 
           291 | 
             return [self.counterList numberOfCounters];
  | 
        
        
            | 
            | 
           292 | 
           }
  | 
        
        
            | 
            | 
           293 | 
              | 
        
        
            | 
            | 
           294 | 
           #pragma mark - Metrics related methods
  | 
        
        
            | 
            | 
           295 | 
              | 
        
        
            | 
            | 
           296 | 
           - (int64_t)valueForIntMetric:(nonnull NSString *)metricName {
  | 
        
        
            | 
            | 
           297 | 
             return [self.counterList valueForIntMetric:metricName];
  | 
        
        
            | 
            | 
           298 | 
           }
  | 
        
        
            | 
            | 
           299 | 
              | 
        
        
            | 
            | 
           300 | 
           - (void)setIntValue:(int64_t)value forMetric:(nonnull NSString *)metricName {
  | 
        
        
            | 
            | 
           301 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           302 | 
               NSString *validatedMetricName = self.isInternal ? metricName : FPRReservableName(metricName);
  | 
        
        
            | 
            | 
           303 | 
               if (validatedMetricName.length > 0) {
  | 
        
        
            | 
            | 
           304 | 
                 [self.counterList setIntValue:value forMetric:validatedMetricName];
  | 
        
        
            | 
            | 
           305 | 
                 [self.activeStage setIntValue:value forMetric:validatedMetricName];
  | 
        
        
            | 
            | 
           306 | 
               } else {
  | 
        
        
            | 
            | 
           307 | 
                 FPRLogError(kFPRTraceInvalidName, @"The metric name is invalid.");
  | 
        
        
            | 
            | 
           308 | 
               }
  | 
        
        
            | 
            | 
           309 | 
             } else {
  | 
        
        
            | 
            | 
           310 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           311 | 
                           @"Failed to set value for metric %@ because trace %@ has not been started.",
  | 
        
        
            | 
            | 
           312 | 
                           metricName, self.name);
  | 
        
        
            | 
            | 
           313 | 
             }
  | 
        
        
            | 
            | 
           314 | 
           }
  | 
        
        
            | 
            | 
           315 | 
              | 
        
        
            | 
            | 
           316 | 
           - (void)incrementMetric:(nonnull NSString *)metricName byInt:(int64_t)incrementValue {
  | 
        
        
            | 
            | 
           317 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           318 | 
               NSString *validatedMetricName = self.isInternal ? metricName : FPRReservableName(metricName);
  | 
        
        
            | 
            | 
           319 | 
               if (validatedMetricName.length > 0) {
  | 
        
        
            | 
            | 
           320 | 
                 [self.counterList incrementMetric:validatedMetricName byInt:incrementValue];
  | 
        
        
            | 
            | 
           321 | 
                 [self.activeStage incrementMetric:validatedMetricName byInt:incrementValue];
  | 
        
        
            | 
            | 
           322 | 
                 FPRLogDebug(kFPRClientMetricLogged, @"Incrementing metric %@ to %lld on trace %@",
  | 
        
        
            | 
            | 
           323 | 
                             validatedMetricName, [self valueForIntMetric:metricName], self.name);
  | 
        
        
            | 
            | 
           324 | 
               } else {
  | 
        
        
            | 
            | 
           325 | 
                 FPRLogError(kFPRTraceInvalidName, @"The metric name is invalid.");
  | 
        
        
            | 
            | 
           326 | 
               }
  | 
        
        
            | 
            | 
           327 | 
             } else {
  | 
        
        
            | 
            | 
           328 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           329 | 
                           @"Failed to increment the trace metric %@ because trace %@ has not been started.",
  | 
        
        
            | 
            | 
           330 | 
                           metricName, self.name);
  | 
        
        
            | 
            | 
           331 | 
             }
  | 
        
        
            | 
            | 
           332 | 
           }
  | 
        
        
            | 
            | 
           333 | 
              | 
        
        
            | 
            | 
           334 | 
           - (void)deleteMetric:(nonnull NSString *)metricName {
  | 
        
        
            | 
            | 
           335 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           336 | 
               [self.counterList deleteMetric:metricName];
  | 
        
        
            | 
            | 
           337 | 
               [self.activeStage deleteMetric:metricName];
  | 
        
        
            | 
            | 
           338 | 
             }
  | 
        
        
            | 
            | 
           339 | 
           }
  | 
        
        
            | 
            | 
           340 | 
              | 
        
        
            | 
            | 
           341 | 
           #pragma mark - Custom attributes related methods
  | 
        
        
            | 
            | 
           342 | 
              | 
        
        
            | 
            | 
           343 | 
           - (NSDictionary<NSString *, NSString *> *)attributes {
  | 
        
        
            | 
            | 
           344 | 
             return [self.customAttributes copy];
  | 
        
        
            | 
            | 
           345 | 
           }
  | 
        
        
            | 
            | 
           346 | 
              | 
        
        
            | 
            | 
           347 | 
           - (void)setValue:(NSString *)value forAttribute:(nonnull NSString *)attribute {
  | 
        
        
            | 
            | 
           348 | 
             BOOL canAddAttribute = YES;
  | 
        
        
            | 
            | 
           349 | 
             if ([self isTraceStopped]) {
  | 
        
        
            | 
            | 
           350 | 
               FPRLogError(kFPRTraceAlreadyStopped,
  | 
        
        
            | 
            | 
           351 | 
                           @"Failed to set attribute %@ because trace %@ has already stopped.", attribute,
  | 
        
        
            | 
            | 
           352 | 
                           self.name);
  | 
        
        
            | 
            | 
           353 | 
               canAddAttribute = NO;
  | 
        
        
            | 
            | 
           354 | 
             }
  | 
        
        
            | 
            | 
           355 | 
              | 
        
        
            | 
            | 
           356 | 
             NSString *validatedName = FPRReservableAttributeName(attribute);
  | 
        
        
            | 
            | 
           357 | 
             NSString *validatedValue = FPRValidatedAttributeValue(value);
  | 
        
        
            | 
            | 
           358 | 
              | 
        
        
            | 
            | 
           359 | 
             if (validatedName == nil) {
  | 
        
        
            | 
            | 
           360 | 
               FPRLogError(kFPRAttributeNoName,
  | 
        
        
            | 
            | 
           361 | 
                           @"Failed to initialize because of a nil or zero length attribute name.");
  | 
        
        
            | 
            | 
           362 | 
               canAddAttribute = NO;
  | 
        
        
            | 
            | 
           363 | 
             }
  | 
        
        
            | 
            | 
           364 | 
              | 
        
        
            | 
            | 
           365 | 
             if (validatedValue == nil) {
  | 
        
        
            | 
            | 
           366 | 
               FPRLogError(kFPRAttributeNoValue,
  | 
        
        
            | 
            | 
           367 | 
                           @"Failed to initialize because of a nil or zero length attribute value.");
  | 
        
        
            | 
            | 
           368 | 
               canAddAttribute = NO;
  | 
        
        
            | 
            | 
           369 | 
             }
  | 
        
        
            | 
            | 
           370 | 
              | 
        
        
            | 
            | 
           371 | 
             if (self.customAttributes.allKeys.count >= kFPRMaxTraceCustomAttributesCount) {
  | 
        
        
            | 
            | 
           372 | 
               FPRLogError(kFPRMaxAttributesReached,
  | 
        
        
            | 
            | 
           373 | 
                           @"Only %d attributes allowed. Already reached maximum attribute count.",
  | 
        
        
            | 
            | 
           374 | 
                           kFPRMaxTraceCustomAttributesCount);
  | 
        
        
            | 
            | 
           375 | 
               canAddAttribute = NO;
  | 
        
        
            | 
            | 
           376 | 
             }
  | 
        
        
            | 
            | 
           377 | 
              | 
        
        
            | 
            | 
           378 | 
             if (canAddAttribute) {
  | 
        
        
            | 
            | 
           379 | 
               // Ensure concurrency during update of attributes.
  | 
        
        
            | 
            | 
           380 | 
               dispatch_sync(self.customAttributesSerialQueue, ^{
  | 
        
        
            | 
            | 
           381 | 
                 self.customAttributes[validatedName] = validatedValue;
  | 
        
        
            | 
            | 
           382 | 
               });
  | 
        
        
            | 
            | 
           383 | 
             }
  | 
        
        
            | 
            | 
           384 | 
             FPRLogDebug(kFPRClientMetricLogged, @"Setting attribute %@ to %@ on trace %@", validatedName,
  | 
        
        
            | 
            | 
           385 | 
                         validatedValue, self.name);
  | 
        
        
            | 
            | 
           386 | 
           }
  | 
        
        
            | 
            | 
           387 | 
              | 
        
        
            | 
            | 
           388 | 
           - (NSString *)valueForAttribute:(NSString *)attribute {
  | 
        
        
            | 
            | 
           389 | 
             // TODO(b/175053654): Should this be happening on the serial queue for thread safety?
  | 
        
        
            | 
            | 
           390 | 
             return self.customAttributes[attribute];
  | 
        
        
            | 
            | 
           391 | 
           }
  | 
        
        
            | 
            | 
           392 | 
              | 
        
        
            | 
            | 
           393 | 
           - (void)removeAttribute:(NSString *)attribute {
  | 
        
        
            | 
            | 
           394 | 
             if ([self isTraceStopped]) {
  | 
        
        
            | 
            | 
           395 | 
               FPRLogError(kFPRTraceNotStarted,
  | 
        
        
            | 
            | 
           396 | 
                           @"Failed to remove attribute %@ because trace %@ has already stopped.", attribute,
  | 
        
        
            | 
            | 
           397 | 
                           self.name);
  | 
        
        
            | 
            | 
           398 | 
               return;
  | 
        
        
            | 
            | 
           399 | 
             }
  | 
        
        
            | 
            | 
           400 | 
              | 
        
        
            | 
            | 
           401 | 
             [self.customAttributes removeObjectForKey:attribute];
  | 
        
        
            | 
            | 
           402 | 
           }
  | 
        
        
            | 
            | 
           403 | 
              | 
        
        
            | 
            | 
           404 | 
           #pragma mark - Utility methods
  | 
        
        
            | 
            | 
           405 | 
              | 
        
        
            | 
            | 
           406 | 
           - (void)sessionChanged:(NSNotification *)notification {
  | 
        
        
            | 
            | 
           407 | 
             if ([self isTraceActive]) {
  | 
        
        
            | 
            | 
           408 | 
               NSDictionary<NSString *, FPRSessionDetails *> *userInfo = notification.userInfo;
  | 
        
        
            | 
            | 
           409 | 
               FPRSessionDetails *sessionDetails = [userInfo valueForKey:kFPRSessionIdNotificationKey];
  | 
        
        
            | 
            | 
           410 | 
               if (sessionDetails) {
  | 
        
        
            | 
            | 
           411 | 
                 [self updateTraceWithSessionId:sessionDetails];
  | 
        
        
            | 
            | 
           412 | 
               }
  | 
        
        
            | 
            | 
           413 | 
             }
  | 
        
        
            | 
            | 
           414 | 
           }
  | 
        
        
            | 
            | 
           415 | 
              | 
        
        
            | 
            | 
           416 | 
           - (void)updateTraceWithSessionId:(FPRSessionDetails *)sessionDetails {
  | 
        
        
            | 
            | 
           417 | 
             dispatch_sync(self.sessionIdSerialQueue, ^{
  | 
        
        
            | 
            | 
           418 | 
               [self.activeSessions addObject:sessionDetails];
  | 
        
        
            | 
            | 
           419 | 
             });
  | 
        
        
            | 
            | 
           420 | 
           }
  | 
        
        
            | 
            | 
           421 | 
              | 
        
        
            | 
            | 
           422 | 
           - (BOOL)isTraceStarted {
  | 
        
        
            | 
            | 
           423 | 
             return self.startTime != nil;
  | 
        
        
            | 
            | 
           424 | 
           }
  | 
        
        
            | 
            | 
           425 | 
              | 
        
        
            | 
            | 
           426 | 
           - (BOOL)isTraceStopped {
  | 
        
        
            | 
            | 
           427 | 
             return (self.startTime != nil && self.stopTime != nil);
  | 
        
        
            | 
            | 
           428 | 
           }
  | 
        
        
            | 
            | 
           429 | 
              | 
        
        
            | 
            | 
           430 | 
           - (BOOL)isTraceActive {
  | 
        
        
            | 
            | 
           431 | 
             return (self.startTime != nil && self.stopTime == nil);
  | 
        
        
            | 
            | 
           432 | 
           }
  | 
        
        
            | 
            | 
           433 | 
              | 
        
        
            | 
            | 
           434 | 
           @end
  |