-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathCountlyHealthTracker.m
More file actions
292 lines (243 loc) · 10.1 KB
/
CountlyHealthTracker.m
File metadata and controls
292 lines (243 loc) · 10.1 KB
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
//
// CountlyHealthTracker.m
// CountlyTestApp-iOS
//
// Created by Arif Burak Demiray on 20.05.2025.
// Copyright © 2025 Countly. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CountlyHealthTracker.h"
#import "CountlyCommon.h"
@interface CountlyHealthTracker ()
@property (nonatomic, assign) long countLogWarning;
@property (nonatomic, assign) long countLogError;
@property (nonatomic, assign) long countBackoffRequest;
@property (nonatomic, assign) long countConsecutiveBackoffRequest;
@property (nonatomic, assign) long consecutiveBackoffRequest;
@property (nonatomic, assign) NSInteger statusCode;
@property (nonatomic, copy) NSString *errorMessage;
@property (nonatomic, assign) BOOL healthCheckEnabled;
@property (nonatomic, assign) BOOL healthCheckSent;
@property (nonatomic, strong) dispatch_queue_t hcQueue;
@end
@implementation CountlyHealthTracker
NSString * const keyLogError = @"LErr";
NSString * const keyLogWarning = @"LWar";
NSString * const keyStatusCode = @"RStatC";
NSString * const keyErrorMessage = @"REMsg";
NSString * const keyBackoffRequest = @"BReq";
NSString * const keyConsecutiveBackoffRequest = @"CBReq";
NSString * const requestKeyErrorCount = @"el";
NSString * const requestKeyWarningCount = @"wl";
NSString * const requestKeyStatusCode = @"sc";
NSString * const requestKeyRequestError = @"em";
NSString * const requestKeyBackoffRequest = @"bom";
NSString * const requestKeyConsecutiveBackoffRequest = @"cbom";
+ (instancetype)sharedInstance {
static CountlyHealthTracker *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (instancetype)init{
self = [super init];
if (self) {
_errorMessage = @"";
_statusCode = -1;
_healthCheckSent = NO;
_healthCheckEnabled = YES;
// queue for health tracker state
_hcQueue = dispatch_queue_create("ly.count.healthtracker.queue", DISPATCH_QUEUE_SERIAL);
NSDictionary *initialState = [CountlyPersistency.sharedInstance retrieveHealthCheckTrackerState];
[self setupInitialCounters:initialState];
}
return self;
}
- (void)setupInitialCounters:(NSDictionary *)initialState {
if (initialState == nil || [initialState count] == 0) {
return;
}
self.countLogWarning = [initialState[keyLogWarning] longValue];
self.countLogError = [initialState[keyLogError] longValue];
self.statusCode = [initialState[keyStatusCode] integerValue];
self.errorMessage = initialState[keyErrorMessage] ?: @"";
self.countBackoffRequest = [initialState[keyBackoffRequest] longValue];
self.consecutiveBackoffRequest = [initialState[keyConsecutiveBackoffRequest] longValue];
CLY_LOG_D(@"%s loaded initial health check state: [%@]", __FUNCTION__, initialState);
}
- (void)logWarning {
dispatch_async(self.hcQueue, ^{
self.countLogWarning++;
});
}
- (void)logError {
dispatch_async(self.hcQueue, ^{
self.countLogError++;
});
}
- (void)logFailedNetworkRequestWithStatusCode:(NSInteger)statusCode
errorResponse:(NSString *)errorResponse {
if (statusCode <= 0 || statusCode >= 1000 || errorResponse == nil) {
return;
}
dispatch_async(self.hcQueue, ^{
self.statusCode = statusCode;
if (errorResponse.length > 1000) {
// copy ensures immutability even if errorResponse was NSMutableString
self.errorMessage = [errorResponse substringToIndex:1000];
} else {
self.errorMessage = [errorResponse copy];
}
CLY_LOG_D(@"%s statusCode: [%d], errorResponse: [%@]", __FUNCTION__, (int)statusCode, errorResponse);
});
}
- (void)logBackoffRequest {
CLY_LOG_D(@"%s", __FUNCTION__);
dispatch_async(self.hcQueue, ^{
self.countBackoffRequest++;
self.countConsecutiveBackoffRequest++;
});
}
- (void)logConsecutiveBackoffRequest {
CLY_LOG_D(@"%s", __FUNCTION__);
dispatch_async(self.hcQueue, ^{
self.consecutiveBackoffRequest = MAX(self.consecutiveBackoffRequest, self.countConsecutiveBackoffRequest);
self.countConsecutiveBackoffRequest = 0;
});
}
- (void)clearAndSave {
CLY_LOG_D(@"%s", __FUNCTION__);
dispatch_async(self.hcQueue, ^{
[self clearValues];
[CountlyPersistency.sharedInstance storeHealthCheckTrackerState:@{}];
});
}
- (void)saveState {
CLY_LOG_D(@"%s", __FUNCTION__);
dispatch_async(self.hcQueue, ^{
[self logConsecutiveBackoffRequest];
NSDictionary *healthCheckState = @{
keyLogWarning: @(self.countLogWarning),
keyLogError: @(self.countLogError),
keyStatusCode: @(self.statusCode),
keyErrorMessage: self.errorMessage ?: @"",
keyBackoffRequest: @(self.countBackoffRequest),
keyConsecutiveBackoffRequest: @(self.consecutiveBackoffRequest)
};
[CountlyPersistency.sharedInstance storeHealthCheckTrackerState:healthCheckState];
});
}
- (void)resetInstance {
CLY_LOG_D(@"%s resetting health check state", __FUNCTION__);
dispatch_async(self.hcQueue, ^{
[self clearValues];
self->_healthCheckSent = NO;
self->_healthCheckEnabled = YES;
[CountlyPersistency.sharedInstance storeHealthCheckTrackerState:@{}];
});
}
- (void)clearValues {
CLY_LOG_W(@"%s clearing counters", __FUNCTION__);
self.countLogWarning = 0;
self.countLogError = 0;
self.statusCode = -1;
self.errorMessage = @"";
self.countBackoffRequest = 0;
self.consecutiveBackoffRequest = 0;
self.countConsecutiveBackoffRequest = 0;
}
- (void)sendHealthCheck {
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary) {
CLY_LOG_W(@"%s currently in temporary id mode, omitting", __FUNCTION__);
return;
}
if (!CountlyServerConfig.sharedInstance.networkingEnabled)
{
CLY_LOG_D(@"%s 'sendHealthCheck' is aborted: SDK Networking is disabled from server config!", __FUNCTION__);
return;
}
if (!_healthCheckEnabled || _healthCheckSent) {
CLY_LOG_D(@"%s health check status, healthCheckSent: [%d], healthCheckEnabled: [%d]", __FUNCTION__, _healthCheckSent, _healthCheckEnabled);
return;
}
NSURLSessionTask* task = [CountlyCommon.sharedInstance.URLSession dataTaskWithRequest:[self healthCheckRequest] completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
if (error)
{
CLY_LOG_W(@"%s error while sending health checks error: [%@]", __FUNCTION__, error);
return;
}
NSError *jsonError;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError || !jsonResponse) {
CLY_LOG_I(@"%s error while sending health checks, Failed to parse JSON response error: [%@]", __FUNCTION__, jsonError);
return;
}
if (!jsonResponse[@"result"]) {
CLY_LOG_D(@"%s Retrieved request response does not match expected pattern response: [%@]", __FUNCTION__, jsonResponse);
return;
}
[self clearAndSave];
self->_healthCheckSent = YES;
}];
[task resume];
}
- (NSString *)dictionaryToJsonString:(NSDictionary *)json {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
NSString *encodedData = @"";
if (!error && data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
encodedData = [jsonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
} else {
CLY_LOG_W(@"%s failed to create json for hc request error: [%@]", __FUNCTION__, error);
}
return encodedData;
}
- (NSURLRequest *)healthCheckRequest {
__block long snapshotLogError;
__block long snapshotLogWarning;
__block NSInteger snapshotStatusCode;
__block NSString *snapshotErrorMessage;
__block long snapshotBackoffRequest;
__block long snapshotConsecutiveBackoffRequest;
dispatch_sync(self.hcQueue, ^{
snapshotLogError = self.countLogError;
snapshotLogWarning = self.countLogWarning;
snapshotStatusCode = self.statusCode;
snapshotErrorMessage = [self.errorMessage copy] ?: @"";
snapshotBackoffRequest = self.countBackoffRequest;
snapshotConsecutiveBackoffRequest = self.consecutiveBackoffRequest;
});
NSString *queryString = [CountlyConnectionManager.sharedInstance queryEssentials];
queryString = [queryString stringByAppendingFormat:@"&%@=%@", @"hc", [self dictionaryToJsonString:@{
requestKeyErrorCount: @(snapshotLogError),
requestKeyWarningCount: @(snapshotLogWarning),
requestKeyStatusCode: @(snapshotStatusCode),
requestKeyRequestError: snapshotErrorMessage,
requestKeyBackoffRequest: @(snapshotBackoffRequest),
requestKeyConsecutiveBackoffRequest: @(snapshotConsecutiveBackoffRequest)
}]];
queryString = [queryString stringByAppendingFormat:@"&%@=%@", @"metrics", [self dictionaryToJsonString:@{
CLYMetricKeyAppVersion: CountlyDeviceInfo.appVersion
}]];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSString* hcSendURL = [CountlyConnectionManager.sharedInstance.host stringByAppendingFormat:@"%@",kCountlyEndpointI];
CLY_LOG_D(@"%s generated health check request: [%@]", __FUNCTION__, queryString);
if (queryString.length > kCountlyGETRequestMaxLength || CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:hcSendURL]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
NSString* withQueryString = [hcSendURL stringByAppendingFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:withQueryString]];
return request;
}
}
@end