Skip to content

Commit

Permalink
add trace origin to swift file
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Jan 13, 2025
1 parent 8a3a012 commit b03c19a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 91 deletions.
12 changes: 12 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@
A8F17B2E2901765900990B25 /* SentryRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F17B2D2901765900990B25 /* SentryRequest.m */; };
A8F17B342902870300990B25 /* SentryHttpStatusCodeRange.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F17B332902870300990B25 /* SentryHttpStatusCodeRange.m */; };
D4252AE32D119D3F00184F6F /* SentryDataWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4252AE22D119D3B00184F6F /* SentryDataWrapperTests.swift */; };
D48724DB2D352597005DE483 /* SentryTraceOrigins.swift in Sources */ = {isa = PBXBuildFile; fileRef = D48724DA2D352591005DE483 /* SentryTraceOrigins.swift */; };
D4AF00212D2E92FD00F5F3D7 /* SentryNSFileManagerSwizzling.m in Sources */ = {isa = PBXBuildFile; fileRef = D4AF00202D2E92FD00F5F3D7 /* SentryNSFileManagerSwizzling.m */; };
D4AF00232D2E931000F5F3D7 /* SentryNSFileManagerSwizzling.h in Headers */ = {isa = PBXBuildFile; fileRef = D4AF00222D2E931000F5F3D7 /* SentryNSFileManagerSwizzling.h */; };
D4AF00252D2E93C400F5F3D7 /* SentryNSFileManagerSwizzlingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D4AF00242D2E93C400F5F3D7 /* SentryNSFileManagerSwizzlingTests.m */; };
Expand Down Expand Up @@ -1878,6 +1879,7 @@
A8F17B2D2901765900990B25 /* SentryRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryRequest.m; sourceTree = "<group>"; };
A8F17B332902870300990B25 /* SentryHttpStatusCodeRange.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryHttpStatusCodeRange.m; sourceTree = "<group>"; };
D4252AE22D119D3B00184F6F /* SentryDataWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDataWrapperTests.swift; sourceTree = "<group>"; };
D48724DA2D352591005DE483 /* SentryTraceOrigins.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTraceOrigins.swift; sourceTree = "<group>"; };
D4AF00202D2E92FD00F5F3D7 /* SentryNSFileManagerSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryNSFileManagerSwizzling.m; sourceTree = "<group>"; };
D4AF00222D2E931000F5F3D7 /* SentryNSFileManagerSwizzling.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryNSFileManagerSwizzling.h; path = include/SentryNSFileManagerSwizzling.h; sourceTree = "<group>"; };
D4AF00242D2E93C400F5F3D7 /* SentryNSFileManagerSwizzlingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryNSFileManagerSwizzlingTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3660,6 +3662,14 @@
name = Transaction;
sourceTree = "<group>";
};
D48724D92D35258A005DE483 /* Transactions */ = {
isa = PBXGroup;
children = (
D48724DA2D352591005DE483 /* SentryTraceOrigins.swift */,
);
path = Transactions;
sourceTree = "<group>";
};
D4F2B5332D0C69CC00649E42 /* Recording */ = {
isa = PBXGroup;
children = (
Expand All @@ -3671,6 +3681,7 @@
D800942328F82E8D005D3943 /* Swift */ = {
isa = PBXGroup;
children = (
D48724D92D35258A005DE483 /* Transactions */,
D8CAC02D2BA0663E00E38F34 /* Integrations */,
621D9F2D2B9B030E003D94DE /* Helper */,
D8F016B42B962533007B9AFB /* Extensions */,
Expand Down Expand Up @@ -4899,6 +4910,7 @@
7BC9A20628F41781001E7C4C /* SentryMeasurementUnit.m in Sources */,
63FE71A020DA4C1100CDBAE8 /* SentryCrashInstallation.m in Sources */,
63FE713520DA4C1100CDBAE8 /* SentryCrashMemory.c in Sources */,
D48724DB2D352597005DE483 /* SentryTraceOrigins.swift in Sources */,
63FE714520DA4C1100CDBAE8 /* SentryCrashObjC.c in Sources */,
63FE710520DA4C1000CDBAE8 /* SentryAsyncSafeLog.c in Sources */,
0A2D8D5B289815C0008720F6 /* SentryBaseIntegration.m in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Sources/Sentry/include/SentryTraceOrigins.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ static NSString *const SentryTraceOriginUIEventTracker = @"auto.ui.event_tracker
static NSString *const SentryTraceOriginAutoAppStart = @"auto.app.start";
static NSString *const SentryTraceOriginAutoAppStartProfile = @"auto.app.start.profile";
static NSString *const SentryTraceOriginAutoNSData = @"auto.file.ns_data";
static NSString *const SentryTraceOriginManualData = @"manual.file.data";
static NSString *const SentryTraceOriginAutoDBCoreData = @"auto.db.core_data";
static NSString *const SentryTraceOriginAutoHttpNSURLSession = @"auto.http.ns_url_session";
static NSString *const SentryTraceOriginAutoUIViewController = @"auto.ui.view_controller";
Expand Down
4 changes: 2 additions & 2 deletions Sources/Swift/Helper/Data+SentryTracing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public extension Data {
self = try tracker
.measureNSData(
from: url,
origin: SentryTraceOriginManualData) { url, options, errorPtr in
origin: SentryTraceOrigin.manualData) { url, options, errorPtr in
do {
return try Data(contentsOf: url, options: options)
} catch {
Expand All @@ -41,7 +41,7 @@ public extension Data {
self,
writeTo: url,
options: options,
origin: SentryTraceOriginManualData) { url, options, errorPtr in
origin: SentryTraceOrigin.manualData) { url, options, errorPtr in
do {
try self.write(to: url, options: options)
return true
Expand Down
6 changes: 6 additions & 0 deletions Sources/Swift/Transactions/SentryTraceOrigins.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is the Swift addition for `SentryTraceOrigins.h` in the `Sentry` module.

@objcMembers
public class SentryTraceOrigin {
static let manualData = "manual.file.data"
}
72 changes: 36 additions & 36 deletions Tests/SentryTests/Helper/SentryDataWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SentryDataWrapperTests: XCTestCase {
fixture.data,
writeToFile: fixture.filePath,
atomically: false,
origin: SentryTraceOriginAutoNSData
origin: "custom.origin"
) { path, useAuxiliareFile -> Bool in
methodPath = path
methodAuxiliareFile = useAuxiliareFile
Expand All @@ -65,7 +65,7 @@ class SentryDataWrapperTests: XCTestCase {
XCTAssertFalse(result)

result = sut.measure(fixture.data, writeToFile: fixture.filePath, atomically: true,
origin: SentryTraceOriginAutoNSData) { _, useAuxiliareFile -> Bool in
origin: "custom.origin") { _, useAuxiliareFile -> Bool in
methodAuxiliareFile = useAuxiliareFile
return true
}
Expand All @@ -81,7 +81,7 @@ class SentryDataWrapperTests: XCTestCase {
var methodError: NSError?

try! sut.measure(fixture.data, writeToFile: fixture.filePath, options: .atomic,
origin: SentryTraceOriginAutoNSData) { path, writingOption, _ -> Bool in
origin: "custom.origin") { path, writingOption, _ -> Bool in
methodPath = path
methodOptions = writingOption
return true
Expand All @@ -92,7 +92,7 @@ class SentryDataWrapperTests: XCTestCase {

do {
try sut.measure(fixture.data, writeToFile: fixture.filePath, options: .withoutOverwriting,
origin: SentryTraceOriginAutoNSData) { _, writingOption, errorPointer -> Bool in
origin: "custom.origin") { _, writingOption, errorPointer -> Bool in
methodOptions = writingOption
errorPointer?.pointee = NSError(domain: "Test Error", code: -2, userInfo: nil)
return false
Expand All @@ -111,7 +111,7 @@ class SentryDataWrapperTests: XCTestCase {
var span: Span?

sut.measure(fixture.data, writeToFile: fixture.filePath, atomically: false,
origin: SentryTraceOriginAutoNSData) { _, _ -> Bool in
origin: "custom.origin") { _, _ -> Bool in
span = self.firstSpan(transaction)
XCTAssertFalse(span?.isFinished ?? true)
self.advanceTime(bySeconds: 4)
Expand All @@ -122,8 +122,8 @@ class SentryDataWrapperTests: XCTestCase {
assertDataSpan(
span,
path: fixture.filePath,
operation: SentrySpanOperationFileRead,
size: fixture.data.count
operation: SentrySpanOperationFileWrite,
size: fixture.data.count, origin: "custom.origin"
)
}

Expand All @@ -133,7 +133,7 @@ class SentryDataWrapperTests: XCTestCase {
var span: Span?

sut.measure(fixture.data, writeToFile: fixture.filePath, atomically: false,
origin: SentryTraceOriginAutoNSData) { _, _ -> Bool in
origin: "custom.origin") { _, _ -> Bool in
span = self.firstSpan(transaction)
XCTAssertFalse(span?.isFinished ?? true)
self.advanceTime(bySeconds: 4)
Expand All @@ -159,7 +159,7 @@ class SentryDataWrapperTests: XCTestCase {
var span: SentrySpan?

sut.measure(fixture.data, writeToFile: fixture.filePath, atomically: false,
origin: SentryTraceOriginAutoNSData) { _, _ -> Bool in
origin: "custom.origin") { _, _ -> Bool in
span = self.firstSpan(transaction) as? SentrySpan
XCTAssertFalse(span?.isFinished ?? true)
return true
Expand All @@ -178,15 +178,15 @@ class SentryDataWrapperTests: XCTestCase {
var span: Span?

sut.measure(self.fixture.data, writeToFile: self.fixture.filePath, atomically: false,
origin: SentryTraceOriginAutoNSData) { _, _ -> Bool in
origin: "custom.origin") { _, _ -> Bool in
span = self.firstSpan(transaction)
XCTAssertFalse(span?.isFinished ?? true)
self.advanceTime(bySeconds: 4)
return true
}

self.assertSpanDuration(span: span, expectedDuration: 4)
self.assertDataSpan(span, path: self.fixture.filePath, operation: SentrySpanOperationFileWrite, size: self.fixture.data.count, mainThread: false)
self.assertDataSpan(span, path: self.fixture.filePath, operation: SentrySpanOperationFileWrite, size: self.fixture.data.count, origin: "custom.origin", mainThread: false)
expect.fulfill()
}

Expand All @@ -199,7 +199,7 @@ class SentryDataWrapperTests: XCTestCase {
var span: Span?

try! sut.measure(fixture.data, writeToFile: fixture.filePath, options: .atomic,
origin: SentryTraceOriginAutoNSData) { _, _, _ -> Bool in
origin: "custom.origin") { _, _, _ -> Bool in
span = self.firstSpan(transaction)
XCTAssertFalse(span?.isFinished ?? true)
self.advanceTime(bySeconds: 3)
Expand All @@ -211,7 +211,7 @@ class SentryDataWrapperTests: XCTestCase {
span,
path: fixture.filePath,
operation: SentrySpanOperationFileWrite,
size: fixture.data.count
size: fixture.data.count, origin: "custom.origin"
)
}

Expand All @@ -222,7 +222,7 @@ class SentryDataWrapperTests: XCTestCase {

let expect = expectation(description: "")
try! sut.measure(fixture.data, writeToFile: fixture.sentryPath, options: .atomic,
origin: SentryTraceOriginAutoNSData) { _, _, _ -> Bool in
origin: "custom.origin") { _, _, _ -> Bool in
span = self.firstSpan(transaction)
expect.fulfill()
return true
Expand All @@ -239,7 +239,7 @@ class SentryDataWrapperTests: XCTestCase {
var usedPath: String?

let data = sut.measureNSData(fromFile: fixture.filePath,
origin: SentryTraceOriginAutoNSData) { path in
origin: "custom.origin") { path in
span = self.firstSpan(transaction)
usedPath = path
return self.fixture.data
Expand All @@ -252,7 +252,7 @@ class SentryDataWrapperTests: XCTestCase {
span,
path: fixture.filePath,
operation: SentrySpanOperationFileRead,
size: fixture.data.count
size: fixture.data.count, origin: "custom.origin"
)
}

Expand All @@ -264,7 +264,7 @@ class SentryDataWrapperTests: XCTestCase {
var usedOptions: NSData.ReadingOptions?

let data = try? sut.measureNSData(fromFile: self.fixture.filePath, options: .uncached,
origin: SentryTraceOriginAutoNSData) { path, options, _ -> Data in
origin: "custom.origin") { path, options, _ -> Data in
span = self.firstSpan(transaction)
usedOptions = options
usedPath = path
Expand All @@ -275,7 +275,7 @@ class SentryDataWrapperTests: XCTestCase {
XCTAssertEqual(data?.count, fixture.data.count)
XCTAssertEqual(usedOptions, .uncached)

assertDataSpan(span, path: fixture.filePath, operation: SentrySpanOperationFileRead, size: fixture.data.count)
assertDataSpan(span, path: fixture.filePath, operation: SentrySpanOperationFileRead, size: fixture.data.count, origin: "custom.origin")
}

func testReadFromURLOptionsError() {
Expand All @@ -287,7 +287,7 @@ class SentryDataWrapperTests: XCTestCase {
var usedOptions: NSData.ReadingOptions?

let data = try? sut.measureNSData(from: url, options: .uncached,
origin: SentryTraceOriginAutoNSData) { url, options, _ in
origin: "custom.origin") { url, options, _ in
span = self.firstSpan(transaction)
usedOptions = options
usedUrl = url
Expand All @@ -298,7 +298,7 @@ class SentryDataWrapperTests: XCTestCase {
XCTAssertEqual(data?.count, fixture.data.count)
XCTAssertEqual(usedOptions, .uncached)

assertDataSpan(span, path: url.path, operation: SentrySpanOperationFileRead, size: fixture.data.count)
assertDataSpan(span, path: url.path, operation: SentrySpanOperationFileRead, size: fixture.data.count, origin: "custom.origin")
}

func testDontTrackSentryFilesRead() {
Expand All @@ -308,7 +308,7 @@ class SentryDataWrapperTests: XCTestCase {

let expect = expectation(description: "")
let _ = sut.measureNSData(fromFile: fixture.sentryPath,
origin: SentryTraceOriginAutoNSData) { _ in
origin: "custom.origin") { _ in
span = self.firstSpan(transaction)
expect.fulfill()
return nil
Expand All @@ -325,37 +325,37 @@ class SentryDataWrapperTests: XCTestCase {
return result?.first
}

private func assertDataSpan(_ span: Span?, path: String, operation: String, size: Int, mainThread: Bool = true ) {
XCTAssertNotNil(span)
XCTAssertEqual(span?.operation, operation)
XCTAssertEqual(span?.origin, "auto.file.ns_data")
XCTAssertTrue(span?.isFinished ?? false)
XCTAssertEqual(span?.data["file.size"] as? Int, size)
XCTAssertEqual(span?.data["file.path"] as? String, path)
XCTAssertEqual(span?.data["blocked_main_thread"] as? Bool ?? false, mainThread)
private func assertDataSpan(_ span: Span?, path: String, operation: String, size: Int, origin: String, mainThread: Bool = true, file: StaticString = #file, line: UInt = #line) {
XCTAssertNotNil(span, file: file, line: line)
XCTAssertEqual(span?.operation, operation, file: file, line: line)
XCTAssertEqual(span?.origin, origin, file: file, line: line)
XCTAssertTrue(span?.isFinished ?? false, file: file, line: line)
XCTAssertEqual(span?.data["file.size"] as? Int, size, file: file, line: line)
XCTAssertEqual(span?.data["file.path"] as? String, path, file: file, line: line)
XCTAssertEqual(span?.data["blocked_main_thread"] as? Bool ?? false, mainThread, file: file, line: line)

if mainThread {
guard let frames = (span as? SentrySpan)?.frames else {
XCTFail("File IO Span in the main thread has no frames")
XCTFail("File IO Span in the main thread has no frames", file: file, line: line)
return
}
XCTAssertEqual(frames.first, TestData.mainFrame)
XCTAssertEqual(frames.last, TestData.testFrame)
XCTAssertEqual(frames.first, TestData.mainFrame, file: file, line: line)
XCTAssertEqual(frames.last, TestData.testFrame, file: file, line: line)
}

let lastComponent = (path as NSString).lastPathComponent

if operation == SentrySpanOperationFileRead {
XCTAssertEqual(span?.spanDescription, lastComponent)
XCTAssertEqual(span?.spanDescription, lastComponent, file: file, line: line)
} else {
let bytesDescription = SentryByteCountFormatter.bytesCountDescription( UInt(size))
XCTAssertEqual(span?.spanDescription ?? "", "\(lastComponent) (\(bytesDescription))")
XCTAssertEqual(span?.spanDescription ?? "", "\(lastComponent) (\(bytesDescription))", file: file, line: line)
}
}

private func assertSpanDuration(span: Span?, expectedDuration: TimeInterval) {
private func assertSpanDuration(span: Span?, expectedDuration: TimeInterval, file: StaticString = #file, line: UInt = #line) {
let duration = span?.timestamp?.timeIntervalSince(span!.startTimestamp!)
XCTAssertEqual(duration, expectedDuration)
XCTAssertEqual(duration, expectedDuration, file: file, line: line)
}

private func advanceTime(bySeconds: TimeInterval) {
Expand Down
Loading

0 comments on commit b03c19a

Please sign in to comment.