forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyAPMNetworkLog.m
134 lines (111 loc) · 4.67 KB
/
CountlyAPMNetworkLog.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
// CountlyAPMNetworkLog.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyAPMNetworkLog ()
@property (nonatomic) NSTimeInterval startTime;
@property (nonatomic) NSTimeInterval endTime;
@property (nonatomic) NSInteger HTTPStatusCode;
@property (nonatomic) long long sentDataSize;
@property (nonatomic) long long receivedDataSize;
@property (nonatomic) NSInteger connectionType;
@end
NSString* const kCountlyReservedEventAPM = @"[CLY]_apm";
@implementation CountlyAPMNetworkLog
+ (instancetype)createWithRequest:(NSURLRequest *)request startImmediately:(BOOL)startImmediately
{
NSString* hostAndPath = [request.URL.host stringByAppendingString:request.URL.path];
__block BOOL isException = NO;
[CountlyAPM.sharedInstance.exceptionURLs
enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if([request.URL.host isEqualToString:obj] || [hostAndPath hasPrefix:obj])
{
isException = YES;
*stop = YES;
}
}];
if (isException) return nil;
CountlyAPMNetworkLog* nl = CountlyAPMNetworkLog.new;
nl.request = request;
nl.sentDataSize = [self.class sentDataSizeForRequest:request];
if(startImmediately)
{
nl.connectionType = CountlyDeviceInfo.connectionType;
nl.startTime = NSDate.date.timeIntervalSince1970;
}
return nl;
}
- (void)start
{
self.sentDataSize = [self.class sentDataSizeForRequest:self.request];
self.connectionType = CountlyDeviceInfo.connectionType;
self.startTime = NSDate.date.timeIntervalSince1970;
}
- (void)updateWithResponse:(NSURLResponse *)response
{
self.HTTPStatusCode =((NSHTTPURLResponse*)response).statusCode;
self.receivedDataSize = [response expectedContentLength];
if(self.receivedDataSize == NSURLResponseUnknownLength)
self.receivedDataSize = 0; //NOTE: sometimes expectedContentLength is not available
}
- (void)finishWithStatusCode:(NSInteger)statusCode andDataSize:(long long)dataSize
{
self.HTTPStatusCode = statusCode;
self.receivedDataSize = dataSize;
[self finish];
}
- (void)finish
{
self.endTime = NSDate.date.timeIntervalSince1970;
NSDictionary* segmentation =
@{
@"n": self.request.URL.absoluteString,
@"e": @(self.HTTPStatusCode),
@"h": self.request.URL.host,
@"p": self.request.URL.path,
@"c": @(self.connectionType),
@"H": @YES,
@"u": @NO
};
[Countly.sharedInstance recordEvent:kCountlyReservedEventAPM segmentation:segmentation count:1 sum:self.sentDataSize + self.receivedDataSize duration:self.endTime - self.startTime timestamp:self.startTime];
COUNTLY_LOG(@"APM log recorded:\n%@", self);
}
+ (long long)sentDataSizeForRequest:(NSURLRequest *)request
{
__block long long sentDataSize = 0;
[request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop)
{
sentDataSize += [key lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + [obj lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
}];
sentDataSize += [request.URL.absoluteString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
sentDataSize += request.HTTPBody.length;
return sentDataSize;
}
- (NSString *)description
{
return [NSString stringWithFormat: @"Request host: %@ \n"
"Request path: %@ \n"
"Start Time: %f \n"
"End Time: %f \n"
"Time Elapsed: %f \n"
"HTTP Status Code: %lu \n"
"Sent Data Size: %lu \n"
"Received Data Size: %lu \n"
"Connection Type: %i \n"
"Request Successfull: %i \n"
"\n\n",
self.request.URL.host,
self.request.URL.path,
self.startTime,
self.endTime,
self.endTime-self.startTime,
(long)self.HTTPStatusCode,
(long)self.sentDataSize,
(long)self.receivedDataSize,
(int)self.connectionType,
self.connectionType!=0 && self.HTTPStatusCode/100 == 2] ;
}
@end