Skip to content

Commit 9a0a3cd

Browse files
authored
ref: Convert SentryFileIOTrackingIntegration to swift (#7252)
* chore: Convert `SentryNSDataSwizzling` and `SentryNSFileManagerSwizzling` to Swift * refactor: Remove legacy swizzling imports and add new helper classes for swizzling functionality * Add tests for SentryNSDataSwizzlingHelper * Add SentryNSFileManagerSwizzlingHelper and corresponding tests * Use weak reference for tracker * refactor: remove unused tracker property from SentryNSDataSwizzling and SentryNSFileManagerSwizzling * ref: Convert SentryFileIOTrackingIntegration to swift * Remove duplicate references * Remove unused options property
1 parent 84bc307 commit 9a0a3cd

File tree

9 files changed

+80
-60
lines changed

9 files changed

+80
-60
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,7 @@
821821
D8A3649D2C91AA3300AC569B /* SentryReplayApi.h in Headers */ = {isa = PBXBuildFile; fileRef = D8A3649A2C91AA3300AC569B /* SentryReplayApi.h */; settings = {ATTRIBUTES = (Public, ); }; };
822822
D8A65B5D2C98656800974B74 /* SentryReplayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A65B5C2C98656000974B74 /* SentryReplayView.swift */; };
823823
D8ACE3C82762187200F5A213 /* SentryFileIOTrackerHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = D8ACE3C52762187200F5A213 /* SentryFileIOTrackerHelper.m */; };
824-
D8ACE3C92762187200F5A213 /* SentryFileIOTrackingIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = D8ACE3C62762187200F5A213 /* SentryFileIOTrackingIntegration.m */; };
825824
D8ACE3CE2762187D00F5A213 /* SentryFileIOTrackerHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = D8ACE3CB2762187D00F5A213 /* SentryFileIOTrackerHelper.h */; };
826-
D8ACE3CF2762187D00F5A213 /* SentryFileIOTrackingIntegration.h in Headers */ = {isa = PBXBuildFile; fileRef = D8ACE3CC2762187D00F5A213 /* SentryFileIOTrackingIntegration.h */; };
827825
D8AE48BF2C578D540092A2A6 /* SentrySDKLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AE48BE2C578D540092A2A6 /* SentrySDKLog.swift */; };
828826
D8AE48C12C57B1550092A2A6 /* SentryLevelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AE48C02C57B1550092A2A6 /* SentryLevelTests.swift */; };
829827
D8AFC0012BD252B900118BE1 /* SentryOnDemandReplayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AFC0002BD252B900118BE1 /* SentryOnDemandReplayTests.swift */; };
@@ -1978,9 +1976,7 @@
19781976
D8A3649B2C91AA3300AC569B /* SentryReplayApi.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryReplayApi.m; sourceTree = "<group>"; };
19791977
D8A65B5C2C98656000974B74 /* SentryReplayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryReplayView.swift; sourceTree = "<group>"; };
19801978
D8ACE3C52762187200F5A213 /* SentryFileIOTrackerHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryFileIOTrackerHelper.m; sourceTree = "<group>"; };
1981-
D8ACE3C62762187200F5A213 /* SentryFileIOTrackingIntegration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryFileIOTrackingIntegration.m; sourceTree = "<group>"; };
19821979
D8ACE3CB2762187D00F5A213 /* SentryFileIOTrackerHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryFileIOTrackerHelper.h; path = include/SentryFileIOTrackerHelper.h; sourceTree = "<group>"; };
1983-
D8ACE3CC2762187D00F5A213 /* SentryFileIOTrackingIntegration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryFileIOTrackingIntegration.h; path = include/SentryFileIOTrackingIntegration.h; sourceTree = "<group>"; };
19841980
D8AE48B12C5786AA0092A2A6 /* SentryLogC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryLogC.h; path = include/SentryLogC.h; sourceTree = "<group>"; };
19851981
D8AE48BE2C578D540092A2A6 /* SentrySDKLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySDKLog.swift; sourceTree = "<group>"; };
19861982
D8AE48C02C57B1550092A2A6 /* SentryLevelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLevelTests.swift; sourceTree = "<group>"; };
@@ -4107,8 +4103,6 @@
41074103
children = (
41084104
D8ACE3CB2762187D00F5A213 /* SentryFileIOTrackerHelper.h */,
41094105
D8ACE3C52762187200F5A213 /* SentryFileIOTrackerHelper.m */,
4110-
D8ACE3CC2762187D00F5A213 /* SentryFileIOTrackingIntegration.h */,
4111-
D8ACE3C62762187200F5A213 /* SentryFileIOTrackingIntegration.m */,
41124106
);
41134107
name = IO;
41144108
sourceTree = "<group>";
@@ -4208,7 +4202,6 @@
42084202
03F84D2527DD414C008FE43F /* SentryThreadState.hpp in Headers */,
42094203
8E4E7C6D25DAAAFE006AB9E2 /* SentryTransaction.h in Headers */,
42104204
FAE2DABA2E1F318900262307 /* SentryProfilingSwiftHelpers.h in Headers */,
4211-
D8ACE3CF2762187D00F5A213 /* SentryFileIOTrackingIntegration.h in Headers */,
42124205
FA27ECA12EBA325A00F2ECF7 /* SentryTraceContext+Private.h in Headers */,
42134206
7BECF42226145C5D00D9826E /* SentryMechanismContext.h in Headers */,
42144207
63FE718920DA4C1100CDBAE8 /* SentryCrash.h in Headers */,
@@ -4367,6 +4360,8 @@
43674360
6271ADF32BA06D9B0098D2E9 /* SentryInternalSerializable.h in Headers */,
43684361
D8853C842833EABC00700D64 /* SentryANRTrackerV1.h in Headers */,
43694362
63FE715B20DA4C1100CDBAE8 /* SentryCrashSignalInfo.h in Headers */,
4363+
F4F33D502F1F35C500753FE2 /* SentryNSDataSwizzlingHelper.h in Headers */,
4364+
F4F33D512F1F35C500753FE2 /* SentryNSFileManagerSwizzlingHelper.h in Headers */,
43704365
63FE70E520DA4C1000CDBAE8 /* SentryCrashMonitor_CPPException.h in Headers */,
43714366
D867063E27C3BC2400048851 /* SentryCoreDataSwizzling.h in Headers */,
43724367
7D9B07A023D1E89900C5FC8E /* SentryMeta.h in Headers */,
@@ -4839,12 +4834,11 @@
48394834
7B63459B280EB9E200CFA05A /* SentryUIEventTrackingIntegration.m in Sources */,
48404835
15E0A8ED240F2CB000F044E3 /* SentrySerialization.m in Sources */,
48414836
7BC85235245880AE005A70F0 /* SentryDataCategoryMapper.m in Sources */,
4842-
D8ACE3C92762187200F5A213 /* SentryFileIOTrackingIntegration.m in Sources */,
48434837
63FE713B20DA4C1100CDBAE8 /* SentryCrashFileUtils.c in Sources */,
48444838
63FE716920DA4C1100CDBAE8 /* SentryCrashStackCursor.c in Sources */,
48454839
7BA61CCA247D128B00C130A8 /* SentryDefaultThreadInspector.m in Sources */,
48464840
63FE718D20DA4C1100CDBAE8 /* SentryCrashReportStore.c in Sources */,
4847-
F440C1B12F23C24800C52354 /* SentryNSDataSwizzlingHelper.m in Sources */,
4841+
F440C1B12F23C24800C52354 /* SentryNSDatawizzlingHelper.m in Sources */,
48484842
F440C1B22F23C24800C52354 /* SentryNSFileManagerSwizzlingHelper.m in Sources */,
48494843
7BA0C0482805600A003E0326 /* SentryTransportAdapter.m in Sources */,
48504844
63FE712920DA4C1000CDBAE8 /* SentryCrashCPU_arm.c in Sources */,
@@ -4888,6 +4882,8 @@
48884882
7D082B8323C628790029866B /* SentryMeta.m in Sources */,
48894883
63FE710720DA4C1000CDBAE8 /* SentryCrashStackCursor_SelfThread.m in Sources */,
48904884
63FE711120DA4C1000CDBAE8 /* SentryCrashDebug.c in Sources */,
4885+
F4F33D482F1F358400753FE2 /* SentryNSFileManagerSwizzling.swift in Sources */,
4886+
F4F33D492F1F358400753FE2 /* SentryNSDataSwizzling.swift in Sources */,
48914887
7B883F49253D714C00879E62 /* SentryCrashUUIDConversion.c in Sources */,
48924888
843FB3232D0CD04D00558F18 /* SentryUserAccess.m in Sources */,
48934889
63FE716720DA4C1100CDBAE8 /* SentryCrashCPU.c in Sources */,

