Skip to content

Commit baffd0b

Browse files
authored
feat(apple): add manual file I/O tracing and experimental swizzling options (#12999)
1 parent 2417118 commit baffd0b

File tree

1 file changed

+100
-14
lines changed

1 file changed

+100
-14
lines changed

docs/platforms/apple/common/tracing/instrumentation/automatic-instrumentation.mdx

+100-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ If another transaction is already bound to the scope (if it's been set manually,
3939

4040
To disable the `UIViewController` Tracing:
4141

42-
4342
```swift {tabTitle:Swift}
4443
import Sentry
4544

@@ -67,7 +66,6 @@ SentrySDK.start { options in
6766
6867
You can deactivate tracing for specific UIViewControllers by configuring <PlatformLink to="/configuration/swizzling#skip-swizzling-for-specific-classes">`swizzleClassNameExcludes`</PlatformLink>:
6968
70-
7169
```swift {tabTitle:Swift}
7270
import Sentry
7371
@@ -91,7 +89,6 @@ SentrySDK.start { options in
9189
9290
[UIViewController]: https://developer.apple.com/documentation/uikit/uiviewcontroller
9391
94-
9592
## App Start Tracing
9693
9794
This feature is available for iOS, tvOS, and Mac Catalyst.
@@ -141,7 +138,6 @@ You can filter for different app start types in [Discover](/product/explore/disc
141138
142139
To enable prewarmed app start tracing:
143140
144-
145141
```swift {tabTitle:Swift}
146142
import Sentry
147143
@@ -178,7 +174,6 @@ Network Tracking is enabled by default once you <PlatformLink to="/tracing/">set
178174
179175
To disable the HTTP instrumentation:
180176
181-
182177
```swift {tabTitle:Swift}
183178
import Sentry
184179
@@ -207,7 +202,6 @@ Sentry adds an extra header with the trace id in the outgoing HTTP requests to c
207202
You can set the `tracePropagationTarget` option to filter which requests Sentry adds the extra header to.
208203
For example, to ensure that only your app backend will receive the trace id.
209204
210-
211205
```swift {tabTitle:Swift}
212206
import Sentry
213207
@@ -234,8 +228,7 @@ If `tracePropagationTargets` is not provided, trace data is attached to every ou
234228
235229
The Sentry SDK adds spans for file I/O operations to ongoing transactions bound to the scope. Currently, the SDK supports operations with [NSData][NSData], but many other APIs like [NSFileManager][NSFileManager], [NSString][NSString] and [NSBundle][NSBundle] uses [NSData][NSData].
236230
237-
Since 8.0.0, this feature has been enabled by default. To disable it:
238-
231+
Since 8.0.0, this feature has been enabled by default to trace `NSData` operations. To disable it:
239232
240233
```swift {tabTitle:Swift}
241234
import Sentry
@@ -261,17 +254,114 @@ SentrySDK.start { options in
261254
}];
262255
```
263256
257+
Starting with iOS 18, macOS 15, and tvOS 18, the `NSFileManager` is no longer backed by `NSData` and needs to be swizzled directly.
258+
This feature is currently experimental and disabled by default.
259+
To use automatic file I/O tracing for `NSFileManager` you need to enable it:
260+
261+
```swift {tabTitle:Swift}
262+
import Sentry
263+
264+
SentrySDK.start { options in
265+
options.dsn = "___PUBLIC_DSN___"
266+
options.enableFileIOTracing = true
267+
options.experimental.enableFileManagerSwizzling = true
268+
}
269+
```
270+
271+
```objc {tabTitle:Objective-C}
272+
@import Sentry;
273+
274+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
275+
options.dsn = "___PUBLIC_DSN___"
276+
options.enableFileIOTracing = YES;
277+
options.experimental.enableFileManagerSwizzling = YES;
278+
}];
279+
```
280+
281+
Starting with iOS 18, tvOS 18, and macOS 15 the Swift classes `Data` and `FileManager` are no longer backed by their Objective-C counterparts `NSData` and `NSFileManager` and therefore cannot be instrumented automatically anymore. Instead, please use [Manual File I/O Tracing](#manual-fileio-tracing).
282+
264283
[NSData]: https://developer.apple.com/documentation/foundation/nsdata
265284
[NSFileManager]: https://developer.apple.com/documentation/foundation/nsfilemanager
266285
[NSString]: https://developer.apple.com/documentation/foundation/nsstring
267286
[NSBundle]: https://developer.apple.com/documentation/foundation/nsbundle
268287
288+
### Manual File/IO Tracing
289+
290+
Starting with version 8.48.0, the Sentry SDK offers extensions for the types `Data` and `FileManager` to track reading and writing data, as well as creating, moving, copying, and deleting files.
291+
292+
The method signatures of the extension resemble the ones of the original types, but they add a span to the current transaction:
293+
294+
**Data Operations:**
295+
296+
- `Data.init(contentsOfUrlWithSentryTracing:)` for `Data(contentsOf:)`
297+
- `data.writeWithSentryTracing(to:options:)` for `data.write(to:)`
298+
299+
```swift {tabTitle:Swift}
300+
// Read data from a file URL
301+
let data = Data(contentsOfUrlWithSentryTracing: url)
302+
303+
// Write data to a file URL
304+
data.writeWithSentryTracing(to: url)
305+
```
306+
307+
**FileManager Operations:**
308+
309+
- `FileManager.createFileWithSentryTracing(atPath:contents:attributes:)` for `FileManager.createFile(atPath:contents:attributes:)`
310+
- `FileManager.moveItemWithSentryTracing(at:to:)` for `FileManager.moveItem(at:to:)`
311+
- `FileManager.moveItemWithSentryTracing(atPath:toPath:)` for `FileManager.moveItem(atPath:toPath:)`
312+
- `FileManager.copyItemWithSentryTracing(at:to:)` for `FileManager.copyItem(at:to:)`
313+
- `FileManager.copyItemWithSentryTracing(atPath:toPath:)` for `FileManager.copyItem(atPath:toPath:)`
314+
- `FileManager.removeItemWithSentryTracing(at:)` for `FileManager.removeItem(at:)`
315+
- `FileManager.removeItemWithSentryTracing(atPath:)` for `FileManager.removeItem(atPath:)`
316+
317+
```swift {tabTitle:Swift}
318+
let fileManager = FileManager.default
319+
320+
// Create a file with given content
321+
fileManager.createFileWithSentryTracing(atPath: path, contents: data)
322+
323+
// Move a file or directory
324+
try fileManager.moveItemWithSentryTracing(at: sourceUrl, to: destinationUrl)
325+
try fileManager.moveItemWithSentryTracing(atPath: sourcePath, toPath: destinationPath)
326+
327+
// Copy a file or directory
328+
try fileManager.copyItemWithSentryTracing(at: sourceUrl, to: destinationUrl)
329+
try fileManager.copyItemWithSentryTracing(atPath: sourcePath, toPath: destinationPath)
330+
331+
// Remove a file or directory
332+
try fileManager.removeItemWithSentryTracing(at: url)
333+
try fileManager.removeItemWithSentryTracing(atPath: path)
334+
```
335+
336+
We recommend you disable the swizzling of `NSData` and `NSFileManager` when using manual file I/O tracing, as the internal behavior of these classes differs between platform versions and can cause duplicate spans in your traces.
337+
338+
```swift {tabTitle:Swift}
339+
import Sentry
340+
341+
SentrySDK.start { options in
342+
options.dsn = "___PUBLIC_DSN___";
343+
options.enableFileIOTracing = true
344+
options.experimental.enableDataSwizzling = false
345+
options.experimental.enableFileManagerSwizzling = false
346+
}
347+
```
348+
349+
```objc {tabTitle:Objective-C}
350+
@import Sentry;
351+
352+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
353+
options.dsn = @"___PUBLIC_DSN___";
354+
options.enableFileIOTracing = YES;
355+
options.experimental.enableDataSwizzling = NO;
356+
options.experimental.enableFileManagerSwizzling = NO;
357+
}];
358+
```
359+
269360
## Core Data Tracing
270361
271362
The Sentry SDK adds spans for Core Data operations to ongoing transactions bound to the scope. Currently, the SDK supports fetch and save operations with [NSManagedObjectContext](https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext).
272363
Since 8.0.0, this feature has been enabled by default. To disable it:
273364
274-
275365
```swift {tabTitle:Swift}
276366
import Sentry
277367
@@ -300,7 +390,6 @@ SentrySDK.start { options in
300390
301391
User interaction tracing, once enabled, captures transactions for clicks. This feature is unavailable for SwiftUI. Since 8.0.0, this feature has been enabled by default. To disable it:
302392
303-
304393
```swift {tabTitle:Swift}
305394
import Sentry
306395
@@ -381,10 +470,8 @@ By adding a span for a view controller when it's loaded, time to full display (T
381470

382471
Since Cocoa SDK version 8.33.0, the SDK doesn't create [time to initial display (TTID)](#time-to-initial-display) and time to full display (TTFD) spans for UIViewControllers presented in the background, because the logic requires UI frames to be drawn.
383472

384-
385473
_Time to full display is disabled by default, but you can enable it by setting:_
386474

387-
388475
```swift {tabTitle:Swift}
389476
import Sentry
390477

@@ -427,15 +514,14 @@ _(New in version 8.18.0), stable since 8.42.0._
427514

428515
We're working to update our **Performance** product offering in order to be able to provide better insights and highlight specific actions you can take to improve your mobile app's overall performance. The `performanceV2` option changes the following behavior:
429516

430-
* The app start duration will now finish when the first frame is drawn instead of when the OS posts the `UIWindowDidBecomeVisibleNotification`.
517+
- The app start duration will now finish when the first frame is drawn instead of when the OS posts the `UIWindowDidBecomeVisibleNotification`.
431518

432519
This change will be the default in the next major version.
433520

434521
## Opt Out
435522

436523
You can opt out of all automatic instrumentations using the options:
437524

438-
439525
```swift {tabTitle:Swift}
440526
import Sentry
441527

0 commit comments

Comments
 (0)