-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyPerformanceMonitoring.m
350 lines (271 loc) · 11 KB
/
CountlyPerformanceMonitoring.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// CountlyPerformanceMonitoring.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
NSString* const kCountlyPMKeyType = @"type";
NSString* const kCountlyPMKeyNetwork = @"network";
NSString* const kCountlyPMKeyDevice = @"device";
NSString* const kCountlyPMKeyName = @"name";
NSString* const kCountlyPMKeyAPMMetrics = @"apm_metrics";
NSString* const kCountlyPMKeyResponseTime = @"response_time";
NSString* const kCountlyPMKeyResponsePayloadSize = @"response_payload_size";
NSString* const kCountlyPMKeyResponseCode = @"response_code";
NSString* const kCountlyPMKeyRequestPayloadSize = @"request_payload_size";
NSString* const kCountlyPMKeyDuration = @"duration";
NSString* const kCountlyPMKeyStartTime = @"stz";
NSString* const kCountlyPMKeyEndTime = @"etz";
NSString* const kCountlyPMKeyAppStart = @"app_start";
NSString* const kCountlyPMKeyAppInForeground = @"app_in_foreground";
NSString* const kCountlyPMKeyAppInBackground = @"app_in_background";
@interface CountlyPerformanceMonitoring ()
@property (nonatomic) NSMutableDictionary* startedCustomTraces;
@property (nonatomic) BOOL hasAlreadyRecordedAppStartDurationTrace;
@end
@implementation CountlyPerformanceMonitoring
BOOL enableAppStartTimeTracking;
BOOL enableManualAppLoadedTrigger;
BOOL enableForegroundBackgroundTracking;
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyPerformanceMonitoring* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (void) startWithConfig:(CountlyAPMConfig *) apmConfig
{
enableAppStartTimeTracking = apmConfig.enableAppStartTimeTracking;
enableManualAppLoadedTrigger = apmConfig.enableManualAppLoadedTrigger;
if(enableAppStartTimeTracking && !enableManualAppLoadedTrigger) {
CLY_LOG_W(@"%s, Automatic app start tracking is currently not supported, use manual app loaded trigger for now by setting 'config.apm.enableManualAppLoadedTrigger' Then call '[Countly.sharedInstance appLoadingFinished]'", __FUNCTION__);
}
enableForegroundBackgroundTracking = apmConfig.enableForegroundBackgroundTracking;
[self startPerformanceMonitoring];
}
- (instancetype)init
{
if (self = [super init])
{
self.startedCustomTraces = NSMutableDictionary.new;
}
return self;
}
#pragma mark ---
- (void)startPerformanceMonitoring
{
if (!enableForegroundBackgroundTracking)
return;
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
CLY_LOG_D(@"Starting performance monitoring foreground/background tracking...");
#if (TARGET_OS_OSX)
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationWillResignActive:) name:NSApplicationWillResignActiveNotification object:nil];
#elif (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
#endif
if (CountlyDeviceInfo.isInBackground)
[self startBackgroundTrace];
else
[self startForegroundTrace];
}
- (void)stopPerformanceMonitoring
{
#if (TARGET_OS_OSX)
[NSNotificationCenter.defaultCenter removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self name:NSApplicationWillResignActiveNotification object:nil];
#elif (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
[NSNotificationCenter.defaultCenter removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
#endif
[self clearAllCustomTraces];
}
#pragma mark ---
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
CLY_LOG_D(@"applicationDidBecomeActive: (Performance Monitoring)");
if (!enableForegroundBackgroundTracking)
return;
[self startForegroundTrace];
}
- (void)applicationWillResignActive:(NSNotification *)notification
{
CLY_LOG_D(@"applicationWillResignActive: (Performance Monitoring)");
if (!enableForegroundBackgroundTracking)
return;
[self startBackgroundTrace];
}
- (void)startForegroundTrace
{
if (!enableForegroundBackgroundTracking)
return;
[self endBackgroundTrace];
[self startCustomTrace:kCountlyPMKeyAppInForeground];
}
- (void)endForegroundTrace
{
if (!enableForegroundBackgroundTracking)
return;
[self endCustomTrace:kCountlyPMKeyAppInForeground metrics:nil];
}
- (void)startBackgroundTrace
{
if (!enableForegroundBackgroundTracking)
return;
[self endForegroundTrace];
[self startCustomTrace:kCountlyPMKeyAppInBackground];
}
- (void)endBackgroundTrace
{
if (!enableForegroundBackgroundTracking)
return;
[self endCustomTrace:kCountlyPMKeyAppInBackground metrics:nil];
}
#pragma mark ---
- (void)recordAppStartDurationTraceWithStartTime:(long long)startTime endTime:(long long)endTime
{
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
if(!enableAppStartTimeTracking || !enableManualAppLoadedTrigger)
{
CLY_LOG_W(@"Set 'enableAppStartTimeTracking' and 'enableManualAppLoadedTrigger' in config to record App start duration trace!");
return;
}
if (self.hasAlreadyRecordedAppStartDurationTrace)
{
CLY_LOG_W(@"App start duration trace can be recorded once per app launch. So, it will not be recorded this time!");
return;
}
long long appStartDuration = endTime - startTime;
CLY_LOG_D(@"App is loaded and displayed its first view in %lld milliseconds.", appStartDuration);
NSDictionary* metrics =
@{
kCountlyPMKeyDuration: @(appStartDuration),
};
NSDictionary* trace =
@{
kCountlyPMKeyType: kCountlyPMKeyDevice,
kCountlyPMKeyName: kCountlyPMKeyAppStart,
kCountlyPMKeyAPMMetrics: metrics,
kCountlyPMKeyStartTime: @(startTime),
kCountlyPMKeyEndTime: @(endTime),
};
[CountlyConnectionManager.sharedInstance sendPerformanceMonitoringTrace:[trace cly_JSONify]];
self.hasAlreadyRecordedAppStartDurationTrace = YES;
}
- (void)recordNetworkTrace:(NSString *)traceName
requestPayloadSize:(NSInteger)requestPayloadSize
responsePayloadSize:(NSInteger)responsePayloadSize
responseStatusCode:(NSInteger)responseStatusCode
startTime:(long long)startTime
endTime:(long long)endTime
{
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
if (!traceName.length)
return;
traceName = [traceName cly_truncatedKey:@"Network trace name"];
NSDictionary* metrics =
@{
kCountlyPMKeyRequestPayloadSize: @(requestPayloadSize),
kCountlyPMKeyResponseTime: @(endTime - startTime),
kCountlyPMKeyResponseCode: @(responseStatusCode),
kCountlyPMKeyResponsePayloadSize: @(responsePayloadSize),
};
NSDictionary* trace =
@{
kCountlyPMKeyType: kCountlyPMKeyNetwork,
kCountlyPMKeyName: traceName,
kCountlyPMKeyAPMMetrics: metrics,
kCountlyPMKeyStartTime: @(startTime),
kCountlyPMKeyEndTime: @(endTime),
};
[CountlyConnectionManager.sharedInstance sendPerformanceMonitoringTrace:[trace cly_JSONify]];
}
- (void)startCustomTrace:(NSString *)traceName
{
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
if (!traceName.length)
return;
@synchronized (self.startedCustomTraces)
{
if (self.startedCustomTraces[traceName])
{
CLY_LOG_W(@"Custom trace with name '%@' already started!", traceName);
return;
}
NSNumber* startTime = @((long long)(CountlyCommon.sharedInstance.uniqueTimestamp * 1000));
self.startedCustomTraces[traceName] = startTime;
}
CLY_LOG_D(@"Custom trace with name '%@' just started!", traceName);
}
- (void)endCustomTrace:(NSString *)traceName metrics:(NSDictionary *)metrics
{
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
if (!traceName.length)
return;
NSNumber* startTime = nil;
@synchronized (self.startedCustomTraces)
{
startTime = self.startedCustomTraces[traceName];
[self.startedCustomTraces removeObjectForKey:traceName];
}
if (!startTime)
{
CLY_LOG_W(@"Custom trace with name '%@' not started yet or cancelled/ended before!", traceName);
return;
}
traceName = [traceName cly_truncatedKey:@"Custom trace name"];
NSDictionary* metricsTruncated = [metrics cly_truncated:@"Custom trace metric"];
metrics = [metricsTruncated cly_limited:@"Custom trace metric"];
NSNumber* endTime = @((long long)(CountlyCommon.sharedInstance.uniqueTimestamp * 1000));
NSMutableDictionary* mutableMetrics = metrics.mutableCopy;
if (!mutableMetrics)
mutableMetrics = NSMutableDictionary.new;
long long duration = endTime.longLongValue - startTime.longLongValue;
mutableMetrics[kCountlyPMKeyDuration] = @(duration);
NSDictionary* trace =
@{
kCountlyPMKeyType: kCountlyPMKeyDevice,
kCountlyPMKeyName: traceName,
kCountlyPMKeyAPMMetrics: mutableMetrics,
kCountlyPMKeyStartTime: startTime,
kCountlyPMKeyEndTime: endTime,
};
CLY_LOG_D(@"Custom trace with name '%@' just ended with duration %lld ms.", traceName, duration);
[CountlyConnectionManager.sharedInstance sendPerformanceMonitoringTrace:[trace cly_JSONify]];
}
- (void)cancelCustomTrace:(NSString *)traceName
{
if (!CountlyConsentManager.sharedInstance.consentForPerformanceMonitoring)
return;
if (!traceName.length)
return;
NSNumber* startTime = nil;
@synchronized (self.startedCustomTraces)
{
startTime = self.startedCustomTraces[traceName];
[self.startedCustomTraces removeObjectForKey:traceName];
}
if (!startTime)
{
CLY_LOG_W(@"Custom trace with name '%@' not started yet or cancelled/ended before!", traceName);
return;
}
CLY_LOG_D(@"Custom trace with name '%@' cancelled!", traceName);
}
- (void)clearAllCustomTraces
{
@synchronized (self.startedCustomTraces)
{
[self.startedCustomTraces removeAllObjects];
}
}
@end