Sources/Sentry/SentryFileIOTrackingIntegration.m

Lines changed: 0 additions & 43 deletions
This file was deleted.

Sources/Sentry/SentrySDKInternal.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#import "SentryClient+Private.h"
77
#import "SentryCoreDataTrackingIntegration.h"
88
#import "SentryCrash.h"
9-
#import "SentryFileIOTrackingIntegration.h"
109
#import "SentryHub+Private.h"
1110
#import "SentryInternalDefines.h"
1211
#import "SentryLogC.h"
@@ -505,7 +504,7 @@ + (void)endSession
505504
[SentryUIEventTrackingIntegration class],
506505
#endif // SENTRY_HAS_UIKIT
507506
[SentryAutoBreadcrumbTrackingIntegration class], [SentryCoreDataTrackingIntegration class],
508-
[SentryFileIOTrackingIntegration class], nil];
507+
nil];
509508

510509
return defaultIntegrations;
511510
}

Sources/Sentry/include/SentryPrivate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
#import "SentryInstallation.h"
7070
#import "SentryMeta.h"
7171
#import "SentryMsgPackSerializer.h"
72+
#import "SentryNSDataSwizzlingHelper.h"
7273
#import "SentryNSDictionarySanitize.h"
74+
#import "SentryNSFileManagerSwizzlingHelper.h"
7375
#import "SentryPerformanceTracker.h"
7476
#import "SentryProfiler+Private.h"
7577
#import "SentrySDKInternal.h"

