forked from getsentry/sentry-cocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentryFileIOTrackerHelper.m
More file actions
324 lines (270 loc) · 11.2 KB
/
SentryFileIOTrackerHelper.m
File metadata and controls
324 lines (270 loc) · 11.2 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
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
#import "SentryFileIOTrackerHelper.h"
#import "SentryByteCountFormatter.h"
#import "SentryClient+Private.h"
#import "SentryFrame.h"
#import "SentryHub+Private.h"
#import "SentryInternalDefines.h"
#import "SentryLogC.h"
#import "SentrySDK+Private.h"
#import "SentryScope+Private.h"
#import "SentrySpan.h"
#import "SentrySpanDataKey.h"
#import "SentrySpanOperation.h"
#import "SentrySpanProtocol.h"
#import "SentryStacktrace.h"
#import "SentrySwift.h"
#import "SentryThread.h"
#import "SentryTracer.h"
@interface SentryFileIOTrackerHelper ()
@property (nonatomic, assign) BOOL isEnabled;
@property (nonatomic, strong) NSMutableSet<NSData *> *processingData;
@property (nonatomic, copy) SentryStacktrace *_Nullable (^stacktraceRetrieval)(void);
@end
@implementation SentryFileIOTrackerHelper
NSString *const SENTRY_TRACKING_COUNTER_KEY = @"SENTRY_TRACKING_COUNTER_KEY";
- (instancetype)initWithThreadInspector:(SentryStacktrace *_Nullable (^)(void))stacktraceRetrieval
{
if (self = [super init]) {
self.stacktraceRetrieval = stacktraceRetrieval;
}
return self;
}
- (void)enable
{
@synchronized(self) {
self.isEnabled = YES;
}
}
- (void)disable
{
@synchronized(self) {
self.isEnabled = NO;
}
}
- (BOOL)measureNSData:(NSData *)data
writeToFile:(NSString *)path
atomically:(BOOL)useAuxiliaryFile
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
method:(BOOL (^)(NSString *, BOOL))method
{
id<SentrySpan> span = [self startTrackingWritingNSData:data
filePath:path
origin:origin
processDirectoryPath:processDirectoryPath];
BOOL result = method(path, useAuxiliaryFile);
if (span != nil) {
[self finishTrackingNSData:@(data.length) span:span];
}
return result;
}
- (BOOL)measureNSData:(NSData *)data
writeToFile:(NSString *)path
options:(NSDataWritingOptions)writeOptionsMask
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
error:(NSError **)error
method:(BOOL (^)(NSString *, NSDataWritingOptions, NSError **))method
{
id<SentrySpan> span = [self startTrackingWritingNSData:data
filePath:path
origin:origin
processDirectoryPath:processDirectoryPath];
BOOL result = method(path, writeOptionsMask, error);
if (span != nil) {
[self finishTrackingNSData:@(data.length) span:span];
}
return result;
}
- (void)measureNSDataFromFile:(NSString *)path
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
method:(NSNumber * (^)(void))method
{
id<SentrySpan> span = [self startTrackingReadingFilePath:path
origin:origin
operation:SentrySpanOperationFileRead
processDirectoryPath:processDirectoryPath];
NSNumber *length = method();
if (span != nil) {
[self finishTrackingNSData:length span:span];
}
[self endTrackingFile];
}
- (void)measureNSDataFromURL:(NSURL *)url
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
method:(NSNumber * (^)(void))method
{
// We dont track reads from a url that is not a file url
// because these reads are handled by NSURLSession and
// SentryNetworkTracker will create spans in these cases.
if (![url.scheme isEqualToString:NSURLFileScheme]) {
method();
return;
}
id<SentrySpan> span = [self startTrackingReadingFilePath:url.path
origin:origin
operation:SentrySpanOperationFileRead
processDirectoryPath:processDirectoryPath];
NSNumber *length = method();
if (span != nil) {
[self finishTrackingNSData:length span:span];
}
[self endTrackingFile];
return;
}
- (BOOL)measureNSFileManagerCreateFileAtPath:(NSString *)path
data:(NSData *)data
attributes:(NSDictionary<NSFileAttributeKey, id> *)attributes
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
method:
(BOOL (^)(NSString *_Nonnull, NSData *_Nonnull,
NSDictionary<NSFileAttributeKey, id> *_Nonnull))method
{
id<SentrySpan> span = [self startTrackingWritingNSData:data
filePath:path
origin:origin
processDirectoryPath:processDirectoryPath];
BOOL result = method(path, data, attributes);
if (span != nil) {
[self finishTrackingNSData:@(data.length) span:span];
}
return result;
}
- (nullable id<SentrySpan>)spanForPath:(NSString *)path
origin:(NSString *)origin
operation:(NSString *)operation
processDirectoryPath:(NSString *)processDirectoryPath
{
return [self spanForPath:path
origin:origin
operation:operation
processDirectoryPath:processDirectoryPath
size:0];
}
- (nullable id<SentrySpan>)spanForPath:(NSString *)path
origin:(NSString *)origin
operation:(NSString *)operation
processDirectoryPath:(NSString *)processDirectoryPath
size:(NSUInteger)size
{
@synchronized(self) {
if (!self.isEnabled) {
return nil;
}
}
if ([self ignoreFile:path]) {
return nil;
}
NSString *spanDescription = [self transactionDescriptionForFile:path fileSize:size];
id<SentrySpan> _Nullable currentSpan = [SentrySDKInternal.currentHub.scope span];
if (currentSpan == NULL) {
SENTRY_LOG_DEBUG(@"No transaction bound to scope. Won't track file IO operation.");
return nil;
}
id<SentrySpan> _Nullable ioSpan = [currentSpan startChildWithOperation:operation
description:spanDescription];
if (ioSpan == nil) {
SENTRY_LOG_DEBUG(@"No transaction bound to scope. Won't track file IO operation.");
return nil;
}
ioSpan.origin = origin;
[ioSpan setDataValue:path forKey:SentrySpanDataKeyFilePath];
if (size > 0) {
[ioSpan setDataValue:[NSNumber numberWithUnsignedInteger:size]
forKey:SentrySpanDataKeyFileSize];
}
SENTRY_LOG_DEBUG(
@"Automatically started a new span with description: %@, operation: %@, origin: %@",
ioSpan.description, operation, origin);
[self mainThreadExtraInfo:ioSpan processDirectoryPath:processDirectoryPath];
return ioSpan;
}
- (void)mainThreadExtraInfo:(id<SentrySpan>)span
processDirectoryPath:(NSString *)processDirectoryPath
{
BOOL isMainThread = [NSThread isMainThread];
[span setDataValue:@(isMainThread) forKey:SPAN_DATA_BLOCKED_MAIN_THREAD];
if (!isMainThread) {
return;
}
SentryStacktrace *stackTrace = self.stacktraceRetrieval();
NSArray *frames = [stackTrace.frames
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(SentryFrame *frame,
NSDictionary<NSString *, id> *bindings) {
return [frame.package hasPrefix:processDirectoryPath];
}]];
if (frames.count <= 1) {
// This means the call was made only by system APIs
// and only the 'main' frame remains in the stack
// therefore, there is nothing to do about it
// and we should not report it as an issue.
[span setDataValue:@(NO) forKey:SPAN_DATA_BLOCKED_MAIN_THREAD];
} else {
[((SentrySpan *)span) setFrames:frames];
}
}
- (nullable id<SentrySpan>)startTrackingWritingNSData:(NSData *)data
filePath:(NSString *)path
origin:(NSString *)origin
processDirectoryPath:(NSString *)processDirectoryPath
{
return [self spanForPath:path
origin:origin
operation:SentrySpanOperationFileWrite
processDirectoryPath:processDirectoryPath
size:data.length];
}
- (nullable id<SentrySpan>)startTrackingReadingFilePath:(NSString *)path
origin:(NSString *)origin
operation:(NSString *)operation
processDirectoryPath:(NSString *)processDirectoryPath
{
// Some iOS versions nest constructors calls. This counter help us avoid create more than one
// span for the same operation.
NSNumber *count =
[[NSThread currentThread].threadDictionary objectForKey:SENTRY_TRACKING_COUNTER_KEY];
[[NSThread currentThread].threadDictionary setObject:[NSNumber numberWithInt:count.intValue + 1]
forKey:SENTRY_TRACKING_COUNTER_KEY];
if (count)
return nil;
return [self spanForPath:path
origin:origin
operation:operation
processDirectoryPath:processDirectoryPath
size:0];
}
- (void)endTrackingFile
{
NSNumber *count =
[[NSThread currentThread].threadDictionary objectForKey:SENTRY_TRACKING_COUNTER_KEY];
if (!count)
return;
if (count.intValue <= 1) {
[[NSThread currentThread].threadDictionary removeObjectForKey:SENTRY_TRACKING_COUNTER_KEY];
} else {
[[NSThread currentThread].threadDictionary
setObject:[NSNumber numberWithInt:count.intValue - 1]
forKey:SENTRY_TRACKING_COUNTER_KEY];
}
}
- (void)finishTrackingNSData:(NSNumber *)length span:(id<SentrySpan>)span
{
[span setDataValue:length forKey:SentrySpanDataKeyFileSize];
[span finish];
SENTRY_LOG_DEBUG(@"Automatically finished span %@", span.description);
}
- (BOOL)ignoreFile:(NSString *)path
{
SentryFileManager *fileManager = [SentrySDKInternal.currentHub getClient].fileManager;
return fileManager.sentryPath != nil && [path hasPrefix:fileManager.sentryPath];
}
- (NSString *)transactionDescriptionForFile:(NSString *)path fileSize:(NSUInteger)size
{
return size > 0 ? [NSString stringWithFormat:@"%@ (%@)", [path lastPathComponent],
[SentryByteCountFormatter bytesCountDescription:size]]
: [NSString stringWithFormat:@"%@", [path lastPathComponent]];
}
@end