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/FPRClient.h"
|
|
|
16 |
#import "FirebasePerformance/Sources/FPRClient+Private.h"
|
|
|
17 |
|
|
|
18 |
#import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
|
|
|
19 |
#import "FirebasePerformance/Sources/AppActivity/FPRScreenTraceTracker+Private.h"
|
|
|
20 |
#import "FirebasePerformance/Sources/AppActivity/FPRScreenTraceTracker.h"
|
|
|
21 |
#import "FirebasePerformance/Sources/AppActivity/FPRSessionManager+Private.h"
|
|
|
22 |
#import "FirebasePerformance/Sources/AppActivity/FPRTraceBackgroundActivityTracker.h"
|
|
|
23 |
#import "FirebasePerformance/Sources/Common/FPRConsoleURLGenerator.h"
|
|
|
24 |
#import "FirebasePerformance/Sources/Common/FPRConstants.h"
|
|
|
25 |
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
|
|
|
26 |
#import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags.h"
|
|
|
27 |
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
|
|
|
28 |
#import "FirebasePerformance/Sources/FPRNanoPbUtils.h"
|
|
|
29 |
#import "FirebasePerformance/Sources/Instrumentation/FPRInstrumentation.h"
|
|
|
30 |
#import "FirebasePerformance/Sources/Loggers/FPRGDTLogger.h"
|
|
|
31 |
#import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h"
|
|
|
32 |
#import "FirebasePerformance/Sources/Timer/FIRTrace+Private.h"
|
|
|
33 |
|
|
|
34 |
#import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
|
|
|
35 |
|
|
|
36 |
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
|
|
|
37 |
|
|
|
38 |
@interface FPRClient ()
|
|
|
39 |
|
|
|
40 |
/** The original configuration object used to initialize the client. */
|
|
|
41 |
@property(nonatomic, strong) FPRConfiguration *config;
|
|
|
42 |
|
|
|
43 |
/** The object that manages all automatic class instrumentation. */
|
|
|
44 |
@property(nonatomic) FPRInstrumentation *instrumentation;
|
|
|
45 |
|
|
|
46 |
@end
|
|
|
47 |
|
|
|
48 |
@implementation FPRClient
|
|
|
49 |
|
|
|
50 |
+ (void)load {
|
|
|
51 |
__weak NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
|
|
52 |
__block id listener;
|
|
|
53 |
|
|
|
54 |
void (^observerBlock)(NSNotification *) = ^(NSNotification *aNotification) {
|
|
|
55 |
NSDictionary *appInfoDict = aNotification.userInfo;
|
|
|
56 |
NSNumber *isDefaultApp = appInfoDict[kFIRAppIsDefaultAppKey];
|
|
|
57 |
if (![isDefaultApp boolValue]) {
|
|
|
58 |
return;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
NSString *appName = appInfoDict[kFIRAppNameKey];
|
|
|
62 |
FIRApp *app = [FIRApp appNamed:appName];
|
|
|
63 |
FIROptions *options = app.options;
|
|
|
64 |
NSError *error = nil;
|
|
|
65 |
|
|
|
66 |
// Based on the environment variable SDK decides if events are dispatchd to Autopush or Prod.
|
|
|
67 |
// By default, events are sent to Prod.
|
|
|
68 |
BOOL useAutoPush = NO;
|
|
|
69 |
NSDictionary<NSString *, NSString *> *environment = [NSProcessInfo processInfo].environment;
|
|
|
70 |
if (environment[@"FPR_AUTOPUSH_ENV"] != nil &&
|
|
|
71 |
[environment[@"FPR_AUTOPUSH_ENV"] isEqualToString:@"1"]) {
|
|
|
72 |
useAutoPush = YES;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
FPRConfiguration *configuration = [FPRConfiguration configurationWithAppID:options.googleAppID
|
|
|
76 |
APIKey:options.APIKey
|
|
|
77 |
autoPush:useAutoPush];
|
|
|
78 |
if (![[self sharedInstance] startWithConfiguration:configuration error:&error]) {
|
|
|
79 |
FPRLogError(kFPRClientInitialize, @"Failed to initialize the client with error: %@.", error);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
[notificationCenter removeObserver:listener];
|
|
|
83 |
listener = nil;
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
// Register the Perf library for Firebase Core tracking.
|
|
|
87 |
[FIRApp registerLibrary:@"fire-perf" // From go/firebase-sdk-platform-info
|
|
|
88 |
withVersion:[NSString stringWithUTF8String:kFPRSDKVersion]];
|
|
|
89 |
listener = [notificationCenter addObserverForName:kFIRAppReadyToConfigureSDKNotification
|
|
|
90 |
object:[FIRApp class]
|
|
|
91 |
queue:nil
|
|
|
92 |
usingBlock:observerBlock];
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
+ (FPRClient *)sharedInstance {
|
|
|
96 |
static FPRClient *sharedInstance = nil;
|
|
|
97 |
static dispatch_once_t token;
|
|
|
98 |
dispatch_once(&token, ^{
|
|
|
99 |
sharedInstance = [[FPRClient alloc] init];
|
|
|
100 |
});
|
|
|
101 |
return sharedInstance;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
- (instancetype)init {
|
|
|
105 |
self = [super init];
|
|
|
106 |
if (self) {
|
|
|
107 |
_instrumentation = [[FPRInstrumentation alloc] init];
|
|
|
108 |
_swizzled = NO;
|
|
|
109 |
_eventsQueue = dispatch_queue_create("com.google.perf.FPREventsQueue", DISPATCH_QUEUE_SERIAL);
|
|
|
110 |
_eventsQueueGroup = dispatch_group_create();
|
|
|
111 |
_configuration = [FPRConfigurations sharedInstance];
|
|
|
112 |
_projectID = [FIROptions defaultOptions].projectID;
|
|
|
113 |
_bundleID = [FIROptions defaultOptions].bundleID;
|
|
|
114 |
}
|
|
|
115 |
return self;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
- (BOOL)startWithConfiguration:(FPRConfiguration *)config error:(NSError *__autoreleasing *)error {
|
|
|
119 |
self.config = config;
|
|
|
120 |
NSInteger logSource = [self.configuration logSource];
|
|
|
121 |
|
|
|
122 |
dispatch_group_async(self.eventsQueueGroup, self.eventsQueue, ^{
|
|
|
123 |
// Create the Logger for the Perf SDK events to be sent to Google Data Transport.
|
|
|
124 |
self.gdtLogger = [[FPRGDTLogger alloc] initWithLogSource:logSource];
|
|
|
125 |
|
|
|
126 |
#ifdef TARGET_HAS_MOBILE_CONNECTIVITY
|
|
|
127 |
// Create telephony network information object ahead of time to avoid runtime delays.
|
|
|
128 |
FPRNetworkInfo();
|
|
|
129 |
#endif
|
|
|
130 |
|
|
|
131 |
// Update the configuration flags.
|
|
|
132 |
[self.configuration update];
|
|
|
133 |
|
|
|
134 |
[FPRClient cleanupClearcutCacheDirectory];
|
|
|
135 |
});
|
|
|
136 |
|
|
|
137 |
// Set up instrumentation.
|
|
|
138 |
[self checkAndStartInstrumentation];
|
|
|
139 |
|
|
|
140 |
self.configured = YES;
|
|
|
141 |
|
|
|
142 |
static dispatch_once_t onceToken;
|
|
|
143 |
dispatch_once(&onceToken, ^{
|
|
|
144 |
FPRLogInfo(kFPRClientInitialize,
|
|
|
145 |
@"Firebase Performance Monitoring is successfully initialized! In a minute, visit "
|
|
|
146 |
@"the Firebase console to view your data: %@",
|
|
|
147 |
[FPRConsoleURLGenerator generateDashboardURLWithProjectID:self.projectID
|
|
|
148 |
bundleID:self.bundleID]);
|
|
|
149 |
});
|
|
|
150 |
|
|
|
151 |
return YES;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
- (void)checkAndStartInstrumentation {
|
|
|
155 |
BOOL instrumentationEnabled = self.configuration.isInstrumentationEnabled;
|
|
|
156 |
if (instrumentationEnabled && !self.isSwizzled) {
|
|
|
157 |
[self.instrumentation registerInstrumentGroup:kFPRInstrumentationGroupNetworkKey];
|
|
|
158 |
[self.instrumentation registerInstrumentGroup:kFPRInstrumentationGroupUIKitKey];
|
|
|
159 |
self.swizzled = YES;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
#pragma mark - Public methods
|
|
|
164 |
|
|
|
165 |
- (void)logTrace:(FIRTrace *)trace {
|
|
|
166 |
if (self.configured == NO) {
|
|
|
167 |
FPRLogError(kFPRClientPerfNotConfigured, @"Dropping trace event %@. Perf SDK not configured.",
|
|
|
168 |
trace.name);
|
|
|
169 |
return;
|
|
|
170 |
}
|
|
|
171 |
if ([trace isCompleteAndValid]) {
|
|
|
172 |
dispatch_group_async(self.eventsQueueGroup, self.eventsQueue, ^{
|
|
|
173 |
firebase_perf_v1_PerfMetric metric = FPRGetPerfMetricMessage(self.config.appID);
|
|
|
174 |
FPRSetTraceMetric(&metric, FPRGetTraceMetric(trace));
|
|
|
175 |
FPRSetApplicationProcessState(&metric,
|
|
|
176 |
FPRApplicationProcessState(trace.backgroundTraceState));
|
|
|
177 |
|
|
|
178 |
// Log the trace metric with its console URL.
|
|
|
179 |
if ([trace.name hasPrefix:kFPRPrefixForScreenTraceName]) {
|
|
|
180 |
FPRLogInfo(kFPRClientMetricLogged,
|
|
|
181 |
@"Logging trace metric - %@ %.4fms. In a minute, visit the Firebase console to "
|
|
|
182 |
@"view your data: %@",
|
|
|
183 |
trace.name, metric.trace_metric.duration_us / 1000.0,
|
|
|
184 |
[FPRConsoleURLGenerator generateScreenTraceURLWithProjectID:self.projectID
|
|
|
185 |
bundleID:self.bundleID
|
|
|
186 |
traceName:trace.name]);
|
|
|
187 |
} else {
|
|
|
188 |
FPRLogInfo(kFPRClientMetricLogged,
|
|
|
189 |
@"Logging trace metric - %@ %.4fms. In a minute, visit the Firebase console to "
|
|
|
190 |
@"view your data: %@",
|
|
|
191 |
trace.name, metric.trace_metric.duration_us / 1000.0,
|
|
|
192 |
[FPRConsoleURLGenerator generateCustomTraceURLWithProjectID:self.projectID
|
|
|
193 |
bundleID:self.bundleID
|
|
|
194 |
traceName:trace.name]);
|
|
|
195 |
}
|
|
|
196 |
[self processAndLogEvent:metric];
|
|
|
197 |
});
|
|
|
198 |
} else {
|
|
|
199 |
FPRLogWarning(kFPRClientInvalidTrace, @"Invalid trace, skipping send.");
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
- (void)logNetworkTrace:(nonnull FPRNetworkTrace *)trace {
|
|
|
204 |
if (self.configured == NO) {
|
|
|
205 |
FPRLogError(kFPRClientPerfNotConfigured, @"Dropping trace event %@. Perf SDK not configured.",
|
|
|
206 |
trace.URLRequest.URL.absoluteString);
|
|
|
207 |
return;
|
|
|
208 |
}
|
|
|
209 |
dispatch_group_async(self.eventsQueueGroup, self.eventsQueue, ^{
|
|
|
210 |
if ([trace isValid]) {
|
|
|
211 |
firebase_perf_v1_NetworkRequestMetric networkRequestMetric =
|
|
|
212 |
FPRGetNetworkRequestMetric(trace);
|
|
|
213 |
int64_t duration = networkRequestMetric.has_time_to_response_completed_us
|
|
|
214 |
? networkRequestMetric.time_to_response_completed_us
|
|
|
215 |
: 0;
|
|
|
216 |
|
|
|
217 |
NSString *responseCode = networkRequestMetric.has_http_response_code
|
|
|
218 |
? [@(networkRequestMetric.http_response_code) stringValue]
|
|
|
219 |
: @"UNKNOWN";
|
|
|
220 |
FPRLogInfo(kFPRClientMetricLogged,
|
|
|
221 |
@"Logging network request trace - %@, Response code: %@, %.4fms",
|
|
|
222 |
trace.trimmedURLString, responseCode, duration / 1000.0);
|
|
|
223 |
firebase_perf_v1_PerfMetric metric = FPRGetPerfMetricMessage(self.config.appID);
|
|
|
224 |
FPRSetNetworkRequestMetric(&metric, networkRequestMetric);
|
|
|
225 |
FPRSetApplicationProcessState(&metric,
|
|
|
226 |
FPRApplicationProcessState(trace.backgroundTraceState));
|
|
|
227 |
|
|
|
228 |
[self processAndLogEvent:metric];
|
|
|
229 |
}
|
|
|
230 |
});
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
- (void)logGaugeMetric:(nonnull NSArray *)gaugeData forSessionId:(nonnull NSString *)sessionId {
|
|
|
234 |
if (self.configured == NO) {
|
|
|
235 |
FPRLogError(kFPRClientPerfNotConfigured, @"Dropping session event. Perf SDK not configured.");
|
|
|
236 |
return;
|
|
|
237 |
}
|
|
|
238 |
dispatch_group_async(self.eventsQueueGroup, self.eventsQueue, ^{
|
|
|
239 |
firebase_perf_v1_PerfMetric metric = FPRGetPerfMetricMessage(self.config.appID);
|
|
|
240 |
firebase_perf_v1_GaugeMetric gaugeMetric = firebase_perf_v1_GaugeMetric_init_default;
|
|
|
241 |
if ((gaugeData != nil && gaugeData.count != 0) && (sessionId != nil && sessionId.length != 0)) {
|
|
|
242 |
gaugeMetric = FPRGetGaugeMetric(gaugeData, sessionId);
|
|
|
243 |
}
|
|
|
244 |
FPRSetGaugeMetric(&metric, gaugeMetric);
|
|
|
245 |
[self processAndLogEvent:metric];
|
|
|
246 |
});
|
|
|
247 |
|
|
|
248 |
// Check and update the sessionID if the session is running for too long.
|
|
|
249 |
[[FPRSessionManager sharedInstance] renewSessionIdIfRunningTooLong];
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
- (void)processAndLogEvent:(firebase_perf_v1_PerfMetric)event {
|
|
|
253 |
BOOL tracingEnabled = self.configuration.isDataCollectionEnabled;
|
|
|
254 |
if (!tracingEnabled) {
|
|
|
255 |
FPRLogDebug(kFPRClientPerfNotConfigured, @"Dropping event since data collection is disabled.");
|
|
|
256 |
return;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
BOOL sdkEnabled = [self.configuration sdkEnabled];
|
|
|
260 |
if (!sdkEnabled) {
|
|
|
261 |
FPRLogInfo(kFPRClientSDKDisabled, @"Dropping event since Performance SDK is disabled.");
|
|
|
262 |
return;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
static dispatch_once_t onceToken;
|
|
|
266 |
dispatch_once(&onceToken, ^{
|
|
|
267 |
if (self.installations == nil) {
|
|
|
268 |
// Delayed initialization of installations because FIRApp needs to be configured first.
|
|
|
269 |
self.installations = [FIRInstallations installations];
|
|
|
270 |
}
|
|
|
271 |
});
|
|
|
272 |
|
|
|
273 |
// Attempts to dispatch events if successfully retrieve installation ID.
|
|
|
274 |
[self.installations
|
|
|
275 |
installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
|
|
|
276 |
if (error) {
|
|
|
277 |
FPRLogError(kFPRClientInstanceIDNotAvailable, @"FIRInstallations error: %@",
|
|
|
278 |
error.description);
|
|
|
279 |
} else {
|
|
|
280 |
dispatch_group_async(self.eventsQueueGroup, self.eventsQueue, ^{
|
|
|
281 |
firebase_perf_v1_PerfMetric updatedEvent = event;
|
|
|
282 |
updatedEvent.application_info.app_instance_id = FPREncodeString(identifier);
|
|
|
283 |
[self.gdtLogger logEvent:updatedEvent];
|
|
|
284 |
});
|
|
|
285 |
}
|
|
|
286 |
}];
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
#pragma mark - Clearcut log directory removal methods
|
|
|
290 |
|
|
|
291 |
+ (void)cleanupClearcutCacheDirectory {
|
|
|
292 |
NSString *logDirectoryPath = [FPRClient logDirectoryPath];
|
|
|
293 |
|
|
|
294 |
if (logDirectoryPath != nil) {
|
|
|
295 |
BOOL logDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:logDirectoryPath];
|
|
|
296 |
|
|
|
297 |
if (logDirectoryExists) {
|
|
|
298 |
NSError *directoryError = nil;
|
|
|
299 |
[[NSFileManager defaultManager] removeItemAtPath:logDirectoryPath error:&directoryError];
|
|
|
300 |
|
|
|
301 |
if (directoryError) {
|
|
|
302 |
FPRLogDebug(kFPRClientTempDirectory,
|
|
|
303 |
@"Failed to delete the stale log directory at path: %@ with error: %@.",
|
|
|
304 |
logDirectoryPath, directoryError);
|
|
|
305 |
}
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
+ (NSString *)logDirectoryPath {
|
|
|
311 |
static NSString *cacheDir;
|
|
|
312 |
static NSString *fireperfCacheDir;
|
|
|
313 |
static dispatch_once_t onceToken;
|
|
|
314 |
|
|
|
315 |
dispatch_once(&onceToken, ^{
|
|
|
316 |
cacheDir =
|
|
|
317 |
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
|
|
|
318 |
|
|
|
319 |
if (!cacheDir) {
|
|
|
320 |
fireperfCacheDir = nil;
|
|
|
321 |
} else {
|
|
|
322 |
fireperfCacheDir = [cacheDir stringByAppendingPathComponent:@"firebase_perf_logging"];
|
|
|
323 |
}
|
|
|
324 |
});
|
|
|
325 |
|
|
|
326 |
return fireperfCacheDir;
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
#pragma mark - Unswizzling, use only for unit tests
|
|
|
330 |
|
|
|
331 |
- (void)disableInstrumentation {
|
|
|
332 |
[self.instrumentation deregisterInstrumentGroup:kFPRInstrumentationGroupNetworkKey];
|
|
|
333 |
[self.instrumentation deregisterInstrumentGroup:kFPRInstrumentationGroupUIKitKey];
|
|
|
334 |
self.swizzled = NO;
|
|
|
335 |
[self.configuration setInstrumentationEnabled:NO];
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
@end
|