Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add extension for FileManager to track file I/O operations with Sentry #4863

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

philprime
Copy link
Contributor

@philprime philprime commented Feb 18, 2025

This PR is derived from #4605

It introduces an extension to FileManager with these methods:

  • createFileWithSentryTracing(atPath:contents:attributes:) mapped to createFile(atPath:contents:attributes:)
  • removeItemWithSentryTracing(at:) mapped to removeItem(at:)
  • removeItemWithSentryTracing(atPath:) mapped to removeItem(atPath:)
  • copyItemWithSentryTracing(at:to:) mapped to copyItem(at:to:)
  • copyItemWithSentryTracing(atPath:toPath:) mapped to copyItem(atPath:toPath)
  • moveItemWithSentryTracing(at:to:) mapped to moveItem(at:to:)
  • moveItemWithSentryTracing(atPath:toPath:) mapped to moveItem(atPath:toPath:)

Blocked by:

@philprime philprime self-assigned this Feb 18, 2025
Copy link

github-actions bot commented Feb 18, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 391c6d8

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

2 similar comments
Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

@philprime philprime changed the base branch from main to philprime/file-io-swizzling February 20, 2025 14:10
@philprime philprime force-pushed the philprime/manual-file-manager-tracking branch from 9bb9c45 to 95d1308 Compare February 20, 2025 14:25
@philprime philprime changed the base branch from philprime/file-io-swizzling to main February 20, 2025 14:25
@philprime philprime marked this pull request as ready for review February 20, 2025 15:21
Copy link

codecov bot commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 96.63512% with 32 lines in your changes missing coverage. Please review.

Project coverage is 92.338%. Comparing base (25f4e43) to head (391c6d8).

Files with missing lines Patch % Lines
...formance/IO/SentryFileIOTracker+SwiftHelpers.swift 72.649% 32 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #4863       +/-   ##
=============================================
+ Coverage   92.175%   92.338%   +0.163%     
=============================================
  Files          659       662        +3     
  Lines        77491     78435      +944     
  Branches     27260     28458     +1198     
=============================================
+ Hits         71428     72426      +998     
+ Misses        5968      5913       -55     
- Partials        95        96        +1     
Files with missing lines Coverage Δ
Sources/Sentry/SentryFileIOTracker.m 96.470% <100.000%> (+2.458%) ⬆️
...ons/Performance/IO/FileManager+SentryTracing.swift 100.000% <100.000%> (ø)
...rmance/IO/FileManagerTracingIntegrationTests.swift 100.000% <100.000%> (ø)
...formance/IO/SentryFileIOTracker+SwiftHelpers.swift 72.649% <72.649%> (ø)

... and 24 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 25f4e43...391c6d8. Read the comment docs.

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great; with a few improvements, this will be an excellent feature.

Comment on lines +19 to +22
let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("test-\(testName.hashValue.description)")
try! FileManager.default
.createDirectory(at: tempDir, withIntermediateDirectories: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Same as #4862 (comment).

Comment on lines +6 to +8
private class Fixture {

let data = "SOME DATA".data(using: .utf8)!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Same as #4862 (comment).

contents: data,
attributes: attr,
origin: SentryTraceOrigin.manualFileData) { path, data, attr in
self.createFile(atPath: path, contents: data, attributes: attr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: It would be great to have a test that fails when doing this because then we could seriously break users.

Suggested change
self.createFile(atPath: path, contents: data, attributes: attr)
self.createFile(atPath: path, contents: data, attributes: nil)

Comment on lines +132 to +133
XCTAssertEqual(span.data["file.path"] as? String, fixture.filePathToCreate)
XCTAssertNil(span.data["file.size"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Same as #4862 (comment)

Comment on lines +16 to +21
guard let span = self.span(forPath: url.path, origin: origin, operation: SentrySpanOperation.fileRead) else {
return try method(url, options)
}
defer {
span.finish()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Same as #4862 (comment) and this applies to all methods here.

/// - dstPath: The path at which to place the copy of `srcPath`.
/// This path must include the name of the file or directory in its new location.
/// - Note: See ``FileManager.copyItem(atPath:toPath:)`` for more information.
func copyItemWithSentryTracing(at srcPath: String, to dstPath: String) throws {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: This doesn't fully align with the naming of the FileManager API, if I'm not mistaken.

Suggested change
func copyItemWithSentryTracing(at srcPath: String, to dstPath: String) throws {
func copyItemWithSentryTracing(atPath srcPath: String, toPath dstPath: String) throws {

/// - dstPath: The new path for the item in `srcPath`.
/// This path must include the name of the file or directory in its new location.
/// - Note: See ``FileManager.moveItem(atPath:toPath:)`` for more information.
func moveItemWithSentryTracing(at srcPath: String, to dstPath: String) throws {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: Again please align with the FileManager naming

Suggested change
func moveItemWithSentryTracing(at srcPath: String, to dstPath: String) throws {
func moveItemWithSentryTracing(atPath srcPath: String, toPath dstPath: String) throws {

import SentryTestUtils
import XCTest

class FileManagerSentryTracingIntegrationTests: XCTestCase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: These tests are quite extensive. It would be great to add some more to validate that the FileManager operations work correctly when the SDK isn't started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

2 participants