-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@_implementationOnly import _SentryPrivate | ||
|
||
/// A ``Data`` extension that tracks read and write operations with Sentry. | ||
/// | ||
/// - Note: Methods provided by this extension reflect the same functionality as the original ``Data`` methods, but they track the operation with Sentry. | ||
public extension Data { | ||
|
||
// MARK: - Reading Data from a File | ||
|
||
/// Creates a data object from the data at the specified file URL, tracking the operation with Sentry. | ||
/// | ||
/// - Important: Using this method with auto-instrumentation for file operations enabled can lead to duplicate spans on older operating system versions. | ||
/// It is recommended to use either automatic or manual instrumentation. You can disable automatic instrumentation by setting | ||
/// `options.enableSwizzling` to `false` when initializing Sentry. | ||
/// - Parameters: | ||
/// - url: The location on disk of the data to read. | ||
/// - options: The mask specifying the options to use when reading the data. For more information, see ``NSData.ReadingOptions``. | ||
/// - Note: See ``Data.init(contentsOf:options:)`` for more information. | ||
init(contentsOfUrlWithSentryTracing url: URL, options: Data.ReadingOptions = []) throws { | ||
let tracker = SentryFileIOTracker.sharedInstance() | ||
self = try tracker | ||
.measureReadingData( | ||
from: url, | ||
options: options, | ||
origin: SentryTraceOrigin.manualFileData) { url, options in | ||
try Data(contentsOf: url, options: options) | ||
} | ||
} | ||
|
||
// MARK: - Writing Data to a File | ||
|
||
/// Write the contents of the `Data` to a location, tracking the operation with Sentry. | ||
/// | ||
/// - Important: Using this method with auto-instrumentation for file operations enabled can lead to duplicate spans on older operating system versions. | ||
/// It is recommended to use either automatic or manual instrumentation. You can disable automatic instrumentation by setting | ||
/// `options.enableSwizzling` to `false` when initializing Sentry. | ||
/// - Parameters: | ||
/// - url: The location to write the data into. | ||
/// - options: Options for writing the data. Default value is `[]`. | ||
/// - Note: See ``Data.write(to:options:)`` for more information. | ||
func writeWithSentryTracing(to url: URL, options: Data.WritingOptions = []) throws { | ||
let tracker = SentryFileIOTracker.sharedInstance() | ||
try tracker | ||
.measureWritingData( | ||
self, | ||
to: url, | ||
options: options, | ||
origin: SentryTraceOrigin.manualFileData) { data, url, options in | ||
try data.write(to: url, options: options) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@_implementationOnly import _SentryPrivate | ||
|
||
extension SentryFileIOTracker { | ||
func measureReadingData( | ||
from url: URL, | ||
options: Data.ReadingOptions, | ||
origin: String, | ||
method: (_ url: URL, _ options: Data.ReadingOptions) throws -> Data | ||
) rethrows -> Data { | ||
// 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. | ||
guard url.scheme == NSURLFileScheme else { | ||
return try method(url, options) | ||
} | ||
guard let span = self.span(forPath: url.path, origin: origin, operation: SentrySpanOperation.fileRead) else { | ||
return try method(url, options) | ||
} | ||
defer { | ||
span.finish() | ||
} | ||
let data = try method(url, options) | ||
span.setData(value: data.count, key: SentrySpanKey.fileSize) | ||
Check failure on line 23 in Sources/Swift/Integrations/Performance/IO/SentryFileIOTracker+SwiftHelpers.swift
|
||
return data | ||
} | ||
|
||
func measureWritingData( | ||
_ data: Data, | ||
to url: URL, | ||
options: Data.WritingOptions, | ||
origin: String, | ||
method: (_ data: Data, _ url: URL, _ options: Data.WritingOptions) throws -> Void | ||
) rethrows { | ||
// 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. | ||
guard url.scheme == NSURLFileScheme else { | ||
return try method(data, url, options) | ||
} | ||
guard let span = self.span(forPath: url.path, origin: origin, operation: SentrySpanOperation.fileWrite, size: UInt(data.count)) else { | ||
return try method(data, url, options) | ||
} | ||
defer { | ||
span.finish() | ||
} | ||
try method(data, url, options) | ||
} | ||
} |