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/FIRPerformance.h"
|
|
|
16 |
#import "FirebasePerformance/Sources/FIRPerformance+Internal.h"
|
|
|
17 |
#import "FirebasePerformance/Sources/FIRPerformance_Private.h"
|
|
|
18 |
|
|
|
19 |
#import "FirebasePerformance/Sources/Common/FPRConstants.h"
|
|
|
20 |
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
|
|
|
21 |
#import "FirebasePerformance/Sources/FPRClient+Private.h"
|
|
|
22 |
#import "FirebasePerformance/Sources/FPRClient.h"
|
|
|
23 |
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
|
|
|
24 |
#import "FirebasePerformance/Sources/FPRDataUtils.h"
|
|
|
25 |
#import "FirebasePerformance/Sources/Instrumentation/FPRInstrumentation.h"
|
|
|
26 |
#import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h"
|
|
|
27 |
|
|
|
28 |
static NSString *const kFirebasePerfErrorDomain = @"com.firebase.perf";
|
|
|
29 |
|
|
|
30 |
@implementation FIRPerformance
|
|
|
31 |
|
|
|
32 |
#pragma mark - Public methods
|
|
|
33 |
|
|
|
34 |
+ (instancetype)sharedInstance {
|
|
|
35 |
static FIRPerformance *firebasePerformance = nil;
|
|
|
36 |
static dispatch_once_t onceToken;
|
|
|
37 |
dispatch_once(&onceToken, ^{
|
|
|
38 |
firebasePerformance = [[FIRPerformance alloc] init];
|
|
|
39 |
});
|
|
|
40 |
return firebasePerformance;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
+ (FIRTrace *)startTraceWithName:(NSString *)name {
|
|
|
44 |
FIRTrace *trace = [[self sharedInstance] traceWithName:name];
|
|
|
45 |
[trace start];
|
|
|
46 |
return trace;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
- (FIRTrace *)traceWithName:(NSString *)name {
|
|
|
50 |
if (![self isPerfConfigured]) {
|
|
|
51 |
FPRLogError(kFPRTraceNotCreated, @"Failed creating trace %@. Firebase is not configured.",
|
|
|
52 |
name);
|
|
|
53 |
[NSException raise:kFirebasePerfErrorDomain
|
|
|
54 |
format:@"The default Firebase app has not yet been configured. Add [FirebaseApp "
|
|
|
55 |
@"configure] to your application initialization."];
|
|
|
56 |
return nil;
|
|
|
57 |
}
|
|
|
58 |
FIRTrace *trace = [[FIRTrace alloc] initWithName:name];
|
|
|
59 |
return trace;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Checks if the SDK has been successfully configured.
|
|
|
64 |
*
|
|
|
65 |
* @return YES if SDK is configured successfully, otherwise NO.
|
|
|
66 |
*/
|
|
|
67 |
- (BOOL)isPerfConfigured {
|
|
|
68 |
return self.fprClient.isConfigured;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
#pragma mark - Internal methods
|
|
|
72 |
|
|
|
73 |
- (instancetype)init {
|
|
|
74 |
self = [super init];
|
|
|
75 |
if (self) {
|
|
|
76 |
_customAttributes = [[NSMutableDictionary<NSString *, NSString *> alloc] init];
|
|
|
77 |
_customAttributesSerialQueue =
|
|
|
78 |
dispatch_queue_create("com.google.perf.customAttributes", DISPATCH_QUEUE_SERIAL);
|
|
|
79 |
_fprClient = [FPRClient sharedInstance];
|
|
|
80 |
}
|
|
|
81 |
return self;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
- (BOOL)isDataCollectionEnabled {
|
|
|
85 |
return [FPRConfigurations sharedInstance].isDataCollectionEnabled;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
- (void)setDataCollectionEnabled:(BOOL)dataCollectionEnabled {
|
|
|
89 |
BOOL performanceDataCollectionEnabled = self.dataCollectionEnabled;
|
|
|
90 |
if (performanceDataCollectionEnabled != dataCollectionEnabled) {
|
|
|
91 |
[[FPRConfigurations sharedInstance] setDataCollectionEnabled:dataCollectionEnabled];
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
- (BOOL)isInstrumentationEnabled {
|
|
|
96 |
return self.fprClient.isSwizzled || [FPRConfigurations sharedInstance].isInstrumentationEnabled;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
- (void)setInstrumentationEnabled:(BOOL)instrumentationEnabled {
|
|
|
100 |
[[FPRConfigurations sharedInstance] setInstrumentationEnabled:instrumentationEnabled];
|
|
|
101 |
if (instrumentationEnabled) {
|
|
|
102 |
[self.fprClient checkAndStartInstrumentation];
|
|
|
103 |
} else {
|
|
|
104 |
if (self.fprClient.isSwizzled) {
|
|
|
105 |
FPRLogError(kFPRInstrumentationDisabledAfterConfigure,
|
|
|
106 |
@"Failed to disable instrumentation because Firebase Performance has already "
|
|
|
107 |
@"been configured. It will be disabled when the app restarts.");
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
#pragma mark - Custom attributes related methods
|
|
|
113 |
|
|
|
114 |
- (NSDictionary<NSString *, NSString *> *)attributes {
|
|
|
115 |
return [self.customAttributes copy];
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
- (void)setValue:(NSString *)value forAttribute:(nonnull NSString *)attribute {
|
|
|
119 |
NSString *validatedName = FPRReservableAttributeName(attribute);
|
|
|
120 |
NSString *validatedValue = FPRValidatedAttributeValue(value);
|
|
|
121 |
|
|
|
122 |
BOOL canAddAttribute = YES;
|
|
|
123 |
if (validatedName == nil) {
|
|
|
124 |
FPRLogError(kFPRAttributeNoName,
|
|
|
125 |
@"Failed to initialize because of a nil or zero length attribute name.");
|
|
|
126 |
canAddAttribute = NO;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if (validatedValue == nil) {
|
|
|
130 |
FPRLogError(kFPRAttributeNoValue,
|
|
|
131 |
@"Failed to initialize because of a nil or zero length attribute value.");
|
|
|
132 |
canAddAttribute = NO;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if (self.customAttributes.allKeys.count >= kFPRMaxGlobalCustomAttributesCount) {
|
|
|
136 |
FPRLogError(kFPRMaxAttributesReached,
|
|
|
137 |
@"Only %d attributes allowed. Already reached maximum attribute count.",
|
|
|
138 |
kFPRMaxGlobalCustomAttributesCount);
|
|
|
139 |
canAddAttribute = NO;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
if (canAddAttribute) {
|
|
|
143 |
// Ensure concurrency during update of attributes.
|
|
|
144 |
dispatch_sync(self.customAttributesSerialQueue, ^{
|
|
|
145 |
self.customAttributes[validatedName] = validatedValue;
|
|
|
146 |
});
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
- (NSString *)valueForAttribute:(NSString *)attribute {
|
|
|
151 |
// TODO(b/175053654): Should this be happening on the serial queue for thread safety?
|
|
|
152 |
return self.customAttributes[attribute];
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
- (void)removeAttribute:(NSString *)attribute {
|
|
|
156 |
[self.customAttributes removeObjectForKey:attribute];
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
@end
|