Sources/Swift/Core/Integrations/Integrations.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ private struct AnyIntegration {
5252
.init(SentryAutoSessionTrackingIntegration.self),
5353
.init(SentryNetworkTrackingIntegration.self),
5454
.init(SentryHangTrackerIntegrationObjC.self),
55-
.init(SentryMetricsIntegration.self)
55+
.init(SentryMetricsIntegration.self),
56+
.init(SentryFileIOTrackingIntegration.self)
5657
])
5758

5859
#if (os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) || os(visionOS)) && !SENTRY_NO_UIKIT
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
@_implementationOnly import _SentryPrivate
2+
3+
typealias FileIOTrackingIntegrationProvider = FileIOTrackerProvider & NSDataSwizzlingProvider & NSFileManagerSwizzlingProvider
4+
5+
final class SentryFileIOTrackingIntegration<Dependencies: FileIOTrackingIntegrationProvider>: NSObject, SwiftIntegration {
6+
private let tracker: SentryFileIOTracker
7+
private let nsDataSwizzling: SentryNSDataSwizzling
8+
private let nsFileManagerSwizzling: SentryNSFileManagerSwizzling
9+
10+
init?(with options: Options, dependencies: Dependencies) {
11+
// Check if tracing is enabled
12+
guard options.isTracingEnabled else {
13+
SentrySDKLog.debug("Not going to enable \(Self.name) because isTracingEnabled is disabled.")
14+
return nil
15+
}
16+
17+
// Check if auto performance tracing is enabled
18+
guard options.enableAutoPerformanceTracing else {
19+
SentrySDKLog.debug("Not going to enable \(Self.name) because enableAutoPerformanceTracing is disabled.")
20+
return nil
21+
}
22+
23+
// Check if file IO tracing is enabled
24+
guard options.enableFileIOTracing else {
25+
SentrySDKLog.debug("Not going to enable \(Self.name) because enableFileIOTracing is disabled.")
26+
return nil
27+
}
28+
29+
self.tracker = dependencies.fileIOTracker
30+
self.nsDataSwizzling = dependencies.nsDataSwizzling
31+
self.nsFileManagerSwizzling = dependencies.nsFileManagerSwizzling
32+
33+
super.init()
34+
35+
tracker.enable()
36+
37+
nsDataSwizzling.start(withOptions: options, tracker: tracker)
38+
nsFileManagerSwizzling.start(withOptions: options, tracker: tracker)
39+
}
40+
41+
func uninstall() {
42+
tracker.disable()
43+
44+
nsDataSwizzling.stop()
45+
nsFileManagerSwizzling.stop()
46+
}
47+
48+
static var name: String {
49+
"SentryFileIOTrackingIntegration"
50+
}
51+
}

Sources/Swift/SentryDependencyContainer.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,22 @@ extension SentryDependencyContainer: DateProviderProvider {}
331331

332332
extension SentryDependencyContainer: AutoSessionTrackingProvider { }
333333

334+
protocol FileIOTrackerProvider {
335+
var fileIOTracker: SentryFileIOTracker { get }
336+
}
337+
338+
protocol NSDataSwizzlingProvider {
339+
var nsDataSwizzling: SentryNSDataSwizzling { get }
340+
}
341+
342+
protocol NSFileManagerSwizzlingProvider {
343+
var nsFileManagerSwizzling: SentryNSFileManagerSwizzling { get }
344+
}
345+
346+
extension SentryDependencyContainer: FileIOTrackerProvider { }
347+
extension SentryDependencyContainer: NSDataSwizzlingProvider { }
348+
extension SentryDependencyContainer: NSFileManagerSwizzlingProvider { }
349+
334350
#if (os(iOS) || os(tvOS) || os(visionOS)) && !SENTRY_NO_UIKIT
335351
protocol FramesTrackingProvider {
336352
var framesTracker: SentryFramesTracker { get }
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import Sentry
1+
@_spi(Private) @testable import Sentry
22
import XCTest
33

44
class SentryFileIoTrackingUnitTests: XCTestCase {
55

66
func test_FileIOTracking_Disabled() {
77
let options = Options()
88
options.enableFileIOTracing = false
9-
let sut = SentryFileIOTrackingIntegration()
10-
let result = sut.install(with: options)
9+
let sut = SentryFileIOTrackingIntegration(with: options, dependencies: SentryDependencyContainer.sharedInstance())
1110

12-
XCTAssertFalse(result)
11+
XCTAssertNil(sut)
1312
}
1413
}

Tests/SentryTests/SentryTests-Bridging-Header.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
#import "SentryEnvelopeRateLimit.h"
9696
#import "SentryEvent+Private.h"
9797
#import "SentryFileIOTrackerHelper.h"
98-
#import "SentryFileIOTrackingIntegration.h"
9998
#import "SentryFileManager+Test.h"
10099
#import "SentryFileManagerHelper.h"
101100
#import "SentryFormatter.h"

0 commit comments

Comments
 (0)