Skip to content

Commit b6823f9

Browse files
authored
Add scoped attributes to plcrashreport (#125)
* Add scoped attributes to plcrashreport * Use +=
1 parent 41814ae commit b6823f9

File tree

8 files changed

+56
-10
lines changed

8 files changed

+56
-10
lines changed

Sources/Features/Attributes/AttributesProvider.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,27 @@ extension AttributesProvider: SignalContext {
4444
self.faultInfo.faultMessage = faultMessage
4545
}
4646

47-
func set(errorType: String?) {
48-
self.attributes["error.type"] = errorType
49-
}
50-
5147
var attachmentPaths: [String] {
5248
return allAttachments.map(\.path)
5349
}
5450

5551
var allAttachments: Attachments {
5652
attachments + attributesSources.map(\.attachments).reduce([], +)
5753
}
54+
55+
var scopedAttributes: Attributes {
56+
return immutable + attributes;
57+
}
58+
var dynamicAttributes: Attributes {
59+
return attributesSources.map(\.mutable).merging()
60+
}
5861

5962
var allAttributes: Attributes {
6063
return attributes + defaultAttributes
6164
}
6265

6366
var defaultAttributes: Attributes {
64-
return immutable + attributesSources.map(\.mutable).merging()
67+
return immutable + dynamicAttributes
6568
}
6669
}
6770

Sources/Features/Attributes/DefaultAttributes.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import UIKit
1111

1212
final class FaultInfo: AttributesSource {
1313
var faultMessage: String?
14+
15+
var immutable: [String : Any?] {
16+
return ["error.type": "Crash"]
17+
}
1418
var mutable: [String: Any?] {
1519
return ["error.message": faultMessage]
1620
}

Sources/Features/Client/BacktraceReporter.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ extension BacktraceReporter: BacktraceClientCustomizing {
7676
return attributesProvider.attributes
7777
} set {
7878
attributesProvider.attributes = newValue
79+
80+
guard let attributeData = try? JSONSerialization.data(withJSONObject: attributesProvider.scopedAttributes) else {
81+
return
82+
}
83+
self.reporter.setCustomData(data: attributeData)
84+
7985
}
8086
}
8187

@@ -111,10 +117,11 @@ extension BacktraceReporter {
111117
func generate(exception: NSException? = nil, attachmentPaths: [String] = [],
112118
faultMessage: String? = nil) throws -> BacktraceReport {
113119
attributesProvider.set(faultMessage: faultMessage)
114-
attributesProvider.set(errorType: "Exception")
115120
let resource = try reporter.generateLiveReport(exception: exception,
116121
attributes: attributesProvider.allAttributes,
117122
attachmentPaths: attachmentPaths + attributesProvider.attachmentPaths)
123+
124+
resource.attributes["error.type"] = "Exception"
118125
return resource
119126
}
120127
}

Sources/Features/Extensions/Foundation+Extensions.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ extension Dictionary {
55
static func + (lhs: Dictionary, rhs: Dictionary) -> Dictionary {
66
return lhs.merging(rhs, uniquingKeysWith: {_, new in new})
77
}
8+
9+
static func += (left: inout Dictionary, right: Dictionary) {
10+
for (key, value) in right {
11+
left[key] = value
12+
}
13+
}
814
}
915

1016
// From: https://stackoverflow.com/a/57886995

Sources/Features/Repository/Model/BacktraceReport.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import CrashReporter
2323
self.attachmentPaths = attachmentPaths
2424
self.attributes = attributes
2525
super.init()
26+
27+
self.extendCrashAttributes()
2628
}
27-
29+
2830
init(managedObject: Crash) throws {
2931
guard let reportData = managedObject.reportData,
3032
let identifierString = managedObject.hashProperty,
@@ -37,7 +39,10 @@ import CrashReporter
3739
self.identifier = identifier
3840
self.attachmentPaths = attachmentPaths
3941
self.attributes = (try? AttributesStorage.retrieve(fileName: identifier.uuidString)) ?? [:]
42+
4043
super.init()
44+
45+
self.extendCrashAttributes()
4146
}
4247
}
4348

@@ -46,4 +51,18 @@ extension BacktraceReport: PersistentStorable {
4651
typealias ManagedObjectType = Crash
4752

4853
static var entityName: String { return "Crash" }
54+
55+
private func extendCrashAttributes() {
56+
guard let customData = self.plCrashReport.customData else {
57+
return
58+
}
59+
60+
if let attributes = try? JSONSerialization.jsonObject(with: customData, options: []) as? [String: Any] {
61+
self.attributes += attributes
62+
} else {
63+
return
64+
}
65+
}
66+
67+
4968
}

Sources/Public/BacktraceCrashReporter.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ extension BacktraceCrashReporter: CrashReporting {
3333
let signalInfo = signalInfoPointer?.pointee else {
3434
return
3535
}
36-
attributesProvider.set(errorType: "Crash")
36+
3737
attributesProvider.set(faultMessage: "siginfo_t.si_signo: \(signalInfo.si_signo)")
3838

39-
try? AttributesStorage.store(attributesProvider.allAttributes, fileName: BacktraceCrashReporter.crashName)
39+
try? AttributesStorage.store(attributesProvider.dynamicAttributes, fileName: BacktraceCrashReporter.crashName)
4040
try? AttachmentsStorage.store(attributesProvider.allAttachments, fileName: BacktraceCrashReporter.crashName)
4141
}
4242

@@ -60,10 +60,15 @@ extension BacktraceCrashReporter: CrashReporting {
6060
// This function retrieves, constructs, and sends the pending crash report
6161
func pendingCrashReport() throws -> BacktraceReport {
6262
let reportData = try reporter.loadPendingCrashReportDataAndReturnError()
63+
6364
let attributes = (try? AttributesStorage.retrieve(fileName: BacktraceCrashReporter.crashName)) ?? [:]
6465
let attachmentPaths = copiedFileAttachments.map(\.path)
6566
return try BacktraceReport(report: reportData, attributes: attributes, attachmentPaths: attachmentPaths)
6667
}
68+
69+
func setCustomData(data: Data) {
70+
self.reporter.customData = data
71+
}
6772

6873
// This function is called to copy stored file attachments
6974
// from pending crashes so that they are not overwritten by the

Sources/Public/Internal/CrashReporting.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ protocol CrashReporting {
88
func hasPendingCrashes() -> Bool
99
func enableCrashReporting() throws
1010
func signalContext(_ mutableContext: inout SignalContext)
11+
func setCustomData(data: Data) -> Void
1112
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Foundation
22

33
protocol SignalContext: CustomStringConvertible {
4+
var scopedAttributes: Attributes { get }
45
var allAttributes: Attributes { get }
6+
var dynamicAttributes: Attributes { get }
57
var allAttachments: Attachments { get }
68
var attributes: Attributes { get set }
79
// File attachments are stored to disk as URLs
810
var attachments: Attachments { get set }
911
// File attachments are used in `BacktraceReport` as string paths
1012
var attachmentPaths: [String] { get }
1113
func set(faultMessage: String?)
12-
func set(errorType: String?)
1314
}

0 commit comments

Comments
 (0)